| Line | Hits | Source |
|---|---|---|
| 1 | 1 | var Class = require('../util/Class'); |
| 2 | 1 | var character = require('../util/character'); |
| 3 | 1 | var Token = require('./Token'); |
| 4 | 1 | var Lexer = Class(function(rule) { |
| 5 | 258 | this.rule = rule; //当前语法规则 |
| 6 | 258 | this.init(); |
| 7 | }).methods({ | |
| 8 | init: function() { | |
| 9 | 259 | this.code = ''; //要解析的代码 |
| 10 | 259 | this.peek = ''; //向前看字符 |
| 11 | 259 | this.index = 0; //向前看字符字符索引 |
| 12 | 259 | this.isReg = Lexer.IS_REG; //当前/是否是perl风格正则表达式 |
| 13 | 259 | this.tokenList = []; //结果的token列表 |
| 14 | 259 | this.parentheseState = false; //(开始时标记之前终结符是否为if/for/while等关键字 |
| 15 | 259 | this.parentheseStack = []; //圆括号深度记录当前是否为if/for/while等语句内部 |
| 16 | 259 | this.cacheLine = 0; //行缓存值 |
| 17 | 259 | this.totalLine = 1; //总行数 |
| 18 | 259 | this.colNum = 0; //列 |
| 19 | 259 | this.colMax = 0; //最大列数 |
| 20 | }, | |
| 21 | parse: function(code) { | |
| 22 | 215 | this.code = code || ''; |
| 23 | 215 | var temp = []; |
| 24 | 215 | this.scan(temp); |
| 25 | 206 | return temp; |
| 26 | }, | |
| 27 | parseOn: function() { | |
| 28 | 1 | var temp = []; |
| 29 | 1 | this.scan(temp); |
| 30 | 1 | return temp; |
| 31 | }, | |
| 32 | tokens: function() { | |
| 33 | 180 | return this.tokenList; |
| 34 | }, | |
| 35 | scan: function(temp) { | |
| 36 | 216 | var perlReg = this.rule.perlReg(); |
| 37 | 216 | var length = this.code.length; |
| 38 | 216 | var count = 0; |
| 39 | outer: | |
| 40 | while(this.index < length) { | |
| 41 | 454611 | if(this.cacheLine > 0 && count >= this.cacheLine) { |
| 42 | 1 | break; |
| 43 | } | |
| 44 | 454610 | this.readch(); |
| 45 | //perl风格正则 | |
| 46 | 454610 | if(perlReg && this.isReg == Lexer.IS_REG && this.peek == character.SLASH && !{ '/': true, '*': true }[this.code.charAt(this.index)]) { |
| 47 | 493 | this.dealReg(temp, length); |
| 48 | 489 | this.isReg = Lexer.NOT_REG; |
| 49 | } | |
| 50 | //依次遍历匹配规则,命中则继续 | |
| 51 | else { | |
| 52 | 454117 | for(var i = 0, matches = this.rule.matches(), len = matches.length; i < len; i++) { |
| 53 | 7446499 | var match = matches[i]; |
| 54 | 7446499 | if(match.match(this.peek, this.code, this.index)) { |
| 55 | 454116 | var token = new Token(match.tokenType(), match.content(), match.val(), this.index - 1); |
| 56 | 454116 | var error = match.error(); |
| 57 | 454116 | var matchLen = match.content().length; |
| 58 | 454116 | if(token.type() == Token.ID && this.rule.keyWords().hasOwnProperty(token.content())) { |
| 59 | 21489 | token.type(Token.KEYWORD); |
| 60 | } | |
| 61 | 454116 | temp.push(token); |
| 62 | 454116 | this.tokenList.push(token); |
| 63 | 454116 | this.index += matchLen - 1; |
| 64 | 454116 | var n = character.count(token.val(), character.LINE); |
| 65 | 454116 | count += n; |
| 66 | 454116 | this.totalLine += n; |
| 67 | 454116 | if(n) { |
| 68 | 32320 | var j = match.content().indexOf(character.LINE); |
| 69 | 32320 | var k = match.content().lastIndexOf(character.LINE); |
| 70 | 32320 | this.colMax = Math.max(this.colMax, this.colNum + j); |
| 71 | 32320 | this.colNum = match.content().length - k; |
| 72 | } | |
| 73 | else { | |
| 74 | 421796 | this.colNum += matchLen; |
| 75 | } | |
| 76 | 454116 | this.colMax = Math.max(this.colMax, this.colNum); |
| 77 | 454116 | if(error) { |
| 78 | 5 | this.error(error, this.code.slice(this.index - matchLen, this.index)); |
| 79 | } | |
| 80 | //支持perl正则需判断关键字、圆括号对除号语义的影响 | |
| 81 | 454112 | if(perlReg && match.perlReg() != Lexer.IGNORE) { |
| 82 | 258338 | if(match.perlReg() == Lexer.SPECIAL) { |
| 83 | 96063 | this.isReg = match.special(); |
| 84 | } | |
| 85 | else { | |
| 86 | 162275 | this.isReg = match.perlReg(); |
| 87 | } | |
| 88 | 258338 | if(this.peek == character.LEFT_PARENTHESE) { |
| 89 | 19887 | this.parentheseStack.push(this.parentheseState); |
| 90 | 19887 | this.parentheseState = false; |
| 91 | } | |
| 92 | 238451 | else if(this.peek == character.RIGHT_PARENTHESE) { |
| 93 | 19870 | this.isReg = this.parentheseStack.pop() ? Lexer.IS_REG : Lexer.NOT_REG; |
| 94 | } | |
| 95 | else { | |
| 96 | 218581 | this.parentheseState = match.parenthese(); |
| 97 | } | |
| 98 | } | |
| 99 | 454112 | continue outer; |
| 100 | } | |
| 101 | } | |
| 102 | //如果有未匹配的,说明规则不完整,加入other类型并抛出警告 | |
| 103 | 1 | this.error('unknow token'); |
| 104 | } | |
| 105 | } | |
| 106 | 207 | return this; |
| 107 | }, | |
| 108 | readch: function() { | |
| 109 | 463472 | this.peek = this.code.charAt(this.index++); |
| 110 | }, | |
| 111 | dealReg: function(temp, length) { | |
| 112 | 493 | var lastIndex = this.index - 1; |
| 113 | 493 | var res = false; |
| 114 | outer: | |
| 115 | do { | |
| 116 | 6803 | this.readch(); |
| 117 | 6803 | if(this.peek == character.LINE) { |
| 118 | 2 | this.error('SyntaxError: unterminated regular expression literal ' + this.peek, this.code.slice(lastIndex, this.index)); |
| 119 | 1 | break; |
| 120 | } | |
| 121 | 6801 | else if(this.peek == character.BACK_SLASH) { |
| 122 | 490 | this.index++; |
| 123 | } | |
| 124 | 6311 | else if(this.peek == character.LEFT_BRACKET) { |
| 125 | 264 | do { |
| 126 | 1368 | this.readch(); |
| 127 | 1368 | if(this.peek == character.LINE) { |
| 128 | 2 | this.error('SyntaxError: unterminated regular expression literal ' + this.peek, this.code.slice(lastIndex, this.index)); |
| 129 | 1 | break outer; |
| 130 | } | |
| 131 | 1366 | else if(this.peek == character.BACK_SLASH) { |
| 132 | 265 | this.index++; |
| 133 | } | |
| 134 | 1101 | else if(this.peek == character.RIGHT_BRACKET) { |
| 135 | 262 | continue outer; |
| 136 | } | |
| 137 | } while(this.index < length); | |
| 138 | } | |
| 139 | 6047 | else if(this.peek == character.SLASH) { |
| 140 | 488 | res = true; |
| 141 | 488 | var hash = {}; |
| 142 | 488 | var flag = { |
| 143 | 'g': true, | |
| 144 | 'i': true, | |
| 145 | 'm': true, | |
| 146 | 'y': true | |
| 147 | }; | |
| 148 | //正则的flag中有gimy4种,大小写敏感且不能重复 | |
| 149 | 488 | do { |
| 150 | 691 | this.readch(); |
| 151 | 691 | if(character.isLetter(this.peek)) { |
| 152 | 205 | if(hash.hasOwnProperty(this.peek) || !flag.hasOwnProperty(this.peek)) { |
| 153 | 2 | this.error('SyntaxError: invalid regular expression flag ' + this.peek, this.code.slice(lastIndex, this.index)); |
| 154 | 1 | break outer; |
| 155 | } | |
| 156 | 203 | hash[this.peek] = true; |
| 157 | } | |
| 158 | else { | |
| 159 | 486 | break outer; |
| 160 | } | |
| 161 | } while(this.index <= length); | |
| 162 | } | |
| 163 | } while(this.index < length); | |
| 164 | 490 | if(!res) { |
| 165 | 3 | this.error('SyntaxError: unterminated regular expression literal', this.code.slice(lastIndex, this.index - 1)); |
| 166 | } | |
| 167 | 489 | var token = new Token(Token.REG, this.code.slice(lastIndex, --this.index), lastIndex); |
| 168 | 489 | temp.push(token); |
| 169 | 489 | this.tokenList.push(token); |
| 170 | 489 | this.colNum += this.index - lastIndex; |
| 171 | 489 | this.colMax = Math.max(this.colMax, this.colNum); |
| 172 | 489 | return this; |
| 173 | }, | |
| 174 | cache: function(i) { | |
| 175 | 1 | if(!character.isUndefined(i) && i !== null) { |
| 176 | 1 | this.cacheLine = i; |
| 177 | } | |
| 178 | 1 | return this.cacheLine; |
| 179 | }, | |
| 180 | finish: function() { | |
| 181 | 2 | return this.index >= this.code.length; |
| 182 | }, | |
| 183 | line: function() { | |
| 184 | 16 | return this.totalLine; |
| 185 | }, | |
| 186 | col: function() { | |
| 187 | 1 | return this.colMax; |
| 188 | }, | |
| 189 | error: function(s, str) { | |
| 190 | 15 | if(character.isUndefined(str)) { |
| 191 | 1 | str = this.code.substr(this.index - 1, 20); |
| 192 | } | |
| 193 | 15 | if(Lexer.mode() === Lexer.STRICT) { |
| 194 | 9 | throw new Error(s + ', line ' + this.line() + ' col ' + this.colNum + '\n' + str); |
| 195 | } | |
| 196 | 6 | else if(Lexer.mode() === Lexer.LOOSE && !character.isUndefined(console)) { |
| 197 | 6 | if(console.warn) { |
| 198 | 6 | console.warn(s + ', line ' + this.line() + ' col ' + this.colNum + '\n' + str); |
| 199 | } | |
| 200 | } | |
| 201 | 6 | return this; |
| 202 | } | |
| 203 | }).statics({ | |
| 204 | IGNORE: 0, | |
| 205 | IS_REG: 1, | |
| 206 | NOT_REG: 2, | |
| 207 | SPECIAL: 3, | |
| 208 | STRICT: 0, | |
| 209 | LOOSE: 1, | |
| 210 | mode: function(i) { | |
| 211 | 33 | if(!character.isUndefined(i)) { |
| 212 | 10 | cmode = i; |
| 213 | } | |
| 214 | 33 | return cmode; |
| 215 | } | |
| 216 | }); | |
| 217 | 1 | var cmode = Lexer.STRICT; |
| 218 | 1 | module.exports = Lexer; |
| Line | Hits | Source |
|---|---|---|
| 1 | 1 | var Class = require('../util/Class'); |
| 2 | 1 | var character = require('../util/character'); |
| 3 | 1 | var tid = 0; |
| 4 | 1 | var types; |
| 5 | 1 | var Token = Class(function(type, content, val, sIndex) { |
| 6 | 456191 | this.t = type; //token类型 |
| 7 | 456191 | this.c = content; //token的字面内容,string包括头尾的引号 |
| 8 | 456191 | if(character.isNumber(val)) { |
| 9 | 494 | sIndex = val; |
| 10 | 494 | val = content; |
| 11 | } | |
| 12 | 455697 | else if(character.isUndefined(val)) { |
| 13 | 1581 | val = content; |
| 14 | 1581 | sIndex = -1; |
| 15 | } | |
| 16 | 456191 | this.v = val; //token的值,一般情况下等于content,特殊如string情况下值是不加头尾的引号 |
| 17 | 456191 | this.id = tid++; //token的索引 |
| 18 | 456191 | this.si = sIndex; //token在源码字符串中的索引 |
| 19 | }).methods({ | |
| 20 | type: function(t) { | |
| 21 | 1636563 | if(!character.isUndefined(t)) { |
| 22 | 21489 | this.t = t; |
| 23 | } | |
| 24 | 1636563 | return this.t; |
| 25 | }, | |
| 26 | content: function(c) { | |
| 27 | 1667743 | if(!character.isUndefined(c)) { |
| 28 | 1 | this.c = c; |
| 29 | } | |
| 30 | 1667743 | return this.c; |
| 31 | }, | |
| 32 | val: function(v) { | |
| 33 | 454118 | if(!character.isUndefined(v)) { |
| 34 | 1 | this.v = v; |
| 35 | } | |
| 36 | 454118 | return this.v; |
| 37 | }, | |
| 38 | tag: function(t) { | |
| 39 | 2 | if(!character.isUndefined(t)) { |
| 40 | 1 | this.t = t; |
| 41 | } | |
| 42 | 2 | return Token.type(this.t); |
| 43 | }, | |
| 44 | tid: function(id) { | |
| 45 | 2 | if(!character.isUndefined(id)) { |
| 46 | 1 | this.id = id; |
| 47 | } | |
| 48 | 2 | return this.id; |
| 49 | }, | |
| 50 | sIndex: function(si) { | |
| 51 | 6 | if(!character.isUndefined(si)) { |
| 52 | 1 | this.si = si; |
| 53 | } | |
| 54 | 6 | return this.si; |
| 55 | } | |
| 56 | }).statics({ | |
| 57 | IGNORE: -2, | |
| 58 | VIRTUAL: -1, | |
| 59 | OTHER: 0, | |
| 60 | BLANK: 1, | |
| 61 | TAB: 2, | |
| 62 | LINE: 3, | |
| 63 | NUMBER: 4, | |
| 64 | ID: 5, | |
| 65 | COMMENT: 6, | |
| 66 | STRING: 7, | |
| 67 | SIGN: 8, | |
| 68 | REG: 9, | |
| 69 | KEYWORD: 10, | |
| 70 | ANNOT: 11, | |
| 71 | HEAD: 12, | |
| 72 | TEMPLATE: 13, | |
| 73 | ENTER: 14, | |
| 74 | PROPERTY: 15, | |
| 75 | VARS: 16, | |
| 76 | HACK: 17, | |
| 77 | IMPORTANT: 18, | |
| 78 | type: function(tag) { | |
| 79 | 6 | if(character.isUndefined(types)) { |
| 80 | 1 | types = []; |
| 81 | 1 | Object.keys(Token).forEach(function(o) { |
| 82 | 25 | if(typeof Token[o] == 'number') { |
| 83 | 21 | types[Token[o]] = o; |
| 84 | } | |
| 85 | }); | |
| 86 | } | |
| 87 | 6 | return types[tag]; |
| 88 | } | |
| 89 | }); | |
| 90 | 1 | module.exports = Token; |
| Line | Hits | Source |
|---|---|---|
| 1 | 1 | var Match = require('./Match'); |
| 2 | 1 | var character = require('../../util/character'); |
| 3 | 1 | var CharacterSet = Match.extend(function(type, str, setPReg) { |
| 4 | 512 | Match.call(this, type, setPReg); |
| 5 | 512 | this.str = str; |
| 6 | }).methods({ | |
| 7 | match: function(c, code, index) { | |
| 8 | 141011 | var isIn = this.str.indexOf(c) > -1; |
| 9 | 141011 | if(isIn) { |
| 10 | 133495 | this.result = c; |
| 11 | } | |
| 12 | 141011 | return isIn; |
| 13 | } | |
| 14 | }); | |
| 15 | 1 | module.exports = CharacterSet; |
| Line | Hits | Source |
|---|---|---|
| 1 | 1 | var Match = require('./Match'); |
| 2 | 1 | var character = require('../../util/character'); |
| 3 | 1 | var CompleteEqual = Match.extend(function(type, result, setPReg) { |
| 4 | 8576 | Match.call(this, type, setPReg); |
| 5 | 8576 | this.result = result; |
| 6 | }).methods({ | |
| 7 | match: function(c, code, index) { | |
| 8 | 5598603 | return code.substr(--index, this.result.length) == this.result; |
| 9 | } | |
| 10 | }); | |
| 11 | 1 | module.exports = CompleteEqual; |
| Line | Hits | Source |
|---|---|---|
| 1 | 1 | var Match = require('./Match'); |
| 2 | 1 | var Token = require('../Token'); |
| 3 | 1 | var character = require('../../util/character'); |
| 4 | 1 | var LineParse = Match.extend(function(type, begin, end, mutiline, setPReg) { |
| 5 | 772 | if(character.isUndefined(mutiline)) { |
| 6 | 0 | mutiline = false; |
| 7 | } | |
| 8 | 772 | Match.call(this, type, setPReg); |
| 9 | 772 | this.begin = begin; |
| 10 | 772 | this.end = end; |
| 11 | 772 | this.msg = null; |
| 12 | 772 | this.mutiline = mutiline; |
| 13 | }).methods({ | |
| 14 | match: function(c, code, index) { | |
| 15 | 762999 | this.msg = null; |
| 16 | 762999 | if(this.begin == code.charAt(index - 1)) { |
| 17 | 6827 | var len = code.length, |
| 18 | lastIndex = index - 1, | |
| 19 | res = false; | |
| 20 | 6827 | while(index < len) { |
| 21 | 57358 | var c = code.charAt(index++); |
| 22 | //转义 | |
| 23 | 57358 | if(c == character.BACK_SLASH) { |
| 24 | 402 | if(code.charAt(index++) == character.ENTER) { |
| 25 | 1 | index++; |
| 26 | } | |
| 27 | } | |
| 28 | 56956 | else if(c == character.LINE && !this.mutiline) { |
| 29 | 1 | break; |
| 30 | } | |
| 31 | 56955 | else if(c == this.end) { |
| 32 | 6825 | res = true; |
| 33 | 6825 | break; |
| 34 | } | |
| 35 | } | |
| 36 | 6827 | if(!res) { |
| 37 | 2 | this.msg = 'SyntaxError: unterminated ' + Token.type(this.type).toLowerCase() + ' literal'; |
| 38 | } | |
| 39 | 6827 | this.result = code.slice(lastIndex, index); |
| 40 | 6827 | return true; |
| 41 | } | |
| 42 | 756172 | return false; |
| 43 | }, | |
| 44 | error: function() { | |
| 45 | 6827 | return this.msg; |
| 46 | }, | |
| 47 | val: function() { | |
| 48 | 6827 | return this.content().slice(this.begin.length, -this.end.length); |
| 49 | } | |
| 50 | }); | |
| 51 | 1 | module.exports = LineParse; |
| Line | Hits | Source |
|---|---|---|
| 1 | 1 | var Match = require('./Match'); |
| 2 | 1 | var Token = require('../Token'); |
| 3 | 1 | var character = require('../../util/character'); |
| 4 | 1 | var LineSearch = Match.extend(function(type, begin, end, contain, setPReg) { |
| 5 | 516 | if(character.isUndefined(contain)) { |
| 6 | 258 | contain = false; |
| 7 | } | |
| 8 | 516 | Match.call(this, type, setPReg); |
| 9 | 516 | this.begin = begin; |
| 10 | 516 | this.end = end; |
| 11 | 516 | this.contain = contain; |
| 12 | 516 | this.msg = null; |
| 13 | }).methods({ | |
| 14 | match: function(c, code, index) { | |
| 15 | 521900 | this.msg = null; |
| 16 | 521900 | if(this.begin == code.substr(--index, this.begin.length)) { |
| 17 | 5001 | var i = code.indexOf(this.end, index + this.begin.length); |
| 18 | 5001 | if(i == -1) { |
| 19 | 2 | if(this.contain) { |
| 20 | 1 | this.msg = 'SyntaxError: unterminated ' + Token.type(this.type).toLowerCase(); |
| 21 | } | |
| 22 | 2 | i = code.length; |
| 23 | } | |
| 24 | 4999 | else if(this.contain) { |
| 25 | 214 | i += this.end.length; |
| 26 | } | |
| 27 | 5001 | this.result = code.slice(index, i); |
| 28 | 5001 | return true; |
| 29 | } | |
| 30 | 516899 | return false; |
| 31 | }, | |
| 32 | error: function() { | |
| 33 | 5001 | return this.msg; |
| 34 | } | |
| 35 | }); | |
| 36 | 1 | module.exports = LineSearch; |
| Line | Hits | Source |
|---|---|---|
| 1 | 1 | var Class = require('../../util/Class'); |
| 2 | 1 | var character = require('../../util/character'); |
| 3 | 1 | var Lexer = require('../Lexer'); |
| 4 | 1 | module.exports = Class(function(type, setPReg, special, parenthese) { |
| 5 | 11428 | this.type = type; |
| 6 | 11428 | if(character.isUndefined(setPReg)) { |
| 7 | 1956 | setPReg = Lexer.IGNORE; |
| 8 | } | |
| 9 | 11428 | this.setPReg = setPReg; |
| 10 | 11428 | this.result = null; |
| 11 | //忽略0,是1,否2,特殊3 | |
| 12 | 11428 | if(setPReg) { |
| 13 | 9472 | if(character.isUndefined(special)) { |
| 14 | 9216 | special = function() { |
| 15 | 0 | return Lexer.IGNORE; |
| 16 | }; | |
| 17 | } | |
| 18 | 9472 | if(character.isUndefined(parenthese)) { |
| 19 | 9216 | parenthese = function() { |
| 20 | 122518 | return false; |
| 21 | }; | |
| 22 | } | |
| 23 | } | |
| 24 | 11428 | this.special = special; |
| 25 | 11428 | this.parenthese = parenthese; |
| 26 | }).methods({ | |
| 27 | tokenType: function() { | |
| 28 | 454116 | return this.type; |
| 29 | }, | |
| 30 | perlReg: function() { | |
| 31 | 874725 | return this.setPReg; |
| 32 | }, | |
| 33 | val: function() { | |
| 34 | 447289 | return this.content(); |
| 35 | }, | |
| 36 | content: function() { | |
| 37 | 1651434 | return this.result; |
| 38 | }, | |
| 39 | match: function(c, code, index) { | |
| 40 | //需被实现 | |
| 41 | 0 | throw new Error('match needs to be implement'); |
| 42 | }, | |
| 43 | error: function() { | |
| 44 | 338703 | return false; |
| 45 | } | |
| 46 | }); |
| Line | Hits | Source |
|---|---|---|
| 1 | 1 | var Match = require('./Match'); |
| 2 | 1 | var RegMatch = Match.extend(function(type, reg, valid, setPReg, special, parenthese) { |
| 3 | 1052 | if(typeof valid == 'number') { |
| 4 | 256 | parenthese = special; |
| 5 | 256 | special = setPReg; |
| 6 | 256 | setPReg = valid; |
| 7 | 256 | valid = null; |
| 8 | } | |
| 9 | 1052 | Match.call(this, type, setPReg, special, parenthese); |
| 10 | 1052 | this.reg = reg; |
| 11 | 1052 | this.valid = valid; |
| 12 | }).methods({ | |
| 13 | match: function(c, code, index) { | |
| 14 | 421986 | var self = this, |
| 15 | res = self.reg.exec(code.slice(index - 1)); | |
| 16 | 421986 | self.msg = null; |
| 17 | 421986 | if(res) { |
| 18 | 103585 | self.result = res[0]; |
| 19 | 103585 | if(self.valid) { |
| 20 | 7522 | for(var i = 0, keys = Object.keys(self.valid), len = keys.length; i < len; i++) { |
| 21 | 7522 | if(self.valid[keys[i]].test(self.result)) { |
| 22 | 2 | self.msg = keys[i]; |
| 23 | 2 | break; |
| 24 | } | |
| 25 | } | |
| 26 | } | |
| 27 | 103585 | return true; |
| 28 | } | |
| 29 | 318401 | return false; |
| 30 | }, | |
| 31 | error: function() { | |
| 32 | 103585 | return this.msg; |
| 33 | } | |
| 34 | }); | |
| 35 | 1 | module.exports = RegMatch; |
| Line | Hits | Source |
|---|---|---|
| 1 | 1 | var Rule = require('./Rule'); |
| 2 | 1 | var LineSearch = require('../match/LineSearch'); |
| 3 | 1 | var LineParse = require('../match/LineParse'); |
| 4 | 1 | var CompleteEqual = require('../match/CompleteEqual'); |
| 5 | 1 | var RegMatch = require('../match/RegMatch'); |
| 6 | 1 | var CharacterSet = require('../match/CharacterSet'); |
| 7 | 1 | var Token = require('../Token'); |
| 8 | 1 | var Lexer = require('../Lexer'); |
| 9 | 1 | var character = require('../../util/character'); |
| 10 | 1 | var EcmascriptRule = Rule.extend(function() { |
| 11 | 256 | var self = this; |
| 12 | 256 | Rule.call(self, EcmascriptRule.KEYWORDS, true); |
| 13 | ||
| 14 | 256 | self.addMatch(new CompleteEqual(Token.BLANK, character.BLANK)); |
| 15 | 256 | self.addMatch(new CompleteEqual(Token.TAB, character.TAB)); |
| 16 | 256 | self.addMatch(new CompleteEqual(Token.ENTER, character.ENTER)); |
| 17 | 256 | self.addMatch(new CompleteEqual(Token.LINE, character.LINE)); |
| 18 | ||
| 19 | 256 | self.addMatch(new LineSearch(Token.COMMENT, '//', '\n')); |
| 20 | 256 | self.addMatch(new LineSearch(Token.COMMENT, '/*', '*/', true)); |
| 21 | 256 | self.addMatch(new LineParse(Token.STRING, '"', '"', false, Lexer.IS_REG)); |
| 22 | 256 | self.addMatch(new LineParse(Token.STRING, "'", "'", false, Lexer.IS_REG)); |
| 23 | 256 | self.addMatch(new LineParse(Token.TEMPLATE, '`', '`', true, Lexer.IS_REG)); |
| 24 | ||
| 25 | 256 | self.addMatch(new RegMatch(Token.ID, /^[$a-zA-Z_][$\w]*/, Lexer.SPECIAL, function() { |
| 26 | 96063 | return !!(self.keyWords().hasOwnProperty(this.content())); |
| 27 | }, function() { | |
| 28 | 96063 | return ['if', 'for', 'while'].indexOf(this.content()) != -1; |
| 29 | })); | |
| 30 | ||
| 31 | 256 | self.addMatch(new RegMatch(Token.NUMBER, /^\.\d+(?:E[+-]?\d*)?/i, { |
| 32 | 'SyntaxError: missing exponent': /E[+-]?$/i | |
| 33 | }, Lexer.NOT_REG)); | |
| 34 | ||
| 35 | 256 | self.addMatch(new CompleteEqual(Token.SIGN, ']', Lexer.NOT_REG)); |
| 36 | ||
| 37 | 256 | ['*=', '/=', '+=', '-=', '%=', '^=', '&=', '|=', '&&', '--', '++', '===', '==', '!==', '!=', '||', '>>>=', '<<<=', '<<<', '>>>', '>>=', '<<=', '<<', '>>', '>=', '<=', '...', '?:'].forEach(function(o) { |
| 38 | 7168 | self.addMatch(new CompleteEqual(Token.SIGN, o, Lexer.IS_REG)); |
| 39 | }); | |
| 40 | 256 | self.addMatch(new CharacterSet(Token.SIGN, ':;/?.,[]{}~!^|%=-+*()~><&\\', Lexer.IS_REG)); |
| 41 | ||
| 42 | 256 | self.addMatch(new RegMatch(Token.NUMBER, /^0x[\da-f]*/i, { |
| 43 | "SyntaxError: missing hexadecimal digits after '0x'": /^0x$/i | |
| 44 | }, Lexer.NOT_REG)); | |
| 45 | 256 | self.addMatch(new RegMatch(Token.NUMBER, /^\d+\.?\d*(?:E[+-]?\d*)?/i, { |
| 46 | 'SyntaxError: missing exponent': /E[+-]?$/i | |
| 47 | }, Lexer.NOT_REG)); | |
| 48 | ||
| 49 | 256 | self.addMatch(new CharacterSet(Token.BLANK, '\u00a0\f\u000b\u200b\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000')); |
| 50 | }).statics({ | |
| 51 | KEYWORDS: 'abstract boolean break byte case catch class const continue debugger default delete do double else enum export extends false final finally float for function goto if implements import in instanceof int interface let long native new null package private protected public return short static super switch synchronized this throw throws transient true try typeof var void volatile while with'.split(' ') | |
| 52 | }); | |
| 53 | 1 | module.exports = EcmascriptRule; |
| Line | Hits | Source |
|---|---|---|
| 1 | 1 | var Class = require('../../util/Class'); |
| 2 | 1 | var Rule = Class(function(words, pReg) { |
| 3 | 258 | var self = this; |
| 4 | 258 | self.kw = {}; |
| 5 | 258 | words.forEach(function(o) { |
| 6 | 15474 | self.kw[o] = true; |
| 7 | }); | |
| 8 | 258 | self.pReg = pReg || false; |
| 9 | 258 | self.matchList = []; |
| 10 | }).methods({ | |
| 11 | perlReg: function() { | |
| 12 | 216 | return this.pReg; |
| 13 | }, | |
| 14 | addMatch: function(match) { | |
| 15 | 11428 | this.matchList.push(match); |
| 16 | 11428 | return this; |
| 17 | }, | |
| 18 | matches: function() { | |
| 19 | 454117 | return this.matchList; |
| 20 | }, | |
| 21 | keyWords: function() { | |
| 22 | 192126 | return this.kw; |
| 23 | } | |
| 24 | }); | |
| 25 | 1 | module.exports = Rule; |
| Line | Hits | Source |
|---|---|---|
| 1 | 1 | var Class = require('../../util/Class'); |
| 2 | 1 | var JsNode = require('./Node'); |
| 3 | 1 | var Token = require('../../lexer/Token'); |
| 4 | 1 | var Parser = require('./Parser'); |
| 5 | 1 | var id = 0; |
| 6 | 1 | var Context = Class(function(parent, name) { |
| 7 | 65 | this.id = id++; |
| 8 | 65 | this.parser = new Parser(); |
| 9 | 65 | this.parent = parent || null; //父上下文,如果是全局则为空 |
| 10 | 65 | this.name = name || null; //上下文名称,即函数名,函数表达式为空,全局也为空 |
| 11 | 65 | this.children = []; //函数声明或函数表达式所产生的上下文 |
| 12 | 65 | this.childrenMap = Object.create(null); //键是函数名,值是上下文,匿名函数表达式键为cid |
| 13 | 65 | this.vars = []; //变量var声明 |
| 14 | 65 | this.varsMap = Object.create(null); //键为id字面量,值是它的token的节点 |
| 15 | 65 | this.vardeclMap = Object.create(null); //var赋值记录,优先级vardecl > fndecl > varnodecl |
| 16 | 65 | this.params = []; //形参,函数上下文才有,即全局无 |
| 17 | 65 | this.paramsMap = Object.create(null); //键为id字面量,值是它的token的节点 |
| 18 | 65 | this.aParams = []; //实参,函数表达式才有 |
| 19 | 65 | this.vids = []; //上下文环境里用到的变量id |
| 20 | 65 | this.vidsMap = Object.create(null); //键为id字面量,值是它的token的节点 |
| 21 | 65 | this.returns = []; //上下文环境里return语句 |
| 22 | 65 | this.node = null; //对应的ast的节点 |
| 23 | 65 | this.thisIs = null; //this指向,仅函数表达式call或apply执行时有用 |
| 24 | 65 | if(!this.isTop()) { |
| 25 | 26 | this.parent.addChild(this); |
| 26 | } | |
| 27 | }).methods({ | |
| 28 | parse: function(code) { | |
| 29 | 35 | var ast; |
| 30 | 35 | if(code instanceof JsNode) { |
| 31 | 1 | ast = code; |
| 32 | } | |
| 33 | else { | |
| 34 | 34 | ast = this.parser.parse(code); |
| 35 | } | |
| 36 | 35 | recursion(ast, this); |
| 37 | 35 | return this; |
| 38 | }, | |
| 39 | getId: function() { | |
| 40 | 29 | return this.id; |
| 41 | }, | |
| 42 | getName: function() { | |
| 43 | 27 | return this.name; |
| 44 | }, | |
| 45 | getParent: function() { | |
| 46 | 2 | return this.parent; |
| 47 | }, | |
| 48 | isTop: function() { | |
| 49 | 66 | return !this.parent; |
| 50 | }, | |
| 51 | isFnexpr: function() { | |
| 52 | 1 | return !this.isTop() && !this.name; |
| 53 | }, | |
| 54 | hasParam: function(p) { | |
| 55 | 6 | return p in this.paramsMap; |
| 56 | }, | |
| 57 | getParams: function() { | |
| 58 | 1 | return this.params; |
| 59 | }, | |
| 60 | addParam: function(p) { | |
| 61 | //形参不可能重复,无需判断 | |
| 62 | 10 | this.paramsMap[p] = this.params.length; |
| 63 | 10 | this.params.push(p); |
| 64 | 10 | return this; |
| 65 | }, | |
| 66 | getAParams: function() { | |
| 67 | 4 | return this.aParams; |
| 68 | }, | |
| 69 | addAParam: function(ap) { | |
| 70 | 9 | this.aParams.push(ap); |
| 71 | 9 | return this; |
| 72 | }, | |
| 73 | getChild: function(name) { | |
| 74 | 7 | return this.childrenMap[name]; |
| 75 | }, | |
| 76 | getChildren: function() { | |
| 77 | 15 | return this.children; |
| 78 | }, | |
| 79 | //通过name查找函数声明,id查找表达式 | |
| 80 | hasChild: function(name) { | |
| 81 | 48 | return name in this.childrenMap; |
| 82 | }, | |
| 83 | addChild: function(child) { | |
| 84 | 26 | var name = child.getName(); |
| 85 | //函数表达式名字为空用id删除 | |
| 86 | 26 | if(name) { |
| 87 | 12 | if(name in this.vardeclMap) { |
| 88 | 1 | return this; |
| 89 | } | |
| 90 | 11 | this.delVar(name); |
| 91 | 11 | this.delChild(name); |
| 92 | } | |
| 93 | else { | |
| 94 | 14 | this.delChild(child.getId()); |
| 95 | } | |
| 96 | 25 | name = name || child.getId(); |
| 97 | 25 | this.childrenMap[name] = child; |
| 98 | 25 | this.children.push(child); |
| 99 | 25 | return this; |
| 100 | }, | |
| 101 | //name函数声明,id表达式 | |
| 102 | delChild: function(name) { | |
| 103 | 38 | if(this.hasChild(name)) { |
| 104 | 1 | var i = this.children.indexOf(this.childrenMap[name]); |
| 105 | 1 | this.children.splice(i, 1); |
| 106 | 1 | delete this.childrenMap[name]; |
| 107 | } | |
| 108 | 38 | return this; |
| 109 | }, | |
| 110 | hasVar: function(v) { | |
| 111 | 38 | return v in this.varsMap; |
| 112 | }, | |
| 113 | addVar: function(node, assign) { | |
| 114 | 17 | var v = node.leaves()[0].token().content(); |
| 115 | //赋值拥有最高优先级,会覆盖掉之前的函数声明和var | |
| 116 | 17 | if(assign) { |
| 117 | 13 | this.delVar(v); |
| 118 | 13 | this.delChild(v); |
| 119 | 13 | this.vardeclMap[v] = true; |
| 120 | } | |
| 121 | //仅仅是var声明无赋值,且已有过声明或函数,忽略之 | |
| 122 | 4 | else if(this.hasVar(v) || this.hasChild(v)) { |
| 123 | 2 | return this; |
| 124 | } | |
| 125 | 15 | this.varsMap[v] = node; |
| 126 | 15 | this.vars.push(node); |
| 127 | 15 | return this; |
| 128 | }, | |
| 129 | delVar: function(v) { | |
| 130 | 24 | if(this.hasVar(v)) { |
| 131 | 2 | var i = this.vars.indexOf(this.varsMap[v]); |
| 132 | 2 | this.vars.splice(i, 1); |
| 133 | 2 | delete this.varsMap[v]; |
| 134 | } | |
| 135 | 24 | return this; |
| 136 | }, | |
| 137 | getVars: function() { | |
| 138 | 4 | return this.vars; |
| 139 | }, | |
| 140 | addReturn: function(node) { | |
| 141 | 5 | this.returns.push(node); |
| 142 | 5 | return this; |
| 143 | }, | |
| 144 | getReturns: function() { | |
| 145 | 4 | return this.returns; |
| 146 | }, | |
| 147 | hasVid: function(v) { | |
| 148 | 8 | return v in this.vidsMap; |
| 149 | }, | |
| 150 | getVid: function(v) { | |
| 151 | 1 | return this.vidsMap[v]; |
| 152 | }, | |
| 153 | addVid: function(node) { | |
| 154 | 18 | var v = node.token().content(); |
| 155 | 18 | this.vids.push(node); |
| 156 | 18 | this.vidsMap[v] = this.vidsMap[v] || []; |
| 157 | 18 | this.vidsMap[v].push(node); |
| 158 | 18 | return this; |
| 159 | }, | |
| 160 | getVids: function() { | |
| 161 | 2 | return this.vids; |
| 162 | }, | |
| 163 | getNode: function() { | |
| 164 | 1 | return this.node; |
| 165 | }, | |
| 166 | setNode: function(n) { | |
| 167 | 26 | this.node = n; |
| 168 | 26 | return this; |
| 169 | }, | |
| 170 | setThis: function(t) { | |
| 171 | 4 | this.thisIs = t; |
| 172 | 4 | return this; |
| 173 | }, | |
| 174 | getThis: function() { | |
| 175 | 3 | return this.thisIs; |
| 176 | } | |
| 177 | }); | |
| 178 | ||
| 179 | 1 | function recursion(node, context) { |
| 180 | 668 | var isToken = node.name() == JsNode.TOKEN; |
| 181 | 668 | var isVirtual = isToken && node.token().type() == Token.VIRTUAL; |
| 182 | 668 | if(isToken) { |
| 183 | 385 | if(!isVirtual) { |
| 184 | 379 | var token = node.token(); |
| 185 | 379 | var s = token.content(); |
| 186 | 379 | if(s == 'return') { |
| 187 | 5 | context.addReturn(node); |
| 188 | } | |
| 189 | } | |
| 190 | } | |
| 191 | else { | |
| 192 | 283 | if(node.name() == JsNode.VARDECL) { |
| 193 | 17 | vardecl(node, context); |
| 194 | } | |
| 195 | 266 | else if(node.name() == JsNode.FNDECL) { |
| 196 | 12 | context = fndecl(node, context); |
| 197 | } | |
| 198 | 254 | else if(node.name() == JsNode.FNEXPR) { |
| 199 | 14 | context = fnexpr(node, context); |
| 200 | } | |
| 201 | 240 | else if(node.name() == JsNode.PRMREXPR) { |
| 202 | 52 | prmrexpr(node, context); |
| 203 | } | |
| 204 | 283 | node.leaves().forEach(function(leaf, i) { |
| 205 | 633 | recursion(leaf, context); |
| 206 | }); | |
| 207 | } | |
| 208 | } | |
| 209 | 1 | function vardecl(node, context) { |
| 210 | 17 | var leaves = node.leaves(); |
| 211 | 17 | var assign = !!leaves[1]; |
| 212 | 17 | context.addVar(node, assign); |
| 213 | } | |
| 214 | 1 | function fndecl(node, context) { |
| 215 | 12 | var v = node.leaves()[1].leaves().content(); |
| 216 | 12 | var child = new Context(context, v); |
| 217 | 12 | child.setNode(node); |
| 218 | 12 | var params = node.leaves()[3]; |
| 219 | 12 | if(params.name() == JsNode.FNPARAMS) { |
| 220 | 1 | addParam(params, child); |
| 221 | } | |
| 222 | 12 | return child; |
| 223 | } | |
| 224 | 1 | function fnexpr(node, context) { |
| 225 | //函数表达式name为空 | |
| 226 | 14 | var child = new Context(context); |
| 227 | 14 | child.setNode(node); |
| 228 | //记录形参 | |
| 229 | 14 | var params; |
| 230 | 14 | var v = node.leaves()[1]; |
| 231 | 14 | if(v.token().content() != '(') { |
| 232 | 6 | params = node.leaves()[3]; |
| 233 | } | |
| 234 | else { | |
| 235 | 8 | params = node.leaves()[2]; |
| 236 | } | |
| 237 | 14 | if(params.name() == JsNode.FNPARAMS) { |
| 238 | 7 | addParam(params, child); |
| 239 | } | |
| 240 | //匿名函数检查实参传入情况,包括call和apply设置this | |
| 241 | 14 | var next = node.next(); |
| 242 | //!function(){}()形式 | |
| 243 | 14 | if(next && next.name() == JsNode.ARGS) { |
| 244 | 7 | var leaves = next.leaves(); |
| 245 | //长度2为()空参数,长度3有参数,第2个节点 | |
| 246 | 7 | if(leaves.length == 3) { |
| 247 | 1 | addAParam(leaves[1], child); |
| 248 | } | |
| 249 | } | |
| 250 | //call或applay | |
| 251 | 7 | else if(next |
| 252 | && next.name() == JsNode.TOKEN | |
| 253 | && next.token().content() == '.' | |
| 254 | && next.next() | |
| 255 | && next.next().name() == JsNode.TOKEN | |
| 256 | && ['call', 'apply'].indexOf(next.next().token().content()) > -1) { | |
| 257 | 2 | var mmb = node.parent(); |
| 258 | 2 | if(mmb.name() == JsNode.MMBEXPR) { |
| 259 | 2 | var callexpr = mmb.parent(); |
| 260 | 2 | if(callexpr.name() == JsNode.CALLEXPR) { |
| 261 | 2 | var isApply = next.next().token().content() == 'apply'; |
| 262 | 2 | next = mmb.next(); |
| 263 | 2 | if(next && next.name() == JsNode.ARGS) { |
| 264 | 2 | var leaves = next.leaves(); |
| 265 | //长度2为()空参数,长度3有参数,第2个节点 | |
| 266 | 2 | if(leaves.length == 3) { |
| 267 | 2 | isApply ? addApplyAParam(leaves[1], child) : addCallAParam(leaves[1], child); |
| 268 | } | |
| 269 | } | |
| 270 | } | |
| 271 | } | |
| 272 | } | |
| 273 | //(function(){})()形式 | |
| 274 | else { | |
| 275 | 5 | var prmr = node.parent(); |
| 276 | 5 | var prev = node.prev(); |
| 277 | 5 | if(prmr.name() == JsNode.PRMREXPR |
| 278 | && prev | |
| 279 | && prev.name() == JsNode.TOKEN | |
| 280 | && prev.token().content() == '(') { | |
| 281 | 5 | next = prmr.next(); |
| 282 | 5 | if(next && next.name() == JsNode.ARGS) { |
| 283 | 3 | var leaves = next.leaves(); |
| 284 | //长度2为()空参数,长度3有参数,第2个节点 | |
| 285 | 3 | if(leaves.length == 3) { |
| 286 | 1 | addAParam(leaves[1], child); |
| 287 | } | |
| 288 | } | |
| 289 | //(function(){}).call()形式 | |
| 290 | 2 | else if(next |
| 291 | && next.name() == JsNode.TOKEN | |
| 292 | && next.token().content() == '.' | |
| 293 | && next.next() | |
| 294 | && next.next().name() == JsNode.TOKEN | |
| 295 | && ['call', 'apply'].indexOf(next.next().token().content()) > -1) { | |
| 296 | 2 | var mmb = prmr.parent(); |
| 297 | 2 | if(mmb.name() == JsNode.MMBEXPR) { |
| 298 | 2 | var callexpr = mmb.parent(); |
| 299 | 2 | if(callexpr.name() == JsNode.CALLEXPR) { |
| 300 | 2 | var isApply = next.next().token().content() == 'apply'; |
| 301 | 2 | next = mmb.next(); |
| 302 | 2 | if(next && next.name() == JsNode.ARGS) { |
| 303 | 2 | var leaves = next.leaves(); |
| 304 | //长度2为()空参数,长度3有参数,第2个节点 | |
| 305 | 2 | if(leaves.length == 3) { |
| 306 | 2 | isApply ? addApplyAParam(leaves[1], child) : addCallAParam(leaves[1], child); |
| 307 | } | |
| 308 | } | |
| 309 | } | |
| 310 | } | |
| 311 | } | |
| 312 | } | |
| 313 | } | |
| 314 | 14 | return child; |
| 315 | } | |
| 316 | //支持es6 | |
| 317 | 1 | function addParam(params, child) { |
| 318 | 8 | params.leaves().forEach(function(leaf, i) { |
| 319 | 13 | if(leaf.name() == JsNode.TOKEN && leaf.token().content() != ',') { |
| 320 | 9 | child.addParam(leaf.token().content()); |
| 321 | } | |
| 322 | 4 | else if(leaf.name() == JsNode.RESTPARAM) { |
| 323 | 1 | child.addParam(leaf.leaves()[1].token().content()); |
| 324 | } | |
| 325 | }); | |
| 326 | } | |
| 327 | 1 | function addAParam(params, child) { |
| 328 | 2 | params.leaves().forEach(function(leaf, i) { |
| 329 | 6 | if(i % 2 == 0) { |
| 330 | 4 | child.addAParam(leaf); |
| 331 | } | |
| 332 | }); | |
| 333 | } | |
| 334 | 1 | function addCallAParam(params, child) { |
| 335 | 2 | params.leaves().forEach(function(leaf, i) { |
| 336 | 6 | if(i == 0) { |
| 337 | 2 | child.setThis(leaf); |
| 338 | } | |
| 339 | 4 | else if(i % 2 == 1) { |
| 340 | 2 | child.addAParam(leaf); |
| 341 | } | |
| 342 | }); | |
| 343 | } | |
| 344 | 1 | function addApplyAParam(params, child) { |
| 345 | 2 | child.setThis(params.leaves()[0]); |
| 346 | 2 | params.leaves()[2].leaves()[0].leaves().forEach(function(leaf, i) { |
| 347 | 8 | if(i % 2 == 1) { |
| 348 | 3 | child.addAParam(leaf); |
| 349 | } | |
| 350 | }); | |
| 351 | } | |
| 352 | 1 | function prmrexpr(node, context) { |
| 353 | 52 | var first = node.leaves()[0]; |
| 354 | 52 | if(first.name() == JsNode.TOKEN) { |
| 355 | 48 | var token = first.token(); |
| 356 | 48 | if(token.type() == Token.ID || token.content() == 'this') { |
| 357 | 18 | context.addVid(first); |
| 358 | } | |
| 359 | } | |
| 360 | } | |
| 361 | ||
| 362 | 1 | module.exports = Context; |
| Line | Hits | Source |
|---|---|---|
| 1 | 1 | var Class = require('../../util/Class'); |
| 2 | 1 | var Node = Class(function(type, children) { |
| 3 | 770136 | this.type = type; |
| 4 | 770136 | if(type == Node.TOKEN) { |
| 5 | 140139 | this.children = children; |
| 6 | } | |
| 7 | 629997 | else if(Array.isArray(children)) { |
| 8 | 0 | this.children = children; |
| 9 | } | |
| 10 | else { | |
| 11 | 629997 | this.children = children ? [children] : []; |
| 12 | } | |
| 13 | 770136 | this.p = null; |
| 14 | 770136 | this.pr = null; |
| 15 | 770136 | this.ne = null; |
| 16 | 770136 | return this; |
| 17 | }).methods({ | |
| 18 | name: function() { | |
| 19 | 240479 | return this.type; |
| 20 | }, | |
| 21 | leaves: function() { | |
| 22 | 98888 | return this.children; |
| 23 | }, | |
| 24 | add: function() { | |
| 25 | 166758 | var self = this; |
| 26 | 166758 | var args = Array.prototype.slice.call(arguments, 0); |
| 27 | 166758 | args.forEach(function(node) { |
| 28 | 238672 | node.parent(self); |
| 29 | 238672 | var last = self.children[self.children.length - 1]; |
| 30 | 238672 | if(last) { |
| 31 | 139985 | last.next(node); |
| 32 | 139985 | node.prev(last); |
| 33 | } | |
| 34 | 238672 | self.children.push(node); |
| 35 | }); | |
| 36 | 166758 | return self; |
| 37 | }, | |
| 38 | token: function() { | |
| 39 | 278615 | return this.children; |
| 40 | }, | |
| 41 | parent: function(p) { | |
| 42 | 238688 | if(p) { |
| 43 | 238672 | this.p = p; |
| 44 | } | |
| 45 | 238688 | return this.p; |
| 46 | }, | |
| 47 | prev: function(pr) { | |
| 48 | 139992 | if(pr) { |
| 49 | 139985 | this.pr = pr; |
| 50 | } | |
| 51 | 139992 | return this.pr; |
| 52 | }, | |
| 53 | next: function(ne) { | |
| 54 | 140025 | if(ne) { |
| 55 | 139985 | this.ne = ne; |
| 56 | } | |
| 57 | 140025 | return this.ne; |
| 58 | } | |
| 59 | }).statics({ | |
| 60 | PROGRAM: 'program', | |
| 61 | ELEMS: 'elems', | |
| 62 | ELEM: 'elem', | |
| 63 | CSTSTMT: 'cststmt', | |
| 64 | LETSTMT: 'letstmt', | |
| 65 | VARSTMT: 'varstmt', | |
| 66 | VARDECL: 'vardecl', | |
| 67 | FNBODY: 'fnbody', | |
| 68 | BLOCK: 'block', | |
| 69 | ITERSTMT: 'iterstmt', | |
| 70 | TOKEN: 'token', | |
| 71 | FNPARAMS: 'fnparams', | |
| 72 | BINDELEMENT: 'bindelement', | |
| 73 | RESTPARAM: 'restparam', | |
| 74 | EXPR: 'expr', | |
| 75 | CLASSDECL: 'classdecl', | |
| 76 | CLASSTAIL: 'classtail', | |
| 77 | HERITAGE: 'heritage', | |
| 78 | CLASSBODY: 'classbody', | |
| 79 | METHOD: 'method', | |
| 80 | SUPERSTMT: 'superstmt', | |
| 81 | GETFN: 'getfn', | |
| 82 | SETFN: 'setfn', | |
| 83 | PROGRAM: 'program', | |
| 84 | STMT: 'stmt', | |
| 85 | ASSIGN: 'assign', | |
| 86 | EMPTSTMT: 'emptstmt', | |
| 87 | IFSTMT: 'ifstmt', | |
| 88 | CNTNSTMT: 'cntnstmt', | |
| 89 | BRKSTMT: 'brkstmt', | |
| 90 | RETSTMT: 'retstmt', | |
| 91 | WITHSTMT: 'withstmt', | |
| 92 | SWCHSTMT: 'swchstmt', | |
| 93 | CASEBLOCK: 'caseblock', | |
| 94 | CASECLAUSE: 'caseclause', | |
| 95 | DFTCLAUSE: 'dftclause', | |
| 96 | LABSTMT: 'labstmt', | |
| 97 | THRSTMT: 'thrstmt', | |
| 98 | TRYSTMT: 'trystmt', | |
| 99 | DEBSTMT: 'debstmt', | |
| 100 | EXPRSTMT: 'exprstmt', | |
| 101 | CACH: 'cach', | |
| 102 | FINL: 'finl', | |
| 103 | FNDECL: 'fndecl', | |
| 104 | FNEXPR: 'fnexpr', | |
| 105 | ASSIGNEXPR: 'assignexpr', | |
| 106 | CNDTEXPR: 'cndtexpr', | |
| 107 | LOGOREXPR: 'logorexpr', | |
| 108 | LOGANDEXPR: 'logandexpr', | |
| 109 | BITOREXPR: 'bitorexpr', | |
| 110 | BITANDEXPR: 'bitandexpr', | |
| 111 | BITXOREXPR: 'bitxorexpr', | |
| 112 | EQEXPR: 'eqexpr', | |
| 113 | RELTEXPR: 'reltexpr', | |
| 114 | SHIFTEXPR: 'shiftexpr', | |
| 115 | ADDEXPR: 'addexpr', | |
| 116 | MTPLEXPR: 'mtplexpr', | |
| 117 | UNARYEXPR: 'unaryexpr', | |
| 118 | MMBEXPR: 'mmbexpr', | |
| 119 | PRMREXPR: 'prmrexpr', | |
| 120 | ARRLTR: 'arrltr', | |
| 121 | OBJLTR: 'objltr', | |
| 122 | PROPTASSIGN: 'proptassign', | |
| 123 | PROPTNAME: 'proptname', | |
| 124 | PROPTSETS: 'propsets', | |
| 125 | ARGS: 'args', | |
| 126 | ARGLIST: 'arglist', | |
| 127 | IMPTSTMT: 'imptstmt', | |
| 128 | POSTFIXEXPR: 'postfixexpr', | |
| 129 | NEWEXPR: 'newexpr', | |
| 130 | CALLEXPR: 'callexpr', | |
| 131 | getKey: function(s) { | |
| 132 | 2 | if(!s) { |
| 133 | 1 | throw new Error('empty value'); |
| 134 | } | |
| 135 | 1 | if(!keys) { |
| 136 | 1 | var self = this; |
| 137 | 1 | keys = {}; |
| 138 | 1 | Object.keys(this).forEach(function(k) { |
| 139 | 74 | var v = self[k]; |
| 140 | 74 | keys[v] = k; |
| 141 | }); | |
| 142 | } | |
| 143 | 1 | return keys[s]; |
| 144 | } | |
| 145 | }); | |
| 146 | 1 | var keys; |
| 147 | 1 | module.exports = Node; |
| Line | Hits | Source |
|---|---|---|
| 1 | 1 | var Class = require('../../util/Class'); |
| 2 | 1 | var character = require('../../util/character'); |
| 3 | 1 | var Lexer = require('../../lexer/Lexer'); |
| 4 | 1 | var Rule = require('../../lexer/rule/EcmascriptRule'); |
| 5 | 1 | var Token = require('../../lexer/Token'); |
| 6 | 1 | var Node = require('./Node'); |
| 7 | 1 | var S = {}; |
| 8 | 1 | S[Token.BLANK] = S[Token.TAB] = S[Token.COMMENT] = S[Token.LINE] = S[Token.ENTER] = true; |
| 9 | 1 | var Parser = Class(function(lexer) { |
| 10 | 215 | this.init(lexer); |
| 11 | }).methods({ | |
| 12 | parse: function(code) { | |
| 13 | 180 | this.lexer.parse(code); |
| 14 | 180 | this.tree = this.program(); |
| 15 | 151 | return this.tree; |
| 16 | }, | |
| 17 | ast: function() { | |
| 18 | 4 | return this.tree; |
| 19 | }, | |
| 20 | init: function(lexer) { | |
| 21 | 216 | this.look = null; |
| 22 | 216 | this.tokens = null; |
| 23 | 216 | this.lastLine = 1; |
| 24 | 216 | this.lastCol = 1; |
| 25 | 216 | this.line = 1; |
| 26 | 216 | this.col = 1; |
| 27 | 216 | this.index = 0; |
| 28 | 216 | this.length = 0; |
| 29 | 216 | this.ignores = {}; |
| 30 | 216 | this.hasMoveLine = false; |
| 31 | 216 | this.tree = {}; |
| 32 | 216 | if(lexer) { |
| 33 | 150 | this.lexer = lexer; |
| 34 | } | |
| 35 | 66 | else if(this.lexer) { |
| 36 | 1 | this.lexer.init(); |
| 37 | } | |
| 38 | else { | |
| 39 | 65 | this.lexer = new Lexer(new Rule()); |
| 40 | } | |
| 41 | }, | |
| 42 | program: function() { | |
| 43 | 180 | this.tokens = this.lexer.tokens(); |
| 44 | 180 | this.length = this.tokens.length; |
| 45 | 180 | if(this.tokens.length) { |
| 46 | 178 | this.move(); |
| 47 | } | |
| 48 | 180 | var node = new Node(Node.PROGRAM); |
| 49 | 180 | while(this.look) { |
| 50 | 212 | node.add(this.element()); |
| 51 | } | |
| 52 | 151 | return node; |
| 53 | }, | |
| 54 | element: function(allowSuper) { | |
| 55 | 5380 | if(this.look.content() == 'function') { |
| 56 | 199 | return this.fndecl(); |
| 57 | } | |
| 58 | 5181 | else if(this.look.content() == 'class') { |
| 59 | 13 | return this.classdecl(); |
| 60 | } | |
| 61 | else { | |
| 62 | 5168 | return this.stmt(allowSuper); |
| 63 | } | |
| 64 | }, | |
| 65 | stmt: function(allowSuper) { | |
| 66 | 10854 | if(!this.look) { |
| 67 | 1 | this.error(); |
| 68 | } | |
| 69 | 10853 | switch(this.look.content()) { |
| 70 | case 'let': | |
| 71 | 2 | return this.letstmt(); |
| 72 | case 'const': | |
| 73 | 2 | return this.cststmt(); |
| 74 | case 'var': | |
| 75 | 1160 | return this.varstmt(); |
| 76 | case '{': | |
| 77 | 1791 | return this.block(); |
| 78 | case ';': | |
| 79 | 35 | return this.emptstmt(); |
| 80 | case 'if': | |
| 81 | 1692 | return this.ifstmt(); |
| 82 | case 'do': | |
| 83 | case 'while': | |
| 84 | case 'for': | |
| 85 | 458 | return this.iterstmt(); |
| 86 | case 'continue': | |
| 87 | 18 | return this.cntnstmt(); |
| 88 | case 'break': | |
| 89 | 123 | return this.brkstmt(); |
| 90 | case 'return': | |
| 91 | 1636 | return this.retstmt(); |
| 92 | case 'with': | |
| 93 | 1 | return this.withstmt(); |
| 94 | case 'switch': | |
| 95 | 8 | return this.swchstmt(); |
| 96 | case 'throw': | |
| 97 | 39 | return this.thrstmt(); |
| 98 | case 'try': | |
| 99 | 55 | return this.trystmt(); |
| 100 | case 'debugger': | |
| 101 | 1 | return this.debstmt(); |
| 102 | case 'super': | |
| 103 | 3 | if(!allowSuper) { |
| 104 | 1 | this.error('super must in a class'); |
| 105 | } | |
| 106 | 2 | return this.superstmt(); |
| 107 | case 'import': | |
| 108 | 0 | return this.imptstmt(); |
| 109 | default: | |
| 110 | 3829 | if(this.look.type() == Token.ID) { |
| 111 | 2943 | for(var i = this.index; i < this.length; i++) { |
| 112 | 3955 | var token = this.tokens[i]; |
| 113 | 3955 | if(!S[token.type()]) { |
| 114 | 2940 | if(token.content() == ':') { |
| 115 | 2 | return this.labstmt(); |
| 116 | } | |
| 117 | else { | |
| 118 | 2938 | return this.exprstmt(); |
| 119 | } | |
| 120 | } | |
| 121 | } | |
| 122 | } | |
| 123 | 889 | return this.exprstmt(); |
| 124 | } | |
| 125 | }, | |
| 126 | exprstmt: function() { | |
| 127 | 3827 | var node = new Node(Node.EXPRSTMT); |
| 128 | 3827 | node.add(this.expr(), this.match(';')); |
| 129 | 3818 | return node; |
| 130 | }, | |
| 131 | cststmt: function(noSem) { | |
| 132 | 2 | var node = new Node(Node.CSTSTMT); |
| 133 | 2 | node.add( |
| 134 | this.match('const'), | |
| 135 | this.vardecl() | |
| 136 | ); | |
| 137 | 2 | while(this.look && this.look.content() == ',') { |
| 138 | 1 | node.add( |
| 139 | this.match(), | |
| 140 | this.vardecl() | |
| 141 | ); | |
| 142 | } | |
| 143 | 2 | if(!noSem) { |
| 144 | 2 | node.add(this.match(';')); |
| 145 | } | |
| 146 | 2 | return node; |
| 147 | }, | |
| 148 | letstmt: function(noSem) { | |
| 149 | 2 | var node = new Node(Node.LETSTMT); |
| 150 | 2 | node.add( |
| 151 | this.match('let'), | |
| 152 | this.vardecl() | |
| 153 | ); | |
| 154 | 2 | while(this.look && this.look.content() == ',') { |
| 155 | 1 | node.add( |
| 156 | this.match(), | |
| 157 | this.vardecl() | |
| 158 | ); | |
| 159 | } | |
| 160 | 2 | if(!noSem) { |
| 161 | 2 | node.add(this.match(';')); |
| 162 | } | |
| 163 | 2 | return node; |
| 164 | }, | |
| 165 | varstmt: function(noSem) { | |
| 166 | 1244 | var node = new Node(Node.VARSTMT); |
| 167 | 1244 | node.add( |
| 168 | this.match('var'), | |
| 169 | this.vardecl() | |
| 170 | ); | |
| 171 | 1242 | while(this.look && this.look.content() == ',') { |
| 172 | 1555 | node.add( |
| 173 | this.match(), | |
| 174 | this.vardecl() | |
| 175 | ); | |
| 176 | } | |
| 177 | 1242 | if(!noSem) { |
| 178 | 1158 | node.add(this.match(';')); |
| 179 | } | |
| 180 | 1242 | return node; |
| 181 | }, | |
| 182 | vardecl: function() { | |
| 183 | 2805 | var node = new Node(Node.VARDECL); |
| 184 | 2805 | if(!this.look) { |
| 185 | 1 | this.error('missing variable name'); |
| 186 | } | |
| 187 | 2804 | if(this.look.content() == '[') { |
| 188 | 0 | node.add(this.arrltr()); |
| 189 | } | |
| 190 | else { | |
| 191 | 2804 | node.add(this.match(Token.ID, 'missing variable name')); |
| 192 | } | |
| 193 | 2804 | if(this.look && this.look.content() == '=') { |
| 194 | 1919 | node.add(this.assign()); |
| 195 | } | |
| 196 | 2803 | return node; |
| 197 | }, | |
| 198 | assign: function() { | |
| 199 | 1919 | var node = new Node(Node.ASSIGN); |
| 200 | 1919 | node.add(this.match('=')); |
| 201 | 1919 | if(!this.look) { |
| 202 | 1 | this.error(); |
| 203 | } | |
| 204 | 1918 | node.add(this.assignexpr()); |
| 205 | 1918 | return node; |
| 206 | }, | |
| 207 | block: function() { | |
| 208 | 1906 | var node = new Node(Node.BLOCK); |
| 209 | 1906 | node.add(this.match('{')); |
| 210 | 1906 | while(this.look && this.look.content() != '}') { |
| 211 | 2988 | node.add(this.stmt()); |
| 212 | } | |
| 213 | 1906 | node.add(this.match('}', 'missing } in compound statement')); |
| 214 | 1906 | return node; |
| 215 | }, | |
| 216 | emptstmt: function() { | |
| 217 | 35 | var node = new Node(Node.EMPTSTMT); |
| 218 | 35 | node.add(this.match(';')); |
| 219 | 35 | return node; |
| 220 | }, | |
| 221 | ifstmt: function() { | |
| 222 | 1692 | var node = new Node(Node.IFSTMT); |
| 223 | 1692 | node.add( |
| 224 | this.match('if'), | |
| 225 | this.match('('), | |
| 226 | this.expr(), | |
| 227 | this.match(')'), | |
| 228 | this.stmt() | |
| 229 | ); | |
| 230 | 1691 | if(this.look && this.look.content() == 'else') { |
| 231 | 325 | node.add( |
| 232 | this.match('else'), | |
| 233 | this.stmt() | |
| 234 | ); | |
| 235 | } | |
| 236 | 1691 | return node; |
| 237 | }, | |
| 238 | iterstmt: function() { | |
| 239 | 458 | var node = new Node(Node.ITERSTMT); |
| 240 | 458 | switch(this.look.content()) { |
| 241 | case 'do': | |
| 242 | 9 | node.add( |
| 243 | this.match(), | |
| 244 | this.stmt(), | |
| 245 | this.match('while'), | |
| 246 | this.match('('), | |
| 247 | this.expr(), | |
| 248 | this.match(')'), | |
| 249 | this.match(';') | |
| 250 | ); | |
| 251 | 9 | break; |
| 252 | case 'while': | |
| 253 | 152 | node.add( |
| 254 | this.match(), | |
| 255 | this.match('('), | |
| 256 | this.expr(), | |
| 257 | this.match(')'), | |
| 258 | this.stmt() | |
| 259 | ); | |
| 260 | 152 | break; |
| 261 | case 'for': | |
| 262 | 297 | node.add( |
| 263 | this.match(), | |
| 264 | this.match('(') | |
| 265 | ); | |
| 266 | 297 | if(!this.look) { |
| 267 | 1 | this.error(); |
| 268 | } | |
| 269 | 296 | if(this.look.content() == 'var' || this.look.content() == 'let') { |
| 270 | 84 | var node2 = this.look.content() == 'var' ? this.varstmt(true) : this.letstmt(true); |
| 271 | 84 | if(!this.look) { |
| 272 | 1 | this.error('missing ; after for-loop initializer'); |
| 273 | } | |
| 274 | 83 | if(this.look.content() == 'in') { |
| 275 | 19 | if(node2.leaves().length > 2) { |
| 276 | 1 | this.error('invalid for/in left-hand side'); |
| 277 | } | |
| 278 | 18 | node.add(node2); |
| 279 | 18 | node.add( |
| 280 | this.match(), | |
| 281 | this.expr() | |
| 282 | ); | |
| 283 | } | |
| 284 | else { | |
| 285 | 64 | node.add(node2); |
| 286 | 64 | node.add(this.match(';')); |
| 287 | 64 | if(this.look.content() != ';') { |
| 288 | 63 | node.add(this.expr()); |
| 289 | } | |
| 290 | 63 | node.add(this.match(';')); |
| 291 | 63 | if(!this.look) { |
| 292 | 1 | this.error(); |
| 293 | } | |
| 294 | 62 | if(this.look.content() != ')') { |
| 295 | 59 | node.add(this.expr()); |
| 296 | } | |
| 297 | } | |
| 298 | } | |
| 299 | else { | |
| 300 | 212 | if(this.look.content() == 'in') { |
| 301 | 1 | this.error(); |
| 302 | } | |
| 303 | 211 | var hasIn = false; |
| 304 | 211 | for(var i = this.index; i < this.length; i++) { |
| 305 | 1924 | var t = this.tokens[i]; |
| 306 | 1924 | if(t.content() == 'in') { |
| 307 | 87 | hasIn = true; |
| 308 | 87 | break; |
| 309 | } | |
| 310 | 1837 | else if(t.content() == ')') { |
| 311 | 120 | break; |
| 312 | } | |
| 313 | } | |
| 314 | 211 | if(hasIn) { |
| 315 | 87 | node.add(this.expr(true), this.match('in'), this.expr()); |
| 316 | } | |
| 317 | else { | |
| 318 | 124 | if(this.look.content() != ';') { |
| 319 | 68 | node.add(this.expr()); |
| 320 | } | |
| 321 | //for的;不能省略,强制判断 | |
| 322 | 124 | if(!this.look || this.look.content() != ';') { |
| 323 | 1 | this.error('missing ;') |
| 324 | } | |
| 325 | 123 | node.add(this.match(';')); |
| 326 | 123 | if(!this.look) { |
| 327 | 1 | this.error(); |
| 328 | } | |
| 329 | 122 | if(this.look.content() != ';') { |
| 330 | 121 | node.add(this.expr()); |
| 331 | } | |
| 332 | 122 | if(!this.look || this.look.content() != ';') { |
| 333 | 1 | this.error('missing ;') |
| 334 | } | |
| 335 | 121 | node.add(this.match(';')); |
| 336 | 121 | if(!this.look) { |
| 337 | 1 | this.error(); |
| 338 | } | |
| 339 | 120 | if(this.look.content() != ')') { |
| 340 | 112 | node.add(this.expr()); |
| 341 | } | |
| 342 | } | |
| 343 | } | |
| 344 | 287 | node.add(this.match(')')); |
| 345 | 287 | node.add(this.stmt()); |
| 346 | } | |
| 347 | 448 | return node; |
| 348 | }, | |
| 349 | cntnstmt: function() { | |
| 350 | 18 | var node = new Node(Node.CNTNSTMT); |
| 351 | 18 | node.add(this.match('continue', true)); |
| 352 | 18 | if(this.look && this.look.type() == Token.ID) { |
| 353 | 1 | node.add(this.match()); |
| 354 | } | |
| 355 | 18 | node.add(this.match(';')); |
| 356 | 18 | return node; |
| 357 | }, | |
| 358 | brkstmt: function() { | |
| 359 | 123 | var node = new Node(Node.BRKSTMT); |
| 360 | 123 | node.add(this.match('break', true)); |
| 361 | 123 | if(this.look && this.look.type() == Token.ID) { |
| 362 | 1 | node.add(this.match()); |
| 363 | } | |
| 364 | 123 | node.add(this.match(';')); |
| 365 | 123 | return node; |
| 366 | }, | |
| 367 | retstmt: function() { | |
| 368 | 1636 | var node = new Node(Node.RETSTMT); |
| 369 | 1636 | node.add(this.match('return', true)); |
| 370 | //return后换行视作省略;,包括多行注释的换行 | |
| 371 | 1636 | if(this.look) { |
| 372 | 1635 | if(this.look.content() == ';' |
| 373 | || this.look.content() == '}' | |
| 374 | || this.look.type() == Token.LINE | |
| 375 | || this.look.type() == Token.COMMENT) { | |
| 376 | 82 | node.add(this.match(';')); |
| 377 | } | |
| 378 | else { | |
| 379 | 1553 | node.add(this.expr(), this.match(';')); |
| 380 | } | |
| 381 | } | |
| 382 | else { | |
| 383 | 1 | node.add(this.match(';')); |
| 384 | } | |
| 385 | 1636 | return node; |
| 386 | }, | |
| 387 | withstmt: function() { | |
| 388 | 1 | var node = new Node(Node.WITHSTMT); |
| 389 | 1 | node.add( |
| 390 | this.match('with'), | |
| 391 | this.match('('), | |
| 392 | this.expr(), | |
| 393 | this.match(')'), | |
| 394 | this.stmt() | |
| 395 | ); | |
| 396 | 1 | return node; |
| 397 | }, | |
| 398 | swchstmt: function() { | |
| 399 | 8 | var node = new Node(Node.SWCHSTMT); |
| 400 | 8 | node.add( |
| 401 | this.match('switch'), | |
| 402 | this.match('('), | |
| 403 | this.expr(), | |
| 404 | this.match(')'), | |
| 405 | this.caseblock() | |
| 406 | ); | |
| 407 | 7 | return node; |
| 408 | }, | |
| 409 | caseblock: function() { | |
| 410 | 8 | var node = new Node(Node.CASEBLOCK); |
| 411 | 8 | node.add(this.match('{')); |
| 412 | 8 | while(this.look && this.look.content() != '}') { |
| 413 | 102 | if(this.look.content() == 'case') { |
| 414 | 99 | node.add(this.caseclause()); |
| 415 | } | |
| 416 | 3 | else if(this.look.content() == 'default') { |
| 417 | 2 | node.add(this.dftclause()); |
| 418 | } | |
| 419 | else { | |
| 420 | 1 | this.error('invalid switch statement'); |
| 421 | } | |
| 422 | } | |
| 423 | 7 | node.add(this.match('}')); |
| 424 | 7 | return node; |
| 425 | }, | |
| 426 | caseclause: function() { | |
| 427 | 99 | var node = new Node(Node.CASECLAUSE); |
| 428 | 99 | node.add( |
| 429 | this.match('case'), | |
| 430 | this.expr(), | |
| 431 | this.match(':') | |
| 432 | ); | |
| 433 | 99 | while(this.look |
| 434 | && this.look.content() != 'case' | |
| 435 | && this.look.content() != 'default' | |
| 436 | && this.look.content() != '}') { | |
| 437 | 228 | node.add(this.stmt()); |
| 438 | } | |
| 439 | 99 | return node; |
| 440 | }, | |
| 441 | dftclause: function() { | |
| 442 | 2 | var node = new Node(Node.DFTCLAUSE); |
| 443 | 2 | node.add( |
| 444 | this.match('default'), | |
| 445 | this.match(':') | |
| 446 | ); | |
| 447 | 2 | while(this.look && this.look.content() != '}') { |
| 448 | 2 | node.add(this.stmt()); |
| 449 | } | |
| 450 | 2 | return node; |
| 451 | }, | |
| 452 | labstmt: function() { | |
| 453 | 2 | var node = new Node(Node.LABSTMT); |
| 454 | 2 | node.add( |
| 455 | this.match(Token.ID), | |
| 456 | this.match(':'), | |
| 457 | this.stmt() | |
| 458 | ); | |
| 459 | 2 | return node; |
| 460 | }, | |
| 461 | thrstmt: function() { | |
| 462 | 39 | var node = new Node(Node.THRSTMT); |
| 463 | 39 | node.add( |
| 464 | this.match('throw', true), | |
| 465 | this.expr(), | |
| 466 | this.match(';') | |
| 467 | ); | |
| 468 | 39 | return node; |
| 469 | }, | |
| 470 | trystmt: function() { | |
| 471 | 55 | var node = new Node(Node.TRYSTMT); |
| 472 | 55 | node.add( |
| 473 | this.match('try'), | |
| 474 | this.block() | |
| 475 | ); | |
| 476 | 55 | if(this.look && this.look.content() == 'catch') { |
| 477 | 54 | node.add(this.cach()); |
| 478 | 54 | if(this.look && this.look.content() == 'finally') { |
| 479 | 5 | node.add(this.finl()); |
| 480 | } | |
| 481 | } | |
| 482 | else { | |
| 483 | 1 | node.add(this.finl()); |
| 484 | } | |
| 485 | 55 | return node; |
| 486 | }, | |
| 487 | debstmt: function() { | |
| 488 | 1 | var node = new Node(Node.DEBSTMT); |
| 489 | 1 | node.add(this.match('debugger'), this.match(';')); |
| 490 | 1 | return node; |
| 491 | }, | |
| 492 | cach: function() { | |
| 493 | 54 | var node = new Node(Node.CACH); |
| 494 | 54 | node.add( |
| 495 | this.match('catch'), | |
| 496 | this.match('('), | |
| 497 | this.match(Token.ID, 'missing identifier in catch'), | |
| 498 | this.match(')'), | |
| 499 | this.block() | |
| 500 | ); | |
| 501 | 54 | return node; |
| 502 | }, | |
| 503 | finl: function() { | |
| 504 | 6 | var node = new Node(Node.FINL); |
| 505 | 6 | node.add( |
| 506 | this.match('finally'), | |
| 507 | this.block() | |
| 508 | ); | |
| 509 | 6 | return node; |
| 510 | }, | |
| 511 | superstmt: function() { | |
| 512 | 2 | var node = new Node(Node.SUPERSTMT); |
| 513 | 2 | node.add(this.match('super')); |
| 514 | 2 | if(!this.look) { |
| 515 | 0 | this.error(); |
| 516 | } | |
| 517 | 2 | if(this.look.content() == '.') { |
| 518 | 1 | while(this.look && this.look.content() == '.') { |
| 519 | 2 | node.add(this.match()); |
| 520 | 2 | if(!this.look) { |
| 521 | 0 | this.error(); |
| 522 | } | |
| 523 | 2 | if(this.look.content() == 'super') { |
| 524 | 1 | node.add(this.match()); |
| 525 | } | |
| 526 | else { | |
| 527 | 1 | break; |
| 528 | } | |
| 529 | } | |
| 530 | 1 | if(this.look.content() != '(') { |
| 531 | 1 | node.add(this.match(Token.ID)); |
| 532 | 1 | while(this.look && this.look.content() == '.') { |
| 533 | 0 | node.add(this.match(), this.match(Token.ID)); |
| 534 | } | |
| 535 | } | |
| 536 | } | |
| 537 | 2 | node.add( |
| 538 | this.args(), | |
| 539 | this.match(';') | |
| 540 | ); | |
| 541 | 2 | return node; |
| 542 | }, | |
| 543 | imptstmt: function() { | |
| 544 | 0 | var node = new Node(Node.IMPTSTMT); |
| 545 | 0 | return node; |
| 546 | }, | |
| 547 | fndecl: function() { | |
| 548 | 199 | var node = new Node(Node.FNDECL); |
| 549 | 199 | node.add( |
| 550 | this.match('function'), | |
| 551 | this.match(Token.ID, 'function statement requires a name'), | |
| 552 | this.match('(') | |
| 553 | ); | |
| 554 | 199 | if(!this.look) { |
| 555 | 1 | this.error('missing formal parameter'); |
| 556 | } | |
| 557 | 198 | if(this.look.content() != ')') { |
| 558 | 155 | node.add(this.fnparams()); |
| 559 | } | |
| 560 | 198 | node.add( |
| 561 | this.match(')'), | |
| 562 | this.match('{'), | |
| 563 | this.fnbody(), | |
| 564 | this.match('}') | |
| 565 | ); | |
| 566 | 198 | return node; |
| 567 | }, | |
| 568 | fnexpr: function() { | |
| 569 | 1625 | var node = new Node(Node.FNEXPR); |
| 570 | 1625 | node.add(this.match('function')); |
| 571 | 1625 | if(!this.look) { |
| 572 | 1 | this.error('missing formal parameter'); |
| 573 | } | |
| 574 | 1624 | if(this.look.type() == Token.ID) { |
| 575 | 22 | node.add(this.match()); |
| 576 | } | |
| 577 | 1624 | node.add(this.match('(')); |
| 578 | 1624 | if(!this.look) { |
| 579 | 1 | this.error(); |
| 580 | } | |
| 581 | 1623 | if(this.look.content() != ')') { |
| 582 | 1147 | node.add(this.fnparams()); |
| 583 | } | |
| 584 | 1621 | node.add( |
| 585 | this.match(')'), | |
| 586 | this.match('{'), | |
| 587 | this.fnbody(), | |
| 588 | this.match('}', 'missing } in compound statement') | |
| 589 | ); | |
| 590 | 1621 | return node; |
| 591 | }, | |
| 592 | fnparams: function() { | |
| 593 | 1303 | var node = new Node(Node.FNPARAMS); |
| 594 | 1303 | node.add(this.match(Token.ID, 'missing formal parameter')); |
| 595 | 1303 | while(this.look && this.look.content() == ',') { |
| 596 | 985 | node.add(this.match()); |
| 597 | 985 | if(!this.look) { |
| 598 | 1 | this.error('missing formal parameter'); |
| 599 | } | |
| 600 | 984 | if(this.look.type() == Token.ID) { |
| 601 | 980 | node.add(this.match()); |
| 602 | 980 | if(this.look && this.look.content() == '=') { |
| 603 | 3 | node.add(this.bindelement()); |
| 604 | } | |
| 605 | } | |
| 606 | 4 | else if(this.look.content() == '...') { |
| 607 | 3 | node.add(this.restparam()); |
| 608 | } | |
| 609 | else { | |
| 610 | 1 | this.error('missing formal parameter'); |
| 611 | } | |
| 612 | } | |
| 613 | 1301 | return node; |
| 614 | }, | |
| 615 | bindelement: function() { | |
| 616 | 3 | var node = new Node(Node.BINDELEMENT); |
| 617 | 3 | node.add(this.match('='), this.assignexpr()); |
| 618 | 3 | return node; |
| 619 | }, | |
| 620 | restparam: function() { | |
| 621 | 3 | var node = new Node(Node.RESTPARAM); |
| 622 | 3 | node.add(this.match('...'), this.match(Token.ID)); |
| 623 | 3 | return node; |
| 624 | }, | |
| 625 | fnbody: function(allowSuper) { | |
| 626 | 1826 | var node = new Node(Node.FNBODY); |
| 627 | 1826 | while(this.look && this.look.content() != '}') { |
| 628 | 5168 | node.add(this.element(allowSuper)); |
| 629 | } | |
| 630 | 1826 | return node; |
| 631 | }, | |
| 632 | classdecl: function() { | |
| 633 | 13 | var node = new Node(Node.CLASSDECL); |
| 634 | 13 | node.add(this.match('class'), this.match(Token.ID)); |
| 635 | 13 | if(!this.look) { |
| 636 | 1 | this.error(); |
| 637 | } | |
| 638 | 12 | if(this.look.content() == 'extends') { |
| 639 | 3 | node.add(this.heratige()); |
| 640 | } | |
| 641 | 12 | node.add( |
| 642 | this.match('{'), | |
| 643 | this.classbody(), | |
| 644 | this.match('}') | |
| 645 | ); | |
| 646 | 9 | return node; |
| 647 | }, | |
| 648 | heratige: function() { | |
| 649 | 3 | var node = new Node(Node.HERITAGE); |
| 650 | 3 | node.add(this.match('extends'), this.match(Token.ID)); |
| 651 | 3 | return node; |
| 652 | }, | |
| 653 | classbody: function() { | |
| 654 | 12 | var node = new Node(Node.CLASSBODY), |
| 655 | methods = {}, | |
| 656 | hasStatic = false; | |
| 657 | 12 | while(this.look && this.look.content() != '}') { |
| 658 | 11 | if(this.look.content() == ';') { |
| 659 | 1 | node.add(this.match()); |
| 660 | 1 | continue; |
| 661 | } | |
| 662 | 10 | hasStatic = false; |
| 663 | 10 | if(this.look.content() == 'static') { |
| 664 | 2 | node.add(this.match()); |
| 665 | 2 | hasStatic = true; |
| 666 | } | |
| 667 | 10 | if(!this.look) { |
| 668 | 1 | this.error(); |
| 669 | } | |
| 670 | 9 | node.add(this.method(hasStatic, methods)); |
| 671 | } | |
| 672 | 9 | return node; |
| 673 | }, | |
| 674 | method: function(hasStatic, methods, statics) { | |
| 675 | 9 | var node = new Node(Node.METHOD); |
| 676 | 9 | if(this.look.content() == 'get') { |
| 677 | 1 | node.add(this.match(), this.getfn()); |
| 678 | } | |
| 679 | 8 | else if(this.look.content() == 'set') { |
| 680 | 1 | node.add(this.match(), this.setfn()); |
| 681 | } | |
| 682 | else { | |
| 683 | 7 | node.add(this.match(Token.ID)); |
| 684 | 7 | var id = node.leaves()[0].token().content(); |
| 685 | 7 | if(methods.hasOwnProperty(id)) { |
| 686 | 1 | this.error('duplicate method decl in class'); |
| 687 | } | |
| 688 | 6 | methods[id] = true; |
| 689 | 6 | node.add(this.match('(')); |
| 690 | 5 | if(this.look.content() != ')') { |
| 691 | 1 | node.add(this.fnparams()); |
| 692 | } | |
| 693 | 5 | node.add( |
| 694 | this.match(')'), | |
| 695 | this.match('{'), | |
| 696 | this.fnbody(true), | |
| 697 | this.match('}', 'missing } in compound statement') | |
| 698 | ); | |
| 699 | } | |
| 700 | 7 | return node; |
| 701 | }, | |
| 702 | expr: function(noIn) { | |
| 703 | 11056 | var node = new Node(Node.EXPR), |
| 704 | assignexpr = this.assignexpr(noIn); | |
| 705 | 11043 | if(this.look && this.look.content() == ',') { |
| 706 | 540 | node.add(assignexpr); |
| 707 | 540 | while(this.look && this.look.content() == ',') { |
| 708 | 1051 | node.add(this.match(), this.assignexpr(noIn)); |
| 709 | } | |
| 710 | } | |
| 711 | else { | |
| 712 | 10503 | return assignexpr; |
| 713 | } | |
| 714 | 540 | return node; |
| 715 | }, | |
| 716 | assignexpr: function(noIn) { | |
| 717 | 30758 | var node = new Node(Node.ASSIGNEXPR), |
| 718 | cndt = this.cndtexpr(noIn); | |
| 719 | 30745 | if(this.look && { |
| 720 | '*=': true, | |
| 721 | '/=': true, | |
| 722 | '%=': true, | |
| 723 | '+=': true, | |
| 724 | '-=': true, | |
| 725 | '<<=': true, | |
| 726 | '>>=': true, | |
| 727 | '>>>=': true, | |
| 728 | '&=': true, | |
| 729 | '^=': true, | |
| 730 | '|=': true, | |
| 731 | '=': true | |
| 732 | }.hasOwnProperty(this.look.content())) { | |
| 733 | 3675 | node.add(cndt, this.match(), this.assignexpr(noIn)); |
| 734 | } | |
| 735 | else { | |
| 736 | 27070 | return cndt; |
| 737 | } | |
| 738 | 3675 | return node; |
| 739 | }, | |
| 740 | cndtexpr: function(noIn) { | |
| 741 | 30758 | var node = new Node(Node.CNDTEXPR), |
| 742 | logorexpr = this.logorexpr(noIn); | |
| 743 | 30745 | if(this.look && this.look.content() == '?') { |
| 744 | 878 | node.add( |
| 745 | logorexpr, | |
| 746 | this.match(), | |
| 747 | this.assignexpr(noIn), | |
| 748 | this.match(':'), | |
| 749 | this.assignexpr(noIn) | |
| 750 | ); | |
| 751 | } | |
| 752 | else { | |
| 753 | 29867 | return logorexpr; |
| 754 | } | |
| 755 | 878 | return node; |
| 756 | }, | |
| 757 | logorexpr: function(noIn) { | |
| 758 | 30758 | var node = new Node(Node.LOGOREXPR), |
| 759 | logandexpr = this.logandexpr(noIn); | |
| 760 | 30745 | if(this.look && this.look.content() == '||') { |
| 761 | 1015 | node.add(logandexpr); |
| 762 | 1015 | while(this.look && this.look.content() == '||') { |
| 763 | 1150 | node.add( |
| 764 | this.match(), | |
| 765 | this.logandexpr(noIn) | |
| 766 | ); | |
| 767 | } | |
| 768 | } | |
| 769 | else { | |
| 770 | 29730 | return logandexpr; |
| 771 | } | |
| 772 | 1015 | return node; |
| 773 | }, | |
| 774 | logandexpr: function(noIn) { | |
| 775 | 31908 | var node = new Node(Node.LOGANDEXPR), |
| 776 | bitorexpr = this.bitorexpr(noIn); | |
| 777 | 31895 | if(this.look && this.look.content() == '&&') { |
| 778 | 1129 | node.add(bitorexpr); |
| 779 | 1129 | while(this.look && this.look.content() == '&&') { |
| 780 | 1375 | node.add( |
| 781 | this.match(), | |
| 782 | this.bitorexpr(noIn) | |
| 783 | ); | |
| 784 | } | |
| 785 | } | |
| 786 | else { | |
| 787 | 30766 | return bitorexpr; |
| 788 | } | |
| 789 | 1129 | return node; |
| 790 | }, | |
| 791 | bitorexpr: function(noIn) { | |
| 792 | 33283 | var node = new Node(Node.BITOREXPR), |
| 793 | bitxorexpr = this.bitxorexpr(noIn); | |
| 794 | 33270 | if(this.look && this.look.content() == '|') { |
| 795 | 5 | node.add(bitxorexpr); |
| 796 | 5 | while(this.look && this.look.content() == '|') { |
| 797 | 5 | node.add( |
| 798 | this.match(), | |
| 799 | this.bitxorexpr(noIn) | |
| 800 | ); | |
| 801 | } | |
| 802 | } | |
| 803 | else { | |
| 804 | 33265 | return bitxorexpr; |
| 805 | } | |
| 806 | 5 | return node; |
| 807 | }, | |
| 808 | bitxorexpr: function(noIn) { | |
| 809 | 33288 | var node = new Node(Node.BITXOREXPR), |
| 810 | bitandexpr = this.bitandexpr(noIn); | |
| 811 | 33275 | if(this.look && this.look.content() == '^') { |
| 812 | 2 | node.add(bitandexpr); |
| 813 | 2 | while(this.look && this.look.content() == '^') { |
| 814 | 2 | node.add( |
| 815 | this.match(), | |
| 816 | this.bitandexpr(noIn) | |
| 817 | ); | |
| 818 | } | |
| 819 | } | |
| 820 | else { | |
| 821 | 33273 | return bitandexpr; |
| 822 | } | |
| 823 | 2 | return node; |
| 824 | }, | |
| 825 | bitandexpr: function(noIn) { | |
| 826 | 33290 | var node = new Node(Node.BITANDEXPR), |
| 827 | eqexpr = this.eqexpr(noIn); | |
| 828 | 33277 | if(this.look && this.look.content() == '&') { |
| 829 | 18 | node.add(eqexpr); |
| 830 | 18 | while(this.look && this.look.content() == '&') { |
| 831 | 18 | node.add( |
| 832 | this.match(), | |
| 833 | this.eqexpr(noIn) | |
| 834 | ); | |
| 835 | } | |
| 836 | } | |
| 837 | else { | |
| 838 | 33259 | return eqexpr; |
| 839 | } | |
| 840 | 18 | return node; |
| 841 | }, | |
| 842 | eqexpr: function(noIn) { | |
| 843 | 33308 | var node = new Node(Node.EQEXPR), |
| 844 | reltexpr = this.reltexpr(noIn); | |
| 845 | 33295 | if(this.look && { |
| 846 | '==': true, | |
| 847 | '===': true, | |
| 848 | '!==': true, | |
| 849 | '!=': true | |
| 850 | }.hasOwnProperty(this.look.content())) { | |
| 851 | 1589 | node.add(reltexpr); |
| 852 | 1589 | while(this.look && { |
| 853 | '==': true, | |
| 854 | '===': true, | |
| 855 | '!==': true, | |
| 856 | '!=': true | |
| 857 | }.hasOwnProperty(this.look.content())) { | |
| 858 | 1590 | node.add( |
| 859 | this.match(), | |
| 860 | this.reltexpr(noIn) | |
| 861 | ); | |
| 862 | } | |
| 863 | } | |
| 864 | else { | |
| 865 | 31706 | return reltexpr; |
| 866 | } | |
| 867 | 1589 | return node; |
| 868 | }, | |
| 869 | reltexpr: function(noIn) { | |
| 870 | 34898 | var node = new Node(Node.RELTEXPR), |
| 871 | shiftexpr = this.shiftexpr(); | |
| 872 | 34885 | if(this.look && ({ |
| 873 | '<': true, | |
| 874 | '>': true, | |
| 875 | '>=': true, | |
| 876 | '<=': true, | |
| 877 | 'instanceof': true | |
| 878 | }.hasOwnProperty(this.look.content()) || (!noIn && this.look.content() == 'in'))) { | |
| 879 | 477 | node.add(shiftexpr); |
| 880 | 477 | while(this.look && ({ |
| 881 | '<': true, | |
| 882 | '>': true, | |
| 883 | '>=': true, | |
| 884 | '<=': true, | |
| 885 | 'instanceof': true | |
| 886 | }.hasOwnProperty(this.look.content()) || (!noIn && this.look.content() == 'in'))) { | |
| 887 | 477 | node.add( |
| 888 | this.match(), | |
| 889 | this.shiftexpr() | |
| 890 | ); | |
| 891 | } | |
| 892 | } | |
| 893 | else { | |
| 894 | 34408 | return shiftexpr; |
| 895 | } | |
| 896 | 477 | return node; |
| 897 | }, | |
| 898 | shiftexpr: function() { | |
| 899 | 35375 | var node = new Node(Node.SHIFTEXPR), |
| 900 | addexpr = this.addexpr(); | |
| 901 | 35362 | if(this.look && ['<<', '>>', '>>>'].indexOf(this.look.content()) != -1) { |
| 902 | 8 | node.add(addexpr); |
| 903 | 8 | while(this.look && ['<<', '>>', '>>>'].indexOf(this.look.content()) != -1) { |
| 904 | 9 | node.add( |
| 905 | this.match(), | |
| 906 | this.addexpr() | |
| 907 | ); | |
| 908 | } | |
| 909 | } | |
| 910 | else { | |
| 911 | 35354 | return addexpr; |
| 912 | } | |
| 913 | 8 | return node; |
| 914 | }, | |
| 915 | addexpr: function() { | |
| 916 | 35384 | var node = new Node(Node.ADDEXPR), |
| 917 | mtplexpr = this.mtplexpr(); | |
| 918 | 35371 | if(this.look && ['+', '-'].indexOf(this.look.content()) != -1) { |
| 919 | 777 | node.add(mtplexpr); |
| 920 | 777 | while(this.look && ['+', '-'].indexOf(this.look.content()) != -1) { |
| 921 | 1318 | node.add( |
| 922 | this.match(), | |
| 923 | this.mtplexpr() | |
| 924 | ); | |
| 925 | } | |
| 926 | } | |
| 927 | else { | |
| 928 | 34594 | return mtplexpr; |
| 929 | } | |
| 930 | 777 | return node; |
| 931 | }, | |
| 932 | mtplexpr: function() { | |
| 933 | 36702 | var node = new Node(Node.MTPLEXPR), |
| 934 | unaryexpr = this.unaryexpr(); | |
| 935 | 36689 | if(this.look && ['*', '/', '%'].indexOf(this.look.content()) != -1) { |
| 936 | 51 | node.add(unaryexpr); |
| 937 | 51 | while(this.look && ['*', '/', '%'].indexOf(this.look.content()) != -1) { |
| 938 | 52 | node.add( |
| 939 | this.match(), | |
| 940 | this.unaryexpr() | |
| 941 | ); | |
| 942 | } | |
| 943 | } | |
| 944 | else { | |
| 945 | 36638 | return unaryexpr; |
| 946 | } | |
| 947 | 51 | return node; |
| 948 | }, | |
| 949 | unaryexpr: function() { | |
| 950 | 38472 | var node = new Node(Node.UNARYEXPR); |
| 951 | 38472 | if(!this.look) { |
| 952 | 2 | this.error(); |
| 953 | } | |
| 954 | 38470 | switch(this.look.content()) { |
| 955 | case 'delete': | |
| 956 | case 'void': | |
| 957 | case 'typeof': | |
| 958 | case '++': | |
| 959 | case '--': | |
| 960 | case '+': | |
| 961 | case '-': | |
| 962 | case '~': | |
| 963 | case '!': | |
| 964 | 1718 | node.add( |
| 965 | this.match(), | |
| 966 | this.unaryexpr() | |
| 967 | ); | |
| 968 | 1716 | break; |
| 969 | default: | |
| 970 | 36752 | return this.postfixexpr(); |
| 971 | } | |
| 972 | 1716 | return node; |
| 973 | }, | |
| 974 | postfixexpr: function() { | |
| 975 | 36752 | var node = new Node(Node.POSTFIXEXPR); |
| 976 | 36752 | var leftexpr = this.leftexpr(); |
| 977 | 36741 | if(this.look && ['++', '--'].indexOf(this.look.content()) > -1 && !this.hasMoveLine) { |
| 978 | 327 | node.add(leftexpr); |
| 979 | 327 | while(this.look && ['++', '--'].indexOf(this.look.content()) > -1) { |
| 980 | 327 | node.add(this.match(undefined, true)); |
| 981 | } | |
| 982 | } | |
| 983 | else { | |
| 984 | 36414 | return leftexpr; |
| 985 | } | |
| 986 | 327 | return node; |
| 987 | }, | |
| 988 | leftexpr: function() { | |
| 989 | 36752 | if(this.look.content() == 'new') { |
| 990 | 190 | return this.newexpr(); |
| 991 | } | |
| 992 | else { | |
| 993 | 36562 | return this.callexpr(); |
| 994 | } | |
| 995 | }, | |
| 996 | newexpr: function(depth) { | |
| 997 | 191 | depth = depth || 0; |
| 998 | 191 | var node = new Node(Node.NEWEXPR); |
| 999 | 191 | node.add(this.match('new')); |
| 1000 | 191 | if(!this.look) { |
| 1001 | 1 | this.error(); |
| 1002 | } | |
| 1003 | 190 | if(this.look.content() == 'new') { |
| 1004 | 1 | node.add(this.newexpr(depth + 1)); |
| 1005 | } | |
| 1006 | else { | |
| 1007 | 189 | node.add(this.mmbexpr()); |
| 1008 | } | |
| 1009 | 190 | if(this.look && this.look.content() == '(') { |
| 1010 | 179 | node.add(this.args()); |
| 1011 | } | |
| 1012 | 190 | if(this.look && ['.', '['].indexOf(this.look.content()) > -1) { |
| 1013 | 11 | var mmb = new Node(Node.MMBEXPR); |
| 1014 | 11 | mmb.add(node); |
| 1015 | 11 | while(this.look) { |
| 1016 | 20 | if(this.look.content() == '.') { |
| 1017 | 10 | mmb.add( |
| 1018 | this.match(), | |
| 1019 | this.match(Token.ID) | |
| 1020 | ); | |
| 1021 | } | |
| 1022 | 10 | else if(this.look.content() == '[') { |
| 1023 | 1 | mmb.add( |
| 1024 | this.match(), | |
| 1025 | this.expr(), | |
| 1026 | this.match(']') | |
| 1027 | ); | |
| 1028 | } | |
| 1029 | else { | |
| 1030 | 9 | break; |
| 1031 | } | |
| 1032 | } | |
| 1033 | 11 | if(depth == 0 && this.look && this.look.content() == '(') { |
| 1034 | 8 | var callexpr = this.callexpr(mmb); |
| 1035 | 8 | return callexpr; |
| 1036 | } | |
| 1037 | 3 | return mmb; |
| 1038 | } | |
| 1039 | 179 | return node; |
| 1040 | }, | |
| 1041 | callexpr: function(mmb) { | |
| 1042 | 36570 | var node = new Node(Node.CALLEXPR); |
| 1043 | 36570 | mmb = mmb || this.mmbexpr(); |
| 1044 | 36561 | if(this.look && this.look.content() == '(') { |
| 1045 | 5156 | node.add( |
| 1046 | mmb, | |
| 1047 | this.args() | |
| 1048 | ); | |
| 1049 | 5155 | if(this.look && ['.', '[', '('].indexOf(this.look.content()) > -1) { |
| 1050 | 364 | while(this.look) { |
| 1051 | 1186 | if(this.look.content() == '.') { |
| 1052 | 426 | node.add( |
| 1053 | this.match(), | |
| 1054 | this.match(Token.ID) | |
| 1055 | ); | |
| 1056 | } | |
| 1057 | 760 | else if(this.look.content() == '[') { |
| 1058 | 45 | node.add( |
| 1059 | this.match(), | |
| 1060 | this.expr(), | |
| 1061 | this.match(']') | |
| 1062 | ); | |
| 1063 | } | |
| 1064 | 715 | else if(this.look.content() == '(') { |
| 1065 | 351 | node.add(this.args()); |
| 1066 | } | |
| 1067 | else { | |
| 1068 | 364 | break; |
| 1069 | } | |
| 1070 | } | |
| 1071 | } | |
| 1072 | } | |
| 1073 | else { | |
| 1074 | 31405 | return mmb; |
| 1075 | } | |
| 1076 | 5155 | return node; |
| 1077 | }, | |
| 1078 | mmbexpr: function() { | |
| 1079 | 36751 | var node = new Node(Node.MMBEXPR); |
| 1080 | 36751 | var mmb; |
| 1081 | 36751 | if(this.look.content() == 'function') { |
| 1082 | 1625 | mmb = this.fnexpr(); |
| 1083 | } | |
| 1084 | else { | |
| 1085 | 35126 | mmb = this.prmrexpr(); |
| 1086 | } | |
| 1087 | 36742 | if(this.look && ['.', '['].indexOf(this.look.content()) > -1) { |
| 1088 | 10737 | node.add(mmb); |
| 1089 | 10737 | while(this.look) { |
| 1090 | 23519 | if(this.look.content() == '.') { |
| 1091 | 10859 | node.add( |
| 1092 | this.match(), | |
| 1093 | this.match(Token.ID) | |
| 1094 | ); | |
| 1095 | } | |
| 1096 | 12660 | else if(this.look.content() == '[') { |
| 1097 | 1928 | node.add( |
| 1098 | this.match(), | |
| 1099 | this.expr(), | |
| 1100 | this.match(']') | |
| 1101 | ); | |
| 1102 | } | |
| 1103 | else { | |
| 1104 | 10732 | break; |
| 1105 | } | |
| 1106 | } | |
| 1107 | } | |
| 1108 | else { | |
| 1109 | 26005 | return mmb; |
| 1110 | } | |
| 1111 | 10737 | return node; |
| 1112 | }, | |
| 1113 | prmrexpr: function() { | |
| 1114 | 35126 | var node = new Node(Node.PRMREXPR); |
| 1115 | 35126 | switch(this.look.type()) { |
| 1116 | case Token.ID: | |
| 1117 | case Token.NUMBER: | |
| 1118 | case Token.STRING: | |
| 1119 | case Token.REG: | |
| 1120 | case Token.TEMPLATE: | |
| 1121 | 29061 | node.add(this.match()); |
| 1122 | 29061 | break; |
| 1123 | default: | |
| 1124 | 6065 | switch(this.look.content()) { |
| 1125 | case 'this': | |
| 1126 | case 'null': | |
| 1127 | case 'true': | |
| 1128 | case 'false': | |
| 1129 | 3381 | node.add(this.match()); |
| 1130 | 3381 | break; |
| 1131 | case '(': | |
| 1132 | 1087 | node.add(this.match(), this.expr(), this.match(')')); |
| 1133 | 1083 | break; |
| 1134 | case '[': | |
| 1135 | 867 | node.add(this.arrltr()); |
| 1136 | 867 | break; |
| 1137 | case '{': | |
| 1138 | 729 | node.add(this.objltr()); |
| 1139 | 729 | break; |
| 1140 | default: | |
| 1141 | 1 | this.error(); |
| 1142 | } | |
| 1143 | } | |
| 1144 | 35121 | return node; |
| 1145 | }, | |
| 1146 | arrltr: function() { | |
| 1147 | 867 | var node = new Node(Node.ARRLTR); |
| 1148 | 867 | node.add(this.match('[')); |
| 1149 | 867 | while(this.look && this.look.content() != ']') { |
| 1150 | 2366 | if(this.look.content() == ',') { |
| 1151 | 864 | node.add(this.match()); |
| 1152 | } | |
| 1153 | else { | |
| 1154 | 1502 | node.add(this.assignexpr()); |
| 1155 | } | |
| 1156 | } | |
| 1157 | 867 | node.add(this.match(']')); |
| 1158 | 867 | return node; |
| 1159 | }, | |
| 1160 | objltr: function() { | |
| 1161 | 729 | var node = new Node(Node.OBJLTR); |
| 1162 | 729 | node.add(this.match('{')); |
| 1163 | 729 | while(this.look && this.look.content() != '}') { |
| 1164 | 2002 | node.add(this.proptassign()); |
| 1165 | 2002 | if(this.look && this.look.content() == ',') { |
| 1166 | 1511 | node.add(this.match()); |
| 1167 | } | |
| 1168 | } | |
| 1169 | 729 | node.add(this.match('}')); |
| 1170 | 729 | return node; |
| 1171 | }, | |
| 1172 | proptassign: function() { | |
| 1173 | 2002 | var node = new Node(Node.PROPTASSIGN); |
| 1174 | 2002 | if(!this.look) { |
| 1175 | 0 | this.error(); |
| 1176 | } | |
| 1177 | 2002 | if(this.look.content() == 'get') { |
| 1178 | 28 | node.add(this.match()); |
| 1179 | 28 | if(!this.look) { |
| 1180 | 0 | this.error(); |
| 1181 | } | |
| 1182 | 28 | if(this.look.content() == ':') { |
| 1183 | 28 | node.add(this.match(), this.assignexpr()); |
| 1184 | } | |
| 1185 | else { | |
| 1186 | 0 | node.add(this.getfn()); |
| 1187 | } | |
| 1188 | } | |
| 1189 | 1974 | else if(this.look.content() == 'set') { |
| 1190 | 30 | node.add(this.match()); |
| 1191 | 30 | if(!this.look) { |
| 1192 | 0 | this.error(); |
| 1193 | } | |
| 1194 | 30 | if(this.look.content() == ':') { |
| 1195 | 30 | node.add(this.match(), this.assignexpr()); |
| 1196 | } | |
| 1197 | else { | |
| 1198 | 0 | node.add(this.setfn()); |
| 1199 | } | |
| 1200 | } | |
| 1201 | else { | |
| 1202 | 1944 | switch(this.look.type()) { |
| 1203 | case Token.ID: | |
| 1204 | case Token.STRING: | |
| 1205 | case Token.NUMBER: | |
| 1206 | 1944 | node.add( |
| 1207 | this.match(), | |
| 1208 | this.match(':'), | |
| 1209 | this.assignexpr() | |
| 1210 | ); | |
| 1211 | 1944 | break; |
| 1212 | default: | |
| 1213 | 0 | this.error('invalid property id'); |
| 1214 | } | |
| 1215 | } | |
| 1216 | 2002 | return node; |
| 1217 | }, | |
| 1218 | getfn: function() { | |
| 1219 | 1 | var node = new Node(Node.GETFN); |
| 1220 | 1 | node.add( |
| 1221 | this.proptname(), | |
| 1222 | this.match('('), | |
| 1223 | this.match(')'), | |
| 1224 | this.match('{'), | |
| 1225 | this.fnbody(), | |
| 1226 | this.match('}') | |
| 1227 | ); | |
| 1228 | 1 | return node; |
| 1229 | }, | |
| 1230 | setfn: function() { | |
| 1231 | 1 | var node = new Node(Node.SETFN); |
| 1232 | 1 | node.add( |
| 1233 | this.proptname(), | |
| 1234 | this.match('('), | |
| 1235 | this.propsets(), | |
| 1236 | this.match(')'), | |
| 1237 | this.match('{'), | |
| 1238 | this.fnbody(), | |
| 1239 | this.match('}') | |
| 1240 | ); | |
| 1241 | 1 | return node; |
| 1242 | }, | |
| 1243 | proptname: function() { | |
| 1244 | 2 | var node = new Node(Node.PROPTNAME); |
| 1245 | 2 | if(this.look) { |
| 1246 | 2 | switch(this.look.type()) { |
| 1247 | case Token.ID: | |
| 1248 | case Token.NUMBER: | |
| 1249 | case Token.STRING: | |
| 1250 | 2 | node.add(this.match()); |
| 1251 | 2 | break; |
| 1252 | default: | |
| 1253 | 0 | this.error('missing name after . operator'); |
| 1254 | } | |
| 1255 | } | |
| 1256 | 2 | return node; |
| 1257 | }, | |
| 1258 | propsets: function() { | |
| 1259 | 1 | var node = new Node(Node.PROPTSETS); |
| 1260 | 1 | node.add(this.match(Token.ID, 'setter functions must have one argument')); |
| 1261 | 1 | return node; |
| 1262 | }, | |
| 1263 | args: function() { | |
| 1264 | 5688 | var node = new Node(Node.ARGS); |
| 1265 | 5688 | node.add(this.match('(')); |
| 1266 | 5688 | if(!this.look) { |
| 1267 | 1 | this.error(); |
| 1268 | } | |
| 1269 | 5687 | if(this.look.content() != ')') { |
| 1270 | 4947 | node.add(this.arglist()); |
| 1271 | } | |
| 1272 | 5687 | node.add(this.match(')')); |
| 1273 | 5687 | return node; |
| 1274 | }, | |
| 1275 | arglist: function() { | |
| 1276 | 4947 | var node = new Node(Node.ARGLIST); |
| 1277 | 4947 | node.add(this.assignexpr()); |
| 1278 | 4947 | while(this.look && this.look.content() == ',') { |
| 1279 | 2848 | node.add(this.match(), this.assignexpr()); |
| 1280 | } | |
| 1281 | 4947 | return node; |
| 1282 | }, | |
| 1283 | virtual: function(s) { | |
| 1284 | 1581 | return new Node(Node.TOKEN, new Token(Token.VIRTUAL, s)); |
| 1285 | }, | |
| 1286 | match: function(type, line, msg) { | |
| 1287 | 140140 | if(typeof type == 'boolean') { |
| 1288 | 0 | msg = line; |
| 1289 | 0 | line = type; |
| 1290 | 0 | type = undefined; |
| 1291 | } | |
| 1292 | 140140 | if(typeof line != 'boolean') { |
| 1293 | 137997 | line = false; |
| 1294 | 137997 | msg = line; |
| 1295 | } | |
| 1296 | //未定义为所有非空白token | |
| 1297 | 140140 | if(character.isUndefined(type)) { |
| 1298 | 71758 | if(this.look) { |
| 1299 | 71758 | var l = this.look; |
| 1300 | 71758 | this.move(line); |
| 1301 | 71758 | return new Node(Node.TOKEN, l); |
| 1302 | } | |
| 1303 | else { | |
| 1304 | 0 | this.error('syntax error' + (msg || '')); |
| 1305 | } | |
| 1306 | } | |
| 1307 | //或者根据token的type或者content匹配 | |
| 1308 | 68382 | else if(typeof type == 'string') { |
| 1309 | //特殊处理;,不匹配但有换行或者末尾时自动补全,还有受限行 | |
| 1310 | 52697 | if(type == ';' |
| 1311 | && (!this.look | |
| 1312 | || (this.look.content() != type && this.hasMoveLine) | |
| 1313 | || this.look.content() == '}') | |
| 1314 | ) { | |
| 1315 | 1581 | if(this.look && S[this.look.type()]) { |
| 1316 | 36 | this.move(); |
| 1317 | } | |
| 1318 | 1581 | return this.virtual(';'); |
| 1319 | } | |
| 1320 | 51116 | else if(this.look && this.look.content() == type) { |
| 1321 | 51115 | var l = this.look; |
| 1322 | 51115 | this.move(line); |
| 1323 | 51115 | return new Node(Node.TOKEN, l); |
| 1324 | } | |
| 1325 | else { | |
| 1326 | 1 | this.error('missing ' + type + (msg || '')); |
| 1327 | } | |
| 1328 | } | |
| 1329 | 15685 | else if(typeof type == 'number') { |
| 1330 | 15685 | if(this.look && this.look.type() == type) { |
| 1331 | 15685 | var l = this.look; |
| 1332 | 15685 | this.move(line); |
| 1333 | 15685 | return new Node(Node.TOKEN, l); |
| 1334 | } | |
| 1335 | else { | |
| 1336 | 0 | this.error('missing ' + Token.type(type) + (msg || '')); |
| 1337 | } | |
| 1338 | } | |
| 1339 | }, | |
| 1340 | move: function(line) { | |
| 1341 | 138772 | this.lastLine = this.line; |
| 1342 | 138772 | this.lastCol = this.col; |
| 1343 | //遗留下来的换行符 | |
| 1344 | 138772 | this.hasMoveLine = false; |
| 1345 | 138772 | do { |
| 1346 | 247985 | this.look = this.tokens[this.index++]; |
| 1347 | 247985 | if(!this.look) { |
| 1348 | 171 | return; |
| 1349 | } | |
| 1350 | //存下忽略的token | |
| 1351 | 247814 | if(S[this.look.type()]) { |
| 1352 | 109249 | this.ignores[this.index - 1] = this.look; |
| 1353 | } | |
| 1354 | //包括line的情况下要跳出 | |
| 1355 | 247814 | if(this.look.type() == Token.LINE) { |
| 1356 | 17784 | this.line++; |
| 1357 | 17784 | this.col = 1; |
| 1358 | 17784 | this.hasMoveLine = true; |
| 1359 | 17784 | if(line) { |
| 1360 | 35 | break; |
| 1361 | } | |
| 1362 | } | |
| 1363 | 230030 | else if(this.look.type() == Token.COMMENT) { |
| 1364 | 2742 | var s = this.look.content(); |
| 1365 | 2742 | var n = character.count(this.look.content(), character.LINE); |
| 1366 | 2742 | if(n > 0) { |
| 1367 | 90 | this.line += n; |
| 1368 | 90 | var i = s.lastIndexOf(character.LINE); |
| 1369 | 90 | this.col += s.length - i - 1; |
| 1370 | 90 | this.hasMoveLine = true; |
| 1371 | 90 | if(line) { |
| 1372 | 1 | break; |
| 1373 | } | |
| 1374 | } | |
| 1375 | } | |
| 1376 | else { | |
| 1377 | 227288 | this.col += this.look.content().length; |
| 1378 | 227288 | if(!S[this.look.type()]) { |
| 1379 | 138565 | break; |
| 1380 | } | |
| 1381 | } | |
| 1382 | } while(this.index <= this.length); | |
| 1383 | }, | |
| 1384 | error: function(msg) { | |
| 1385 | 28 | msg = 'SyntaxError: ' + (msg || ' syntax error'); |
| 1386 | 28 | throw new Error(msg + ' line ' + this.lastLine + ' col ' + this.lastCol); |
| 1387 | }, | |
| 1388 | ignore: function() { | |
| 1389 | 8 | return this.ignores; |
| 1390 | } | |
| 1391 | }); | |
| 1392 | 1 | module.exports = Parser; |
| Line | Hits | Source |
|---|---|---|
| 1 | 1 | function inheritPrototype(subType, superType) { |
| 2 | 10 | var prototype = Object.create(superType.prototype); |
| 3 | 10 | prototype.constructor = subType; |
| 4 | 10 | subType.prototype = prototype; |
| 5 | //继承static变量 | |
| 6 | 10 | Object.keys(superType).forEach(function(k) { |
| 7 | 37 | subType[k] = superType[k]; |
| 8 | }); | |
| 9 | 10 | return subType; |
| 10 | } | |
| 11 | 1 | function wrap(fn) { |
| 12 | 19 | fn.extend = function(sub) { |
| 13 | 10 | inheritPrototype(sub, fn); |
| 14 | 10 | return wrap(sub); |
| 15 | } | |
| 16 | 19 | fn.methods = function(o) { |
| 17 | 16 | Object.keys(o).forEach(function(k) { |
| 18 | 191 | fn.prototype[k] = o[k]; |
| 19 | }); | |
| 20 | 16 | return fn; |
| 21 | }; | |
| 22 | 19 | fn.statics = function(o) { |
| 23 | 8 | Object.keys(o).forEach(function(k) { |
| 24 | 136 | fn[k] = o[k]; |
| 25 | }); | |
| 26 | 8 | return fn; |
| 27 | }; | |
| 28 | 19 | return fn; |
| 29 | } | |
| 30 | 1 | function klass(cons) { |
| 31 | 9 | return wrap(cons || function() {}); |
| 32 | } | |
| 33 | 1 | klass.extend = inheritPrototype; |
| 34 | 1 | module.exports = klass; |
| Line | Hits | Source |
|---|---|---|
| 1 | 1 | exports.LINE = '\n'; |
| 2 | 1 | exports.ENTER = '\r'; |
| 3 | 1 | exports.BLANK = ' '; |
| 4 | 1 | exports.TAB = '\t'; |
| 5 | 1 | exports.UNDERLINE = '_'; |
| 6 | 1 | exports.DOLLAR = '$'; |
| 7 | 1 | exports.SHARP = '#'; |
| 8 | 1 | exports.MINUS = '-'; |
| 9 | 1 | exports.AT = '@'; |
| 10 | 1 | exports.SLASH = '/'; |
| 11 | 1 | exports.BACK_SLASH = '\\'; |
| 12 | 1 | exports.DECIMAL = '.'; |
| 13 | 1 | exports.LEFT_BRACKET = '['; |
| 14 | 1 | exports.RIGHT_BRACKET = ']'; |
| 15 | 1 | exports.STAR = '*'; |
| 16 | 1 | exports.LEFT_PARENTHESE = '('; |
| 17 | 1 | exports.RIGHT_PARENTHESE = ')'; |
| 18 | 1 | exports.COMMA = ','; |
| 19 | 1 | exports.SEMICOLON = ';'; |
| 20 | 1 | exports.EQUAL = '='; |
| 21 | 1 | exports.isDigit = function(c) { |
| 22 | 0 | return c >= '0' && c <= '9'; |
| 23 | }; | |
| 24 | 1 | exports.isDigit16 = function(c) { |
| 25 | 0 | return exports.isDigit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'); |
| 26 | }; | |
| 27 | 1 | exports.isDigit2 = function(c) { |
| 28 | 0 | return c == '0' || c == '1'; |
| 29 | }; | |
| 30 | 1 | exports.isDigit8 = function(c) { |
| 31 | 0 | return c >= '0' && c <= '7'; |
| 32 | }; | |
| 33 | 1 | exports.isLetter = function(c) { |
| 34 | 691 | return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); |
| 35 | }; | |
| 36 | 1 | exports.count = function(s, c) { |
| 37 | 456858 | var count = 0, |
| 38 | i = -1; | |
| 39 | 456858 | while((i = s.indexOf(c, i + 1)) != -1) { |
| 40 | 33562 | count++; |
| 41 | } | |
| 42 | 456858 | return count; |
| 43 | }; | |
| 44 | 1 | exports.isUndefined = function(s) { |
| 45 | 4385992 | return typeof s == 'undefined'; |
| 46 | }; | |
| 47 | 1 | exports.isString = function(s) { |
| 48 | 0 | return Object.prototype.toString.call(s) == "[object String]"; |
| 49 | }; | |
| 50 | 1 | exports.isNumber = function(s) { |
| 51 | 456191 | return Object.prototype.toString.call(s) == "[object Number]"; |
| 52 | }; |