Created
June 20, 2010 05:14
-
-
Save manveru/445580 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
start = Void program:Program Void { return program } | |
Void = Space* | |
Space "Space" = [\t ]+ | |
Stop "Stop" = [\n\r;]+ Void | |
EOF "End of File" = !. | |
Comment "Comment" = c:"#" t:(&[^\n\r] .)* (Stop / EOF) { | |
return c + t.map(function(a){ return a[1] }).join("") | |
} | |
Program = (Expression / Comment)* | |
Expression = exp:( | |
("(" Expression ")") / | |
Unslot / | |
Keyslot / | |
Bislot / | |
Assignment / | |
Variable / | |
Literal | |
) (Stop / EOF) { | |
return exp | |
} | |
Assignment = Variable Void "=" Void Value | |
Bislot = rec:Value msg:BiMessage arg:Value { return [rec, msg, arg] } | |
Unslot = rec:Value msg:UnMessage { return [rec, msg] } | |
Keyslot = rec:Value msgs:KeyMessage+ { | |
return [rec].concat(msgs) | |
} | |
UnMessage = Space a:Variable &(!":") b:[?!]? { | |
return a + b | |
} | |
BiMessage = Space a:[+=*/!&%$<>~?^|-]+ Space { | |
return a.join("") | |
} | |
KeyMessage = Space a:Variable b:":" Space c:Value { | |
return [a + b, c] | |
} | |
Value = literal:Literal { return literal } | |
/ "(" exp:(Unslot / Keyslot / Bislot) ")" { return exp } | |
/ Block | |
/ Variable | |
Variable = a:[a-zA-Z] b:[a-zA-Z0-9_-]* { | |
return a + b.join("") | |
} | |
Block = BlockStart "|" BlockEnd { | |
return ["block", [], []] | |
} | |
BlockStart "Block start" = "[" | |
BlockEnd "Block end" = "]" | |
Blockslots "Block slots" = Void a:(Blockslot Space)* b:Blockslot? Void { | |
if (b != '') { | |
var tmp = [] | |
var i = 0 | |
for (n in a) { | |
tmp[i] = a[n][1] | |
i++ | |
} | |
tmp[i + 1] = b[1] | |
return ["slots", tmp] | |
} else { | |
return ["slots", []] | |
} | |
} | |
Blockslot "Block slot" = Variable | |
BlockBody "Block body" = Void body:( | |
Unslot | |
/ Keyslot | |
/ Bislot | |
/ Literal | |
)* Void { | |
return body | |
} | |
Method = MethodStart slots:Blockslots "|" body:BlockBody MethodEnd { | |
return [slots, body] | |
} | |
MethodStart "Method start" = "(" | |
MethodEnd "Method end" = ")" | |
Literal "Literal" = Number / String / Array | |
Number = Hexadecimal / Octal / Float / Decimal / Zero | |
Float "Float" = head:[0-9]+ "." tail:[0-9]+ { | |
return parseFloat(head.join("") + "." + tail.join("")) | |
} | |
Decimal "Decimal" = head:[1-9] tail:[0-9]* { | |
return parseInt(head + tail.join(""), 10) | |
} | |
Zero "Decimal" = "0" { return 0 } | |
Hexadecimal "Hexadecimal" = "0x" hex:[0-9a-fA-F]+ { | |
return parseInt(hex.join(""), 16) | |
} | |
Octal "Octal" = "0" oct:[0-7]+ { | |
return parseInt(oct.join(""), 8) | |
} | |
String "String" = InterpolatedString / UninterpolatedString | |
InterpolatedString = '"' cont:(InterpolatedEscape / InterpolatedRaw)* '"' { | |
return cont.join("") | |
} | |
InterpolatedRaw = [^\\"] | |
InterpolatedEscape = "\\" esc:( | |
"a" { return "\u0007" } | |
/ "b" { return "\u0008" } | |
/ "f" { return "\u000c" } | |
/ "n" { return "\u000a" } | |
/ "r" { return "\u000d" } | |
/ "t" { return "\u0009" } | |
/ "v" { return "\u000b" } | |
/ "\\" { return "\\" } | |
/ '"' { return '"' } | |
) { return esc } | |
UninterpolatedString = "'" cont:(UninterpolatedEscape / UninterpolatedRaw)* "'" { | |
return cont.join("") | |
} | |
UninterpolatedRaw = [^\\'] | |
UninterpolatedEscape = "\\" esc:( | |
"\\" { return "\\" } | |
/ "'" { return "'" } | |
/ c:. { return "\\" + c } | |
) { return esc } | |
Array "Array" = "[" a:(Void Value Void ",")* b:(Void Value Void ","?)? Void "]" { | |
if (b != '') { | |
var tmp = [] | |
var i = 0 | |
for (n in a) { | |
tmp[i] = a[n][1] | |
i++ | |
} | |
tmp[i + 1] = b[1] | |
return ["array", tmp] | |
} else { | |
return ["array", []] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment