-
-
Notifications
You must be signed in to change notification settings - Fork 279
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 all commits
e7e3aeb
75314d0
489d136
a84df57
4b5296a
ba50a32
0bedce9
d6ec7cd
f45c4f1
bd65ca6
2cc74bf
e1afb68
60a6d0a
c4b4b6a
7f2b3ff
74e73b7
4b620a5
2d5b811
e202158
1b9fb80
4ff6534
db271da
5e60327
2c5f36d
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 |
|---|---|---|
|
|
@@ -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,19 +18,25 @@ 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 () { | ||
| return new Expression(this.readExpressionTokens()) | ||
| } | ||
|
|
||
| * readExpressionTokens (): IterableIterator<Token> { | ||
| yield * this.readExpressionTokensFromHere() | ||
| } | ||
|
|
||
| * readExpressionTokensFromHere (): IterableIterator<Token> { | ||
| while (this.p < this.N) { | ||
| const operator = this.readOperator() | ||
| if (operator) { | ||
|
|
@@ -44,6 +51,11 @@ export class Tokenizer { | |
| return | ||
| } | ||
| } | ||
|
|
||
| * readGroupedExpressionTokens (lhs: Token): IterableIterator<Token> { | ||
| yield lhs | ||
| yield * this.readExpressionTokensFromHere() | ||
| } | ||
| readOperator (): OperatorToken | undefined { | ||
| this.skipBlank() | ||
| const end = this.matchTrie(this.opTrie) | ||
|
|
@@ -80,6 +92,7 @@ export class Tokenizer { | |
| readFilter (): FilterToken | null { | ||
| this.skipBlank() | ||
| if (this.end()) return null | ||
| if (this.peek() === ')') return null | ||
| this.assert(this.read() === '|', `expected "|" before filter`) | ||
| const name = this.readIdentifier() | ||
| if (!name.size()) { | ||
|
|
@@ -94,9 +107,9 @@ export class Tokenizer { | |
| const arg = this.readFilterArg() | ||
| arg && args.push(arg) | ||
| this.skipBlank() | ||
| this.assert(this.end() || this.peek() === ',' || this.peek() === '|', () => `unexpected character ${this.snapshot()}`) | ||
| this.assert(this.end() || this.peek() === ',' || this.peek() === '|' || this.peek() === ')', () => `unexpected character ${this.snapshot()}`) | ||
| } while (this.peek() === ',') | ||
| } else if (this.peek() === '|' || this.end()) { | ||
| } else if (this.peek() === '|' || this.peek() === ')' || this.end()) { | ||
| // do nothing | ||
| } else { | ||
| throw this.error('expected ":" after filter name') | ||
|
|
@@ -307,10 +320,14 @@ export class Tokenizer { | |
| return -1 | ||
| } | ||
|
|
||
| readValue (): ValueToken | undefined { | ||
| readValue (): ValueToken | FilteredValueToken | undefined { | ||
| this.skipBlank() | ||
| const begin = this.p | ||
| const variable = this.readLiteral() || this.readQuoted() || this.readRange() || this.readNumber() | ||
| let variable: ValueToken | FilteredValueToken | undefined = this.readLiteral() || this.readQuoted() | ||
| if (!variable && this.peek() === '(') { | ||
| variable = this.readGroupOrRange() | ||
| } | ||
| variable = variable || this.readNumber() | ||
|
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. why
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. Had a quick look,it also returns |
||
| const props = this.readProperties(!variable) | ||
| if (!props.length) return variable | ||
| return new PropertyAccessToken(variable, props, this.input, begin, this.p) | ||
|
|
@@ -385,18 +402,32 @@ export class Tokenizer { | |
| return literal | ||
| } | ||
|
|
||
| readRange (): RangeToken | undefined { | ||
| readGroupOrRange (): FilteredValueToken | RangeToken | undefined { | ||
| this.skipBlank() | ||
| const begin = this.p | ||
| if (this.peek() !== '(') return | ||
| ++this.p | ||
| const lhs = this.readValueOrThrow() | ||
| this.skipBlank() | ||
| this.assert(this.read() === '.' && this.read() === '.', 'invalid range syntax') | ||
| const rhs = this.readValueOrThrow() | ||
| this.skipBlank() | ||
| this.assert(this.read() === ')', 'invalid range syntax') | ||
| return new RangeToken(this.input, begin, this.p, lhs, rhs, this.file) | ||
|
|
||
| if (this.peek() === '.' && this.peek(1) === '.') { | ||
| this.p += 2 | ||
| const rhs = this.readValueOrThrow() | ||
| this.skipBlank() | ||
| this.assert(this.read() === ')', 'invalid range syntax') | ||
| return new RangeToken(this.input, begin, this.p, lhs, rhs, this.file) | ||
| } | ||
|
|
||
| if (this.groupedExpressions) { | ||
| const initial = new Expression(this.readGroupedExpressionTokens(lhs)) | ||
|
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 see, we need reuse the lhs already parsed here. Let's see if changing const initial = new Expression([lhs, ...this.readExpressionTokens()])If we can't change that type, we can try instead: const initial = new Expression([lhs, ...this.readExpressionTokens()].values()) |
||
| this.assert(initial.valid(), () => `invalid value expression: ${this.snapshot()}`) | ||
| const filters = this.readFilters() | ||
| this.skipBlank() | ||
| this.assert(this.read() === ')', 'unbalanced parentheses') | ||
| return new FilteredValueToken(initial, filters, this.input, begin, this.p, this.file) | ||
| } | ||
|
|
||
| throw this.error('invalid range syntax') | ||
| } | ||
|
|
||
| readValueOrThrow (): ValueToken { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand why group expression tokens consists of a
lhsand a list of other expression tokens.The name
readExpressionTokensFromHereis also wield.