Skip to content

Commit

Permalink
Dart 3.4 dependency and fix linter issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
renggli committed May 25, 2024
1 parent 16e7d2c commit 74a88b9
Show file tree
Hide file tree
Showing 24 changed files with 150 additions and 109 deletions.
2 changes: 2 additions & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ linter:
- comment_references
- directives_ordering
- invalid_case_patterns
- missing_code_block_language_in_doc_comment
- no_self_assignments
- no_wildcard_variable_uses
- omit_local_variable_types
Expand All @@ -33,6 +34,7 @@ linter:
- unnecessary_await_in_return
- unnecessary_breaks
- unnecessary_lambdas
- unnecessary_library_name
- unnecessary_null_aware_operator_on_extension_on_nullable
- unnecessary_null_checks
- unnecessary_parenthesis
Expand Down
2 changes: 1 addition & 1 deletion example/calc.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// Calculator from the tutorial.
library calc;
library;

import 'dart:io';
import 'dart:math';
Expand Down
2 changes: 1 addition & 1 deletion lib/context.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// This package contains the representation of parse results.
@Deprecated('Use `import petitparser:core` instead')
library context;
library;

export 'src/core/context.dart';
export 'src/core/result.dart';
2 changes: 1 addition & 1 deletion lib/core.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/// This package contains the core classes of the framework.
///
/// {@canonicalFor parser.Parser}
library core;
library;

export 'src/core/context.dart';
export 'src/core/exception.dart';
Expand Down
2 changes: 1 addition & 1 deletion lib/debug.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// This package contains some simple debugging tools.
library debug;
library;

export 'src/debug/profile.dart';
export 'src/debug/progress.dart';
Expand Down
2 changes: 1 addition & 1 deletion lib/definition.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// This package simplifies the creation of complicated recursive grammars.
library definition;
library;

export 'src/definition/grammar.dart';
export 'src/definition/parser.dart';
Expand Down
2 changes: 1 addition & 1 deletion lib/expression.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// This package simplifies the creation of expression parsers.
library expression;
library;

export 'src/expression/builder.dart';
export 'src/expression/group.dart';
2 changes: 1 addition & 1 deletion lib/indent.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/// This package simplifies the creation of indention based parsers.
library indent;
library;

export 'src/indent/indent.dart';
2 changes: 1 addition & 1 deletion lib/matcher.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// This package contains helpers to simplify parsing and data extraction.
library matcher;
library;

export 'src/matcher/accept.dart';
export 'src/matcher/matches.dart';
Expand Down
2 changes: 1 addition & 1 deletion lib/parser.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// This package contains the standard parser implementations.
library parser;
library;

export 'src/core/parser.dart';
export 'src/parser/action/cast.dart';
Expand Down
2 changes: 1 addition & 1 deletion lib/petitparser.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// This package exports the core library of PetitParser, a dynamic parser
/// combinator framework.
library petitparser;
library;

export 'core.dart';
export 'definition.dart';
Expand Down
2 changes: 1 addition & 1 deletion lib/reflection.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// This package contains tools to reflect on and transform parsers.
library reflection;
library;

export 'src/reflection/analyzer.dart';
export 'src/reflection/iterable.dart';
Expand Down
8 changes: 5 additions & 3 deletions lib/src/core/parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,11 @@ abstract class Parser<R> {
/// parser that accepts a digit. The resulting `example` parser accepts one
/// or more digits.
///
/// final letter = letter();
/// final example = letter.plus();
/// example.replace(letter, digit());
/// ```dart
/// final letter = letter();
/// final example = letter.plus();
/// example.replace(letter, digit());
/// ```
///
/// Override this method and [Parser.children] in all subclasses that
/// reference other parsers.
Expand Down
8 changes: 6 additions & 2 deletions lib/src/debug/profile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,19 @@ import '../shared/types.dart';
///
/// For example, the snippet
///
/// final parser = letter() & word().star();
/// profile(parser).parse('f1234567890');
/// ```dart
/// final parser = letter() & word().star();
/// profile(parser).parse('f1234567890');
/// ```
///
/// prints the following output:
///
/// ```text
/// 1 2006 SequenceParser
/// 1 697 PossessiveRepeatingParser[0..*]
/// 11 406 SingleCharacterParser[letter or digit expected]
/// 1 947 SingleCharacterParser[letter expected]
/// ```
///
/// The first number refers to the number of activations of each parser, and
/// the second number is the microseconds spent in this parser and all its
Expand Down
22 changes: 13 additions & 9 deletions lib/src/debug/progress.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,22 @@ import '../shared/types.dart';
///
/// For example, the snippet
///
/// final parser = letter() & word().star();
/// progress(parser).parse('f123');
/// ```dart
/// final parser = letter() & word().star();
/// progress(parser).parse('f123');
/// ```
///
/// prints the following output:
///
/// * SequenceParser
/// * SingleCharacterParser[letter expected]
/// ** PossessiveRepeatingParser[0..*]
/// ** SingleCharacterParser[letter or digit expected]
/// *** SingleCharacterParser[letter or digit expected]
/// **** SingleCharacterParser[letter or digit expected]
/// ***** SingleCharacterParser[letter or digit expected]
/// ```text
/// * SequenceParser
/// * SingleCharacterParser[letter expected]
/// ** PossessiveRepeatingParser[0..*]
/// ** SingleCharacterParser[letter or digit expected]
/// *** SingleCharacterParser[letter or digit expected]
/// **** SingleCharacterParser[letter or digit expected]
/// ***** SingleCharacterParser[letter or digit expected]
/// ```
///
/// Jumps backwards mean that the parser is back-tracking. Often choices can
/// be reordered to avoid such expensive parses.
Expand Down
28 changes: 16 additions & 12 deletions lib/src/debug/trace.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,25 @@ import '../shared/types.dart';
///
/// For example, the snippet
///
/// final parser = letter() & word().star();
/// trace(parser).parse('f1');
/// ```dart
/// final parser = letter() & word().star();
/// trace(parser).parse('f1');
/// ```
///
/// produces the following output:
///
/// SequenceParser<dynamic>
/// SingleCharacterParser[letter expected]
/// Success[1:2]: f
/// PossessiveRepeatingParser<String>[0..*]
/// SingleCharacterParser[letter or digit expected]
/// Success[1:3]: 1
/// SingleCharacterParser[letter or digit expected]
/// Failure[1:3]: letter or digit expected
/// Success[1:3]: [1]
/// Success[1:3]: [f, [1]]
/// ```text
/// SequenceParser<dynamic>
/// SingleCharacterParser[letter expected]
/// Success[1:2]: f
/// PossessiveRepeatingParser<String>[0..*]
/// SingleCharacterParser[letter or digit expected]
/// Success[1:3]: 1
/// SingleCharacterParser[letter or digit expected]
/// Failure[1:3]: letter or digit expected
/// Success[1:3]: [1]
/// Success[1:3]: [f, [1]]
/// ```
///
/// Indentation signifies the activation of a parser object. Reverse indentation
/// signifies the returning of a parse result either with a success or failure
Expand Down
47 changes: 27 additions & 20 deletions lib/src/definition/grammar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ import 'resolve.dart';
///
/// Consider the following example to parse a list of numbers:
///
/// class ListGrammarDefinition extends GrammarDefinition {
/// Parser start() => ref0(list).end();
/// Parser list() => ref0(element) & char(',') & ref0(list)
/// | ref0(element);
/// Parser element() => digit().plus().flatten();
/// }
/// ```dart
/// class ListGrammarDefinition extends GrammarDefinition {
/// Parser start() => ref0(list).end();
/// Parser list() => ref0(element) & char(',') & ref0(list)
/// | ref0(element);
/// Parser element() => digit().plus().flatten();
/// }
/// ```
///
/// Since this is plain Dart code, common refactorings such as renaming a
/// production updates all references correctly. Also code navigation and code
Expand All @@ -30,35 +32,40 @@ import 'resolve.dart';
/// grammar definition and override overriding the necessary productions defined
/// in the superclass:
///
/// class ListParserDefinition extends ListGrammarDefinition {
/// Parser element() => super.element().map((value) => int.parse(value));
/// }
/// ```dart
/// class ListParserDefinition extends ListGrammarDefinition {
/// Parser element() => super.element().map((value) => int.parse(value));
/// }
/// ```
///
/// Note that productions can be parametrized. Define such productions with
/// positional arguments, and refer to them using [ref1], [ref2], ... where
/// the number corresponds to the argument count.
///
/// Consider extending the above grammar with a parametrized token production:
///
/// class TokenizedListGrammarDefinition extends GrammarDefinition {
/// Parser start() => ref0(list).end();
/// Parser list() => ref0(element) & ref1(token, char(',')) & ref0(list)
/// | ref0(element);
/// Parser element() => ref1(token, digit().plus());
/// Parser token(Parser parser) => parser.token().trim();
/// }
/// ```dart
/// class TokenizedListGrammarDefinition extends GrammarDefinition {
/// Parser start() => ref0(list).end();
/// Parser list() => ref0(element) & ref1(token, char(',')) & ref0(list)
/// | ref0(element);
/// Parser element() => ref1(token, digit().plus());
/// Parser token(Parser parser) => parser.token().trim();
/// }
/// ```
///
/// To get a runnable parser call the [build] method on the definition. It
/// resolves recursive references and returns an efficient parser that can be
/// further composed. The optional `start` reference specifies a different
/// starting production within the grammar. The optional `arguments`
/// parametrize the start production.
///
/// final parser = new ListParserDefinition().build();
///
/// parser.parse('1'); // [1]
/// parser.parse('1,2,3'); // [1, 2, 3]
/// ```dart
/// final parser = new ListParserDefinition().build();
///
/// parser.parse('1'); // [1]
/// parser.parse('1,2,3'); // [1, 2, 3]
/// ```
@optionalTypeArgs
abstract class GrammarDefinition<R> {
const GrammarDefinition();
Expand Down
67 changes: 39 additions & 28 deletions lib/src/expression/builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,60 +12,71 @@ import 'utils.dart';
/// The following code creates the empty expression builder producing values of
/// type [num]:
///
/// final builder = ExpressionBuilder<num>();
/// ```dart
/// final builder = ExpressionBuilder<num>();
/// ```
///
/// Every [ExpressionBuilder] needs to define at least one primitive type to
/// parse. In this example these are the literal numbers. The mapping function
/// converts the string input into an actual number.
///
/// builder.primitive(digit()
/// .plus()
/// .seq(char('.').seq(digit().plus()).optional())
/// .flatten()
/// .trim()
/// .map(num.parse));
/// ```dart
/// builder.primitive(digit()
/// .plus()
/// .seq(char('.').seq(digit().plus()).optional())
/// .flatten()
/// .trim()
/// .map(num.parse));
/// ```dart
///
/// Then we define the operator-groups in descending precedence. The highest
/// precedence have parentheses. The mapping function receives both the opening
/// parenthesis, the value, and the closing parenthesis as arguments:
///
/// builder.group().wrapper(
/// char('(').trim(), char(')').trim(), (left, value, right) => value);
/// ```dart
/// builder.group().wrapper(
/// char('(').trim(), char(')').trim(), (left, value, right) => value);
/// ```
///
/// Then come the normal arithmetic operators. We are using
/// [cascade notation](https://dart.dev/guides/language/language-tour#cascade-notation)
/// to define multiple operators on the same precedence-group. The mapping
/// functions receive both, the terms and the parsed operator in the order they
/// appear in the parsed input:
///
/// // Negation is a prefix operator.
/// builder.group().prefix(char('-').trim(), (operator, value) => -value);
/// ```dart
/// // Negation is a prefix operator.
/// builder.group().prefix(char('-').trim(), (operator, value) => -value);
///
/// // Power is right-associative.
/// builder.group().right(char('^').trim(), (left, operator, right) => math.pow(left, right));
/// // Power is right-associative.
/// builder.group().right(char('^').trim(), (left, operator, right) => math.pow(left, right));
///
/// // Multiplication and addition are left-associative, multiplication has
/// // higher priority than addition.
/// builder.group()
/// ..left(char('*').trim(), (left, operator, right) => left * right)
/// ..left(char('/').trim(), (left, operator, right) => left / right);
/// builder.group()
/// ..left(char('+').trim(), (left, operator, right) => left + right)
/// ..left(char('-').trim(), (left, operator, right) => left - right);
/// // Multiplication and addition are left-associative, multiplication has
/// // higher priority than addition.
/// builder.group()
/// ..left(char('*').trim(), (left, operator, right) => left * right)
/// ..left(char('/').trim(), (left, operator, right) => left / right);
/// builder.group()
/// ..left(char('+').trim(), (left, operator, right) => left + right)
/// ..left(char('-').trim(), (left, operator, right) => left - right);
/// ```
///
/// Finally we can build the parser:
///
/// final parser = builder.build();
/// ```dart
/// final parser = builder.build();
/// ```
///
/// After executing the above code we get an efficient parser that correctly
/// evaluates expressions like:
///
/// parser.parse('-8'); // -8
/// parser.parse('1+2*3'); // 7
/// parser.parse('1*2+3'); // 5
/// parser.parse('8/4/2'); // 2
/// parser.parse('2^2^3'); // 256
///
/// ```dart
/// parser.parse('-8'); // -8
/// parser.parse('1+2*3'); // 7
/// parser.parse('1*2+3'); // 5
/// parser.parse('8/4/2'); // 2
/// parser.parse('2^2^3'); // 256
/// ```
class ExpressionBuilder<T> {
final List<Parser<T>> _primitives = [];
final List<ExpressionGroup<T>> _groups = [];
Expand Down
4 changes: 3 additions & 1 deletion lib/src/matcher/matches.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ extension MatchesParserExtension<T> on Parser<T> {
///
/// For example, with the parser
///
/// final parser = letter().plus().flatten();
/// ```dart
/// final parser = letter().plus().flatten();
/// ```
///
/// `parser.allMatches('abc de')` results in the iterable `['abc', 'de']`; and
/// `parser.allMatches('abc de', overlapping: true)` results in the iterable
Expand Down
Loading

0 comments on commit 74a88b9

Please sign in to comment.