Coverage

98%
1195
1181
14

/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) {
5245 this.rule = rule; //当前语法规则
6245 this.init();
7}).methods({
8 init: function() {
9246 this.code = ''; //要解析的代码
10246 this.peek = ''; //向前看字符
11246 this.index = 0; //向前看字符字符索引
12246 this.isReg = Lexer.IS_REG; //当前/是否是perl风格正则表达式
13246 this.tokenList = []; //结果的token列表
14246 this.parentheseState = false; //(开始时标记之前终结符是否为if/for/while等关键字
15246 this.parentheseStack = []; //圆括号深度记录当前是否为if/for/while等语句内部
16246 this.cacheLine = 0; //行缓存值
17246 this.totalLine = 1; //总行数
18246 this.colNum = 0; //列
19246 this.colMax = 0; //最大列数
20 },
21 parse: function(code) {
22202 this.code = code || '';
23202 var temp = [];
24202 this.scan(temp);
25193 return temp;
26 },
27 parseOn: function() {
281 var temp = [];
291 this.scan(temp);
301 return temp;
31 },
32 tokens: function() {
33167 return this.tokenList;
34 },
35 scan: function(temp) {
36203 var perlReg = this.rule.perlReg();
37203 var length = this.code.length;
38203 var count = 0;
39 outer:
40 while(this.index < length) {
41454460 if(this.cacheLine > 0 && count >= this.cacheLine) {
421 break;
43 }
44454459 this.readch();
45 //perl风格正则
46454459 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 {
52453966 for(var i = 0, matches = this.rule.matches(), len = matches.length; i < len; i++) {
537880077 var match = matches[i];
547880077 if(match.match(this.peek, this.code, this.index)) {
55453965 var token = new Token(match.tokenType(), match.content(), match.val(), this.index - 1);
56453965 var error = match.error();
57453965 var matchLen = match.content().length;
58453965 if(token.type() == Token.ID && this.rule.keyWords().hasOwnProperty(token.content())) {
5921465 token.type(Token.KEYWORD);
60 }
61453965 temp.push(token);
62453965 this.tokenList.push(token);
63453965 this.index += matchLen - 1;
64453965 var n = character.count(token.val(), character.LINE);
65453965 count += n;
66453965 this.totalLine += n;
67453965 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 {
74421645 this.colNum += matchLen;
75 }
76453965 this.colMax = Math.max(this.colMax, this.colNum);
77453965 if(error) {
785 this.error(error, this.code.slice(this.index - matchLen, this.index));
79 }
80 //支持perl正则需判断关键字、圆括号对除号语义的影响
81453961 if(perlReg && match.perlReg() != Lexer.IGNORE) {
82258227 if(match.perlReg() == Lexer.SPECIAL) {
8396002 this.isReg = match.special();
84 }
85 else {
86162225 this.isReg = match.perlReg();
87 }
88258227 if(this.peek == character.LEFT_PARENTHESE) {
8919875 this.parentheseStack.push(this.parentheseState);
9019875 this.parentheseState = false;
91 }
92238352 else if(this.peek == character.RIGHT_PARENTHESE) {
9319858 this.isReg = this.parentheseStack.pop() ? Lexer.IS_REG : Lexer.NOT_REG;
94 }
95 else {
96218494 this.parentheseState = match.parenthese();
97 }
98 }
99453961 continue outer;
100 }
101 }
102 //如果有未匹配的,说明规则不完整,抛出错误
1031 this.error('unknow token');
104 }
105 }
106194 return this;
107 },
108 readch: function() {
109463321 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) {
6456037 this.t = type; //token类型
7456037 this.c = content; //token的字面内容,string包括头尾的引号
8456037 if(character.isNumber(val)) {
9494 sIndex = val;
10494 val = content;
11 }
12455543 else if(character.isUndefined(val)) {
131578 val = content;
141578 sIndex = -1;
15 }
16456037 this.v = val; //token的值,一般情况下等于content,特殊如string情况下值是不加头尾的引号
17456037 this.id = tid++; //token的索引
18456037 this.si = sIndex; //token在源码字符串中的索引
19}).methods({
20 type: function(t) {
211635737 if(!character.isUndefined(t)) {
2221465 this.t = t;
23 }
241635737 return this.t;
25 },
26 content: function(c) {
271670740 if(!character.isUndefined(c)) {
281 this.c = c;
29 }
301670740 return this.c;
31 },
32 val: function(v) {
33453967 if(!character.isUndefined(v)) {
341 this.v = v;
35 }
36453967 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) {
797 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 }
877 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) {
4486 Match.call(this, type, setPReg);
5486 this.str = str;
6}).methods({
7 match: function(c, code, index) {
8140961 var isIn = this.str.indexOf(c) > -1;
9140961 if(isIn) {
10133444 this.result = c;
11 }
12140961 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) {
49119 Match.call(this, type, setPReg);
59119 this.result = result;
6}).methods({
7 match: function(c, code, index) {
86032949 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) {
5733 if(character.isUndefined(mutiline)) {
60 mutiline = false;
7 }
8733 Match.call(this, type, setPReg);
9733 this.begin = begin;
10733 this.end = end;
11733 this.msg = null;
12733 this.mutiline = mutiline;
13 }).methods({
14 match: function(c, code, index) {
15762664 this.msg = null;
16762664 if(this.begin == code.charAt(index - 1)) {
176828 var len = code.length,
18 lastIndex = index - 1,
19 res = false;
206828 while(index < len) {
2157360 var c = code.charAt(index++);
22 //转义
2357360 if(c == character.BACK_SLASH) {
24402 if(code.charAt(index++) == character.ENTER) {
251 index++;
26 }
27 }
2856958 else if(c == character.LINE && !this.mutiline) {
291 break;
30 }
3156957 else if(c == this.end) {
326826 res = true;
336826 break;
34 }
35 }
366828 if(!res) {
372 this.msg = 'SyntaxError: unterminated ' + Token.type(this.type).toLowerCase() + ' literal';
38 }
396828 this.result = code.slice(lastIndex, index);
406828 return true;
41 }
42755836 return false;
43 },
44 error: function() {
456828 return this.msg;
46 },
47 val: function() {
486828 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%
33
33
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) {
5490 if(character.isUndefined(contain)) {
6245 contain = false;
7 }
8490 Match.call(this, type, setPReg);
9490 this.begin = begin;
10490 this.end = end;
11490 this.contain = contain;
12490 this.msg = null;
13}).methods({
14 match: function(c, code, index) {
15521678 this.msg = null;
16521678 if(this.begin == code.substr(--index, this.begin.length)) {
17 //支持多个end匹配时不支持包含选项
185001 if(!this.contain && Array.isArray(this.end)) {
194786 for(var j = 0, len = this.end.length; j < len; j++) {
2014358 var i = code.indexOf(this.end[j], index + this.begin.length);
2114358 if(i != -1) {
224785 this.result = code.slice(index, i);
234785 return true;
24 }
25 }
26 //都不匹配时到末尾
271 this.result = code.slice(index);
281 return true;
29 }
30 else {
31215 var i = code.indexOf(this.end, index + this.begin.length);
32215 if(i == -1) {
331 if(this.contain) {
341 this.msg = 'SyntaxError: unterminated ' + Token.type(this.type).toLowerCase();
35 }
361 i = code.length;
37 }
38214 else if(this.contain) {
39214 i += this.end.length;
40 }
41215 this.result = code.slice(index, i);
42215 return true;
43 }
44 }
45516677 return false;
46 },
47 error: function() {
485001 return this.msg;
49 }
50});
511module.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) {
511828 this.type = type;
611828 if(character.isUndefined(setPReg)) {
72594 setPReg = Lexer.IGNORE;
8 }
911828 this.setPReg = setPReg;
1011828 this.result = null;
11 //忽略0,是1,否2,特殊3
1211828 if(setPReg) {
139234 if(character.isUndefined(special)) {
148991 special = function() {
150 return Lexer.IGNORE;
16 };
17 }
189234 if(character.isUndefined(parenthese)) {
198991 parenthese = function() {
20122492 return false;
21 };
22 }
23 }
2411828 this.special = special;
2511828 this.parenthese = parenthese;
26}).methods({
27 tokenType: function() {
28453965 return this.type;
29 },
30 perlReg: function() {
31874413 return this.setPReg;
32 },
33 val: function() {
34447137 return this.content();
35 },
36 content: function() {
371650859 return this.result;
38 },
39 match: function(c, code, index) {
40 //需被实现
410 throw new Error('match needs to be implement');
42 },
43 error: function() {
44338611 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) {
31000 if(typeof valid == 'number') {
4243 parenthese = special;
5243 special = setPReg;
6243 setPReg = valid;
7243 valid = null;
8 }
91000 Match.call(this, type, setPReg, special, parenthese);
101000 this.reg = reg;
111000 this.valid = valid;
12 }).methods({
13 match: function(c, code, index) {
14421825 var self = this,
15 res = self.reg.exec(code.slice(index - 1));
16421825 self.msg = null;
17421825 if(res) {
18103525 self.result = res[0];
19103525 if(self.valid) {
207523 for(var i = 0, keys = Object.keys(self.valid), len = keys.length; i < len; i++) {
217523 if(self.valid[keys[i]].test(self.result)) {
222 self.msg = keys[i];
232 break;
24 }
25 }
26 }
27103525 return true;
28 }
29318300 return false;
30 },
31 error: function() {
32103525 return this.msg;
33 }
34 });
351module.exports = RegMatch;

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

100%
36
36
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() {
11243 var self = this;
12243 Rule.call(self, EcmascriptRule.KEYWORDS, true);
13
14243 self.addMatch(new CompleteEqual(Token.BLANK, character.BLANK));
15243 self.addMatch(new CompleteEqual(Token.TAB, character.TAB));
16243 self.addMatch(new CompleteEqual(Token.LINE, character.ENTER + character.LINE));
17243 self.addMatch(new CompleteEqual(Token.LINE, character.ENTER));
18243 self.addMatch(new CompleteEqual(Token.LINE, character.LINE));
19
20243 self.addMatch(new LineSearch(Token.COMMENT, '//', [character.ENTER + character.LINE, character.ENTER, character.LINE]));
21243 self.addMatch(new LineSearch(Token.COMMENT, '/*', '*/', true));
22243 self.addMatch(new LineParse(Token.STRING, '"', '"', false, Lexer.IS_REG));
23243 self.addMatch(new LineParse(Token.STRING, "'", "'", false, Lexer.IS_REG));
24243 self.addMatch(new LineParse(Token.TEMPLATE, '`', '`', true, Lexer.IS_REG));
25
26243 self.addMatch(new RegMatch(Token.ID, /^[$a-zA-Z_][$\w]*/, Lexer.SPECIAL, function() {
2796002 return !!(self.keyWords().hasOwnProperty(this.content()));
28 }, function() {
2996002 return ['if', 'for', 'while'].indexOf(this.content()) != -1;
30 }));
31
32243 self.addMatch(new RegMatch(Token.NUMBER, /^\.\d+(?:E[+-]?\d*)?/i, {
33 'SyntaxError: missing exponent': /E[+-]?$/i
34 }, Lexer.NOT_REG));
35
36243 self.addMatch(new CompleteEqual(Token.SIGN, ']', Lexer.NOT_REG));
37
38243 ['*=', '/=', '+=', '-=', '%=', '^=', '&=', '|=', '&&', '--', '++', '===', '==', '!==', '!=', '||', '>>>=', '<<<=', '<<<', '>>>', '>>=', '<<=', '<<', '>>', '>=', '<=', '...', '?:', '=>'].forEach(function(o) {
397047 self.addMatch(new CompleteEqual(Token.SIGN, o, Lexer.IS_REG));
40 });
41243 self.addMatch(new CharacterSet(Token.SIGN, ':;/?.,[]{}~!^|%=-+*()~><&\\', Lexer.IS_REG));
42
43243 self.addMatch(new RegMatch(Token.NUMBER, /^0x[\da-f]*/i, {
44 "SyntaxError: missing hexadecimal digits after '0x'": /^0x$/i
45 }, Lexer.NOT_REG));
46243 self.addMatch(new RegMatch(Token.NUMBER, /^\d+\.?\d*(?:E[+-]?\d*)?/i, {
47 'SyntaxError: missing exponent': /E[+-]?$/i
48 }, Lexer.NOT_REG));
49
50243 self.addMatch(new CompleteEqual(Token.LINE, '\u2028'));
51243 self.addMatch(new CompleteEqual(Token.LINE, '\u2029'));
52243 self.addMatch(new CharacterSet(Token.BLANK, '\f\u000b\u00A0\uFEFF\u200b\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000'));
53}).statics({
54 KEYWORDS: 'break case catch class const continue debugger default delete do else enum export extends false finally for function if implements import in instanceof interface let new null package private protected public return static super switch this throw true try typeof var void while with yield'.split(' ')
55});
561module.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) {
3245 var self = this;
4245 self.kw = {};
5245 words.forEach(function(o) {
611305 self.kw[o] = true;
7 });
8245 self.pReg = pReg || false;
9245 self.matchList = [];
10 }).methods({
11 perlReg: function() {
12203 return this.pReg;
13 },
14 addMatch: function(match) {
1511828 this.matchList.push(match);
1611828 return this;
17 },
18 matches: function() {
19453966 return this.matchList;
20 },
21 keyWords: function() {
22192004 return this.kw;
23 }
24 });
251module.exports = Rule;

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

99%
202
201
1
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) {
15418 var v = node.token().content();
15518 this.vids.push(node);
15618 this.vidsMap[v] = this.vidsMap[v] || [];
15718 this.vidsMap[v].push(node);
15818 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) {
180662 var isToken = node.name() == JsNode.TOKEN;
181662 var isVirtual = isToken && node.token().type() == Token.VIRTUAL;
182662 if(isToken) {
183382 if(!isVirtual) {
184376 var token = node.token();
185376 var s = token.content();
186376 if(s == 'return') {
1875 context.addReturn(node);
188 }
189 }
190 }
191 else {
192280 if(node.name() == JsNode.VARDECL) {
19317 vardecl(node, context);
194 }
195263 else if(node.name() == JsNode.FNDECL) {
19612 context = fndecl(node, context);
197 }
198251 else if(node.name() == JsNode.FNEXPR) {
19914 context = fnexpr(node, context);
200 }
201237 else if(node.name() == JsNode.PRMREXPR) {
20251 prmrexpr(node, context);
203 }
204280 node.leaves().forEach(function(leaf, i) {
205627 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 else {
3090 child.setThis(undefined);
310 }
311 }
312 }
313 }
314 }
315 }
316 }
31714 return child;
318}
319
3201function addParam(params, child) {
3218 params.leaves().forEach(function(leaf, i) {
32212 if(leaf.name() == JsNode.TOKEN && leaf.token().content() != ',') {
32310 child.addParam(leaf.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 if(params.leaves()[2]) {
3472 params.leaves()[2].leaves()[0].leaves().forEach(function(leaf, i) {
3488 if(i % 2 == 1) {
3493 child.addAParam(leaf);
350 }
351 });
352 }
353}
3541function prmrexpr(node, context) {
35551 var first = node.leaves()[0];
35651 if(first.name() == JsNode.TOKEN) {
35747 var token = first.token();
35847 if(token.type() == Token.ID || token.content() == 'this') {
35918 context.addVid(first);
360 }
361 }
362}
363
3641module.exports = Context;

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

97%
47
46
1
LineHitsSource
11var Class = require('../../util/Class');
21var Node = Class(function(type, children) {
3770065 this.type = type;
4770065 if(type == Node.TOKEN) {
5140032 this.children = children;
6 }
7630033 else if(Array.isArray(children)) {
80 this.children = children;
9 }
10 else {
11630033 this.children = children ? [children] : [];
12 }
13770065 this.p = null;
14770065 this.pr = null;
15770065 this.ne = null;
16770065 return this;
17}).methods({
18 name: function() {
19240238 return this.type;
20 },
21 leaves: function() {
2298819 return this.children;
23 },
24 leaf: function(i) {
251 return this.children[i];
26 },
27 number: function() {
281 return this.children.length;
29 },
30 add: function() {
31169467 var self = this;
32169467 var args = Array.prototype.slice.call(arguments, 0);
33169467 args.forEach(function(node) {
34238519 node.parent(self);
35238519 var last = self.children[self.children.length - 1];
36238519 if(last) {
37139887 last.next(node);
38139887 node.prev(last);
39 }
40238519 self.children.push(node);
41 });
42169467 return self;
43 },
44 token: function() {
45278406 return this.children;
46 },
47 parent: function(p) {
48238535 if(p) {
49238519 this.p = p;
50 }
51238535 return this.p;
52 },
53 prev: function(pr) {
54139894 if(pr) {
55139887 this.pr = pr;
56 }
57139894 return this.pr;
58 },
59 next: function(ne) {
60139927 if(ne) {
61139887 this.ne = ne;
62 }
63139927 return this.ne;
64 }
65}).statics({
66 PROGRAM: 'program',
67 ELEMS: 'elems',
68 ELEM: 'elem',
69 CSTSTMT: 'cststmt',
70 LETSTMT: 'letstmt',
71 VARSTMT: 'varstmt',
72 VARDECL: 'vardecl',
73 FNBODY: 'fnbody',
74 BLOCK: 'block',
75 ITERSTMT: 'iterstmt',
76 TOKEN: 'token',
77 FNPARAMS: 'fnparams',
78 BINDELEMENT: 'bindelement',
79 RESTPARAM: 'restparam',
80 EXPR: 'expr',
81 CLASSDECL: 'classdecl',
82 CLASSTAIL: 'classtail',
83 HERITAGE: 'heritage',
84 CLASSBODY: 'classbody',
85 METHOD: 'method',
86 SUPERSTMT: 'superstmt',
87 GETFN: 'getfn',
88 SETFN: 'setfn',
89 PROGRAM: 'program',
90 STMT: 'stmt',
91 ASSIGN: 'assign',
92 EMPTSTMT: 'emptstmt',
93 IFSTMT: 'ifstmt',
94 CNTNSTMT: 'cntnstmt',
95 BRKSTMT: 'brkstmt',
96 RETSTMT: 'retstmt',
97 WITHSTMT: 'withstmt',
98 SWCHSTMT: 'swchstmt',
99 CASEBLOCK: 'caseblock',
100 CASECLAUSE: 'caseclause',
101 DFTCLAUSE: 'dftclause',
102 LABSTMT: 'labstmt',
103 THRSTMT: 'thrstmt',
104 TRYSTMT: 'trystmt',
105 DEBSTMT: 'debstmt',
106 EXPRSTMT: 'exprstmt',
107 CACH: 'cach',
108 FINL: 'finl',
109 FNDECL: 'fndecl',
110 FNEXPR: 'fnexpr',
111 ASSIGNEXPR: 'assignexpr',
112 CNDTEXPR: 'cndtexpr',
113 LOGOREXPR: 'logorexpr',
114 LOGANDEXPR: 'logandexpr',
115 BITOREXPR: 'bitorexpr',
116 BITANDEXPR: 'bitandexpr',
117 BITXOREXPR: 'bitxorexpr',
118 EQEXPR: 'eqexpr',
119 RELTEXPR: 'reltexpr',
120 SHIFTEXPR: 'shiftexpr',
121 ADDEXPR: 'addexpr',
122 MTPLEXPR: 'mtplexpr',
123 UNARYEXPR: 'unaryexpr',
124 MMBEXPR: 'mmbexpr',
125 PRMREXPR: 'prmrexpr',
126 ARRLTR: 'arrltr',
127 OBJLTR: 'objltr',
128 PROPTASSIGN: 'proptassign',
129 PROPTNAME: 'proptname',
130 PROPTSETS: 'propsets',
131 ARGS: 'args',
132 ARGLIST: 'arglist',
133 IMPTSTMT: 'imptstmt',
134 POSTFIXEXPR: 'postfixexpr',
135 NEWEXPR: 'newexpr',
136 CALLEXPR: 'callexpr',
137 ARRBINDPAT: 'arrbindpat',
138 OBJBINDPAT: 'objbindpat',
139 BINDPROPT: 'bindpropt',
140 SINGLENAME: 'singlename',
141 BINDELEM: 'bindelem',
142 BINDREST: 'bindrest',
143 BINDID: 'bindid',
144 SPREAD: 'spread',
145 ARRCMPH: 'arrcmph',
146 CMPHFOR: 'cmphfor',
147 getKey: function(s) {
1482 if(!s) {
1491 throw new Error('empty value');
150 }
1511 if(!keys) {
1521 var self = this;
1531 keys = {};
1541 Object.keys(this).forEach(function(k) {
15584 var v = self[k];
15684 keys[v] = k;
157 });
158 }
1591 return keys[s];
160 }
161});
1621var keys;
1631module.exports = Node;

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

99%
533
529
4
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) {
10202 this.init(lexer);
11}).methods({
12 parse: function(code) {
13167 this.lexer.parse(code);
14167 this.tree = this.program();
15141 return this.tree;
16 },
17 ast: function() {
184 return this.tree;
19 },
20 init: function(lexer) {
21203 this.look = null;
22203 this.tokens = null;
23203 this.lastLine = 1;
24203 this.lastCol = 1;
25203 this.line = 1;
26203 this.col = 1;
27203 this.index = 0;
28203 this.length = 0;
29203 this.ignores = {};
30203 this.hasMoveLine = false;
31203 this.tree = {};
32203 if(lexer) {
33137 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() {
43167 this.tokens = this.lexer.tokens();
44167 this.length = this.tokens.length;
45167 if(this.tokens.length) {
46165 this.move();
47 }
48167 var node = new Node(Node.PROGRAM);
49167 while(this.look) {
50198 node.add(this.element());
51 }
52141 return node;
53 },
54 element: function(allowSuper) {
555364 if(this.look.content() == 'function') {
56198 return this.fndecl();
57 }
58 else {
595166 return this.stmt(allowSuper);
60 }
61 },
62 stmt: function(allowSuper) {
6310852 if(!this.look) {
641 this.error();
65 }
6610851 switch(this.look.content()) {
67 case 'var':
681163 return this.varstmt();
69 case '{':
701791 return this.block();
71 case ';':
7235 return this.emptstmt();
73 case 'if':
741692 return this.ifstmt();
75 case 'do':
76 case 'while':
77 case 'for':
78458 return this.iterstmt();
79 case 'continue':
8018 return this.cntnstmt();
81 case 'break':
82123 return this.brkstmt();
83 case 'return':
841636 return this.retstmt();
85 case 'with':
861 return this.withstmt();
87 case 'switch':
888 return this.swchstmt();
89 case 'throw':
9039 return this.thrstmt();
91 case 'try':
9255 return this.trystmt();
93 case 'debugger':
941 return this.debstmt();
95 default:
963831 if(this.look.type() == Token.ID) {
972943 for(var i = this.index; i < this.length; i++) {
983955 var token = this.tokens[i];
993955 if(!S[token.type()]) {
1002940 if(token.content() == ':') {
1012 return this.labstmt();
102 }
103 else {
1042938 return this.exprstmt();
105 }
106 }
107 }
108 }
109891 return this.exprstmt();
110 }
111 },
112 exprstmt: function() {
1133829 var node = new Node(Node.EXPRSTMT);
1143829 node.add(this.expr(), this.match(';'));
1153819 return node;
116 },
117 varstmt: function(noSem) {
1181247 var node = new Node(Node.VARSTMT);
1191247 node.add(
120 this.match('var'),
121 this.vardecl()
122 );
1231244 while(this.look && this.look.content() == ',') {
1241555 node.add(
125 this.match(),
126 this.vardecl()
127 );
128 }
1291244 if(!noSem) {
1301160 node.add(this.match(';'));
131 }
1321244 return node;
133 },
134 vardecl: function() {
1352802 var node = new Node(Node.VARDECL);
1362802 if(!this.look) {
1371 this.error('missing variable name');
138 }
1392801 node.add(this.match(Token.ID, 'missing variable name'));
1402801 if(this.look && this.look.content() == '=') {
1411919 node.add(this.assign());
142 }
1432799 return node;
144 },
145 assign: function() {
1461919 var node = new Node(Node.ASSIGN);
1471919 node.add(this.match('='));
1481919 if(!this.look) {
1491 this.error();
150 }
1511918 node.add(this.assignexpr());
1521917 return node;
153 },
154 block: function() {
1551906 var node = new Node(Node.BLOCK);
1561906 node.add(this.match('{'));
1571906 while(this.look && this.look.content() != '}') {
1582988 node.add(this.stmt());
159 }
1601906 node.add(this.match('}', 'missing } in compound statement'));
1611906 return node;
162 },
163 emptstmt: function() {
16435 var node = new Node(Node.EMPTSTMT);
16535 node.add(this.match(';'));
16635 return node;
167 },
168 ifstmt: function() {
1691692 var node = new Node(Node.IFSTMT);
1701692 node.add(
171 this.match('if'),
172 this.match('('),
173 this.expr(),
174 this.match(')'),
175 this.stmt()
176 );
1771691 if(this.look && this.look.content() == 'else') {
178325 node.add(
179 this.match('else'),
180 this.stmt()
181 );
182 }
1831691 return node;
184 },
185 iterstmt: function() {
186458 var node = new Node(Node.ITERSTMT);
187458 switch(this.look.content()) {
188 case 'do':
1899 node.add(
190 this.match(),
191 this.stmt(),
192 this.match('while'),
193 this.match('('),
194 this.expr(),
195 this.match(')'),
196 this.match(';')
197 );
1989 break;
199 case 'while':
200152 node.add(
201 this.match(),
202 this.match('('),
203 this.expr(),
204 this.match(')'),
205 this.stmt()
206 );
207152 break;
208 case 'for':
209297 node.add(
210 this.match(),
211 this.match('(')
212 );
213297 if(!this.look) {
2141 this.error();
215 }
216296 if(this.look.content() == 'var' || this.look.content() == 'let') {
21784 var node2 = this.look.content() == 'var' ? this.varstmt(true) : this.letstmt(true);
21884 if(!this.look) {
2191 this.error('missing ; after for-loop initializer');
220 }
22183 if(this.look.content() == 'in') {
22219 if(node2.leaves().length > 2) {
2231 this.error('invalid for/in left-hand side');
224 }
22518 node.add(node2);
22618 node.add(
227 this.match(),
228 this.expr()
229 );
230 }
231 else {
23264 node.add(node2);
23364 node.add(this.match(';'));
23464 if(this.look.content() != ';') {
23563 node.add(this.expr());
236 }
23763 node.add(this.match(';'));
23863 if(!this.look) {
2391 this.error();
240 }
24162 if(this.look.content() != ')') {
24259 node.add(this.expr());
243 }
244 }
245 }
246 else {
247212 if(this.look.content() == 'in') {
2481 this.error();
249 }
250211 var hasIn = false;
251211 for(var i = this.index; i < this.length; i++) {
2521924 var t = this.tokens[i];
2531924 if(t.content() == 'in') {
25487 hasIn = true;
25587 break;
256 }
2571837 else if(t.content() == ')') {
258120 break;
259 }
260 }
261211 if(hasIn) {
26287 node.add(this.expr(true), this.match('in'), this.expr());
263 }
264 else {
265124 if(this.look.content() != ';') {
26668 node.add(this.expr());
267 }
268 //for的;不能省略,强制判断
269124 if(!this.look || this.look.content() != ';') {
2701 this.error('missing ;')
271 }
272123 node.add(this.match(';'));
273123 if(!this.look) {
2741 this.error();
275 }
276122 if(this.look.content() != ';') {
277121 node.add(this.expr());
278 }
279122 if(!this.look || this.look.content() != ';') {
2801 this.error('missing ;')
281 }
282121 node.add(this.match(';'));
283121 if(!this.look) {
2841 this.error();
285 }
286120 if(this.look.content() != ')') {
287112 node.add(this.expr());
288 }
289 }
290 }
291287 node.add(this.match(')'));
292287 node.add(this.stmt());
293 }
294448 return node;
295 },
296 cntnstmt: function() {
29718 var node = new Node(Node.CNTNSTMT);
29818 node.add(this.match('continue', true));
29918 if(this.look && this.look.type() == Token.ID) {
3001 node.add(this.match());
301 }
30218 node.add(this.match(';'));
30318 return node;
304 },
305 brkstmt: function() {
306123 var node = new Node(Node.BRKSTMT);
307123 node.add(this.match('break', true));
308123 if(this.look && this.look.type() == Token.ID) {
3091 node.add(this.match());
310 }
311123 node.add(this.match(';'));
312123 return node;
313 },
314 retstmt: function() {
3151636 var node = new Node(Node.RETSTMT);
3161636 node.add(this.match('return', true));
317 //return后换行视作省略;,包括多行注释的换行
3181636 if(this.look) {
3191635 if(this.look.content() == ';'
320 || this.look.content() == '}'
321 || this.look.type() == Token.LINE
322 || this.look.type() == Token.COMMENT) {
32382 node.add(this.match(';'));
324 }
325 else {
3261553 node.add(this.expr(), this.match(';'));
327 }
328 }
329 else {
3301 node.add(this.match(';'));
331 }
3321636 return node;
333 },
334 withstmt: function() {
3351 var node = new Node(Node.WITHSTMT);
3361 node.add(
337 this.match('with'),
338 this.match('('),
339 this.expr(),
340 this.match(')'),
341 this.stmt()
342 );
3431 return node;
344 },
345 swchstmt: function() {
3468 var node = new Node(Node.SWCHSTMT);
3478 node.add(
348 this.match('switch'),
349 this.match('('),
350 this.expr(),
351 this.match(')'),
352 this.caseblock()
353 );
3547 return node;
355 },
356 caseblock: function() {
3578 var node = new Node(Node.CASEBLOCK);
3588 node.add(this.match('{'));
3598 while(this.look && this.look.content() != '}') {
360102 if(this.look.content() == 'case') {
36199 node.add(this.caseclause());
362 }
3633 else if(this.look.content() == 'default') {
3642 node.add(this.dftclause());
365 }
366 else {
3671 this.error('invalid switch statement');
368 }
369 }
3707 node.add(this.match('}'));
3717 return node;
372 },
373 caseclause: function() {
37499 var node = new Node(Node.CASECLAUSE);
37599 node.add(
376 this.match('case'),
377 this.expr(),
378 this.match(':')
379 );
38099 while(this.look
381 && this.look.content() != 'case'
382 && this.look.content() != 'default'
383 && this.look.content() != '}') {
384228 node.add(this.stmt());
385 }
38699 return node;
387 },
388 dftclause: function() {
3892 var node = new Node(Node.DFTCLAUSE);
3902 node.add(
391 this.match('default'),
392 this.match(':')
393 );
3942 while(this.look && this.look.content() != '}') {
3952 node.add(this.stmt());
396 }
3972 return node;
398 },
399 labstmt: function() {
4002 var node = new Node(Node.LABSTMT);
4012 node.add(
402 this.match(Token.ID),
403 this.match(':'),
404 this.stmt()
405 );
4062 return node;
407 },
408 thrstmt: function() {
40939 var node = new Node(Node.THRSTMT);
41039 node.add(
411 this.match('throw', true),
412 this.expr(),
413 this.match(';')
414 );
41539 return node;
416 },
417 trystmt: function() {
41855 var node = new Node(Node.TRYSTMT);
41955 node.add(
420 this.match('try'),
421 this.block()
422 );
42355 if(this.look && this.look.content() == 'catch') {
42454 node.add(this.cach());
42554 if(this.look && this.look.content() == 'finally') {
4265 node.add(this.finl());
427 }
428 }
429 else {
4301 node.add(this.finl());
431 }
43255 return node;
433 },
434 debstmt: function() {
4351 var node = new Node(Node.DEBSTMT);
4361 node.add(this.match('debugger'), this.match(';'));
4371 return node;
438 },
439 cach: function() {
44054 var node = new Node(Node.CACH);
44154 node.add(
442 this.match('catch'),
443 this.match('('),
444 this.match(Token.ID, 'missing identifier in catch'),
445 this.match(')'),
446 this.block()
447 );
44854 return node;
449 },
450 finl: function() {
4516 var node = new Node(Node.FINL);
4526 node.add(
453 this.match('finally'),
454 this.block()
455 );
4566 return node;
457 },
458 fndecl: function() {
459198 var node = new Node(Node.FNDECL);
460198 node.add(
461 this.match('function'),
462 this.match(Token.ID, 'function statement requires a name'),
463 this.match('(')
464 );
465198 if(!this.look) {
4661 this.error('missing formal parameter');
467 }
468197 if(this.look.content() != ')') {
469154 node.add(this.fnparams());
470 }
471197 node.add(
472 this.match(')'),
473 this.match('{'),
474 this.fnbody(),
475 this.match('}')
476 );
477197 return node;
478 },
479 fnexpr: function() {
4801625 var node = new Node(Node.FNEXPR);
4811625 node.add(this.match('function'));
4821625 if(!this.look) {
4831 this.error('missing formal parameter');
484 }
4851624 if(this.look.type() == Token.ID) {
48622 node.add(this.match());
487 }
4881624 node.add(this.match('('));
4891624 if(!this.look) {
4901 this.error();
491 }
4921623 if(this.look.content() != ')') {
4931147 node.add(this.fnparams());
494 }
4951622 node.add(
496 this.match(')'),
497 this.match('{'),
498 this.fnbody(),
499 this.match('}', 'missing } in compound statement')
500 );
5011621 return node;
502 },
503 fnparams: function() {
5041301 var node = new Node(Node.FNPARAMS);
5051301 while(this.look && this.look.content() != ')') {
5062282 node.add(this.match(Token.ID, 'missing formal parameter'));
5072281 if(this.look && this.look.content() == ',') {
508982 node.add(this.match());
509 }
510 }
5111300 return node;
512 },
513 fnbody: function(allowSuper) {
5141818 var node = new Node(Node.FNBODY);
5151818 while(this.look && this.look.content() != '}') {
5165166 node.add(this.element(allowSuper));
517 }
5181818 return node;
519 },
520 expr: function(noIn) {
52111058 var node = new Node(Node.EXPR),
522 assignexpr = this.assignexpr(noIn);
52311044 if(this.look && this.look.content() == ',') {
524540 node.add(assignexpr);
525540 while(this.look && this.look.content() == ',') {
5261051 node.add(this.match(), this.assignexpr(noIn));
527 }
528 }
529 else {
53010504 return assignexpr;
531 }
532540 return node;
533 },
534 assignexpr: function(noIn) {
53530764 var node = new Node(Node.ASSIGNEXPR),
536 cndt = this.cndtexpr(noIn);
53730749 if(this.look && {
538 '*=': true,
539 '/=': true,
540 '%=': true,
541 '+=': true,
542 '-=': true,
543 '<<=': true,
544 '>>=': true,
545 '>>>=': true,
546 '&=': true,
547 '^=': true,
548 '|=': true,
549 '=': true
550 }.hasOwnProperty(this.look.content())) {
5513675 node.add(cndt, this.match(), this.assignexpr(noIn));
552 }
553 else {
55427074 return cndt;
555 }
5563675 return node;
557 },
558 cndtexpr: function(noIn) {
55930764 var node = new Node(Node.CNDTEXPR),
560 logorexpr = this.logorexpr(noIn);
56130749 if(this.look && this.look.content() == '?') {
562878 node.add(
563 logorexpr,
564 this.match(),
565 this.assignexpr(noIn),
566 this.match(':'),
567 this.assignexpr(noIn)
568 );
569 }
570 else {
57129871 return logorexpr;
572 }
573878 return node;
574 },
575 logorexpr: function(noIn) {
57630764 var node = new Node(Node.LOGOREXPR),
577 logandexpr = this.logandexpr(noIn);
57830749 if(this.look && this.look.content() == '||') {
5791015 node.add(logandexpr);
5801015 while(this.look && this.look.content() == '||') {
5811150 node.add(
582 this.match(),
583 this.logandexpr(noIn)
584 );
585 }
586 }
587 else {
58829734 return logandexpr;
589 }
5901015 return node;
591 },
592 logandexpr: function(noIn) {
59331914 var node = new Node(Node.LOGANDEXPR),
594 bitorexpr = this.bitorexpr(noIn);
59531899 if(this.look && this.look.content() == '&&') {
5961129 node.add(bitorexpr);
5971129 while(this.look && this.look.content() == '&&') {
5981375 node.add(
599 this.match(),
600 this.bitorexpr(noIn)
601 );
602 }
603 }
604 else {
60530770 return bitorexpr;
606 }
6071129 return node;
608 },
609 bitorexpr: function(noIn) {
61033289 var node = new Node(Node.BITOREXPR),
611 bitxorexpr = this.bitxorexpr(noIn);
61233274 if(this.look && this.look.content() == '|') {
6135 node.add(bitxorexpr);
6145 while(this.look && this.look.content() == '|') {
6155 node.add(
616 this.match(),
617 this.bitxorexpr(noIn)
618 );
619 }
620 }
621 else {
62233269 return bitxorexpr;
623 }
6245 return node;
625 },
626 bitxorexpr: function(noIn) {
62733294 var node = new Node(Node.BITXOREXPR),
628 bitandexpr = this.bitandexpr(noIn);
62933279 if(this.look && this.look.content() == '^') {
6302 node.add(bitandexpr);
6312 while(this.look && this.look.content() == '^') {
6322 node.add(
633 this.match(),
634 this.bitandexpr(noIn)
635 );
636 }
637 }
638 else {
63933277 return bitandexpr;
640 }
6412 return node;
642 },
643 bitandexpr: function(noIn) {
64433296 var node = new Node(Node.BITANDEXPR),
645 eqexpr = this.eqexpr(noIn);
64633281 if(this.look && this.look.content() == '&') {
64718 node.add(eqexpr);
64818 while(this.look && this.look.content() == '&') {
64918 node.add(
650 this.match(),
651 this.eqexpr(noIn)
652 );
653 }
654 }
655 else {
65633263 return eqexpr;
657 }
65818 return node;
659 },
660 eqexpr: function(noIn) {
66133314 var node = new Node(Node.EQEXPR),
662 reltexpr = this.reltexpr(noIn);
66333299 if(this.look && {
664 '==': true,
665 '===': true,
666 '!==': true,
667 '!=': true
668 }.hasOwnProperty(this.look.content())) {
6691589 node.add(reltexpr);
6701589 while(this.look && {
671 '==': true,
672 '===': true,
673 '!==': true,
674 '!=': true
675 }.hasOwnProperty(this.look.content())) {
6761590 node.add(
677 this.match(),
678 this.reltexpr(noIn)
679 );
680 }
681 }
682 else {
68331710 return reltexpr;
684 }
6851589 return node;
686 },
687 reltexpr: function(noIn) {
68834904 var node = new Node(Node.RELTEXPR),
689 shiftexpr = this.shiftexpr();
69034889 if(this.look && ({
691 '<': true,
692 '>': true,
693 '>=': true,
694 '<=': true,
695 'instanceof': true
696 }.hasOwnProperty(this.look.content()) || (!noIn && this.look.content() == 'in'))) {
697477 node.add(shiftexpr);
698477 while(this.look && ({
699 '<': true,
700 '>': true,
701 '>=': true,
702 '<=': true,
703 'instanceof': true
704 }.hasOwnProperty(this.look.content()) || (!noIn && this.look.content() == 'in'))) {
705477 node.add(
706 this.match(),
707 this.shiftexpr()
708 );
709 }
710 }
711 else {
71234412 return shiftexpr;
713 }
714477 return node;
715 },
716 shiftexpr: function() {
71735381 var node = new Node(Node.SHIFTEXPR),
718 addexpr = this.addexpr();
71935366 if(this.look && ['<<', '>>', '>>>'].indexOf(this.look.content()) != -1) {
7208 node.add(addexpr);
7218 while(this.look && ['<<', '>>', '>>>'].indexOf(this.look.content()) != -1) {
7229 node.add(
723 this.match(),
724 this.addexpr()
725 );
726 }
727 }
728 else {
72935358 return addexpr;
730 }
7318 return node;
732 },
733 addexpr: function() {
73435390 var node = new Node(Node.ADDEXPR),
735 mtplexpr = this.mtplexpr();
73635375 if(this.look && ['+', '-'].indexOf(this.look.content()) != -1) {
737777 node.add(mtplexpr);
738777 while(this.look && ['+', '-'].indexOf(this.look.content()) != -1) {
7391318 node.add(
740 this.match(),
741 this.mtplexpr()
742 );
743 }
744 }
745 else {
74634598 return mtplexpr;
747 }
748777 return node;
749 },
750 mtplexpr: function() {
75136708 var node = new Node(Node.MTPLEXPR),
752 unaryexpr = this.unaryexpr();
75336693 if(this.look && ['*', '/', '%'].indexOf(this.look.content()) != -1) {
75451 node.add(unaryexpr);
75551 while(this.look && ['*', '/', '%'].indexOf(this.look.content()) != -1) {
75652 node.add(
757 this.match(),
758 this.unaryexpr()
759 );
760 }
761 }
762 else {
76336642 return unaryexpr;
764 }
76551 return node;
766 },
767 unaryexpr: function() {
76838478 var node = new Node(Node.UNARYEXPR);
76938478 if(!this.look) {
7702 this.error();
771 }
77238476 switch(this.look.content()) {
773 case 'delete':
774 case 'void':
775 case 'typeof':
776 case '++':
777 case '--':
778 case '+':
779 case '-':
780 case '~':
781 case '!':
7821718 node.add(
783 this.match(),
784 this.unaryexpr()
785 );
7861716 break;
787 default:
78836758 return this.postfixexpr();
789 }
7901716 return node;
791 },
792 postfixexpr: function() {
79336758 var node = new Node(Node.POSTFIXEXPR);
79436758 var leftexpr = this.leftexpr();
79536745 if(this.look && ['++', '--'].indexOf(this.look.content()) > -1 && !this.hasMoveLine) {
796327 node.add(leftexpr);
797327 while(this.look && ['++', '--'].indexOf(this.look.content()) > -1) {
798327 node.add(this.match(undefined, true));
799 }
800 }
801 else {
80236418 return leftexpr;
803 }
804327 return node;
805 },
806 leftexpr: function() {
80736758 if(this.look.content() == 'new') {
808190 return this.newexpr();
809 }
810 else {
81136568 return this.callexpr();
812 }
813 },
814 newexpr: function(depth) {
815191 depth = depth || 0;
816191 var node = new Node(Node.NEWEXPR);
817191 node.add(this.match('new'));
818191 if(!this.look) {
8191 this.error();
820 }
821190 if(this.look.content() == 'new') {
8221 node.add(this.newexpr(depth + 1));
823 }
824 else {
825189 node.add(this.mmbexpr());
826 }
827190 if(this.look && this.look.content() == '(') {
828179 node.add(this.args());
829 }
830190 if(this.look && ['.', '['].indexOf(this.look.content()) > -1) {
83111 var mmb = new Node(Node.MMBEXPR);
83211 mmb.add(node);
83311 while(this.look) {
83420 if(this.look.content() == '.') {
83510 mmb.add(
836 this.match(),
837 this.match(Token.ID)
838 );
839 }
84010 else if(this.look.content() == '[') {
8411 mmb.add(
842 this.match(),
843 this.expr(),
844 this.match(']')
845 );
846 }
847 else {
8489 break;
849 }
850 }
85111 if(depth == 0 && this.look && this.look.content() == '(') {
8528 var callexpr = this.callexpr(mmb);
8538 return callexpr;
854 }
8553 return mmb;
856 }
857179 return node;
858 },
859 callexpr: function(mmb) {
86036576 var node = new Node(Node.CALLEXPR);
86136576 mmb = mmb || this.mmbexpr();
86236565 if(this.look && this.look.content() == '(') {
8635156 node.add(
864 mmb,
865 this.args()
866 );
8675155 if(this.look && ['.', '[', '('].indexOf(this.look.content()) > -1) {
868364 while(this.look) {
8691186 if(this.look.content() == '.') {
870426 node.add(
871 this.match(),
872 this.match(Token.ID)
873 );
874 }
875760 else if(this.look.content() == '[') {
87645 node.add(
877 this.match(),
878 this.expr(),
879 this.match(']')
880 );
881 }
882715 else if(this.look.content() == '(') {
883351 node.add(this.args());
884 }
885 else {
886364 break;
887 }
888 }
889 }
890 }
891 else {
89231409 return mmb;
893 }
8945155 return node;
895 },
896 mmbexpr: function() {
89736757 var node = new Node(Node.MMBEXPR);
89836757 var mmb;
89936757 if(this.look.content() == 'function') {
9001625 mmb = this.fnexpr();
901 }
902 else {
90335132 mmb = this.prmrexpr();
904 }
90536746 if(this.look && ['.', '['].indexOf(this.look.content()) > -1) {
90610737 node.add(mmb);
90710737 while(this.look) {
90823519 if(this.look.content() == '.') {
90910859 node.add(
910 this.match(),
911 this.match(Token.ID)
912 );
913 }
91412660 else if(this.look.content() == '[') {
9151928 node.add(
916 this.match(),
917 this.expr(),
918 this.match(']')
919 );
920 }
921 else {
92210732 break;
923 }
924 }
925 }
926 else {
92726009 return mmb;
928 }
92910737 return node;
930 },
931 prmrexpr: function() {
93235132 var node = new Node(Node.PRMREXPR);
93335132 switch(this.look.type()) {
934 case Token.ID:
935 case Token.NUMBER:
936 case Token.STRING:
937 case Token.REG:
938 case Token.TEMPLATE:
93929061 node.add(this.match());
94029061 break;
941 default:
9426071 switch(this.look.content()) {
943 case 'this':
944 case 'null':
945 case 'true':
946 case 'false':
9473381 node.add(this.match());
9483381 break;
949 case '(':
9501087 node.add(this.match(), this.expr(), this.match(')'));
9511083 break;
952 case '[':
953870 node.add(this.arrltr());
954869 break;
955 case '{':
956732 node.add(this.objltr());
957731 break;
958 default:
9591 this.error();
960 }
961 }
96235125 return node;
963 },
964 arrltr: function() {
965870 var node = new Node(Node.ARRLTR);
966870 node.add(this.match('['));
967870 while(this.look && this.look.content() != ']') {
9682380 if(this.look.content() == ',') {
969874 node.add(this.match());
970 }
971 else {
9721506 node.add(this.assignexpr());
973 }
974 }
975870 node.add(this.match(']', 'missing ] after element list'));
976869 return node;
977 },
978 objltr: function() {
979732 var node = new Node(Node.OBJLTR);
980732 node.add(this.match('{'));
981732 while(this.look && this.look.content() != '}') {
9822006 node.add(this.proptassign());
9832005 if(this.look && this.look.content() == ',') {
9841513 node.add(this.match());
985 }
986 }
987731 node.add(this.match('}', 'missing } after property list'));
988731 return node;
989 },
990 proptassign: function() {
9912006 var node = new Node(Node.PROPTASSIGN);
9922006 switch(this.look.type()) {
993 case Token.ID:
994 case Token.STRING:
995 case Token.NUMBER:
9962005 node.add(
997 this.match(),
998 this.match(':', 'missing : after property id'),
999 this.assignexpr()
1000 );
10012005 break;
1002 default:
10031 this.error('invalid property id');
1004 }
10052005 return node;
1006 },
1007 args: function() {
10085686 var node = new Node(Node.ARGS);
10095686 node.add(this.match('('));
10105686 if(!this.look) {
10111 this.error();
1012 }
10135685 if(this.look.content() != ')') {
10144947 node.add(this.arglist());
1015 }
10165685 node.add(this.match(')'));
10175685 return node;
1018 },
1019 arglist: function() {
10204947 var node = new Node(Node.ARGLIST);
10214947 while(this.look && this.look.content() != ')') {
10227795 node.add(this.assignexpr());
10237795 if(this.look && this.look.content() == ',') {
10242848 node.add(this.match());
1025 }
1026 }
10274947 return node;
1028 },
1029 virtual: function(s) {
10301578 return new Node(Node.TOKEN, new Token(Token.VIRTUAL, s));
1031 },
1032 match: function(type, line, msg) {
1033140035 if(typeof type == 'boolean') {
10340 msg = line;
10350 line = type;
10360 type = undefined;
1037 }
1038140035 if(typeof line != 'boolean') {
1039137892 line = false;
1040137892 msg = line;
1041 }
1042 //未定义为所有非空白token
1043140035 if(character.isUndefined(type)) {
104470720 if(this.look) {
104570720 var l = this.look;
104670720 this.move(line);
104770720 return new Node(Node.TOKEN, l);
1048 }
1049 else {
10500 this.error('syntax error' + (msg || ''));
1051 }
1052 }
1053 //或者根据token的type或者content匹配
105469315 else if(typeof type == 'string') {
1055 //特殊处理;,不匹配但有换行或者末尾时自动补全,还有受限行
105652683 if(type == ';'
1057 && (!this.look
1058 || (this.look.content() != type && this.hasMoveLine)
1059 || this.look.content() == '}')
1060 ) {
10611578 if(this.look && S[this.look.type()]) {
106236 this.move();
1063 }
10641578 return this.virtual(';');
1065 }
106651105 else if(this.look && this.look.content() == type) {
106751103 var l = this.look;
106851103 this.move(line);
106951103 return new Node(Node.TOKEN, l);
1070 }
1071 else {
10722 this.error('missing ' + type + (msg || ''));
1073 }
1074 }
107516632 else if(typeof type == 'number') {
107616632 if(this.look && this.look.type() == type) {
107716631 var l = this.look;
107816631 this.move(line);
107916631 return new Node(Node.TOKEN, l);
1080 }
1081 else {
10821 this.error('missing ' + Token.type(type) + (msg || ''));
1083 }
1084 }
1085 },
1086 move: function(line) {
1087138655 this.lastLine = this.line;
1088138655 this.lastCol = this.col;
1089 //遗留下来的换行符
1090138655 this.hasMoveLine = false;
1091138655 do {
1092247829 this.look = this.tokens[this.index++];
1093247829 if(!this.look) {
1094159 return;
1095 }
1096 //存下忽略的token
1097247670 if(S[this.look.type()]) {
1098109210 this.ignores[this.index - 1] = this.look;
1099 }
1100 //包括line的情况下要跳出
1101247670 if(this.look.type() == Token.LINE) {
110217784 this.line++;
110317784 this.col = 1;
110417784 this.hasMoveLine = true;
110517784 if(line) {
110635 break;
1107 }
1108 }
1109229886 else if(this.look.type() == Token.COMMENT) {
11102742 var s = this.look.content();
11112742 var n = character.count(this.look.content(), character.LINE);
11122742 if(n > 0) {
111390 this.line += n;
111490 var i = s.lastIndexOf(character.LINE);
111590 this.col += s.length - i - 1;
111690 this.hasMoveLine = true;
111790 if(line) {
11181 break;
1119 }
1120 }
1121 }
1122 else {
1123227144 this.col += this.look.content().length;
1124227144 if(!S[this.look.type()]) {
1125138460 break;
1126 }
1127 }
1128 } while(this.index <= this.length);
1129 },
1130 error: function(msg) {
113125 msg = 'SyntaxError: ' + (msg || ' syntax error');
113225 throw new Error(msg + ' line ' + this.lastLine + ' col ' + this.lastCol);
1133 },
1134 ignore: function() {
11358 return this.ignores;
1136 }
1137});
11381module.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) {
18179 fn.prototype[k] = o[k];
19 });
2016 return fn;
21 };
2219 fn.statics = function(o) {
238 Object.keys(o).forEach(function(k) {
24146 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) {
37456707 var count = 0,
38 i = -1;
39456707 while((i = s.indexOf(c, i + 1)) != -1) {
4033562 count++;
41 }
42456707 return count;
43};
441exports.isUndefined = function(s) {
454387613 return typeof s == 'undefined';
46};
471exports.isString = function(s) {
480 return Object.prototype.toString.call(s) == "[object String]";
49};
501exports.isNumber = function(s) {
51456037 return Object.prototype.toString.call(s) == "[object Number]";
52};