From 4108eb58c1e7dd1ca04caf17b351c43f3bd6844f Mon Sep 17 00:00:00 2001 From: Marvin Schlegel Date: Thu, 2 May 2024 17:45:04 +0200 Subject: [PATCH] lexer implement separators and fix literals --- Test/TestLexer.hs | 5 ++++- src/Parser/Lexer.x | 27 +++++++++++++++++++++------ 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/Test/TestLexer.hs b/Test/TestLexer.hs index 8c1cd91..b07b38a 100644 --- a/Test/TestLexer.hs +++ b/Test/TestLexer.hs @@ -26,6 +26,8 @@ testCharLiteral = TestCase $ assertEqual "scan ''f''" [CHARLITERAL 'f'] $ alexSc testBoolLiteralTrue = TestCase $ assertEqual "scan 'true'" [BOOLLITERAL True] $ alexScanTokens "true" testBoolLiteralFalse = TestCase $ assertEqual "scan 'false'" [BOOLLITERAL False] $ alexScanTokens "false" +testLBrace = TestCase $ assertEqual "scan '('" [LBRACE] $ alexScanTokens "(" + tests = TestList [ TestLabel "TestCommentSomething" testCommentSomething, TestLabel "TestEmptyComment" testEmptyComment, @@ -40,5 +42,6 @@ tests = TestList [ TestLabel "TestIntLiteral2" testIntLiteral2, TestLabel "TestCharLiteral" testCharLiteral, TestLabel "TestBoolLiteralTrue" testBoolLiteralTrue, - TestLabel "TestBoolLiteralFalse" testBoolLiteralFalse + TestLabel "TestBoolLiteralFalse" testBoolLiteralFalse, + TestLabel "TestLBrace" testLBrace ] \ No newline at end of file diff --git a/src/Parser/Lexer.x b/src/Parser/Lexer.x index 54694cc..13b944a 100644 --- a/src/Parser/Lexer.x +++ b/src/Parser/Lexer.x @@ -1,6 +1,6 @@ { module Parser.Lexer(Token(..), alexScanTokens) where -import Data.Char +import Text.Read } %wrapper "basic" @@ -65,14 +65,23 @@ tokens :- "void" { \_ -> VOID} "volatile" { \_ -> VOLATILE} "while" { \_ -> WHILE} - -- end keywords - $JavaLetter$JavaLetterOrDigit* { \s -> IDENTIFIER s } -- Literals - [1-9]([0-9\_]*[0-9])* { \s -> INTEGERLITERAL $ read $ filter ((/=) '_') s } - "'"."'" { \s -> CHARLITERAL $ read $ filter ((/=) '\'') s } "true" { \_ -> BOOLLITERAL True } "false" { \_ -> BOOLLITERAL False } "null" { \_ -> NULLLITERAL } + -- end keywords + $JavaLetter$JavaLetterOrDigit* { \s -> IDENTIFIER s } + -- Literals + [1-9]([0-9\_]*[0-9])* { \s -> case readMaybe $ filter ((/=) '_') s of Just a -> INTEGERLITERAL a; Nothing -> error ("failed to parse INTLITERAL " ++ s) } + "'"."'" { \s -> case (s) of _ : c : _ -> CHARLITERAL c; _ -> error ("failed to parse CHARLITERAL " ++ s) } + -- separators + "(" { \_ -> LBRACE } + ")" { \_ -> RBRACE } + "{" { \_ -> LBRACKET } + "}" { \_ -> RBRACKET } + ";" { \_ -> SEMICOLON } + "," { \_ -> COMMA} + "." { \_ -> DOT } { data Token @@ -131,7 +140,13 @@ data Token | CHARLITERAL Char | BOOLLITERAL Bool | NULLLITERAL - + | LBRACE + | RBRACE + | LBRACKET + | RBRACKET + | SEMICOLON + | COMMA + | DOT deriving(Eq,Show) } \ No newline at end of file