Bigram
an experiment with parsing
Input | Rules |
---|---|
tokens = { “=” : [7], “==” : [6], “+” : [5], “-” : [5], “*” : [4], “/” : [4], “!” : [1, prefix], “~” : [1, prefix], “^” : [1], " " : [-1], “(” : [0, next], “)” : [0, “<”] };
for(var i = 0; i < 10; i += 1) tokens[i] = [0];
// each token is defined such [priority, param] // if param is a function it will be used to enter a scope // if param is “<” it will end a scope // if param is “ltr”/“rtl” the token will be processed as such
// this function gets our next token from input // this returns // name : name of the token // ltr : should this token be in left to right order // priority : priority of the token // scope : which scope should this token enter // exit : should this token end current scope
function next(root, input){ var token = input.shift(), action = tokens[token]; if(typeof action === “undefined” ){ return { name : token, ltr : leftToRight, priority : 0, }; }
var priority, param;
if( action instanceof Array ){
priority = parseFloat(action[0]);
param = action[1];
} else {
priority = parseFloat(action);
param = undefined;
}
if(typeof param === "function"){
return {
name : token,
ltr : leftToRight,
priority : priority,
scope : param
};
} else if (param === "<"){
return {
name : token,
ltr : leftToRight,
priority : priority,
exit : true
};
}
var ltr = leftToRight;
if(param === "ltr"){
ltr = true;
} else if (param === "rtl") {
ltr = false;
}
return {
name : token,
priority : priority,
ltr : ltr
};
}
// the last row should say what is // the first scope parser next;