Coverage

97%
1310
1282
28

/Users/army/Sites/homunculus/src/lexer/Lexer.js

100%
128
128
0
LineHitsSource
11var Class = require('../util/Class');
21var character = require('../util/character');
31var Token = require('./Token');
41var Lexer = Class(function(rule) {
5258 this.rule = rule; //当前语法规则
6258 this.init();
7}).methods({
8 init: function() {
9259 this.code = ''; //要解析的代码
10259 this.peek = ''; //向前看字符
11259 this.index = 0; //向前看字符字符索引
12259 this.isReg = Lexer.IS_REG; //当前/是否是perl风格正则表达式
13259 this.tokenList = []; //结果的token列表
14259 this.parentheseState = false; //(开始时标记之前终结符是否为if/for/while等关键字
15259 this.parentheseStack = []; //圆括号深度记录当前是否为if/for/while等语句内部
16259 this.cacheLine = 0; //行缓存值
17259 this.totalLine = 1; //总行数
18259 this.colNum = 0; //列
19259 this.colMax = 0; //最大列数
20 },
21 parse: function(code) {
22215 this.code = code || '';
23215 var temp = [];
24215 this.scan(temp);
25206 return temp;
26 },
27 parseOn: function() {
281 var temp = [];
291 this.scan(temp);
301 return temp;
31 },
32 tokens: function() {
33180 return this.tokenList;
34 },
35 scan: function(temp) {
36216 var perlReg = this.rule.perlReg();
37216 var length = this.code.length;
38216 var count = 0;
39 outer:
40 while(this.index < length) {
41454611 if(this.cacheLine > 0 && count >= this.cacheLine) {
421 break;
43 }
44454610 this.readch();
45 //perl风格正则
46454610 if(perlReg && this.isReg == Lexer.IS_REG && this.peek == character.SLASH && !{ '/': true, '*': true }[this.code.charAt(this.index)]) {
47493 this.dealReg(temp, length);
48489 this.isReg = Lexer.NOT_REG;
49 }
50 //依次遍历匹配规则,命中则继续
51 else {
52454117 for(var i = 0, matches = this.rule.matches(), len = matches.length; i < len; i++) {
537446499 var match = matches[i];
547446499 if(match.match(this.peek, this.code, this.index)) {
55454116 var token = new Token(match.tokenType(), match.content(), match.val(), this.index - 1);
56454116 var error = match.error();
57454116 var matchLen = match.content().length;
58454116 if(token.type() == Token.ID && this.rule.keyWords().hasOwnProperty(token.content())) {
5921489 token.type(Token.KEYWORD);
60 }
61454116 temp.push(token);
62454116 this.tokenList.push(token);
63454116 this.index += matchLen - 1;
64454116 var n = character.count(token.val(), character.LINE);
65454116 count += n;
66454116 this.totalLine += n;
67454116 if(n) {
6832320 var j = match.content().indexOf(character.LINE);
6932320 var k = match.content().lastIndexOf(character.LINE);
7032320 this.colMax = Math.max(this.colMax, this.colNum + j);
7132320 this.colNum = match.content().length - k;
72 }
73 else {
74421796 this.colNum += matchLen;
75 }
76454116 this.colMax = Math.max(this.colMax, this.colNum);
77454116 if(error) {
785 this.error(error, this.code.slice(this.index - matchLen, this.index));
79 }
80 //支持perl正则需判断关键字、圆括号对除号语义的影响
81454112 if(perlReg && match.perlReg() != Lexer.IGNORE) {
82258338 if(match.perlReg() == Lexer.SPECIAL) {
8396063 this.isReg = match.special();
84 }
85 else {
86162275 this.isReg = match.perlReg();
87 }
88258338 if(this.peek == character.LEFT_PARENTHESE) {
8919887 this.parentheseStack.push(this.parentheseState);
9019887 this.parentheseState = false;
91 }
92238451 else if(this.peek == character.RIGHT_PARENTHESE) {
9319870 this.isReg = this.parentheseStack.pop() ? Lexer.IS_REG : Lexer.NOT_REG;
94 }
95 else {
96218581 this.parentheseState = match.parenthese();
97 }
98 }
99454112 continue outer;
100 }
101 }
102 //如果有未匹配的,说明规则不完整,加入other类型并抛出警告
1031 this.error('unknow token');
104 }
105 }
106207 return this;
107 },
108 readch: function() {
109463472 this.peek = this.code.charAt(this.index++);
110 },
111 dealReg: function(temp, length) {
112493 var lastIndex = this.index - 1;
113493 var res = false;
114 outer:
115 do {
1166803 this.readch();
1176803 if(this.peek == character.LINE) {
1182 this.error('SyntaxError: unterminated regular expression literal ' + this.peek, this.code.slice(lastIndex, this.index));
1191 break;
120 }
1216801 else if(this.peek == character.BACK_SLASH) {
122490 this.index++;
123 }
1246311 else if(this.peek == character.LEFT_BRACKET) {
125264 do {
1261368 this.readch();
1271368 if(this.peek == character.LINE) {
1282 this.error('SyntaxError: unterminated regular expression literal ' + this.peek, this.code.slice(lastIndex, this.index));
1291 break outer;
130 }
1311366 else if(this.peek == character.BACK_SLASH) {
132265 this.index++;
133 }
1341101 else if(this.peek == character.RIGHT_BRACKET) {
135262 continue outer;
136 }
137 } while(this.index < length);
138 }
1396047 else if(this.peek == character.SLASH) {
140488 res = true;
141488 var hash = {};
142488 var flag = {
143 'g': true,
144 'i': true,
145 'm': true,
146 'y': true
147 };
148 //正则的flag中有gimy4种,大小写敏感且不能重复
149488 do {
150691 this.readch();
151691 if(character.isLetter(this.peek)) {
152205 if(hash.hasOwnProperty(this.peek) || !flag.hasOwnProperty(this.peek)) {
1532 this.error('SyntaxError: invalid regular expression flag ' + this.peek, this.code.slice(lastIndex, this.index));
1541 break outer;
155 }
156203 hash[this.peek] = true;
157 }
158 else {
159486 break outer;
160 }
161 } while(this.index <= length);
162 }
163 } while(this.index < length);
164490 if(!res) {
1653 this.error('SyntaxError: unterminated regular expression literal', this.code.slice(lastIndex, this.index - 1));
166 }
167489 var token = new Token(Token.REG, this.code.slice(lastIndex, --this.index), lastIndex);
168489 temp.push(token);
169489 this.tokenList.push(token);
170489 this.colNum += this.index - lastIndex;
171489 this.colMax = Math.max(this.colMax, this.colNum);
172489 return this;
173 },
174 cache: function(i) {
1751 if(!character.isUndefined(i) && i !== null) {
1761 this.cacheLine = i;
177 }
1781 return this.cacheLine;
179 },
180 finish: function() {
1812 return this.index >= this.code.length;
182 },
183 line: function() {
18416 return this.totalLine;
185 },
186 col: function() {
1871 return this.colMax;
188 },
189 error: function(s, str) {
19015 if(character.isUndefined(str)) {
1911 str = this.code.substr(this.index - 1, 20);
192 }
19315 if(Lexer.mode() === Lexer.STRICT) {
1949 throw new Error(s + ', line ' + this.line() + ' col ' + this.colNum + '\n' + str);
195 }
1966 else if(Lexer.mode() === Lexer.LOOSE && !character.isUndefined(console)) {
1976 if(console.warn) {
1986 console.warn(s + ', line ' + this.line() + ' col ' + this.colNum + '\n' + str);
199 }
200 }
2016 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) {
21133 if(!character.isUndefined(i)) {
21210 cmode = i;
213 }
21433 return cmode;
215 }
216});
2171var cmode = Lexer.STRICT;
2181module.exports = Lexer;

/Users/army/Sites/homunculus/src/lexer/Token.js

100%
41
41
0
LineHitsSource
11var Class = require('../util/Class');
21var character = require('../util/character');
31var tid = 0;
41var types;
51var Token = Class(function(type, content, val, sIndex) {
6456191 this.t = type; //token类型
7456191 this.c = content; //token的字面内容,string包括头尾的引号
8456191 if(character.isNumber(val)) {
9494 sIndex = val;
10494 val = content;
11 }
12455697 else if(character.isUndefined(val)) {
131581 val = content;
141581 sIndex = -1;
15 }
16456191 this.v = val; //token的值,一般情况下等于content,特殊如string情况下值是不加头尾的引号
17456191 this.id = tid++; //token的索引
18456191 this.si = sIndex; //token在源码字符串中的索引
19}).methods({
20 type: function(t) {
211636563 if(!character.isUndefined(t)) {
2221489 this.t = t;
23 }
241636563 return this.t;
25 },
26 content: function(c) {
271667701 if(!character.isUndefined(c)) {
281 this.c = c;
29 }
301667701 return this.c;
31 },
32 val: function(v) {
33454118 if(!character.isUndefined(v)) {
341 this.v = v;
35 }
36454118 return this.v;
37 },
38 tag: function(t) {
392 if(!character.isUndefined(t)) {
401 this.t = t;
41 }
422 return Token.type(this.t);
43 },
44 tid: function(id) {
452 if(!character.isUndefined(id)) {
461 this.id = id;
47 }
482 return this.id;
49 },
50 sIndex: function(si) {
516 if(!character.isUndefined(si)) {
521 this.si = si;
53 }
546 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) {
796 if(character.isUndefined(types)) {
801 types = [];
811 Object.keys(Token).forEach(function(o) {
8225 if(typeof Token[o] == 'number') {
8321 types[Token[o]] = o;
84 }
85 });
86 }
876 return types[tag];
88 }
89});
901module.exports = Token;

/Users/army/Sites/homunculus/src/lexer/match/CharacterSet.js

100%
10
10
0
LineHitsSource
11var Match = require('./Match');
21var character = require('../../util/character');
31var CharacterSet = Match.extend(function(type, str, setPReg) {
4512 Match.call(this, type, setPReg);
5512 this.str = str;
6}).methods({
7 match: function(c, code, index) {
8141011 var isIn = this.str.indexOf(c) > -1;
9141011 if(isIn) {
10133495 this.result = c;
11 }
12141011 return isIn;
13 }
14});
151module.exports = CharacterSet;

/Users/army/Sites/homunculus/src/lexer/match/CompleteEqual.js

100%
7
7
0
LineHitsSource
11var Match = require('./Match');
21var character = require('../../util/character');
31var CompleteEqual = Match.extend(function(type, result, setPReg) {
48576 Match.call(this, type, setPReg);
58576 this.result = result;
6}).methods({
7 match: function(c, code, index) {
85598603 return code.substr(--index, this.result.length) == this.result;
9 }
10});
111module.exports = CompleteEqual;

/Users/army/Sites/homunculus/src/lexer/match/LineParse.js

96%
32
31
1
LineHitsSource
11var Match = require('./Match');
21var Token = require('../Token');
31var character = require('../../util/character');
41var LineParse = Match.extend(function(type, begin, end, mutiline, setPReg) {
5772 if(character.isUndefined(mutiline)) {
60 mutiline = false;
7 }
8772 Match.call(this, type, setPReg);
9772 this.begin = begin;
10772 this.end = end;
11772 this.msg = null;
12772 this.mutiline = mutiline;
13 }).methods({
14 match: function(c, code, index) {
15762999 this.msg = null;
16762999 if(this.begin == code.charAt(index - 1)) {
176827 var len = code.length,
18 lastIndex = index - 1,
19 res = false;
206827 while(index < len) {
2157358 var c = code.charAt(index++);
22 //转义
2357358 if(c == character.BACK_SLASH) {
24402 if(code.charAt(index++) == character.ENTER) {
251 index++;
26 }
27 }
2856956 else if(c == character.LINE && !this.mutiline) {
291 break;
30 }
3156955 else if(c == this.end) {
326825 res = true;
336825 break;
34 }
35 }
366827 if(!res) {
372 this.msg = 'SyntaxError: unterminated ' + Token.type(this.type).toLowerCase() + ' literal';
38 }
396827 this.result = code.slice(lastIndex, index);
406827 return true;
41 }
42756172 return false;
43 },
44 error: function() {
456827 return this.msg;
46 },
47 val: function() {
486827 return this.content().slice(this.begin.length, -this.end.length);
49 }
50 });
511module.exports = LineParse;

/Users/army/Sites/homunculus/src/lexer/match/LineSearch.js

100%
25
25
0
LineHitsSource
11var Match = require('./Match');
21var Token = require('../Token');
31var character = require('../../util/character');
41var LineSearch = Match.extend(function(type, begin, end, contain, setPReg) {
5516 if(character.isUndefined(contain)) {
6258 contain = false;
7 }
8516 Match.call(this, type, setPReg);
9516 this.begin = begin;
10516 this.end = end;
11516 this.contain = contain;
12516 this.msg = null;
13}).methods({
14 match: function(c, code, index) {
15521900 this.msg = null;
16521900 if(this.begin == code.substr(--index, this.begin.length)) {
175001 var i = code.indexOf(this.end, index + this.begin.length);
185001 if(i == -1) {
192 if(this.contain) {
201 this.msg = 'SyntaxError: unterminated ' + Token.type(this.type).toLowerCase();
21 }
222 i = code.length;
23 }
244999 else if(this.contain) {
25214 i += this.end.length;
26 }
275001 this.result = code.slice(index, i);
285001 return true;
29 }
30516899 return false;
31 },
32 error: function() {
335001 return this.msg;
34 }
35});
361module.exports = LineSearch;

/Users/army/Sites/homunculus/src/lexer/match/Match.js

91%
24
22
2
LineHitsSource
11var Class = require('../../util/Class');
21var character = require('../../util/character');
31var Lexer = require('../Lexer');
41module.exports = Class(function(type, setPReg, special, parenthese) {
511428 this.type = type;
611428 if(character.isUndefined(setPReg)) {
71956 setPReg = Lexer.IGNORE;
8 }
911428 this.setPReg = setPReg;
1011428 this.result = null;
11 //忽略0,是1,否2,特殊3
1211428 if(setPReg) {
139472 if(character.isUndefined(special)) {
149216 special = function() {
150 return Lexer.IGNORE;
16 };
17 }
189472 if(character.isUndefined(parenthese)) {
199216 parenthese = function() {
20122518 return false;
21 };
22 }
23 }
2411428 this.special = special;
2511428 this.parenthese = parenthese;
26}).methods({
27 tokenType: function() {
28454116 return this.type;
29 },
30 perlReg: function() {
31874725 return this.setPReg;
32 },
33 val: function() {
34447289 return this.content();
35 },
36 content: function() {
371651434 return this.result;
38 },
39 match: function(c, code, index) {
40 //需被实现
410 throw new Error('match needs to be implement');
42 },
43 error: function() {
44338703 return false;
45 }
46});

/Users/army/Sites/homunculus/src/lexer/match/RegMatch.js

100%
23
23
0
LineHitsSource
11var Match = require('./Match');
21var RegMatch = Match.extend(function(type, reg, valid, setPReg, special, parenthese) {
31052 if(typeof valid == 'number') {
4256 parenthese = special;
5256 special = setPReg;
6256 setPReg = valid;
7256 valid = null;
8 }
91052 Match.call(this, type, setPReg, special, parenthese);
101052 this.reg = reg;
111052 this.valid = valid;
12 }).methods({
13 match: function(c, code, index) {
14421986 var self = this,
15 res = self.reg.exec(code.slice(index - 1));
16421986 self.msg = null;
17421986 if(res) {
18103585 self.result = res[0];
19103585 if(self.valid) {
207522 for(var i = 0, keys = Object.keys(self.valid), len = keys.length; i < len; i++) {
217522 if(self.valid[keys[i]].test(self.result)) {
222 self.msg = keys[i];
232 break;
24 }
25 }
26 }
27103585 return true;
28 }
29318401 return false;
30 },
31 error: function() {
32103585 return this.msg;
33 }
34 });
351module.exports = RegMatch;

/Users/army/Sites/homunculus/src/lexer/rule/EcmascriptRule.js

100%
33
33
0
LineHitsSource
11var Rule = require('./Rule');
21var LineSearch = require('../match/LineSearch');
31var LineParse = require('../match/LineParse');
41var CompleteEqual = require('../match/CompleteEqual');
51var RegMatch = require('../match/RegMatch');
61var CharacterSet = require('../match/CharacterSet');
71var Token = require('../Token');
81var Lexer = require('../Lexer');
91var character = require('../../util/character');
101var EcmascriptRule = Rule.extend(function() {
11256 var self = this;
12256 Rule.call(self, EcmascriptRule.KEYWORDS, true);
13
14256 self.addMatch(new CompleteEqual(Token.BLANK, character.BLANK));
15256 self.addMatch(new CompleteEqual(Token.TAB, character.TAB));
16256 self.addMatch(new CompleteEqual(Token.ENTER, character.ENTER));
17256 self.addMatch(new CompleteEqual(Token.LINE, character.LINE));
18
19256 self.addMatch(new LineSearch(Token.COMMENT, '//', '\n'));
20256 self.addMatch(new LineSearch(Token.COMMENT, '/*', '*/', true));
21256 self.addMatch(new LineParse(Token.STRING, '"', '"', false, Lexer.IS_REG));
22256 self.addMatch(new LineParse(Token.STRING, "'", "'", false, Lexer.IS_REG));
23256 self.addMatch(new LineParse(Token.TEMPLATE, '`', '`', true, Lexer.IS_REG));
24
25256 self.addMatch(new RegMatch(Token.ID, /^[$a-zA-Z_][$\w]*/, Lexer.SPECIAL, function() {
2696063 return !!(self.keyWords().hasOwnProperty(this.content()));
27 }, function() {
2896063 return ['if', 'for', 'while'].indexOf(this.content()) != -1;
29 }));
30
31256 self.addMatch(new RegMatch(Token.NUMBER, /^\.\d+(?:E[+-]?\d*)?/i, {
32 'SyntaxError: missing exponent': /E[+-]?$/i
33 }, Lexer.NOT_REG));
34
35256 self.addMatch(new CompleteEqual(Token.SIGN, ']', Lexer.NOT_REG));
36
37256 ['*=', '/=', '+=', '-=', '%=', '^=', '&=', '|=', '&&', '--', '++', '===', '==', '!==', '!=', '||', '>>>=', '<<<=', '<<<', '>>>', '>>=', '<<=', '<<', '>>', '>=', '<=', '...', '?:'].forEach(function(o) {
387168 self.addMatch(new CompleteEqual(Token.SIGN, o, Lexer.IS_REG));
39 });
40256 self.addMatch(new CharacterSet(Token.SIGN, ':;/?.,[]{}~!^|%=-+*()~><&\\', Lexer.IS_REG));
41
42256 self.addMatch(new RegMatch(Token.NUMBER, /^0x[\da-f]*/i, {
43 "SyntaxError: missing hexadecimal digits after '0x'": /^0x$/i
44 }, Lexer.NOT_REG));
45256 self.addMatch(new RegMatch(Token.NUMBER, /^\d+\.?\d*(?:E[+-]?\d*)?/i, {
46 'SyntaxError: missing exponent': /E[+-]?$/i
47 }, Lexer.NOT_REG));
48
49256 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});
531module.exports = EcmascriptRule;

/Users/army/Sites/homunculus/src/lexer/rule/Rule.js

100%
14
14
0
LineHitsSource
11var Class = require('../../util/Class');
21var Rule = Class(function(words, pReg) {
3258 var self = this;
4258 self.kw = {};
5258 words.forEach(function(o) {
615474 self.kw[o] = true;
7 });
8258 self.pReg = pReg || false;
9258 self.matchList = [];
10 }).methods({
11 perlReg: function() {
12216 return this.pReg;
13 },
14 addMatch: function(match) {
1511428 this.matchList.push(match);
1611428 return this;
17 },
18 matches: function() {
19454117 return this.matchList;
20 },
21 keyWords: function() {
22192126 return this.kw;
23 }
24 });
251module.exports = Rule;

/Users/army/Sites/homunculus/src/parser/js/Context.js

100%
202
202
0
LineHitsSource
11var Class = require('../../util/Class');
21var JsNode = require('./Node');
31var Token = require('../../lexer/Token');
41var Parser = require('./Parser');
51var id = 0;
61var Context = Class(function(parent, name) {
765 this.id = id++;
865 this.parser = new Parser();
965 this.parent = parent || null; //父上下文,如果是全局则为空
1065 this.name = name || null; //上下文名称,即函数名,函数表达式为空,全局也为空
1165 this.children = []; //函数声明或函数表达式所产生的上下文
1265 this.childrenMap = Object.create(null); //键是函数名,值是上下文,匿名函数表达式键为cid
1365 this.vars = []; //变量var声明
1465 this.varsMap = Object.create(null); //键为id字面量,值是它的token的节点
1565 this.vardeclMap = Object.create(null); //var赋值记录,优先级vardecl > fndecl > varnodecl
1665 this.params = []; //形参,函数上下文才有,即全局无
1765 this.paramsMap = Object.create(null); //键为id字面量,值是它的token的节点
1865 this.aParams = []; //实参,函数表达式才有
1965 this.vids = []; //上下文环境里用到的变量id
2065 this.vidsMap = Object.create(null); //键为id字面量,值是它的token的节点
2165 this.returns = []; //上下文环境里return语句
2265 this.node = null; //对应的ast的节点
2365 this.thisIs = null; //this指向,仅函数表达式call或apply执行时有用
2465 if(!this.isTop()) {
2526 this.parent.addChild(this);
26 }
27}).methods({
28 parse: function(code) {
2935 var ast;
3035 if(code instanceof JsNode) {
311 ast = code;
32 }
33 else {
3434 ast = this.parser.parse(code);
35 }
3635 recursion(ast, this);
3735 return this;
38 },
39 getId: function() {
4029 return this.id;
41 },
42 getName: function() {
4327 return this.name;
44 },
45 getParent: function() {
462 return this.parent;
47 },
48 isTop: function() {
4966 return !this.parent;
50 },
51 isFnexpr: function() {
521 return !this.isTop() && !this.name;
53 },
54 hasParam: function(p) {
556 return p in this.paramsMap;
56 },
57 getParams: function() {
581 return this.params;
59 },
60 addParam: function(p) {
61 //形参不可能重复,无需判断
6210 this.paramsMap[p] = this.params.length;
6310 this.params.push(p);
6410 return this;
65 },
66 getAParams: function() {
674 return this.aParams;
68 },
69 addAParam: function(ap) {
709 this.aParams.push(ap);
719 return this;
72 },
73 getChild: function(name) {
747 return this.childrenMap[name];
75 },
76 getChildren: function() {
7715 return this.children;
78 },
79 //通过name查找函数声明,id查找表达式
80 hasChild: function(name) {
8148 return name in this.childrenMap;
82 },
83 addChild: function(child) {
8426 var name = child.getName();
85 //函数表达式名字为空用id删除
8626 if(name) {
8712 if(name in this.vardeclMap) {
881 return this;
89 }
9011 this.delVar(name);
9111 this.delChild(name);
92 }
93 else {
9414 this.delChild(child.getId());
95 }
9625 name = name || child.getId();
9725 this.childrenMap[name] = child;
9825 this.children.push(child);
9925 return this;
100 },
101 //name函数声明,id表达式
102 delChild: function(name) {
10338 if(this.hasChild(name)) {
1041 var i = this.children.indexOf(this.childrenMap[name]);
1051 this.children.splice(i, 1);
1061 delete this.childrenMap[name];
107 }
10838 return this;
109 },
110 hasVar: function(v) {
11138 return v in this.varsMap;
112 },
113 addVar: function(node, assign) {
11417 var v = node.leaves()[0].token().content();
115 //赋值拥有最高优先级,会覆盖掉之前的函数声明和var
11617 if(assign) {
11713 this.delVar(v);
11813 this.delChild(v);
11913 this.vardeclMap[v] = true;
120 }
121 //仅仅是var声明无赋值,且已有过声明或函数,忽略之
1224 else if(this.hasVar(v) || this.hasChild(v)) {
1232 return this;
124 }
12515 this.varsMap[v] = node;
12615 this.vars.push(node);
12715 return this;
128 },
129 delVar: function(v) {
13024 if(this.hasVar(v)) {
1312 var i = this.vars.indexOf(this.varsMap[v]);
1322 this.vars.splice(i, 1);
1332 delete this.varsMap[v];
134 }
13524 return this;
136 },
137 getVars: function() {
1384 return this.vars;
139 },
140 addReturn: function(node) {
1415 this.returns.push(node);
1425 return this;
143 },
144 getReturns: function() {
1454 return this.returns;
146 },
147 hasVid: function(v) {
1488 return v in this.vidsMap;
149 },
150 getVid: function(v) {
1511 return this.vidsMap[v];
152 },
153 addVid: function(node) {
15412 var v = node.token().content();
15512 this.vids.push(node);
15612 this.vidsMap[v] = this.vidsMap[v] || [];
15712 this.vidsMap[v].push(node);
15812 return this;
159 },
160 getVids: function() {
1612 return this.vids;
162 },
163 getNode: function() {
1641 return this.node;
165 },
166 setNode: function(n) {
16726 this.node = n;
16826 return this;
169 },
170 setThis: function(t) {
1714 this.thisIs = t;
1724 return this;
173 },
174 getThis: function() {
1753 return this.thisIs;
176 }
177 });
178
1791function recursion(node, context) {
180668 var isToken = node.name() == JsNode.TOKEN;
181668 var isVirtual = isToken && node.token().type() == Token.VIRTUAL;
182668 if(isToken) {
183385 if(!isVirtual) {
184379 var token = node.token();
185379 var s = token.content();
186379 if(s == 'return') {
1875 context.addReturn(node);
188 }
189 }
190 }
191 else {
192283 if(node.name() == JsNode.VARDECL) {
19317 vardecl(node, context);
194 }
195266 else if(node.name() == JsNode.FNDECL) {
19612 context = fndecl(node, context);
197 }
198254 else if(node.name() == JsNode.FNEXPR) {
19914 context = fnexpr(node, context);
200 }
201240 else if(node.name() == JsNode.PRMREXPR) {
20252 prmrexpr(node, context);
203 }
204283 node.leaves().forEach(function(leaf, i) {
205633 recursion(leaf, context);
206 });
207 }
208}
2091function vardecl(node, context) {
21017 var leaves = node.leaves();
21117 var assign = !!leaves[1];
21217 context.addVar(node, assign);
213}
2141function fndecl(node, context) {
21512 var v = node.leaves()[1].leaves().content();
21612 var child = new Context(context, v);
21712 child.setNode(node);
21812 var params = node.leaves()[3];
21912 if(params.name() == JsNode.FNPARAMS) {
2201 addParam(params, child);
221 }
22212 return child;
223}
2241function fnexpr(node, context) {
225 //函数表达式name为空
22614 var child = new Context(context);
22714 child.setNode(node);
228 //记录形参
22914 var params;
23014 var v = node.leaves()[1];
23114 if(v.token().content() != '(') {
2326 params = node.leaves()[3];
233 }
234 else {
2358 params = node.leaves()[2];
236 }
23714 if(params.name() == JsNode.FNPARAMS) {
2387 addParam(params, child);
239 }
240 //匿名函数检查实参传入情况,包括call和apply设置this
24114 var next = node.next();
242 //!function(){}()形式
24314 if(next && next.name() == JsNode.ARGS) {
2447 var leaves = next.leaves();
245 //长度2为()空参数,长度3有参数,第2个节点
2467 if(leaves.length == 3) {
2471 addAParam(leaves[1], child);
248 }
249 }
250 //call或applay
2517 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) {
2572 var mmb = node.parent();
2582 if(mmb.name() == JsNode.MMBEXPR) {
2592 var callexpr = mmb.parent();
2602 if(callexpr.name() == JsNode.CALLEXPR) {
2612 var isApply = next.next().token().content() == 'apply';
2622 next = mmb.next();
2632 if(next && next.name() == JsNode.ARGS) {
2642 var leaves = next.leaves();
265 //长度2为()空参数,长度3有参数,第2个节点
2662 if(leaves.length == 3) {
2672 isApply ? addApplyAParam(leaves[1], child) : addCallAParam(leaves[1], child);
268 }
269 }
270 }
271 }
272 }
273 //(function(){})()形式
274 else {
2755 var prmr = node.parent();
2765 var prev = node.prev();
2775 if(prmr.name() == JsNode.PRMREXPR
278 && prev
279 && prev.name() == JsNode.TOKEN
280 && prev.token().content() == '(') {
2815 next = prmr.next();
2825 if(next && next.name() == JsNode.ARGS) {
2833 var leaves = next.leaves();
284 //长度2为()空参数,长度3有参数,第2个节点
2853 if(leaves.length == 3) {
2861 addAParam(leaves[1], child);
287 }
288 }
289 //(function(){}).call()形式
2902 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) {
2962 var mmb = prmr.parent();
2972 if(mmb.name() == JsNode.MMBEXPR) {
2982 var callexpr = mmb.parent();
2992 if(callexpr.name() == JsNode.CALLEXPR) {
3002 var isApply = next.next().token().content() == 'apply';
3012 next = mmb.next();
3022 if(next && next.name() == JsNode.ARGS) {
3032 var leaves = next.leaves();
304 //长度2为()空参数,长度3有参数,第2个节点
3052 if(leaves.length == 3) {
3062 isApply ? addApplyAParam(leaves[1], child) : addCallAParam(leaves[1], child);
307 }
308 }
309 }
310 }
311 }
312 }
313 }
31414 return child;
315}
316//支持es6
3171function addParam(params, child) {
3188 params.leaves().forEach(function(leaf, i) {
31913 if(leaf.name() == JsNode.TOKEN && leaf.token().content() != ',') {
3209 child.addParam(leaf.token().content());
321 }
3224 else if(leaf.name() == JsNode.RESTPARAM) {
3231 child.addParam(leaf.leaves()[1].token().content());
324 }
325 });
326}
3271function addAParam(params, child) {
3282 params.leaves().forEach(function(leaf, i) {
3296 if(i % 2 == 0) {
3304 child.addAParam(leaf);
331 }
332 });
333}
3341function addCallAParam(params, child) {
3352 params.leaves().forEach(function(leaf, i) {
3366 if(i == 0) {
3372 child.setThis(leaf);
338 }
3394 else if(i % 2 == 1) {
3402 child.addAParam(leaf);
341 }
342 });
343}
3441function addApplyAParam(params, child) {
3452 child.setThis(params.leaves()[0]);
3462 params.leaves()[2].leaves()[0].leaves().forEach(function(leaf, i) {
3478 if(i % 2 == 1) {
3483 child.addAParam(leaf);
349 }
350 });
351}
3521function prmrexpr(node, context) {
35352 var first = node.leaves()[0];
35452 if(first.name() == JsNode.TOKEN) {
35548 var token = first.token();
35648 if(token.type() == Token.ID) {
35712 context.addVid(first);
358 }
359 }
360}
361
3621module.exports = Context;

/Users/army/Sites/homunculus/src/parser/js/Node.js

97%
45
44
1
LineHitsSource
11var Class = require('../../util/Class');
21var Node = Class(function(type, children) {
3770136 this.type = type;
4770136 if(type == Node.TOKEN) {
5140139 this.children = children;
6 }
7629997 else if(Array.isArray(children)) {
80 this.children = children;
9 }
10 else {
11629997 this.children = children ? [children] : [];
12 }
13770136 this.p = null;
14770136 this.pr = null;
15770136 this.ne = null;
16770136 return this;
17}).methods({
18 name: function() {
19240479 return this.type;
20 },
21 leaves: function() {
2298888 return this.children;
23 },
24 add: function() {
25166758 var self = this;
26166758 var args = Array.prototype.slice.call(arguments, 0);
27166758 args.forEach(function(node) {
28238672 node.parent(self);
29238672 var last = self.children[self.children.length - 1];
30238672 if(last) {
31139985 last.next(node);
32139985 node.prev(last);
33 }
34238672 self.children.push(node);
35 });
36166758 return self;
37 },
38 token: function() {
39278609 return this.children;
40 },
41 parent: function(p) {
42238688 if(p) {
43238672 this.p = p;
44 }
45238688 return this.p;
46 },
47 prev: function(pr) {
48139992 if(pr) {
49139985 this.pr = pr;
50 }
51139992 return this.pr;
52 },
53 next: function(ne) {
54140025 if(ne) {
55139985 this.ne = ne;
56 }
57140025 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) {
1322 if(!s) {
1331 throw new Error('empty value');
134 }
1351 if(!keys) {
1361 var self = this;
1371 keys = {};
1381 Object.keys(this).forEach(function(k) {
13974 var v = self[k];
14074 keys[v] = k;
141 });
142 }
1431 return keys[s];
144 }
145});
1461var keys;
1471module.exports = Node;

/Users/army/Sites/homunculus/src/parser/js/Parser.js

97%
661
642
19
LineHitsSource
11var Class = require('../../util/Class');
21var character = require('../../util/character');
31var Lexer = require('../../lexer/Lexer');
41var Rule = require('../../lexer/rule/EcmascriptRule');
51var Token = require('../../lexer/Token');
61var Node = require('./Node');
71var S = {};
81S[Token.BLANK] = S[Token.TAB] = S[Token.COMMENT] = S[Token.LINE] = S[Token.ENTER] = true;
91var Parser = Class(function(lexer) {
10215 this.init(lexer);
11}).methods({
12 parse: function(code) {
13180 this.lexer.parse(code);
14180 this.tree = this.program();
15151 return this.tree;
16 },
17 ast: function() {
184 return this.tree;
19 },
20 init: function(lexer) {
21216 this.look = null;
22216 this.tokens = null;
23216 this.lastLine = 1;
24216 this.lastCol = 1;
25216 this.line = 1;
26216 this.col = 1;
27216 this.index = 0;
28216 this.length = 0;
29216 this.ignores = {};
30216 this.hasMoveLine = false;
31216 this.tree = {};
32216 if(lexer) {
33150 this.lexer = lexer;
34 }
3566 else if(this.lexer) {
361 this.lexer.init();
37 }
38 else {
3965 this.lexer = new Lexer(new Rule());
40 }
41 },
42 program: function() {
43180 this.tokens = this.lexer.tokens();
44180 this.length = this.tokens.length;
45180 if(this.tokens.length) {
46178 this.move();
47 }
48180 var node = new Node(Node.PROGRAM);
49180 while(this.look) {
50212 node.add(this.element());
51 }
52151 return node;
53 },
54 element: function(allowSuper) {
555380 if(this.look.content() == 'function') {
56199 return this.fndecl();
57 }
585181 else if(this.look.content() == 'class') {
5913 return this.classdecl();
60 }
61 else {
625168 return this.stmt(allowSuper);
63 }
64 },
65 stmt: function(allowSuper) {
6610854 if(!this.look) {
671 this.error();
68 }
6910853 switch(this.look.content()) {
70 case 'let':
712 return this.letstmt();
72 case 'const':
732 return this.cststmt();
74 case 'var':
751160 return this.varstmt();
76 case '{':
771791 return this.block();
78 case ';':
7935 return this.emptstmt();
80 case 'if':
811692 return this.ifstmt();
82 case 'do':
83 case 'while':
84 case 'for':
85458 return this.iterstmt();
86 case 'continue':
8718 return this.cntnstmt();
88 case 'break':
89123 return this.brkstmt();
90 case 'return':
911636 return this.retstmt();
92 case 'with':
931 return this.withstmt();
94 case 'switch':
958 return this.swchstmt();
96 case 'throw':
9739 return this.thrstmt();
98 case 'try':
9955 return this.trystmt();
100 case 'debugger':
1011 return this.debstmt();
102 case 'super':
1033 if(!allowSuper) {
1041 this.error('super must in a class');
105 }
1062 return this.superstmt();
107 case 'import':
1080 return this.imptstmt();
109 default:
1103829 if(this.look.type() == Token.ID) {
1112943 for(var i = this.index; i < this.length; i++) {
1123955 var token = this.tokens[i];
1133955 if(!S[token.type()]) {
1142940 if(token.content() == ':') {
1152 return this.labstmt();
116 }
117 else {
1182938 return this.exprstmt();
119 }
120 }
121 }
122 }
123889 return this.exprstmt();
124 }
125 },
126 exprstmt: function() {
1273827 var node = new Node(Node.EXPRSTMT);
1283827 node.add(this.expr(), this.match(';'));
1293818 return node;
130 },
131 cststmt: function(noSem) {
1322 var node = new Node(Node.CSTSTMT);
1332 node.add(
134 this.match('const'),
135 this.vardecl()
136 );
1372 while(this.look && this.look.content() == ',') {
1381 node.add(
139 this.match(),
140 this.vardecl()
141 );
142 }
1432 if(!noSem) {
1442 node.add(this.match(';'));
145 }
1462 return node;
147 },
148 letstmt: function(noSem) {
1492 var node = new Node(Node.LETSTMT);
1502 node.add(
151 this.match('let'),
152 this.vardecl()
153 );
1542 while(this.look && this.look.content() == ',') {
1551 node.add(
156 this.match(),
157 this.vardecl()
158 );
159 }
1602 if(!noSem) {
1612 node.add(this.match(';'));
162 }
1632 return node;
164 },
165 varstmt: function(noSem) {
1661244 var node = new Node(Node.VARSTMT);
1671244 node.add(
168 this.match('var'),
169 this.vardecl()
170 );
1711242 while(this.look && this.look.content() == ',') {
1721555 node.add(
173 this.match(),
174 this.vardecl()
175 );
176 }
1771242 if(!noSem) {
1781158 node.add(this.match(';'));
179 }
1801242 return node;
181 },
182 vardecl: function() {
1832805 var node = new Node(Node.VARDECL);
1842805 if(!this.look) {
1851 this.error('missing variable name');
186 }
1872804 if(this.look.content() == '[') {
1880 node.add(this.arrltr());
189 }
190 else {
1912804 node.add(this.match(Token.ID, 'missing variable name'));
192 }
1932804 if(this.look && this.look.content() == '=') {
1941919 node.add(this.assign());
195 }
1962803 return node;
197 },
198 assign: function() {
1991919 var node = new Node(Node.ASSIGN);
2001919 node.add(this.match('='));
2011919 if(!this.look) {
2021 this.error();
203 }
2041918 node.add(this.assignexpr());
2051918 return node;
206 },
207 block: function() {
2081906 var node = new Node(Node.BLOCK);
2091906 node.add(this.match('{'));
2101906 while(this.look && this.look.content() != '}') {
2112988 node.add(this.stmt());
212 }
2131906 node.add(this.match('}', 'missing } in compound statement'));
2141906 return node;
215 },
216 emptstmt: function() {
21735 var node = new Node(Node.EMPTSTMT);
21835 node.add(this.match(';'));
21935 return node;
220 },
221 ifstmt: function() {
2221692 var node = new Node(Node.IFSTMT);
2231692 node.add(
224 this.match('if'),
225 this.match('('),
226 this.expr(),
227 this.match(')'),
228 this.stmt()
229 );
2301691 if(this.look && this.look.content() == 'else') {
231325 node.add(
232 this.match('else'),
233 this.stmt()
234 );
235 }
2361691 return node;
237 },
238 iterstmt: function() {
239458 var node = new Node(Node.ITERSTMT);
240458 switch(this.look.content()) {
241 case 'do':
2429 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 );
2519 break;
252 case 'while':
253152 node.add(
254 this.match(),
255 this.match('('),
256 this.expr(),
257 this.match(')'),
258 this.stmt()
259 );
260152 break;
261 case 'for':
262297 node.add(
263 this.match(),
264 this.match('(')
265 );
266297 if(!this.look) {
2671 this.error();
268 }
269296 if(this.look.content() == 'var' || this.look.content() == 'let') {
27084 var node2 = this.look.content() == 'var' ? this.varstmt(true) : this.letstmt(true);
27184 if(!this.look) {
2721 this.error('missing ; after for-loop initializer');
273 }
27483 if(this.look.content() == 'in') {
27519 if(node2.leaves().length > 2) {
2761 this.error('invalid for/in left-hand side');
277 }
27818 node.add(node2);
27918 node.add(
280 this.match(),
281 this.expr()
282 );
283 }
284 else {
28564 node.add(node2);
28664 node.add(this.match(';'));
28764 if(this.look.content() != ';') {
28863 node.add(this.expr());
289 }
29063 node.add(this.match(';'));
29163 if(!this.look) {
2921 this.error();
293 }
29462 if(this.look.content() != ')') {
29559 node.add(this.expr());
296 }
297 }
298 }
299 else {
300212 if(this.look.content() == 'in') {
3011 this.error();
302 }
303211 var hasIn = false;
304211 for(var i = this.index; i < this.length; i++) {
3051924 var t = this.tokens[i];
3061924 if(t.content() == 'in') {
30787 hasIn = true;
30887 break;
309 }
3101837 else if(t.content() == ')') {
311120 break;
312 }
313 }
314211 if(hasIn) {
31587 node.add(this.expr(true), this.match('in'), this.expr());
316 }
317 else {
318124 if(this.look.content() != ';') {
31968 node.add(this.expr());
320 }
321 //for的;不能省略,强制判断
322124 if(!this.look || this.look.content() != ';') {
3231 this.error('missing ;')
324 }
325123 node.add(this.match(';'));
326123 if(!this.look) {
3271 this.error();
328 }
329122 if(this.look.content() != ';') {
330121 node.add(this.expr());
331 }
332122 if(!this.look || this.look.content() != ';') {
3331 this.error('missing ;')
334 }
335121 node.add(this.match(';'));
336121 if(!this.look) {
3371 this.error();
338 }
339120 if(this.look.content() != ')') {
340112 node.add(this.expr());
341 }
342 }
343 }
344287 node.add(this.match(')'));
345287 node.add(this.stmt());
346 }
347448 return node;
348 },
349 cntnstmt: function() {
35018 var node = new Node(Node.CNTNSTMT);
35118 node.add(this.match('continue', true));
35218 if(this.look && this.look.type() == Token.ID) {
3531 node.add(this.match());
354 }
35518 node.add(this.match(';'));
35618 return node;
357 },
358 brkstmt: function() {
359123 var node = new Node(Node.BRKSTMT);
360123 node.add(this.match('break', true));
361123 if(this.look && this.look.type() == Token.ID) {
3621 node.add(this.match());
363 }
364123 node.add(this.match(';'));
365123 return node;
366 },
367 retstmt: function() {
3681636 var node = new Node(Node.RETSTMT);
3691636 node.add(this.match('return', true));
370 //return后换行视作省略;,包括多行注释的换行
3711636 if(this.look) {
3721635 if(this.look.content() == ';'
373 || this.look.content() == '}'
374 || this.look.type() == Token.LINE
375 || this.look.type() == Token.COMMENT) {
37682 node.add(this.match(';'));
377 }
378 else {
3791553 node.add(this.expr(), this.match(';'));
380 }
381 }
382 else {
3831 node.add(this.match(';'));
384 }
3851636 return node;
386 },
387 withstmt: function() {
3881 var node = new Node(Node.WITHSTMT);
3891 node.add(
390 this.match('with'),
391 this.match('('),
392 this.expr(),
393 this.match(')'),
394 this.stmt()
395 );
3961 return node;
397 },
398 swchstmt: function() {
3998 var node = new Node(Node.SWCHSTMT);
4008 node.add(
401 this.match('switch'),
402 this.match('('),
403 this.expr(),
404 this.match(')'),
405 this.caseblock()
406 );
4077 return node;
408 },
409 caseblock: function() {
4108 var node = new Node(Node.CASEBLOCK);
4118 node.add(this.match('{'));
4128 while(this.look && this.look.content() != '}') {
413102 if(this.look.content() == 'case') {
41499 node.add(this.caseclause());
415 }
4163 else if(this.look.content() == 'default') {
4172 node.add(this.dftclause());
418 }
419 else {
4201 this.error('invalid switch statement');
421 }
422 }
4237 node.add(this.match('}'));
4247 return node;
425 },
426 caseclause: function() {
42799 var node = new Node(Node.CASECLAUSE);
42899 node.add(
429 this.match('case'),
430 this.expr(),
431 this.match(':')
432 );
43399 while(this.look
434 && this.look.content() != 'case'
435 && this.look.content() != 'default'
436 && this.look.content() != '}') {
437228 node.add(this.stmt());
438 }
43999 return node;
440 },
441 dftclause: function() {
4422 var node = new Node(Node.DFTCLAUSE);
4432 node.add(
444 this.match('default'),
445 this.match(':')
446 );
4472 while(this.look && this.look.content() != '}') {
4482 node.add(this.stmt());
449 }
4502 return node;
451 },
452 labstmt: function() {
4532 var node = new Node(Node.LABSTMT);
4542 node.add(
455 this.match(Token.ID),
456 this.match(':'),
457 this.stmt()
458 );
4592 return node;
460 },
461 thrstmt: function() {
46239 var node = new Node(Node.THRSTMT);
46339 node.add(
464 this.match('throw', true),
465 this.expr(),
466 this.match(';')
467 );
46839 return node;
469 },
470 trystmt: function() {
47155 var node = new Node(Node.TRYSTMT);
47255 node.add(
473 this.match('try'),
474 this.block()
475 );
47655 if(this.look && this.look.content() == 'catch') {
47754 node.add(this.cach());
47854 if(this.look && this.look.content() == 'finally') {
4795 node.add(this.finl());
480 }
481 }
482 else {
4831 node.add(this.finl());
484 }
48555 return node;
486 },
487 debstmt: function() {
4881 var node = new Node(Node.DEBSTMT);
4891 node.add(this.match('debugger'), this.match(';'));
4901 return node;
491 },
492 cach: function() {
49354 var node = new Node(Node.CACH);
49454 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 );
50154 return node;
502 },
503 finl: function() {
5046 var node = new Node(Node.FINL);
5056 node.add(
506 this.match('finally'),
507 this.block()
508 );
5096 return node;
510 },
511 superstmt: function() {
5122 var node = new Node(Node.SUPERSTMT);
5132 node.add(this.match('super'));
5142 if(!this.look) {
5150 this.error();
516 }
5172 if(this.look.content() == '.') {
5181 while(this.look && this.look.content() == '.') {
5192 node.add(this.match());
5202 if(!this.look) {
5210 this.error();
522 }
5232 if(this.look.content() == 'super') {
5241 node.add(this.match());
525 }
526 else {
5271 break;
528 }
529 }
5301 if(this.look.content() != '(') {
5311 node.add(this.match(Token.ID));
5321 while(this.look && this.look.content() == '.') {
5330 node.add(this.match(), this.match(Token.ID));
534 }
535 }
536 }
5372 node.add(
538 this.args(),
539 this.match(';')
540 );
5412 return node;
542 },
543 imptstmt: function() {
5440 var node = new Node(Node.IMPTSTMT);
5450 return node;
546 },
547 fndecl: function() {
548199 var node = new Node(Node.FNDECL);
549199 node.add(
550 this.match('function'),
551 this.match(Token.ID, 'function statement requires a name'),
552 this.match('(')
553 );
554199 if(!this.look) {
5551 this.error('missing formal parameter');
556 }
557198 if(this.look.content() != ')') {
558155 node.add(this.fnparams());
559 }
560198 node.add(
561 this.match(')'),
562 this.match('{'),
563 this.fnbody(),
564 this.match('}')
565 );
566198 return node;
567 },
568 fnexpr: function() {
5691625 var node = new Node(Node.FNEXPR);
5701625 node.add(this.match('function'));
5711625 if(!this.look) {
5721 this.error('missing formal parameter');
573 }
5741624 if(this.look.type() == Token.ID) {
57522 node.add(this.match());
576 }
5771624 node.add(this.match('('));
5781624 if(!this.look) {
5791 this.error();
580 }
5811623 if(this.look.content() != ')') {
5821147 node.add(this.fnparams());
583 }
5841621 node.add(
585 this.match(')'),
586 this.match('{'),
587 this.fnbody(),
588 this.match('}', 'missing } in compound statement')
589 );
5901621 return node;
591 },
592 fnparams: function() {
5931303 var node = new Node(Node.FNPARAMS);
5941303 node.add(this.match(Token.ID, 'missing formal parameter'));
5951303 while(this.look && this.look.content() == ',') {
596985 node.add(this.match());
597985 if(!this.look) {
5981 this.error('missing formal parameter');
599 }
600984 if(this.look.type() == Token.ID) {
601980 node.add(this.match());
602980 if(this.look && this.look.content() == '=') {
6033 node.add(this.bindelement());
604 }
605 }
6064 else if(this.look.content() == '...') {
6073 node.add(this.restparam());
608 }
609 else {
6101 this.error('missing formal parameter');
611 }
612 }
6131301 return node;
614 },
615 bindelement: function() {
6163 var node = new Node(Node.BINDELEMENT);
6173 node.add(this.match('='), this.assignexpr());
6183 return node;
619 },
620 restparam: function() {
6213 var node = new Node(Node.RESTPARAM);
6223 node.add(this.match('...'), this.match(Token.ID));
6233 return node;
624 },
625 fnbody: function(allowSuper) {
6261826 var node = new Node(Node.FNBODY);
6271826 while(this.look && this.look.content() != '}') {
6285168 node.add(this.element(allowSuper));
629 }
6301826 return node;
631 },
632 classdecl: function() {
63313 var node = new Node(Node.CLASSDECL);
63413 node.add(this.match('class'), this.match(Token.ID));
63513 if(!this.look) {
6361 this.error();
637 }
63812 if(this.look.content() == 'extends') {
6393 node.add(this.heratige());
640 }
64112 node.add(
642 this.match('{'),
643 this.classbody(),
644 this.match('}')
645 );
6469 return node;
647 },
648 heratige: function() {
6493 var node = new Node(Node.HERITAGE);
6503 node.add(this.match('extends'), this.match(Token.ID));
6513 return node;
652 },
653 classbody: function() {
65412 var node = new Node(Node.CLASSBODY),
655 methods = {},
656 hasStatic = false;
65712 while(this.look && this.look.content() != '}') {
65811 if(this.look.content() == ';') {
6591 node.add(this.match());
6601 continue;
661 }
66210 hasStatic = false;
66310 if(this.look.content() == 'static') {
6642 node.add(this.match());
6652 hasStatic = true;
666 }
66710 if(!this.look) {
6681 this.error();
669 }
6709 node.add(this.method(hasStatic, methods));
671 }
6729 return node;
673 },
674 method: function(hasStatic, methods, statics) {
6759 var node = new Node(Node.METHOD);
6769 if(this.look.content() == 'get') {
6771 node.add(this.match(), this.getfn());
678 }
6798 else if(this.look.content() == 'set') {
6801 node.add(this.match(), this.setfn());
681 }
682 else {
6837 node.add(this.match(Token.ID));
6847 var id = node.leaves()[0].token().content();
6857 if(methods.hasOwnProperty(id)) {
6861 this.error('duplicate method decl in class');
687 }
6886 methods[id] = true;
6896 node.add(this.match('('));
6905 if(this.look.content() != ')') {
6911 node.add(this.fnparams());
692 }
6935 node.add(
694 this.match(')'),
695 this.match('{'),
696 this.fnbody(true),
697 this.match('}', 'missing } in compound statement')
698 );
699 }
7007 return node;
701 },
702 expr: function(noIn) {
70311056 var node = new Node(Node.EXPR),
704 assignexpr = this.assignexpr(noIn);
70511043 if(this.look && this.look.content() == ',') {
706540 node.add(assignexpr);
707540 while(this.look && this.look.content() == ',') {
7081051 node.add(this.match(), this.assignexpr(noIn));
709 }
710 }
711 else {
71210503 return assignexpr;
713 }
714540 return node;
715 },
716 assignexpr: function(noIn) {
71730758 var node = new Node(Node.ASSIGNEXPR),
718 cndt = this.cndtexpr(noIn);
71930745 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())) {
7333675 node.add(cndt, this.match(), this.assignexpr(noIn));
734 }
735 else {
73627070 return cndt;
737 }
7383675 return node;
739 },
740 cndtexpr: function(noIn) {
74130758 var node = new Node(Node.CNDTEXPR),
742 logorexpr = this.logorexpr(noIn);
74330745 if(this.look && this.look.content() == '?') {
744878 node.add(
745 logorexpr,
746 this.match(),
747 this.assignexpr(noIn),
748 this.match(':'),
749 this.assignexpr(noIn)
750 );
751 }
752 else {
75329867 return logorexpr;
754 }
755878 return node;
756 },
757 logorexpr: function(noIn) {
75830758 var node = new Node(Node.LOGOREXPR),
759 logandexpr = this.logandexpr(noIn);
76030745 if(this.look && this.look.content() == '||') {
7611015 node.add(logandexpr);
7621015 while(this.look && this.look.content() == '||') {
7631150 node.add(
764 this.match(),
765 this.logandexpr(noIn)
766 );
767 }
768 }
769 else {
77029730 return logandexpr;
771 }
7721015 return node;
773 },
774 logandexpr: function(noIn) {
77531908 var node = new Node(Node.LOGANDEXPR),
776 bitorexpr = this.bitorexpr(noIn);
77731895 if(this.look && this.look.content() == '&&') {
7781129 node.add(bitorexpr);
7791129 while(this.look && this.look.content() == '&&') {
7801375 node.add(
781 this.match(),
782 this.bitorexpr(noIn)
783 );
784 }
785 }
786 else {
78730766 return bitorexpr;
788 }
7891129 return node;
790 },
791 bitorexpr: function(noIn) {
79233283 var node = new Node(Node.BITOREXPR),
793 bitxorexpr = this.bitxorexpr(noIn);
79433270 if(this.look && this.look.content() == '|') {
7955 node.add(bitxorexpr);
7965 while(this.look && this.look.content() == '|') {
7975 node.add(
798 this.match(),
799 this.bitxorexpr(noIn)
800 );
801 }
802 }
803 else {
80433265 return bitxorexpr;
805 }
8065 return node;
807 },
808 bitxorexpr: function(noIn) {
80933288 var node = new Node(Node.BITXOREXPR),
810 bitandexpr = this.bitandexpr(noIn);
81133275 if(this.look && this.look.content() == '^') {
8122 node.add(bitandexpr);
8132 while(this.look && this.look.content() == '^') {
8142 node.add(
815 this.match(),
816 this.bitandexpr(noIn)
817 );
818 }
819 }
820 else {
82133273 return bitandexpr;
822 }
8232 return node;
824 },
825 bitandexpr: function(noIn) {
82633290 var node = new Node(Node.BITANDEXPR),
827 eqexpr = this.eqexpr(noIn);
82833277 if(this.look && this.look.content() == '&') {
82918 node.add(eqexpr);
83018 while(this.look && this.look.content() == '&') {
83118 node.add(
832 this.match(),
833 this.eqexpr(noIn)
834 );
835 }
836 }
837 else {
83833259 return eqexpr;
839 }
84018 return node;
841 },
842 eqexpr: function(noIn) {
84333308 var node = new Node(Node.EQEXPR),
844 reltexpr = this.reltexpr(noIn);
84533295 if(this.look && {
846 '==': true,
847 '===': true,
848 '!==': true,
849 '!=': true
850 }.hasOwnProperty(this.look.content())) {
8511589 node.add(reltexpr);
8521589 while(this.look && {
853 '==': true,
854 '===': true,
855 '!==': true,
856 '!=': true
857 }.hasOwnProperty(this.look.content())) {
8581590 node.add(
859 this.match(),
860 this.reltexpr(noIn)
861 );
862 }
863 }
864 else {
86531706 return reltexpr;
866 }
8671589 return node;
868 },
869 reltexpr: function(noIn) {
87034898 var node = new Node(Node.RELTEXPR),
871 shiftexpr = this.shiftexpr();
87234885 if(this.look && ({
873 '<': true,
874 '>': true,
875 '>=': true,
876 '<=': true,
877 'instanceof': true
878 }.hasOwnProperty(this.look.content()) || (!noIn && this.look.content() == 'in'))) {
879477 node.add(shiftexpr);
880477 while(this.look && ({
881 '<': true,
882 '>': true,
883 '>=': true,
884 '<=': true,
885 'instanceof': true
886 }.hasOwnProperty(this.look.content()) || (!noIn && this.look.content() == 'in'))) {
887477 node.add(
888 this.match(),
889 this.shiftexpr()
890 );
891 }
892 }
893 else {
89434408 return shiftexpr;
895 }
896477 return node;
897 },
898 shiftexpr: function() {
89935375 var node = new Node(Node.SHIFTEXPR),
900 addexpr = this.addexpr();
90135362 if(this.look && ['<<', '>>', '>>>'].indexOf(this.look.content()) != -1) {
9028 node.add(addexpr);
9038 while(this.look && ['<<', '>>', '>>>'].indexOf(this.look.content()) != -1) {
9049 node.add(
905 this.match(),
906 this.addexpr()
907 );
908 }
909 }
910 else {
91135354 return addexpr;
912 }
9138 return node;
914 },
915 addexpr: function() {
91635384 var node = new Node(Node.ADDEXPR),
917 mtplexpr = this.mtplexpr();
91835371 if(this.look && ['+', '-'].indexOf(this.look.content()) != -1) {
919777 node.add(mtplexpr);
920777 while(this.look && ['+', '-'].indexOf(this.look.content()) != -1) {
9211318 node.add(
922 this.match(),
923 this.mtplexpr()
924 );
925 }
926 }
927 else {
92834594 return mtplexpr;
929 }
930777 return node;
931 },
932 mtplexpr: function() {
93336702 var node = new Node(Node.MTPLEXPR),
934 unaryexpr = this.unaryexpr();
93536689 if(this.look && ['*', '/', '%'].indexOf(this.look.content()) != -1) {
93651 node.add(unaryexpr);
93751 while(this.look && ['*', '/', '%'].indexOf(this.look.content()) != -1) {
93852 node.add(
939 this.match(),
940 this.unaryexpr()
941 );
942 }
943 }
944 else {
94536638 return unaryexpr;
946 }
94751 return node;
948 },
949 unaryexpr: function() {
95038472 var node = new Node(Node.UNARYEXPR);
95138472 if(!this.look) {
9522 this.error();
953 }
95438470 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 '!':
9641718 node.add(
965 this.match(),
966 this.unaryexpr()
967 );
9681716 break;
969 default:
97036752 return this.postfixexpr();
971 }
9721716 return node;
973 },
974 postfixexpr: function() {
97536752 var node = new Node(Node.POSTFIXEXPR);
97636752 var leftexpr = this.leftexpr();
97736741 if(this.look && ['++', '--'].indexOf(this.look.content()) > -1 && !this.hasMoveLine) {
978327 node.add(leftexpr);
979327 while(this.look && ['++', '--'].indexOf(this.look.content()) > -1) {
980327 node.add(this.match(undefined, true));
981 }
982 }
983 else {
98436414 return leftexpr;
985 }
986327 return node;
987 },
988 leftexpr: function() {
98936752 if(this.look.content() == 'new') {
990190 return this.newexpr();
991 }
992 else {
99336562 return this.callexpr();
994 }
995 },
996 newexpr: function(depth) {
997191 depth = depth || 0;
998191 var node = new Node(Node.NEWEXPR);
999191 node.add(this.match('new'));
1000191 if(!this.look) {
10011 this.error();
1002 }
1003190 if(this.look.content() == 'new') {
10041 node.add(this.newexpr(depth + 1));
1005 }
1006 else {
1007189 node.add(this.mmbexpr());
1008 }
1009190 if(this.look && this.look.content() == '(') {
1010179 node.add(this.args());
1011 }
1012190 if(this.look && ['.', '['].indexOf(this.look.content()) > -1) {
101311 var mmb = new Node(Node.MMBEXPR);
101411 mmb.add(node);
101511 while(this.look) {
101620 if(this.look.content() == '.') {
101710 mmb.add(
1018 this.match(),
1019 this.match(Token.ID)
1020 );
1021 }
102210 else if(this.look.content() == '[') {
10231 mmb.add(
1024 this.match(),
1025 this.expr(),
1026 this.match(']')
1027 );
1028 }
1029 else {
10309 break;
1031 }
1032 }
103311 if(depth == 0 && this.look && this.look.content() == '(') {
10348 var callexpr = this.callexpr(mmb);
10358 return callexpr;
1036 }
10373 return mmb;
1038 }
1039179 return node;
1040 },
1041 callexpr: function(mmb) {
104236570 var node = new Node(Node.CALLEXPR);
104336570 mmb = mmb || this.mmbexpr();
104436561 if(this.look && this.look.content() == '(') {
10455156 node.add(
1046 mmb,
1047 this.args()
1048 );
10495155 if(this.look && ['.', '[', '('].indexOf(this.look.content()) > -1) {
1050364 while(this.look) {
10511186 if(this.look.content() == '.') {
1052426 node.add(
1053 this.match(),
1054 this.match(Token.ID)
1055 );
1056 }
1057760 else if(this.look.content() == '[') {
105845 node.add(
1059 this.match(),
1060 this.expr(),
1061 this.match(']')
1062 );
1063 }
1064715 else if(this.look.content() == '(') {
1065351 node.add(this.args());
1066 }
1067 else {
1068364 break;
1069 }
1070 }
1071 }
1072 }
1073 else {
107431405 return mmb;
1075 }
10765155 return node;
1077 },
1078 mmbexpr: function() {
107936751 var node = new Node(Node.MMBEXPR);
108036751 var mmb;
108136751 if(this.look.content() == 'function') {
10821625 mmb = this.fnexpr();
1083 }
1084 else {
108535126 mmb = this.prmrexpr();
1086 }
108736742 if(this.look && ['.', '['].indexOf(this.look.content()) > -1) {
108810737 node.add(mmb);
108910737 while(this.look) {
109023519 if(this.look.content() == '.') {
109110859 node.add(
1092 this.match(),
1093 this.match(Token.ID)
1094 );
1095 }
109612660 else if(this.look.content() == '[') {
10971928 node.add(
1098 this.match(),
1099 this.expr(),
1100 this.match(']')
1101 );
1102 }
1103 else {
110410732 break;
1105 }
1106 }
1107 }
1108 else {
110926005 return mmb;
1110 }
111110737 return node;
1112 },
1113 prmrexpr: function() {
111435126 var node = new Node(Node.PRMREXPR);
111535126 switch(this.look.type()) {
1116 case Token.ID:
1117 case Token.NUMBER:
1118 case Token.STRING:
1119 case Token.REG:
1120 case Token.TEMPLATE:
112129061 node.add(this.match());
112229061 break;
1123 default:
11246065 switch(this.look.content()) {
1125 case 'this':
1126 case 'null':
1127 case 'true':
1128 case 'false':
11293381 node.add(this.match());
11303381 break;
1131 case '(':
11321087 node.add(this.match(), this.expr(), this.match(')'));
11331083 break;
1134 case '[':
1135867 node.add(this.arrltr());
1136867 break;
1137 case '{':
1138729 node.add(this.objltr());
1139729 break;
1140 default:
11411 this.error();
1142 }
1143 }
114435121 return node;
1145 },
1146 arrltr: function() {
1147867 var node = new Node(Node.ARRLTR);
1148867 node.add(this.match('['));
1149867 while(this.look && this.look.content() != ']') {
11502366 if(this.look.content() == ',') {
1151864 node.add(this.match());
1152 }
1153 else {
11541502 node.add(this.assignexpr());
1155 }
1156 }
1157867 node.add(this.match(']'));
1158867 return node;
1159 },
1160 objltr: function() {
1161729 var node = new Node(Node.OBJLTR);
1162729 node.add(this.match('{'));
1163729 while(this.look && this.look.content() != '}') {
11642002 node.add(this.proptassign());
11652002 if(this.look && this.look.content() == ',') {
11661511 node.add(this.match());
1167 }
1168 }
1169729 node.add(this.match('}'));
1170729 return node;
1171 },
1172 proptassign: function() {
11732002 var node = new Node(Node.PROPTASSIGN);
11742002 if(!this.look) {
11750 this.error();
1176 }
11772002 if(this.look.content() == 'get') {
117828 node.add(this.match());
117928 if(!this.look) {
11800 this.error();
1181 }
118228 if(this.look.content() == ':') {
118328 node.add(this.match(), this.assignexpr());
1184 }
1185 else {
11860 node.add(this.getfn());
1187 }
1188 }
11891974 else if(this.look.content() == 'set') {
119030 node.add(this.match());
119130 if(!this.look) {
11920 this.error();
1193 }
119430 if(this.look.content() == ':') {
119530 node.add(this.match(), this.assignexpr());
1196 }
1197 else {
11980 node.add(this.setfn());
1199 }
1200 }
1201 else {
12021944 switch(this.look.type()) {
1203 case Token.ID:
1204 case Token.STRING:
1205 case Token.NUMBER:
12061944 node.add(
1207 this.match(),
1208 this.match(':'),
1209 this.assignexpr()
1210 );
12111944 break;
1212 default:
12130 this.error('invalid property id');
1214 }
1215 }
12162002 return node;
1217 },
1218 getfn: function() {
12191 var node = new Node(Node.GETFN);
12201 node.add(
1221 this.proptname(),
1222 this.match('('),
1223 this.match(')'),
1224 this.match('{'),
1225 this.fnbody(),
1226 this.match('}')
1227 );
12281 return node;
1229 },
1230 setfn: function() {
12311 var node = new Node(Node.SETFN);
12321 node.add(
1233 this.proptname(),
1234 this.match('('),
1235 this.propsets(),
1236 this.match(')'),
1237 this.match('{'),
1238 this.fnbody(),
1239 this.match('}')
1240 );
12411 return node;
1242 },
1243 proptname: function() {
12442 var node = new Node(Node.PROPTNAME);
12452 if(this.look) {
12462 switch(this.look.type()) {
1247 case Token.ID:
1248 case Token.NUMBER:
1249 case Token.STRING:
12502 node.add(this.match());
12512 break;
1252 default:
12530 this.error('missing name after . operator');
1254 }
1255 }
12562 return node;
1257 },
1258 propsets: function() {
12591 var node = new Node(Node.PROPTSETS);
12601 node.add(this.match(Token.ID, 'setter functions must have one argument'));
12611 return node;
1262 },
1263 args: function() {
12645688 var node = new Node(Node.ARGS);
12655688 node.add(this.match('('));
12665688 if(!this.look) {
12671 this.error();
1268 }
12695687 if(this.look.content() != ')') {
12704947 node.add(this.arglist());
1271 }
12725687 node.add(this.match(')'));
12735687 return node;
1274 },
1275 arglist: function() {
12764947 var node = new Node(Node.ARGLIST);
12774947 node.add(this.assignexpr());
12784947 while(this.look && this.look.content() == ',') {
12792848 node.add(this.match(), this.assignexpr());
1280 }
12814947 return node;
1282 },
1283 virtual: function(s) {
12841581 return new Node(Node.TOKEN, new Token(Token.VIRTUAL, s));
1285 },
1286 match: function(type, line, msg) {
1287140140 if(typeof type == 'boolean') {
12880 msg = line;
12890 line = type;
12900 type = undefined;
1291 }
1292140140 if(typeof line != 'boolean') {
1293137997 line = false;
1294137997 msg = line;
1295 }
1296 //未定义为所有非空白token
1297140140 if(character.isUndefined(type)) {
129871758 if(this.look) {
129971758 var l = this.look;
130071758 this.move(line);
130171758 return new Node(Node.TOKEN, l);
1302 }
1303 else {
13040 this.error('syntax error' + (msg || ''));
1305 }
1306 }
1307 //或者根据token的type或者content匹配
130868382 else if(typeof type == 'string') {
1309 //特殊处理;,不匹配但有换行或者末尾时自动补全,还有受限行
131052697 if(type == ';'
1311 && (!this.look
1312 || (this.look.content() != type && this.hasMoveLine)
1313 || this.look.content() == '}')
1314 ) {
13151581 if(this.look && S[this.look.type()]) {
131636 this.move();
1317 }
13181581 return this.virtual(';');
1319 }
132051116 else if(this.look && this.look.content() == type) {
132151115 var l = this.look;
132251115 this.move(line);
132351115 return new Node(Node.TOKEN, l);
1324 }
1325 else {
13261 this.error('missing ' + type + (msg || ''));
1327 }
1328 }
132915685 else if(typeof type == 'number') {
133015685 if(this.look && this.look.type() == type) {
133115685 var l = this.look;
133215685 this.move(line);
133315685 return new Node(Node.TOKEN, l);
1334 }
1335 else {
13360 this.error('missing ' + Token.type(type) + (msg || ''));
1337 }
1338 }
1339 },
1340 move: function(line) {
1341138772 this.lastLine = this.line;
1342138772 this.lastCol = this.col;
1343 //遗留下来的换行符
1344138772 this.hasMoveLine = false;
1345138772 do {
1346247985 this.look = this.tokens[this.index++];
1347247985 if(!this.look) {
1348171 return;
1349 }
1350 //存下忽略的token
1351247814 if(S[this.look.type()]) {
1352109249 this.ignores[this.index - 1] = this.look;
1353 }
1354 //包括line的情况下要跳出
1355247814 if(this.look.type() == Token.LINE) {
135617784 this.line++;
135717784 this.col = 1;
135817784 this.hasMoveLine = true;
135917784 if(line) {
136035 break;
1361 }
1362 }
1363230030 else if(this.look.type() == Token.COMMENT) {
13642742 var s = this.look.content();
13652742 var n = character.count(this.look.content(), character.LINE);
13662742 if(n > 0) {
136790 this.line += n;
136890 var i = s.lastIndexOf(character.LINE);
136990 this.col += s.length - i - 1;
137090 this.hasMoveLine = true;
137190 if(line) {
13721 break;
1373 }
1374 }
1375 }
1376 else {
1377227288 this.col += this.look.content().length;
1378227288 if(!S[this.look.type()]) {
1379138565 break;
1380 }
1381 }
1382 } while(this.index <= this.length);
1383 },
1384 error: function(msg) {
138528 msg = 'SyntaxError: ' + (msg || ' syntax error');
138628 throw new Error(msg + ' line ' + this.lastLine + ' col ' + this.lastCol);
1387 },
1388 ignore: function() {
13898 return this.ignores;
1390 }
1391});
13921module.exports = Parser;

/Users/army/Sites/homunculus/src/util/Class.js

100%
24
24
0
LineHitsSource
11function inheritPrototype(subType, superType) {
210 var prototype = Object.create(superType.prototype);
310 prototype.constructor = subType;
410 subType.prototype = prototype;
5 //继承static变量
610 Object.keys(superType).forEach(function(k) {
737 subType[k] = superType[k];
8 });
910 return subType;
10}
111function wrap(fn) {
1219 fn.extend = function(sub) {
1310 inheritPrototype(sub, fn);
1410 return wrap(sub);
15 }
1619 fn.methods = function(o) {
1716 Object.keys(o).forEach(function(k) {
18191 fn.prototype[k] = o[k];
19 });
2016 return fn;
21 };
2219 fn.statics = function(o) {
238 Object.keys(o).forEach(function(k) {
24136 fn[k] = o[k];
25 });
268 return fn;
27 };
2819 return fn;
29}
301function klass(cons) {
319 return wrap(cons || function() {});
32}
331klass.extend = inheritPrototype;
341module.exports = klass;

/Users/army/Sites/homunculus/src/util/character.js

87%
41
36
5
LineHitsSource
11exports.LINE = '\n';
21exports.ENTER = '\r';
31exports.BLANK = ' ';
41exports.TAB = '\t';
51exports.UNDERLINE = '_';
61exports.DOLLAR = '$';
71exports.SHARP = '#';
81exports.MINUS = '-';
91exports.AT = '@';
101exports.SLASH = '/';
111exports.BACK_SLASH = '\\';
121exports.DECIMAL = '.';
131exports.LEFT_BRACKET = '[';
141exports.RIGHT_BRACKET = ']';
151exports.STAR = '*';
161exports.LEFT_PARENTHESE = '(';
171exports.RIGHT_PARENTHESE = ')';
181exports.COMMA = ',';
191exports.SEMICOLON = ';';
201exports.EQUAL = '=';
211exports.isDigit = function(c) {
220 return c >= '0' && c <= '9';
23};
241exports.isDigit16 = function(c) {
250 return exports.isDigit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
26};
271exports.isDigit2 = function(c) {
280 return c == '0' || c == '1';
29};
301exports.isDigit8 = function(c) {
310 return c >= '0' && c <= '7';
32};
331exports.isLetter = function(c) {
34691 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
35};
361exports.count = function(s, c) {
37456858 var count = 0,
38 i = -1;
39456858 while((i = s.indexOf(c, i + 1)) != -1) {
4033562 count++;
41 }
42456858 return count;
43};
441exports.isUndefined = function(s) {
454385950 return typeof s == 'undefined';
46};
471exports.isString = function(s) {
480 return Object.prototype.toString.call(s) == "[object String]";
49};
501exports.isNumber = function(s) {
51456191 return Object.prototype.toString.call(s) == "[object Number]";
52};