From b8e566a2a0d5ea4ab02cf71924f6d023e551add6 Mon Sep 17 00:00:00 2001 From: Marvin Schlegel Date: Wed, 1 May 2024 22:16:12 +0200 Subject: [PATCH] lexer implement identifiers --- Test/TestLexer.hs | 9 +++++++-- src/Parser/Lexer.x | 6 ++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/Test/TestLexer.hs b/Test/TestLexer.hs index 94046da..848cb4d 100644 --- a/Test/TestLexer.hs +++ b/Test/TestLexer.hs @@ -11,11 +11,16 @@ testEmptyComment = TestCase $ assertEqual "scan '/*x*/'" emptyTokenList $ alex testLineComment = TestCase $ assertEqual "scan '// comment'" emptyTokenList $ alexScanTokens "// comment" testLineCommentEnds = TestCase $ assertEqual "scan '// com\\n'" emptyTokenList $ alexScanTokens "// com\n" - +testIdentifier = TestCase $ assertEqual "scan 'identifier'" [Identifier "identifier"] $ alexScanTokens "identifier" +testShortIdentifier = TestCase $ assertEqual "scan 'i'" [Identifier "i"] $ alexScanTokens "i" +testIdentifierWithNumber = TestCase $ assertEqual "scan 'i2'" [Identifier "i2"] $ alexScanTokens "i2" tests = TestList [ TestLabel "TestCommentSomething" testCommentSomething, TestLabel "TestEmptyComment" testEmptyComment, TestLabel "TestLineComment" testLineComment, - TestLabel "TestLineCommentEnds" testLineCommentEnds + TestLabel "TestLineCommentEnds" testLineCommentEnds, + TestLabel "TestIdentifier" testIdentifier, + TestLabel "TestShortIdentifier" testShortIdentifier, + TestLabel "TestIdentifierWithNumber" testIdentifierWithNumber ] \ No newline at end of file diff --git a/src/Parser/Lexer.x b/src/Parser/Lexer.x index d5a472f..d500674 100644 --- a/src/Parser/Lexer.x +++ b/src/Parser/Lexer.x @@ -7,17 +7,19 @@ $digit = 0-9 $alpha = [a-zA-Z] $alphanum = [a-zA-Z0-9] +$JavaLetter = [A-Za-z\_\$] +$JavaLetterOrDigit = [A-Za-z\_\$0-9] tokens :- $white ; "/*"(.|\n)*"*/" ; "//".* ; - + $JavaLetter$JavaLetterOrDigit* { \s -> Identifier s } { data Token - = Different + = Identifier String deriving(Eq,Show) } \ No newline at end of file