Skip to content

Commit

Permalink
Issue pegjs#235 (auto-labels): Use new feature in the parser itself
Browse files Browse the repository at this point in the history
  • Loading branch information
Mingun committed Jan 21, 2018
1 parent 515e00a commit ff696c7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 75 deletions.
61 changes: 18 additions & 43 deletions lib/parser.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 19 additions & 32 deletions src/parser.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,6 @@
"with"
];
function extractOptional(optional, index) {
return optional ? optional[index] : null;
}
function extractList(list, index) {
return list.map(element => element[index]);
}
function buildList(head, tail, index) {
return [head].concat(extractList(tail, index));
}
let reservedWords = new Set(RESERVED_WORDS_JS);
let comments = {};
Expand All @@ -107,11 +96,11 @@
// ---- Syntactic Grammar -----

Grammar
= __ initializer:(Initializer __)? rules:(Rule __)+ {
= __ initializer:(@Initializer __)? rules:(@Rule __)+ {
return {
type: "grammar",
initializer: extractOptional(initializer, 0),
rules: extractList(rules, 0),
initializer: initializer,
rules: rules,
comments: comments,
location: location()
};
Expand All @@ -124,7 +113,7 @@ Initializer

Rule
= name:IdentifierName __
displayName:(StringLiteral __)?
displayName:(@StringLiteral __)?
"=" __
expression:Expression EOS
{
Expand All @@ -134,7 +123,7 @@ Rule
expression: displayName !== null
? {
type: "named",
name: displayName[0],
name: displayName,
expression: expression,
location: location()
}
Expand All @@ -147,52 +136,50 @@ Expression
= ChoiceExpression

ChoiceExpression
= head:ActionExpression tail:(__ "/" __ ActionExpression)* {
= head:ActionExpression tail:(__ "/" __ @ActionExpression)* {
return tail.length > 0
? {
type: "choice",
alternatives: buildList(head, tail, 3),
alternatives: [head, ...tail],
location: location()
}
: head;
}

ActionExpression
= expression:SequenceExpression code:(__ CodeBlock)? {
= expression:SequenceExpression code:(__ @CodeBlock)? {
return code !== null
? {
type: "action",
expression: expression,
code: code[1],
code: code,
location: location()
}
: expression;
}

SequenceExpression
= head:LabeledExpression? tail:(__ LabeledExpression)* {
= head:LabeledExpression? tail:(__ @LabeledExpression)* {
return head === null
? { type: "sequence", elements: [], location: location() }
: tail.length > 0
? {
type: "sequence",
elements: buildList(head, tail, 1),
elements: [head, ...tail],
location: location()
}
: head;
}

LabeledExpression
= "@" label:(Identifier __ ":")? __ expression:PrefixedExpression {
let lbl = extractOptional(label, 0);
if (lbl && reservedWords.has(lbl[0])) {
error(`Label can't be a reserved word "${lbl[0]}".`, lbl[1]);
= "@" label:(@Identifier __ ":")? __ expression:PrefixedExpression {
if (label && reservedWords.has(label[0])) {
error(`Label can't be a reserved word "${label[0]}".`, label[1]);
}
return {
type: "labeled",
label: lbl ? lbl[0] :null,
label: label ? label[0] :null,
auto: true,
expression: expression,
location: location()
Expand Down Expand Up @@ -262,14 +249,14 @@ RangeExpression
}

RangeOperator
= "|" __ exact:RangeBoundary __ delimiter:("," __ Expression __)? "|" {
return [exact, exact, extractOptional(delimiter, 2)];
= "|" __ exact:RangeBoundary __ delimiter:("," __ @Expression __)? "|" {
return [exact, exact, delimiter];
}
/ "|" __ min:RangeBoundary? __ ".." __ max:RangeBoundary? __ delimiter:("," __ Expression __)? "|" {
/ "|" __ min:RangeBoundary? __ ".." __ max:RangeBoundary? __ delimiter:("," __ @Expression __)? "|" {
return [
min !== null ? min : { constant: true, value: 0 },
max !== null ? max : { constant: true, value: null },
extractOptional(delimiter, 2)
delimiter
];
}

Expand Down

0 comments on commit ff696c7

Please sign in to comment.