Skip to content

Instantly share code, notes, and snippets.

@twfarland
Created August 7, 2012 17:08
Show Gist options
  • Save twfarland/3287351 to your computer and use it in GitHub Desktop.
Save twfarland/3287351 to your computer and use it in GitHub Desktop.
Experimental peg.js grammar for Ethpretho
start
= seq
validchar
= [0-9a-zA-Z_?!+\-=@#$%^&*/.]
atom
= chars:validchar+ { return chars.join(""); }
symbol
= x:[A-z_$]? xs:[A-z_$0-9]* { return x + xs.join(""); }
comment
= ";;" c:[^\n]* { return {comment: c.join("")}; }
string
= '"' '"' { return {string: ""}; }
/ '"' chars:chars '"' { return {string: chars}; }
chars
= chars:char+ { return chars.join(""); }
char
= [^"\\\0-\x1F\x7f]
/ '\\"' { return '"'; }
/ "\\\\" { return "\\"; }
/ "\\/" { return "/"; }
/ "\\b" { return "\b"; }
/ "\\f" { return "\f"; }
/ "\\n" { return "\n"; }
/ "\\r" { return "\r"; }
/ "\\t" { return "\t"; }
/ "\\u" h1:hexDigit h2:hexDigit h3:hexDigit h4:hexDigit {
return String.fromCharCode(parseInt("0x" + h1 + h2 + h3 + h4));
}
hexDigit
= [0-9a-fA-F]
_
= [ \t\n\r]
key
= string
/ symbol
pair
= k:key v:expr { return {pair: [k, v]}; }
object
= "{" ps:pair* "}" { return {object: ps}; }
array
= "[" xs:expr* "]" { return {array: xs}; }
_expr_
= atom
/ "(" xs:expr* ")" { return xs; }
/ string
/ array
/ object
/ comment
/ "'" x:expr { return {quote: x}; }
/ "," x:expr { return {unquote: x}; }
expr
= _* x:_expr_ _* { return x; }
seq
= xs:expr* { return {seq: xs}; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment