From 57ccd4b0c549e0129fda0a3fa1d36fd19998a1b1 Mon Sep 17 00:00:00 2001 From: Jeff Avallone Date: Wed, 17 Dec 2014 11:53:04 -0500 Subject: [PATCH] Refactoring to enhance the SyntaxNode extending --- src/js/parser/javascript.js | 36 ++++++++-------- src/js/parser/javascript/any_character.js | 5 +-- src/js/parser/javascript/charset.js | 16 ++++--- src/js/parser/javascript/charset_range.js | 5 +-- src/js/parser/javascript/escape.js | 5 +-- src/js/parser/javascript/literal.js | 5 +-- src/js/parser/javascript/match.js | 5 +-- src/js/parser/javascript/match_fragment.js | 5 +-- src/js/parser/javascript/{base.js => node.js} | 43 +++++++++++++------ src/js/parser/javascript/regexp.js | 5 +-- src/js/parser/javascript/root.js | 5 +-- src/js/parser/javascript/subexp.js | 5 +-- 12 files changed, 75 insertions(+), 65 deletions(-) rename src/js/parser/javascript/{base.js => node.js} (93%) diff --git a/src/js/parser/javascript.js b/src/js/parser/javascript.js index 1b7a297..2713f8b 100644 --- a/src/js/parser/javascript.js +++ b/src/js/parser/javascript.js @@ -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() { diff --git a/src/js/parser/javascript/any_character.js b/src/js/parser/javascript/any_character.js index aec4687..8ee6376 100644 --- a/src/js/parser/javascript/any_character.js +++ b/src/js/parser/javascript/any_character.js @@ -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'); } -}); +}; diff --git a/src/js/parser/javascript/charset.js b/src/js/parser/javascript/charset.js index 72c2bcf..a9c8140 100644 --- a/src/js/parser/javascript/charset.js +++ b/src/js/parser/javascript/charset.js @@ -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 !== ''; } -}); +}; diff --git a/src/js/parser/javascript/charset_range.js b/src/js/parser/javascript/charset_range.js index c8980a0..9b19ef2 100644 --- a/src/js/parser/javascript/charset_range.js +++ b/src/js/parser/javascript/charset_range.js @@ -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 })); } -}); +}; diff --git a/src/js/parser/javascript/escape.js b/src/js/parser/javascript/escape.js index f55f91f..6b83b70 100644 --- a/src/js/parser/javascript/escape.js +++ b/src/js/parser/javascript/escape.js @@ -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(); } -}); +}; diff --git a/src/js/parser/javascript/literal.js b/src/js/parser/javascript/literal.js index 169d20c..1b5e68f 100644 --- a/src/js/parser/javascript/literal.js +++ b/src/js/parser/javascript/literal.js @@ -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, { }); }); } -}); +}; diff --git a/src/js/parser/javascript/match.js b/src/js/parser/javascript/match.js index 56b5724..c506095 100644 --- a/src/js/parser/javascript/match.js +++ b/src/js/parser/javascript/match.js @@ -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) }; } -}); +}; diff --git a/src/js/parser/javascript/match_fragment.js b/src/js/parser/javascript/match_fragment.js index 70f61a5..408954a 100644 --- a/src/js/parser/javascript/match_fragment.js +++ b/src/js/parser/javascript/match_fragment.js @@ -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) }); } -}); +}; diff --git a/src/js/parser/javascript/base.js b/src/js/parser/javascript/node.js similarity index 93% rename from src/js/parser/javascript/base.js rename to src/js/parser/javascript/node.js index 107b56e..11bf36d 100644 --- a/src/js/parser/javascript/base.js +++ b/src/js/parser/javascript/node.js @@ -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(), diff --git a/src/js/parser/javascript/regexp.js b/src/js/parser/javascript/regexp.js index c27d26f..8335f35 100644 --- a/src/js/parser/javascript/regexp.js +++ b/src/js/parser/javascript/regexp.js @@ -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'))); } -}); +}; diff --git a/src/js/parser/javascript/root.js b/src/js/parser/javascript/root.js index 9e703b0..87a660f 100644 --- a/src/js/parser/javascript/root.js +++ b/src/js/parser/javascript/root.js @@ -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) }; } -}); +}; diff --git a/src/js/parser/javascript/subexp.js b/src/js/parser/javascript/subexp.js index 4bec1b0..e6f0772 100644 --- a/src/js/parser/javascript/subexp.js +++ b/src/js/parser/javascript/subexp.js @@ -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) }); } -}); +};