Skip to content

Instantly share code, notes, and snippets.

@Diullei
Created June 19, 2015 14:24
Show Gist options
  • Save Diullei/0334c7386a911364745c to your computer and use it in GitHub Desktop.
Save Diullei/0334c7386a911364745c to your computer and use it in GitHub Desktop.
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