-
-
Notifications
You must be signed in to change notification settings - Fork 273
feat: Add support of inner expressions enclosed by parentheses #863
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
e7e3aeb
75314d0
489d136
a84df57
4b5296a
ba50a32
0bedce9
d6ec7cd
f45c4f1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| import { FilteredValueToken, TagToken, HTMLToken, HashToken, QuotedToken, LiquidTagToken, OutputToken, ValueToken, Token, RangeToken, FilterToken, TopLevelToken, PropertyAccessToken, OperatorToken, LiteralToken, IdentifierToken, NumberToken } from '../tokens' | ||
| import { FilteredValueToken, TagToken, HTMLToken, HashToken, QuotedToken, LiquidTagToken, OutputToken, ValueToken, Token, RangeToken, FilterToken, TopLevelToken, PropertyAccessToken, OperatorToken, LiteralToken, IdentifierToken, NumberToken, GroupedExpressionToken } from '../tokens' | ||
| import { OperatorHandler } from '../render/operator' | ||
| import { TrieNode, LiteralValue, Trie, createTrie, ellipsis, literalValues, TokenizationError, TYPES, QUOTE, BLANK, NUMBER, SIGN, isWord, isString } from '../util' | ||
| import { Operators, Expression } from '../render' | ||
|
|
@@ -9,6 +9,7 @@ import { whiteSpaceCtrl } from './whitespace-ctrl' | |
| export class Tokenizer { | ||
| p: number | ||
| N: number | ||
| public groupedExpressions: boolean | ||
| private rawBeginAt = -1 | ||
| private opTrie: Trie<OperatorHandler> | ||
| private literalTrie: Trie<LiteralValue> | ||
|
|
@@ -17,12 +18,14 @@ export class Tokenizer { | |
| public input: string, | ||
| operators: Operators = defaultOptions.operators, | ||
| public file?: string, | ||
| range?: [number, number] | ||
| range?: [number, number], | ||
| groupedExpressions = false | ||
| ) { | ||
| this.p = range ? range[0] : 0 | ||
| this.N = range ? range[1] : input.length | ||
| this.opTrie = createTrie(operators) | ||
| this.literalTrie = createTrie(literalValues) | ||
| this.groupedExpressions = groupedExpressions | ||
| } | ||
|
|
||
| readExpression () { | ||
|
|
@@ -310,7 +313,15 @@ export class Tokenizer { | |
| readValue (): ValueToken | undefined { | ||
| this.skipBlank() | ||
| const begin = this.p | ||
| const variable = this.readLiteral() || this.readQuoted() || this.readRange() || this.readNumber() | ||
| let variable: ValueToken | undefined = this.readLiteral() || this.readQuoted() | ||
| if (!variable && this.peek() === '(') { | ||
| if (this.groupedExpressions && !this.looksLikeRange()) { | ||
| variable = this.readGroupedExpression() | ||
| } else { | ||
| variable = this.readRange() | ||
| } | ||
| } | ||
| variable = variable || this.readNumber() | ||
| const props = this.readProperties(!variable) | ||
| if (!props.length) return variable | ||
| return new PropertyAccessToken(variable, props, this.input, begin, this.p) | ||
|
|
@@ -420,6 +431,70 @@ export class Tokenizer { | |
| return new QuotedToken(this.input, begin, this.p, this.file) | ||
| } | ||
|
|
||
| readGroupedExpression (): GroupedExpressionToken | undefined { | ||
| this.skipBlank() | ||
| if (this.peek() !== '(') return | ||
| const begin = this.p | ||
| ++this.p | ||
| const closeParen = this.findMatchingParen() | ||
| if (closeParen === -1) { | ||
| this.p = begin | ||
| return | ||
| } | ||
| const savedN = this.N | ||
|
skynetigor marked this conversation as resolved.
Outdated
|
||
| this.N = closeParen | ||
| const fvt = this.readFilteredValue() | ||
| this.N = savedN | ||
| this.p = closeParen + 1 | ||
| return new GroupedExpressionToken(fvt.initial, fvt.filters, this.input, begin, this.p, this.file) | ||
| } | ||
|
|
||
| private findMatchingParen (): number { | ||
| let depth = 1 | ||
| let i = this.p | ||
| while (i < this.N && depth > 0) { | ||
| const ch = this.input[i] | ||
| if (ch === '(') { | ||
| depth++ | ||
| } else if (ch === ')') { | ||
| depth-- | ||
| if (depth === 0) return i | ||
| } else if (ch === '"' || ch === "'") { | ||
| const quote = ch | ||
| i++ | ||
| while (i < this.N && this.input[i] !== quote) { | ||
| if (this.input[i] === '\\') i++ | ||
| i++ | ||
| } | ||
| } | ||
| i++ | ||
| } | ||
| return -1 | ||
| } | ||
|
|
||
| private looksLikeRange (): boolean { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The parsing is no longer near O(N) complexity with |
||
| let i = this.p + 1 | ||
| let depth = 1 | ||
| while (i < this.N && depth > 0) { | ||
| const ch = this.input[i] | ||
| if (ch === '(') { | ||
| depth++ | ||
| } else if (ch === ')') { | ||
| depth-- | ||
| } else if (ch === '"' || ch === "'") { | ||
| i++ | ||
| while (i < this.N && this.input[i] !== ch) { | ||
| if (this.input[i] === '\\') i++ | ||
| i++ | ||
| } | ||
| } else if (depth === 1 && ch === '.' && this.input[i + 1] === '.') { | ||
| return true | ||
| } | ||
| i++ | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| * readFileNameTemplate (options: NormalizedFullOptions): IterableIterator<TopLevelToken> { | ||
| const { outputDelimiterLeft } = options | ||
| const htmlStopStrings = [',', ' ', '\r', '\n', '\t', outputDelimiterLeft] | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| import { QuotedToken, RangeToken, OperatorToken, Token, PropertyAccessToken, OperatorType, operatorTypes } from '../tokens' | ||
| import { isRangeToken, isPropertyAccessToken, UndefinedVariableError, range, isOperatorToken, assert } from '../util' | ||
| import { QuotedToken, RangeToken, OperatorToken, Token, PropertyAccessToken, OperatorType, operatorTypes, GroupedExpressionToken } from '../tokens' | ||
| import { isRangeToken, isPropertyAccessToken, isGroupedExpressionToken, UndefinedVariableError, range, isOperatorToken, assert } from '../util' | ||
| import type { Context } from '../context' | ||
| import type { UnaryOperatorHandler } from '../render' | ||
| import { Drop } from '../drop' | ||
|
|
@@ -40,6 +40,12 @@ export function * evalToken (token: Token | undefined, ctx: Context, lenient = f | |
| if ('content' in token) return token.content | ||
| if (isPropertyAccessToken(token)) return yield evalPropertyAccessToken(token, ctx, lenient) | ||
| if (isRangeToken(token)) return yield evalRangeToken(token, ctx) | ||
| if (isGroupedExpressionToken(token)) return yield evalGroupedExpressionToken(token, ctx, lenient) | ||
| } | ||
|
|
||
| function * evalGroupedExpressionToken (token: GroupedExpressionToken, ctx: Context, lenient: boolean): IterableIterator<unknown> { | ||
| assert(token.resolvedValue, 'grouped expression not resolved') | ||
| return yield token.resolvedValue!.value(ctx, lenient) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Try to create something like |
||
| } | ||
|
|
||
| function * evalPropertyAccessToken (token: PropertyAccessToken, ctx: Context, lenient: boolean): IterableIterator<unknown> { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ import { Hash, ValueToken, Liquid, Tag, evalToken, Emitter, TagToken, TopLevelTo | |
| import { assertEmpty, isValueToken, toEnumerable } from '../util' | ||
| import { ForloopDrop } from '../drop/forloop-drop' | ||
| import { Parser } from '../parser' | ||
| import { Arguments } from '../template' | ||
| import { Arguments, resolveGroupedExpressions } from '../template' | ||
|
|
||
| const MODIFIERS = ['offset', 'limit', 'reversed'] | ||
|
|
||
|
|
@@ -26,6 +26,7 @@ export default class extends Tag { | |
|
|
||
| this.variable = variable.content | ||
| this.collection = collection | ||
| resolveGroupedExpressions(this.collection, liquid) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this can be removed now. |
||
| this.hash = new Hash(this.tokenizer, liquid.options.keyValueSeparator) | ||
| this.templates = [] | ||
| this.elseTemplates = [] | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ import { Argument, Template, Value } from '.' | |
| import { isKeyValuePair } from '../parser/filter-arg' | ||
| import { PropertyAccessToken, ValueToken } from '../tokens' | ||
| import { | ||
| isGroupedExpressionToken, | ||
| isNumberToken, | ||
| isPropertyAccessToken, | ||
| isQuotedToken, | ||
|
|
@@ -371,6 +372,16 @@ function * extractValueTokenVariables (token: ValueToken): Generator<Variable> { | |
| if (isRangeToken(token)) { | ||
| yield * extractValueTokenVariables(token.lhs) | ||
| yield * extractValueTokenVariables(token.rhs) | ||
| } else if (isGroupedExpressionToken(token)) { | ||
| for (const t of token.initial.postfix) { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. create a |
||
| if (isValueToken(t)) yield * extractValueTokenVariables(t) | ||
| } | ||
| for (const filter of token.filters) { | ||
| for (const arg of filter.args) { | ||
| if (isKeyValuePair(arg) && arg[1]) yield * extractValueTokenVariables(arg[1]) | ||
| else if (isValueToken(arg)) yield * extractValueTokenVariables(arg) | ||
| } | ||
| } | ||
| } else if (isPropertyAccessToken(token)) { | ||
| yield extractPropertyAccessVariable(token) | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,29 @@ | ||
| import { Filter } from './filter' | ||
| import { Expression } from '../render' | ||
| import { Tokenizer } from '../parser' | ||
| import { assert } from '../util' | ||
| import type { FilteredValueToken } from '../tokens' | ||
| import { assert, isGroupedExpressionToken, isRangeToken, isPropertyAccessToken } from '../util' | ||
| import { FilteredValueToken, Token } from '../tokens' | ||
| import type { Liquid } from '../liquid' | ||
| import type { Context } from '../context' | ||
|
|
||
| export function resolveGroupedExpressions (token: Token, liquid: Liquid): void { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess this should be moved to |
||
| if (isGroupedExpressionToken(token)) { | ||
| const fvt = new FilteredValueToken( | ||
| token.initial, token.filters, | ||
| token.input, token.begin, token.end, token.file | ||
| ) | ||
| token.resolvedValue = new Value(fvt, liquid) | ||
| } | ||
| if (isRangeToken(token)) { | ||
| resolveGroupedExpressions(token.lhs, liquid) | ||
| resolveGroupedExpressions(token.rhs, liquid) | ||
| } | ||
| if (isPropertyAccessToken(token)) { | ||
| if (token.variable) resolveGroupedExpressions(token.variable, liquid) | ||
| for (const prop of token.props) resolveGroupedExpressions(prop, liquid) | ||
| } | ||
| } | ||
|
|
||
| export class Value { | ||
| public readonly filters: Filter[] = [] | ||
| public readonly initial: Expression | ||
|
|
@@ -15,10 +33,13 @@ export class Value { | |
| */ | ||
| public constructor (input: string | FilteredValueToken, liquid: Liquid) { | ||
| const token: FilteredValueToken = typeof input === 'string' | ||
| ? new Tokenizer(input, liquid.options.operators).readFilteredValue() | ||
| ? new Tokenizer(input, liquid.options.operators, undefined, undefined, liquid.options.groupedExpressions).readFilteredValue() | ||
| : input | ||
| this.initial = token.initial | ||
| this.filters = token.filters.map(token => new Filter(token, this.getFilter(liquid, token.name), liquid)) | ||
| for (const t of this.initial.postfix) { | ||
| resolveGroupedExpressions(t, liquid) | ||
| } | ||
| } | ||
|
|
||
| public * value (ctx: Context, lenient?: boolean): Generator<unknown, unknown, unknown> { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import { Token } from './token' | ||
| import { FilterToken } from './filter-token' | ||
| import { TokenKind } from '../parser' | ||
| import { Expression } from '../render' | ||
|
|
||
| export class GroupedExpressionToken extends Token { | ||
| public resolvedValue?: { value (ctx: any, lenient?: boolean): Generator<unknown, unknown, unknown> } | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. templates (include |
||
| constructor ( | ||
| public initial: Expression, | ||
| public filters: FilterToken[], | ||
| public input: string, | ||
| public begin: number, | ||
| public end: number, | ||
| public file?: string | ||
| ) { | ||
| super(TokenKind.GroupedExpression, input, begin, end, file) | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.