Tweaks to lexer, incl. matching func all at once

Reviewers: xymostech

Reviewed By: xymostech

Differential Revision: http://phabricator.benalpert.com/D42
This commit is contained in:
Ben Alpert 2013-07-07 21:27:08 -07:00
parent eab3322db3
commit 2643f72a9d
3 changed files with 25 additions and 52 deletions

View File

@ -65,11 +65,11 @@ var buildGroup = function(group, prev) {
var charLookup = {
"*": "\u2217",
"-": "\u2212",
"cdot": "\u22C5",
"lvert": "|",
"rvert": "|",
"pm": "\u00b1",
"div": "\u00f7"
"\\cdot": "\u22C5",
"\\lvert": "|",
"\\rvert": "|",
"\\pm": "\u00b1",
"\\div": "\u00f7"
};
var textit = function(value) {

View File

@ -1,13 +1,6 @@
var DEFAULT_STATE = 0,
FUNC_STATE = 1;
function Lexer() {
};
var funcs = [
'cdot', 'frac', 'lvert', 'rvert', 'pm', 'div'
];
var normals = [
[/^[/|a-zA-Z0-9.]/, 'ORD'],
[/^[*+-]/, 'BIN'],
@ -19,6 +12,11 @@ var normals = [
[/^[)\]]/, 'CLOSE']
];
var funcs = [
'cdot', 'frac', 'lvert', 'rvert', 'pm', 'div'
];
var anyFunc = new RegExp("^\\\\(" + funcs.join("|") + ")(?![a-zA-Z])");
Lexer.prototype.doMatch = function(match) {
this.yytext = match;
this.yyleng = match.length;
@ -27,46 +25,31 @@ Lexer.prototype.doMatch = function(match) {
this.yylloc.last_column = this._pos + match.length;
this._pos += match.length;
this._input = this._input.slice(match.length);
};
Lexer.prototype.lex = function() {
// Get rid of whitespace
var whitespace = this._input.substr(this._pos).match(/^\s*/)[0];
var whitespace = this._input.match(/^\s*/)[0];
this._pos += whitespace.length;
this._input = this._input.slice(whitespace.length);
if (this._pos >= this._input.length) {
if (this._input.length === 0) {
return 'EOF';
}
var toMatch = this._input.substr(this._pos);
var match;
if (this.state === DEFAULT_STATE) {
if (/^\\/.test(toMatch)) {
this.state = FUNC_STATE;
this.doMatch('\\');
return '\\';
} else {
for (var i = 0; i < normals.length; i++) {
var normal = normals[i];
if ((match = this._input.match(anyFunc))) {
this.doMatch(match[0]);
return match[1];
} else {
for (var i = 0; i < normals.length; i++) {
var normal = normals[i];
var match = toMatch.match(normal[0]);
if (match) {
this.doMatch(match[0]);
return normal[1];
}
}
}
} else if (this.state === FUNC_STATE) {
for (var i = 0; i < funcs.length; i++) {
var func = funcs[i];
var regex = new RegExp('^' + func + '(?!a-zA-Z)');
var match = toMatch.match(regex);
if (match) {
if ((match = this._input.match(normal[0]))) {
this.doMatch(match[0]);
this.state = DEFAULT_STATE;
return func;
return normal[1];
}
}
}
@ -87,8 +70,6 @@ Lexer.prototype.setInput = function(input) {
last_line: 1,
last_column: 0
};
this.state = DEFAULT_STATE;
};
module.exports = new Lexer();

View File

@ -1,13 +1,5 @@
/* description: Parses end executes mathematical expressions. */
/* lexical grammar */
%lex
%%
<<EOF>> return 'EOF'
/lex
/* operator associations and precedence */
%left '^'
@ -45,8 +37,8 @@ group
{$$ = $1;}
| '{' ex '}'
{$$ = $2;}
| '\' func
{$$ = $2;}
| func
{$$ = $1;}
;
func