feat: create trie programmatically in options

This commit is contained in:
Jason Etcovitch
2021-02-04 09:11:28 +08:00
committed by Jun Yang
parent 8734e2e6ce
commit befc33c4eb
28 changed files with 196 additions and 146 deletions
+2 -11
View File
@@ -1,16 +1,7 @@
import { IDENTIFIER } from '../util/character'
import { Trie } from '../util/operator-trie'
const trie = {
a: { n: { d: { end: true, needBoundary: true } } },
o: { r: { end: true, needBoundary: true } },
c: { o: { n: { t: { a: { i: { n: { s: { end: true, needBoundary: true } } } } } } } },
'=': { '=': { end: true } },
'!': { '=': { end: true } },
'>': { end: true, '=': { end: true } },
'<': { end: true, '=': { end: true } }
}
export function matchOperator (str: string, begin: number, end = str.length) {
export function matchOperator (str: string, begin: number, trie: Trie, end = str.length) {
let node = trie
let i = begin
let info
+3 -1
View File
@@ -22,6 +22,7 @@ import { TokenizationError } from '../util/error'
import { NormalizedFullOptions, defaultOptions } from '../liquid-options'
import { TYPES, QUOTE, BLANK, IDENTIFIER } from '../util/character'
import { matchOperator } from './match-operator'
import { Trie } from '../util/operator-trie'
export class Tokenizer {
p = 0
@@ -30,6 +31,7 @@ export class Tokenizer {
constructor (
private input: string,
private trie: Trie,
private file: string = ''
) {
this.N = input.length
@@ -54,7 +56,7 @@ export class Tokenizer {
}
readOperator (): OperatorToken | undefined {
this.skipBlank()
const end = matchOperator(this.input, this.p, this.p + 8)
const end = matchOperator(this.input, this.p, this.trie, this.p + 8)
if (end === -1) return
return new OperatorToken(this.input, this.p, (this.p = end), this.file)
}