add some keywords

This commit is contained in:
rhiannon morris 2022-05-04 15:30:52 +02:00
parent 96e81584b6
commit 4973ce15bd
2 changed files with 18 additions and 1 deletions

View file

@ -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,

View file

@ -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],