From 01afae9b79017ebc0f6002a3070f654566b3228b Mon Sep 17 00:00:00 2001 From: Ilya Puchka Date: Sun, 30 Sep 2018 21:57:19 +0100 Subject: [PATCH] Fix parsing token components with parenthesis without spaces (#254) * fix parsing token components with brackets without spaces * handle more edge cases * do not use force unwrap * use first/last instead of hasPrefix/hasSuffix * update CHANGELOG --- CHANGELOG.md | 4 +++- Sources/Tokenizer.swift | 6 ++++++ Tests/StencilTests/IfNodeSpec.swift | 2 +- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9eb5a89..647f89b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,7 +16,9 @@ _None_ ### Bug Fixes -_None_ +- Fixed using parenthesis in boolean expressions, they now can be used without spaces around them. + [Ilya Puchka](https://github.com/ilyapuchka) + [#254](https://github.com/stencilproject/Stencil/pull/254) ### Internal Changes diff --git a/Sources/Tokenizer.swift b/Sources/Tokenizer.swift index cb5d483..d86852f 100644 --- a/Sources/Tokenizer.swift +++ b/Sources/Tokenizer.swift @@ -17,6 +17,12 @@ extension String { components[components.count-1] += word } else if specialCharacters.contains(word) { components[components.count-1] += word + } else if word != "(" && word.first == "(" || word != ")" && word.first == ")" { + components.append(String(word.prefix(1))) + appendWord(String(word.dropFirst())) + } else if word != "(" && word.last == "(" || word != ")" && word.last == ")" { + appendWord(String(word.dropLast())) + components.append(String(word.suffix(1))) } else { components.append(word) } diff --git a/Tests/StencilTests/IfNodeSpec.swift b/Tests/StencilTests/IfNodeSpec.swift index c9d6ae7..1141b26 100644 --- a/Tests/StencilTests/IfNodeSpec.swift +++ b/Tests/StencilTests/IfNodeSpec.swift @@ -145,7 +145,7 @@ class IfNodeTests: XCTestCase { $0.it("can parse an if with complex expression") { let tokens: [Token] = [ - .block(value: "if value == \"test\" and not name", at: .unknown), + .block(value: "if value == \"test\" and (not name or not (name and surname) or( some )and other )", at: .unknown), .text(value: "true", at: .unknown), .block(value: "endif", at: .unknown) ]