From 0bec1d7ab8284f07806bccf0e832a74a3e888707 Mon Sep 17 00:00:00 2001 From: Don Isaac Date: Thu, 28 Mar 2024 14:46:09 -0400 Subject: [PATCH] fix(semantics): support asIteration() for _iter nodes --- packages/ohm-js/src/semanticsDeferredInit.js | 4 ++++ packages/ohm-js/test/test-semantics.js | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/packages/ohm-js/src/semanticsDeferredInit.js b/packages/ohm-js/src/semanticsDeferredInit.js index cd092ab0..15be35cf 100644 --- a/packages/ohm-js/src/semanticsDeferredInit.js +++ b/packages/ohm-js/src/semanticsDeferredInit.js @@ -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( @@ -22,6 +25,7 @@ function initBuiltInSemantics(builtInRules) { nonemptyListOf: actions.nonEmpty, EmptyListOf: actions.empty, NonemptyListOf: actions.nonEmpty, + _iter: actions.self, }, ); } diff --git a/packages/ohm-js/test/test-semantics.js b/packages/ohm-js/test/test-semantics.js index 1cb4c8dd..420d7a22 100644 --- a/packages/ohm-js/test/test-semantics.js +++ b/packages/ohm-js/test/test-semantics.js @@ -809,6 +809,7 @@ test('asIteration', t => { ' Start = ListOf listOf', ' anyTwo = any any', ' anyThree = any any any', + ' manyLetters = letter+', '}', ]); const s = g.createSemantics().addAttribute('value', { @@ -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.