[Lexer] Prevent the regex from being greedy
This commit is contained in:
@@ -10,7 +10,7 @@ import Foundation
|
|||||||
|
|
||||||
public struct Lexer {
|
public struct Lexer {
|
||||||
public let templateString:String
|
public let templateString:String
|
||||||
let regex = NSRegularExpression(pattern: "(\\{\\{.*\\}\\}|\\{%.*%\\}|\\{#.*#\\})", options: nil, error: nil)
|
let regex = NSRegularExpression(pattern: "(\\{\\{.*?\\}\\}|\\{%.*?%\\}|\\{#.*?#\\})", options: nil, error: nil)
|
||||||
|
|
||||||
public init(templateString:String) {
|
public init(templateString:String) {
|
||||||
self.templateString = templateString
|
self.templateString = templateString
|
||||||
|
|||||||
@@ -46,4 +46,13 @@ class LexerTests: XCTestCase {
|
|||||||
XCTAssertEqual(tokens[2], Token.Text(value: "."))
|
XCTAssertEqual(tokens[2], Token.Text(value: "."))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func testTokenizeTwoVariables() { // Don't be greedy
|
||||||
|
let lexer = Lexer(templateString:"{{ thing }}{{ name }}")
|
||||||
|
let tokens = lexer.tokenize()
|
||||||
|
|
||||||
|
XCTAssertEqual(tokens.count, 2)
|
||||||
|
XCTAssertEqual(tokens[0], Token.Variable(value: "thing"))
|
||||||
|
XCTAssertEqual(tokens[1], Token.Variable(value: "name"))
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user