Skip to content

Instantly share code, notes, and snippets.

@aziis98
Last active December 23, 2018 23:14
Show Gist options
  • Select an option

  • Save aziis98/74e91da6f255300c749e19c2826c4e3c to your computer and use it in GitHub Desktop.

Select an option

Save aziis98/74e91da6f255300c749e19c2826c4e3c to your computer and use it in GitHub Desktop.
Generate the parser with https://pegjs.org/documentation
Expression
= _ expression:(BinaryExpression / Macro / UnaryExpression / BraceBlock / Value) _ {
return expression;
}
ArgBlock
= BraceBlock / BracketBlock
BracketBlock
= "[" expression:Expression "]" {
return {
__type__: 'bracket-argument',
expression
}
}
BraceBlock
= "{" expression:Expression "}" {
return {
__type__: 'brace-argument',
expression
}
}
Macro
= "\\" name:Identifier args:ArgBlock* {
return {
__type__: 'macro',
name, args
};
}
UnaryExpression
= op:Operator expression:Expression {
return {
__type__: 'unary',
op, expression
};
}
BinaryExpression
= left:(_ (Macro / UnaryExpression / Value) _) _ op:Operator _ right:Expression {
return {
__type__: 'binary',
left: left[1],
op,
right
};
}
Operator
= [\+\-*/=<>%$!?^_]+ {
return text();
}
Value
= [0-9]+ ("." [0-9]*)? {
return {
__type__: 'value',
value: parseFloat(text())
};
}
Identifier
= [a-zA-Z][a-zA-Z0-9]* {
return text();
}
_ "whitespace"
= [ \t\n\r]*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment