From e362a545513848612fb64b36fa0bb64917bae3c9 Mon Sep 17 00:00:00 2001 From: Jeff Avallone Date: Wed, 17 Dec 2014 14:44:48 -0500 Subject: [PATCH] Converting methods into properties where possible/reasonable --- src/js/parser/javascript/charset.js | 25 +++-- src/js/parser/javascript/charset_range.js | 5 + src/js/parser/javascript/escape.js | 27 ++--- src/js/parser/javascript/grammar.peg | 24 ++--- src/js/parser/javascript/literal.js | 4 + src/js/parser/javascript/match.js | 54 +++++----- src/js/parser/javascript/match_fragment.js | 32 +++--- src/js/parser/javascript/node.js | 14 --- src/js/parser/javascript/regexp.js | 26 ++--- src/js/parser/javascript/repeat.js | 106 +++++++++----------- src/js/parser/javascript/repeat_any.js | 9 +- src/js/parser/javascript/repeat_optional.js | 9 +- src/js/parser/javascript/repeat_required.js | 9 +- src/js/parser/javascript/repeat_spec.js | 24 ++--- src/js/parser/javascript/root.js | 29 +++--- src/js/parser/javascript/subexp.js | 24 ++--- 16 files changed, 197 insertions(+), 224 deletions(-) diff --git a/src/js/parser/javascript/charset.js b/src/js/parser/javascript/charset.js index a9c8140..415489b 100644 --- a/src/js/parser/javascript/charset.js +++ b/src/js/parser/javascript/charset.js @@ -4,25 +4,17 @@ export default { type: 'charset', _render() { - var elements = _.unique(this.parts.elements, part => { - if (part.literal) { - return part.literal.textValue; - } else { - return part.textValue; - } - }); - this.partContainer = this.container.group(); - return Q.all(_.map(elements, part => { + return Q.all(_.map(this.elements, part => { return part.render(this.partContainer.group()); })) .then(() => { - this.spaceVertically(elements, { + this.spaceVertically(this.elements, { padding: 5 }); - return this.renderLabeledBox(this.invert() ? 'None of:' : 'One of:', this.partContainer, { + return this.renderLabeledBox(this.invert ? 'None of:' : 'One of:', this.partContainer, { padding: 5 }); }); @@ -40,7 +32,14 @@ export default { }; }, - invert() { - return this._invert.textValue !== ''; + setup() { + this.invert = this.properties.invert !== ''; + this.elements = _.unique(this.properties.parts.elements, part => { + if (part.literal) { + return part.literal.textValue; + } else { + return part.textValue; + } + }); } }; diff --git a/src/js/parser/javascript/charset_range.js b/src/js/parser/javascript/charset_range.js index 9b19ef2..60422be 100644 --- a/src/js/parser/javascript/charset_range.js +++ b/src/js/parser/javascript/charset_range.js @@ -19,5 +19,10 @@ export default { .then(this.spaceHorizontally.bind(this, contents, { padding: 5 })); + }, + + setup() { + this.first = this.properties.first; + this.last = this.properties.last; } }; diff --git a/src/js/parser/javascript/escape.js b/src/js/parser/javascript/escape.js index 6b83b70..047ebed 100644 --- a/src/js/parser/javascript/escape.js +++ b/src/js/parser/javascript/escape.js @@ -3,16 +3,8 @@ import _ from 'lodash'; export default { type: 'escape', - code() { - return this.esc.code.textValue; - }, - - arg() { - return this.esc.arg.textValue; - }, - _render() { - return this.renderLabel(_.result(this, this.code())) + return this.renderLabel(_.result(this, this.code)) .then(label => { label.select('rect').attr({ rx: 3, @@ -21,6 +13,11 @@ export default { }); }, + setup() { + this.code = this.properties.esc.properties.code.textValue; + this.arg = this.properties.esc.properties.arg.textValue; + }, + // Escape code mappings b: 'word boundary', B: 'non-word boundary', @@ -45,21 +42,19 @@ export default { 8: 'Back reference (group = 8)', 9: 'Back reference (group = 9)', 0() { - var arg = this.arg(); - - if (arg) { - return 'octal: ' + arg; + if (this.arg) { + return 'octal: ' + this.arg; } else { return 'null'; } }, c() { - return 'ctrl-' + this.arg(); + return 'ctrl-' + this.arg; }, x() { - return '0x' + this.arg().toUpperCase(); + return '0x' + this.arg.toUpperCase(); }, u() { - return 'U+' + this.arg().toUpperCase(); + return 'U+' + this.arg.toUpperCase(); } }; diff --git a/src/js/parser/javascript/grammar.peg b/src/js/parser/javascript/grammar.peg index c1130fe..12faf31 100644 --- a/src/js/parser/javascript/grammar.peg +++ b/src/js/parser/javascript/grammar.peg @@ -1,25 +1,25 @@ grammar JavascriptRegexp - root <- ( ( "/" regexp "/" _flags:[igm]* ) / regexp ""? ) - regexp <- _match:match _alternates:( "|" match )* - match <- _anchor_start:anchor_start? - (!repeat) _parts:match_fragment* - _anchor_end:anchor_end? - match_fragment <- _content:( subexp / charset / terminal ) _repeat:repeat? + root <- ( ( "/" regexp "/" flags:[igm]* ) / regexp ""? ) + regexp <- match:match alternates:( "|" match )* + match <- anchor_start:anchor_start? + (!repeat) parts:match_fragment* + anchor_end:anchor_end? + match_fragment <- content:( subexp / charset / terminal ) repeat:repeat? anchor_start <- "^" anchor_end <- "$" - repeat <- _spec:( repeat_any / repeat_required / repeat_optional / repeat_spec ) _greedy:repeat_greedy? + repeat <- spec:( repeat_any / repeat_required / repeat_optional / repeat_spec ) greedy:repeat_greedy? repeat_any <- "*" repeat_required <- "+" repeat_optional <- "?" - repeat_spec <- ( "{" _min:[0-9]+ "," _max:[0-9]+ "}" - / "{" _min:[0-9]+ ",}" - / "{" _exact:[0-9]+ "}" ) + repeat_spec <- ( "{" min:[0-9]+ "," max:[0-9]+ "}" + / "{" min:[0-9]+ ",}" + / "{" exact:[0-9]+ "}" ) repeat_greedy <- "?" - subexp <- "(" _capture:( subexp_no_capture / subexp_positive_lookahead / subexp_negative_lookahead )? regexp ")" + subexp <- "(" capture:( subexp_no_capture / subexp_positive_lookahead / subexp_negative_lookahead )? regexp ")" subexp_no_capture <- "?:" subexp_positive_lookahead <- "?=" subexp_negative_lookahead <- "?!" - charset <- "[" _invert:"^"? parts:( charset_range / charset_terminal )* "]" + charset <- "[" invert:"^"? parts:( charset_range / charset_terminal )* "]" charset_range <- first:charset_terminal "-" last:charset_terminal charset_terminal <- charset_escape / charset_literal diff --git a/src/js/parser/javascript/literal.js b/src/js/parser/javascript/literal.js index 1b5e68f..57790f6 100644 --- a/src/js/parser/javascript/literal.js +++ b/src/js/parser/javascript/literal.js @@ -16,5 +16,9 @@ export default { ry: 3 }); }); + }, + + setup() { + this.literal = this.properties.literal; } }; diff --git a/src/js/parser/javascript/match.js b/src/js/parser/javascript/match.js index c506095..92192d2 100644 --- a/src/js/parser/javascript/match.js +++ b/src/js/parser/javascript/match.js @@ -6,21 +6,20 @@ export default { _render() { var start, end, - parts = this.parts(), partPromises; - if (this.anchorStart()) { + if (this.anchorStart) { start = this.renderLabel('Start of line') .invoke('addClass', 'anchor'); } - if (this.anchorEnd()) { + if (this.anchorEnd) { end = this.renderLabel('End of line') .invoke('addClass', 'anchor'); } - if (start || end || parts.length !== 1) { - partPromises = _.map(parts, part => { + if (start || end || this.parts.length !== 1) { + partPromises = _.map(this.parts, part => { return part.render(this.container.group()); }); @@ -48,34 +47,10 @@ export default { this.container.path(paths.join(''))); }); } else { - return this.proxy(parts[0]); + return this.proxy(this.parts[0]); } }, - anchorStart() { - return this._anchor_start.textValue !== ''; - }, - - anchorEnd() { - return this._anchor_end.textValue !== ''; - }, - - parts() { - return _.reduce(this._parts.elements, function(result, node) { - var last = _.last(result); - - if (last && node.elements[0].type === 'literal' && node.elements[1].textValue === '' && last.elements[0].type === 'literal' && last.elements[1].textValue === '') { - last.textValue += node.textValue; - last.elements[0].textValue += node.elements[0].textValue; - last.elements[0].literal.textValue += node.elements[0].literal.textValue; - } else { - result.push(node); - } - - return result; - }, []); - }, - _getAnchor() { var start = this.normalizeBBox(_.first(this.items).getBBox()), end = this.normalizeBBox(_.last(this.items).getBBox()), @@ -87,5 +62,24 @@ export default { ax2: matrix.x(end.ax2, end.ay), ay: matrix.y(start.ax, start.ay) }; + }, + + setup() { + this.parts = _.reduce(this.properties.parts.elements, function(result, node) { + var last = _.last(result); + + if (last && node.elements[0].type === 'literal' && node.elements[1].textValue === '' && last.elements[0].type === 'literal' && last.elements[1].textValue === '') { + last.textValue += node.textValue; + last.elements[0].textValue += node.elements[0].textValue; + last.elements[0].literal.textValue += node.elements[0].literal.textValue; + } else { + result.push(node); + } + + return result; + }, []); + + this.anchorStart = this.properties.anchor_start.textValue !== ''; + this.anchorEnd = this.properties.anchor_end.textValue !== ''; } }; diff --git a/src/js/parser/javascript/match_fragment.js b/src/js/parser/javascript/match_fragment.js index 408954a..1ef1163 100644 --- a/src/js/parser/javascript/match_fragment.js +++ b/src/js/parser/javascript/match_fragment.js @@ -4,34 +4,34 @@ export default { type: 'match-fragment', _render() { - if (this._repeat.textValue === '') { - return this.proxy(this._content); + if (!this.repeat) { + return this.proxy(this.content); } else { - return this._content.render(this.container.group()) + return this.content.render(this.container.group()) .then(() => { var box, paths = []; - this._content.transform(this._repeat.contentPosition()); + this.content.transform(this.repeat.contentPosition); - box = this._content.getBBox(); + box = this.content.getBBox(); - if (this._repeat.hasSkip()) { + if (this.repeat.hasSkip) { let vert = Math.max(0, box.ay - box.y - 10), horiz = box.width - 10; paths.push(`M0,${box.ay}q10,0 10,-10v${-vert}q0,-10 10,-10h${horiz}q10,0 10,10v${vert}q0,10 10,10`); - if (!this._repeat.greedy()) { + if (!this.repeat.greedy) { paths.push(`M10,${box.ay - 15}l5,5m-5,-5l-5,5`); } } - if (this._repeat.hasLoop()) { + if (this.repeat.hasLoop) { let vert = box.y2 - box.ay - 10; paths.push(`M${box.x},${box.ay}q-10,0 -10,10v${vert}q0,10 10,10h${box.width}q10,0 10,-10v${-vert}q0,-10 -10,-10`); - if (this._repeat.greedy()) { + if (this.repeat.greedy) { paths.push(`M${box.x2 + 10},${box.ay + 15}l5,-5m-5,5l-5,-5`); } } @@ -42,10 +42,10 @@ export default { } }) .then(() => { - var labelStr = this._repeat.label(), + var labelStr = this.repeat.label, label, labelBox, - labelShift = (this._repeat.hasSkip() ? 5 : 0), + labelShift = (this.repeat.hasSkip ? 5 : 0), box = this.getBBox(); if (labelStr) { @@ -60,7 +60,7 @@ export default { }, _getAnchor() { - var anchor = this._content.getAnchor(), + var anchor = this.content.getAnchor(), matrix = this.transform().localMatrix; return _.extend(anchor, { @@ -68,5 +68,13 @@ export default { ax2: matrix.x(anchor.ax2, anchor.ay), ay: matrix.y(anchor.ax, anchor.ay) }); + }, + + setup() { + this.content = this.properties.content; + + if (this.properties.repeat.textValue !== '') { + this.repeat = this.properties.repeat; + } } }; diff --git a/src/js/parser/javascript/node.js b/src/js/parser/javascript/node.js index d24c3d2..5b4e90f 100644 --- a/src/js/parser/javascript/node.js +++ b/src/js/parser/javascript/node.js @@ -11,20 +11,6 @@ export default class Node { this.elements = elements || []; this.properties = properties; - - // TEMPORARY - _.forOwn(this.properties, (_, key) => { - Object.defineProperty(this, key, { - get: function() { - return this.properties[key]; - }, - - set: function(value) { - this.properties[key] = value; - } - }); - }); - // /TEMPORARY } set module(mod) { diff --git a/src/js/parser/javascript/regexp.js b/src/js/parser/javascript/regexp.js index 8335f35..f27ec2e 100644 --- a/src/js/parser/javascript/regexp.js +++ b/src/js/parser/javascript/regexp.js @@ -5,39 +5,38 @@ export default { type: 'regexp', _render() { - var matches = this.matches(), - matchContainer; + var matchContainer; - if (matches.length === 1) { - return this.proxy(matches[0]); + if (this.matches.length === 1) { + return this.proxy(this.matches[0]); } else { matchContainer = this.container.group() .addClass('regexp-matches') .transform(Snap.matrix() .translate(20, 0)); - return Q.all(_.map(matches, match => { + return Q.all(_.map(this.matches, match => { return match.render(matchContainer.group()); })) .then(() => { var containerBox, paths; - this.spaceVertically(matches, { + this.spaceVertically(this.matches, { padding: 5 }); containerBox = this.getBBox(); - paths = _.map(matches, this.makeConnectorLine.bind(this, containerBox)); + paths = _.map(this.matches, this.makeConnectorLine.bind(this, containerBox)); - paths.push(this.makeSideLine(containerBox, _.first(matches))); - paths.push(this.makeSideLine(containerBox, _.last(matches))); + paths.push(this.makeSideLine(containerBox, _.first(this.matches))); + paths.push(this.makeSideLine(containerBox, _.last(this.matches))); this.container.prepend( this.container.path(paths.join(''))); matchContainer.prepend( - matchContainer.path(_.map(matches, match => { + matchContainer.path(_.map(this.matches, match => { var box = match.getBBox(), container = matchContainer.getBBox(); @@ -87,7 +86,10 @@ export default { } }, - matches() { - return [this._match].concat(_.map(this._alternates.elements, _.property('match'))); + setup() { + this.matches = [this.properties.match] + .concat(_.map(this.properties.alternates.elements, element => { + return element.properties.match; + })); } }; diff --git a/src/js/parser/javascript/repeat.js b/src/js/parser/javascript/repeat.js index 29e539a..84ba6c2 100644 --- a/src/js/parser/javascript/repeat.js +++ b/src/js/parser/javascript/repeat.js @@ -1,60 +1,50 @@ -export default { - minimum() { - return this._spec.minimum(); - }, - - maximum() { - return this._spec.maximum(); - }, - - greedy() { - return (this._greedy.textValue === ''); - }, - - hasSkip() { - return this.minimum() === 0; - }, - - hasLoop() { - return this.maximum() === -1 || this.maximum() > 1; - }, - - contentPosition() { - var x = 0, y = 0; - - if (this.hasLoop()) { - x = 10; - } - - if (this.hasSkip()) { - y = 10; - x = 15; - } - - return Snap.matrix().translate(x, y); - }, - - label() { - var maximum = this.maximum(), - minimum = this.minimum(), - formatTimes = times => { - if (times === 1) { - return 'once'; - } else { - return `${times} times`; - } - }; - - if (minimum >= 2 && maximum === -1) { - return `${minimum - 1}+ times`; - } else if (minimum <= 1 && maximum >= 2) { - return `at most ${formatTimes(maximum - 1)}`; - } else if (minimum >= 2 && maximum >= 2) { - if (minimum === maximum) { - return formatTimes(minimum - 1); - } else { - return `${minimum - 1}...${formatTimes(maximum - 1)}`; - } - } +function formatTimes(times) { + if (times === 1) { + return 'once'; + } else { + return `${times} times`; + } +} + +export default { + setup() { + this.minimum = this.properties.spec.minimum; + this.maximum = this.properties.spec.maximum; + this.greedy = (this.properties.greedy.textValue === ''); + this.hasSkip = this.minimum === 0; + this.hasLoop = this.maximum === -1 || this.maximum > 1; + + Object.defineProperty(this, 'contentPosition', { + get: function() { + var x = 0, y = 0; + + if (this.hasLoop) { + x = 10; + } + + if (this.hasSkip) { + y = 10; + x = 15; + } + + return Snap.matrix().translate(x, y); + } + }); + + Object.defineProperty(this, 'label', { + get: function() { + if (this.minimum >= 2 && this.maximum === -1) { + return `${this.minimum - 1}+ times`; + } else if (this.minimum <= 1 && this.maximum >= 2) { + return `at most ${formatTimes(this.maximum - 1)}`; + } else if (this.minimum >= 2 && this.maximum >= 2) { + if (this.minimum === this.maximum) { + return formatTimes(this.minimum - 1); + } else { + return `${this.minimum - 1}...${formatTimes(this.maximum - 1)}`; + } + } + } + }); } } diff --git a/src/js/parser/javascript/repeat_any.js b/src/js/parser/javascript/repeat_any.js index ea76d00..268b297 100644 --- a/src/js/parser/javascript/repeat_any.js +++ b/src/js/parser/javascript/repeat_any.js @@ -1,9 +1,6 @@ export default { - minimum() { - return 0; - }, - - maximum() { - return -1; + setup() { + this.minimum = 0; + this.maximum = -1; } }; diff --git a/src/js/parser/javascript/repeat_optional.js b/src/js/parser/javascript/repeat_optional.js index 03c73bb..4dc5f59 100644 --- a/src/js/parser/javascript/repeat_optional.js +++ b/src/js/parser/javascript/repeat_optional.js @@ -1,9 +1,6 @@ export default { - minimum() { - return 0; - }, - - maximum() { - return 1; + setup() { + this.minimum = 0; + this.maximum = 1; } }; diff --git a/src/js/parser/javascript/repeat_required.js b/src/js/parser/javascript/repeat_required.js index 2ca7754..5c6aef1 100644 --- a/src/js/parser/javascript/repeat_required.js +++ b/src/js/parser/javascript/repeat_required.js @@ -1,9 +1,6 @@ export default { - minimum() { - return 1; - }, - - maximum() { - return -1; + setup() { + this.minimum = 1; + this.maximum = -1; } }; diff --git a/src/js/parser/javascript/repeat_spec.js b/src/js/parser/javascript/repeat_spec.js index 93181d6..cf1eb7e 100644 --- a/src/js/parser/javascript/repeat_spec.js +++ b/src/js/parser/javascript/repeat_spec.js @@ -1,21 +1,19 @@ export default { - minimum() { - if (this._min) { - return Number(this._min.textValue); - } else if (this._exact) { - return Number(this._exact.textValue); + setup() { + if (this.properties.min) { + this.minimum = Number(this.properties.min.textValue); + } else if (this.properties.exact) { + this.minimum = Number(this.properties.exact.textValue); } else { - return 0; + this.minimum = 0; } - }, - maximum() { - if (this._max) { - return Number(this._max.textValue); - } else if (this._exact) { - return Number(this._exact.textValue); + if (this.properties.max) { + this.maximum = Number(this.properties.max.textValue); + } else if (this.properties.exact) { + this.maximum = Number(this.properties.exact.textValue); } else { - return -1; + this.maximum = -1; } } }; diff --git a/src/js/parser/javascript/root.js b/src/js/parser/javascript/root.js index 87a660f..c4d6bc2 100644 --- a/src/js/parser/javascript/root.js +++ b/src/js/parser/javascript/root.js @@ -4,16 +4,15 @@ export default { type: 'root', _render() { - var flags = this.flags(), - flagLabels = []; + var flagLabels = []; - if (flags.global) { + if (this.flags.global) { flagLabels.push('Global'); } - if (flags.ignore_case) { + if (this.flags.ignore_case) { flagLabels.push('Ignore Case'); } - if (flags.multiline) { + if (this.flags.multiline) { flagLabels.push('Multiline'); } @@ -52,19 +51,21 @@ export default { }); }, - flags() { - var flags; + setup() { + var flagsStr; - if (this._flags) { - flags = this._flags.textValue; + if (this.flags) { + flagsStr = this.flags.textValue; } else { - flags = ''; + flagsStr = ''; } - return { - global: /g/.test(flags), - ignore_case: /i/.test(flags), - multiline: /m/.test(flags) + this.flags = { + global: /g/.test(flagsStr), + ignore_case: /i/.test(flagsStr), + multiline: /m/.test(flagsStr) }; + + this.regexp = this.properties.regexp } }; diff --git a/src/js/parser/javascript/subexp.js b/src/js/parser/javascript/subexp.js index e6f0772..2296cc6 100644 --- a/src/js/parser/javascript/subexp.js +++ b/src/js/parser/javascript/subexp.js @@ -12,11 +12,9 @@ export default { }, _render() { - var label = this.groupLabel(); - - if (label) { + if (this.label) { return this.regexp.render(this.container.group()) - .then(this.renderLabeledBox.bind(this, label, this.regexp, { + .then(this.renderLabeledBox.bind(this, this.label, this.regexp, { padding: 10 })); } else { @@ -24,14 +22,6 @@ export default { } }, - groupLabel() { - if (_.has(this.labelMap, this._capture.textValue)) { - return this.labelMap[this._capture.textValue]; - } else { - return 'group #' + (groupCounter++); - } - }, - resetCounter() { groupCounter = 1; }, @@ -45,5 +35,15 @@ export default { ax2: matrix.x(anchor.ax2, anchor.ay), ay: matrix.y(anchor.ax, anchor.ay) }); + }, + + setup() { + if (_.has(this.labelMap, this.properties.capture.textValue)) { + this.label = this.labelMap[this.properties.capture.textValue]; + } else { + this.label = 'group #' + (groupCounter++); + } + + this.regexp = this.properties.regexp; } };