From 5d49da69a6b1d4392f60f8822deaa92e31b51a00 Mon Sep 17 00:00:00 2001 From: Marvin Schlegel Date: Wed, 1 May 2024 22:41:06 +0200 Subject: [PATCH] lexer implement keywords --- Test/TestLexer.hs | 7 ++- src/Parser/Lexer.x | 104 ++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 108 insertions(+), 3 deletions(-) diff --git a/Test/TestLexer.hs b/Test/TestLexer.hs index 848cb4d..ddd55c9 100644 --- a/Test/TestLexer.hs +++ b/Test/TestLexer.hs @@ -15,6 +15,9 @@ testIdentifier = TestCase $ assertEqual "scan 'identifier'" [Identifier "identif testShortIdentifier = TestCase $ assertEqual "scan 'i'" [Identifier "i"] $ alexScanTokens "i" testIdentifierWithNumber = TestCase $ assertEqual "scan 'i2'" [Identifier "i2"] $ alexScanTokens "i2" +testKeywordBreak = TestCase $ assertEqual "scan 'break'" [BREAK] $ alexScanTokens "break" +testKeywordInt = TestCase $ assertEqual "scan 'int'" [INT] $ alexScanTokens "int" + tests = TestList [ TestLabel "TestCommentSomething" testCommentSomething, TestLabel "TestEmptyComment" testEmptyComment, @@ -22,5 +25,7 @@ tests = TestList [ TestLabel "TestLineCommentEnds" testLineCommentEnds, TestLabel "TestIdentifier" testIdentifier, TestLabel "TestShortIdentifier" testShortIdentifier, - TestLabel "TestIdentifierWithNumber" testIdentifierWithNumber + TestLabel "TestIdentifierWithNumber" testIdentifierWithNumber, + TestLabel "TestKeywordBreak" testKeywordBreak, + TestLabel "TestKeywordInt" testKeywordInt ] \ No newline at end of file diff --git a/src/Parser/Lexer.x b/src/Parser/Lexer.x index d500674..d961036 100644 --- a/src/Parser/Lexer.x +++ b/src/Parser/Lexer.x @@ -14,12 +14,112 @@ tokens :- $white ; "/*"(.|\n)*"*/" ; "//".* ; + -- keywords + "abstract" { \_ -> ABSTRACT } + "assert" { \_ -> BOOLEAN } + "boolean" { \_ -> BOOLEAN} + "break" { \_ -> BREAK} + "byte" { \_ -> BYTE} + "case" { \_ -> CASE} + "catch" { \_ -> CATCH} + "char" { \_ -> CHAR} + "class" { \_ -> CLASS} + "const" { \_ -> CONST} + "continue" { \_ -> CONTINUE} + "default" { \_ -> DEFAULT} + "do" { \_ -> DO} + "double" { \_ -> DOUBLE} + "else" { \_ -> ELSE} + "enum" { \_ -> ENUM} + "extends" { \_ -> EXTENDS} + "final" { \_ -> FINAL} + "finally" { \_ -> FINALLY} + "float" { \_ -> FLOAT} + "for" { \_ -> FOR} + "if" { \_ -> IF} + "goto" { \_ -> GOTO} + "implements" { \_ -> IMPLEMENTS} + "import" { \_ -> IMPORT} + "instanceof" { \_ -> INSTANCEOF} + "int" { \_ -> INT} + "long" { \_ -> LONG} + "native" { \_ -> NATIVE} + "new" { \_ -> NEW} + "package" { \_ -> PACKAGE} + "private" { \_ -> PRIVATE} + "protected" { \_ -> PROTECTED} + "public" { \_ -> PUBLIC} + "return" { \_ -> RETURN} + "short" { \_ -> SHORT} + "static" { \_ -> STATIC} + "strictfp" { \_ -> STRICTFP} + "super" { \_ -> SUPER} + "switch" { \_ -> SWITCH} + "synchronized" { \_ -> SYNCHRONIZED} + "this" { \_ -> THIS} + "throw" { \_ -> THROW} + "throws" { \_ -> THROWS} + "transient" { \_ -> TRANSIENT} + "try" { \_ -> TRY} + "void" { \_ -> VOID} + "volatile" { \_ -> VOLATILE} + "while" { \_ -> WHILE} + -- end keywords $JavaLetter$JavaLetterOrDigit* { \s -> Identifier s } - { data Token - = Identifier String + = ABSTRACT + | ASSERT + | BOOLEAN + | BREAK + | BYTE + | CASE + | CATCH + | CHAR + | CLASS + | CONST + | CONTINUE + | DEFAULT + | DO + | DOUBLE + | ELSE + | ENUM + | EXTENDS + | FINAL + | FINALLY + | FLOAT + | FOR + | IF + | GOTO + | IMPLEMENTS + | IMPORT + | INSTANCEOF + | INT + | INTERFACE + | LONG + | NATIVE + | NEW + | PACKAGE + | PRIVATE + | PROTECTED + | PUBLIC + | RETURN + | SHORT + | STATIC + | STRICTFP + | SUPER + | SWITCH + | SYNCHRONIZED + | THIS + | THROW + | THROWS + | TRANSIENT + | TRY + | VOID + | VOLATILE + | WHILE + | Identifier String deriving(Eq,Show) } \ No newline at end of file