Skip to content

Commit 456b540

Browse files
authored
fix(semantics): support asIteration() for _iter nodes (#472)
1 parent 03b5b17 commit 456b540

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

packages/ohm-js/src/semanticsDeferredInit.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ function initBuiltInSemantics(builtInRules) {
1313
nonEmpty(first, _, rest) {
1414
return this.iteration([first].concat(rest.children));
1515
},
16+
self(..._children) {
17+
return this;
18+
},
1619
};
1720

1821
Semantics.BuiltInSemantics = Semantics.createSemantics(builtInRules, null).addOperation(
@@ -22,6 +25,7 @@ function initBuiltInSemantics(builtInRules) {
2225
nonemptyListOf: actions.nonEmpty,
2326
EmptyListOf: actions.empty,
2427
NonemptyListOf: actions.nonEmpty,
28+
_iter: actions.self,
2529
},
2630
);
2731
}

packages/ohm-js/test/test-semantics.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,7 @@ test('asIteration', t => {
809809
' Start = ListOf<letter, ","> listOf<any, ".">',
810810
' anyTwo = any any',
811811
' anyThree = any any any',
812+
' manyLetters = letter+',
812813
'}',
813814
]);
814815
const s = g.createSemantics().addAttribute('value', {
@@ -823,10 +824,14 @@ test('asIteration', t => {
823824
any(_) {
824825
return this.sourceString;
825826
},
827+
manyLetters(letters) {
828+
return letters.asIteration().children.map(c => c.value).join('');
829+
},
826830
});
827831
t.is(s(g.match('a, b, c')).value, 'abc', 'one nonempty, one empty');
828832
t.is(s(g.match('a, b, c 1.2.3')).value, 'abc123', 'baby you and me');
829833
t.is(s(g.match('')).value, '', 'both empty');
834+
t.is(s(g.match('abc', 'manyLetters')).value, 'abc', 'one or more letters');
830835

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

0 commit comments

Comments
 (0)