d_simple_parser 1.0.7

  • Readme
  • Changelog
  • Example
  • Installing
  • 49

A simple and easy to use text parsing dart library,d_simple_parser comes with elegant parsers that you can use to build complex and more elegant parsers.

Usage #

A simple usage example:

import 'package:d_simple_parser/d_simple_parser.dart';

void main() {
  
    print((char('a') | char('b')).seq(char('c')).firstStringMatch('abc')); // bc
           // is the same as writing
    print(char('a').or(char('b')).seq(char('c')).firstStringMatch('abc')); // bc
           // is the same as writing
    print(((char('a') | char('b')) & char('c')).firstStringMatch('abc')); // bc


  
    print(char('a').seq(spaceOptional()).seq(char('b')).allStringMatches('ab a b a    b')); // [ab, a b, a    b]
  
    print(letter().seq(space()).seq(digit()).firstStringMatch('Yes!, I agree , year 2019 was not the best year for me.')); // r 2
    // is the same as writing
    print(pattern('a-zA-Z').seq(char(' ').plus()).seq(pattern('0-9')).firstStringMatch('Yes!, I agree , year 2019 was not the best year for me.'));// r 2
    
    print(letter().plus().seq(space()).seq(digit().plus()).firstStringMatch('Yes!, I agree , year 2019 was not the best year for me.')); // year 2019
    // is the same as writing
    print(pattern('a-zA-Z').plus().seq(char(' ').plus()).seq(pattern('0-9').plus()).firstStringMatch('Yes!, I agree , year 2019 was not the best year for me.')); // year 2019

  
    //The almighty any(end,except) function
    var awesomeParser = string('awesome',caseSensitive: false).seq(char('!'));
    print(any(end: awesomeParser).firstStringMatch('I am Awesome!')); // I
    print(any(end: awesomeParser).plus().firstStringMatch('I am Awesome!')); // I am 
    print(any(end: awesomeParser).plus().seq(awesomeParser).firstStringMatch('I am Awesome!')); // I am Awesome!

    final startTagParser = char('<').seq(any(end: char('>'),except: '<>').plus()).seq(char('>'));
    final endTagParser = char('<').seq(word().plus()).seq(char('/')).seq(spaceOptional()).seq(char('>'));
    final elementParser = startTagParser.seq(any(end: endTagParser).plus()).seq(endTagParser);
    print(startTagParser.firstStringMatch('<i>42<i/>')); // <i>
    print(endTagParser.firstStringMatch('<i>42<i/>')); // <i/>
    print(elementParser.firstStringMatch('<i>42<i/>')); // <i>42<i/>

  
    print(allInput().firstStringMatch('Hello world!')); // Hello world!
  
    //hasMatch method
    print(char('a').hasMatch('bc')); // false
    print(char('b').hasMatch('bc')); // true
    print(char('c').hasMatch('bc')); // true

    //replaceIn method
    print(char('b').replaceIn(input: 'bc', replacement: 'ab')); // abc
        //replace multiple spaces with a single space
    print(space().replaceIn(input: 'I         am         Awesome!  ', replacement: ' ')); // I am Awesome!

    //replaceInMapped
    print(letter().replaceInMapped(input: 'abc', replace: (match){
      return match == 'b' ? 'B' : match;
    })); //aBc
 
    //removeFrom method
    print(char('a').seq(char('b')).seq(char('c')).removeFrom('abcdefg')); // defg

   //repeat method
   print(char('a').repeat(min:2,max:3).allStringMatches('aaaaaa aa aaa aa aaaaaa aaa a aa')); // [aaa, aaa, aa, aaa, aa, aaa, aaa, aaa, aa]

   //Lastly, A parser that parses most of HTML and XML elements

  final input = '''<k>
           <tag attr1="attribute1"> Text </tag>
           <TAG> TEXT </TAG>
           <i></i>
           <b/>
           <v href="qwety"/></k>
        ''';

 var anyElement = (spaceOptional().seq(spaceOptional().seq(char('<').seq(any(end:char('>'),except:'/').star()).seq(char('>'))).seq(spaceOptional()) & (spaceOptional().seq(spaceOptional().seq (char('<').seq(any(end:char('>'),except:'/').star()).seq(char('>')))
     .seq(spaceOptional()).seq((spaceOptional().seq(spaceOptional().seq (char('<').seq(any(end:char('>'),except:'/').star()).seq(char('>')))
     .seq(spaceOptional()).seq(any(end:spaceOptional().seq(char('<').seq(char('/')).seq(spaceOptional()).seq(word().plus()).seq(spaceOptional()).seq(char('>')))
     .seq(spaceOptional()),except:'<>').plus()).seq(spaceOptional().seq(char('<').seq(char('/')).seq(spaceOptional()).seq(word().plus()).seq(spaceOptional()).seq(char('>')))
     .seq(spaceOptional()))).seq(spaceOptional()) | spaceOptional().seq(spaceOptional().seq (char('<').seq(any(end:char('>'),except:'/').star()).seq(char('>'))).seq(spaceOptional()).seq(spaceOptional()).seq(spaceOptional().seq(char('<').seq(char('/')).seq(spaceOptional()).seq(word().plus()).seq(spaceOptional()).seq(char('>')))
     .seq(spaceOptional()))).seq(spaceOptional()) | spaceOptional().seq(char('<').seq(any(end:char('/').seq(char('>')),except:'<>').plus()).seq(char('/').seq(char('>'))))
     .seq(spaceOptional())).plus()).seq(spaceOptional().seq(char('<').seq(char('/')).seq(spaceOptional()).seq(word().plus()).seq(spaceOptional()).seq(char('>'))).seq(spaceOptional()))).seq(spaceOptional())  | spaceOptional().seq(spaceOptional().seq (char('<').seq(any(end:char('>'),except:'/').star()).seq(char('>')))
     .seq(spaceOptional()).seq(any(end:spaceOptional().seq(char('<').seq(char('/')).seq(spaceOptional()).seq(word().plus()).seq(spaceOptional()).seq(char('>'))).seq(spaceOptional()),except:'<>').plus()).seq(spaceOptional().seq(char('<').seq(char('/')).seq(spaceOptional()).seq(word().plus()).seq(spaceOptional()).seq(char('>'))).seq(spaceOptional())))
     .seq(spaceOptional()) | spaceOptional().seq(spaceOptional().seq (char('<').seq(any(end:char('>'),except:'/').star()).seq(char('>'))).seq(spaceOptional()).seq(spaceOptional()).seq(spaceOptional().seq(char('<').seq(char('/')).seq(spaceOptional()).seq(word().plus()).seq(spaceOptional()).seq(char('>'))).seq(spaceOptional())))
     .seq(spaceOptional()) | spaceOptional().seq(char('<').seq(any(end:char('/').seq(char('>')),except:'<>').plus()).seq(char('/').seq(char('>')))).seq(spaceOptional()) | spaceOptional().seq (char('<').seq(any(end:char('>'),except:'/').star()).seq(char('>'))).seq(spaceOptional()) | word().plus() | char('<').seq(char('!')).seq(char('-')).seq(char('-'))
     .seq(any(end:char('-').seq(char('-')).seq(char('>'))).star()).seq(char('-').seq(char('-')).seq(char('>'))))
     .star() & spaceOptional().seq(char('<').seq(char('/')).seq(spaceOptional()).seq(word().plus()).seq(spaceOptional()).seq(char('>')))
     .seq(spaceOptional())).seq(spaceOptional())).or(spaceOptional().seq(char('<').seq(any(end:char('/').seq(char('>')),except:'<>').plus()).seq(char('/').seq(char('>'))))
     .seq(spaceOptional()) | spaceOptional().seq (char('<').seq(any(end:char('>'),except:'/').star()).seq(char('>'))).seq(spaceOptional()) | char('<').seq(char('!')).seq(char('-')).seq(char('-')).seq(any(end:char('-').seq(char('-')).seq(char('>'))).star()).seq(char('-').seq(char('-')).seq(char('>'))));

 print(anyElement.allStringMatches(input)); // [<k>,            <tag attr1="attribute1"> Text </tag>,            <TAG> TEXT </TAG>,            <i></i>,            <b/>,            <v href="qwety"/>]

   

}

For more examples on how to use d_simple_parser see d_simple_parser_test.dart

1.0.7 #

- Minor performance improvements

1.0.6 #

- space_parsers improved

1.0.5 #

- Minor changes

1.0.4 #

- Added replaceInMapped method

1.0.3 #

- Added more examples

1.0.2 #

- Version 1.0.2

1.0.1 #

- source code formant

1.0.0 #

- Initial version, created by Stagehand

example/d_simple_parser_example.dart

/**
 * Copyright 2020 - MagabeLab (Tanzania). All Rights Reserved.
 * Author Edwin Magabe    edyma50@yahoo.com
 */

import 'package:d_simple_parser/d_simple_parser.dart';

void main() {




  final input = '''<k>
           <tag attr1="attribute1"> Text </tag>
           <TAG> TEXT </TAG>
           <i></i>
           <b/>
           <v href="qwety"/></k>
        ''';

 var anyElement = (spaceOptional().seq(spaceOptional().seq(char('<').seq(any(end:char('>'),except:'/').star()).seq(char('>'))).seq(spaceOptional()) & (spaceOptional().seq(spaceOptional().seq (char('<').seq(any(end:char('>'),except:'/').star()).seq(char('>')))
     .seq(spaceOptional()).seq((spaceOptional().seq(spaceOptional().seq (char('<').seq(any(end:char('>'),except:'/').star()).seq(char('>')))
     .seq(spaceOptional()).seq(any(end:spaceOptional().seq(char('<').seq(char('/')).seq(spaceOptional()).seq(word().plus()).seq(spaceOptional()).seq(char('>')))
     .seq(spaceOptional()),except:'<>').plus()).seq(spaceOptional().seq(char('<').seq(char('/')).seq(spaceOptional()).seq(word().plus()).seq(spaceOptional()).seq(char('>')))
     .seq(spaceOptional()))).seq(spaceOptional()) | spaceOptional().seq(spaceOptional().seq (char('<').seq(any(end:char('>'),except:'/').star()).seq(char('>'))).seq(spaceOptional()).seq(spaceOptional()).seq(spaceOptional().seq(char('<').seq(char('/')).seq(spaceOptional()).seq(word().plus()).seq(spaceOptional()).seq(char('>')))
     .seq(spaceOptional()))).seq(spaceOptional()) | spaceOptional().seq(char('<').seq(any(end:char('/').seq(char('>')),except:'<>').plus()).seq(char('/').seq(char('>'))))
     .seq(spaceOptional())).plus()).seq(spaceOptional().seq(char('<').seq(char('/')).seq(spaceOptional()).seq(word().plus()).seq(spaceOptional()).seq(char('>'))).seq(spaceOptional()))).seq(spaceOptional())  | spaceOptional().seq(spaceOptional().seq (char('<').seq(any(end:char('>'),except:'/').star()).seq(char('>')))
     .seq(spaceOptional()).seq(any(end:spaceOptional().seq(char('<').seq(char('/')).seq(spaceOptional()).seq(word().plus()).seq(spaceOptional()).seq(char('>'))).seq(spaceOptional()),except:'<>').plus()).seq(spaceOptional().seq(char('<').seq(char('/')).seq(spaceOptional()).seq(word().plus()).seq(spaceOptional()).seq(char('>'))).seq(spaceOptional())))
     .seq(spaceOptional()) | spaceOptional().seq(spaceOptional().seq (char('<').seq(any(end:char('>'),except:'/').star()).seq(char('>'))).seq(spaceOptional()).seq(spaceOptional()).seq(spaceOptional().seq(char('<').seq(char('/')).seq(spaceOptional()).seq(word().plus()).seq(spaceOptional()).seq(char('>'))).seq(spaceOptional())))
     .seq(spaceOptional()) | spaceOptional().seq(char('<').seq(any(end:char('/').seq(char('>')),except:'<>').plus()).seq(char('/').seq(char('>')))).seq(spaceOptional()) | spaceOptional().seq (char('<').seq(any(end:char('>'),except:'/').star()).seq(char('>'))).seq(spaceOptional()) | word().plus() | char('<').seq(char('!')).seq(char('-')).seq(char('-'))
     .seq(any(end:char('-').seq(char('-')).seq(char('>'))).star()).seq(char('-').seq(char('-')).seq(char('>'))))
     .star() & spaceOptional().seq(char('<').seq(char('/')).seq(spaceOptional()).seq(word().plus()).seq(spaceOptional()).seq(char('>')))
     .seq(spaceOptional())).seq(spaceOptional())).or(spaceOptional().seq(char('<').seq(any(end:char('/').seq(char('>')),except:'<>').plus()).seq(char('/').seq(char('>'))))
     .seq(spaceOptional()) | spaceOptional().seq (char('<').seq(any(end:char('>'),except:'/').star()).seq(char('>'))).seq(spaceOptional()) | char('<').seq(char('!')).seq(char('-')).seq(char('-')).seq(any(end:char('-').seq(char('-')).seq(char('>'))).star()).seq(char('-').seq(char('-')).seq(char('>'))));

 print(anyElement.allStringMatches(input));

}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  d_simple_parser: ^1.0.7

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:d_simple_parser/d_simple_parser.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
96
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
49
Learn more about scoring.

We analyzed this package on Jul 3, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.13

Health suggestions

Fix lib/core/core.dart. (-0.50 points)

Analysis of lib/core/core.dart reported 1 hint:

line 1 col 1: Prefer using /// for doc comments.

Fix lib/core/parser.dart. (-0.50 points)

Analysis of lib/core/parser.dart reported 1 hint:

line 1 col 1: Prefer using /// for doc comments.

Fix lib/parsers/all_input_parser.dart. (-0.50 points)

Analysis of lib/parsers/all_input_parser.dart reported 1 hint:

line 1 col 1: Prefer using /// for doc comments.

Fix additional 6 files with analysis or formatting issues. (-3 points)

Additional issues in the following files:

  • lib/parsers/any_character_parser.dart (1 hint)
  • lib/parsers/char_parsers.dart (1 hint)
  • lib/parsers/parsers.dart (1 hint)
  • lib/parsers/pattern_parsers.dart (1 hint)
  • lib/parsers/space_parsers.dart (1 hint)
  • lib/parsers/string_parser.dart (1 hint)

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.5.0 <3.0.0
meta ^1.1.8 1.1.8
Dev dependencies
pedantic ^1.8.0
test ^1.6.0