if (typeof exports !== 'undefined') { var window = {Unicode: require('./unicodecategories').Unicode}; exports.Tokenizer = Tokenizer; } /*! * Tokenizer for JavaScript / ECMAScript 5 * (c) Peter van der Zee, qfox.nl */ /** * @param {Object} inp * @param {Object} options * @property {boolean} [options.tagLiterals] Instructs the tokenizer to also parse tag literals */ function Tokenizer(inp, options){ this.inp = inp||''; // replace all other line terminators with \n (leave \r\n in tact though). we should probably remove the shadowInp when finished... // only replace \r if it is not followed by a \n else \r\n would become \n\n causing a double newline where it is just a single this.shadowInp = (inp||'').replace(Tokenizer.regexNormalizeNewlines, '\n'); this.pos = 0; this.line = 0; this.column = 0; this.cache = {}; this.errorStack = []; this.wtree = []; this.btree = []; // this.regexWhiteSpace = Tokenizer.regexWhiteSpace; this.regexLineTerminator = Tokenizer.regexLineTerminator; // used in fallback this.regexAsciiIdentifier = Tokenizer.regexAsciiIdentifier; this.hashAsciiIdentifier = Tokenizer.hashAsciiIdentifier; // this.regexHex = Tokenizer.regexHex; this.hashHex = Tokenizer.hashHex this.regexUnicodeEscape = Tokenizer.regexUnicodeEscape; this.regexIdentifierStop = Tokenizer.regexIdentifierStop; this.hashIdentifierStop = Tokenizer.hashIdentifierStop; // this.regexPunctuators = Tokenizer.regexPunctuators; this.regexNumber = Tokenizer.regexNumber; this.regexNewline = Tokenizer.regexNewline; this.regexBig = Tokenizer.regexBig; this.regexBigAlt = Tokenizer.regexBigAlt; // stuff for parsing tag literals this.regexTagName = Tokenizer.regexTagName; this.regexTagAttributes = Tokenizer.regexTagAttributes; this.regexTagUnarySuffix = Tokenizer.regexTagUnarySuffix; this.regexTagBinarySuffix = Tokenizer.regexTagBinarySuffix; this.regexTagBody = Tokenizer.regexTagBody; this.regexTagOpenOrClose = Tokenizer.regexTagOpenOrClose; this.regexTagClose = Tokenizer.regexTagClose; this.regexRemoveEscape = Tokenizer.regexRemoveEscape; this.tokenCount = 0; this.tokenCountNoWhite = 0; this.Unicode = window.Unicode; // if the Parser throws an error. it will set this property to the next match // at the time of the error (which was not what it was expecting at that point) // and pass on an "error" match. the error should be scooped on the stack and // this property should be returned, without looking at the input... this.errorEscape = null; // support tag literals this.tagLiterals = false || (options && options.tagLiterals); }; Tokenizer.prototype = { // token constants... (should use these some day) REGEX: 1, IDENTIFIER: 2, NUMERIC_HEX: 3, NUMERIC_DEC: 4, STRING_SINGLE: 5, STRING_DOUBLE: 6, COMMENT_SINGLE: 7, COMMENT_MULTI: 8, WHITE_SPACE: 9, LINETERMINATOR: 10, PUNCTUATOR: 11, EOF: 12, ASI: 13, ERROR: 14, TAG: 15, CURLY_METHOD: 16, inp:null, shadowInp:null, pos:null, line:null, column:null, cache:null, errorStack:null, wtree: null, // contains whitespace (spaces, comments, newlines) btree: null, // does not contain any whitespace tokens. regexLineTerminator:null, regexAsciiIdentifier:null, hashAsciiIdentifier:null, hashHex:null, regexUnicodeEscape:null, regexIdentifierStop:null, hashIdentifierStop:null, regexNumber:null, regexNewline:null, regexBig:null, regexBigAlt:null, tokenCount:null, tokenCountNoWhite:null, Unicode:null, tagLiterals: false, // custom tag literal support. allows
kind of (sub-expression) tokens // storeCurrentAndFetchNextToken(bool, false, false true) to get just one token storeCurrentAndFetchNextToken: function(noRegex, returnValue, stack, _dontStore){ var regex = !noRegex; // TOFIX :) var pos = this.pos; var inp = this.inp; var shadowInp = this.shadowInp; var matchedNewline = false; do { if (!_dontStore) { ++this.tokenCount; stack.push(returnValue); // did the parent Parser throw up? if (this.errorEscape) { returnValue = this.errorEscape; this.errorEscape = null; return returnValue; } } _dontStore = false; if (pos >= inp.length) { returnValue = {start:inp.length,stop:inp.length,name:12/*EOF*/}; break; } var returnValue = null; var start = pos; var chr = inp[pos]; // 1 ws 2 lt 3 scmt 4 mcmt 5/6 str 7 nr 8 rx 9 punc //if (true) { // substring method (I think this is faster..) var part2 = inp.substring(pos,pos+4); var part = this.regexBig.exec(part2); //} else { // // non-substring method (lastIndex) // // this method does not need a substring to apply it // this.regexBigAlt.lastIndex = pos; // var part = this.regexBigAlt.exec(inp); //} if (part[1]) { //this.regexWhiteSpace.test(chr)) { // SP, TAB, VT, FF, NBSP, BOM (, TOFIX: USP) ++pos; returnValue = {start:start,stop:pos,name:9/*WHITE_SPACE*/,line:this.line,col:this.column,isWhite:true}; ++this.column; } else if (part[2]) { //this.regexLineTerminator.test(chr)) { // LF, CR, LS, PS var end = pos+1; if (chr=='\r' && inp[pos+1] == '\n') ++end; // support crlf=>lf returnValue = {start:pos,stop:end,name:10/*LINETERMINATOR*/,line:this.line,col:this.column,isWhite:true}; pos = end; // mark newlines for ASI matchedNewline = true; ++this.line; this.column = 0; returnValue.hasNewline = 1; } else if (part[3]) { //chr == '/' && inp[pos+1] == '/') { pos = shadowInp.indexOf('\n',pos); if (pos == -1) pos = inp.length; returnValue = {start:start,stop:pos,name:7/*COMMENT_SINGLE*/,line:this.line,col:this.column,isComment:true,isWhite:true}; this.column = returnValue.stop; } else if (part[4]) { //chr == '/' && inp[pos+1] == '*') { var newpos = inp.indexOf('*/',pos); if (newpos == -1) { newpos = shadowInp.indexOf('\n', pos); if (newpos < 0) pos += 2; else pos = newpos; returnValue = {start:start,stop:pos,name:14/*error*/,value:inp.substring(start, pos),line:this.line,col:this.column,isComment:true,isWhite:true,tokenError:true,error:Tokenizer.Error.UnterminatedMultiLineComment}; this.errorStack.push(returnValue); } else { pos = newpos+2; returnValue = {start:start,stop:pos,name:8/*COMMENT_MULTI*/,value:inp.substring(start, pos),line:this.line,col:this.column,isComment:true,isWhite:true}; // multi line comments are also reason for asi, but only if they contain at least one newline (use shadow input, because all line terminators would be valid...) var shadowValue = shadowInp.substring(start, pos); var i = 0, hasNewline = 0; while (i < (i = shadowValue.indexOf('\n', i+1))) { ++hasNewline; } if (hasNewline) { matchedNewline = true; returnValue.hasNewline = hasNewline; this.line += hasNewline; this.column = 0; } else { this.column = returnValue.stop; } } } else if (part[5]) { //chr == "'") { // old method //console.log("old method"); var hasNewline = 0; do { // process escaped characters while (pos < inp.length && inp[++pos] == '\\') { if (shadowInp[pos+1] == '\n') ++hasNewline; ++pos; } if (this.regexLineTerminator.test(inp[pos])) { returnValue = {start:start,stop:pos,name:14/*error*/,value:inp.substring(start, pos),isString:true,tokenError:true,error:Tokenizer.Error.UnterminatedDoubleStringNewline}; this.errorStack.push(returnValue); break; } } while (pos < inp.length && inp[pos] != "'"); if (returnValue) {} // error else if (inp[pos] != "'") { returnValue = {start:start,stop:pos,name:14/*error*/,value:inp.substring(start, pos),isString:true,tokenError:true,error:Tokenizer.Error.UnterminatedDoubleStringOther}; this.errorStack.push(returnValue); } else { ++pos; returnValue = {start:start,stop:pos,name:5/*STRING_SINGLE*/,isPrimitive:true,isString:true}; if (hasNewline) { returnValue.hasNewline = hasNewline; this.line += hasNewline; this.column = 0; } else { this.column += (pos-start); } } } else if (part[6]) { //chr == '"') { var hasNewline = 0; // TODO: something like this: var regexmatch = /([^\']|$)+/.match(); do { // process escaped chars while (pos < inp.length && inp[++pos] == '\\') { if (shadowInp[pos+1] == '\n') ++hasNewline; ++pos; } if (this.regexLineTerminator.test(inp[pos])) { returnValue = {start:start,stop:pos,name:14/*error*/,value:inp.substring(start, pos),isString:true,tokenError:true,error:Tokenizer.Error.UnterminatedSingleStringNewline}; this.errorStack.push(returnValue); break; } } while (pos < inp.length && inp[pos] != '"'); if (returnValue) {} else if (inp[pos] != '"') { returnValue = {start:start,stop:pos,name:14/*error*/,value:inp.substring(start, pos),isString:true,tokenError:true,error:Tokenizer.Error.UnterminatedSingleStringOther}; this.errorStack.push(returnValue); } else { ++pos; returnValue = {start:start,stop:pos,name:6/*STRING_DOUBLE*/,isPrimitive:true,isString:true}; if (hasNewline) { returnValue.hasNewline = hasNewline; this.line += hasNewline; this.column = 0; } else { this.column += (pos-start); } } } else if (part[7]) { //(chr >= '0' && chr <= '9') || (chr == '.' && inp[pos+1] >= '0' && inp[pos+1] <= '9')) { var nextPart = inp.substring(pos, pos+30); var match = nextPart.match(this.regexNumber); if (match[2]) { // decimal var value = match[2]; var parsingOctal = value[0] == '0' && value[1] && value[1] != 'e' && value[1] != 'E' && value[1] != '.'; if (parsingOctal) { returnValue = {start:start,stop:pos,name:14/*error*/,isNumber:true,isOctal:true,tokenError:true,error:Tokenizer.Error.IllegalOctalEscape,value:value}; this.errorStack.push(returnValue); } else { returnValue = {start:start,stop:start+value.length,name:4/*NUMERIC_DEC*/,isPrimitive:true,isNumber:true,value:value}; } } else if (match[1]) { // hex var value = match[1]; returnValue = {start:start,stop:start+value.length,name:3/*NUMERIC_HEX*/,isPrimitive:true,isNumber:true,value:value}; } else { throw 'unexpected parser errror... regex fail :('; } if (value.length < 300) { pos += value.length; } else { // old method of parsing numbers. only used for extremely long number literals (300+ chars). // this method does not require substringing... just memory :) var tmpReturnValue = this.oldNumberParser(pos, chr, inp, returnValue, start, Tokenizer); pos = tmpReturnValue[0]; returnValue = tmpReturnValue[1]; } } else if (regex && part[8]) { //chr == '/') { // regex cannot start with /* (would be multiline comment, and not make sense anyways). but if it was /* then an earlier if would have eated it. so we only check for / var twinfo = []; // matching {[( info var found = false; var parens = []; var nonLethalError = null; while (++pos < inp.length) { chr = shadowInp[pos]; // parse RegularExpressionChar if (chr == '\n') { returnValue = {start:start,stop:pos,name:14/*error*/,tokenError:true,errorHasContent:true,error:Tokenizer.Error.UnterminatedRegularExpressionNewline}; this.errorStack.push(returnValue); break; // fail } else if (chr == '/') { found = true; break; } else if (chr == '?' || chr == '*' || chr == '+') { nonLethalError = Tokenizer.Error.NothingToRepeat; } else if (chr == '^') { if ( inp[pos-1] != '/' && inp[pos-1] != '|' && inp[pos-1] != '(' && !(inp[pos-3] == '(' && inp[pos-2] == '?' && (inp[pos-1] == ':' || inp[pos-1] == '!' || inp[pos-1] == '=')) ) { nonLethalError = Tokenizer.Error.StartOfMatchShouldBeAtStart; } } else if (chr == '$') { if (inp[pos+1] != '/' && inp[pos+1] != '|' && inp[pos+1] != ')') nonLethalError = Tokenizer.Error.DollarShouldBeEnd; } else if (chr == '}') { nonLethalError = Tokenizer.Error.MissingOpeningCurly; } else { // it's a "character" (can be group or class), something to match // match parenthesis if (chr == '(') { parens.push(pos-start); } else if (chr == ')') { if (parens.length == 0) { nonLethalError = {start:start,stop:pos,name:14/*error*/,tokenError:true,error:Tokenizer.Error.RegexNoOpenGroups}; } else { var twin = parens.pop(); var now = pos-start; twinfo[twin] = now; twinfo[now] = twin; } } // first process character class if (chr == '[') { var before = pos-start; while (++pos < inp.length && shadowInp[pos] != '\n' && inp[pos] != ']') { // only newline is not allowed in class range // anything else can be escaped, most of it does not have to be escaped... if (inp[pos] == '\\') { if (shadowInp[pos+1] == '\n') break; else ++pos; // skip next char. (mainly prohibits ] to be picked up as closing the group...) } } if (inp[pos] != ']') { returnValue = {start:start,stop:pos,name:14/*error*/,tokenError:true,error:Tokenizer.Error.ClosingClassRangeNotFound}; this.errorStack.push(returnValue); break; } else { var after = pos-start; twinfo[before] = after; twinfo[after] = before; } } else if (chr == '\\' && shadowInp[pos+1] != '\n') { // is ok anywhere in the regex (match next char literally, regardless of its otherwise special meaning) ++pos; } // now process repeaters (+, ? and *) // non-collecting group (?:...) and positive (?=...) or negative (?!...) lookahead if (chr == '(') { if (inp[pos+1] == '?' && (inp[pos+2] == ':' || inp[pos+2] == '=' || inp[pos+2] == '!')) { pos += 2; } } // matching "char" else if (inp[pos+1] == '?') ++pos; else if (inp[pos+1] == '*' || inp[pos+1] == '+') { ++pos; if (inp[pos+1] == '?') ++pos; // non-greedy match } else if (inp[pos+1] == '{') { pos += 1; var before = pos-start; // quantifier: // - {n} // - {n,} // - {n,m} if (!/[0-9]/.test(inp[pos+1])) { nonLethalError = Tokenizer.Error.QuantifierRequiresNumber; } while (++pos < inp.length && /[0-9]/.test(inp[pos+1])); if (inp[pos+1] == ',') { ++pos; while (pos < inp.length && /[0-9]/.test(inp[pos+1])) ++pos; } if (inp[pos+1] != '}') { nonLethalError = Tokenizer.Error.QuantifierRequiresClosingCurly; } else { ++pos; var after = pos-start; twinfo[before] = after; twinfo[after] = before; if (inp[pos+1] == '?') ++pos; // non-greedy match } } } } // if found=false, fail right now. otherwise try to parse an identifiername (that's all RegularExpressionFlags is..., but it's constructed in a stupid fashion) if (!found || returnValue) { if (!returnValue) { returnValue = {start:start,stop:pos,name:14/*error*/,tokenError:true,error:Tokenizer.Error.UnterminatedRegularExpressionOther}; this.errorStack.push(returnValue); } } else { // this is the identifier scanner, for now do ++pos; while (pos < inp.length && this.hashAsciiIdentifier[inp[pos]]); /*this.regexAsciiIdentifier.test(inp[pos])*/ if (parens.length) { // nope, this is still an error, there was at least one paren that did not have a matching twin if (parens.length > 0) returnValue = {start:start,stop:pos,name:14/*error*/,tokenError:true,error:Tokenizer.Error.RegexOpenGroup}; this.errorStack.push(returnValue); } else if (nonLethalError) { returnValue = {start:start,stop:pos,name:14/*error*/,errorHasContent:true,tokenError:true,error:nonLethalError}; this.errorStack.push(returnValue); } else { returnValue = {start:start,stop:pos,name:1/*REG_EX*/,isPrimitive:true}; } } returnValue.twinfo = twinfo; } else if (regex && part[9] && this.tagLiterals) { // allows you to use this literally (in places where an expression is allowed) in js: // simple tag: // // tree, unary, content, multiline: //