adding precedence to the Jison parser

This commit is contained in:
Jeremy Ashkenas
2010-01-31 12:55:00 -05:00
parent 3a748755df
commit db00cd6ed4
2 changed files with 139 additions and 3 deletions

View File

@@ -1,5 +1,5 @@
(function(){
var Parser, __a, __b, __c, __d, __e, __f, bnf, grammar, name, non_terminal, o, option, parser, part, tokens, unwrap;
var Parser, __a, __b, __c, __d, __e, __f, bnf, grammar, name, non_terminal, o, operators, option, parser, part, tokens, unwrap;
var __hasProp = Object.prototype.hasOwnProperty;
Parser = require('jison').Parser;
// DSL ===================================================================
@@ -15,6 +15,8 @@
return [pattern_string, '$$ = $1;'];
}
};
// Precedence ===========================================================
operators = [["left", '?'], ["right", 'NOT', '!', '!!', '~', '++', '--'], ["left", '*', '/', '%'], ["left", '+', '-'], ["left", '<<', '>>', '>>>'], ["left", '&', '|', '^'], ["left", '<=', '<', '>', '>='], ["right", '==', '!=', 'IS', 'ISNT'], ["left", '&&', '||', 'AND', 'OR'], ["right", '-=', '+=', '/=', '*=', '%='], ["right", 'DELETE', 'INSTANCEOF', 'TYPEOF'], ["left", '.'], ["right", 'INDENT'], ["left", 'OUTDENT'], ["right", 'WHEN', 'LEADING_WHEN', 'IN', 'OF', 'BY'], ["right", 'THROW', 'FOR', 'NEW', 'SUPER'], ["left", 'EXTENDS'], ["left", '||=', '&&=', '?='], ["right", 'ASSIGN', 'RETURN'], ["right", '->', '=>', 'UNLESS', 'IF', 'ELSE', 'WHILE']];
// Grammar ==============================================================
grammar = {
// All parsing will end in this rule, being the trunk of the AST.
@@ -194,6 +196,59 @@
return new OpNode($2, $1, $3);
})
],
// Try abbreviated expressions to make the grammar build faster:
// UnaryOp: [
// o "!"
// o "!!"
// o "NOT"
// o "~"
// o "--"
// o "++"
// o "DELETE"
// o "TYPEOF"
// ]
//
// BinaryOp: [
// o "*"
// o "/"
// o "%"
// o "+"
// o "-"
// o "<<"
// o ">>"
// o ">>>"
// o "&"
// o "|"
// o "^"
// o "<="
// o "<"
// o ">"
// o ">="
// o "=="
// o "!="
// o "IS"
// o "ISNT"
// o "&&"
// o "||"
// o "AND"
// o "OR"
// o "?"
// o "-="
// o "+="
// o "/="
// o "*="
// o "%="
// o "||="
// o "&&="
// o "?="
// o "INSTANCEOF"
// o "IN"
// ]
//
// Operation: [
// o "Expression BinaryOp Expression", -> new OpNode($2, $1, $3)
// o "UnaryOp Expression", -> new OpNode($1, $2)
// ]
// The existence operator.
Existence: [o("Expression ?", function() {
return new ExistenceNode($1);
@@ -532,7 +587,8 @@
tokens = tokens.join(" ");
parser = new Parser({
tokens: tokens,
bnf: bnf
bnf: bnf,
operators: operators
}, {
debug: false
});