I have another question related to How to extend default PEG.js arithmetic example to allow multiple expressions not single one?
I have this grammar:
start = code:statements {
return {
"type": "Program",
"body": code
};
}
statements = head:(if / expression_statement) tail:(_ (if / expression_statement))* {
return [head].concat(tail.map(function(element) {
return element[1];
}));
}
expression_statement = expression:expression {
return {
"type": "ExpressionStatement",
"expression": expression
};
}
if = "if" _ expression:(comparison / expression) _ "then" body:(statements / _) "end" {
return {
"type": "IfStatement",
"test": expression,
"consequent": {
"type": "BlockStatement",
"body": body
},
"alternate": null
};
}
expression = expression:(arithmetic / literal) { return expression; }
literal = value:(string / Integer) {
return {"type": "Literal", "value": value };
}
variable = variable:name {
return {
"type": "Identifier",
"name": variable
}
}
name = [A-Z_a-z][A-Z_a-z0-9]* { return text(); }
comparison = _ left:expression _ "==" _ right:expression _ {
return {
"type": "BinaryExpression",
"operator": "==",
"left": left,
"right": right
};
}
string = "\"" ([^"] / "\\\\\"")* "\"" {
return JSON.parse(text());
}
arithmetic
= head:term tail:(_ ("+" / "-") _ term)* {
return tail.reduce(function(result, element) {
return {
"type": "BinaryExpression",
"operator": element[1],
"left": result,
"right": element[3]
};
}, head);
}
term
= head:factor tail:(_ ("*" / "/") _ factor)* {
return tail.reduce(function(result, element) {
return {
"type": "BinaryExpression",
"operator": element[1],
"left": result,
"right": element[3]
};
}, head);
}
factor
= "(" _ expr:arithmetic _ ")" { return expr; }
/ literal
Integer "integer"
= _ [0-9]+ { return parseInt(text(), 10); }
_ "whitespace"
= [ \t\n\r]* {
return [];
}
The parser is for creating JavaScript AST (using Esprima Object structure).
I was trying to parse ruby like if
statements:
This works fine, it create empty if
:
if "foo" == "bar" then
end
but this fail to parse:
if "foo" == "bar" then
10 + 10
end
It fail with error:
Parse Error: Expected "(", "*", "+", "-", "/", "\"", "if", or integer but "e" found.
Error in line 3
end
^
I also want it to work with more than one line:
if "foo" == "bar" then
10 + 10
10 * 10
end
I think that I should add "end" as type ahead but I'm not sure where.
Edit: I was trying this:
statements = head:(if / expression_statement) tail:(_ &"end" / (if / expression_statement)*) {
return [head].concat(tail.map(function(element) {
if (element) {
return element[2];
}
}).filter(Boolean));
}
but it don't work for two expression lines and if I use * at the end
tail:(_ &"end" / (if / expression_statement))*
I've got infinite loop parser error. I was also trying multiple combinations of &"end" but they don't work. I also found this issue on GitHub pegjs/pegjs#57 but it didn't help in any way.
I've solved the issue with this code:
statements = head:(if / expression_statement) tail:( (!"end" _ (if / expression_statement) .)*) {
return [head].concat(tail.map(function(element) {
if (element) {
return element[2];
}
}).filter(Boolean));
}