Created
March 16, 2009 22:06
-
-
Save thomaslang/80120 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var buildTokenTree = function (tokenList) { | |
| //var tokenList = tokenList; | |
| var openParenthesisStack = []; | |
| var booleanOperatorStack = []; | |
| //var currentToken = null; | |
| for (var i=0; i < tokenList.length; i++) { | |
| //currentToken = tokenList[i]; | |
| switch ( tokenList[i].tokenType ) { | |
| case "OPEN_PAREN" : | |
| openParenthesisStack.push(i); | |
| break; | |
| case "CLOSE_PAREN" : | |
| i = resolveParenthesesPair(i); | |
| break; | |
| case "COMPARATOR" : | |
| i = this.resolveComparison(i); | |
| break; | |
| case "BOOL_OP" : | |
| booleanOperatorStack.push(i); | |
| break; | |
| } | |
| }; | |
| var resolveParenthesesPair = function (closeParenthesisPosition) { | |
| }; | |
| var resolveComparison = function (comparatorPosition) { | |
| var p = comparatorPosition; | |
| var r = 0; //counts number of removed tokens preceeding comparator | |
| if ( preceedingTokenIs('WORD',p) && followingTokenIs('STRING',p) ) { | |
| tokenList[p].left = tokenList[p-1]; | |
| tokenList[p].right = tokenList[p+1]; | |
| removeToken(p-1); | |
| removeToken(p+1); | |
| r = r+1; | |
| }; | |
| return p-r; | |
| }; | |
| var resolveBooleanOperator = function (booleanOperatorPosition) { | |
| }; | |
| var preceedingTokenIs = function (tokenType, currentTokenPosition) { | |
| if (currentTokenPosition == 0) return false; | |
| if (tokenList[currentTokenPosition-1].tokenType == tokenType) return true; | |
| }; | |
| var followingTokenIs = function (tokenType, currentTokenPosition) { | |
| if (currentTokenPosition == tokenList.length-1) return false; | |
| if (tokenList[currentTokenPosition+1].tokenType == tokenType) return true; | |
| }; | |
| var removeToken = function (tokenPosition) { | |
| tokenList.splice(tokenPosition, 1); | |
| }; | |
| return tokenList; | |
| }; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment