Skip to content

Instantly share code, notes, and snippets.

@itsbth
Created January 5, 2012 19:58
Show Gist options
  • Select an option

  • Save itsbth/1566943 to your computer and use it in GitHub Desktop.

Select an option

Save itsbth/1566943 to your computer and use it in GitHub Desktop.
Uploaded by UploadToGist for Sublime Text 2
{Parser} = require 'jison'
# Stolen from coffee-script
# Since we're going to be wrapped in a function by Jison in any case, if our
# action immediately returns a value, we can optimize by removing the function
# wrapper and just returning the value directly.
unwrap = /^function\s*\(\)\s*\{\s*return\s*([\s\S]*);\s*\}/
# Our handy DSL for Jison grammar generation, thanks to
# [Tim Caswell](http://github.com/creationix). For every rule in the grammar,
# we pass the pattern-defining string, the action to run, and extra options,
# optionally. If no action is specified, we simply pass the value of the
# previous nonterminal.
o = (patternString, action, options) ->
patternString = patternString.replace /\s{2,}/g, ' '
return [patternString, '$$ = $1;', options] unless action
action = if match = unwrap.exec action then match[1] else "(#{action}())"
action = action.replace /\bnew /g, '$&yy.'
action = action.replace /\b(?:Block\.wrap|extend)\b/g, 'yy.$&'
[patternString, "$$ = #{action};", options]
t = (regex, name) ->
[regex.toString().slice(1, -1), if name then "return #{JSON.stringify(name)};" else "/* om nom nom */"]
grammar = lex: {}
grammar.lex.rules = [
t /\s+/
t /[0-9]+(?:\.[0-9]+)?\b/, 'NUMBER'
t /\*/, '*'
t /\//, '/'
t /\+/, '+'
t /\-/, '-'
t /\=/, '='
t /\(/, '('
t /\)/, ')'
t /\?/, '?'
t /:/, ':'
t /\{/, '{'
t /\}/, '}'
t /\[/, '['
t /\]/, ']'
t /if/, 'IF'
t /else/, 'ELSE'
t /elseif/, 'ELSEIF'
t /while/, 'WHILE'
t /[a-z][A-Za-z0-9_]+/, 'FUNC'
t /[A-Z][A-Za-z0-9_]+/, 'VAR'
t /@/, '@'
t /\n/, 'EOL'
]
grammar.bnf =
Root: [
o 'DirectiveList StatementList'
]
DirectiveList: [
o 'Directive DirectiveList'
o 'Directive'
o ''
]
Directive: [
o '@ VAR EOL'
]
StatementList: [
o 'Statement StatementList'
o 'Statement'
o ''
]
console.log JSON.stringify grammar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment