Coverage

93%
1264
1188
76

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

97%
126
123
3
LineHitsSource
11var Class = require('../util/Class');
21var character = require('../util/character');
31var Token = require('./Token');
41var Lexer = Class(function(rule) {
5207 this.rule = rule; //当前语法规则
6207 this.init();
7}).methods({
8 init: function() {
9208 this.code = ''; //要解析的代码
10208 this.peek = ''; //向前看字符
11208 this.index = 0; //向前看字符字符索引
12208 this.isReg = Lexer.IS_REG; //当前/是否是perl风格正则表达式
13208 this.tokenList = []; //结果的token列表
14208 this.parentheseState = false; //(开始时标记之前终结符是否为if/for/while等关键字
15208 this.parentheseStack = []; //圆括号深度记录当前是否为if/for/while等语句内部
16208 this.cacheLine = 0; //行缓存值
17208 this.totalLine = 1; //总行数
18208 this.colNum = 0; //列
19208 this.colMax = 0; //最大列数
20 },
21 parse: function(code) {
22181 this.code = code || '';
23181 var temp = [];
24181 this.scan(temp);
25176 return temp;
26 },
27 parseOn: function() {
281 var temp = [];
291 this.scan(temp);
301 return temp;
31 },
32 tokens: function() {
33154 return this.tokenList;
34 },
35 scan: function(temp) {
36182 var perlReg = this.rule.perlReg();
37182 var length = this.code.length;
38182 var count = 0;
39 outer:
40 while(this.index < length) {
41448690 if(this.cacheLine > 0 && count >= this.cacheLine) {
421 break;
43 }
44448689 this.readch();
45 //perl风格正则
46448689 if(perlReg && this.isReg == Lexer.IS_REG && this.peek == character.SLASH && !{ '/': true, '*': true }[this.code.charAt(this.index)]) {
47484 this.dealReg(temp, length);
48482 this.isReg = Lexer.NOT_REG;
49 }
50 //依次遍历匹配规则,命中则继续
51 else {
52448205 for(var i = 0, matches = this.rule.matches(), len = matches.length; i < len; i++) {
537357053 var match = matches[i];
547357053 if(match.match(this.peek, this.code, this.index)) {
55448204 var token = new Token(match.tokenType(), match.content(), match.val(), this.index - 1);
56448204 var error = match.error();
57448204 var matchLen = match.content().length;
58448204 if(token.type() == Token.ID && this.rule.keyWords().hasOwnProperty(token.content())) {
5921148 token.type(Token.KEYWORD);
60 }
61448204 temp.push(token);
62448204 this.tokenList.push(token);
63448204 this.index += matchLen - 1;
64448204 var n = character.count(token.val(), character.LINE);
65448204 count += n;
66448204 this.totalLine += n;
67448204 if(n) {
6831682 var j = match.content().indexOf(character.LINE);
6931682 var k = match.content().lastIndexOf(character.LINE);
7031682 this.colMax = Math.max(this.colMax, this.colNum + j);
7131682 this.colNum = match.content().length - k;
72 }
73 else {
74416522 this.colNum += matchLen;
75 }
76448204 this.colMax = Math.max(this.colMax, this.colNum);
77448204 if(error) {
783 this.error(error, this.code.slice(this.index - matchLen, this.index));
79 }
80 //支持perl正则需判断关键字、圆括号对除号语义的影响
81448202 if(perlReg && match.perlReg() != Lexer.IGNORE) {
82255323 if(match.perlReg() == Lexer.SPECIAL) {
8394889 this.isReg = match.special();
84 }
85 else {
86160434 this.isReg = match.perlReg();
87 }
88255323 if(this.peek == character.LEFT_PARENTHESE) {
8919650 this.parentheseStack.push(this.parentheseState);
9019650 this.parentheseState = false;
91 }
92235673 else if(this.peek == character.RIGHT_PARENTHESE) {
9319634 this.isReg = this.parentheseStack.pop() ? Lexer.IS_REG : Lexer.NOT_REG;
94 }
95 else {
96216039 this.parentheseState = match.parenthese();
97 }
98 }
99448202 continue outer;
100 }
101 }
102 //如果有未匹配的,说明规则不完整,加入other类型并抛出警告
1031 this.error('unknow token');
104 }
105 }
106177 return this;
107 },
108 readch: function() {
109457489 this.peek = this.code.charAt(this.index++);
110 },
111 dealReg: function(temp, length) {
112484 var lastIndex = this.index - 1;
113484 var res = false;
114 outer:
115 do {
1166770 this.readch();
1176770 if(this.peek == character.LINE) {
1180 break;
119 }
1206770 else if(this.peek == character.BACK_SLASH) {
121490 this.index++;
122 }
1236280 else if(this.peek == character.LEFT_BRACKET) {
124259 do {
1251345 this.readch();
1261345 if(this.peek == character.LINE) {
1270 break outer;
128 }
1291345 else if(this.peek == character.BACK_SLASH) {
130265 this.index++;
131 }
1321080 else if(this.peek == character.RIGHT_BRACKET) {
133259 continue outer;
134 }
135 } while(this.index < length);
136 }
1376021 else if(this.peek == character.SLASH) {
138483 res = true;
139483 var hash = {};
140483 var flag = {
141 'g': true,
142 'i': true,
143 'm': true,
144 'y': true
145 };
146 //正则的flag中有gimy4种,大小写敏感且不能重复
147483 do {
148685 this.readch();
149685 if(character.isLetter(this.peek)) {
150203 if(hash.hasOwnProperty(this.peek) || !flag.hasOwnProperty(this.peek)) {
1511 this.error('SyntaxError: invalid regular expression flag ' + this.peek, this.code.slice(lastIndex, this.index));
1520 break outer;
153 }
154202 hash[this.peek] = true;
155 }
156 else {
157482 break outer;
158 }
159 } while(this.index <= length);
160 }
161 } while(this.index < length);
162483 if(!res) {
1631 this.error('SyntaxError: unterminated regular expression literal', this.code.slice(lastIndex, this.index - 1));
164 }
165482 var token = new Token(Token.REG, this.code.slice(lastIndex, --this.index), lastIndex);
166482 temp.push(token);
167482 this.tokenList.push(token);
168482 this.colNum += this.index - lastIndex;
169482 this.colMax = Math.max(this.colMax, this.colNum);
170482 return this;
171 },
172 cache: function(i) {
1731 if(!character.isUndefined(i) && i !== null) {
1741 this.cacheLine = i;
175 }
1761 return this.cacheLine;
177 },
178 finish: function() {
1792 return this.index >= this.code.length;
180 },
181 line: function() {
1827 return this.totalLine;
183 },
184 col: function() {
1851 return this.colMax;
186 },
187 error: function(s, str) {
1886 if(character.isUndefined(str)) {
1891 str = this.code.substr(this.index - 1, 20);
190 }
1916 if(Lexer.mode() === Lexer.STRICT) {
1925 throw new Error(s + ', line ' + this.line() + ' col ' + this.colNum + '\n' + str);
193 }
1941 else if(Lexer.mode() === Lexer.LOOSE && !character.isUndefined(console)) {
1951 if(console.warn) {
1961 console.warn(s + ', line ' + this.line() + ' col ' + this.colNum + '\n' + str);
197 }
198 }
1991 return this;
200 }
201}).statics({
202 IGNORE: 0,
203 IS_REG: 1,
204 NOT_REG: 2,
205 SPECIAL: 3,
206 STRICT: 0,
207 LOOSE: 1,
208 mode: function(i) {
20913 if(!character.isUndefined(i)) {
2104 cmode = i;
211 }
21213 return cmode;
213 }
214});
2151var cmode = Lexer.STRICT;
2161module.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) {
6450268 this.t = type; //token类型
7450268 this.c = content; //token的字面内容,string包括头尾的引号
8450268 if(character.isNumber(val)) {
9487 sIndex = val;
10487 val = content;
11 }
12449781 else if(character.isUndefined(val)) {
131577 val = content;
141577 sIndex = -1;
15 }
16450268 this.v = val; //token的值,一般情况下等于content,特殊如string情况下值是不加头尾的引号
17450268 this.id = tid++; //token的索引
18450268 this.si = sIndex; //token在源码字符串中的索引
19}).methods({
20 type: function(t) {
211603693 if(!character.isUndefined(t)) {
2221148 this.t = t;
23 }
241603693 return this.t;
25 },
26 content: function(c) {
271637819 if(!character.isUndefined(c)) {
281 this.c = c;
29 }
301637819 return this.c;
31 },
32 val: function(v) {
33448206 if(!character.isUndefined(v)) {
341 this.v = v;
35 }
36448206 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) {
794 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 }
874 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) {
4410 Match.call(this, type, setPReg);
5410 this.str = str;
6}).methods({
7 match: function(c, code, index) {
8139316 var isIn = this.str.indexOf(c) > -1;
9139316 if(isIn) {
10131855 this.result = c;
11 }
12139316 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) {
46893 Match.call(this, type, setPReg);
56893 this.result = result;
6}).methods({
7 match: function(c, code, index) {
85530965 return code.substr(--index, this.result.length) == this.result;
9 }
10});
111module.exports = CompleteEqual;

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

90%
32
29
3
LineHitsSource
11var Match = require('./Match');
21var Token = require('../Token');
31var character = require('../../util/character');
41var LineParse = Match.extend(function(type, begin, end, mutiline, setPReg) {
5619 if(character.isUndefined(mutiline)) {
60 mutiline = false;
7 }
8619 Match.call(this, type, setPReg);
9619 this.begin = begin;
10619 this.end = end;
11619 this.msg = null;
12619 this.mutiline = mutiline;
13 }).methods({
14 match: function(c, code, index) {
15754005 this.msg = null;
16754005 if(this.begin == code.charAt(index - 1)) {
176781 var len = code.length,
18 lastIndex = index - 1,
19 res = false;
206781 while(index < len) {
2157017 var c = code.charAt(index++);
22 //转义
2357017 if(c == character.BACK_SLASH) {
24401 if(code.charAt(index++) == character.ENTER) {
250 index++;
26 }
27 }
2856616 else if(c == character.LINE && !this.mutiline) {
290 break;
30 }
3156616 else if(c == this.end) {
326780 res = true;
336780 break;
34 }
35 }
366781 if(!res) {
371 this.msg = 'SyntaxError: unterminated ' + Token.type(this.type).toLowerCase() + ' literal';
38 }
396781 this.result = code.slice(lastIndex, index);
406781 return true;
41 }
42747224 return false;
43 },
44 error: function() {
456781 return this.msg;
46 },
47 val: function() {
486781 return this.content().slice(this.begin.length, -this.end.length);
49 }
50 });
511module.exports = LineParse;

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

96%
25
24
1
LineHitsSource
11var Match = require('./Match');
21var Token = require('../Token');
31var character = require('../../util/character');
41var LineSearch = Match.extend(function(type, begin, end, contain, setPReg) {
5414 if(character.isUndefined(contain)) {
6207 contain = false;
7 }
8414 Match.call(this, type, setPReg);
9414 this.begin = begin;
10414 this.end = end;
11414 this.contain = contain;
12414 this.msg = null;
13}).methods({
14 match: function(c, code, index) {
15515657 this.msg = null;
16515657 if(this.begin == code.substr(--index, this.begin.length)) {
174792 var i = code.indexOf(this.end, index + this.begin.length);
184792 if(i == -1) {
191 if(this.contain) {
200 this.msg = 'SyntaxError: unterminated ' + Token.type(this.type).toLowerCase();
21 }
221 i = code.length;
23 }
244791 else if(this.contain) {
25213 i += this.end.length;
26 }
274792 this.result = code.slice(index, i);
284792 return true;
29 }
30510865 return false;
31 },
32 error: function() {
334792 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) {
59184 this.type = type;
69184 if(character.isUndefined(setPReg)) {
71599 setPReg = Lexer.IGNORE;
8 }
99184 this.setPReg = setPReg;
109184 this.result = null;
11 //忽略0,是1,否2,特殊3
129184 if(setPReg) {
137585 if(character.isUndefined(special)) {
147380 special = function() {
150 return Lexer.IGNORE;
16 };
17 }
187585 if(character.isUndefined(parenthese)) {
197380 parenthese = function() {
20121150 return false;
21 };
22 }
23 }
249184 this.special = special;
259184 this.parenthese = parenthese;
26}).methods({
27 tokenType: function() {
28448204 return this.type;
29 },
30 perlReg: function() {
31863959 return this.setPReg;
32 },
33 val: function() {
34441423 return this.content();
35 },
36 content: function() {
371629436 return this.result;
38 },
39 match: function(c, code, index) {
40 //需被实现
410 return false;
42 },
43 error: function() {
44334275 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) {
3848 if(typeof valid == 'number') {
4205 parenthese = special;
5205 special = setPReg;
6205 setPReg = valid;
7205 valid = null;
8 }
9848 Match.call(this, type, setPReg, special, parenthese);
10848 this.reg = reg;
11848 this.valid = valid;
12 }).methods({
13 match: function(c, code, index) {
14417110 var self = this,
15 res = self.reg.exec(code.slice(index - 1));
16417110 self.msg = null;
17417110 if(res) {
18102356 self.result = res[0];
19102356 if(self.valid) {
207467 for(var i = 0, keys = Object.keys(self.valid), len = keys.length; i < len; i++) {
217467 if(self.valid[keys[i]].test(self.result)) {
222 self.msg = keys[i];
232 break;
24 }
25 }
26 }
27102356 return true;
28 }
29314754 return false;
30 },
31 error: function() {
32102356 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() {
11205 var self = this;
12205 Rule.call(self, EcmascriptRule.KEYWORDS, true);
13
14205 self.addMatch(new CompleteEqual(Token.BLANK, character.BLANK));
15205 self.addMatch(new CompleteEqual(Token.TAB, character.TAB));
16205 self.addMatch(new CompleteEqual(Token.ENTER, character.ENTER));
17205 self.addMatch(new CompleteEqual(Token.LINE, character.LINE));
18
19205 self.addMatch(new LineSearch(Token.COMMENT, '//', '\n'));
20205 self.addMatch(new LineSearch(Token.COMMENT, '/*', '*/', true));
21205 self.addMatch(new LineParse(Token.STRING, '"', '"', false, Lexer.IS_REG));
22205 self.addMatch(new LineParse(Token.STRING, "'", "'", false, Lexer.IS_REG));
23205 self.addMatch(new LineParse(Token.TEMPLATE, '`', '`', true, Lexer.IS_REG));
24
25205 self.addMatch(new RegMatch(Token.ID, /^[$a-zA-Z_][$\w]*/, Lexer.SPECIAL, function() {
2694889 return !!(self.keyWords().hasOwnProperty(this.content()));
27 }, function() {
2894889 return ['if', 'for', 'while'].indexOf(this.content()) != -1;
29 }));
30
31205 self.addMatch(new RegMatch(Token.NUMBER, /^\.\d+(?:E[+-]?\d*)?/i, {
32 'SyntaxError: missing exponent': /E[+-]?$/i
33 }, Lexer.NOT_REG));
34
35205 self.addMatch(new CompleteEqual(Token.SIGN, ']', Lexer.NOT_REG));
36
37205 ['*=', '/=', '+=', '-=', '%=', '^=', '&=', '|=', '&&', '--', '++', '===', '==', '!==', '!=', '||', '>>>=', '<<<=', '<<<', '>>>', '>>=', '<<=', '<<', '>>', '>=', '<=', '...', '?:'].forEach(function(o) {
385740 self.addMatch(new CompleteEqual(Token.SIGN, o, Lexer.IS_REG));
39 });
40205 self.addMatch(new CharacterSet(Token.SIGN, ':;/?.,[]{}~!^|%=-+*()~><&\\', Lexer.IS_REG));
41
42205 self.addMatch(new RegMatch(Token.NUMBER, /^0x[\da-f]*/i, {
43 "SyntaxError: missing hexadecimal digits after '0x'": /^0x$/i
44 }, Lexer.NOT_REG));
45205 self.addMatch(new RegMatch(Token.NUMBER, /^\d+\.?\d*(?:E[+-]?\d*)?/i, {
46 'SyntaxError: missing exponent': /E[+-]?$/i
47 }, Lexer.NOT_REG));
48
49205 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 char 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) {
3207 var self = this;
4207 self.kw = {};
5207 words.forEach(function(o) {
612670 self.kw[o] = true;
7 });
8207 self.pReg = pReg || false;
9207 self.matchList = [];
10 }).methods({
11 perlReg: function() {
12182 return this.pReg;
13 },
14 addMatch: function(match) {
159184 this.matchList.push(match);
169184 return this;
17 },
18 matches: function() {
19448205 return this.matchList;
20 },
21 keyWords: function() {
22189778 return this.kw;
23 }
24 });
251module.exports = Rule;

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

86%
158
136
22
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) {
729 this.id = id++;
829 this.parser = new Parser();
929 this.parent = parent || null; //父上下文,如果是全局则为空
1029 this.name = name || null; //上下文名称,即函数名,函数表达式为空,全局也为空
1129 this.thisIs = null; //上下文环境中this的值,函数表达式中可能会赋值
1229 this.children = []; //函数声明或函数表达式所产生的上下文
1329 this.childrenMap = {}; //键是函数名,值是上下文,匿名函数表达式键为cid
1429 this.vars = []; //变量var声明
1529 this.varsMap = {}; //键为id字面量,值是它的token的节点
1629 this.params = []; //形参,函数上下文才有,即全局无
1729 this.paramsMap = {}; //键为id字面量,值是它的token的节点
1829 this.aParams = []; //实参,函数表达式才有
1929 this.vids = []; //上下文环境里用到的变量id
2029 this.vidsMap = {}; //键为id字面量,值是它的token的节点
2129 this.returns = []; //上下文环境里return语句
2229 if(!this.isTop()) {
2310 this.parent.addChild(this);
24 }
25}).methods({
26 parse: function(code) {
2715 var ast;
2815 if(code instanceof JsNode) {
290 ast = code;
30 }
31 else {
3215 ast = this.parser.parse(code);
33 }
3415 recursion(ast, this);
3515 return this;
36 },
37 getId: function() {
389 return this.id;
39 },
40 getName: function() {
4110 return this.name;
42 },
43 getParent: function() {
440 return this.parent;
45 },
46 getThis: function() {
470 return this.thisIs;
48 },
49 setThis: function(t) {
500 this.thisIs = t;
510 return this;
52 },
53 isTop: function() {
5429 return !this.parent;
55 },
56 isFnexpr: function() {
570 return !this.isTop() && !this.name;
58 },
59 hasParam: function(p) {
603 return this.paramsMap.hasOwnProperty(p);
61 },
62 getParams: function() {
630 return this.params;
64 },
65 addParam: function(p) {
66 //形参不可能重复,无需判断
673 this.paramsMap[p] = this.params.length;
683 this.params.push(p);
693 return this;
70 },
71 getAParams: function() {
721 return this.aParams;
73 },
74 addAParam: function(ap) {
753 this.aParams.push(ap);
763 return this;
77 },
78 getChild: function(name) {
794 return this.childrenMap[name];
80 },
81 getChildren: function() {
825 return this.children;
83 },
84 //通过name查找函数声明,id查找表达式
85 hasChild: function(name) {
8626 return this.childrenMap.hasOwnProperty(name);
87 },
88 addChild: function(child) {
8910 var name = child.getName();
90 //函数表达式名字为空用id删除
9110 if(name) {
926 this.delChild(name);
93 }
94 else {
954 this.delChild(child.getId());
96 }
9710 name = name || child.getId();
9810 this.childrenMap[name] = child;
9910 this.children.push(child);
10010 return this;
101 },
102 //name函数声明,id表达式
103 delChild: function(name) {
10420 if(this.hasChild(name)) {
1051 var i = this.children.indexOf(this.childrenMap[name]);
1061 this.children.splice(i, 1);
1071 delete this.childrenMap[name];
108 }
10920 return this;
110 },
111 hasVar: function(v) {
11221 return this.varsMap.hasOwnProperty(v);
113 },
114 addVar: function(node, assign) {
11513 var v = node.leaves()[0].token().content();
116 //赋值拥有最高优先级,会覆盖掉之前的函数声明和var
11713 if(assign) {
11810 this.delVar(v);
11910 this.delChild(v);
120 }
121 //仅仅是var声明无赋值,且已有过声明或函数,忽略之
1223 else if(this.hasVar(v) || this.hasChild(v)) {
1232 return this;
124 }
12511 this.varsMap[v] = node;
12611 this.vars.push(node);
12711 return this;
128 },
129 delVar: function(v) {
13010 if(this.hasVar(v)) {
1310 var i = this.vars.indexOf(this.varsMap[v]);
1320 this.vars.splice(i, 1);
1330 delete this.varsMap[v];
134 }
13510 return this;
136 },
137 getVars: function() {
1383 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) {
1487 return this.vidsMap.hasOwnProperty(v);
149 },
150 addVid: function(node) {
1518 var v = node.token().content();
1528 if(this.vidsMap.hasOwnProperty(v)) {
1530 return;
154 }
1558 this.vids.push(node);
1568 this.vidsMap[v] = node;
1578 return this;
158 },
159 getVids: function() {
1600 return this.vids;
161 }
162});
163
1641function recursion(node, context) {
165302 var isToken = node.name() == JsNode.TOKEN;
166302 var isVirtual = isToken && node.token().type() == Token.VIRTUAL;
167302 if(isToken) {
168175 if(!isVirtual) {
169171 var token = node.token();
170171 var s = token.content();
171171 if(s == 'return') {
1725 context.addReturn(node);
173 }
174 }
175 }
176 else {
177127 if(node.name() == JsNode.VARDECL) {
17813 vardecl(node, context);
179 }
180114 else if(node.name() == JsNode.FNDECL) {
1816 context = fndecl(node, context);
182 }
183108 else if(node.name() == JsNode.FNEXPR) {
1844 context = fnexpr(node, context);
185 }
186104 else if(node.name() == JsNode.PRMREXPR) {
18724 prmrexpr(node, context);
188 }
189127 node.leaves().forEach(function(leaf, i) {
190287 recursion(leaf, context);
191 });
192 }
193}
1941function vardecl(node, context) {
19513 var leaves = node.leaves();
19613 var assign = !!leaves[1];
19713 context.addVar(node, assign);
198}
1991function fndecl(node, context) {
2006 var v = node.leaves()[1].leaves().content();
2016 var child = new Context(context, v);
2026 var params = node.leaves()[3];
2036 if(params.name() == JsNode.FNPARAMS) {
2041 addParam(params, child);
205 }
2066 return child;
207}
2081function fnexpr(node, context) {
209 //函数表达式name为空
2104 var child = new Context(context);
211 //记录形参
2124 var params;
2134 var v = node.leaves()[1];
2144 if(v.name() == JsNode.TOKEN) {
2154 params = node.leaves()[3];
216 }
217 else {
2180 params = node.leaves()[2];
219 }
2204 if(params.name() == JsNode.FNPARAMS) {
2210 addParam(params, child);
222 }
223 //匿名函数检查实参传入情况
2244 var next = node.next();
225 //!function(){}()形式
2264 if(next && next.name() == JsNode.ARGS) {
2274 var leaves = next.leaves();
228 //长度2为()空参数,长度3有参数,第2个节点
2294 if(leaves.length == 3) {
2301 addAParam(leaves[1], child);
231 }
232 }
233 //(function(){})()形式
234 else {
2350 var prmr = node.parent();
2360 var prev = node.prev();
2370 if(prmr.name() == JsNode.PRMREXPR && prev && prev.name() == JsNode.TOKEN && prev.token().content() == '(') {
2380 next = prmr.next();
2390 if(next && next.name() == JsNode.ARGS) {
2400 var leaves = next.leaves();
241 //长度2为()空参数,长度3有参数,第2个节点
2420 if(leaves.length == 3) {
2430 addAParam(leaves[1], child);
244 }
245 }
246 }
247 }
2484 return child;
249}
250//支持es6
2511function addParam(params, child) {
2521 params.leaves().forEach(function(leaf, i) {
2536 if(leaf.name() == JsNode.TOKEN && leaf.token().content() != ',') {
2542 child.addParam(leaf.token().content());
255 }
2564 else if(leaf.name() == JsNode.RESTPARAM) {
2571 child.addParam(leaf.leaves()[1].token().content());
258 }
259 });
260}
2611function addAParam(params, child) {
2621 params.leaves().forEach(function(leaf, i) {
2635 if(i % 2 == 0) {
2643 child.addAParam(leaf);
265 }
266 });
267}
2681function prmrexpr(node, context) {
26924 var first = node.leaves()[0];
27024 if(first.name() == JsNode.TOKEN) {
27122 var token = first.token();
27222 if(token.type() == Token.ID) {
2738 context.addVid(first);
274 }
275 }
276}
277
2781module.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) {
3755349 this.type = type;
4755349 if(type == Node.TOKEN) {
5137126 this.children = children;
6 }
7618223 else if(Array.isArray(children)) {
80 this.children = children;
9 }
10 else {
11618223 this.children = children ? [children] : [];
12 }
13755349 this.p = null;
14755349 this.pr = null;
15755349 this.ne = null;
16755349 return this;
17}).methods({
18 name: function() {
19234688 return this.type;
20 },
21 leaves: function() {
2296697 return this.children;
23 },
24 add: function() {
25163216 var self = this;
26163216 var args = Array.prototype.slice.call(arguments, 0);
27163216 args.forEach(function(node) {
28233601 node.parent(self);
29233601 var last = self.children[self.children.length - 1];
30233601 if(last) {
31136983 last.next(node);
32136983 node.prev(last);
33 }
34233601 self.children.push(node);
35 });
36163216 return self;
37 },
38 token: function() {
39272512 return this.children;
40 },
41 parent: function(p) {
42233604 if(p) {
43233601 this.p = p;
44 }
45233604 return this.p;
46 },
47 prev: function(pr) {
48136985 if(pr) {
49136983 this.pr = pr;
50 }
51136985 return this.pr;
52 },
53 next: function(ne) {
54136988 if(ne) {
55136983 this.ne = ne;
56 }
57136988 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

94%
661
622
39
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) {
10172 this.init(lexer);
11}).methods({
12 parse: function(code) {
13154 this.lexer.parse(code);
14154 this.tree = this.program();
15128 return this.tree;
16 },
17 ast: function() {
182 return this.tree;
19 },
20 init: function(lexer) {
21173 this.look = null;
22173 this.tokens = null;
23173 this.lastLine = 1;
24173 this.lastCol = 1;
25173 this.line = 1;
26173 this.col = 1;
27173 this.index = 0;
28173 this.length = 0;
29173 this.ignores = {};
30173 this.hasMoveLine = false;
31173 this.tree = {};
32173 if(lexer) {
33143 this.lexer = lexer;
34 }
3530 else if(this.lexer) {
361 this.lexer.init();
37 }
38 else {
3929 this.lexer = new Lexer(new Rule());
40 }
41 },
42 program: function() {
43154 this.tokens = this.lexer.tokens();
44154 this.length = this.tokens.length;
45154 if(this.tokens.length) {
46152 this.move();
47 }
48154 var node = new Node(Node.PROGRAM);
49154 while(this.look) {
50181 node.add(this.element());
51 }
52128 return node;
53 },
54 element: function(allowSuper) {
555202 if(this.look.content() == 'function') {
56191 return this.fndecl();
57 }
585011 else if(this.look.content() == 'class') {
5910 return this.classdecl();
60 }
61 else {
625001 return this.stmt(allowSuper);
63 }
64 },
65 stmt: function(allowSuper) {
6610548 if(!this.look) {
671 this.error();
68 }
6910547 switch(this.look.content()) {
70 case 'let':
712 return this.letstmt();
72 case 'const':
732 return this.cststmt();
74 case 'var':
751124 return this.varstmt();
76 case '{':
771743 return this.block();
78 case ';':
7934 return this.emptstmt();
80 case 'if':
811659 return this.ifstmt();
82 case 'do':
83 case 'while':
84 case 'for':
85447 return this.iterstmt();
86 case 'continue':
8718 return this.cntnstmt();
88 case 'break':
89123 return this.brkstmt();
90 case 'return':
911611 return this.retstmt();
92 case 'with':
931 return this.withstmt();
94 case 'switch':
958 return this.swchstmt();
96 case 'throw':
9737 return this.thrstmt();
98 case 'try':
9955 return this.trystmt();
100 case 'debugger':
1011 return this.debstmt();
102 case 'super':
1030 if(!allowSuper) {
1040 this.error('super must in a class');
105 }
1060 return this.superstmt();
107 case 'import':
1080 return this.imptstmt();
109 default:
1103682 if(this.look.type() == Token.ID) {
1112860 for(var i = this.index; i < this.length; i++) {
1123861 var token = this.tokens[i];
1133861 if(!S[token.type()]) {
1142857 if(token.content() == ':') {
1152 return this.labstmt();
116 }
117 else {
1182855 return this.exprstmt();
119 }
120 }
121 }
122 }
123825 return this.exprstmt();
124 }
125 },
126 exprstmt: function() {
1273680 var node = new Node(Node.EXPRSTMT);
1283680 node.add(this.expr(), this.match(';'));
1293671 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) {
1661201 var node = new Node(Node.VARSTMT);
1671201 node.add(
168 this.match('var'),
169 this.vardecl()
170 );
1711199 while(this.look && this.look.content() == ',') {
1721542 node.add(
173 this.match(),
174 this.vardecl()
175 );
176 }
1771199 if(!noSem) {
1781122 node.add(this.match(';'));
179 }
1801199 return node;
181 },
182 vardecl: function() {
1832749 var node = new Node(Node.VARDECL);
1842749 if(!this.look) {
1851 this.error('missing variable name');
186 }
1872748 if(this.look.content() == '[') {
1880 node.add(this.arrltr());
189 }
190 else {
1912748 node.add(this.match(Token.ID, 'missing variable name'));
192 }
1932748 if(this.look && this.look.content() == '=') {
1941869 node.add(this.assign());
195 }
1962747 return node;
197 },
198 assign: function() {
1991869 var node = new Node(Node.ASSIGN);
2001869 node.add(this.match('='));
2011869 if(!this.look) {
2021 this.error();
203 }
2041868 node.add(this.assignexpr());
2051868 return node;
206 },
207 block: function() {
2081858 var node = new Node(Node.BLOCK);
2091858 node.add(this.match('{'));
2101858 while(this.look && this.look.content() != '}') {
2112902 node.add(this.stmt());
212 }
2131858 node.add(this.match('}', 'missing } in compound statement'));
2141858 return node;
215 },
216 emptstmt: function() {
21734 var node = new Node(Node.EMPTSTMT);
21834 node.add(this.match(';'));
21934 return node;
220 },
221 ifstmt: function() {
2221659 var node = new Node(Node.IFSTMT);
2231659 node.add(
224 this.match('if'),
225 this.match('('),
226 this.expr(),
227 this.match(')'),
228 this.stmt()
229 );
2301658 if(this.look && this.look.content() == 'else') {
231316 node.add(
232 this.match('else'),
233 this.stmt()
234 );
235 }
2361658 return node;
237 },
238 iterstmt: function() {
239447 var node = new Node(Node.ITERSTMT);
240447 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':
253149 node.add(
254 this.match(),
255 this.match('('),
256 this.expr(),
257 this.match(')'),
258 this.stmt()
259 );
260149 break;
261 case 'for':
262289 node.add(
263 this.match(),
264 this.match('(')
265 );
266289 if(!this.look) {
2671 this.error();
268 }
269288 if(this.look.content() == 'var' || this.look.content() == 'let') {
27077 var node2 = this.look.content() == 'var' ? this.varstmt(true) : this.letstmt(true);
27177 if(!this.look) {
2721 this.error('missing ; after for-loop initializer');
273 }
27476 if(this.look.content() == 'in') {
27518 if(node2.leaves().length > 2) {
2761 this.error('invalid for/in left-hand side');
277 }
27817 node.add(node2);
27917 node.add(
280 this.match(),
281 this.expr()
282 );
283 }
284 else {
28558 node.add(node2);
28658 node.add(this.match(';'));
28758 if(this.look.content() != ';') {
28857 node.add(this.expr());
289 }
29057 node.add(this.match(';'));
29157 if(!this.look) {
2921 this.error();
293 }
29456 if(this.look.content() != ')') {
29553 node.add(this.expr());
296 }
297 }
298 }
299 else {
300211 if(this.look.content() == 'in') {
3011 this.error();
302 }
303210 var hasIn = false;
304210 for(var i = this.index; i < this.length; i++) {
3051912 var t = this.tokens[i];
3061912 if(t.content() == 'in') {
30787 hasIn = true;
30887 break;
309 }
3101825 else if(t.content() == ')') {
311119 break;
312 }
313 }
314210 if(hasIn) {
31587 node.add(this.expr(true), this.match('in'), this.expr());
316 }
317 else {
318123 if(this.look.content() != ';') {
31967 node.add(this.expr());
320 }
321 //for的;不能省略,强制判断
322123 if(!this.look || this.look.content() != ';') {
3231 this.error('missing ;')
324 }
325122 node.add(this.match(';'));
326122 if(!this.look) {
3271 this.error();
328 }
329121 if(this.look.content() != ';') {
330120 node.add(this.expr());
331 }
332121 if(!this.look || this.look.content() != ';') {
3331 this.error('missing ;')
334 }
335120 node.add(this.match(';'));
336120 if(!this.look) {
3371 this.error();
338 }
339119 if(this.look.content() != ')') {
340111 node.add(this.expr());
341 }
342 }
343 }
344279 node.add(this.match(')'));
345279 node.add(this.stmt());
346 }
347437 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() {
3681611 var node = new Node(Node.RETSTMT);
3691611 node.add(this.match('return', true));
370 //return后换行视作省略;,包括多行注释的换行
3711611 if(this.look) {
3721610 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 {
3791528 node.add(this.expr(), this.match(';'));
380 }
381 }
382 else {
3831 node.add(this.match(';'));
384 }
3851611 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() {
46237 var node = new Node(Node.THRSTMT);
46337 node.add(
464 this.match('throw', true),
465 this.expr(),
466 this.match(';')
467 );
46837 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() {
5120 var node = new Node(Node.SUPERSTMT);
5130 node.add(this.match('super'));
5140 if(!this.look) {
5150 this.error();
516 }
5170 if(this.look.content() == '.') {
5180 while(this.look && this.look.content() == '.') {
5190 node.add(this.match());
5200 if(!this.look) {
5210 this.error();
522 }
5230 if(this.look.content() == 'super') {
5240 node.add(this.match());
525 }
526 else {
5270 break;
528 }
529 }
5300 if(this.look.content() != '(') {
5310 node.add(this.match(Token.ID));
5320 while(this.look && this.look.content() == '.') {
5330 node.add(this.match(), this.match(Token.ID));
534 }
535 }
536 }
5370 node.add(
538 this.args(),
539 this.match(';')
540 );
5410 return node;
542 },
543 imptstmt: function() {
5440 var node = new Node(Node.IMPTSTMT);
5450 return node;
546 },
547 fndecl: function() {
548191 var node = new Node(Node.FNDECL);
549191 node.add(
550 this.match('function'),
551 this.match(Token.ID, 'function statement requires a name'),
552 this.match('(')
553 );
554191 if(!this.look) {
5550 this.error('missing formal parameter');
556 }
557191 if(this.look.content() != ')') {
558154 node.add(this.fnparams());
559 }
560191 node.add(
561 this.match(')'),
562 this.match('{'),
563 this.fnbody(),
564 this.match('}')
565 );
566191 return node;
567 },
568 fnexpr: function() {
5691577 var node = new Node(Node.FNEXPR);
5701577 node.add(this.match('function'));
5711577 if(!this.look) {
5721 this.error('missing formal parameter');
573 }
5741576 if(this.look.type() == Token.ID) {
57516 node.add(this.match());
576 }
5771576 node.add(this.match('('));
5781576 if(!this.look) {
5791 this.error();
580 }
5811575 if(this.look.content() != ')') {
5821110 node.add(this.fnparams());
583 }
5841573 node.add(
585 this.match(')'),
586 this.match('{'),
587 this.fnbody(),
588 this.match('}', 'missing } in compound statement')
589 );
5901573 return node;
591 },
592 fnparams: function() {
5931265 var node = new Node(Node.FNPARAMS);
5941265 node.add(this.match(Token.ID, 'missing formal parameter'));
5951265 while(this.look && this.look.content() == ',') {
596972 node.add(this.match());
597972 if(!this.look) {
5981 this.error('missing formal parameter');
599 }
600971 if(this.look.type() == Token.ID) {
601967 node.add(this.match());
602967 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 }
6131263 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) {
6261769 var node = new Node(Node.FNBODY);
6271769 while(this.look && this.look.content() != '}') {
6285021 node.add(this.element(allowSuper));
629 }
6301769 return node;
631 },
632 classdecl: function() {
63310 var node = new Node(Node.CLASSDECL);
63410 node.add(this.match('class'), this.match(Token.ID));
63510 if(!this.look) {
6361 this.error();
637 }
6389 if(this.look.content() == 'extends') {
6391 node.add(this.heratige());
640 }
6419 node.add(
642 this.match('{'),
643 this.classbody(),
644 this.match('}')
645 );
6467 return node;
647 },
648 heratige: function() {
6491 var node = new Node(Node.HERITAGE);
6501 node.add(this.match('extends'), this.match(Token.ID));
6511 return node;
652 },
653 classbody: function() {
6549 var node = new Node(Node.CLASSBODY),
655 methods = {},
656 hasStatic = false;
6579 while(this.look && this.look.content() != '}') {
6588 if(this.look.content() == ';') {
6591 node.add(this.match());
6601 continue;
661 }
6627 hasStatic = false;
6637 if(this.look.content() == 'static') {
6641 node.add(this.match());
6651 hasStatic = true;
666 }
6677 if(!this.look) {
6680 this.error();
669 }
6707 node.add(this.method(hasStatic, methods));
671 }
6727 return node;
673 },
674 method: function(hasStatic, methods, statics) {
6757 var node = new Node(Node.METHOD);
6767 if(this.look.content() == 'get') {
6771 node.add(this.match(), this.getfn());
678 }
6796 else if(this.look.content() == 'set') {
6801 node.add(this.match(), this.setfn());
681 }
682 else {
6835 node.add(this.match(Token.ID));
6845 var id = node.leaves()[0].token().content();
6855 if(methods.hasOwnProperty(id)) {
6861 this.error('duplicate method decl in class');
687 }
6884 methods[id] = true;
6894 node.add(this.match('('));
6903 if(this.look.content() != ')') {
6911 node.add(this.fnparams());
692 }
6933 node.add(
694 this.match(')'),
695 this.match('{'),
696 this.fnbody(true),
697 this.match('}', 'missing } in compound statement')
698 );
699 }
7005 return node;
701 },
702 expr: function(noIn) {
70310785 var node = new Node(Node.EXPR),
704 assignexpr = this.assignexpr(noIn);
70510772 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 {
71210232 return assignexpr;
713 }
714540 return node;
715 },
716 assignexpr: function(noIn) {
71730174 var node = new Node(Node.ASSIGNEXPR),
718 cndt = this.cndtexpr(noIn);
71930161 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())) {
7333595 node.add(cndt, this.match(), this.assignexpr(noIn));
734 }
735 else {
73626566 return cndt;
737 }
7383595 return node;
739 },
740 cndtexpr: function(noIn) {
74130174 var node = new Node(Node.CNDTEXPR),
742 logorexpr = this.logorexpr(noIn);
74330161 if(this.look && this.look.content() == '?') {
744874 node.add(
745 logorexpr,
746 this.match(),
747 this.assignexpr(noIn),
748 this.match(':'),
749 this.assignexpr(noIn)
750 );
751 }
752 else {
75329287 return logorexpr;
754 }
755874 return node;
756 },
757 logorexpr: function(noIn) {
75830174 var node = new Node(Node.LOGOREXPR),
759 logandexpr = this.logandexpr(noIn);
76030161 if(this.look && this.look.content() == '||') {
7611009 node.add(logandexpr);
7621009 while(this.look && this.look.content() == '||') {
7631142 node.add(
764 this.match(),
765 this.logandexpr(noIn)
766 );
767 }
768 }
769 else {
77029152 return logandexpr;
771 }
7721009 return node;
773 },
774 logandexpr: function(noIn) {
77531316 var node = new Node(Node.LOGANDEXPR),
776 bitorexpr = this.bitorexpr(noIn);
77731303 if(this.look && this.look.content() == '&&') {
7781115 node.add(bitorexpr);
7791115 while(this.look && this.look.content() == '&&') {
7801360 node.add(
781 this.match(),
782 this.bitorexpr(noIn)
783 );
784 }
785 }
786 else {
78730188 return bitorexpr;
788 }
7891115 return node;
790 },
791 bitorexpr: function(noIn) {
79232676 var node = new Node(Node.BITOREXPR),
793 bitxorexpr = this.bitxorexpr(noIn);
79432663 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 {
80432658 return bitxorexpr;
805 }
8065 return node;
807 },
808 bitxorexpr: function(noIn) {
80932681 var node = new Node(Node.BITXOREXPR),
810 bitandexpr = this.bitandexpr(noIn);
81132668 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 {
82132666 return bitandexpr;
822 }
8232 return node;
824 },
825 bitandexpr: function(noIn) {
82632683 var node = new Node(Node.BITANDEXPR),
827 eqexpr = this.eqexpr(noIn);
82832670 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 {
83832652 return eqexpr;
839 }
84018 return node;
841 },
842 eqexpr: function(noIn) {
84332701 var node = new Node(Node.EQEXPR),
844 reltexpr = this.reltexpr(noIn);
84532688 if(this.look && {
846 '==': true,
847 '===': true,
848 '!==': true,
849 '!=': true
850 }.hasOwnProperty(this.look.content())) {
8511572 node.add(reltexpr);
8521572 while(this.look && {
853 '==': true,
854 '===': true,
855 '!==': true,
856 '!=': true
857 }.hasOwnProperty(this.look.content())) {
8581573 node.add(
859 this.match(),
860 this.reltexpr(noIn)
861 );
862 }
863 }
864 else {
86531116 return reltexpr;
866 }
8671572 return node;
868 },
869 reltexpr: function(noIn) {
87034274 var node = new Node(Node.RELTEXPR),
871 shiftexpr = this.shiftexpr();
87234261 if(this.look && ({
873 '<': true,
874 '>': true,
875 '>=': true,
876 '<=': true,
877 'instanceof': true
878 }.hasOwnProperty(this.look.content()) || (!noIn && this.look.content() == 'in'))) {
879460 node.add(shiftexpr);
880460 while(this.look && ({
881 '<': true,
882 '>': true,
883 '>=': true,
884 '<=': true,
885 'instanceof': true
886 }.hasOwnProperty(this.look.content()) || (!noIn && this.look.content() == 'in'))) {
887460 node.add(
888 this.match(),
889 this.shiftexpr()
890 );
891 }
892 }
893 else {
89433801 return shiftexpr;
895 }
896460 return node;
897 },
898 shiftexpr: function() {
89934734 var node = new Node(Node.SHIFTEXPR),
900 addexpr = this.addexpr();
90134721 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 {
91134713 return addexpr;
912 }
9138 return node;
914 },
915 addexpr: function() {
91634743 var node = new Node(Node.ADDEXPR),
917 mtplexpr = this.mtplexpr();
91834730 if(this.look && ['+', '-'].indexOf(this.look.content()) != -1) {
919757 node.add(mtplexpr);
920757 while(this.look && ['+', '-'].indexOf(this.look.content()) != -1) {
9211296 node.add(
922 this.match(),
923 this.mtplexpr()
924 );
925 }
926 }
927 else {
92833973 return mtplexpr;
929 }
930757 return node;
931 },
932 mtplexpr: function() {
93336039 var node = new Node(Node.MTPLEXPR),
934 unaryexpr = this.unaryexpr();
93536026 if(this.look && ['*', '/', '%'].indexOf(this.look.content()) != -1) {
93648 node.add(unaryexpr);
93748 while(this.look && ['*', '/', '%'].indexOf(this.look.content()) != -1) {
93849 node.add(
939 this.match(),
940 this.unaryexpr()
941 );
942 }
943 }
944 else {
94535978 return unaryexpr;
946 }
94748 return node;
948 },
949 unaryexpr: function() {
95037776 var node = new Node(Node.UNARYEXPR);
95137776 if(!this.look) {
9522 this.error();
953 }
95437774 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 '!':
9641688 node.add(
965 this.match(),
966 this.unaryexpr()
967 );
9681686 break;
969 default:
97036086 return this.postfixexpr();
971 }
9721686 return node;
973 },
974 postfixexpr: function() {
97536086 var node = new Node(Node.POSTFIXEXPR);
97636086 var leftexpr = this.leftexpr();
97736075 if(this.look && ['++', '--'].indexOf(this.look.content()) > -1 && !this.hasMoveLine) {
978307 node.add(leftexpr);
979307 while(this.look && ['++', '--'].indexOf(this.look.content()) > -1) {
980307 node.add(this.match(undefined, true));
981 }
982 }
983 else {
98435768 return leftexpr;
985 }
986307 return node;
987 },
988 leftexpr: function() {
98936086 if(this.look.content() == 'new') {
990187 return this.newexpr();
991 }
992 else {
99335899 return this.callexpr();
994 }
995 },
996 newexpr: function(depth) {
997188 depth = depth || 0;
998188 var node = new Node(Node.NEWEXPR);
999188 node.add(this.match('new'));
1000188 if(!this.look) {
10011 this.error();
1002 }
1003187 if(this.look.content() == 'new') {
10041 node.add(this.newexpr(depth + 1));
1005 }
1006 else {
1007186 node.add(this.mmbexpr());
1008 }
1009187 if(this.look && this.look.content() == '(') {
1010176 node.add(this.args());
1011 }
1012187 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 }
1039176 return node;
1040 },
1041 callexpr: function(mmb) {
104235907 var node = new Node(Node.CALLEXPR);
104335907 mmb = mmb || this.mmbexpr();
104435898 if(this.look && this.look.content() == '(') {
10455046 node.add(
1046 mmb,
1047 this.args()
1048 );
10495045 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 {
107430852 return mmb;
1075 }
10765045 return node;
1077 },
1078 mmbexpr: function() {
107936085 var node = new Node(Node.MMBEXPR);
108036085 var mmb;
108136085 if(this.look.content() == 'function') {
10821577 mmb = this.fnexpr();
1083 }
1084 else {
108534508 mmb = this.prmrexpr();
1086 }
108736076 if(this.look && ['.', '['].indexOf(this.look.content()) > -1) {
108810441 node.add(mmb);
108910441 while(this.look) {
109022852 if(this.look.content() == '.') {
109110514 node.add(
1092 this.match(),
1093 this.match(Token.ID)
1094 );
1095 }
109612338 else if(this.look.content() == '[') {
10971902 node.add(
1098 this.match(),
1099 this.expr(),
1100 this.match(']')
1101 );
1102 }
1103 else {
110410436 break;
1105 }
1106 }
1107 }
1108 else {
110925635 return mmb;
1110 }
111110441 return node;
1112 },
1113 prmrexpr: function() {
111434508 var node = new Node(Node.PRMREXPR);
111534508 switch(this.look.type()) {
1116 case Token.ID:
1117 case Token.NUMBER:
1118 case Token.STRING:
1119 case Token.REG:
1120 case Token.TEMPLATE:
112128623 node.add(this.match());
112228623 break;
1123 default:
11245885 switch(this.look.content()) {
1125 case 'this':
1126 case 'null':
1127 case 'true':
1128 case 'false':
11293240 node.add(this.match());
11303240 break;
1131 case '(':
11321068 node.add(this.match(), this.expr(), this.match(')'));
11331064 break;
1134 case '[':
1135863 node.add(this.arrltr());
1136863 break;
1137 case '{':
1138713 node.add(this.objltr());
1139713 break;
1140 default:
11411 this.error();
1142 }
1143 }
114434503 return node;
1145 },
1146 arrltr: function() {
1147863 var node = new Node(Node.ARRLTR);
1148863 node.add(this.match('['));
1149863 while(this.look && this.look.content() != ']') {
11502359 if(this.look.content() == ',') {
1151862 node.add(this.match());
1152 }
1153 else {
11541497 node.add(this.assignexpr());
1155 }
1156 }
1157863 node.add(this.match(']'));
1158863 return node;
1159 },
1160 objltr: function() {
1161713 var node = new Node(Node.OBJLTR);
1162713 node.add(this.match('{'));
1163713 while(this.look && this.look.content() != '}') {
11641980 node.add(this.proptassign());
11651980 if(this.look && this.look.content() == ',') {
11661497 node.add(this.match());
1167 }
1168 }
1169713 node.add(this.match('}'));
1170713 return node;
1171 },
1172 proptassign: function() {
11731980 var node = new Node(Node.PROPTASSIGN);
11741980 if(!this.look) {
11750 this.error();
1176 }
11771980 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 }
11891952 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 {
12021922 switch(this.look.type()) {
1203 case Token.ID:
1204 case Token.STRING:
1205 case Token.NUMBER:
12061922 node.add(
1207 this.match(),
1208 this.match(':'),
1209 this.assignexpr()
1210 );
12111922 break;
1212 default:
12130 this.error('invalid property id');
1214 }
1215 }
12161980 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() {
12645573 var node = new Node(Node.ARGS);
12655573 node.add(this.match('('));
12665573 if(!this.look) {
12671 this.error();
1268 }
12695572 if(this.look.content() != ')') {
12704859 node.add(this.arglist());
1271 }
12725572 node.add(this.match(')'));
12735572 return node;
1274 },
1275 arglist: function() {
12764859 var node = new Node(Node.ARGLIST);
12774859 node.add(this.assignexpr());
12784859 while(this.look && this.look.content() == ',') {
12792788 node.add(this.match(), this.assignexpr());
1280 }
12814859 return node;
1282 },
1283 virtual: function(s) {
12841577 return new Node(Node.TOKEN, new Token(Token.VIRTUAL, s));
1285 },
1286 match: function(type, line, msg) {
1287137127 if(typeof type == 'boolean') {
12880 msg = line;
12890 line = type;
12900 type = undefined;
1291 }
1292137127 if(typeof line != 'boolean') {
1293135031 line = false;
1294135031 msg = line;
1295 }
1296 //未定义为所有非空白token
1297137127 if(character.isUndefined(type)) {
129870414 if(this.look) {
129970414 var l = this.look;
130070414 this.move(line);
130170414 return new Node(Node.TOKEN, l);
1302 }
1303 else {
13040 this.error('syntax error' + (msg || ''));
1305 }
1306 }
1307 //或者根据token的type或者content匹配
130866713 else if(typeof type == 'string') {
1309 //特殊处理;,不匹配但有换行或者末尾时自动补全,还有受限行
131051483 if(type == ';'
1311 && (!this.look
1312 || (this.look.content() != type && this.hasMoveLine)
1313 || this.look.content() == '}')
1314 ) {
13151577 if(this.look && S[this.look.type()]) {
131636 this.move();
1317 }
13181577 return this.virtual(';');
1319 }
132049906 else if(this.look && this.look.content() == type) {
132149905 var l = this.look;
132249905 this.move(line);
132349905 return new Node(Node.TOKEN, l);
1324 }
1325 else {
13261 this.error('missing ' + type + (msg || ''));
1327 }
1328 }
132915230 else if(typeof type == 'number') {
133015230 if(this.look && this.look.type() == type) {
133115230 var l = this.look;
133215230 this.move(line);
133315230 return new Node(Node.TOKEN, l);
1334 }
1335 else {
13360 this.error('missing ' + Token.type(type) + (msg || ''));
1337 }
1338 }
1339 },
1340 move: function(line) {
1341135737 this.lastLine = this.line;
1342135737 this.lastCol = this.col;
1343 //遗留下来的换行符
1344135737 this.hasMoveLine = false;
1345135737 do {
1346242057 this.look = this.tokens[this.index++];
1347242057 if(!this.look) {
1348146 return;
1349 }
1350 //存下忽略的token
1351241911 if(S[this.look.type()]) {
1352106356 this.ignores[this.index - 1] = this.look;
1353 }
1354 //包括line的情况下要跳出
1355241911 if(this.look.type() == Token.LINE) {
135617150 this.line++;
135717150 this.col = 1;
135817150 this.hasMoveLine = true;
135917150 if(line) {
136035 break;
1361 }
1362 }
1363224761 else if(this.look.type() == Token.COMMENT) {
13642534 var s = this.look.content();
13652534 var n = character.count(this.look.content(), character.LINE);
13662534 if(n > 0) {
136789 this.line += n;
136889 var i = s.lastIndexOf(character.LINE);
136989 this.col += s.length - i - 1;
137089 this.hasMoveLine = true;
137189 if(line) {
13721 break;
1373 }
1374 }
1375 }
1376 else {
1377222227 this.col += this.look.content().length;
1378222227 if(!S[this.look.type()]) {
1379135555 break;
1380 }
1381 }
1382 } while(this.index <= this.length);
1383 },
1384 error: function(msg) {
138525 msg = 'SyntaxError: ' + (msg || ' syntax error');
138625 throw new Error(msg + ' line ' + this.lastLine + ' col ' + this.lastCol);
1387 },
1388 ignore: function() {
13897 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) {
18188 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) {
34685 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
35};
361exports.count = function(s, c) {
37450738 var count = 0,
38 i = -1;
39450738 while((i = s.indexOf(c, i + 1)) != -1) {
4032911 count++;
41 }
42450738 return count;
43};
441exports.isUndefined = function(s) {
454302048 return typeof s == 'undefined';
46};
471exports.isString = function(s) {
480 return Object.prototype.toString.call(s) == "[object String]";
49};
501exports.isNumber = function(s) {
51450268 return Object.prototype.toString.call(s) == "[object Number]";
52};