Coverage

97%
1273
1243
30

D:/www/homunculus/src/lexer/Lexer.js

100%
128
128
0
LineHitsSource
11var Class = require('../util/Class');
21var character = require('../util/character');
31var Token = require('./Token');
41var Lexer = Class(function(rule) {
5248 this.rule = rule; //当前语法规则
6248 this.init();
7}).methods({
8 init: function() {
9249 this.code = ''; //要解析的代码
10249 this.peek = ''; //向前看字符
11249 this.index = 0; //向前看字符字符索引
12249 this.isReg = Lexer.IS_REG; //当前/是否是perl风格正则表达式
13249 this.tokenList = []; //结果的token列表
14249 this.parentheseState = false; //(开始时标记之前终结符是否为if/for/while等关键字
15249 this.parentheseStack = []; //圆括号深度记录当前是否为if/for/while等语句内部
16249 this.cacheLine = 0; //行缓存值
17249 this.totalLine = 1; //总行数
18249 this.colNum = 0; //列
19249 this.colMax = 0; //最大列数
20 },
21 parse: function(code) {
22209 this.code = code || '';
23209 var temp = [];
24209 this.scan(temp);
25200 return temp;
26 },
27 parseOn: function() {
281 var temp = [];
291 this.scan(temp);
301 return temp;
31 },
32 tokens: function() {
33174 return this.tokenList;
34 },
35 scan: function(temp) {
36210 var perlReg = this.rule.perlReg();
37210 var length = this.code.length;
38210 var count = 0;
39 outer:
40 while(this.index < length) {
41481850 if(this.cacheLine > 0 && count >= this.cacheLine) {
421 break;
43 }
44481849 this.readch();
45 //perl风格正则
46481849 if(perlReg && this.isReg == Lexer.IS_REG && this.peek == character.SLASH && !{ '/': true, '*': true }[this.code.charAt(this.index)]) {
47493 this.dealReg(temp, length);
48489 this.isReg = Lexer.NOT_REG;
49 }
50 //依次遍历匹配规则,命中则继续
51 else {
52481356 for(var i = 0, matches = this.rule.matches(), len = matches.length; i < len; i++) {
537526071 var match = matches[i];
547526071 if(match.match(this.peek, this.code, this.index)) {
55481355 var token = new Token(match.tokenType(), match.content(), match.val(), this.index - 1);
56481355 var error = match.error();
57481355 var matchLen = match.content().length;
58481355 if(token.type() == Token.ID && this.rule.keyWords().hasOwnProperty(token.content())) {
5921478 token.type(Token.KEYWORD);
60 }
61481355 temp.push(token);
62481355 this.tokenList.push(token);
63481355 this.index += matchLen - 1;
64481355 var n = character.count(token.val(), character.LINE);
65481355 count += n;
66481355 this.totalLine += n;
67481355 if(n) {
6832320 var j = match.content().indexOf(character.LINE);
6932320 var k = match.content().lastIndexOf(character.LINE);
7032320 this.colMax = Math.max(this.colMax, this.colNum + j);
7132320 this.colNum = match.content().length - k;
72 }
73 else {
74449035 this.colNum += matchLen;
75 }
76481355 this.colMax = Math.max(this.colMax, this.colNum);
77481355 if(error) {
785 this.error(error, this.code.slice(this.index - matchLen, this.index));
79 }
80 //支持perl正则需判断关键字、圆括号对除号语义的影响
81481351 if(perlReg && match.perlReg() != Lexer.IGNORE) {
82258261 if(match.perlReg() == Lexer.SPECIAL) {
8396040 this.isReg = match.special();
84 }
85 else {
86162221 this.isReg = match.perlReg();
87 }
88258261 if(this.peek == character.LEFT_PARENTHESE) {
8919876 this.parentheseStack.push(this.parentheseState);
9019876 this.parentheseState = false;
91 }
92238385 else if(this.peek == character.RIGHT_PARENTHESE) {
9319860 this.isReg = this.parentheseStack.pop() ? Lexer.IS_REG : Lexer.NOT_REG;
94 }
95 else {
96218525 this.parentheseState = match.parenthese();
97 }
98 }
99481351 continue outer;
100 }
101 }
102 //如果有未匹配的,说明规则不完整,加入other类型并抛出警告
1031 this.error('unknow token');
104 }
105 }
106201 return this;
107 },
108 readch: function() {
109490711 this.peek = this.code.charAt(this.index++);
110 },
111 dealReg: function(temp, length) {
112493 var lastIndex = this.index - 1;
113493 var res = false;
114 outer:
115 do {
1166803 this.readch();
1176803 if(this.peek == character.LINE) {
1182 this.error('SyntaxError: unterminated regular expression literal ' + this.peek, this.code.slice(lastIndex, this.index));
1191 break;
120 }
1216801 else if(this.peek == character.BACK_SLASH) {
122490 this.index++;
123 }
1246311 else if(this.peek == character.LEFT_BRACKET) {
125264 do {
1261368 this.readch();
1271368 if(this.peek == character.LINE) {
1282 this.error('SyntaxError: unterminated regular expression literal ' + this.peek, this.code.slice(lastIndex, this.index));
1291 break outer;
130 }
1311366 else if(this.peek == character.BACK_SLASH) {
132265 this.index++;
133 }
1341101 else if(this.peek == character.RIGHT_BRACKET) {
135262 continue outer;
136 }
137 } while(this.index < length);
138 }
1396047 else if(this.peek == character.SLASH) {
140488 res = true;
141488 var hash = {};
142488 var flag = {
143 'g': true,
144 'i': true,
145 'm': true,
146 'y': true
147 };
148 //正则的flag中有gimy4种,大小写敏感且不能重复
149488 do {
150691 this.readch();
151691 if(character.isLetter(this.peek)) {
152205 if(hash.hasOwnProperty(this.peek) || !flag.hasOwnProperty(this.peek)) {
1532 this.error('SyntaxError: invalid regular expression flag ' + this.peek, this.code.slice(lastIndex, this.index));
1541 break outer;
155 }
156203 hash[this.peek] = true;
157 }
158 else {
159486 break outer;
160 }
161 } while(this.index <= length);
162 }
163 } while(this.index < length);
164490 if(!res) {
1653 this.error('SyntaxError: unterminated regular expression literal', this.code.slice(lastIndex, this.index - 1));
166 }
167489 var token = new Token(Token.REG, this.code.slice(lastIndex, --this.index), lastIndex);
168489 temp.push(token);
169489 this.tokenList.push(token);
170489 this.colNum += this.index - lastIndex;
171489 this.colMax = Math.max(this.colMax, this.colNum);
172489 return this;
173 },
174 cache: function(i) {
1751 if(!character.isUndefined(i) && i !== null) {
1761 this.cacheLine = i;
177 }
1781 return this.cacheLine;
179 },
180 finish: function() {
1812 return this.index >= this.code.length;
182 },
183 line: function() {
18416 return this.totalLine;
185 },
186 col: function() {
1871 return this.colMax;
188 },
189 error: function(s, str) {
19015 if(character.isUndefined(str)) {
1911 str = this.code.substr(this.index - 1, 20);
192 }
19315 if(Lexer.mode() === Lexer.STRICT) {
1949 throw new Error(s + ', line ' + this.line() + ' col ' + this.colNum + '\n' + str);
195 }
1966 else if(Lexer.mode() === Lexer.LOOSE && !character.isUndefined(console)) {
1976 if(console.warn) {
1986 console.warn(s + ', line ' + this.line() + ' col ' + this.colNum + '\n' + str);
199 }
200 }
2016 return this;
202 }
203}).statics({
204 IGNORE: 0,
205 IS_REG: 1,
206 NOT_REG: 2,
207 SPECIAL: 3,
208 STRICT: 0,
209 LOOSE: 1,
210 mode: function(i) {
21133 if(!character.isUndefined(i)) {
21210 cmode = i;
213 }
21433 return cmode;
215 }
216});
2171var cmode = Lexer.STRICT;
2181module.exports = Lexer;

D:/www/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) {
6483430 this.t = type; //token类型
7483430 this.c = content; //token的字面内容,string包括头尾的引号
8483430 if(character.isNumber(val)) {
9494 sIndex = val;
10494 val = content;
11 }
12482936 else if(character.isUndefined(val)) {
131581 val = content;
141581 sIndex = -1;
15 }
16483430 this.v = val; //token的值,一般情况下等于content,特殊如string情况下值是不加头尾的引号
17483430 this.id = tid++; //token的索引
18483430 this.si = sIndex; //token在源码字符串中的索引
19}).methods({
20 type: function(t) {
211723994 if(!character.isUndefined(t)) {
2221478 this.t = t;
23 }
241723994 return this.t;
25 },
26 content: function(c) {
271709519 if(!character.isUndefined(c)) {
281 this.c = c;
29 }
301709519 return this.c;
31 },
32 val: function(v) {
33481357 if(!character.isUndefined(v)) {
341 this.v = v;
35 }
36481357 return this.v;
37 },
38 tag: function(t) {
392 if(!character.isUndefined(t)) {
401 this.t = t;
41 }
422 return Token.type(this.t);
43 },
44 tid: function(id) {
452 if(!character.isUndefined(id)) {
461 this.id = id;
47 }
482 return this.id;
49 },
50 sIndex: function(si) {
516 if(!character.isUndefined(si)) {
521 this.si = si;
53 }
546 return this.si;
55 }
56}).statics({
57 IGNORE: -2,
58 VIRTUAL: -1,
59 OTHER: 0,
60 BLANK: 1,
61 TAB: 2,
62 LINE: 3,
63 NUMBER: 4,
64 ID: 5,
65 COMMENT: 6,
66 STRING: 7,
67 SIGN: 8,
68 REG: 9,
69 KEYWORD: 10,
70 ANNOT: 11,
71 HEAD: 12,
72 TEMPLATE: 13,
73 ENTER: 14,
74 PROPERTY: 15,
75 VARS: 16,
76 HACK: 17,
77 IMPORTANT: 18,
78 type: function(tag) {
796 if(character.isUndefined(types)) {
801 types = [];
811 Object.keys(Token).forEach(function(o) {
8225 if(typeof Token[o] == 'number') {
8321 types[Token[o]] = o;
84 }
85 });
86 }
876 return types[tag];
88 }
89});
901module.exports = Token;

D:/www/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) {
4492 Match.call(this, type, setPReg);
5492 this.str = str;
6}).methods({
7 match: function(c, code, index) {
8140959 var isIn = this.str.indexOf(c) > -1;
9140959 if(isIn) {
10133448 this.result = c;
11 }
12140959 return isIn;
13 }
14});
151module.exports = CharacterSet;

D:/www/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) {
48246 Match.call(this, type, setPReg);
58246 this.result = result;
6}).methods({
7 match: function(c, code, index) {
85678753 return code.substr(--index, this.result.length) == this.result;
9 }
10});
111module.exports = CompleteEqual;

D:/www/homunculus/src/lexer/match/LineParse.js

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

D:/www/homunculus/src/lexer/match/LineSearch.js

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

D:/www/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) {
510988 this.type = type;
610988 if(character.isUndefined(setPReg)) {
71886 setPReg = Lexer.IGNORE;
8 }
910988 this.setPReg = setPReg;
1010988 this.result = null;
11 //忽略0,是1,否2,特殊3
1210988 if(setPReg) {
139102 if(character.isUndefined(special)) {
148856 special = function() {
150 return Lexer.IGNORE;
16 };
17 }
189102 if(character.isUndefined(parenthese)) {
198856 parenthese = function() {
20122485 return false;
21 };
22 }
23 }
2410988 this.special = special;
2510988 this.parenthese = parenthese;
26}).methods({
27 tokenType: function() {
28481355 return this.type;
29 },
30 perlReg: function() {
31901833 return this.setPReg;
32 },
33 val: function() {
34474528 return this.content();
35 },
36 content: function() {
371733105 return this.result;
38 },
39 match: function(c, code, index) {
40 //需被实现
410 throw new Error('match needs to be implement');
42 },
43 error: function() {
44365970 return false;
45 }
46});

D:/www/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) {
31012 if(typeof valid == 'number') {
4246 parenthese = special;
5246 special = setPReg;
6246 setPReg = valid;
7246 valid = null;
8 }
91012 Match.call(this, type, setPReg, special, parenthese);
101012 this.reg = reg;
111012 this.valid = valid;
12 }).methods({
13 match: function(c, code, index) {
14421845 var self = this,
15 res = self.reg.exec(code.slice(index - 1));
16421845 self.msg = null;
17421845 if(res) {
18103557 self.result = res[0];
19103557 if(self.valid) {
207517 for(var i = 0, keys = Object.keys(self.valid), len = keys.length; i < len; i++) {
217517 if(self.valid[keys[i]].test(self.result)) {
222 self.msg = keys[i];
232 break;
24 }
25 }
26 }
27103557 return true;
28 }
29318288 return false;
30 },
31 error: function() {
32103557 return this.msg;
33 }
34 });
351module.exports = RegMatch;

D:/www/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() {
11246 var self = this;
12246 Rule.call(self, EcmascriptRule.KEYWORDS, true);
13
14246 self.addMatch(new CompleteEqual(Token.BLANK, character.BLANK));
15246 self.addMatch(new CompleteEqual(Token.TAB, character.TAB));
16246 self.addMatch(new CompleteEqual(Token.ENTER, character.ENTER));
17246 self.addMatch(new CompleteEqual(Token.LINE, character.LINE));
18
19246 self.addMatch(new LineSearch(Token.COMMENT, '//', '\n'));
20246 self.addMatch(new LineSearch(Token.COMMENT, '/*', '*/', true));
21246 self.addMatch(new LineParse(Token.STRING, '"', '"', false, Lexer.IS_REG));
22246 self.addMatch(new LineParse(Token.STRING, "'", "'", false, Lexer.IS_REG));
23246 self.addMatch(new LineParse(Token.TEMPLATE, '`', '`', true, Lexer.IS_REG));
24
25246 self.addMatch(new RegMatch(Token.ID, /^[$a-zA-Z_][$\w]*/, Lexer.SPECIAL, function() {
2696040 return !!(self.keyWords().hasOwnProperty(this.content()));
27 }, function() {
2896040 return ['if', 'for', 'while'].indexOf(this.content()) != -1;
29 }));
30
31246 self.addMatch(new RegMatch(Token.NUMBER, /^\.\d+(?:E[+-]?\d*)?/i, {
32 'SyntaxError: missing exponent': /E[+-]?$/i
33 }, Lexer.NOT_REG));
34
35246 self.addMatch(new CompleteEqual(Token.SIGN, ']', Lexer.NOT_REG));
36
37246 ['*=', '/=', '+=', '-=', '%=', '^=', '&=', '|=', '&&', '--', '++', '===', '==', '!==', '!=', '||', '>>>=', '<<<=', '<<<', '>>>', '>>=', '<<=', '<<', '>>', '>=', '<=', '...', '?:'].forEach(function(o) {
386888 self.addMatch(new CompleteEqual(Token.SIGN, o, Lexer.IS_REG));
39 });
40246 self.addMatch(new CharacterSet(Token.SIGN, ':;/?.,[]{}~!^|%=-+*()~><&\\', Lexer.IS_REG));
41
42246 self.addMatch(new RegMatch(Token.NUMBER, /^0x[\da-f]*/i, {
43 "SyntaxError: missing hexadecimal digits after '0x'": /^0x$/i
44 }, Lexer.NOT_REG));
45246 self.addMatch(new RegMatch(Token.NUMBER, /^\d+\.?\d*(?:E[+-]?\d*)?/i, {
46 'SyntaxError: missing exponent': /E[+-]?$/i
47 }, Lexer.NOT_REG));
48
49246 self.addMatch(new CharacterSet(Token.BLANK, '\u00a0\f\u000b\u200b\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000'));
50}).statics({
51 KEYWORDS: 'abstract boolean break byte case catch class const continue debugger default delete do double else enum export extends false final finally float for function goto if implements import in instanceof int interface let long native new null package private protected public return short static super switch synchronized this throw throws transient true try typeof var void volatile while with'.split(' ')
52});
531module.exports = EcmascriptRule;

D:/www/homunculus/src/lexer/rule/Rule.js

100%
14
14
0
LineHitsSource
11var Class = require('../../util/Class');
21var Rule = Class(function(words, pReg) {
3248 var self = this;
4248 self.kw = {};
5248 words.forEach(function(o) {
614884 self.kw[o] = true;
7 });
8248 self.pReg = pReg || false;
9248 self.matchList = [];
10 }).methods({
11 perlReg: function() {
12210 return this.pReg;
13 },
14 addMatch: function(match) {
1510988 this.matchList.push(match);
1610988 return this;
17 },
18 matches: function() {
19481356 return this.matchList;
20 },
21 keyWords: function() {
22192080 return this.kw;
23 }
24 });
251module.exports = Rule;

D:/www/homunculus/src/parser/js/Context.js

100%
165
165
0
LineHitsSource
11var Class = require('../../util/Class');
21var JsNode = require('./Node');
31var Token = require('../../lexer/Token');
41var Parser = require('./Parser');
51var id = 0;
61var Context = Class(function(parent, name) {
757 this.id = id++;
857 this.parser = new Parser();
957 this.parent = parent || null; //父上下文,如果是全局则为空
1057 this.name = name || null; //上下文名称,即函数名,函数表达式为空,全局也为空
1157 this.children = []; //函数声明或函数表达式所产生的上下文
1257 this.childrenMap = Object.create(null); //键是函数名,值是上下文,匿名函数表达式键为cid
1357 this.vars = []; //变量var声明
1457 this.varsMap = Object.create(null); //键为id字面量,值是它的token的节点
1557 this.vardeclMap = Object.create(null); //var赋值记录,优先级vardecl > fndecl > varnodecl
1657 this.params = []; //形参,函数上下文才有,即全局无
1757 this.paramsMap = Object.create(null); //键为id字面量,值是它的token的节点
1857 this.aParams = []; //实参,函数表达式才有
1957 this.vids = []; //上下文环境里用到的变量id
2057 this.vidsMap = Object.create(null); //键为id字面量,值是它的token的节点
2157 this.returns = []; //上下文环境里return语句
2257 this.node = null; //对应的ast的节点
2357 if(!this.isTop()) {
2422 this.parent.addChild(this);
25 }
26}).methods({
27 parse: function(code) {
2831 var ast;
2931 if(code instanceof JsNode) {
301 ast = code;
31 }
32 else {
3330 ast = this.parser.parse(code);
34 }
3531 recursion(ast, this);
3631 return this;
37 },
38 getId: function() {
3921 return this.id;
40 },
41 getName: function() {
4223 return this.name;
43 },
44 getParent: function() {
452 return this.parent;
46 },
47 isTop: function() {
4858 return !this.parent;
49 },
50 isFnexpr: function() {
511 return !this.isTop() && !this.name;
52 },
53 hasParam: function(p) {
546 return p in this.paramsMap;
55 },
56 getParams: function() {
571 return this.params;
58 },
59 addParam: function(p) {
60 //形参不可能重复,无需判断
616 this.paramsMap[p] = this.params.length;
626 this.params.push(p);
636 return this;
64 },
65 getAParams: function() {
661 return this.aParams;
67 },
68 addAParam: function(ap) {
694 this.aParams.push(ap);
704 return this;
71 },
72 getChild: function(name) {
737 return this.childrenMap[name];
74 },
75 getChildren: function() {
7611 return this.children;
77 },
78 //通过name查找函数声明,id查找表达式
79 hasChild: function(name) {
8044 return name in this.childrenMap;
81 },
82 addChild: function(child) {
8322 var name = child.getName();
84 //函数表达式名字为空用id删除
8522 if(name) {
8612 if(name in this.vardeclMap) {
871 return this;
88 }
8911 this.delVar(name);
9011 this.delChild(name);
91 }
92 else {
9310 this.delChild(child.getId());
94 }
9521 name = name || child.getId();
9621 this.childrenMap[name] = child;
9721 this.children.push(child);
9821 return this;
99 },
100 //name函数声明,id表达式
101 delChild: function(name) {
10234 if(this.hasChild(name)) {
1031 var i = this.children.indexOf(this.childrenMap[name]);
1041 this.children.splice(i, 1);
1051 delete this.childrenMap[name];
106 }
10734 return this;
108 },
109 hasVar: function(v) {
11038 return v in this.varsMap;
111 },
112 addVar: function(node, assign) {
11317 var v = node.leaves()[0].token().content();
114 //赋值拥有最高优先级,会覆盖掉之前的函数声明和var
11517 if(assign) {
11613 this.delVar(v);
11713 this.delChild(v);
11813 this.vardeclMap[v] = true;
119 }
120 //仅仅是var声明无赋值,且已有过声明或函数,忽略之
1214 else if(this.hasVar(v) || this.hasChild(v)) {
1222 return this;
123 }
12415 this.varsMap[v] = node;
12515 this.vars.push(node);
12615 return this;
127 },
128 delVar: function(v) {
12924 if(this.hasVar(v)) {
1302 var i = this.vars.indexOf(this.varsMap[v]);
1312 this.vars.splice(i, 1);
1322 delete this.varsMap[v];
133 }
13424 return this;
135 },
136 getVars: function() {
1374 return this.vars;
138 },
139 addReturn: function(node) {
1405 this.returns.push(node);
1415 return this;
142 },
143 getReturns: function() {
1444 return this.returns;
145 },
146 hasVid: function(v) {
1478 return v in this.vidsMap;
148 },
149 getVid: function(v) {
1501 return this.vidsMap[v];
151 },
152 addVid: function(node) {
15312 var v = node.token().content();
15412 this.vids.push(node);
15512 this.vidsMap[v] = this.vidsMap[v] || [];
15612 this.vidsMap[v].push(node);
15712 return this;
158 },
159 getVids: function() {
1602 return this.vids;
161 },
162 getNode: function() {
1631 return this.node;
164 },
165 setNode: function(n) {
16622 this.node = n;
16722 return this;
168 }
169});
170
1711function recursion(node, context) {
172545 var isToken = node.name() == JsNode.TOKEN;
173545 var isVirtual = isToken && node.token().type() == Token.VIRTUAL;
174545 if(isToken) {
175315 if(!isVirtual) {
176309 var token = node.token();
177309 var s = token.content();
178309 if(s == 'return') {
1795 context.addReturn(node);
180 }
181 }
182 }
183 else {
184230 if(node.name() == JsNode.VARDECL) {
18517 vardecl(node, context);
186 }
187213 else if(node.name() == JsNode.FNDECL) {
18812 context = fndecl(node, context);
189 }
190201 else if(node.name() == JsNode.FNEXPR) {
19110 context = fnexpr(node, context);
192 }
193191 else if(node.name() == JsNode.PRMREXPR) {
19439 prmrexpr(node, context);
195 }
196230 node.leaves().forEach(function(leaf, i) {
197514 recursion(leaf, context);
198 });
199 }
200}
2011function vardecl(node, context) {
20217 var leaves = node.leaves();
20317 var assign = !!leaves[1];
20417 context.addVar(node, assign);
205}
2061function fndecl(node, context) {
20712 var v = node.leaves()[1].leaves().content();
20812 var child = new Context(context, v);
20912 child.setNode(node);
21012 var params = node.leaves()[3];
21112 if(params.name() == JsNode.FNPARAMS) {
2121 addParam(params, child);
213 }
21412 return child;
215}
2161function fnexpr(node, context) {
217 //函数表达式name为空
21810 var child = new Context(context);
21910 child.setNode(node);
220 //记录形参
22110 var params;
22210 var v = node.leaves()[1];
22310 if(v.token().content() != '(') {
2244 params = node.leaves()[3];
225 }
226 else {
2276 params = node.leaves()[2];
228 }
22910 if(params.name() == JsNode.FNPARAMS) {
2303 addParam(params, child);
231 }
232 //匿名函数检查实参传入情况
23310 var next = node.next();
234 //!function(){}()形式
23510 if(next && next.name() == JsNode.ARGS) {
2367 var leaves = next.leaves();
237 //长度2为()空参数,长度3有参数,第2个节点
2387 if(leaves.length == 3) {
2391 addAParam(leaves[1], child);
240 }
241 }
242 //(function(){})()形式
243 else {
2443 var prmr = node.parent();
2453 var prev = node.prev();
2463 if(prmr.name() == JsNode.PRMREXPR && prev && prev.name() == JsNode.TOKEN && prev.token().content() == '(') {
2473 next = prmr.next();
2483 if(next && next.name() == JsNode.ARGS) {
2493 var leaves = next.leaves();
250 //长度2为()空参数,长度3有参数,第2个节点
2513 if(leaves.length == 3) {
2521 addAParam(leaves[1], child);
253 }
254 }
255 }
256 }
25710 return child;
258}
259//支持es6
2601function addParam(params, child) {
2614 params.leaves().forEach(function(leaf, i) {
2629 if(leaf.name() == JsNode.TOKEN && leaf.token().content() != ',') {
2635 child.addParam(leaf.token().content());
264 }
2654 else if(leaf.name() == JsNode.RESTPARAM) {
2661 child.addParam(leaf.leaves()[1].token().content());
267 }
268 });
269}
2701function addAParam(params, child) {
2712 params.leaves().forEach(function(leaf, i) {
2726 if(i % 2 == 0) {
2734 child.addAParam(leaf);
274 }
275 });
276}
2771function prmrexpr(node, context) {
27839 var first = node.leaves()[0];
27939 if(first.name() == JsNode.TOKEN) {
28037 var token = first.token();
28137 if(token.type() == Token.ID) {
28212 context.addVid(first);
283 }
284 }
285}
286
2871module.exports = Context;

D:/www/homunculus/src/parser/js/Node.js

97%
45
44
1
LineHitsSource
11var Class = require('../../util/Class');
21var Node = Class(function(type, children) {
3769731 this.type = type;
4769731 if(type == Node.TOKEN) {
5140062 this.children = children;
6 }
7629669 else if(Array.isArray(children)) {
80 this.children = children;
9 }
10 else {
11629669 this.children = children ? [children] : [];
12 }
13769731 this.p = null;
14769731 this.pr = null;
15769731 this.ne = null;
16769731 return this;
17}).methods({
18 name: function() {
19240092 return this.type;
20 },
21 leaves: function() {
2298793 return this.children;
23 },
24 add: function() {
25166670 var self = this;
26166670 var args = Array.prototype.slice.call(arguments, 0);
27166670 args.forEach(function(node) {
28238547 node.parent(self);
29238547 var last = self.children[self.children.length - 1];
30238547 if(last) {
31139912 last.next(node);
32139912 node.prev(last);
33 }
34238547 self.children.push(node);
35 });
36166670 return self;
37 },
38 token: function() {
39278424 return this.children;
40 },
41 parent: function(p) {
42238553 if(p) {
43238547 this.p = p;
44 }
45238553 return this.p;
46 },
47 prev: function(pr) {
48139917 if(pr) {
49139912 this.pr = pr;
50 }
51139917 return this.pr;
52 },
53 next: function(ne) {
54139926 if(ne) {
55139912 this.ne = ne;
56 }
57139926 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;

D:/www/homunculus/src/parser/js/Parser.js

96%
661
640
21
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) {
10205 this.init(lexer);
11}).methods({
12 parse: function(code) {
13174 this.lexer.parse(code);
14174 this.tree = this.program();
15147 return this.tree;
16 },
17 ast: function() {
184 return this.tree;
19 },
20 init: function(lexer) {
21206 this.look = null;
22206 this.tokens = null;
23206 this.lastLine = 1;
24206 this.lastCol = 1;
25206 this.line = 1;
26206 this.col = 1;
27206 this.index = 0;
28206 this.length = 0;
29206 this.ignores = {};
30206 this.hasMoveLine = false;
31206 this.tree = {};
32206 if(lexer) {
33148 this.lexer = lexer;
34 }
3558 else if(this.lexer) {
361 this.lexer.init();
37 }
38 else {
3957 this.lexer = new Lexer(new Rule());
40 }
41 },
42 program: function() {
43174 this.tokens = this.lexer.tokens();
44174 this.length = this.tokens.length;
45174 if(this.tokens.length) {
46172 this.move();
47 }
48174 var node = new Node(Node.PROGRAM);
49174 while(this.look) {
50206 node.add(this.element());
51 }
52147 return node;
53 },
54 element: function(allowSuper) {
555374 if(this.look.content() == 'function') {
56198 return this.fndecl();
57 }
585176 else if(this.look.content() == 'class') {
5912 return this.classdecl();
60 }
61 else {
625164 return this.stmt(allowSuper);
63 }
64 },
65 stmt: function(allowSuper) {
6610850 if(!this.look) {
671 this.error();
68 }
6910849 switch(this.look.content()) {
70 case 'let':
712 return this.letstmt();
72 case 'const':
732 return this.cststmt();
74 case 'var':
751160 return this.varstmt();
76 case '{':
771791 return this.block();
78 case ';':
7935 return this.emptstmt();
80 case 'if':
811692 return this.ifstmt();
82 case 'do':
83 case 'while':
84 case 'for':
85458 return this.iterstmt();
86 case 'continue':
8718 return this.cntnstmt();
88 case 'break':
89123 return this.brkstmt();
90 case 'return':
911636 return this.retstmt();
92 case 'with':
931 return this.withstmt();
94 case 'switch':
958 return this.swchstmt();
96 case 'throw':
9739 return this.thrstmt();
98 case 'try':
9955 return this.trystmt();
100 case 'debugger':
1011 return this.debstmt();
102 case 'super':
1033 if(!allowSuper) {
1041 this.error('super must in a class');
105 }
1062 return this.superstmt();
107 case 'import':
1080 return this.imptstmt();
109 default:
1103825 if(this.look.type() == Token.ID) {
1112943 for(var i = this.index; i < this.length; i++) {
1123962 var token = this.tokens[i];
1133962 if(!S[token.type()]) {
1142940 if(token.content() == ':') {
1152 return this.labstmt();
116 }
117 else {
1182938 return this.exprstmt();
119 }
120 }
121 }
122 }
123885 return this.exprstmt();
124 }
125 },
126 exprstmt: function() {
1273823 var node = new Node(Node.EXPRSTMT);
1283823 node.add(this.expr(), this.match(';'));
1293814 return node;
130 },
131 cststmt: function(noSem) {
1322 var node = new Node(Node.CSTSTMT);
1332 node.add(
134 this.match('const'),
135 this.vardecl()
136 );
1372 while(this.look && this.look.content() == ',') {
1381 node.add(
139 this.match(),
140 this.vardecl()
141 );
142 }
1432 if(!noSem) {
1442 node.add(this.match(';'));
145 }
1462 return node;
147 },
148 letstmt: function(noSem) {
1492 var node = new Node(Node.LETSTMT);
1502 node.add(
151 this.match('let'),
152 this.vardecl()
153 );
1542 while(this.look && this.look.content() == ',') {
1551 node.add(
156 this.match(),
157 this.vardecl()
158 );
159 }
1602 if(!noSem) {
1612 node.add(this.match(';'));
162 }
1632 return node;
164 },
165 varstmt: function(noSem) {
1661244 var node = new Node(Node.VARSTMT);
1671244 node.add(
168 this.match('var'),
169 this.vardecl()
170 );
1711242 while(this.look && this.look.content() == ',') {
1721555 node.add(
173 this.match(),
174 this.vardecl()
175 );
176 }
1771242 if(!noSem) {
1781158 node.add(this.match(';'));
179 }
1801242 return node;
181 },
182 vardecl: function() {
1832805 var node = new Node(Node.VARDECL);
1842805 if(!this.look) {
1851 this.error('missing variable name');
186 }
1872804 if(this.look.content() == '[') {
1880 node.add(this.arrltr());
189 }
190 else {
1912804 node.add(this.match(Token.ID, 'missing variable name'));
192 }
1932804 if(this.look && this.look.content() == '=') {
1941919 node.add(this.assign());
195 }
1962803 return node;
197 },
198 assign: function() {
1991919 var node = new Node(Node.ASSIGN);
2001919 node.add(this.match('='));
2011919 if(!this.look) {
2021 this.error();
203 }
2041918 node.add(this.assignexpr());
2051918 return node;
206 },
207 block: function() {
2081906 var node = new Node(Node.BLOCK);
2091906 node.add(this.match('{'));
2101906 while(this.look && this.look.content() != '}') {
2112988 node.add(this.stmt());
212 }
2131906 node.add(this.match('}', 'missing } in compound statement'));
2141906 return node;
215 },
216 emptstmt: function() {
21735 var node = new Node(Node.EMPTSTMT);
21835 node.add(this.match(';'));
21935 return node;
220 },
221 ifstmt: function() {
2221692 var node = new Node(Node.IFSTMT);
2231692 node.add(
224 this.match('if'),
225 this.match('('),
226 this.expr(),
227 this.match(')'),
228 this.stmt()
229 );
2301691 if(this.look && this.look.content() == 'else') {
231325 node.add(
232 this.match('else'),
233 this.stmt()
234 );
235 }
2361691 return node;
237 },
238 iterstmt: function() {
239458 var node = new Node(Node.ITERSTMT);
240458 switch(this.look.content()) {
241 case 'do':
2429 node.add(
243 this.match(),
244 this.stmt(),
245 this.match('while'),
246 this.match('('),
247 this.expr(),
248 this.match(')'),
249 this.match(';')
250 );
2519 break;
252 case 'while':
253152 node.add(
254 this.match(),
255 this.match('('),
256 this.expr(),
257 this.match(')'),
258 this.stmt()
259 );
260152 break;
261 case 'for':
262297 node.add(
263 this.match(),
264 this.match('(')
265 );
266297 if(!this.look) {
2671 this.error();
268 }
269296 if(this.look.content() == 'var' || this.look.content() == 'let') {
27084 var node2 = this.look.content() == 'var' ? this.varstmt(true) : this.letstmt(true);
27184 if(!this.look) {
2721 this.error('missing ; after for-loop initializer');
273 }
27483 if(this.look.content() == 'in') {
27519 if(node2.leaves().length > 2) {
2761 this.error('invalid for/in left-hand side');
277 }
27818 node.add(node2);
27918 node.add(
280 this.match(),
281 this.expr()
282 );
283 }
284 else {
28564 node.add(node2);
28664 node.add(this.match(';'));
28764 if(this.look.content() != ';') {
28863 node.add(this.expr());
289 }
29063 node.add(this.match(';'));
29163 if(!this.look) {
2921 this.error();
293 }
29462 if(this.look.content() != ')') {
29559 node.add(this.expr());
296 }
297 }
298 }
299 else {
300212 if(this.look.content() == 'in') {
3011 this.error();
302 }
303211 var hasIn = false;
304211 for(var i = this.index; i < this.length; i++) {
3051924 var t = this.tokens[i];
3061924 if(t.content() == 'in') {
30787 hasIn = true;
30887 break;
309 }
3101837 else if(t.content() == ')') {
311120 break;
312 }
313 }
314211 if(hasIn) {
31587 node.add(this.expr(true), this.match('in'), this.expr());
316 }
317 else {
318124 if(this.look.content() != ';') {
31968 node.add(this.expr());
320 }
321 //for的;不能省略,强制判断
322124 if(!this.look || this.look.content() != ';') {
3231 this.error('missing ;')
324 }
325123 node.add(this.match(';'));
326123 if(!this.look) {
3271 this.error();
328 }
329122 if(this.look.content() != ';') {
330121 node.add(this.expr());
331 }
332122 if(!this.look || this.look.content() != ';') {
3331 this.error('missing ;')
334 }
335121 node.add(this.match(';'));
336121 if(!this.look) {
3371 this.error();
338 }
339120 if(this.look.content() != ')') {
340112 node.add(this.expr());
341 }
342 }
343 }
344287 node.add(this.match(')'));
345287 node.add(this.stmt());
346 }
347448 return node;
348 },
349 cntnstmt: function() {
35018 var node = new Node(Node.CNTNSTMT);
35118 node.add(this.match('continue', true));
35218 if(this.look && this.look.type() == Token.ID) {
3531 node.add(this.match());
354 }
35518 node.add(this.match(';'));
35618 return node;
357 },
358 brkstmt: function() {
359123 var node = new Node(Node.BRKSTMT);
360123 node.add(this.match('break', true));
361123 if(this.look && this.look.type() == Token.ID) {
3621 node.add(this.match());
363 }
364123 node.add(this.match(';'));
365123 return node;
366 },
367 retstmt: function() {
3681636 var node = new Node(Node.RETSTMT);
3691636 node.add(this.match('return', true));
370 //return后换行视作省略;,包括多行注释的换行
3711636 if(this.look) {
3721635 if(this.look.content() == ';'
373 || this.look.content() == '}'
374 || this.look.type() == Token.LINE
375 || this.look.type() == Token.COMMENT) {
37682 node.add(this.match(';'));
377 }
378 else {
3791553 node.add(this.expr(), this.match(';'));
380 }
381 }
382 else {
3831 node.add(this.match(';'));
384 }
3851636 return node;
386 },
387 withstmt: function() {
3881 var node = new Node(Node.WITHSTMT);
3891 node.add(
390 this.match('with'),
391 this.match('('),
392 this.expr(),
393 this.match(')'),
394 this.stmt()
395 );
3961 return node;
397 },
398 swchstmt: function() {
3998 var node = new Node(Node.SWCHSTMT);
4008 node.add(
401 this.match('switch'),
402 this.match('('),
403 this.expr(),
404 this.match(')'),
405 this.caseblock()
406 );
4077 return node;
408 },
409 caseblock: function() {
4108 var node = new Node(Node.CASEBLOCK);
4118 node.add(this.match('{'));
4128 while(this.look && this.look.content() != '}') {
413102 if(this.look.content() == 'case') {
41499 node.add(this.caseclause());
415 }
4163 else if(this.look.content() == 'default') {
4172 node.add(this.dftclause());
418 }
419 else {
4201 this.error('invalid switch statement');
421 }
422 }
4237 node.add(this.match('}'));
4247 return node;
425 },
426 caseclause: function() {
42799 var node = new Node(Node.CASECLAUSE);
42899 node.add(
429 this.match('case'),
430 this.expr(),
431 this.match(':')
432 );
43399 while(this.look
434 && this.look.content() != 'case'
435 && this.look.content() != 'default'
436 && this.look.content() != '}') {
437228 node.add(this.stmt());
438 }
43999 return node;
440 },
441 dftclause: function() {
4422 var node = new Node(Node.DFTCLAUSE);
4432 node.add(
444 this.match('default'),
445 this.match(':')
446 );
4472 while(this.look && this.look.content() != '}') {
4482 node.add(this.stmt());
449 }
4502 return node;
451 },
452 labstmt: function() {
4532 var node = new Node(Node.LABSTMT);
4542 node.add(
455 this.match(Token.ID),
456 this.match(':'),
457 this.stmt()
458 );
4592 return node;
460 },
461 thrstmt: function() {
46239 var node = new Node(Node.THRSTMT);
46339 node.add(
464 this.match('throw', true),
465 this.expr(),
466 this.match(';')
467 );
46839 return node;
469 },
470 trystmt: function() {
47155 var node = new Node(Node.TRYSTMT);
47255 node.add(
473 this.match('try'),
474 this.block()
475 );
47655 if(this.look && this.look.content() == 'catch') {
47754 node.add(this.cach());
47854 if(this.look && this.look.content() == 'finally') {
4795 node.add(this.finl());
480 }
481 }
482 else {
4831 node.add(this.finl());
484 }
48555 return node;
486 },
487 debstmt: function() {
4881 var node = new Node(Node.DEBSTMT);
4891 node.add(this.match('debugger'), this.match(';'));
4901 return node;
491 },
492 cach: function() {
49354 var node = new Node(Node.CACH);
49454 node.add(
495 this.match('catch'),
496 this.match('('),
497 this.match(Token.ID, 'missing identifier in catch'),
498 this.match(')'),
499 this.block()
500 );
50154 return node;
502 },
503 finl: function() {
5046 var node = new Node(Node.FINL);
5056 node.add(
506 this.match('finally'),
507 this.block()
508 );
5096 return node;
510 },
511 superstmt: function() {
5122 var node = new Node(Node.SUPERSTMT);
5132 node.add(this.match('super'));
5142 if(!this.look) {
5150 this.error();
516 }
5172 if(this.look.content() == '.') {
5181 while(this.look && this.look.content() == '.') {
5192 node.add(this.match());
5202 if(!this.look) {
5210 this.error();
522 }
5232 if(this.look.content() == 'super') {
5241 node.add(this.match());
525 }
526 else {
5271 break;
528 }
529 }
5301 if(this.look.content() != '(') {
5311 node.add(this.match(Token.ID));
5321 while(this.look && this.look.content() == '.') {
5330 node.add(this.match(), this.match(Token.ID));
534 }
535 }
536 }
5372 node.add(
538 this.args(),
539 this.match(';')
540 );
5412 return node;
542 },
543 imptstmt: function() {
5440 var node = new Node(Node.IMPTSTMT);
5450 return node;
546 },
547 fndecl: function() {
548198 var node = new Node(Node.FNDECL);
549198 node.add(
550 this.match('function'),
551 this.match(Token.ID, 'function statement requires a name'),
552 this.match('(')
553 );
554198 if(!this.look) {
5550 this.error('missing formal parameter');
556 }
557198 if(this.look.content() != ')') {
558155 node.add(this.fnparams());
559 }
560198 node.add(
561 this.match(')'),
562 this.match('{'),
563 this.fnbody(),
564 this.match('}')
565 );
566198 return node;
567 },
568 fnexpr: function() {
5691621 var node = new Node(Node.FNEXPR);
5701621 node.add(this.match('function'));
5711621 if(!this.look) {
5721 this.error('missing formal parameter');
573 }
5741620 if(this.look.type() == Token.ID) {
57520 node.add(this.match());
576 }
5771620 node.add(this.match('('));
5781620 if(!this.look) {
5791 this.error();
580 }
5811619 if(this.look.content() != ')') {
5821143 node.add(this.fnparams());
583 }
5841617 node.add(
585 this.match(')'),
586 this.match('{'),
587 this.fnbody(),
588 this.match('}', 'missing } in compound statement')
589 );
5901617 return node;
591 },
592 fnparams: function() {
5931299 var node = new Node(Node.FNPARAMS);
5941299 node.add(this.match(Token.ID, 'missing formal parameter'));
5951299 while(this.look && this.look.content() == ',') {
596985 node.add(this.match());
597985 if(!this.look) {
5981 this.error('missing formal parameter');
599 }
600984 if(this.look.type() == Token.ID) {
601980 node.add(this.match());
602980 if(this.look && this.look.content() == '=') {
6033 node.add(this.bindelement());
604 }
605 }
6064 else if(this.look.content() == '...') {
6073 node.add(this.restparam());
608 }
609 else {
6101 this.error('missing formal parameter');
611 }
612 }
6131297 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) {
6261822 var node = new Node(Node.FNBODY);
6271822 while(this.look && this.look.content() != '}') {
6285168 node.add(this.element(allowSuper));
629 }
6301822 return node;
631 },
632 classdecl: function() {
63312 var node = new Node(Node.CLASSDECL);
63412 node.add(this.match('class'), this.match(Token.ID));
63512 if(!this.look) {
6361 this.error();
637 }
63811 if(this.look.content() == 'extends') {
6393 node.add(this.heratige());
640 }
64111 node.add(
642 this.match('{'),
643 this.classbody(),
644 this.match('}')
645 );
6469 return node;
647 },
648 heratige: function() {
6493 var node = new Node(Node.HERITAGE);
6503 node.add(this.match('extends'), this.match(Token.ID));
6513 return node;
652 },
653 classbody: function() {
65411 var node = new Node(Node.CLASSBODY),
655 methods = {},
656 hasStatic = false;
65711 while(this.look && this.look.content() != '}') {
65810 if(this.look.content() == ';') {
6591 node.add(this.match());
6601 continue;
661 }
6629 hasStatic = false;
6639 if(this.look.content() == 'static') {
6641 node.add(this.match());
6651 hasStatic = true;
666 }
6679 if(!this.look) {
6680 this.error();
669 }
6709 node.add(this.method(hasStatic, methods));
671 }
6729 return node;
673 },
674 method: function(hasStatic, methods, statics) {
6759 var node = new Node(Node.METHOD);
6769 if(this.look.content() == 'get') {
6771 node.add(this.match(), this.getfn());
678 }
6798 else if(this.look.content() == 'set') {
6801 node.add(this.match(), this.setfn());
681 }
682 else {
6837 node.add(this.match(Token.ID));
6847 var id = node.leaves()[0].token().content();
6857 if(methods.hasOwnProperty(id)) {
6861 this.error('duplicate method decl in class');
687 }
6886 methods[id] = true;
6896 node.add(this.match('('));
6905 if(this.look.content() != ')') {
6911 node.add(this.fnparams());
692 }
6935 node.add(
694 this.match(')'),
695 this.match('{'),
696 this.fnbody(true),
697 this.match('}', 'missing } in compound statement')
698 );
699 }
7007 return node;
701 },
702 expr: function(noIn) {
70311050 var node = new Node(Node.EXPR),
704 assignexpr = this.assignexpr(noIn);
70511037 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 {
71210497 return assignexpr;
713 }
714540 return node;
715 },
716 assignexpr: function(noIn) {
71730741 var node = new Node(Node.ASSIGNEXPR),
718 cndt = this.cndtexpr(noIn);
71930728 if(this.look && {
720 '*=': true,
721 '/=': true,
722 '%=': true,
723 '+=': true,
724 '-=': true,
725 '<<=': true,
726 '>>=': true,
727 '>>>=': true,
728 '&=': true,
729 '^=': true,
730 '|=': true,
731 '=': true
732 }.hasOwnProperty(this.look.content())) {
7333675 node.add(cndt, this.match(), this.assignexpr(noIn));
734 }
735 else {
73627053 return cndt;
737 }
7383675 return node;
739 },
740 cndtexpr: function(noIn) {
74130741 var node = new Node(Node.CNDTEXPR),
742 logorexpr = this.logorexpr(noIn);
74330728 if(this.look && this.look.content() == '?') {
744878 node.add(
745 logorexpr,
746 this.match(),
747 this.assignexpr(noIn),
748 this.match(':'),
749 this.assignexpr(noIn)
750 );
751 }
752 else {
75329850 return logorexpr;
754 }
755878 return node;
756 },
757 logorexpr: function(noIn) {
75830741 var node = new Node(Node.LOGOREXPR),
759 logandexpr = this.logandexpr(noIn);
76030728 if(this.look && this.look.content() == '||') {
7611015 node.add(logandexpr);
7621015 while(this.look && this.look.content() == '||') {
7631150 node.add(
764 this.match(),
765 this.logandexpr(noIn)
766 );
767 }
768 }
769 else {
77029713 return logandexpr;
771 }
7721015 return node;
773 },
774 logandexpr: function(noIn) {
77531891 var node = new Node(Node.LOGANDEXPR),
776 bitorexpr = this.bitorexpr(noIn);
77731878 if(this.look && this.look.content() == '&&') {
7781129 node.add(bitorexpr);
7791129 while(this.look && this.look.content() == '&&') {
7801375 node.add(
781 this.match(),
782 this.bitorexpr(noIn)
783 );
784 }
785 }
786 else {
78730749 return bitorexpr;
788 }
7891129 return node;
790 },
791 bitorexpr: function(noIn) {
79233266 var node = new Node(Node.BITOREXPR),
793 bitxorexpr = this.bitxorexpr(noIn);
79433253 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 {
80433248 return bitxorexpr;
805 }
8065 return node;
807 },
808 bitxorexpr: function(noIn) {
80933271 var node = new Node(Node.BITXOREXPR),
810 bitandexpr = this.bitandexpr(noIn);
81133258 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 {
82133256 return bitandexpr;
822 }
8232 return node;
824 },
825 bitandexpr: function(noIn) {
82633273 var node = new Node(Node.BITANDEXPR),
827 eqexpr = this.eqexpr(noIn);
82833260 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 {
83833242 return eqexpr;
839 }
84018 return node;
841 },
842 eqexpr: function(noIn) {
84333291 var node = new Node(Node.EQEXPR),
844 reltexpr = this.reltexpr(noIn);
84533278 if(this.look && {
846 '==': true,
847 '===': true,
848 '!==': true,
849 '!=': true
850 }.hasOwnProperty(this.look.content())) {
8511589 node.add(reltexpr);
8521589 while(this.look && {
853 '==': true,
854 '===': true,
855 '!==': true,
856 '!=': true
857 }.hasOwnProperty(this.look.content())) {
8581590 node.add(
859 this.match(),
860 this.reltexpr(noIn)
861 );
862 }
863 }
864 else {
86531689 return reltexpr;
866 }
8671589 return node;
868 },
869 reltexpr: function(noIn) {
87034881 var node = new Node(Node.RELTEXPR),
871 shiftexpr = this.shiftexpr();
87234868 if(this.look && ({
873 '<': true,
874 '>': true,
875 '>=': true,
876 '<=': true,
877 'instanceof': true
878 }.hasOwnProperty(this.look.content()) || (!noIn && this.look.content() == 'in'))) {
879477 node.add(shiftexpr);
880477 while(this.look && ({
881 '<': true,
882 '>': true,
883 '>=': true,
884 '<=': true,
885 'instanceof': true
886 }.hasOwnProperty(this.look.content()) || (!noIn && this.look.content() == 'in'))) {
887477 node.add(
888 this.match(),
889 this.shiftexpr()
890 );
891 }
892 }
893 else {
89434391 return shiftexpr;
895 }
896477 return node;
897 },
898 shiftexpr: function() {
89935358 var node = new Node(Node.SHIFTEXPR),
900 addexpr = this.addexpr();
90135345 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 {
91135337 return addexpr;
912 }
9138 return node;
914 },
915 addexpr: function() {
91635367 var node = new Node(Node.ADDEXPR),
917 mtplexpr = this.mtplexpr();
91835354 if(this.look && ['+', '-'].indexOf(this.look.content()) != -1) {
919777 node.add(mtplexpr);
920777 while(this.look && ['+', '-'].indexOf(this.look.content()) != -1) {
9211318 node.add(
922 this.match(),
923 this.mtplexpr()
924 );
925 }
926 }
927 else {
92834577 return mtplexpr;
929 }
930777 return node;
931 },
932 mtplexpr: function() {
93336685 var node = new Node(Node.MTPLEXPR),
934 unaryexpr = this.unaryexpr();
93536672 if(this.look && ['*', '/', '%'].indexOf(this.look.content()) != -1) {
93651 node.add(unaryexpr);
93751 while(this.look && ['*', '/', '%'].indexOf(this.look.content()) != -1) {
93852 node.add(
939 this.match(),
940 this.unaryexpr()
941 );
942 }
943 }
944 else {
94536621 return unaryexpr;
946 }
94751 return node;
948 },
949 unaryexpr: function() {
95038453 var node = new Node(Node.UNARYEXPR);
95138453 if(!this.look) {
9522 this.error();
953 }
95438451 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 '!':
9641716 node.add(
965 this.match(),
966 this.unaryexpr()
967 );
9681714 break;
969 default:
97036735 return this.postfixexpr();
971 }
9721714 return node;
973 },
974 postfixexpr: function() {
97536735 var node = new Node(Node.POSTFIXEXPR);
97636735 var leftexpr = this.leftexpr();
97736724 if(this.look && ['++', '--'].indexOf(this.look.content()) > -1 && !this.hasMoveLine) {
978327 node.add(leftexpr);
979327 while(this.look && ['++', '--'].indexOf(this.look.content()) > -1) {
980327 node.add(this.match(undefined, true));
981 }
982 }
983 else {
98436397 return leftexpr;
985 }
986327 return node;
987 },
988 leftexpr: function() {
98936735 if(this.look.content() == 'new') {
990190 return this.newexpr();
991 }
992 else {
99336545 return this.callexpr();
994 }
995 },
996 newexpr: function(depth) {
997191 depth = depth || 0;
998191 var node = new Node(Node.NEWEXPR);
999191 node.add(this.match('new'));
1000191 if(!this.look) {
10011 this.error();
1002 }
1003190 if(this.look.content() == 'new') {
10041 node.add(this.newexpr(depth + 1));
1005 }
1006 else {
1007189 node.add(this.mmbexpr());
1008 }
1009190 if(this.look && this.look.content() == '(') {
1010179 node.add(this.args());
1011 }
1012190 if(this.look && ['.', '['].indexOf(this.look.content()) > -1) {
101311 var mmb = new Node(Node.MMBEXPR);
101411 mmb.add(node);
101511 while(this.look) {
101620 if(this.look.content() == '.') {
101710 mmb.add(
1018 this.match(),
1019 this.match(Token.ID)
1020 );
1021 }
102210 else if(this.look.content() == '[') {
10231 mmb.add(
1024 this.match(),
1025 this.expr(),
1026 this.match(']')
1027 );
1028 }
1029 else {
10309 break;
1031 }
1032 }
103311 if(depth == 0 && this.look && this.look.content() == '(') {
10348 var callexpr = this.callexpr(mmb);
10358 return callexpr;
1036 }
10373 return mmb;
1038 }
1039179 return node;
1040 },
1041 callexpr: function(mmb) {
104236553 var node = new Node(Node.CALLEXPR);
104336553 mmb = mmb || this.mmbexpr();
104436544 if(this.look && this.look.content() == '(') {
10455152 node.add(
1046 mmb,
1047 this.args()
1048 );
10495151 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 {
107431392 return mmb;
1075 }
10765151 return node;
1077 },
1078 mmbexpr: function() {
107936734 var node = new Node(Node.MMBEXPR);
108036734 var mmb;
108136734 if(this.look.content() == 'function') {
10821621 mmb = this.fnexpr();
1083 }
1084 else {
108535113 mmb = this.prmrexpr();
1086 }
108736725 if(this.look && ['.', '['].indexOf(this.look.content()) > -1) {
108810733 node.add(mmb);
108910733 while(this.look) {
109023511 if(this.look.content() == '.') {
109110855 node.add(
1092 this.match(),
1093 this.match(Token.ID)
1094 );
1095 }
109612656 else if(this.look.content() == '[') {
10971928 node.add(
1098 this.match(),
1099 this.expr(),
1100 this.match(']')
1101 );
1102 }
1103 else {
110410728 break;
1105 }
1106 }
1107 }
1108 else {
110925992 return mmb;
1110 }
111110733 return node;
1112 },
1113 prmrexpr: function() {
111435113 var node = new Node(Node.PRMREXPR);
111535113 switch(this.look.type()) {
1116 case Token.ID:
1117 case Token.NUMBER:
1118 case Token.STRING:
1119 case Token.REG:
1120 case Token.TEMPLATE:
112129056 node.add(this.match());
112229056 break;
1123 default:
11246057 switch(this.look.content()) {
1125 case 'this':
1126 case 'null':
1127 case 'true':
1128 case 'false':
11293377 node.add(this.match());
11303377 break;
1131 case '(':
11321085 node.add(this.match(), this.expr(), this.match(')'));
11331081 break;
1134 case '[':
1135865 node.add(this.arrltr());
1136865 break;
1137 case '{':
1138729 node.add(this.objltr());
1139729 break;
1140 default:
11411 this.error();
1142 }
1143 }
114435108 return node;
1145 },
1146 arrltr: function() {
1147865 var node = new Node(Node.ARRLTR);
1148865 node.add(this.match('['));
1149865 while(this.look && this.look.content() != ']') {
11502362 if(this.look.content() == ',') {
1151863 node.add(this.match());
1152 }
1153 else {
11541499 node.add(this.assignexpr());
1155 }
1156 }
1157865 node.add(this.match(']'));
1158865 return node;
1159 },
1160 objltr: function() {
1161729 var node = new Node(Node.OBJLTR);
1162729 node.add(this.match('{'));
1163729 while(this.look && this.look.content() != '}') {
11642002 node.add(this.proptassign());
11652002 if(this.look && this.look.content() == ',') {
11661511 node.add(this.match());
1167 }
1168 }
1169729 node.add(this.match('}'));
1170729 return node;
1171 },
1172 proptassign: function() {
11732002 var node = new Node(Node.PROPTASSIGN);
11742002 if(!this.look) {
11750 this.error();
1176 }
11772002 if(this.look.content() == 'get') {
117828 node.add(this.match());
117928 if(!this.look) {
11800 this.error();
1181 }
118228 if(this.look.content() == ':') {
118328 node.add(this.match(), this.assignexpr());
1184 }
1185 else {
11860 node.add(this.getfn());
1187 }
1188 }
11891974 else if(this.look.content() == 'set') {
119030 node.add(this.match());
119130 if(!this.look) {
11920 this.error();
1193 }
119430 if(this.look.content() == ':') {
119530 node.add(this.match(), this.assignexpr());
1196 }
1197 else {
11980 node.add(this.setfn());
1199 }
1200 }
1201 else {
12021944 switch(this.look.type()) {
1203 case Token.ID:
1204 case Token.STRING:
1205 case Token.NUMBER:
12061944 node.add(
1207 this.match(),
1208 this.match(':'),
1209 this.assignexpr()
1210 );
12111944 break;
1212 default:
12130 this.error('invalid property id');
1214 }
1215 }
12162002 return node;
1217 },
1218 getfn: function() {
12191 var node = new Node(Node.GETFN);
12201 node.add(
1221 this.proptname(),
1222 this.match('('),
1223 this.match(')'),
1224 this.match('{'),
1225 this.fnbody(),
1226 this.match('}')
1227 );
12281 return node;
1229 },
1230 setfn: function() {
12311 var node = new Node(Node.SETFN);
12321 node.add(
1233 this.proptname(),
1234 this.match('('),
1235 this.propsets(),
1236 this.match(')'),
1237 this.match('{'),
1238 this.fnbody(),
1239 this.match('}')
1240 );
12411 return node;
1242 },
1243 proptname: function() {
12442 var node = new Node(Node.PROPTNAME);
12452 if(this.look) {
12462 switch(this.look.type()) {
1247 case Token.ID:
1248 case Token.NUMBER:
1249 case Token.STRING:
12502 node.add(this.match());
12512 break;
1252 default:
12530 this.error('missing name after . operator');
1254 }
1255 }
12562 return node;
1257 },
1258 propsets: function() {
12591 var node = new Node(Node.PROPTSETS);
12601 node.add(this.match(Token.ID, 'setter functions must have one argument'));
12611 return node;
1262 },
1263 args: function() {
12645684 var node = new Node(Node.ARGS);
12655684 node.add(this.match('('));
12665684 if(!this.look) {
12671 this.error();
1268 }
12695683 if(this.look.content() != ')') {
12704943 node.add(this.arglist());
1271 }
12725683 node.add(this.match(')'));
12735683 return node;
1274 },
1275 arglist: function() {
12764943 var node = new Node(Node.ARGLIST);
12774943 node.add(this.assignexpr());
12784943 while(this.look && this.look.content() == ',') {
12792844 node.add(this.match(), this.assignexpr());
1280 }
12814943 return node;
1282 },
1283 virtual: function(s) {
12841581 return new Node(Node.TOKEN, new Token(Token.VIRTUAL, s));
1285 },
1286 match: function(type, line, msg) {
1287140063 if(typeof type == 'boolean') {
12880 msg = line;
12890 line = type;
12900 type = undefined;
1291 }
1292140063 if(typeof line != 'boolean') {
1293137920 line = false;
1294137920 msg = line;
1295 }
1296 //未定义为所有非空白token
1297140063 if(character.isUndefined(type)) {
129871733 if(this.look) {
129971733 var l = this.look;
130071733 this.move(line);
130171733 return new Node(Node.TOKEN, l);
1302 }
1303 else {
13040 this.error('syntax error' + (msg || ''));
1305 }
1306 }
1307 //或者根据token的type或者content匹配
130868330 else if(typeof type == 'string') {
1309 //特殊处理;,不匹配但有换行或者末尾时自动补全,还有受限行
131052655 if(type == ';'
1311 && (!this.look
1312 || (this.look.content() != type && this.hasMoveLine)
1313 || this.look.content() == '}')
1314 ) {
13151581 if(this.look && S[this.look.type()]) {
131636 this.move();
1317 }
13181581 return this.virtual(';');
1319 }
132051074 else if(this.look && this.look.content() == type) {
132151073 var l = this.look;
132251073 this.move(line);
132351073 return new Node(Node.TOKEN, l);
1324 }
1325 else {
13261 this.error('missing ' + type + (msg || ''));
1327 }
1328 }
132915675 else if(typeof type == 'number') {
133015675 if(this.look && this.look.type() == type) {
133115675 var l = this.look;
133215675 this.move(line);
133315675 return new Node(Node.TOKEN, l);
1334 }
1335 else {
13360 this.error('missing ' + Token.type(type) + (msg || ''));
1337 }
1338 }
1339 },
1340 move: function(line) {
1341138689 this.lastLine = this.line;
1342138689 this.lastCol = this.col;
1343 //遗留下来的换行符
1344138689 this.hasMoveLine = false;
1345138689 do {
1346263056 this.look = this.tokens[this.index++];
1347263056 if(!this.look) {
1348165 return;
1349 }
1350 //存下忽略的token
1351262891 if(S[this.look.type()]) {
1352124403 this.ignores[this.index - 1] = this.look;
1353 }
1354 //包括line的情况下要跳出
1355262891 if(this.look.type() == Token.LINE) {
135617784 this.line++;
135717784 this.col = 1;
135817784 this.hasMoveLine = true;
135917784 if(line) {
136035 break;
1361 }
1362 }
1363245107 else if(this.look.type() == Token.COMMENT) {
13642742 var s = this.look.content();
13652742 var n = character.count(this.look.content(), character.LINE);
13662742 if(n > 0) {
136790 this.line += n;
136890 var i = s.lastIndexOf(character.LINE);
136990 this.col += s.length - i - 1;
137090 this.hasMoveLine = true;
137190 if(line) {
13721 break;
1373 }
1374 }
1375 }
1376 else {
1377242365 this.col += this.look.content().length;
1378242365 if(!S[this.look.type()]) {
1379138488 break;
1380 }
1381 }
1382 } while(this.index <= this.length);
1383 },
1384 error: function(msg) {
138526 msg = 'SyntaxError: ' + (msg || ' syntax error');
138626 throw new Error(msg + ' line ' + this.lastLine + ' col ' + this.lastCol);
1387 },
1388 ignore: function() {
13898 return this.ignores;
1390 }
1391});
13921module.exports = Parser;

D:/www/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;

D:/www/homunculus/src/util/character.js

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