Skip to content

Commit

Permalink
allow leading pipe in rule bodies (suggested by Jason Merrill)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexwarth committed Mar 5, 2016
1 parent 70a7edf commit 3ce66ea
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 146 deletions.
128 changes: 64 additions & 64 deletions dist/ohm-grammar.js

Large diffs are not rendered by default.

136 changes: 68 additions & 68 deletions dist/ohm.js

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions dist/ohm.min.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions examples/viz/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -136,20 +136,20 @@
expr.viz();
leave();
},
Rule_define: function(n, fs, d, _, b) {
Rule_define: function(n, fs, d, _equals, _optBar, b) {
add('name', n.viz());
d.viz();
enter('ruleDefineBody');
b.viz();
leave();
},
Rule_override: function(n, fs, _, b) {
Rule_override: function(n, fs, _colonEquals, _optBar, b) {
add('name', n.viz());
enter('ruleOverrideBody');
b.viz();
leave();
},
Rule_extend: function(n, fs, _, b) {
Rule_extend: function(n, fs, _plusEquals, _optBar, b) {
add('name', n.viz());
enter('ruleExtendBody');
b.viz();
Expand Down
6 changes: 3 additions & 3 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ function buildGrammar(match, namespace, optOhmGrammarForTesting) {
}
},

Rule_define: function(n, fs, d, _, b) {
Rule_define: function(n, fs, d, _equals, _optBar, b) {
currentRuleName = n.visit();
currentRuleFormals = fs.visit()[0] || [];
// If there is no default start rule yet, set it now. This must be done before visiting
Expand All @@ -115,7 +115,7 @@ function buildGrammar(match, namespace, optOhmGrammarForTesting) {
var description = d.visit()[0];
return decl.define(currentRuleName, currentRuleFormals, body, description);
},
Rule_override: function(n, fs, _, b) {
Rule_override: function(n, fs, _colonEquals, _optBar, b) {
currentRuleName = n.visit();
currentRuleFormals = fs.visit()[0] || [];
overriding = true;
Expand All @@ -125,7 +125,7 @@ function buildGrammar(match, namespace, optOhmGrammarForTesting) {
overriding = false;
return ans;
},
Rule_extend: function(n, fs, _, b) {
Rule_extend: function(n, fs, _plusEquals, _optBar, b) {
currentRuleName = n.visit();
currentRuleFormals = fs.visit()[0] || [];
var body = b.visit();
Expand Down
6 changes: 3 additions & 3 deletions src/ohm-grammar.ohm
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ Ohm {
= "<:" ident

Rule
= ident Formals? ruleDescr? "=" Alt -- define
| ident Formals? ":=" Alt -- override
| ident Formals? "+=" Alt -- extend
= ident Formals? ruleDescr? "=" "|"? Alt -- define
| ident Formals? ":=" "|"? Alt -- override
| ident Formals? "+=" "|"? Alt -- extend

Formals
= "<" ListOf<ident, ","> ">"
Expand Down
56 changes: 56 additions & 0 deletions test/test-ohm-syntax.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,62 @@ test('alt', function(t) {
t.end();
});

test('rule bodies in defs can start with a |, and it\'s a no-op', function(t) {
var m = ohm.grammar('M { altTest = | "a" | "b" }');

it('recognition', function() {
assertFails(t, m.match(''));
assertSucceeds(t, m.match('a'));
assertSucceeds(t, m.match('b'));
assertFails(t, m.match('ab'));
});

it('semantic actions', function() {
var s = m.semantics().addAttribute('v', {});
t.equal(s(m.match('a')).v, 'a');
t.equal(s(m.match('b')).v, 'b');
});
t.end();
});

test('rule bodies in overrides can start with a |, and it\'s a no-op', function(t) {
var m = ohm.grammar('M { space := | "a" | "b" }');

it('recognition', function() {
assertFails(t, m.match('', 'space'));
assertSucceeds(t, m.match('a', 'space'));
assertSucceeds(t, m.match('b', 'space'));
assertFails(t, m.match(' ', 'space'));
assertFails(t, m.match('\t', 'space'));
});

it('semantic actions', function() {
var s = m.semantics().addAttribute('v', {});
t.equal(s(m.match('a', 'space')).v, 'a');
t.equal(s(m.match('b', 'space')).v, 'b');
});
t.end();
});

test('rule bodies in extends can start with a |, and it\'s a no-op', function(t) {
var m = ohm.grammar('M { space += | "a" | "b" }');

it('recognition', function() {
assertFails(t, m.match('', 'space'));
assertSucceeds(t, m.match('a', 'space'));
assertSucceeds(t, m.match('b', 'space'));
assertSucceeds(t, m.match(' ', 'space'));
assertSucceeds(t, m.match('\t', 'space'));
});

it('semantic actions', function() {
var s = m.semantics().addAttribute('v', {});
t.equal(s(m.match('a', 'space')).v, 'a');
t.equal(s(m.match('b', 'space')).v, 'b');
});
t.end();
});

test('seq', function(t) {
test('without bindings', function(t) {
var m = ohm.grammar('M { start = "a" "bc" "z" }');
Expand Down

0 comments on commit 3ce66ea

Please sign in to comment.