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 parser from './javascript/grammar.peg';
import Node from './javascript/node.js';
import Root from './javascript/root.js'; import Root from './javascript/root.js';
import Regexp from './javascript/regexp.js'; import Regexp from './javascript/regexp.js';
import Match from './javascript/match.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 RepeatRequired from './javascript/repeat_required.js';
import RepeatSpec from './javascript/repeat_spec.js'; import RepeatSpec from './javascript/repeat_spec.js';
parser.Parser.Root = Root; parser.Parser.SyntaxNode = Node;
parser.Parser.Regexp = Regexp; parser.Parser.Root = { module: Root };
parser.Parser.Match = Match; parser.Parser.Regexp = { module: Regexp };
parser.Parser.MatchFragment = MatchFragment; parser.Parser.Match = { module: Match };
parser.Parser.Subexp = Subexp; parser.Parser.MatchFragment = { module: MatchFragment };
parser.Parser.Charset = Charset; parser.Parser.Subexp = { module: Subexp };
parser.Parser.CharsetLiteral = CharsetLiteral; parser.Parser.Charset = { module: Charset };
parser.Parser.CharsetEscape = CharsetEscape; parser.Parser.CharsetLiteral = { module: CharsetLiteral };
parser.Parser.CharsetRange = CharsetRange; parser.Parser.CharsetEscape = { module: CharsetEscape };
parser.Parser.Literal = Literal; parser.Parser.CharsetRange = { module: CharsetRange };
parser.Parser.Escape = Escape; parser.Parser.Literal = { module: Literal };
parser.Parser.AnyCharacter = AnyCharacter; parser.Parser.Escape = { module: Escape };
parser.Parser.Repeat = Repeat; parser.Parser.AnyCharacter = { module: AnyCharacter };
parser.Parser.RepeatAny = RepeatAny; parser.Parser.Repeat = { module: Repeat };
parser.Parser.RepeatOptional = RepeatOptional; parser.Parser.RepeatAny = { module: RepeatAny };
parser.Parser.RepeatRequired = RepeatRequired; parser.Parser.RepeatOptional = { module: RepeatOptional };
parser.Parser.RepeatSpec = RepeatSpec; parser.Parser.RepeatRequired = { module: RepeatRequired };
parser.Parser.RepeatSpec = { module: RepeatSpec };
parser.parse = (parse => { parser.parse = (parse => {
return function() { return function() {

View File

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

View File

@ -1,8 +1,6 @@
import _ from 'lodash'; import _ from 'lodash';
import Q from 'q'; import Q from 'q';
import Base from './base.js'; export default {
export default _.extend({}, Base, {
type: 'charset', type: 'charset',
_render() { _render() {
@ -31,14 +29,18 @@ export default _.extend({}, Base, {
}, },
_getAnchor() { _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) ay: matrix.y(0, this.partContainer.getBBox().cy)
}); };
}, },
invert() { invert() {
return this._invert.textValue !== ''; return this._invert.textValue !== '';
} }
}); };

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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