From 7509768e8d1acd7162301d8fe1ace6f7658f3985 Mon Sep 17 00:00:00 2001 From: Marc Scholten Date: Thu, 13 Jan 2022 08:13:31 +0100 Subject: [PATCH] Support signed integers and floats in the Schema SQL parser. Fixes #1309 --- IHP/IDE/SchemaDesigner/Parser.hs | 4 ++-- Test/IDE/SchemaDesigner/ParserSpec.hs | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/IHP/IDE/SchemaDesigner/Parser.hs b/IHP/IDE/SchemaDesigner/Parser.hs index 005db6d51..170c2b9dd 100644 --- a/IHP/IDE/SchemaDesigner/Parser.hs +++ b/IHP/IDE/SchemaDesigner/Parser.hs @@ -421,10 +421,10 @@ varExpr :: Parser Expression varExpr = VarExpression <$> identifier doubleExpr :: Parser Expression -doubleExpr = DoubleExpression <$> Lexer.float +doubleExpr = DoubleExpression <$> (Lexer.signed spaceConsumer Lexer.float) intExpr :: Parser Expression -intExpr = IntExpression <$> Lexer.decimal +intExpr = IntExpression <$> (Lexer.signed spaceConsumer Lexer.decimal) callExpr :: Parser Expression callExpr = do diff --git a/Test/IDE/SchemaDesigner/ParserSpec.hs b/Test/IDE/SchemaDesigner/ParserSpec.hs index 97604f450..7a7559fda 100644 --- a/Test/IDE/SchemaDesigner/ParserSpec.hs +++ b/Test/IDE/SchemaDesigner/ParserSpec.hs @@ -751,6 +751,18 @@ COMMENT ON EXTENSION "uuid-ossp" IS 'generate universally unique identifiers (UU let sql = cs [plain|ALTER SEQUENCE public.a OWNED BY public.b.serial_number;|] parseSql sql `shouldBe` UnknownStatement { raw = "ALTER SEQUENCE public.a OWNED BY public.b.serial_number" } + it "should parse positive IntExpression's" do + parseExpression "1" `shouldBe` (IntExpression 1) + + it "should parse negative IntExpression's" do + parseExpression "-1" `shouldBe` (IntExpression (-1)) + + it "should parse positive DoubleExpression's" do + parseExpression "1.337" `shouldBe` (DoubleExpression 1.337) + + it "should parse negative DoubleExpression's" do + parseExpression "-1.337" `shouldBe` (DoubleExpression (-1.337)) + col :: Column col = Column { name = "" @@ -768,3 +780,9 @@ parseSqlStatements sql = case Megaparsec.runParser Parser.parseDDL "input" sql of Left parserError -> error (cs $ Megaparsec.errorBundlePretty parserError) -- For better error reporting in hspec Right statements -> statements + +parseExpression :: Text -> Expression +parseExpression sql = + case Megaparsec.runParser Parser.expression "input" sql of + Left parserError -> error (cs $ Megaparsec.errorBundlePretty parserError) -- For better error reporting in hspec + Right expression -> expression