fix up utils/tests/peg and utils/peg code to not have diagnostics

This code is opted in to the latest language version but was not ever migrated.

It could possibly be just deleted, as it hasn't been runnable for some time, but I have no idea really.

Change-Id: I7ccda121dcd56ed9388b48c70e3b3c0b2d087e47
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/292042
Commit-Queue: Jake Macdonald <jakemac@google.com>
Auto-Submit: Jake Macdonald <jakemac@google.com>
Reviewed-by: Nate Bosch <nbosch@google.com>
This commit is contained in:
Jake Macdonald 2023-03-31 17:04:38 +00:00 committed by Commit Queue
parent d2184a9d03
commit d259edfa28
2 changed files with 36 additions and 35 deletions

View file

@ -61,13 +61,16 @@ _Rule CHAR([characters]) {
// Find the range of character codes and construct an array of flags for codes // Find the range of character codes and construct an array of flags for codes
// within the range. // within the range.
List<int> codes = characters.codeUnits.toList(); List<int> codes = characters.codeUnits.toList();
codes.sort((a, b) => a < b ? -1 : a > b ? 1 : 0); codes.sort((a, b) => a < b
? -1
: a > b
? 1
: 0);
int lo = codes[0]; int lo = codes[0];
int hi = codes[codes.length - 1]; int hi = codes[codes.length - 1];
if (lo == hi) return CHARCODE(lo); if (lo == hi) return CHARCODE(lo);
int len = hi - lo + 1; int len = hi - lo + 1;
var flags = new List<bool>(len); var flags = List<bool>.filled(len, false);
for (int i = 0; i < len; ++i) flags[i] = false;
for (int code in codes) flags[code - lo] = true; for (int code in codes) flags[code - lo] = true;
return CHARCODE((code) => code >= lo && code <= hi && flags[code - lo]); return CHARCODE((code) => code >= lo && code <= hi && flags[code - lo]);
@ -160,7 +163,7 @@ _Rule MAYBE(rule) => new _OptionalRule(_compile(rule));
* MANY is a value generating matcher. The value is a list of the matches of * MANY is a value generating matcher. The value is a list of the matches of
* [rule]. The list may be empty if [:min == 0:]. * [rule]. The list may be empty if [:min == 0:].
*/ */
_Rule MANY(rule, {separator: null, int min: 1}) { _Rule MANY(rule, {separator = null, int min = 1}) {
assert(0 <= min && min <= 1); assert(0 <= min && min <= 1);
return new _RepeatRule(_compile(rule), _compileOptional(separator), min); return new _RepeatRule(_compile(rule), _compileOptional(separator), min);
} }
@ -269,15 +272,14 @@ class Grammar {
Map<String, Symbol> _symbols; Map<String, Symbol> _symbols;
/** This rule may be set by the user to define whitespace. */ /** This rule may be set by the user to define whitespace. */
_Rule _whitespace; late _Rule _whitespace;
_Rule get whitespace => _whitespace; _Rule get whitespace => _whitespace;
void set whitespace(rule) { void set whitespace(rule) {
_whitespace = _compile(rule); _whitespace = _compile(rule);
} }
Grammar() { Grammar() : _symbols = new Map<String, Symbol>() {
_symbols = new Map<String, Symbol>();
whitespace = CHAR(' \t\r\n'); whitespace = CHAR(' \t\r\n');
} }
@ -286,7 +288,7 @@ class Grammar {
* to define recursive rules. * to define recursive rules.
*/ */
Symbol operator [](String name) { Symbol operator [](String name) {
if (_symbols.containsKey(name)) return _symbols[name]; if (_symbols.containsKey(name)) return _symbols[name]!;
Symbol s = new Symbol(name, this); Symbol s = new Symbol(name, this);
_symbols[name] = s; _symbols[name] = s;
return s; return s;
@ -318,7 +320,9 @@ class Grammar {
var tokens = new List<String>.from(s); var tokens = new List<String>.from(s);
tokens.sort((a, b) => a.startsWith("'") == b.startsWith("'") tokens.sort((a, b) => a.startsWith("'") == b.startsWith("'")
? a.compareTo(b) ? a.compareTo(b)
: a.startsWith("'") ? 1 : -1); : a.startsWith("'")
? 1
: -1);
var expected = tokens.join(' or '); var expected = tokens.join(' or ');
var found = state.max_pos == state._end var found = state.max_pos == state._end
? 'end of file' ? 'end of file'
@ -345,7 +349,7 @@ class Grammar {
class Symbol { class Symbol {
final String name; final String name;
final Grammar grammar; final Grammar grammar;
_Rule _rule; _Rule? _rule;
Symbol(this.name, this.grammar); Symbol(this.name, this.grammar);
@ -358,9 +362,9 @@ class Symbol {
} }
class _ParserState { class _ParserState {
_ParserState(this._text, {_Rule whitespace}) { _ParserState(this._text, {required _Rule whitespace})
_end = this._text.length; : _end = _text.length,
whitespaceRule = whitespace; whitespaceRule = whitespace {
max_rule = []; max_rule = [];
} }
@ -369,7 +373,7 @@ class _ParserState {
// //
bool inWhitespaceMode = false; bool inWhitespaceMode = false;
_Rule whitespaceRule = null; _Rule whitespaceRule;
// Used for constructing an error message. // Used for constructing an error message.
int inhibitExpectedTrackingDepth = 0; int inhibitExpectedTrackingDepth = 0;
@ -444,7 +448,7 @@ int _skip_whitespace(state, pos) {
return pos; return pos;
} }
_Rule _compileOptional(rule) { _Rule? _compileOptional(rule) {
return rule == null ? null : _compile(rule); return rule == null ? null : _compile(rule);
} }
@ -512,7 +516,7 @@ class _SymbolRule extends _Rule {
_match(_ParserState state, int pos) { _match(_ParserState state, int pos) {
if (_symbol._rule == null) if (_symbol._rule == null)
throw new Exception("Symbol '${_symbol.name}' is undefined"); throw new Exception("Symbol '${_symbol.name}' is undefined");
return _symbol._rule.match(state, pos); return _symbol._rule!.match(state, pos);
} }
bool get generatesValue => true; bool get generatesValue => true;
@ -536,9 +540,7 @@ class _SkipRule extends _Rule {
class _StringRule extends _Rule implements _Expectable { class _StringRule extends _Rule implements _Expectable {
final String _string; final String _string;
int _len; int _len;
_StringRule(this._string) { _StringRule(this._string) : _len = _string.length;
_len = _string.length;
}
_match(_ParserState state, int pos) { _match(_ParserState state, int pos) {
if (pos + _len > state._end) return null; if (pos + _len > state._end) return null;
@ -566,10 +568,10 @@ class _RegExpRule extends _Rule {
} }
class _LexicalRule extends _Rule implements _Expectable { class _LexicalRule extends _Rule implements _Expectable {
final String _name; final String? _name;
final _Rule _rule; final _Rule _rule;
_LexicalRule(String this._name, _Rule this._rule); _LexicalRule(this._name, this._rule);
_match(_ParserState state, int pos) { _match(_ParserState state, int pos) {
state.inWhitespaceMode = true; state.inWhitespaceMode = true;
@ -580,9 +582,9 @@ class _LexicalRule extends _Rule implements _Expectable {
return match; return match;
} }
toString() => _name; toString() => _name.toString();
description() => _name == null ? '?' : _name; description() => _name == null ? '?' : _name!;
} }
class _TextValueRule extends _Rule { class _TextValueRule extends _Rule {
@ -608,8 +610,8 @@ class _TextValueRule extends _Rule {
_Rule _compileMultiRule( _Rule _compileMultiRule(
List rules, bool allowReducer, finish(compiledRules, valueCount, reducer)) { List rules, bool allowReducer, finish(compiledRules, valueCount, reducer)) {
int valueCount = 0; int valueCount = 0;
List compiledRules = new List<_Rule>(); List compiledRules = <_Rule>[];
Function reducer; Function? reducer;
for (var rule in rules) { for (var rule in rules) {
if (reducer != null) if (reducer != null)
throw new Exception('Reducer must be last in sequence: $rule'); throw new Exception('Reducer must be last in sequence: $rule');
@ -644,12 +646,11 @@ class _SequenceRule extends _Rule {
// This rule matches the component rules in order. // This rule matches the component rules in order.
final List<_Rule> _rules; final List<_Rule> _rules;
final int _generatingSubRules; final int _generatingSubRules;
final Function _reducer; final Function? _reducer;
bool _generatesValue; bool _generatesValue;
_SequenceRule(List<_Rule> this._rules, int this._generatingSubRules, _SequenceRule(List<_Rule> this._rules, int this._generatingSubRules,
Function this._reducer) { Function? this._reducer)
_generatesValue = _generatingSubRules > 0 || _reducer != null; : _generatesValue = _generatingSubRules > 0 || _reducer != null;
}
_match(state, pos) { _match(state, pos) {
var sequence = []; var sequence = [];
@ -746,7 +747,7 @@ class _NegativeContextRule extends _Rule {
class _RepeatRule extends _Rule { class _RepeatRule extends _Rule {
// Matches zero, one or more items. // Matches zero, one or more items.
_Rule _rule; _Rule _rule;
_Rule _separator; _Rule? _separator;
int _min; int _min;
_RepeatRule(this._rule, this._separator, this._min); _RepeatRule(this._rule, this._separator, this._min);
@ -765,7 +766,7 @@ class _RepeatRule extends _Rule {
while (true) { while (true) {
var newPos = pos; var newPos = pos;
if (_separator != null) { if (_separator != null) {
match = _separator.match(state, pos); match = _separator!.match(state, pos);
if (match == null) return [pos, result]; if (match == null) return [pos, result];
newPos = match[0]; newPos = match[0];
} }
@ -779,7 +780,7 @@ class _RepeatRule extends _Rule {
bool get generatesValue => true; bool get generatesValue => true;
toString() => toString() =>
'MANY(min:$_min, $_rule${_separator==null?'':", sep: $_separator"})'; 'MANY(min:$_min, $_rule${_separator == null ? '' : ", sep: $_separator"})';
} }
class _MemoRule extends _Rule { class _MemoRule extends _Rule {
@ -790,7 +791,7 @@ class _MemoRule extends _Rule {
// A map from position to result. Can this be replaced with something // A map from position to result. Can this be replaced with something
// smaller? // smaller?
// TODO: figure out how to discard the map and parseInstance after parsing. // TODO: figure out how to discard the map and parseInstance after parsing.
Map<int, Object> map; Map<int, Object> map = {};
_MemoRule(this._rule); _MemoRule(this._rule);
@ -852,7 +853,7 @@ _apply(fn, List args) {
List _unspread(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, List _unspread(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v,
w, x, y, z) { w, x, y, z) {
List list = new List(); List list = [];
add(element) { add(element) {
if (element != null) list.add(element); if (element != null) list.add(element);
} }

View file

@ -71,7 +71,7 @@ testTEXT() {
check(g, TEXT(MANY(OR(['1', 'a']))), ' 1 a 1 ', '1 a 1'); check(g, TEXT(MANY(OR(['1', 'a']))), ' 1 a 1 ', '1 a 1');
// Custom processing of the TEXT substring. // Custom processing of the TEXT substring.
var binaryNumber = TEXT(LEX(MANY(OR(['0', '1']))), (str, start, end) { var binaryNumber = TEXT(LEX(MANY(OR(['0', '1']))), (String str, start, end) {
var r = 0; var r = 0;
var zero = '0'.codeUnitAt(0); var zero = '0'.codeUnitAt(0);
for (int i = start; i < end; i++) r = r * 2 + (str.codeUnitAt(i) - zero); for (int i = start; i < end; i++) r = r * 2 + (str.codeUnitAt(i) - zero);