on master help sign in
Text.Token: utilities for token value conversion
bc8da8dMichael Morgan
libs/contrib/Text/Token.idrnew file+38
@@ -0,0 +1,38 @@
1+module Text.Token
2+
3+%access public export
4+%default total
5+
6+||| For a type `kind`, specify a way of converting the recognised
7+||| string into a value.
8+|||
9+||| ```idris example
10+||| data SimpleKind = SKString | SKInt | SKComma
11+|||
12+||| TokenKind SimpleKind where
13+||| tokType SKString = String
14+||| tokType SKInt = Int
15+||| tokType SKComma = ()
16+|||
17+||| tokValue SKString x = x
18+||| tokValue SKInt x = cast x
19+||| tokValue SKComma x = ()
20+||| ```
21+interface TokenKind (k : Type) where
22+ ||| The type that a token of this kind converts to.
23+ tokType : k -> Type
24+
25+ ||| Convert a recognised string into a value. The type is determined
26+ ||| by the kind of token.
27+ tokValue : (kind : k) -> String -> tokType kind
28+
29+||| A token of a particular kind and the text that was recognised.
30+record Token k where
31+ constructor Tok
32+ kind : k
33+ text : String
34+
35+||| Get the value of a `Token k`. The resulting type depends upon
36+||| the kind of token.
37+value : TokenKind k => (t : Token k) -> tokType (kind t)
38+value (Tok k x) = tokValue k x
libs/contrib/contrib.ipkg+1
@@ -84,3 +84,4 @@ modules = CFFI
84 , Text.PrettyPrint.WL
84 , Text.PrettyPrint.WL
85
85
86 , Text.Quantity
86 , Text.Quantity
87+ , Text.Token