Coverage

95%
1266
1208
58

/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) {
5231 this.rule = rule; //当前语法规则
6231 this.init();
7}).methods({
8 init: function() {
9232 this.code = ''; //要解析的代码
10232 this.peek = ''; //向前看字符
11232 this.index = 0; //向前看字符字符索引
12232 this.isReg = Lexer.IS_REG; //当前/是否是perl风格正则表达式
13232 this.tokenList = []; //结果的token列表
14232 this.parentheseState = false; //(开始时标记之前终结符是否为if/for/while等关键字
15232 this.parentheseStack = []; //圆括号深度记录当前是否为if/for/while等语句内部
16232 this.cacheLine = 0; //行缓存值
17232 this.totalLine = 1; //总行数
18232 this.colNum = 0; //列
19232 this.colMax = 0; //最大列数
20 },
21 parse: function(code) {
22194 this.code = code || '';
23194 var temp = [];
24194 this.scan(temp);
25189 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) {
36195 var perlReg = this.rule.perlReg();
37195 var length = this.code.length;
38195 var count = 0;
39 outer:
40 while(this.index < length) {
41448812 if(this.cacheLine > 0 && count >= this.cacheLine) {
421 break;
43 }
44448811 this.readch();
45 //perl风格正则
46448811 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 {
52448327 for(var i = 0, matches = this.rule.matches(), len = matches.length; i < len; i++) {
537360635 var match = matches[i];
547360635 if(match.match(this.peek, this.code, this.index)) {
55448326 var token = new Token(match.tokenType(), match.content(), match.val(), this.index - 1);
56448326 var error = match.error();
57448326 var matchLen = match.content().length;
58448326 if(token.type() == Token.ID && this.rule.keyWords().hasOwnProperty(token.content())) {
5921159 token.type(Token.KEYWORD);
60 }
61448326 temp.push(token);
62448326 this.tokenList.push(token);
63448326 this.index += matchLen - 1;
64448326 var n = character.count(token.val(), character.LINE);
65448326 count += n;
66448326 this.totalLine += n;
67448326 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 {
74416644 this.colNum += matchLen;
75 }
76448326 this.colMax = Math.max(this.colMax, this.colNum);
77448326 if(error) {
783 this.error(error, this.code.slice(this.index - matchLen, this.index));
79 }
80 //支持perl正则需判断关键字、圆括号对除号语义的影响
81448324 if(perlReg && match.perlReg() != Lexer.IGNORE) {
82255431 if(match.perlReg() == Lexer.SPECIAL) {
8394917 this.isReg = match.special();
84 }
85 else {
86160514 this.isReg = match.perlReg();
87 }
88255431 if(this.peek == character.LEFT_PARENTHESE) {
8919670 this.parentheseStack.push(this.parentheseState);
9019670 this.parentheseState = false;
91 }
92235761 else if(this.peek == character.RIGHT_PARENTHESE) {
9319654 this.isReg = this.parentheseStack.pop() ? Lexer.IS_REG : Lexer.NOT_REG;
94 }
95 else {
96216107 this.parentheseState = match.parenthese();
97 }
98 }
99448324 continue outer;
100 }
101 }
102 //如果有未匹配的,说明规则不完整,加入other类型并抛出警告
1031 this.error('unknow token');
104 }
105 }
106190 return this;
107 },
108 readch: function() {
109457611 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) {
6450392 this.t = type; //token类型
7450392 this.c = content; //token的字面内容,string包括头尾的引号
8450392 if(character.isNumber(val)) {
9487 sIndex = val;
10487 val = content;
11 }
12449905 else if(character.isUndefined(val)) {
131579 val = content;
141579 sIndex = -1;
15 }
16450392 this.v = val; //token的值,一般情况下等于content,特殊如string情况下值是不加头尾的引号
17450392 this.id = tid++; //token的索引
18450392 this.si = sIndex; //token在源码字符串中的索引
19}).methods({
20 type: function(t) {
211604479 if(!character.isUndefined(t)) {
2221159 this.t = t;
23 }
241604479 return this.t;
25 },
26 content: function(c) {
271638607 if(!character.isUndefined(c)) {
281 this.c = c;
29 }
301638607 return this.c;
31 },
32 val: function(v) {
33448328 if(!character.isUndefined(v)) {
341 this.v = v;
35 }
36448328 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) {
4458 Match.call(this, type, setPReg);
5458 this.str = str;
6}).methods({
7 match: function(c, code, index) {
8139396 var isIn = this.str.indexOf(c) > -1;
9139396 if(isIn) {
10131931 this.result = c;
11 }
12139396 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) {
47685 Match.call(this, type, setPReg);
57685 this.result = result;
6}).methods({
7 match: function(c, code, index) {
85533731 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) {
5691 if(character.isUndefined(mutiline)) {
60 mutiline = false;
7 }
8691 Match.call(this, type, setPReg);
9691 this.begin = begin;
10691 this.end = end;
11691 this.msg = null;
12691 this.mutiline = mutiline;
13 }).methods({
14 match: function(c, code, index) {
15754329 this.msg = null;
16754329 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 }
42747548 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) {
5462 if(character.isUndefined(contain)) {
6231 contain = false;
7 }
8462 Match.call(this, type, setPReg);
9462 this.begin = begin;
10462 this.end = end;
11462 this.contain = contain;
12462 this.msg = null;
13}).methods({
14 match: function(c, code, index) {
15515873 this.msg = null;
16515873 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 }
30511081 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) {
510240 this.type = type;
610240 if(character.isUndefined(setPReg)) {
71767 setPReg = Lexer.IGNORE;
8 }
910240 this.setPReg = setPReg;
1010240 this.result = null;
11 //忽略0,是1,否2,特殊3
1210240 if(setPReg) {
138473 if(character.isUndefined(special)) {
148244 special = function() {
150 return Lexer.IGNORE;
16 };
17 }
188473 if(character.isUndefined(parenthese)) {
198244 parenthese = function() {
20121190 return false;
21 };
22 }
23 }
2410240 this.special = special;
2510240 this.parenthese = parenthese;
26}).methods({
27 tokenType: function() {
28448326 return this.type;
29 },
30 perlReg: function() {
31864269 return this.setPReg;
32 },
33 val: function() {
34441545 return this.content();
35 },
36 content: function() {
371629858 return this.result;
38 },
39 match: function(c, code, index) {
40 //需被实现
410 return false;
42 },
43 error: function() {
44334365 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) {
3944 if(typeof valid == 'number') {
4229 parenthese = special;
5229 special = setPReg;
6229 setPReg = valid;
7229 valid = null;
8 }
9944 Match.call(this, type, setPReg, special, parenthese);
10944 this.reg = reg;
11944 this.valid = valid;
12 }).methods({
13 match: function(c, code, index) {
14417306 var self = this,
15 res = self.reg.exec(code.slice(index - 1));
16417306 self.msg = null;
17417306 if(res) {
18102388 self.result = res[0];
19102388 if(self.valid) {
207471 for(var i = 0, keys = Object.keys(self.valid), len = keys.length; i < len; i++) {
217471 if(self.valid[keys[i]].test(self.result)) {
222 self.msg = keys[i];
232 break;
24 }
25 }
26 }
27102388 return true;
28 }
29314918 return false;
30 },
31 error: function() {
32102388 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() {
11229 var self = this;
12229 Rule.call(self, EcmascriptRule.KEYWORDS, true);
13
14229 self.addMatch(new CompleteEqual(Token.BLANK, character.BLANK));
15229 self.addMatch(new CompleteEqual(Token.TAB, character.TAB));
16229 self.addMatch(new CompleteEqual(Token.ENTER, character.ENTER));
17229 self.addMatch(new CompleteEqual(Token.LINE, character.LINE));
18
19229 self.addMatch(new LineSearch(Token.COMMENT, '//', '\n'));
20229 self.addMatch(new LineSearch(Token.COMMENT, '/*', '*/', true));
21229 self.addMatch(new LineParse(Token.STRING, '"', '"', false, Lexer.IS_REG));
22229 self.addMatch(new LineParse(Token.STRING, "'", "'", false, Lexer.IS_REG));
23229 self.addMatch(new LineParse(Token.TEMPLATE, '`', '`', true, Lexer.IS_REG));
24
25229 self.addMatch(new RegMatch(Token.ID, /^[$a-zA-Z_][$\w]*/, Lexer.SPECIAL, function() {
2694917 return !!(self.keyWords().hasOwnProperty(this.content()));
27 }, function() {
2894917 return ['if', 'for', 'while'].indexOf(this.content()) != -1;
29 }));
30
31229 self.addMatch(new RegMatch(Token.NUMBER, /^\.\d+(?:E[+-]?\d*)?/i, {
32 'SyntaxError: missing exponent': /E[+-]?$/i
33 }, Lexer.NOT_REG));
34
35229 self.addMatch(new CompleteEqual(Token.SIGN, ']', Lexer.NOT_REG));
36
37229 ['*=', '/=', '+=', '-=', '%=', '^=', '&=', '|=', '&&', '--', '++', '===', '==', '!==', '!=', '||', '>>>=', '<<<=', '<<<', '>>>', '>>=', '<<=', '<<', '>>', '>=', '<=', '...', '?:'].forEach(function(o) {
386412 self.addMatch(new CompleteEqual(Token.SIGN, o, Lexer.IS_REG));
39 });
40229 self.addMatch(new CharacterSet(Token.SIGN, ':;/?.,[]{}~!^|%=-+*()~><&\\', Lexer.IS_REG));
41
42229 self.addMatch(new RegMatch(Token.NUMBER, /^0x[\da-f]*/i, {
43 "SyntaxError: missing hexadecimal digits after '0x'": /^0x$/i
44 }, Lexer.NOT_REG));
45229 self.addMatch(new RegMatch(Token.NUMBER, /^\d+\.?\d*(?:E[+-]?\d*)?/i, {
46 'SyntaxError: missing exponent': /E[+-]?$/i
47 }, Lexer.NOT_REG));
48
49229 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) {
3231 var self = this;
4231 self.kw = {};
5231 words.forEach(function(o) {
614110 self.kw[o] = true;
7 });
8231 self.pReg = pReg || false;
9231 self.matchList = [];
10 }).methods({
11 perlReg: function() {
12195 return this.pReg;
13 },
14 addMatch: function(match) {
1510240 this.matchList.push(match);
1610240 return this;
17 },
18 matches: function() {
19448327 return this.matchList;
20 },
21 keyWords: function() {
22189834 return this.kw;
23 }
24 });
251module.exports = Rule;

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

97%
160
156
4
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) {
752 this.id = id++;
852 this.parser = new Parser();
952 this.parent = parent || null; //父上下文,如果是全局则为空
1052 this.name = name || null; //上下文名称,即函数名,函数表达式为空,全局也为空
1152 this.children = []; //函数声明或函数表达式所产生的上下文
1252 this.childrenMap = Object.create(null); //键是函数名,值是上下文,匿名函数表达式键为cid
1352 this.vars = []; //变量var声明
1452 this.varsMap = Object.create(null); //键为id字面量,值是它的token的节点
1552 this.params = []; //形参,函数上下文才有,即全局无
1652 this.paramsMap = Object.create(null); //键为id字面量,值是它的token的节点
1752 this.aParams = []; //实参,函数表达式才有
1852 this.vids = []; //上下文环境里用到的变量id
1952 this.vidsMap = Object.create(null); //键为id字面量,值是它的token的节点
2052 this.returns = []; //上下文环境里return语句
2152 this.node = null; //对应的ast的节点
2252 if(!this.isTop()) {
2320 this.parent.addChild(this);
24 }
25}).methods({
26 parse: function(code) {
2728 var ast;
2828 if(code instanceof JsNode) {
291 ast = code;
30 }
31 else {
3227 ast = this.parser.parse(code);
33 }
3428 recursion(ast, this);
3528 return this;
36 },
37 getId: function() {
3821 return this.id;
39 },
40 getName: function() {
4121 return this.name;
42 },
43 getParent: function() {
442 return this.parent;
45 },
46 isTop: function() {
4753 return !this.parent;
48 },
49 isFnexpr: function() {
501 return !this.isTop() && !this.name;
51 },
52 hasParam: function(p) {
536 return p in this.paramsMap;
54 },
55 getParams: function() {
560 return this.params;
57 },
58 addParam: function(p) {
59 //形参不可能重复,无需判断
606 this.paramsMap[p] = this.params.length;
616 this.params.push(p);
626 return this;
63 },
64 getAParams: function() {
651 return this.aParams;
66 },
67 addAParam: function(ap) {
684 this.aParams.push(ap);
694 return this;
70 },
71 getChild: function(name) {
727 return this.childrenMap[name];
73 },
74 getChildren: function() {
7511 return this.children;
76 },
77 //通过name查找函数声明,id查找表达式
78 hasChild: function(name) {
7937 return name in this.childrenMap;
80 },
81 addChild: function(child) {
8220 var name = child.getName();
83 //函数表达式名字为空用id删除
8420 if(name) {
8510 this.delChild(name);
86 }
87 else {
8810 this.delChild(child.getId());
89 }
9020 name = name || child.getId();
9120 this.childrenMap[name] = child;
9220 this.children.push(child);
9320 return this;
94 },
95 //name函数声明,id表达式
96 delChild: function(name) {
9730 if(this.hasChild(name)) {
981 var i = this.children.indexOf(this.childrenMap[name]);
991 this.children.splice(i, 1);
1001 delete this.childrenMap[name];
101 }
10230 return this;
103 },
104 hasVar: function(v) {
10521 return v in this.varsMap;
106 },
107 addVar: function(node, assign) {
10813 var v = node.leaves()[0].token().content();
109 //赋值拥有最高优先级,会覆盖掉之前的函数声明和var
11013 if(assign) {
11110 this.delVar(v);
11210 this.delChild(v);
113 }
114 //仅仅是var声明无赋值,且已有过声明或函数,忽略之
1153 else if(this.hasVar(v) || this.hasChild(v)) {
1162 return this;
117 }
11811 this.varsMap[v] = node;
11911 this.vars.push(node);
12011 return this;
121 },
122 delVar: function(v) {
12310 if(this.hasVar(v)) {
1240 var i = this.vars.indexOf(this.varsMap[v]);
1250 this.vars.splice(i, 1);
1260 delete this.varsMap[v];
127 }
12810 return this;
129 },
130 getVars: function() {
1313 return this.vars;
132 },
133 addReturn: function(node) {
1345 this.returns.push(node);
1355 return this;
136 },
137 getReturns: function() {
1384 return this.returns;
139 },
140 hasVid: function(v) {
1418 return v in this.vidsMap;
142 },
143 getVid: function(v) {
1441 return this.vidsMap[v];
145 },
146 addVid: function(node) {
14712 var v = node.token().content();
14812 this.vids.push(node);
14912 this.vidsMap[v] = this.vidsMap[v] || [];
15012 this.vidsMap[v].push(node);
15112 return this;
152 },
153 getVids: function() {
1542 return this.vids;
155 },
156 getNode: function() {
1571 return this.node;
158 },
159 setNode: function(n) {
16020 this.node = n;
16120 return this;
162 }
163});
164
1651function recursion(node, context) {
166494 var isToken = node.name() == JsNode.TOKEN;
167494 var isVirtual = isToken && node.token().type() == Token.VIRTUAL;
168494 if(isToken) {
169285 if(!isVirtual) {
170279 var token = node.token();
171279 var s = token.content();
172279 if(s == 'return') {
1735 context.addReturn(node);
174 }
175 }
176 }
177 else {
178209 if(node.name() == JsNode.VARDECL) {
17913 vardecl(node, context);
180 }
181196 else if(node.name() == JsNode.FNDECL) {
18210 context = fndecl(node, context);
183 }
184186 else if(node.name() == JsNode.FNEXPR) {
18510 context = fnexpr(node, context);
186 }
187176 else if(node.name() == JsNode.PRMREXPR) {
18836 prmrexpr(node, context);
189 }
190209 node.leaves().forEach(function(leaf, i) {
191466 recursion(leaf, context);
192 });
193 }
194}
1951function vardecl(node, context) {
19613 var leaves = node.leaves();
19713 var assign = !!leaves[1];
19813 context.addVar(node, assign);
199}
2001function fndecl(node, context) {
20110 var v = node.leaves()[1].leaves().content();
20210 var child = new Context(context, v);
20310 child.setNode(node);
20410 var params = node.leaves()[3];
20510 if(params.name() == JsNode.FNPARAMS) {
2061 addParam(params, child);
207 }
20810 return child;
209}
2101function fnexpr(node, context) {
211 //函数表达式name为空
21210 var child = new Context(context);
21310 child.setNode(node);
214 //记录形参
21510 var params;
21610 var v = node.leaves()[1];
21710 if(v.token().content() != '(') {
2184 params = node.leaves()[3];
219 }
220 else {
2216 params = node.leaves()[2];
222 }
22310 if(params.name() == JsNode.FNPARAMS) {
2243 addParam(params, child);
225 }
226 //匿名函数检查实参传入情况
22710 var next = node.next();
228 //!function(){}()形式
22910 if(next && next.name() == JsNode.ARGS) {
2307 var leaves = next.leaves();
231 //长度2为()空参数,长度3有参数,第2个节点
2327 if(leaves.length == 3) {
2331 addAParam(leaves[1], child);
234 }
235 }
236 //(function(){})()形式
237 else {
2383 var prmr = node.parent();
2393 var prev = node.prev();
2403 if(prmr.name() == JsNode.PRMREXPR && prev && prev.name() == JsNode.TOKEN && prev.token().content() == '(') {
2413 next = prmr.next();
2423 if(next && next.name() == JsNode.ARGS) {
2433 var leaves = next.leaves();
244 //长度2为()空参数,长度3有参数,第2个节点
2453 if(leaves.length == 3) {
2461 addAParam(leaves[1], child);
247 }
248 }
249 }
250 }
25110 return child;
252}
253//支持es6
2541function addParam(params, child) {
2554 params.leaves().forEach(function(leaf, i) {
2569 if(leaf.name() == JsNode.TOKEN && leaf.token().content() != ',') {
2575 child.addParam(leaf.token().content());
258 }
2594 else if(leaf.name() == JsNode.RESTPARAM) {
2601 child.addParam(leaf.leaves()[1].token().content());
261 }
262 });
263}
2641function addAParam(params, child) {
2652 params.leaves().forEach(function(leaf, i) {
2666 if(i % 2 == 0) {
2674 child.addAParam(leaf);
268 }
269 });
270}
2711function prmrexpr(node, context) {
27236 var first = node.leaves()[0];
27336 if(first.name() == JsNode.TOKEN) {
27434 var token = first.token();
27534 if(token.type() == Token.ID) {
27612 context.addVid(first);
277 }
278 }
279}
280
2811module.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) {
3755830 this.type = type;
4755830 if(type == Node.TOKEN) {
5137236 this.children = children;
6 }
7618594 else if(Array.isArray(children)) {
80 this.children = children;
9 }
10 else {
11618594 this.children = children ? [children] : [];
12 }
13755830 this.p = null;
14755830 this.pr = null;
15755830 this.ne = null;
16755830 return this;
17}).methods({
18 name: function() {
19235230 return this.type;
20 },
21 leaves: function() {
2296826 return this.children;
23 },
24 add: function() {
25163323 var self = this;
26163323 var args = Array.prototype.slice.call(arguments, 0);
27163323 args.forEach(function(node) {
28233780 node.parent(self);
29233780 var last = self.children[self.children.length - 1];
30233780 if(last) {
31137090 last.next(node);
32137090 node.prev(last);
33 }
34233780 self.children.push(node);
35 });
36163323 return self;
37 },
38 token: function() {
39272765 return this.children;
40 },
41 parent: function(p) {
42233786 if(p) {
43233780 this.p = p;
44 }
45233786 return this.p;
46 },
47 prev: function(pr) {
48137095 if(pr) {
49137090 this.pr = pr;
50 }
51137095 return this.pr;
52 },
53 next: function(ne) {
54137104 if(ne) {
55137090 this.ne = ne;
56 }
57137104 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) {
10196 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) {
21197 this.look = null;
22197 this.tokens = null;
23197 this.lastLine = 1;
24197 this.lastCol = 1;
25197 this.line = 1;
26197 this.col = 1;
27197 this.index = 0;
28197 this.length = 0;
29197 this.ignores = {};
30197 this.hasMoveLine = false;
31197 this.tree = {};
32197 if(lexer) {
33144 this.lexer = lexer;
34 }
3553 else if(this.lexer) {
361 this.lexer.init();
37 }
38 else {
3952 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) {
50195 node.add(this.element());
51 }
52141 return node;
53 },
54 element: function(allowSuper) {
555216 if(this.look.content() == 'function') {
56195 return this.fndecl();
57 }
585021 else if(this.look.content() == 'class') {
5910 return this.classdecl();
60 }
61 else {
625011 return this.stmt(allowSuper);
63 }
64 },
65 stmt: function(allowSuper) {
6610558 if(!this.look) {
671 this.error();
68 }
6910557 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:
1103692 if(this.look.type() == Token.ID) {
1112864 for(var i = this.index; i < this.length; i++) {
1123867 var token = this.tokens[i];
1133867 if(!S[token.type()]) {
1142861 if(token.content() == ':') {
1152 return this.labstmt();
116 }
117 else {
1182859 return this.exprstmt();
119 }
120 }
121 }
122 }
123831 return this.exprstmt();
124 }
125 },
126 exprstmt: function() {
1273690 var node = new Node(Node.EXPRSTMT);
1283690 node.add(this.expr(), this.match(';'));
1293681 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() {
548195 var node = new Node(Node.FNDECL);
549195 node.add(
550 this.match('function'),
551 this.match(Token.ID, 'function statement requires a name'),
552 this.match('(')
553 );
554195 if(!this.look) {
5550 this.error('missing formal parameter');
556 }
557195 if(this.look.content() != ')') {
558154 node.add(this.fnparams());
559 }
560195 node.add(
561 this.match(')'),
562 this.match('{'),
563 this.fnbody(),
564 this.match('}')
565 );
566195 return node;
567 },
568 fnexpr: function() {
5691583 var node = new Node(Node.FNEXPR);
5701583 node.add(this.match('function'));
5711583 if(!this.look) {
5721 this.error('missing formal parameter');
573 }
5741582 if(this.look.type() == Token.ID) {
57520 node.add(this.match());
576 }
5771582 node.add(this.match('('));
5781582 if(!this.look) {
5791 this.error();
580 }
5811581 if(this.look.content() != ')') {
5821113 node.add(this.fnparams());
583 }
5841579 node.add(
585 this.match(')'),
586 this.match('{'),
587 this.fnbody(),
588 this.match('}', 'missing } in compound statement')
589 );
5901579 return node;
591 },
592 fnparams: function() {
5931268 var node = new Node(Node.FNPARAMS);
5941268 node.add(this.match(Token.ID, 'missing formal parameter'));
5951268 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 }
6131266 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) {
6261779 var node = new Node(Node.FNBODY);
6271779 while(this.look && this.look.content() != '}') {
6285021 node.add(this.element(allowSuper));
629 }
6301779 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) {
70310798 var node = new Node(Node.EXPR),
704 assignexpr = this.assignexpr(noIn);
70510785 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 {
71210245 return assignexpr;
713 }
714540 return node;
715 },
716 assignexpr: function(noIn) {
71730192 var node = new Node(Node.ASSIGNEXPR),
718 cndt = this.cndtexpr(noIn);
71930179 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())) {
7333598 node.add(cndt, this.match(), this.assignexpr(noIn));
734 }
735 else {
73626581 return cndt;
737 }
7383598 return node;
739 },
740 cndtexpr: function(noIn) {
74130192 var node = new Node(Node.CNDTEXPR),
742 logorexpr = this.logorexpr(noIn);
74330179 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 {
75329305 return logorexpr;
754 }
755874 return node;
756 },
757 logorexpr: function(noIn) {
75830192 var node = new Node(Node.LOGOREXPR),
759 logandexpr = this.logandexpr(noIn);
76030179 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 {
77029170 return logandexpr;
771 }
7721009 return node;
773 },
774 logandexpr: function(noIn) {
77531334 var node = new Node(Node.LOGANDEXPR),
776 bitorexpr = this.bitorexpr(noIn);
77731321 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 {
78730206 return bitorexpr;
788 }
7891115 return node;
790 },
791 bitorexpr: function(noIn) {
79232694 var node = new Node(Node.BITOREXPR),
793 bitxorexpr = this.bitxorexpr(noIn);
79432681 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 {
80432676 return bitxorexpr;
805 }
8065 return node;
807 },
808 bitxorexpr: function(noIn) {
80932699 var node = new Node(Node.BITXOREXPR),
810 bitandexpr = this.bitandexpr(noIn);
81132686 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 {
82132684 return bitandexpr;
822 }
8232 return node;
824 },
825 bitandexpr: function(noIn) {
82632701 var node = new Node(Node.BITANDEXPR),
827 eqexpr = this.eqexpr(noIn);
82832688 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 {
83832670 return eqexpr;
839 }
84018 return node;
841 },
842 eqexpr: function(noIn) {
84332719 var node = new Node(Node.EQEXPR),
844 reltexpr = this.reltexpr(noIn);
84532706 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 {
86531134 return reltexpr;
866 }
8671572 return node;
868 },
869 reltexpr: function(noIn) {
87034292 var node = new Node(Node.RELTEXPR),
871 shiftexpr = this.shiftexpr();
87234279 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 {
89433819 return shiftexpr;
895 }
896460 return node;
897 },
898 shiftexpr: function() {
89934752 var node = new Node(Node.SHIFTEXPR),
900 addexpr = this.addexpr();
90134739 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 {
91134731 return addexpr;
912 }
9138 return node;
914 },
915 addexpr: function() {
91634761 var node = new Node(Node.ADDEXPR),
917 mtplexpr = this.mtplexpr();
91834748 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 {
92833991 return mtplexpr;
929 }
930757 return node;
931 },
932 mtplexpr: function() {
93336057 var node = new Node(Node.MTPLEXPR),
934 unaryexpr = this.unaryexpr();
93536044 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 {
94535996 return unaryexpr;
946 }
94748 return node;
948 },
949 unaryexpr: function() {
95037797 var node = new Node(Node.UNARYEXPR);
95137797 if(!this.look) {
9522 this.error();
953 }
95437795 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 '!':
9641691 node.add(
965 this.match(),
966 this.unaryexpr()
967 );
9681689 break;
969 default:
97036104 return this.postfixexpr();
971 }
9721689 return node;
973 },
974 postfixexpr: function() {
97536104 var node = new Node(Node.POSTFIXEXPR);
97636104 var leftexpr = this.leftexpr();
97736093 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 {
98435786 return leftexpr;
985 }
986307 return node;
987 },
988 leftexpr: function() {
98936104 if(this.look.content() == 'new') {
990187 return this.newexpr();
991 }
992 else {
99335917 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) {
104235925 var node = new Node(Node.CALLEXPR);
104335925 mmb = mmb || this.mmbexpr();
104435916 if(this.look && this.look.content() == '(') {
10455053 node.add(
1046 mmb,
1047 this.args()
1048 );
10495052 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 {
107430863 return mmb;
1075 }
10765052 return node;
1077 },
1078 mmbexpr: function() {
107936103 var node = new Node(Node.MMBEXPR);
108036103 var mmb;
108136103 if(this.look.content() == 'function') {
10821583 mmb = this.fnexpr();
1083 }
1084 else {
108534520 mmb = this.prmrexpr();
1086 }
108736094 if(this.look && ['.', '['].indexOf(this.look.content()) > -1) {
108810443 node.add(mmb);
108910443 while(this.look) {
109022856 if(this.look.content() == '.') {
109110516 node.add(
1092 this.match(),
1093 this.match(Token.ID)
1094 );
1095 }
109612340 else if(this.look.content() == '[') {
10971902 node.add(
1098 this.match(),
1099 this.expr(),
1100 this.match(']')
1101 );
1102 }
1103 else {
110410438 break;
1105 }
1106 }
1107 }
1108 else {
110925651 return mmb;
1110 }
111110443 return node;
1112 },
1113 prmrexpr: function() {
111434520 var node = new Node(Node.PRMREXPR);
111534520 switch(this.look.type()) {
1116 case Token.ID:
1117 case Token.NUMBER:
1118 case Token.STRING:
1119 case Token.REG:
1120 case Token.TEMPLATE:
112128631 node.add(this.match());
112228631 break;
1123 default:
11245889 switch(this.look.content()) {
1125 case 'this':
1126 case 'null':
1127 case 'true':
1128 case 'false':
11293241 node.add(this.match());
11303241 break;
1131 case '(':
11321071 node.add(this.match(), this.expr(), this.match(')'));
11331067 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 }
114434515 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() {
12645580 var node = new Node(Node.ARGS);
12655580 node.add(this.match('('));
12665580 if(!this.look) {
12671 this.error();
1268 }
12695579 if(this.look.content() != ')') {
12704861 node.add(this.arglist());
1271 }
12725579 node.add(this.match(')'));
12735579 return node;
1274 },
1275 arglist: function() {
12764861 var node = new Node(Node.ARGLIST);
12774861 node.add(this.assignexpr());
12784861 while(this.look && this.look.content() == ',') {
12792788 node.add(this.match(), this.assignexpr());
1280 }
12814861 return node;
1282 },
1283 virtual: function(s) {
12841579 return new Node(Node.TOKEN, new Token(Token.VIRTUAL, s));
1285 },
1286 match: function(type, line, msg) {
1287137237 if(typeof type == 'boolean') {
12880 msg = line;
12890 line = type;
12900 type = undefined;
1291 }
1292137237 if(typeof line != 'boolean') {
1293135141 line = false;
1294135141 msg = line;
1295 }
1296 //未定义为所有非空白token
1297137237 if(character.isUndefined(type)) {
129870438 if(this.look) {
129970438 var l = this.look;
130070438 this.move(line);
130170438 return new Node(Node.TOKEN, l);
1302 }
1303 else {
13040 this.error('syntax error' + (msg || ''));
1305 }
1306 }
1307 //或者根据token的type或者content匹配
130866799 else if(typeof type == 'string') {
1309 //特殊处理;,不匹配但有换行或者末尾时自动补全,还有受限行
131051560 if(type == ';'
1311 && (!this.look
1312 || (this.look.content() != type && this.hasMoveLine)
1313 || this.look.content() == '}')
1314 ) {
13151579 if(this.look && S[this.look.type()]) {
131636 this.move();
1317 }
13181579 return this.virtual(';');
1319 }
132049981 else if(this.look && this.look.content() == type) {
132149980 var l = this.look;
132249980 this.move(line);
132349980 return new Node(Node.TOKEN, l);
1324 }
1325 else {
13261 this.error('missing ' + type + (msg || ''));
1327 }
1328 }
132915239 else if(typeof type == 'number') {
133015239 if(this.look && this.look.type() == type) {
133115239 var l = this.look;
133215239 this.move(line);
133315239 return new Node(Node.TOKEN, l);
1334 }
1335 else {
13360 this.error('missing ' + Token.type(type) + (msg || ''));
1337 }
1338 }
1339 },
1340 move: function(line) {
1341135858 this.lastLine = this.line;
1342135858 this.lastCol = this.col;
1343 //遗留下来的换行符
1344135858 this.hasMoveLine = false;
1345135858 do {
1346242192 this.look = this.tokens[this.index++];
1347242192 if(!this.look) {
1348159 return;
1349 }
1350 //存下忽略的token
1351242033 if(S[this.look.type()]) {
1352106370 this.ignores[this.index - 1] = this.look;
1353 }
1354 //包括line的情况下要跳出
1355242033 if(this.look.type() == Token.LINE) {
135617150 this.line++;
135717150 this.col = 1;
135817150 this.hasMoveLine = true;
135917150 if(line) {
136035 break;
1361 }
1362 }
1363224883 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 {
1377222349 this.col += this.look.content().length;
1378222349 if(!S[this.look.type()]) {
1379135663 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) {
18189 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) {
37450860 var count = 0,
38 i = -1;
39450860 while((i = s.indexOf(c, i + 1)) != -1) {
4032911 count++;
41 }
42450860 return count;
43};
441exports.isUndefined = function(s) {
454306930 return typeof s == 'undefined';
46};
471exports.isString = function(s) {
480 return Object.prototype.toString.call(s) == "[object String]";
49};
501exports.isNumber = function(s) {
51450392 return Object.prototype.toString.call(s) == "[object Number]";
52};