0 | module Parser.Lexer.Package
2 | import public Parser.Lexer.Common
3 | import public Libraries.Text.Lexer
4 | import public Libraries.Text.Parser
5 | import public Libraries.Text.Bounded
6 | import Libraries.Text.PrettyPrint.Prettyprinter
9 | import Libraries.Data.String.Extra
10 | import Libraries.Utils.String
12 | import Core.Name.Namespace
21 | | DotSepIdent (Maybe Namespace) String
32 | | IntegerLit Integer
36 | show (Comment str) = "Comment: " ++ str
37 | show EndOfInput = "EndOfInput"
38 | show Equals = "Equals"
39 | show (DotSepIdent ns n) = "DotSepIdentifier: " ++ show ns ++ "." ++ show n
40 | show Separator = "Separator"
47 | show AndOp = "AndOp"
48 | show Space = "Space"
49 | show (StringLit s) = "StringLit: " ++ s
50 | show (IntegerLit i) = "IntegerLit: " ++ show i
53 | Pretty Void Token where
54 | pretty (Comment str) = "Comment:" <++> pretty str
55 | pretty EndOfInput = "EndOfInput"
56 | pretty Equals = "Equals"
57 | pretty (DotSepIdent ns n) = "DotSepIdentifier:" <++> prettyMaybe ns <+> dot <+> pretty n
58 | pretty Separator = "Separator"
64 | pretty EqOp = "EqOp"
65 | pretty AndOp = "AndOp"
66 | pretty Space = "Space"
67 | pretty (StringLit s) = "StringLit:" <++> pretty s
68 | pretty (IntegerLit i) = "IntegerLit:" <++> byShow i
80 | lte = is '<' <+> is '='
83 | gte = is '>' <+> is '='
92 | eqop = is '=' <+> is '='
95 | andop = is '&' <+> is '&'
97 | rawTokens : TokenMap Token
99 | [ (comment, Comment . drop 2)
100 | , (blockComment, Comment . shrink 2)
101 | , (identAllowDashes <+> reject dot, DotSepIdent Nothing)
102 | , (namespacedIdent, uncurry DotSepIdent . mkNamespacedIdent)
103 | , (separator, const Separator)
109 | , (eqop, const EqOp)
110 | , (andop, const AndOp)
111 | , (equals, const Equals)
112 | , (spacesOrNewlines, const Space)
113 | , (stringLit, \s => StringLit (stripQuotes s))
114 | , (intLit, \i => IntegerLit (cast i))
118 | lex : String -> Either (Int, Int, String) (List (WithBounds Token))
120 | case lex rawTokens str of
121 | (tokenData, (l, c, "")) =>
122 | Right $
(filter (useful . val) tokenData)
123 | ++ [MkBounded EndOfInput False (MkBounds l c l c)]
124 | (_, fail) => Left fail
126 | useful : Token -> Bool
127 | useful (Comment c) = False
128 | useful Space = False