Skip to content

Commit c1cc83e

Browse files
committed
General cleanup.
1 parent 382d82a commit c1cc83e

20 files changed

+168
-473
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ print(id.accept('foo')); // true
109109
print(id.accept('123')); // false
110110
```
111111

112-
113112
### Different Kinds of Parsers
114113

115114
PetitParser provides a large set of ready-made parser that you can compose to consume and transform arbitrarily complex languages.
@@ -130,8 +129,11 @@ Terminal parsers are the simplest. We've already seen a few of those:
130129
- [`whitespace()`](https://pub.dev/documentation/petitparser/latest/petitparser/whitespace.html) parses a whitespace character, i.e. ** or **.
131130
- [`word()`](https://pub.dev/documentation/petitparser/latest/petitparser/word.html) parses a single letter, digit, or the underscore character.
132131

133-
By default all parsers use an automatically generated descriptive error message, match case-sensitive, and work on 16-bit UTF-16 code units.
132+
By default all parsers use an automatically generated descriptive error message, match case-sensitive, and work on 16-bit UTF-16 code units. To change this default behavior use the named arguments (where appropriate):
134133

134+
- `message: 'expected a special character'` to use a custom error message,
135+
- `ignoreCase: true` to accept both lower- and uppercase variations, and
136+
- `unicode: true` to decode surrogate pairs and read Unicode code-points.
135137

136138
#### Combinator Parsers
137139

bin/generate_sequence.dart

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -223,32 +223,11 @@ Future<void> generateTest() async {
223223
'position: $j));');
224224
out.writeln('});');
225225
}
226-
out.writeln('});');
227-
228-
out.writeln('group(\'toSequenceParser()\', () {');
229-
out.writeln('final parser = ('
226+
out.writeln('test(\'toSequenceParser()\', () {');
227+
out.writeln('final alternate = ('
230228
'${chars.map((each) => 'char(\'$each\')').join(', ')}).toSequenceParser();');
231-
out.writeln('const record = ('
232-
'${chars.map((each) => '\'$each\'').join(', ')});');
233-
out.writeln('expectParserInvariants(parser);');
234-
out.writeln('test(\'success\', () {');
235-
out.writeln('expect(parser, '
236-
'isParseSuccess(\'$string\', result: record));');
237-
out.writeln('expect(parser, '
238-
'isParseSuccess(\'$string*\', result: record, position: $i));');
229+
out.writeln('expect(alternate, isParserDeepEqual(parser));');
239230
out.writeln('});');
240-
for (var j = 0; j < i; j++) {
241-
out.writeln('test(\'failure at $j\', () {');
242-
out.writeln('expect(parser, isParseFailure(\''
243-
'${string.substring(0, j)}\', '
244-
'message: \'"${chars[j]}" expected\', '
245-
'position: $j));');
246-
out.writeln('expect(parser, isParseFailure(\''
247-
'${string.substring(0, j)}*\', '
248-
'message: \'"${chars[j]}" expected\', '
249-
'position: $j));');
250-
out.writeln('});');
251-
}
252231
out.writeln('});');
253232

254233
out.writeln('group(\'map$i\', () {');

lib/parser.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export 'src/parser/action/map.dart';
1010
export 'src/parser/action/permute.dart';
1111
export 'src/parser/action/pick.dart';
1212
export 'src/parser/action/token.dart';
13-
export 'src/parser/action/trimming.dart';
13+
export 'src/parser/action/trim.dart';
1414
export 'src/parser/action/where.dart';
1515
export 'src/parser/character/any.dart';
1616
export 'src/parser/character/any_of.dart';
@@ -34,7 +34,7 @@ export 'src/parser/combinator/optional.dart';
3434
export 'src/parser/combinator/sequence.dart';
3535
export 'src/parser/combinator/settable.dart';
3636
export 'src/parser/combinator/skip.dart';
37-
export 'src/parser/misc/eof.dart';
37+
export 'src/parser/misc/end.dart';
3838
export 'src/parser/misc/epsilon.dart';
3939
export 'src/parser/misc/failure.dart';
4040
export 'src/parser/misc/label.dart';

lib/src/definition/grammar.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ abstract class GrammarDefinition<R> {
8181

8282
/// Builds a composite parser starting with the specified [parser].
8383
///
84-
/// As argument either pass a reference to a production of this definition or
85-
/// any other parser using productions of this definition.
84+
/// As argument either pass a reference to a production in this definition, or
85+
/// any other parser using productions in this definition.
8686
@useResult
8787
Parser<T> buildFrom<T>(Parser<T> parser) => resolve<T>(parser);
8888
}
File renamed without changes.

lib/src/parser/character/any_of.dart

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@ import 'utils/optimize.dart';
99
@useResult
1010
Parser<String> anyOf(String value,
1111
{String? message, bool ignoreCase = false, bool unicode = false}) {
12-
final predicate = ignoreCase
13-
? optimizedString('${value.toLowerCase()}${value.toUpperCase()}',
14-
unicode: unicode)
15-
: optimizedString(value, unicode: unicode);
12+
final predicate =
13+
optimizedString(value, ignoreCase: ignoreCase, unicode: unicode);
1614
message ??= 'any of "${toReadableString(value, unicode: unicode)}"'
1715
'${ignoreCase ? ' (case-insensitive)' : ''} expected';
1816
return CharacterParser(predicate, message, unicode: unicode);

lib/src/parser/character/char.dart

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,9 @@ Parser<String> char(String value,
1818
{String? message, bool ignoreCase = false, bool unicode = false}) {
1919
final charCode = toCharCode(value, unicode: unicode);
2020
final predicate = ignoreCase
21-
? optimizedString('${value.toLowerCase()}${value.toUpperCase()}',
22-
unicode: unicode)
21+
? optimizedString(value, ignoreCase: ignoreCase, unicode: unicode)
2322
: SingleCharPredicate(charCode);
2423
message ??= '"${toReadableString(value, unicode: unicode)}"'
2524
'${ignoreCase ? ' (case-insensitive)' : ''} expected';
2625
return CharacterParser(predicate, message, unicode: unicode);
2726
}
28-
29-
@useResult
30-
@Deprecated('Use `char(value, message: message, ignoreCase: true)` instead')
31-
Parser<String> charIgnoringCase(String value, [String? message]) =>
32-
char(value, message: message, ignoreCase: true);

lib/src/parser/character/none_of.dart

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@ import 'utils/optimize.dart';
1010
@useResult
1111
Parser<String> noneOf(String value,
1212
{String? message, bool ignoreCase = false, bool unicode = false}) {
13-
final predicate = NotCharPredicate(ignoreCase
14-
? optimizedString('${value.toLowerCase()}${value.toUpperCase()}',
15-
unicode: unicode)
16-
: optimizedString(value, unicode: unicode));
13+
final predicate = NotCharPredicate(
14+
optimizedString(value, ignoreCase: ignoreCase, unicode: unicode));
1715
message ??= 'none of "${toReadableString(value, unicode: unicode)}"'
1816
'${ignoreCase ? ' (case-insensitive)' : ''} expected';
1917
return CharacterParser(predicate, message, unicode: unicode);

lib/src/parser/character/pattern.dart

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import '../../core/parser.dart';
44
import '../action/map.dart';
55
import '../combinator/choice.dart';
66
import '../combinator/sequence.dart';
7-
import '../misc/eof.dart';
7+
import '../misc/end.dart';
88
import '../predicate/character.dart';
99
import '../repeater/possessive.dart';
1010
import 'any.dart';
@@ -52,11 +52,6 @@ Parser<String> pattern(String pattern,
5252
return CharacterParser(predicate, message, unicode: unicode);
5353
}
5454

55-
@useResult
56-
@Deprecated('Use `pattern(value, message: message, ignoreCase: true)` instead')
57-
Parser<String> patternIgnoreCase(String value, [String? message]) =>
58-
pattern(value, message: message, ignoreCase: true);
59-
6055
Parser<List<RangeCharPredicate>> _createParser({required bool unicode}) {
6156
// Parser that consumes a single character.
6257
final character = any(unicode: unicode);

lib/src/parser/character/predicate.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ abstract class CharacterPredicate {
88
/// Tests if the [charCode] satisfies the predicate.
99
bool test(int charCode);
1010

11-
/// Compares the two predicates for equality.
11+
/// Compares the predicate and [other] for equality.
1212
bool isEqualTo(CharacterPredicate other);
1313

1414
@override

lib/src/parser/character/utils/optimize.dart

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@ import '../predicate/lookup.dart';
55
import '../predicate/range.dart';
66

77
/// Creates an optimized character from a string.
8-
CharacterPredicate optimizedString(String string, {required bool unicode}) =>
9-
optimizedRanges(
10-
(unicode ? string.runes : string.codeUnits)
11-
.map((value) => RangeCharPredicate(value, value)),
12-
unicode: unicode);
8+
CharacterPredicate optimizedString(String string,
9+
{required bool unicode, bool ignoreCase = false}) {
10+
if (ignoreCase) string = '${string.toLowerCase()}${string.toUpperCase()}';
11+
return optimizedRanges(
12+
(unicode ? string.runes : string.codeUnits)
13+
.map((value) => RangeCharPredicate(value, value)),
14+
unicode: unicode);
15+
}
1316

1417
/// Creates an optimized predicate from a list of range predicates.
1518
CharacterPredicate optimizedRanges(Iterable<RangeCharPredicate> ranges,
File renamed without changes.

lib/src/parser/predicate/string.dart

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,3 @@ Parser<String> string(String string,
2222
message ?? '"$string" (case-insensitive) expected')
2323
: predicate(string.length, (value) => string == value,
2424
message ?? '"$string" expected');
25-
26-
@useResult
27-
@Deprecated('Use `string(value, ignoreCase: true)` instead')
28-
Parser<String> stringIgnoreCase(String value, [String? message]) =>
29-
string(value, message: message, ignoreCase: true);

0 commit comments

Comments
 (0)