0 | module Parser.Lexer.Common
2 | import public Libraries.Text.Lexer
13 | <+> many (is '-') <+> reject (is '}')
14 | <+> many (isNot '\n')
23 | toEndComment : (k : Nat) -> Recognise (k /= 0)
24 | toEndComment Z = empty
26 | = some (pred (\c => c /= '-' && c /= '{' && c /= '"' && c /= '\''))
27 | <+> (eof <|> toEndComment (S k))
28 | <|> is '{' <+> singleBrace k
29 | <|> is '-' <+> singleDash k
30 | <|> (charLit <|> pred (== '\'')) <+> toEndComment (S k)
31 | <|> stringLit <+> toEndComment (S k)
36 | singleBrace : (k : Nat) -> Lexer
38 | = is '-' <+> many (is '-')
39 | <+> (eof <|> singleDash (S k))
41 | <|> toEndComment (S k)
46 | singleDash : (k : Nat) -> Lexer
48 | = is '-' <+> doubleDash k
49 | <|> is '}' <+> toEndComment k
50 | <|> toEndComment (S k)
55 | doubleDash : (k : Nat) -> Lexer
56 | doubleDash k = with Prelude.(::)
57 | many (is '-') <+> choice
58 | [ is '}' <+> toEndComment k
59 | , many (isNot '\n') <+> toEndComment (S k)
63 | blockComment : Lexer
64 | blockComment = is '{' <+> is '-' <+> many (is '-') <+> (eof <|> toEndComment 1)
70 | data Flavour = AllowDashes | Capitalised | Normal
72 | isIdentStart : Flavour -> Char -> Bool
73 | isIdentStart _ '_' = True
74 | isIdentStart Capitalised x = isUpper x || x > chr 160
75 | isIdentStart _ x = isAlpha x || x > chr 160
77 | isIdentTrailing : Flavour -> Char -> Bool
78 | isIdentTrailing AllowDashes '-' = True
79 | isIdentTrailing _ '\'' = True
80 | isIdentTrailing _ '_' = True
81 | isIdentTrailing _ x = isAlphaNum x || x > chr 160
84 | isIdent : Flavour -> String -> Bool
85 | isIdent flavour string =
86 | case unpack string of
88 | (x::xs) => isIdentStart flavour x && all (isIdentTrailing flavour) xs
91 | ident : Flavour -> Lexer
93 | (pred $
isIdentStart flavour) <+>
94 | (many . pred $
isIdentTrailing flavour)
97 | isIdentNormal : String -> Bool
98 | isIdentNormal = isIdent Normal
101 | identNormal : Lexer
102 | identNormal = ident Normal
105 | identAllowDashes : Lexer
106 | identAllowDashes = ident AllowDashes
108 | namespaceIdent : Lexer
109 | namespaceIdent = ident Capitalised <+> many (is '.' <+> ident Capitalised <+> expect (is '.'))
112 | namespacedIdent : Lexer
113 | namespacedIdent = namespaceIdent <+> opt (is '.' <+> identNormal)
116 | spacesOrNewlines : Lexer
117 | spacesOrNewlines = some (space <|> newline)