Skip to content

Commit

Permalink
extract es6
Browse files Browse the repository at this point in the history
  • Loading branch information
army8735 committed May 21, 2014
1 parent 529f904 commit 2ee3480
Show file tree
Hide file tree
Showing 13 changed files with 3,648 additions and 923 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "homunculus",
"version": "0.1.8",
"version": "0.1.9",
"description": "A lexer&parser by Javascript",
"maintainers": [
{
Expand Down
163 changes: 163 additions & 0 deletions src/parser/es6/Node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
var Class = require('../../util/Class');
var Node = Class(function(type, children) {
this.type = type;
if(type == Node.TOKEN) {
this.children = children;
}
else if(Array.isArray(children)) {
this.children = children;
}
else {
this.children = children ? [children] : [];
}
this.p = null;
this.pr = null;
this.ne = null;
return this;
}).methods({
name: function() {
return this.type;
},
leaves: function() {
return this.children;
},
leaf: function(i) {
return this.children[i];
},
number: function() {
return this.children.length;
},
add: function() {
var self = this;
var args = Array.prototype.slice.call(arguments, 0);
args.forEach(function(node) {
node.parent(self);
var last = self.children[self.children.length - 1];
if(last) {
last.next(node);
node.prev(last);
}
self.children.push(node);
});
return self;
},
token: function() {
return this.children;
},
parent: function(p) {
if(p) {
this.p = p;
}
return this.p;
},
prev: function(pr) {
if(pr) {
this.pr = pr;
}
return this.pr;
},
next: function(ne) {
if(ne) {
this.ne = ne;
}
return this.ne;
}
}).statics({
PROGRAM: 'program',
ELEMS: 'elems',
ELEM: 'elem',
CSTSTMT: 'cststmt',
LETSTMT: 'letstmt',
VARSTMT: 'varstmt',
VARDECL: 'vardecl',
FNBODY: 'fnbody',
BLOCK: 'block',
ITERSTMT: 'iterstmt',
TOKEN: 'token',
FNPARAMS: 'fnparams',
BINDELEMENT: 'bindelement',
RESTPARAM: 'restparam',
EXPR: 'expr',
CLASSDECL: 'classdecl',
CLASSTAIL: 'classtail',
HERITAGE: 'heritage',
CLASSBODY: 'classbody',
METHOD: 'method',
SUPERSTMT: 'superstmt',
GETFN: 'getfn',
SETFN: 'setfn',
PROGRAM: 'program',
STMT: 'stmt',
ASSIGN: 'assign',
EMPTSTMT: 'emptstmt',
IFSTMT: 'ifstmt',
CNTNSTMT: 'cntnstmt',
BRKSTMT: 'brkstmt',
RETSTMT: 'retstmt',
WITHSTMT: 'withstmt',
SWCHSTMT: 'swchstmt',
CASEBLOCK: 'caseblock',
CASECLAUSE: 'caseclause',
DFTCLAUSE: 'dftclause',
LABSTMT: 'labstmt',
THRSTMT: 'thrstmt',
TRYSTMT: 'trystmt',
DEBSTMT: 'debstmt',
EXPRSTMT: 'exprstmt',
CACH: 'cach',
FINL: 'finl',
FNDECL: 'fndecl',
FNEXPR: 'fnexpr',
ASSIGNEXPR: 'assignexpr',
CNDTEXPR: 'cndtexpr',
LOGOREXPR: 'logorexpr',
LOGANDEXPR: 'logandexpr',
BITOREXPR: 'bitorexpr',
BITANDEXPR: 'bitandexpr',
BITXOREXPR: 'bitxorexpr',
EQEXPR: 'eqexpr',
RELTEXPR: 'reltexpr',
SHIFTEXPR: 'shiftexpr',
ADDEXPR: 'addexpr',
MTPLEXPR: 'mtplexpr',
UNARYEXPR: 'unaryexpr',
MMBEXPR: 'mmbexpr',
PRMREXPR: 'prmrexpr',
ARRLTR: 'arrltr',
OBJLTR: 'objltr',
PROPTASSIGN: 'proptassign',
PROPTNAME: 'proptname',
PROPTSETS: 'propsets',
ARGS: 'args',
ARGLIST: 'arglist',
IMPTSTMT: 'imptstmt',
POSTFIXEXPR: 'postfixexpr',
NEWEXPR: 'newexpr',
CALLEXPR: 'callexpr',
ARRBINDPAT: 'arrbindpat',
OBJBINDPAT: 'objbindpat',
BINDPROPT: 'bindpropt',
SINGLENAME: 'singlename',
BINDELEM: 'bindelem',
BINDREST: 'bindrest',
BINDID: 'bindid',
SPREAD: 'spread',
ARRCMPH: 'arrcmph',
CMPHFOR: 'cmphfor',
getKey: function(s) {
if(!s) {
throw new Error('empty value');
}
if(!keys) {
var self = this;
keys = {};
Object.keys(this).forEach(function(k) {
var v = self[k];
keys[v] = k;
});
}
return keys[s];
}
});
var keys;
module.exports = Node;
Loading

0 comments on commit 2ee3480

Please sign in to comment.