Skip to content

Commit

Permalink
fix: expression parsing improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanPiercey committed Mar 19, 2022
1 parent b7f3d3e commit e38b29e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
1╭─ a=x * y
│ ││╰─ attrValue.value "x * y"
│ │├─ attrValue "=x * y"
│ │╰─ attrName
╰─ ╰─ tagName
2╭─ b=x ** y
│ ││╰─ attrValue.value "x ** y"
│ │├─ attrValue "=x ** y"
│ │╰─ attrName
│ ├─ closeTag(a)
│ ├─ openTagEnd(a)
╰─ ╰─ tagName
3╭─ c=x &&
│ ││╰─ attrValue.value "x &&\ny"
│ │├─ attrValue "=x &&\ny"
│ │╰─ attrName
│ ├─ closeTag(b)
│ ├─ openTagEnd(b)
╰─ ╰─ tagName
4╭─ y
│ ├─ closeTag(c)
╰─ ╰─ openTagEnd(c)
4 changes: 4 additions & 0 deletions src/__tests__/fixtures/attr-operators/input.marko
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
a=x * y
b=x ** y
c=x &&
y
18 changes: 9 additions & 9 deletions src/states/EXPRESSION.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,28 +224,28 @@ export const EXPRESSION: StateDefinition<ExpressionMeta> = {

function buildOperatorPattern(isConcise: boolean) {
const binary =
"[*%<&^|?:]" + // Any of these characters can always continue an expression
"|=[=>]" + // We only continue after an equals if it is => or ==
"[!*%<&^|?:]+" + // Any of these characters can always continue an expression
"|=[=>]+" + // We only continue after an equals if it is => or ==
"|/(?:\\b|\\s)" + // We only continue after a forward slash if it isn't //, /* or />
"|\\.(?=\\s)" + // We only continue after a period if it's followed by a space
"|\\bin(?:stanceof)(?=\\s+[^=/,;:>])"; // We only continue after word operators (instanceof/in) when they are not followed by a terminator
const unary =
"!" +
"|a(?:sync|wait)" +
"\\b(?:" +
"a(?:sync|wait)" +
"|class" +
"|function" +
"|new" +
"|typeof" +
"|void";
"|void" +
")\\b";
const lookAheadPattern =
"\\s*(?:" +
binary +
"|\\+" + // any number of plus signs are ok.
`|-${isConcise ? "[^-]" : ""}` + // in concise mode only consume minus signs if not --
`|>${isConcise ? "" : "[>=]"}` + // in html mode only consume closing angle brackets if it is >= or >>
"|\\++" + // any number of plus signs are ok.
`|-${isConcise ? "[^-]" : "+"}` + // in concise mode only consume minus signs if not --
`|>+${isConcise ? "" : "[>=]"}` + // in html mode only consume closing angle brackets if it is >= or >>
")\\s*" +
`|\\s+(?=[${isConcise ? "" : "["}{(])`; // if we have spaces followed by an opening bracket, we'll consume the spaces and let the expression state handle the brackets

const lookBehindPattern = `(?<=${unary}|${binary}|[^-]-|[^+]\\+)`;
return new RegExp(`${lookAheadPattern}|${lookBehindPattern}`, "y");
}
Expand Down

0 comments on commit e38b29e

Please sign in to comment.