Created
July 22, 2016 15:22
-
-
Save jaxrtech/b335f33451f685959c66c8a169524b12 to your computer and use it in GitHub Desktop.
Lalex's grammar in broken PEG.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Some expressions are from the JavaScript example grammar (which is very useful) | |
// https://github.com/pegjs/pegjs/blob/master/examples/javascript.pegjs | |
Program | |
= Statement+ | |
Statement "statement" | |
= Expression | |
/ Comment | |
/* Expressions */ | |
Expression "expression" | |
= ExpressionGroup | |
/ LetExpression | |
/ Function | |
/ Integer | |
/ Assignment | |
/ Identifier | |
/ ParensExpression | |
Function | |
= "\\" _ params:FunctionParameterList _ "->" _ | |
expr:Expression | |
{ return { tag: 'fn', params, expr }; } | |
FunctionParameterList | |
= xs:FunctionParameter* | |
{ return xs; } | |
FunctionParameter | |
= x:Identifier _ | |
{ return x; } | |
ParensExpression | |
= "(" _ x:Expression _ ")" | |
{ return x; } | |
ExpressionGroup "expression group" | |
= "{" _ xs:ExpressionList _ "}" | |
{ return xs; } | |
ExpressionList "expression list" | |
= xs:ExpressionListPart+ | |
{ return xs; } | |
ExpressionListPart | |
= x:Expression _ ExpressionTerminator _ | |
{ return x; } | |
IfExpression "if expression" | |
= $if:IfExpressionIf | |
$elifs:IfExpressionElif* | |
$else:IfExpressionElse | |
{ return { tag: 'if', $if, $elifs, $else }; } | |
IfExpressionIf | |
= "if" _ "(" _ cond:Expression _ ")" _ body:Expression | |
{ return { cond, body }; } | |
IfExpressionElif | |
= "elif" _ "(" _ cond:Expression _ ")" _ body:Expression | |
{ return { cond, body }; } | |
IfExpressionElse | |
= "else" _ x:Expression | |
{ return x; } | |
LetExpression "let expression" | |
= "let" ws+ clause:LetClause? _ body:Expression | |
{ return { tag: 'let', clause, body }; } | |
LetClause "clause of let expression" | |
= xs:LetClauseEdge | |
(ExpressionTerminator _ LetClauseMiddle*)? _ | |
LetClauseEdge? _ "in" | |
{ return xs; } | |
LetClauseEdge | |
= x:Assignment _ | |
{ return x; } | |
LetClauseMiddle | |
= x:Assignment _ ExpressionTerminator _ | |
{ return x; } | |
Assignment "assignment" | |
= ident:Identifier _ "=" _ expr:Expression | |
{ return { tag: 'assign', ident, expr }; } | |
Integer "integer" | |
= [0-9]+ { return parseInt(text(), 10); } | |
Identifier "identifier" | |
= head:IdentifierStart tail:IdentifierPart* | |
{ return { tag: 'ident', name: head + tail.join("") }; } | |
IdentifierStart | |
= [a-z] / [A-Z] | |
IdentifierPart | |
= IdentifierStart | |
/ [0-9] | |
/ "_" | |
/* Comments */ | |
Comment "comment" | |
= "//" text:CommentText LineTerminatorSequence | |
{ return { tag: 'comment', text: text } } | |
CommentText | |
= text:CommentChar* | |
{ return text.join(""); } | |
CommentChar | |
= !LineTerminator c:. | |
{ return c; } | |
/* Whitespace */ | |
ExpressionTerminator | |
= _ (";" / LineTerminatorSequence _) | |
_ "whitespace" = ws* | |
ws = Whitespace | |
Whitespace = Spaces / LineTerminator | |
Spaces = [ \t] | |
LineTerminator = [\n\r] | |
LineTerminatorSequence "end of line" | |
= "\n" | |
/ "\r\n" | |
/ "\r" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment