Unordered parsing? #71
-
is there a good example for parse in un order sequence. i.e. if i am parsing a fragment STAGE:FOO=BAR;PASS=FAIL;BOO=FOO; but the fragment may appear in any order the only rule is that it should only start with STAGE: |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments
-
@renggli any input is appreciated. |
Beta Was this translation helpful? Give feedback.
-
I would write it as such: final key = letter().plus();
final value = letter().plus();
final entry = key & char('=') & value & char(';');
final fragment = string('STAGE:') & entry.plus(); |
Beta Was this translation helpful? Give feedback.
-
@renggli thank you this makes sense but if i want to apply any grammar rule to value then i can't for example number range, date range, string range, character range, etc... Also this also means that two keys can occur again? how can I prevent two name repeating? Let me expand the grammar a bit more. // no fragment should be repeated more then once but can be found or introduced in any order. stage = STAGE : (type | validation | elementsize) ; |
Beta Was this translation helpful? Give feedback.
-
This is not something that is typically specified in the grammar itself. Instead it is validated either while building your model (see example below), or in a separate pass after the model has been created: // ...
final fragment = string('STAGE:').token() & entry.plus();
final start = fragment.map((values) => new Fragment(values[0], values[1]));
class Fragment {
final Token startToken;
final List<Entry> entries;
Fragment(this.startToken, this.entries) {
final keys = entries.map(entry => entry.key);
if (keys.toSet().length != keys.length) {
throw new Exception('Repeated entries at ${startToken.start}: $keys');
}
}
} |
Beta Was this translation helpful? Give feedback.
-
@renggli thank you this is very helpful. |
Beta Was this translation helpful? Give feedback.
This is not something that is typically specified in the grammar itself. Instead it is validated either while building your model (see example below), or in a separate pass after the model has been created: