diff --git a/src/Quox/Lexer.idr b/src/Quox/Lexer.idr index 956840d..6c6dfb8 100644 --- a/src/Quox/Lexer.idr +++ b/src/Quox/Lexer.idr @@ -71,7 +71,10 @@ skip : Lexer -> Tokenizer (Maybe a) skip lex = match lex $ const Nothing simple : List String -> a -> Tokenizer (Maybe a) -simple str = match (choice $ map exact str) . const . Just +simple strs = match (choice $ map exact strs) . const . Just + +keyword : String -> Keyword -> Tokenizer (Maybe Token) +keyword str = match (exact str <+> reject nameCont) . const . Just . K choice : (xs : List (Tokenizer a)) -> {auto 0 _ : NonEmpty xs} -> Tokenizer a choice (t :: ts) = foldl (\a, b => a <|> b) t ts @@ -100,6 +103,10 @@ tokens = choice [ simple ["<<", "⊲"] $ P Triangle, match wild $ const $ P Wild, + keyword "fun" Fun, keyword "λ" Fun, + keyword "let" Let, keyword "in" In, + keyword "case" Case, keyword "of" Of, + match name $ Name, match symbol $ Symbol . assert_total strTail, diff --git a/tests/src/Tests/Lexer.idr b/tests/src/Tests/Lexer.idr index 7e05d8c..3e6551c 100644 --- a/tests/src/Tests/Lexer.idr +++ b/tests/src/Tests/Lexer.idr @@ -109,6 +109,16 @@ tests = "lexer" :- [ rejects' "1abc" ], + "keywords" :- [ + acceptsWith' "fun" [K Fun], + acceptsWith' "λ" [K Fun], + acceptsWith' "let" [K Let], + acceptsWith' "in" [K In], + acceptsWith' "case" [K Case], + acceptsWith' "of" [K Of], + acceptsWith' "funk" [Name "funk"] + ], + "numbers" :- [ acceptsWith' "0" [N Zero], acceptsWith' "1" [N One],