Created
November 2, 2017 21:51
-
-
Save msmorgan/f8541aaec4bb9313b5c6dc4d09a3ecf9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module Text.Lexer.Bikeshedding | |
| {- | |
| Other choices include: | |
| -:, $: | |
| !=>, $=> | |
| :>, :$> | |
| #^, *! -- not really | |
| the possibilities are endless | |
| -} | |
| infix 0 !:, $: | |
| {- | |
| I'm unsatisfied with this name, because this would also be useful for identifier tokens. | |
| -} | |
| ||| A `Literal` represents a `Lexer` and a way to produce a value of | |
| ||| type `a` based on the text it recognises. | |
| public export | |
| Literal : (a : Type) -> Type | |
| Literal a = (Lexer, String -> a) | |
| ||| Make a `TokenMap tok` entry for a `Lexer` that always results in the | |
| ||| same token value. | |
| ||| | |
| ||| ```idris example | |
| ||| data MyToken = MTComma | MTColon | |
| ||| | |
| ||| myMap : TokenMap MyToken | |
| ||| myMap = [ is ',' !: MTComma | |
| ||| , is ':' !: MTColon | |
| ||| ] | |
| ||| ``` | |
| (!:) : Lexer -> tok -> (Lexer, String -> tok) | |
| (!:) l t = (l, const t) | |
| ||| Create a `TokenMap tok` entry from a `Literal a` and a function | |
| ||| that can create a `tok` from an `a`. | |
| ||| | |
| ||| ```idris example | |
| ||| data MyToken = MTInt Int | MTBool Bool | |
| ||| | |
| ||| bool : Literal Bool | |
| ||| bool = (exact "True" <|> exact "False", (== "True")) | |
| ||| | |
| ||| myMap : TokenMap MyToken | |
| ||| myMap = [ (intLit, cast) $: MTInt | |
| ||| , bool $: MTBool | |
| ||| ] | |
| ||| ``` | |
| ($:) : Literal a -> (a -> tok) -> (Lexer, String -> tok) | |
| ($:) (l, mkVal) mkTok = (l, mkTok . mkVal) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hm, maybe it wouldn't be all that useful for identifier tokens.
Or potentially: