parser_combinator

所属分类:collect
开发工具:Dart
文件大小:0KB
下载次数:0
上传日期:2023-08-03 22:59:23
上 传 者sh-1993
说明:  解析器组合符是一组解析器,可用于组合基本解析器以创建更复杂规则的解析器。,
(Parser combinator is a collection of parsers that can be used to combine basic parsers to create parsers for more complex rules.,)

文件列表:
.vscode/ (0, 2023-08-31)
.vscode/settings.json (72, 2023-08-31)
CHANGELOG.md (1759, 2023-08-31)
LICENSE (1487, 2023-08-31)
analysis_options.yaml (956, 2023-08-31)
example/ (0, 2023-08-31)
example/example.dart (2121, 2023-08-31)
example/example_trace.dart (1162, 2023-08-31)
example/localization_example.dart (1799, 2023-08-31)
lib/ (0, 2023-08-31)
lib/code_generator.dart (19600, 2023-08-31)
lib/extra/ (0, 2023-08-31)
lib/extra/csv_parser.dart (1609, 2023-08-31)
lib/extra/json_parser.dart (6515, 2023-08-31)
lib/file_reader.dart (1264, 2023-08-31)
lib/parser/ (0, 2023-08-31)
lib/parser/all_matches.dart (1474, 2023-08-31)
lib/parser/alpha.dart (3011, 2023-08-31)
lib/parser/alpha1.dart (2724, 2023-08-31)
lib/parser/and.dart (1492, 2023-08-31)
lib/parser/any.dart (887, 2023-08-31)
lib/parser/any_char.dart (1851, 2023-08-31)
lib/parser/buffered.dart (1327, 2023-08-31)
lib/parser/calc.dart (684, 2023-08-31)
lib/parser/chainl1.dart (1262, 2023-08-31)
lib/parser/char.dart (2155, 2023-08-31)
lib/parser/choice.dart (32743, 2023-08-31)
lib/parser/delimited.dart (2832, 2023-08-31)
lib/parser/digit.dart (2904, 2023-08-31)
lib/parser/digit1.dart (2643, 2023-08-31)
lib/parser/eof.dart (1618, 2023-08-31)
lib/parser/expected.dart (2367, 2023-08-31)
lib/parser/fast.dart (35532, 2023-08-31)
lib/parser/has_match.dart (1615, 2023-08-31)
lib/parser/malformed.dart (2562, 2023-08-31)
lib/parser/many.dart (2022, 2023-08-31)
lib/parser/many1.dart (2142, 2023-08-31)
lib/parser/many_m_n.dart (1595, 2023-08-31)
... ...

# parser_combinator Parser combinator is a collection of parsers that can be used to combine basic parsers to create parsers for more complex rules. Version: 0.2.9 ## About Parser combinator is intended for general purpose use. It can be used not only to implement parsers, but also to create validators. Combined parser do not require building and can be used immediately. The combined parser can be compiled into static code. Parsers declared as constants are evaluated at compile time. Simple and understandable localization of error messages. Fully customizable parsing error tracking system during development for maximum convenience. Very handy ways to track down parsing errors when tracing during development. Parsing character data not only from strings, but also from any other sources. ## Parsing The general rule is that complex parsers are made up of less complex parsers. The simpler some part of the parser, the easier it is to imagine how it should work. For certain purposes, you can write a custom parser that will parse more efficiently than a combined parser. Or, for example, you can implement a basic set for parsing some data (binary or that is directly in the file) and use them to parse such data. The simplest parsing example: ```dart void main(List args) { const id = Recognize2(Satisfy(isAlpha), SkipWhile(isAlphanumeric)); print(parseString(id.parse, 'Abc')); print(parseString(id.parse, 'xyz123')); print(parseString(calc.parse, '1 + 2 * 3')); print(parseString(calc.parse, '(1 + 2) * 3')); } const calc = _expr; const _add = ChainL1(_mul, _addOps, _mul, _toBinary); const _addOps = Terminated(Tags(['-', '+']), _ws); const _closeParenthesis = Terminated(Tag(')'), _ws); const _expr = Ref(_exprRef); const _mul = ChainL1(_primary, _mulOps, _primary, _toBinary); const _mulOps = Terminated(Tags(['*', '/']), _ws); const _number = Terminated(_number_, _ws); const _number_ = Map1(Recognize2(Opt(Tag('-')), Digit1()), num.parse); const _openParenthesis = Terminated(Tag('('), _ws); const _primary = Choice2(_number, Delimited(_openParenthesis, _expr, _closeParenthesis)); const _ws = SkipWhile(isWhitespace); Parser _exprRef() => _add; num _toBinary(num left, String op, num right) { return switch (op) { '-' => left - right, '+' => left + right, '*' => left * right, '/' => left * right, _ => throw ArgumentError.value(op, 'op'), }; } ``` ## Parsing from files An example of parsing from a text file. ```dart final file = File('test/temp.json'); final fileReader = FileReader(file.openSync(), bufferSize: 1024); final utf8Reader = Utf8Reader(fileReader); try { return parseInput(json_parser.parser.parse, utf8Reader); } finally { fileReader.fp.closeSync(); } ``` ## Localization Localization is implemented through the use of translation through hash tables. Localization is supported at the translation level of error messages and tags used in error messages. This approach uses two independent tables. One table for translating messages, the other for translating tags. Translation is performed only for data for which information for translation is provided. A complete example of the simplest parse localization. ```dart void main(List args) { parse(r'"abc\u123xyz"'); parse('{"abc": `123}'); parse('1.'); } void parse(String text) { print(text); try { parseString(parser, text, messages: _messages, tags: _tags); } catch (e) { print(e); print('-' * 40); } } final _tags = { 'decimal digit': 'десятичная цифра', 'number': 'число', 'string': 'строка', }; const _messages = { ParserErrorMessages.expected4DigitHexadecimalNumber: MessageLocalization(other: 'Ожидается 4-значное шестнадцатеричное число'), ErrorExpectedCharacter.message: MessageLocalization(other: 'Ожидается символ {0}'), ErrorExpectedEndOfInput.message: MessageLocalization(other: 'Ожидается конец входных данных'), ErrorExpectedIntegerValue.message: MessageLocalization(other: 'Ожидается целочисленное значение {0}'), ErrorExpectedTags.message: MessageLocalization( other: 'Ожидаются: {0}', one: 'Ожидается: {0}', ), ErrorUnexpectedCharacter.message: MessageLocalization(other: 'Неожиданный символ {0}'), ErrorUnexpectedEndOfInput.message: MessageLocalization(other: 'Неожиданный конец входных данных'), ErrorUnexpectedInput.message: MessageLocalization(other: 'Неожиданные входные данные'), ErrorUnknownError.message: MessageLocalization(other: 'Неизвестная ошибка'), }; ``` Example of displayed localized error messages (in Russian): ``` "abc\u123xyz" FormatException: line 1, column 10: Неожиданный символ 'x' (0x78) "abc\u123xyz" ^ line 1, column 7: Ожидается 4-значное шестнадцатеричное число "abc\u123xyz" ^^^ ---------------------------------------- {"abc": `123} FormatException: line 1, column 9: Ожидаются: '[', 'false', 'null', 'true', '{', 'строка', 'число' {"abc": `123} ^ ---------------------------------------- 1. FormatException: line 1, column 3: Неожиданный конец входных данных 1. ^ line 1, column 3: Ожидается: 'десятичная цифра' 1. ^ ---------------------------------------- ``` ## Tracing Tracing parsers is very easy. All you have to do is build a traceable parser. Building a traceable parser is also very easy. An example of how to build a traceable parser: ```dart final builder = TracerBuilder(fastParse: fastParse, parse: parse); final tracer = builder.build(parser); ``` Tracer is the same regular parser that will invoke the appropriate methods (`fastParse` and `parse`) of all traced parsers (that is, it will trace the entire parsing process). An example of how to trace parsing: ```dart Result? parse(Parser parser, State state) { stack.add(parser); if (bps.check(name: parser.name)) { print('breakpoint'); } final result = parser.parse(state); stack.removeLast(); return result; } ``` A complete example of a simple parse trace: ```dart void main(List args) { final bps = Breakpoints(); bps.add(name: 'fraction'); bps.add(name: 'integer'); final stack = []; bool fastParse(Parser parser, State state) { stack.add(parser); if (bps.check(name: parser.name)) { print('breakpoint'); } final result = parser.fastParse(state); stack.removeLast(); return result; } Result? parse(Parser parser, State state) { stack.add(parser); if (bps.check(name: parser.name)) { print('breakpoint'); } final result = parser.parse(state); stack.removeLast(); return result; } final builder = TracerBuilder(fastParse: fastParse, parse: parse); final tracer = builder.build(example.parser); final result = tracer.parseString('1.'); print(result); } ``` ## Generating source code from code It is possible to generate valid code from most parsers. An example of generated code. ```dart bool _tag2(State state) { const p = _i1.Tag.parseTag; const tag = 'false'; return p(state, tag); } bool _terminated2(State state) { final pos = state.pos; final r1 = _tag2(state); if (r1) { final r2 = _ws(state); if (r2) { return true; } state.pos = pos; } return false; } Result? _false(State state) { const value = false; final r = _terminated2(state); if (r) { return Result(value); } return null; } ``` An example of how the code can be generated can be found in the `tool` directory.

近期下载者

相关文件


收藏者