From b36419803058089e03f2a4159bbc60a6dbfcf0db Mon Sep 17 00:00:00 2001 From: Jeff Avallone Date: Tue, 2 Dec 2014 21:09:20 -0500 Subject: [PATCH] Adding a MatchFragment rule Also reducing the amount of object cloning that is being done --- src/js/parser/javascript.js | 2 ++ src/js/parser/javascript/grammar.peg | 3 ++- src/js/parser/javascript/match.js | 7 ++++--- src/js/parser/javascript/match_fragment.js | 6 ++++++ 4 files changed, 14 insertions(+), 4 deletions(-) create mode 100644 src/js/parser/javascript/match_fragment.js diff --git a/src/js/parser/javascript.js b/src/js/parser/javascript.js index 969c8d4..ccdf6e7 100644 --- a/src/js/parser/javascript.js +++ b/src/js/parser/javascript.js @@ -3,6 +3,7 @@ import parser from './javascript/grammar.peg'; import Root from './javascript/root.js'; import Regexp from './javascript/regexp.js'; import Match from './javascript/match.js'; +import MatchFragment from './javascript/match_fragment.js'; import Subexp from './javascript/subexp.js'; import Charset from './javascript/charset.js'; import Terminal from './javascript/terminal.js'; @@ -10,6 +11,7 @@ import Terminal from './javascript/terminal.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.Terminal = Terminal; diff --git a/src/js/parser/javascript/grammar.peg b/src/js/parser/javascript/grammar.peg index b606839..03672db 100644 --- a/src/js/parser/javascript/grammar.peg +++ b/src/js/parser/javascript/grammar.peg @@ -2,8 +2,9 @@ grammar JavascriptRegexp root <- ( ( "/" regexp "/" _flags:[igm]* ) / regexp ""? ) regexp <- _match:match _alternates:( "|" match )* match <- _anchor_start:anchor_start? - _parts:( ( subexp / charset / terminal ) repeat? )* + _parts:match_fragment* _anchor_end:anchor_end? + match_fragment <- ( subexp / charset / terminal ) repeat? anchor_start <- "^" anchor_end <- "$" repeat <- ( repeat_any / repeat_required / repeat_optional / repeat_spec ) repeat_greedy? diff --git a/src/js/parser/javascript/match.js b/src/js/parser/javascript/match.js index 62620ac..c8da555 100644 --- a/src/js/parser/javascript/match.js +++ b/src/js/parser/javascript/match.js @@ -17,7 +17,7 @@ export default _.extend({}, Base, { this.contents.parts = _.map(this.parts(), function(part) { var content = container.group(); - part.elements[0].render(content); + part.render(content); return { content, part }; }); @@ -35,7 +35,7 @@ export default _.extend({}, Base, { } _.each(this.contents.parts, function(thing) { - thing.part.elements[0].position(); + thing.part.position(); thing.content.transform(Snap.matrix() .translate(offset, 0)); offset += thing.content.getBBox().width; @@ -62,6 +62,7 @@ export default _.extend({}, Base, { if (last) { if (node.elements[0].type === 'terminal' && last.elements[0].type === 'terminal' && last.elements[1].textValue === '') { + last = _.clone(last, true); last.textValue += node.textValue; last.elements[0].textValue += node.elements[0].textValue; last.elements[1] = node.elements[1]; @@ -71,7 +72,7 @@ export default _.extend({}, Base, { } } - result.push(_.clone(node, true)); + result.push(node); return result; }, []); } diff --git a/src/js/parser/javascript/match_fragment.js b/src/js/parser/javascript/match_fragment.js new file mode 100644 index 0000000..faccf54 --- /dev/null +++ b/src/js/parser/javascript/match_fragment.js @@ -0,0 +1,6 @@ +import _ from 'lodash'; +import Base from './base.js'; + +export default _.extend({}, Base, { + type: 'match_fragment' +});