Converting methods into properties where possible/reasonable

This commit is contained in:
Jeff Avallone 2014-12-17 14:44:48 -05:00
parent 4af524112e
commit e362a54551
16 changed files with 197 additions and 224 deletions

View File

@ -4,25 +4,17 @@ export default {
type: 'charset', type: 'charset',
_render() { _render() {
var elements = _.unique(this.parts.elements, part => {
if (part.literal) {
return part.literal.textValue;
} else {
return part.textValue;
}
});
this.partContainer = this.container.group(); this.partContainer = this.container.group();
return Q.all(_.map(elements, part => { return Q.all(_.map(this.elements, part => {
return part.render(this.partContainer.group()); return part.render(this.partContainer.group());
})) }))
.then(() => { .then(() => {
this.spaceVertically(elements, { this.spaceVertically(this.elements, {
padding: 5 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 padding: 5
}); });
}); });
@ -40,7 +32,14 @@ export default {
}; };
}, },
invert() { setup() {
return this._invert.textValue !== ''; this.invert = this.properties.invert !== '';
this.elements = _.unique(this.properties.parts.elements, part => {
if (part.literal) {
return part.literal.textValue;
} else {
return part.textValue;
}
});
} }
}; };

View File

@ -19,5 +19,10 @@ export default {
.then(this.spaceHorizontally.bind(this, contents, { .then(this.spaceHorizontally.bind(this, contents, {
padding: 5 padding: 5
})); }));
},
setup() {
this.first = this.properties.first;
this.last = this.properties.last;
} }
}; };

View File

@ -3,16 +3,8 @@ import _ from 'lodash';
export default { export default {
type: 'escape', type: 'escape',
code() {
return this.esc.code.textValue;
},
arg() {
return this.esc.arg.textValue;
},
_render() { _render() {
return this.renderLabel(_.result(this, this.code())) return this.renderLabel(_.result(this, this.code))
.then(label => { .then(label => {
label.select('rect').attr({ label.select('rect').attr({
rx: 3, 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 // Escape code mappings
b: 'word boundary', b: 'word boundary',
B: 'non-word boundary', B: 'non-word boundary',
@ -45,21 +42,19 @@ export default {
8: 'Back reference (group = 8)', 8: 'Back reference (group = 8)',
9: 'Back reference (group = 9)', 9: 'Back reference (group = 9)',
0() { 0() {
var arg = this.arg(); if (this.arg) {
return 'octal: ' + this.arg;
if (arg) {
return 'octal: ' + arg;
} else { } else {
return 'null'; return 'null';
} }
}, },
c() { c() {
return 'ctrl-' + this.arg(); return 'ctrl-' + this.arg;
}, },
x() { x() {
return '0x' + this.arg().toUpperCase(); return '0x' + this.arg.toUpperCase();
}, },
u() { u() {
return 'U+' + this.arg().toUpperCase(); return 'U+' + this.arg.toUpperCase();
} }
}; };

View File

@ -1,25 +1,25 @@
grammar JavascriptRegexp grammar JavascriptRegexp
root <- ( ( "/" regexp "/" _flags:[igm]* ) / regexp ""? ) <Root> root <- ( ( "/" regexp "/" flags:[igm]* ) / regexp ""? ) <Root>
regexp <- _match:match _alternates:( "|" match )* <Regexp> regexp <- match:match alternates:( "|" match )* <Regexp>
match <- _anchor_start:anchor_start? match <- anchor_start:anchor_start?
(!repeat) _parts:match_fragment* (!repeat) parts:match_fragment*
_anchor_end:anchor_end? <Match> anchor_end:anchor_end? <Match>
match_fragment <- _content:( subexp / charset / terminal ) _repeat:repeat? <MatchFragment> match_fragment <- content:( subexp / charset / terminal ) repeat:repeat? <MatchFragment>
anchor_start <- "^" anchor_start <- "^"
anchor_end <- "$" anchor_end <- "$"
repeat <- _spec:( repeat_any / repeat_required / repeat_optional / repeat_spec ) _greedy:repeat_greedy? <Repeat> repeat <- spec:( repeat_any / repeat_required / repeat_optional / repeat_spec ) greedy:repeat_greedy? <Repeat>
repeat_any <- "*" <RepeatAny> repeat_any <- "*" <RepeatAny>
repeat_required <- "+" <RepeatRequired> repeat_required <- "+" <RepeatRequired>
repeat_optional <- "?" <RepeatOptional> repeat_optional <- "?" <RepeatOptional>
repeat_spec <- ( "{" _min:[0-9]+ "," _max:[0-9]+ "}" repeat_spec <- ( "{" min:[0-9]+ "," max:[0-9]+ "}"
/ "{" _min:[0-9]+ ",}" / "{" min:[0-9]+ ",}"
/ "{" _exact:[0-9]+ "}" ) <RepeatSpec> / "{" exact:[0-9]+ "}" ) <RepeatSpec>
repeat_greedy <- "?" repeat_greedy <- "?"
subexp <- "(" _capture:( subexp_no_capture / subexp_positive_lookahead / subexp_negative_lookahead )? regexp ")" <Subexp> subexp <- "(" capture:( subexp_no_capture / subexp_positive_lookahead / subexp_negative_lookahead )? regexp ")" <Subexp>
subexp_no_capture <- "?:" subexp_no_capture <- "?:"
subexp_positive_lookahead <- "?=" subexp_positive_lookahead <- "?="
subexp_negative_lookahead <- "?!" subexp_negative_lookahead <- "?!"
charset <- "[" _invert:"^"? parts:( charset_range / charset_terminal )* "]" <Charset> charset <- "[" invert:"^"? parts:( charset_range / charset_terminal )* "]" <Charset>
charset_range <- first:charset_terminal "-" last:charset_terminal <CharsetRange> charset_range <- first:charset_terminal "-" last:charset_terminal <CharsetRange>
charset_terminal <- charset_escape <CharsetEscape> charset_terminal <- charset_escape <CharsetEscape>
/ charset_literal <CharsetLiteral> / charset_literal <CharsetLiteral>

View File

@ -16,5 +16,9 @@ export default {
ry: 3 ry: 3
}); });
}); });
},
setup() {
this.literal = this.properties.literal;
} }
}; };

View File

@ -6,21 +6,20 @@ export default {
_render() { _render() {
var start, end, var start, end,
parts = this.parts(),
partPromises; partPromises;
if (this.anchorStart()) { if (this.anchorStart) {
start = this.renderLabel('Start of line') start = this.renderLabel('Start of line')
.invoke('addClass', 'anchor'); .invoke('addClass', 'anchor');
} }
if (this.anchorEnd()) { if (this.anchorEnd) {
end = this.renderLabel('End of line') end = this.renderLabel('End of line')
.invoke('addClass', 'anchor'); .invoke('addClass', 'anchor');
} }
if (start || end || parts.length !== 1) { if (start || end || this.parts.length !== 1) {
partPromises = _.map(parts, part => { partPromises = _.map(this.parts, part => {
return part.render(this.container.group()); return part.render(this.container.group());
}); });
@ -48,34 +47,10 @@ export default {
this.container.path(paths.join(''))); this.container.path(paths.join('')));
}); });
} else { } 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() { _getAnchor() {
var start = this.normalizeBBox(_.first(this.items).getBBox()), var start = this.normalizeBBox(_.first(this.items).getBBox()),
end = this.normalizeBBox(_.last(this.items).getBBox()), end = this.normalizeBBox(_.last(this.items).getBBox()),
@ -87,5 +62,24 @@ export default {
ax2: matrix.x(end.ax2, end.ay), ax2: matrix.x(end.ax2, end.ay),
ay: matrix.y(start.ax, start.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 !== '';
} }
}; };

View File

@ -4,34 +4,34 @@ export default {
type: 'match-fragment', type: 'match-fragment',
_render() { _render() {
if (this._repeat.textValue === '') { if (!this.repeat) {
return this.proxy(this._content); return this.proxy(this.content);
} else { } else {
return this._content.render(this.container.group()) return this.content.render(this.container.group())
.then(() => { .then(() => {
var box, paths = []; 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), let vert = Math.max(0, box.ay - box.y - 10),
horiz = box.width - 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`); 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`); 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; 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`); 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`); paths.push(`M${box.x2 + 10},${box.ay + 15}l5,-5m-5,5l-5,-5`);
} }
} }
@ -42,10 +42,10 @@ export default {
} }
}) })
.then(() => { .then(() => {
var labelStr = this._repeat.label(), var labelStr = this.repeat.label,
label, label,
labelBox, labelBox,
labelShift = (this._repeat.hasSkip() ? 5 : 0), labelShift = (this.repeat.hasSkip ? 5 : 0),
box = this.getBBox(); box = this.getBBox();
if (labelStr) { if (labelStr) {
@ -60,7 +60,7 @@ export default {
}, },
_getAnchor() { _getAnchor() {
var anchor = this._content.getAnchor(), var anchor = this.content.getAnchor(),
matrix = this.transform().localMatrix; matrix = this.transform().localMatrix;
return _.extend(anchor, { return _.extend(anchor, {
@ -68,5 +68,13 @@ export default {
ax2: matrix.x(anchor.ax2, anchor.ay), ax2: matrix.x(anchor.ax2, anchor.ay),
ay: matrix.y(anchor.ax, 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;
}
} }
}; };

View File

@ -11,20 +11,6 @@ export default class Node {
this.elements = elements || []; this.elements = elements || [];
this.properties = properties; 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) { set module(mod) {

View File

@ -5,39 +5,38 @@ export default {
type: 'regexp', type: 'regexp',
_render() { _render() {
var matches = this.matches(), var matchContainer;
matchContainer;
if (matches.length === 1) { if (this.matches.length === 1) {
return this.proxy(matches[0]); return this.proxy(this.matches[0]);
} else { } else {
matchContainer = this.container.group() matchContainer = this.container.group()
.addClass('regexp-matches') .addClass('regexp-matches')
.transform(Snap.matrix() .transform(Snap.matrix()
.translate(20, 0)); .translate(20, 0));
return Q.all(_.map(matches, match => { return Q.all(_.map(this.matches, match => {
return match.render(matchContainer.group()); return match.render(matchContainer.group());
})) }))
.then(() => { .then(() => {
var containerBox, var containerBox,
paths; paths;
this.spaceVertically(matches, { this.spaceVertically(this.matches, {
padding: 5 padding: 5
}); });
containerBox = this.getBBox(); 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, _.first(this.matches)));
paths.push(this.makeSideLine(containerBox, _.last(matches))); paths.push(this.makeSideLine(containerBox, _.last(this.matches)));
this.container.prepend( this.container.prepend(
this.container.path(paths.join(''))); this.container.path(paths.join('')));
matchContainer.prepend( matchContainer.prepend(
matchContainer.path(_.map(matches, match => { matchContainer.path(_.map(this.matches, match => {
var box = match.getBBox(), var box = match.getBBox(),
container = matchContainer.getBBox(); container = matchContainer.getBBox();
@ -87,7 +86,10 @@ export default {
} }
}, },
matches() { setup() {
return [this._match].concat(_.map(this._alternates.elements, _.property('match'))); this.matches = [this.properties.match]
.concat(_.map(this.properties.alternates.elements, element => {
return element.properties.match;
}));
} }
}; };

View File

@ -1,60 +1,50 @@
export default { function formatTimes(times) {
minimum() { if (times === 1) {
return this._spec.minimum(); return 'once';
}, } else {
return `${times} times`;
maximum() { }
return this._spec.maximum(); }
},
export default {
greedy() { setup() {
return (this._greedy.textValue === ''); this.minimum = this.properties.spec.minimum;
}, this.maximum = this.properties.spec.maximum;
this.greedy = (this.properties.greedy.textValue === '');
hasSkip() { this.hasSkip = this.minimum === 0;
return this.minimum() === 0; this.hasLoop = this.maximum === -1 || this.maximum > 1;
},
Object.defineProperty(this, 'contentPosition', {
hasLoop() { get: function() {
return this.maximum() === -1 || this.maximum() > 1; var x = 0, y = 0;
},
if (this.hasLoop) {
contentPosition() { x = 10;
var x = 0, y = 0; }
if (this.hasLoop()) { if (this.hasSkip) {
x = 10; y = 10;
} x = 15;
}
if (this.hasSkip()) {
y = 10; return Snap.matrix().translate(x, y);
x = 15; }
} });
return Snap.matrix().translate(x, y); Object.defineProperty(this, 'label', {
}, get: function() {
if (this.minimum >= 2 && this.maximum === -1) {
label() { return `${this.minimum - 1}+ times`;
var maximum = this.maximum(), } else if (this.minimum <= 1 && this.maximum >= 2) {
minimum = this.minimum(), return `at most ${formatTimes(this.maximum - 1)}`;
formatTimes = times => { } else if (this.minimum >= 2 && this.maximum >= 2) {
if (times === 1) { if (this.minimum === this.maximum) {
return 'once'; return formatTimes(this.minimum - 1);
} else { } else {
return `${times} times`; return `${this.minimum - 1}...${formatTimes(this.maximum - 1)}`;
} }
}; }
}
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)}`;
}
}
} }
} }

View File

@ -1,9 +1,6 @@
export default { export default {
minimum() { setup() {
return 0; this.minimum = 0;
}, this.maximum = -1;
maximum() {
return -1;
} }
}; };

View File

@ -1,9 +1,6 @@
export default { export default {
minimum() { setup() {
return 0; this.minimum = 0;
}, this.maximum = 1;
maximum() {
return 1;
} }
}; };

View File

@ -1,9 +1,6 @@
export default { export default {
minimum() { setup() {
return 1; this.minimum = 1;
}, this.maximum = -1;
maximum() {
return -1;
} }
}; };

View File

@ -1,21 +1,19 @@
export default { export default {
minimum() { setup() {
if (this._min) { if (this.properties.min) {
return Number(this._min.textValue); this.minimum = Number(this.properties.min.textValue);
} else if (this._exact) { } else if (this.properties.exact) {
return Number(this._exact.textValue); this.minimum = Number(this.properties.exact.textValue);
} else { } else {
return 0; this.minimum = 0;
} }
},
maximum() { if (this.properties.max) {
if (this._max) { this.maximum = Number(this.properties.max.textValue);
return Number(this._max.textValue); } else if (this.properties.exact) {
} else if (this._exact) { this.maximum = Number(this.properties.exact.textValue);
return Number(this._exact.textValue);
} else { } else {
return -1; this.maximum = -1;
} }
} }
}; };

View File

@ -4,16 +4,15 @@ export default {
type: 'root', type: 'root',
_render() { _render() {
var flags = this.flags(), var flagLabels = [];
flagLabels = [];
if (flags.global) { if (this.flags.global) {
flagLabels.push('Global'); flagLabels.push('Global');
} }
if (flags.ignore_case) { if (this.flags.ignore_case) {
flagLabels.push('Ignore Case'); flagLabels.push('Ignore Case');
} }
if (flags.multiline) { if (this.flags.multiline) {
flagLabels.push('Multiline'); flagLabels.push('Multiline');
} }
@ -52,19 +51,21 @@ export default {
}); });
}, },
flags() { setup() {
var flags; var flagsStr;
if (this._flags) { if (this.flags) {
flags = this._flags.textValue; flagsStr = this.flags.textValue;
} else { } else {
flags = ''; flagsStr = '';
} }
return { this.flags = {
global: /g/.test(flags), global: /g/.test(flagsStr),
ignore_case: /i/.test(flags), ignore_case: /i/.test(flagsStr),
multiline: /m/.test(flags) multiline: /m/.test(flagsStr)
}; };
this.regexp = this.properties.regexp
} }
}; };

View File

@ -12,11 +12,9 @@ export default {
}, },
_render() { _render() {
var label = this.groupLabel(); if (this.label) {
if (label) {
return this.regexp.render(this.container.group()) 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 padding: 10
})); }));
} else { } 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() { resetCounter() {
groupCounter = 1; groupCounter = 1;
}, },
@ -45,5 +35,15 @@ export default {
ax2: matrix.x(anchor.ax2, anchor.ay), ax2: matrix.x(anchor.ax2, anchor.ay),
ay: matrix.y(anchor.ax, 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;
} }
}; };