feat: throw an Error if delimiter not matched

This commit is contained in:
harttle
2019-02-20 22:40:09 +08:00
parent c13a16fdd6
commit c33d8f6828
5 changed files with 62 additions and 28 deletions
+3 -1
View File
@@ -1,11 +1,13 @@
export default class Token {
type: string
line: number
col: number
raw: string
input: string
file: string
value: string
constructor (raw, pos, input, file, line) {
constructor (raw, col, input, file, line) {
this.col = col
this.line = line
this.raw = raw
this.input = input
+28 -10
View File
@@ -1,7 +1,9 @@
import whiteSpaceCtrl from './whitespace-ctrl'
import HTMLToken from './html-token'
import TagToken from './tag-token'
import Token from './token'
import OutputToken from './output-token'
import { TokenizationError } from 'src/util/error'
import { LiquidOptions, defaultOptions } from 'src/liquid-options'
enum ParseState { HTML, OUTPUT, TAG }
@@ -14,43 +16,59 @@ export default class Tokenizer {
tokenize (input: string, file?: string) {
const tokens = []
let p = 0
let line = 1
let curLine = 1
let state = ParseState.HTML
let buffer = ''
let bufferBegin = 0
let lineBegin = 0
let line = 1
let col = 1
while (p < input.length) {
if (input[p] === '\n') line++
if (input[p] === '\n') {
curLine++
lineBegin = p + 1
}
const bin = input.substr(p, 2)
if (state === ParseState.HTML) {
if (bin === '{{' || bin === '{%') {
if (buffer) tokens.push(new HTMLToken(buffer, bufferBegin, input, file, line))
if (buffer) tokens.push(new HTMLToken(buffer, col, input, file, line))
buffer = bin
bufferBegin = p
line = curLine
col = p - lineBegin + 1
p += 2
state = bin === '{{' ? ParseState.OUTPUT : ParseState.TAG
continue
}
} else if (state === ParseState.OUTPUT && bin === '}}') {
buffer += '}}'
tokens.push(new OutputToken(buffer, bufferBegin, input, file, line))
tokens.push(new OutputToken(buffer, col, input, file, line))
p += 2
buffer = ''
bufferBegin = p
line = curLine
col = p - lineBegin + 1
state = ParseState.HTML
continue
} else if (bin === '%}') {
buffer += '%}'
tokens.push(new TagToken(buffer, bufferBegin, input, file, line))
tokens.push(new TagToken(buffer, col, input, file, line))
p += 2
buffer = ''
bufferBegin = p
line = curLine
col = p - lineBegin + 1
state = ParseState.HTML
continue
}
buffer += input[p++]
}
if (buffer) tokens.push(new HTMLToken(buffer, bufferBegin, input, file, line))
if (state !== ParseState.HTML) {
const t = state === ParseState.OUTPUT ? 'output' : 'tag'
const str = buffer.length > 16 ? buffer.slice(0, 13) + '...' : buffer
throw new TokenizationError(
new Error(`${t} "${str}" not closed`),
new Token(buffer, col, input, file, line)
)
}
if (buffer) tokens.push(new HTMLToken(buffer, col, input, file, line))
whiteSpaceCtrl(tokens, this.options)
return tokens
+2 -4
View File
@@ -11,14 +11,12 @@ abstract class LiquidError {
name: string
message: string
stack: string
private line: string
private file: string
private input: string
private token: Token
private originalError: Error
constructor (err, token) {
this.input = token.input
this.line = token.line
this.file = token.file
this.originalError = err
this.token = token
@@ -28,7 +26,7 @@ abstract class LiquidError {
captureStack.call(obj)
const err = this.originalError
const context = mkContext(this.input, this.line)
const context = mkContext(this.input, this.token.line)
this.message = mkMessage(err.message, this.token)
this.stack = this.message + '\n' + context +
'\n' + (this.stack || this.message) +
@@ -110,7 +108,7 @@ function mkMessage (msg, token) {
msg += ', file:' + token.file
}
if (token.line) {
msg += ', line:' + token.line
msg += `, line:${token.line}, col:${token.col}`
}
return msg
}