Skip to content

Commit

Permalink
Transform expr? %action into (expr %action)?
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoglan committed Aug 3, 2020
1 parent 39d9c20 commit ca4041d
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 85 deletions.
6 changes: 5 additions & 1 deletion src/ast/action.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
'use strict';

var util = require('../util');
var util = require('../util');
var Maybe = require('./maybe');

var Action = function(expression, actionName) {
if (expression instanceof Maybe)
return new Maybe(new Action(expression._expression, actionName));

this._expression = expression;
this._actionName = actionName;
};
Expand Down
172 changes: 88 additions & 84 deletions src/meta_grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -1170,88 +1170,95 @@
address0 = this._read_repeated_atom();
if (address0 === FAILURE) {
this._offset = index1;
address0 = this._read_terminal();
address0 = this._read_maybe_atom();
if (address0 === FAILURE) {
this._offset = index1;
var index2 = this._offset, elements0 = new Array(5);
var address1 = FAILURE;
var chunk0 = null, max0 = this._offset + 1;
if (max0 <= this._inputSize) {
chunk0 = this._input.substring(this._offset, max0);
}
if (chunk0 === '(') {
address1 = new TreeNode(this._input.substring(this._offset, this._offset + 1), this._offset, []);
this._offset = this._offset + 1;
} else {
address1 = FAILURE;
if (this._offset > this._failure) {
this._failure = this._offset;
this._expected = [];
}
if (this._offset === this._failure) {
this._expected.push('"("');
}
}
if (address1 !== FAILURE) {
elements0[0] = address1;
var address2 = FAILURE;
var remaining0 = 0, index3 = this._offset, elements1 = [], address3 = true;
while (address3 !== FAILURE) {
address3 = this._read__();
if (address3 !== FAILURE) {
elements1.push(address3);
--remaining0;
}
address0 = this._read_terminal();
if (address0 === FAILURE) {
this._offset = index1;
var index2 = this._offset, elements0 = new Array(5);
var address1 = FAILURE;
var chunk0 = null, max0 = this._offset + 1;
if (max0 <= this._inputSize) {
chunk0 = this._input.substring(this._offset, max0);
}
if (remaining0 <= 0) {
address2 = new TreeNode(this._input.substring(index3, this._offset), index3, elements1);
this._offset = this._offset;
if (chunk0 === '(') {
address1 = new TreeNode(this._input.substring(this._offset, this._offset + 1), this._offset, []);
this._offset = this._offset + 1;
} else {
address2 = FAILURE;
address1 = FAILURE;
if (this._offset > this._failure) {
this._failure = this._offset;
this._expected = [];
}
if (this._offset === this._failure) {
this._expected.push('"("');
}
}
if (address2 !== FAILURE) {
elements0[1] = address2;
var address4 = FAILURE;
address4 = this._read_actionable();
if (address4 !== FAILURE) {
elements0[2] = address4;
var address5 = FAILURE;
var remaining1 = 0, index4 = this._offset, elements2 = [], address6 = true;
while (address6 !== FAILURE) {
address6 = this._read__();
if (address6 !== FAILURE) {
elements2.push(address6);
--remaining1;
}
}
if (remaining1 <= 0) {
address5 = new TreeNode(this._input.substring(index4, this._offset), index4, elements2);
this._offset = this._offset;
} else {
address5 = FAILURE;
if (address1 !== FAILURE) {
elements0[0] = address1;
var address2 = FAILURE;
var remaining0 = 0, index3 = this._offset, elements1 = [], address3 = true;
while (address3 !== FAILURE) {
address3 = this._read__();
if (address3 !== FAILURE) {
elements1.push(address3);
--remaining0;
}
if (address5 !== FAILURE) {
elements0[3] = address5;
var address7 = FAILURE;
var chunk1 = null, max1 = this._offset + 1;
if (max1 <= this._inputSize) {
chunk1 = this._input.substring(this._offset, max1);
}
if (remaining0 <= 0) {
address2 = new TreeNode(this._input.substring(index3, this._offset), index3, elements1);
this._offset = this._offset;
} else {
address2 = FAILURE;
}
if (address2 !== FAILURE) {
elements0[1] = address2;
var address4 = FAILURE;
address4 = this._read_actionable();
if (address4 !== FAILURE) {
elements0[2] = address4;
var address5 = FAILURE;
var remaining1 = 0, index4 = this._offset, elements2 = [], address6 = true;
while (address6 !== FAILURE) {
address6 = this._read__();
if (address6 !== FAILURE) {
elements2.push(address6);
--remaining1;
}
}
if (chunk1 === ')') {
address7 = new TreeNode(this._input.substring(this._offset, this._offset + 1), this._offset, []);
this._offset = this._offset + 1;
if (remaining1 <= 0) {
address5 = new TreeNode(this._input.substring(index4, this._offset), index4, elements2);
this._offset = this._offset;
} else {
address7 = FAILURE;
if (this._offset > this._failure) {
this._failure = this._offset;
this._expected = [];
address5 = FAILURE;
}
if (address5 !== FAILURE) {
elements0[3] = address5;
var address7 = FAILURE;
var chunk1 = null, max1 = this._offset + 1;
if (max1 <= this._inputSize) {
chunk1 = this._input.substring(this._offset, max1);
}
if (this._offset === this._failure) {
this._expected.push('")"');
if (chunk1 === ')') {
address7 = new TreeNode(this._input.substring(this._offset, this._offset + 1), this._offset, []);
this._offset = this._offset + 1;
} else {
address7 = FAILURE;
if (this._offset > this._failure) {
this._failure = this._offset;
this._expected = [];
}
if (this._offset === this._failure) {
this._expected.push('")"');
}
}
if (address7 !== FAILURE) {
elements0[4] = address7;
} else {
elements0 = null;
this._offset = index2;
}
}
if (address7 !== FAILURE) {
elements0[4] = address7;
} else {
elements0 = null;
this._offset = index2;
Expand All @@ -1268,18 +1275,15 @@
elements0 = null;
this._offset = index2;
}
} else {
elements0 = null;
this._offset = index2;
}
if (elements0 === null) {
address0 = FAILURE;
} else {
address0 = this._actions.paren_expr(this._input, index2, this._offset, elements0);
this._offset = this._offset;
}
if (address0 === FAILURE) {
this._offset = index1;
if (elements0 === null) {
address0 = FAILURE;
} else {
address0 = this._actions.paren_expr(this._input, index2, this._offset, elements0);
this._offset = this._offset;
}
if (address0 === FAILURE) {
this._offset = index1;
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/meta_grammar.peg
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ action_expression <- actionable _+ action_tag %action

actionable <- sequence
/ repeated_atom
/ maybe_atom
/ terminal
/ "(" _* actionable _* ")" %paren_expr

Expand Down
38 changes: 38 additions & 0 deletions test/canopy/meta_grammar_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,25 @@ jstest.describe("MetaGrammar", function() { with(this) {
}})
}})

describe('on repetitions', function() { with(this) {
before(function() { with(this) {
this.compiler = new Compiler(' \
grammar ActionRepetition \
string <- "foo"+ %make_rep \
')
}})

it('wraps the repetition with an action', function() { with(this) {
assertEqual(['grammar', 'ActionRepetition',
['rule', 'string',
['action', 'make_rep',
['repeat', 1,
['string', 'foo']]]]],

compiler.toSexp() )
}})
}})

describe('on sequences', function() { with(this) {
before(function() { with(this) {
this.compiler = new Compiler(' \
Expand All @@ -349,6 +368,25 @@ jstest.describe("MetaGrammar", function() { with(this) {
compiler.toSexp() )
}})
}})

describe('on maybe nodes', function() { with(this) {
before(function() { with(this) {
this.compiler = new Compiler(' \
grammar ActionMaybe \
string <- "foo"? %make_maybe \
')
}})

it('transforms the action inside the maybe', function() { with(this) {
assertEqual(['grammar', 'ActionMaybe',
['rule', 'string',
['maybe',
['action', 'make_maybe',
['string', 'foo']]]]],

compiler.toSexp() )
}})
}})
}})

describe('type annotation', function() { with(this) {
Expand Down

0 comments on commit ca4041d

Please sign in to comment.