Refactoring to enhance the SyntaxNode extending

This commit is contained in:
Jeff Avallone 2014-12-17 11:53:04 -05:00
parent 6322b48f31
commit 57ccd4b0c5
12 changed files with 75 additions and 65 deletions

View File

@ -1,5 +1,6 @@
import parser from './javascript/grammar.peg';
import Node from './javascript/node.js';
import Root from './javascript/root.js';
import Regexp from './javascript/regexp.js';
import Match from './javascript/match.js';
@ -18,23 +19,24 @@ import RepeatOptional from './javascript/repeat_optional.js';
import RepeatRequired from './javascript/repeat_required.js';
import RepeatSpec from './javascript/repeat_spec.js';
parser.Parser.Root = Root;
parser.Parser.Regexp = Regexp;
parser.Parser.Match = Match;
parser.Parser.MatchFragment = MatchFragment;
parser.Parser.Subexp = Subexp;
parser.Parser.Charset = Charset;
parser.Parser.CharsetLiteral = CharsetLiteral;
parser.Parser.CharsetEscape = CharsetEscape;
parser.Parser.CharsetRange = CharsetRange;
parser.Parser.Literal = Literal;
parser.Parser.Escape = Escape;
parser.Parser.AnyCharacter = AnyCharacter;
parser.Parser.Repeat = Repeat;
parser.Parser.RepeatAny = RepeatAny;
parser.Parser.RepeatOptional = RepeatOptional;
parser.Parser.RepeatRequired = RepeatRequired;
parser.Parser.RepeatSpec = RepeatSpec;
parser.Parser.SyntaxNode = Node;
parser.Parser.Root = { module: Root };
parser.Parser.Regexp = { module: Regexp };
parser.Parser.Match = { module: Match };
parser.Parser.MatchFragment = { module: MatchFragment };
parser.Parser.Subexp = { module: Subexp };
parser.Parser.Charset = { module: Charset };
parser.Parser.CharsetLiteral = { module: CharsetLiteral };
parser.Parser.CharsetEscape = { module: CharsetEscape };
parser.Parser.CharsetRange = { module: CharsetRange };
parser.Parser.Literal = { module: Literal };
parser.Parser.Escape = { module: Escape };
parser.Parser.AnyCharacter = { module: AnyCharacter };
parser.Parser.Repeat = { module: Repeat };
parser.Parser.RepeatAny = { module: RepeatAny };
parser.Parser.RepeatOptional = { module: RepeatOptional };
parser.Parser.RepeatRequired = { module: RepeatRequired };
parser.Parser.RepeatSpec = { module: RepeatSpec };
parser.parse = (parse => {
return function() {

View File

@ -1,10 +1,9 @@
import _ from 'lodash';
import Base from './base.js';
export default _.extend({}, Base, {
export default {
type: 'any-character',
_render() {
return this.renderLabel('any character');
}
});
};

View File

@ -1,8 +1,6 @@
import _ from 'lodash';
import Q from 'q';
import Base from './base.js';
export default _.extend({}, Base, {
export default {
type: 'charset',
_render() {
@ -31,14 +29,18 @@ export default _.extend({}, Base, {
},
_getAnchor() {
var matrix = this.transform().localMatrix;
var box = this.container.getBBox(),
matrix = this.transform().localMatrix;
return _.extend(Base._getAnchor.call(this), {
return {
atype: this.type,
ax: box.x,
ax2: box.x2,
ay: matrix.y(0, this.partContainer.getBBox().cy)
});
};
},
invert() {
return this._invert.textValue !== '';
}
});
};

View File

@ -1,8 +1,7 @@
import _ from 'lodash';
import Q from 'q';
import Base from './base.js';
export default _.extend({}, Base, {
export default {
type: 'charset-range',
_render() {
@ -21,4 +20,4 @@ export default _.extend({}, Base, {
padding: 5
}));
}
});
};

View File

@ -1,7 +1,6 @@
import _ from 'lodash';
import Base from './base.js';
export default _.extend({}, Base, {
export default {
type: 'escape',
code() {
@ -63,4 +62,4 @@ export default _.extend({}, Base, {
u() {
return 'U+' + this.arg().toUpperCase();
}
});
};

View File

@ -1,7 +1,6 @@
import _ from 'lodash';
import Base from './base.js';
export default _.extend({}, Base, {
export default {
type: 'literal',
_render() {
@ -18,4 +17,4 @@ export default _.extend({}, Base, {
});
});
}
});
};

View File

@ -1,8 +1,7 @@
import _ from 'lodash';
import Q from 'q';
import Base from './base.js';
export default _.extend({}, Base, {
export default {
type: 'match',
_render() {
@ -89,4 +88,4 @@ export default _.extend({}, Base, {
ay: matrix.y(start.ax, start.ay)
};
}
});
};

View File

@ -1,7 +1,6 @@
import _ from 'lodash';
import Base from './base.js';
export default _.extend({}, Base, {
export default {
type: 'match-fragment',
_render() {
@ -70,4 +69,4 @@ export default _.extend({}, Base, {
ay: matrix.y(anchor.ax, anchor.ay)
});
}
});
};

View File

@ -4,11 +4,26 @@ import Q from 'q';
var renderCounter = 0,
maxCounter = 0;
export default {
export default class Node {
constructor(textValue, offset, elements, properties) {
_.extend(this, {
textValue,
offset,
elements: elements || []
}, properties);
}
set module(mod) {
_.extend(this, mod);
if (this.setup) {
this.setup();
}
}
setContainer(container) {
this.container = container;
this.container.addClass(this.type);
},
}
getAnchor() {
if (this._proxy) {
@ -16,7 +31,7 @@ export default {
} else {
return this._getAnchor();
}
},
}
_getAnchor() {
var box = this.container.getBBox();
@ -27,11 +42,11 @@ export default {
ax2: box.x2,
ay: box.cy
};
},
}
getBBox() {
return _.extend(this.container.getBBox(), this.getAnchor());
},
}
normalizeBBox(box) {
return _.extend({
@ -40,11 +55,11 @@ export default {
ax2: box.x2,
ay: box.cy
}, box);
},
}
transform(matrix) {
return this.container.transform(matrix);
},
}
renderLabel(text) {
var deferred = Q.defer(),
@ -68,11 +83,11 @@ export default {
});
return deferred.promise;
},
}
startRender() {
renderCounter++;
},
}
doneRender() {
var evt, deferred = Q.defer();
@ -97,7 +112,7 @@ export default {
setTimeout(deferred.resolve.bind(deferred), 1);
return deferred.promise;
},
}
render(container) {
if (container) {
@ -108,13 +123,13 @@ export default {
return this._render()
.then(this.doneRender.bind(this))
.then(_.constant(this));
},
}
proxy(node) {
this.anchorDebug = false;
this._proxy = node;
return node.render(this.container);
},
}
spaceHorizontally(items, options) {
var verticalCenter = 0,
@ -143,7 +158,7 @@ export default {
.add(item.transform().localMatrix)
.translate(0, verticalCenter - box.ay));
}
},
}
spaceVertically(items, options) {
var horizontalCenter = 0;
@ -170,7 +185,7 @@ export default {
.add(item.transform().localMatrix)
.translate(horizontalCenter - item.getBBox().cx, 0));
}
},
}
renderLabeledBox(label, content, options) {
var deferred = Q.defer(),

View File

@ -1,8 +1,7 @@
import _ from 'lodash';
import Q from 'q';
import Base from './base.js';
export default _.extend({}, Base, {
export default {
type: 'regexp',
_render() {
@ -91,4 +90,4 @@ export default _.extend({}, Base, {
matches() {
return [this._match].concat(_.map(this._alternates.elements, _.property('match')));
}
});
};

View File

@ -1,7 +1,6 @@
import _ from 'lodash';
import Base from './base.js';
export default _.extend({}, Base, {
export default {
type: 'root',
_render() {
@ -68,4 +67,4 @@ export default _.extend({}, Base, {
multiline: /m/.test(flags)
};
}
});
};

View File

@ -1,9 +1,8 @@
import _ from 'lodash';
import Base from './base.js';
var groupCounter = 1;
export default _.extend({}, Base, {
export default {
type: 'subexp',
labelMap: {
@ -47,4 +46,4 @@ export default _.extend({}, Base, {
ay: matrix.y(anchor.ax, anchor.ay)
});
}
});
};