Skip to content
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

Fix parsing token components with parenthesis without spaces #254

Merged
merged 6 commits into from
Sep 30, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Sources/Tokenizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ extension String {
components[components.count-1] += word
} else if specialCharacters.contains(word) {
components[components.count-1] += word
} else if word != "(" && word.hasPrefix("(") {
components.append("(")
} else if word != "(" && word.hasPrefix("(") || word != ")" && word.hasPrefix(")") {
components.append(String(word.first!))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please avoid force unwraps, especially in Sources.

You could:

  • use word[0]
  • unroll the if test into a separate one
  • use prefix(1)
  • ...

appendWord(String(word.dropFirst()))
} else if word != ")" && word.hasSuffix(")") {
} else if word != "(" && word.hasSuffix("(") || word != ")" && word.hasSuffix(")") {
appendWord(String(word.dropLast()))
components.append(")")
components.append(String(word.last!))
} else {
components.append(word)
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/StencilTests/IfNodeSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 or not (name and surname))", 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)
]
Expand Down