Skip to content

Commit

Permalink
Avoid regexp for expression continuations (#130)
Browse files Browse the repository at this point in the history
* refactor: avoid regexp parsing for expression continuations
  • Loading branch information
DylanPiercey authored Aug 8, 2022
1 parent 250c17b commit ebc850f
Show file tree
Hide file tree
Showing 28 changed files with 817 additions and 421 deletions.
5 changes: 5 additions & 0 deletions .changeset/tame-geese-drop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"htmljs-parser": patch
---

Switch from regexp based parsing for the expression continuations. This slightly improves performance and more importantly fixes usage of the parser in safari.
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,39 @@
│ ├─ closeTagEnd(tag)
│ ├─ openTagEnd
╰─ ╰─ tagName "tag"
4╭─ tag a = async x => { console.log("y") } b
4╭─ tag a = x => y b
│ │ │ │ │ ╰─ attrName
│ │ │ │ ╰─ attrValue.value "x => y"
│ │ │ ╰─ attrValue "= x => y"
│ │ ╰─ attrName
│ ├─ closeTagEnd(tag)
│ ├─ openTagEnd
╰─ ╰─ tagName "tag"
5╭─ tag a = x => y + 1 b
│ │ │ │ │ ╰─ attrName
│ │ │ │ ╰─ attrValue.value "x => y + 1"
│ │ │ ╰─ attrValue "= x => y + 1"
│ │ ╰─ attrName
│ ├─ closeTagEnd(tag)
│ ├─ openTagEnd
╰─ ╰─ tagName "tag"
6╭─ tag a = x => y = 1 b
│ │ │ │ │ ╰─ attrName
│ │ │ │ ╰─ attrValue.value "x => y = 1"
│ │ │ ╰─ attrValue "= x => y = 1"
│ │ ╰─ attrName
│ ├─ closeTagEnd(tag)
│ ├─ openTagEnd
╰─ ╰─ tagName "tag"
7╭─ tag a = async x => { console.log("y") } b
│ │ │ │ │ ╰─ attrName
│ │ │ │ ╰─ attrValue.value "async x => { console.log(\"y\") }"
│ │ │ ╰─ attrValue "= async x => { console.log(\"y\") }"
│ │ ╰─ attrName
│ ├─ closeTagEnd(tag)
│ ├─ openTagEnd
╰─ ╰─ tagName "tag"
5╭─ tag a = async function (x) { console.log("y") } b
8╭─ tag a = async function (x) { console.log("y") } b
│ │ │ │ │ │├─ closeTagEnd(tag)
│ │ │ │ │ │╰─ openTagEnd
│ │ │ │ │ ╰─ attrName
Expand Down
3 changes: 3 additions & 0 deletions src/__tests__/fixtures/attr-complex-functions/input.marko
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
tag a = function (x) { console.log("y") } b
tag a = (x) => { console.log("y") } b
tag a = x => { console.log("y") } b
tag a = x => y b
tag a = x => y + 1 b
tag a = x => y = 1 b
tag a = async x => { console.log("y") } b
tag a = async function (x) { console.log("y") } b
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,23 @@
│ ││ │ ╰─ attrValue "= 'foo'"
│ ││ ╰─ attrName
│ │╰─ tagName "tag"
╰─ ╰─ openTagStart
╰─ ╰─ openTagStart
12├─
13╭─ tag a = 'foo' instanceofthing String
│ │ │ │ │ │ ╰─ attrName "String"
│ │ │ │ │ ╰─ attrName "instanceofthing"
│ │ │ │ ╰─ attrValue.value "'foo'"
│ │ │ ╰─ attrValue "= 'foo'"
│ │ ╰─ attrName
╰─ ╰─ tagName "tag"
14╭─
╰─ ╰─ openTagEnd
15╭─ tag a = 'foo' instanceof
│ │ │ │ │ │ ├─ closeTagEnd(tag)
│ │ │ │ │ │ ╰─ openTagEnd
│ │ │ │ │ ╰─ attrName "instanceof"
│ │ │ │ ╰─ attrValue.value "'foo'"
│ │ │ ╰─ attrValue "= 'foo'"
│ │ ╰─ attrName
│ ├─ closeTagEnd(tag)
╰─ ╰─ tagName "tag"
6 changes: 5 additions & 1 deletion src/__tests__/fixtures/attr-complex-instanceof/input.marko
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ tag a = 'foo' instanceof;
tag a = 'foo' instanceof, b

<tag a = 'foo' instanceof></tag>
<tag a = 'foo' instanceof/>
<tag a = 'foo' instanceof/>

tag a = 'foo' instanceofthing String

tag a = 'foo' instanceof
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
1╭─ a b=c -- y
│ │ │││ │ ╰─ text
│ │ │││ ╰─ openTagEnd
│ │ ││╰─ attrValue.value
│ │ │╰─ attrValue "=c"
│ │ ╰─ attrName
╰─ ╰─ tagName
2├─
3╭─ a [b=c -- y]
│ │ │││ ╰─ attrName
│ │ ││╰─ attrValue.value "c --"
│ │ │╰─ attrValue "=c --"
│ │ ╰─ attrName
│ ├─ closeTagEnd(a)
╰─ ╰─ tagName
4╭─
│ ├─ openTagEnd
╰─ ╰─ closeTagEnd(a)
3 changes: 3 additions & 0 deletions src/__tests__/fixtures/attr-concise-hyphens/input.marko
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
a b=c -- y

a [b=c -- y]
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
1╭─ tag a = 'foo' instanceof
│ │ │ │ │ ╰─ attrName "instanceof"
│ │ │ │ ╰─ attrValue.value "'foo'"
│ │ │ ╰─ attrValue "= 'foo'"
│ │ ╰─ attrName
╰─ ╰─ tagName "tag"
2╭─
│ ├─ openTagEnd
╰─ ╰─ closeTagEnd(tag)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tag a = 'foo' instanceof
Original file line number Diff line number Diff line change
Expand Up @@ -683,14 +683,17 @@
│ │╰─ openTagEnd:selfClosed "/>"
╰─ ╰─ attrName
159╭─ <a=x >=
│ │││╰─ attrValue.value "x >=\ny"
│ ││├─ attrValue "=x >=\ny"
│ ││││ │╰─ text "=\ny "
│ ││││ ╰─ openTagEnd
│ │││╰─ attrValue.value
│ ││├─ attrValue "=x"
│ ││╰─ attrName
│ │╰─ tagName
╰─ ╰─ openTagStart
160╭─ y a/>
│ │╰─ openTagEnd:selfClosed "/>"
╰─ ╰─ attrName
160╭─ y </a>
│ │ │╰─ closeTagEnd(a)
│ │ ╰─ closeTagName
╰─ ╰─ closeTagStart "</"
161╭─ <a=x &=
│ │││╰─ attrValue.value "x &=\ny"
│ ││├─ attrValue "=x &=\ny"
Expand Down Expand Up @@ -746,23 +749,29 @@
│ │╰─ openTagEnd:selfClosed "/>"
╰─ ╰─ attrName
173╭─ <a=x >>=
│ │││╰─ attrValue.value "x >>=\ny"
│ ││├─ attrValue "=x >>=\ny"
│ ││││ │╰─ text ">=\ny "
│ ││││ ╰─ openTagEnd
│ │││╰─ attrValue.value
│ ││├─ attrValue "=x"
│ ││╰─ attrName
│ │╰─ tagName
╰─ ╰─ openTagStart
174╭─ y a/>
│ │╰─ openTagEnd:selfClosed "/>"
╰─ ╰─ attrName
174╭─ y </a>
│ │ │╰─ closeTagEnd(a)
│ │ ╰─ closeTagName
╰─ ╰─ closeTagStart "</"
175╭─ <a=x >>>=
│ │││╰─ attrValue.value "x >>>=\ny"
│ ││├─ attrValue "=x >>>=\ny"
│ ││││ │╰─ text ">>=\ny "
│ ││││ ╰─ openTagEnd
│ │││╰─ attrValue.value
│ ││├─ attrValue "=x"
│ ││╰─ attrName
│ │╰─ tagName
╰─ ╰─ openTagStart
176╭─ y a/>
│ │╰─ openTagEnd:selfClosed "/>"
╰─ ╰─ attrName
176╭─ y </a>
│ │ │╰─ closeTagEnd(a)
│ │ ╰─ closeTagName
╰─ ╰─ closeTagStart "</"
177╭─ <a=x -=
│ │││╰─ attrValue.value "x -=\ny"
│ ││├─ attrValue "=x -=\ny"
Expand Down Expand Up @@ -836,23 +845,29 @@
│ │╰─ openTagEnd:selfClosed "/>"
╰─ ╰─ attrName
193╭─ <a=x >>
│ │││╰─ attrValue.value "x >>\ny"
│ ││├─ attrValue "=x >>\ny"
│ ││││ │╰─ text ">\ny "
│ ││││ ╰─ openTagEnd
│ │││╰─ attrValue.value
│ ││├─ attrValue "=x"
│ ││╰─ attrName
│ │╰─ tagName
╰─ ╰─ openTagStart
194╭─ y a/>
│ │╰─ openTagEnd:selfClosed "/>"
╰─ ╰─ attrName
194╭─ y </a>
│ │ │╰─ closeTagEnd(a)
│ │ ╰─ closeTagName
╰─ ╰─ closeTagStart "</"
195╭─ <a=x >>>
│ │││╰─ attrValue.value "x >>>\ny"
│ ││├─ attrValue "=x >>>\ny"
│ ││││ │╰─ text ">>\ny "
│ ││││ ╰─ openTagEnd
│ │││╰─ attrValue.value
│ ││├─ attrValue "=x"
│ ││╰─ attrName
│ │╰─ tagName
╰─ ╰─ openTagStart
196╭─ y a/>
│ │╰─ openTagEnd:selfClosed "/>"
╰─ ╰─ attrName
196╭─ y </a>
│ │ │╰─ closeTagEnd(a)
│ │ ╰─ closeTagName
╰─ ╰─ closeTagStart "</"
197╭─ <a=x
│ │││╰─ attrValue.value "x\n( y )"
│ ││├─ attrValue "=x\n( y )"
Expand Down
10 changes: 5 additions & 5 deletions src/__tests__/fixtures/attr-operators-newline-after/input.marko
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ y a/>
<a=x <=
y a/>
<a=x >=
y a/>
y </a>
<a=x &=
y a/>
<a=x &&=
Expand All @@ -171,9 +171,9 @@ y a/>
<a=x ~=
y a/>
<a=x >>=
y a/>
y </a>
<a=x >>>=
y a/>
y </a>
<a=x -=
y a/>
<a=x /=
Expand All @@ -191,9 +191,9 @@ y a/>
<a=x +
++ y a/>
<a=x >>
y a/>
y </a>
<a=x >>>
y a/>
y </a>
<a=x
( y ) a/>
<a=x
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,14 +191,17 @@
│ │╰─ openTagEnd:selfClosed "/>"
╰─ ╰─ attrName
45╭─ <a=x
│ │││╰─ attrValue.value "x\n>= y"
│ ││├─ attrValue "=x\n>= y"
│ │││╰─ attrValue.value
│ ││├─ attrValue "=x"
│ ││╰─ attrName
│ │╰─ tagName
╰─ ╰─ openTagStart
46╭─ >= y a/>
│ │╰─ openTagEnd:selfClosed "/>"
╰─ ╰─ attrName
46╭─ >= y </a>
│ ││ │ │╰─ closeTagEnd(a)
│ ││ │ ╰─ closeTagName
│ ││ ╰─ closeTagStart "</"
│ │╰─ text "= y "
╰─ ╰─ openTagEnd
47╭─ <a=x
│ │││╰─ attrValue.value "x\n&= y"
│ ││├─ attrValue "=x\n&= y"
Expand Down Expand Up @@ -254,23 +257,29 @@
│ │╰─ openTagEnd:selfClosed "/>"
╰─ ╰─ attrName
59╭─ <a=x
│ │││╰─ attrValue.value "x\n>>= y"
│ ││├─ attrValue "=x\n>>= y"
│ │││╰─ attrValue.value
│ ││├─ attrValue "=x"
│ ││╰─ attrName
│ │╰─ tagName
╰─ ╰─ openTagStart
60╭─ >>= y a/>
│ │╰─ openTagEnd:selfClosed "/>"
╰─ ╰─ attrName
60╭─ >>= y </a>
│ ││ │ │╰─ closeTagEnd(a)
│ ││ │ ╰─ closeTagName
│ ││ ╰─ closeTagStart "</"
│ │╰─ text ">= y "
╰─ ╰─ openTagEnd
61╭─ <a=x
│ │││╰─ attrValue.value "x\n>>>= y"
│ ││├─ attrValue "=x\n>>>= y"
│ │││╰─ attrValue.value
│ ││├─ attrValue "=x"
│ ││╰─ attrName
│ │╰─ tagName
╰─ ╰─ openTagStart
62╭─ >>>= y a/>
│ │╰─ openTagEnd:selfClosed "/>"
╰─ ╰─ attrName
62╭─ >>>= y </a>
│ ││ │ │╰─ closeTagEnd(a)
│ ││ │ ╰─ closeTagName
│ ││ ╰─ closeTagStart "</"
│ │╰─ text ">>= y "
╰─ ╰─ openTagEnd
63╭─ <a=x
│ │││╰─ attrValue.value "x\n-= y"
│ ││├─ attrValue "=x\n-= y"
Expand Down Expand Up @@ -344,23 +353,29 @@
│ │╰─ openTagEnd:selfClosed "/>"
╰─ ╰─ attrName
79╭─ <a=x
│ │││╰─ attrValue.value "x\n>>y"
│ ││├─ attrValue "=x\n>>y"
│ │││╰─ attrValue.value
│ ││├─ attrValue "=x"
│ ││╰─ attrName
│ │╰─ tagName
╰─ ╰─ openTagStart
80╭─ >>y a/>
│ │╰─ openTagEnd:selfClosed "/>"
╰─ ╰─ attrName
80╭─ >>y </a>
│ ││ │ │╰─ closeTagEnd(a)
│ ││ │ ╰─ closeTagName
│ ││ ╰─ closeTagStart "</"
│ │╰─ text ">y "
╰─ ╰─ openTagEnd
81╭─ <a=x
│ │││╰─ attrValue.value "x\n>>> y"
│ ││├─ attrValue "=x\n>>> y"
│ │││╰─ attrValue.value
│ ││├─ attrValue "=x"
│ ││╰─ attrName
│ │╰─ tagName
╰─ ╰─ openTagStart
82╭─ >>> y a/>
│ │╰─ openTagEnd:selfClosed "/>"
╰─ ╰─ attrName
82╭─ >>> y </a>
│ ││ │ │╰─ closeTagEnd(a)
│ ││ │ ╰─ closeTagName
│ ││ ╰─ closeTagStart "</"
│ │╰─ text ">> y "
╰─ ╰─ openTagEnd
83╭─ <a=x
│ │││╰─ attrValue.value "x\n( y )"
│ ││├─ attrValue "=x\n( y )"
Expand Down
Loading

0 comments on commit ebc850f

Please sign in to comment.