From 2a519904c4ac408fabb39459104efcc3e09f3a40 Mon Sep 17 00:00:00 2001 From: Jon Duckworth Date: Thu, 16 May 2024 06:39:33 -0400 Subject: [PATCH] Fix parsing of booleanLiterals in cql2-text --- pygeofilter/parsers/cql2_text/grammar.lark | 2 +- pygeofilter/parsers/cql2_text/parser.py | 4 +-- tests/parsers/cql2_text/test_parser.py | 33 ++++++++++++++++++++++ 3 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 tests/parsers/cql2_text/test_parser.py diff --git a/pygeofilter/parsers/cql2_text/grammar.lark b/pygeofilter/parsers/cql2_text/grammar.lark index 0bdf722..d0ac120 100644 --- a/pygeofilter/parsers/cql2_text/grammar.lark +++ b/pygeofilter/parsers/cql2_text/grammar.lark @@ -126,7 +126,7 @@ func.2: attribute "(" expression ("," expression)* ")" -> function envelope: "ENVELOPE"i "(" number number number number ")" -BOOLEAN: ( "TRUE" | "FALSE" ) +BOOLEAN.2: ( "TRUE"i | "FALSE"i) DOUBLE_QUOTED: "\"" /.*?/ "\"" SINGLE_QUOTED: "'" /.*?/ "'" diff --git a/pygeofilter/parsers/cql2_text/parser.py b/pygeofilter/parsers/cql2_text/parser.py index b71ea96..b61a6e5 100644 --- a/pygeofilter/parsers/cql2_text/parser.py +++ b/pygeofilter/parsers/cql2_text/parser.py @@ -178,8 +178,8 @@ def INT(self, value): def FLOAT(self, value): return float(value) - def boolean(self, value): - return value in ("TRUE", "true", "T", "t", "1") + def BOOLEAN(self, value): + return value.lower() == "true" def DOUBLE_QUOTED(self, token): return token[1:-1] diff --git a/tests/parsers/cql2_text/test_parser.py b/tests/parsers/cql2_text/test_parser.py new file mode 100644 index 0000000..966fbf8 --- /dev/null +++ b/tests/parsers/cql2_text/test_parser.py @@ -0,0 +1,33 @@ +from pygeofilter import ast +from pygeofilter.parsers.cql2_text import parse + + +def test_attribute_eq_true_uppercase(): + result = parse("attr = TRUE") + assert result == ast.Equal( + ast.Attribute("attr"), + True, + ) + +def test_attribute_eq_true_lowercase(): + result = parse("attr = true") + assert result == ast.Equal( + ast.Attribute("attr"), + True, + ) + + +def test_attribute_eq_false_uppercase(): + result = parse("attr = FALSE") + assert result == ast.Equal( + ast.Attribute("attr"), + False, + ) + + +def test_attribute_eq_false_lowercase(): + result = parse("attr = false") + assert result == ast.Equal( + ast.Attribute("attr"), + False, + )