Skip to content

Commit

Permalink
Expressions: used raw sequence operators which bypass whitespace allo…
Browse files Browse the repository at this point in the history
…wance, added lots of tests to ensure that it works as intended
  • Loading branch information
GreyCat committed Mar 1, 2024
1 parent 733ed72 commit 2cf6676
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 54 deletions.
141 changes: 88 additions & 53 deletions jvm/src/test/scala/io/kaitai/struct/exprlang/ExpressionsSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -189,80 +189,91 @@ class ExpressionsSpec extends AnyFunSpec {
)
}

// Boolean literals
it("parses true") {
Expressions.parse("true") should be (Bool(true))
}
describe("boolean literals") {
it("parses true") {
Expressions.parse("true") should be(Bool(true))
}

it("parses false") {
Expressions.parse("false") should be (Bool(false))
}
it("parses false") {
Expressions.parse("false") should be(Bool(false))
}

it("parses truer") {
Expressions.parse("truer") should be (Name(identifier("truer")))
it("parses truer") {
Expressions.parse("truer") should be(Name(identifier("truer")))
}
}

// Boolean operations
it("parses not foo") {
Expressions.parse("not foo") should be (
UnaryOp(
Ast.unaryop.Not,
Name(identifier("foo"))
describe("boolean operations") {
it("parses not foo") {
Expressions.parse("not foo") should be(
UnaryOp(
Ast.unaryop.Not,
Name(identifier("foo"))
)
)
)
}
}

it("parses note_len") {
Expressions.parse("note_len") should be (Name(identifier("note_len")))
}
it("parses note_len") {
Expressions.parse("note_len") should be(Name(identifier("note_len")))
}

it("parses notnot") {
Expressions.parse("notnot") should be (Name(identifier("notnot")))
}
it("parses notnot") {
Expressions.parse("notnot") should be(Name(identifier("notnot")))
}

it("parses not not true") {
Expressions.parse("not not true") should be (
UnaryOp(
Ast.unaryop.Not,
it("parses not not true") {
Expressions.parse("not not true") should be(
UnaryOp(
Ast.unaryop.Not,
Bool(true)
UnaryOp(
Ast.unaryop.Not,
Bool(true)
)
)
)
)
}
}

// String literals
it("parses simple string") {
Expressions.parse("\"abc\"") should be (Str("abc"))
}
describe("strings literals") {
it("parses simple string") {
Expressions.parse("\"abc\"") should be(Str("abc"))
}

it("parses interpolated string with newline") {
Expressions.parse("\"abc\\ndef\"") should be (Str("abc\ndef"))
}
it("parses simple string with space at the start") {
Expressions.parse("\" abc\"") should be(Str(" abc"))
}

it("parses non-interpolated string with newline") {
Expressions.parse("'abc\\ndef'") should be (Str("abc\\ndef"))
}
it("parses simple string with space at the end") {
Expressions.parse("\"abc \"") should be(Str("abc "))
}

it("parses interpolated string with zero char") {
Expressions.parse("\"abc\\0def\"") should be (Str("abc\u0000def"))
}
it("parses interpolated string with newline") {
Expressions.parse("\"abc\\ndef\"") should be(Str("abc\ndef"))
}

it("parses non-interpolated string with zero char") {
Expressions.parse("'abc\\0def'") should be (Str("abc\\0def"))
}
it("parses non-interpolated string with newline") {
Expressions.parse("'abc\\ndef'") should be(Str("abc\\ndef"))
}

it("parses interpolated string with octal char") {
Expressions.parse("\"abc\\75def\"") should be (Str("abc=def"))
}
it("parses interpolated string with zero char") {
Expressions.parse("\"abc\\0def\"") should be(Str("abc\u0000def"))
}

it("parses interpolated string with hex unicode char") {
Expressions.parse("\"abc\\u21bbdef\"") should be (Str("abc\u21bbdef"))
}
it("parses non-interpolated string with zero char") {
Expressions.parse("'abc\\0def'") should be(Str("abc\\0def"))
}

it("parses interpolated string with octal char") {
Expressions.parse("\"abc\\75def\"") should be(Str("abc=def"))
}

it("parses double-quoted string with double quote") {
Expressions.parse("\"this \\\" is a quote\"") should be(Str("this \" is a quote"))
it("parses interpolated string with hex unicode char") {
Expressions.parse("\"abc\\u21bbdef\"") should be(Str("abc\u21bbdef"))
}

it("parses double-quoted string with double quote") {
Expressions.parse("\"this \\\" is a quote\"") should be(Str("this \" is a quote"))
}
}

// Casts
Expand Down Expand Up @@ -446,6 +457,30 @@ class ExpressionsSpec extends AnyFunSpec {
)))
}

it("parses f-string with space at the start") {
Expressions.parse("f\" foo\"") should be(InterpolatedStr(Seq(
Str(" foo")
)))
}

it("parses f-string with space at the end") {
Expressions.parse("f\"foo \"") should be(InterpolatedStr(Seq(
Str("foo ")
)))
}

it("parses f-string with double quote at the start") {
Expressions.parse("f\"\\\" is a quote\"") should be(InterpolatedStr(Seq(
Str("\" is a quote")
)))
}

it("parses f-string with space and double quote at the start") {
Expressions.parse("f\" \\\" is a quote\"") should be(InterpolatedStr(Seq(
Str(" \" is a quote")
)))
}

it("parses f-string with f-string in it") {
Expressions.parse("f\"abc{f\"def\"}ghi\"") should be(InterpolatedStr(Seq(
Str("abc"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ object Expressions {
def FLOAT_NUMBER[$: P] = Lexical.floatnumber
def STRING[$: P]: P[String] = Lexical.stringliteral

def fstring[$: P]: P[Ast.expr.InterpolatedStr] = P("f\"" ~/ fstringElement.rep ~ "\"").map(Ast.expr.InterpolatedStr)
def fstring[$: P]: P[Ast.expr.InterpolatedStr] = P("f\"" ~~/ fstringElement.repX ~~ "\"").map(Ast.expr.InterpolatedStr)
def fstringElement[$: P]: P[Ast.expr] = P(
formatExpr |
Lexical.fstringItem.repX(1).
Expand Down

0 comments on commit 2cf6676

Please sign in to comment.