Created
June 19, 2015 14:24
-
-
Save Diullei/0334c7386a911364745c to your computer and use it in GitHub Desktop.
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
require! [jison, util] | |
Parser = jison.Parser | |
_ = (expr) -> | |
rx = // | |
^function\s*\(\)\s*\{\s* | |
( | |
[\s\S]* | |
); | |
\s*\} | |
//gmi | |
"#expr".replace rx, (, a) -> a.replace /var \$\$;/ '' | |
grammar = | |
lex : | |
rules: [ | |
[ "\\s+" '' ] | |
[ "[0-9]+(?:\\.[0-9]+)?\\b" _ -> \NUMBER ] | |
[ "=" _ -> \= ] | |
[ "::" _ -> \:: ] | |
[ "(number|string)\\b" _ -> \TYPE ] | |
[ "[a-zA-Z]+\\b" _ -> \ID ] | |
[ "$" _ -> \EOF ] | |
] | |
operators: [ | |
] | |
bnf: | |
expressions: [ | |
[ 'EXPR EOF' _ -> $1 ] | |
] | |
EXPR: [ | |
[ 'ID :: TYPE = NUMBER' _ !-> $$ = id: $1, op: \= type: $3, val: Number($5) ] | |
] | |
parser = new Parser grammar | |
parserSource = parser.generate! | |
result = parser.parse 'foo:: string = 4' | |
console.log util.inspect result, false, 10, true | |
/* | |
{ id: 'foo', | |
op: '=', | |
type: 'string', | |
val: 4 } | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment