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',
_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;
}
});
}
};

View File

@ -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;
}
};

View File

@ -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();
}
};

View File

@ -1,25 +1,25 @@
grammar JavascriptRegexp
root <- ( ( "/" regexp "/" _flags:[igm]* ) / regexp ""? ) <Root>
regexp <- _match:match _alternates:( "|" match )* <Regexp>
match <- _anchor_start:anchor_start?
(!repeat) _parts:match_fragment*
_anchor_end:anchor_end? <Match>
match_fragment <- _content:( subexp / charset / terminal ) _repeat:repeat? <MatchFragment>
root <- ( ( "/" regexp "/" flags:[igm]* ) / regexp ""? ) <Root>
regexp <- match:match alternates:( "|" match )* <Regexp>
match <- anchor_start:anchor_start?
(!repeat) parts:match_fragment*
anchor_end:anchor_end? <Match>
match_fragment <- content:( subexp / charset / terminal ) repeat:repeat? <MatchFragment>
anchor_start <- "^"
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_required <- "+" <RepeatRequired>
repeat_optional <- "?" <RepeatOptional>
repeat_spec <- ( "{" _min:[0-9]+ "," _max:[0-9]+ "}"
/ "{" _min:[0-9]+ ",}"
/ "{" _exact:[0-9]+ "}" ) <RepeatSpec>
repeat_spec <- ( "{" min:[0-9]+ "," max:[0-9]+ "}"
/ "{" min:[0-9]+ ",}"
/ "{" exact:[0-9]+ "}" ) <RepeatSpec>
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_positive_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_terminal <- charset_escape <CharsetEscape>
/ charset_literal <CharsetLiteral>

View File

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

View File

@ -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 !== '';
}
};

View File

@ -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;
}
}
};

View File

@ -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) {

View File

@ -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;
}));
}
};

View File

@ -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 => {
function 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);
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 `${minimum - 1}...${formatTimes(maximum - 1)}`;
return `${this.minimum - 1}...${formatTimes(this.maximum - 1)}`;
}
}
}
});
}
}

View File

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

View File

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

View File

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

View File

@ -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;
}
}
};

View File

@ -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
}
};

View File

@ -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;
}
};