Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(semantics): support asIteration() for _iter nodes #472

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/ohm-js/src/semanticsDeferredInit.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ function initBuiltInSemantics(builtInRules) {
nonEmpty(first, _, rest) {
return this.iteration([first].concat(rest.children));
},
self(..._children) {
return this;
},
};

Semantics.BuiltInSemantics = Semantics.createSemantics(builtInRules, null).addOperation(
Expand All @@ -22,6 +25,7 @@ function initBuiltInSemantics(builtInRules) {
nonemptyListOf: actions.nonEmpty,
EmptyListOf: actions.empty,
NonemptyListOf: actions.nonEmpty,
_iter: actions.self,
},
);
}
Expand Down
5 changes: 5 additions & 0 deletions packages/ohm-js/test/test-semantics.js
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,7 @@ test('asIteration', t => {
' Start = ListOf<letter, ","> listOf<any, ".">',
' anyTwo = any any',
' anyThree = any any any',
' manyLetters = letter+',
'}',
]);
const s = g.createSemantics().addAttribute('value', {
Expand All @@ -823,10 +824,14 @@ test('asIteration', t => {
any(_) {
return this.sourceString;
},
manyLetters(letters) {
return letters.asIteration().children.map(c => c.value).join('');
},
});
t.is(s(g.match('a, b, c')).value, 'abc', 'one nonempty, one empty');
t.is(s(g.match('a, b, c 1.2.3')).value, 'abc123', 'baby you and me');
t.is(s(g.match('')).value, '', 'both empty');
t.is(s(g.match('abc', 'manyLetters')).value, 'abc', 'one or more letters');

// Check that we can override asIteration for ListOf, and extend it with an action
// for a rule of our own.
Expand Down