0 | module Idrall.Parser.Rule
12 | import Idrall.Parser.Lexer
17 | Rule : {state : Type} -> Type -> Type
18 | Rule ty = Grammar state RawToken True ty
21 | EmptyRule : {state : Type} -> Type -> Type
22 | EmptyRule ty = Grammar state RawToken False ty
25 | whitespace : Rule ()
27 | terminal ("Expected whitespace") $
33 | tokenW : Grammar state (TokenRawToken) True a -> Grammar state (TokenRawToken) True a
36 | _ <- optional whitespace
40 | keyword : String -> Rule ()
42 | terminal ("Expected '" ++ req ++ "'") $
44 | Keyword s => if s == req then Just () else Nothing
48 | symbol : String -> Rule ()
50 | terminal ("Expected '" ++ req ++ "'") $
52 | Symbol s => if s == req then Just () else Nothing
56 | textBegin : Rule IsMultiline
58 | terminal "expected \" or ''" $
60 | StringBegin x => Just x
64 | textBoundary : Rule ()
66 | terminal "expected \" or ''" $
68 | StringBegin _ => Just ()
69 | StringEnd => Just ()
72 | escapeSingle : List Char -> List Char -> Maybe (List Char)
73 | escapeSingle escapeChars [] = pure []
74 | escapeSingle escapeChars (x :: xs) =
75 | if isPrefixOf escapeChars (x :: xs)
76 | then case drop (length escapeChars) (x::xs) of
77 | ('b' :: xs) => pure $
'\b' :: !(escapeSingle escapeChars xs)
78 | ('f' :: xs) => escapeSingle escapeChars ('\f' :: xs)
79 | ('n' :: xs) => escapeSingle escapeChars ('\n' :: xs)
80 | ('r' :: xs) => escapeSingle escapeChars ('\r' :: xs)
81 | ('t' :: xs) => escapeSingle escapeChars ('\t' :: xs)
82 | ('"' :: xs) => escapeSingle escapeChars ('"' :: xs)
83 | ('$' :: xs) => escapeSingle escapeChars ('$' :: xs)
84 | ('\\' :: xs) => pure $
'\\' :: !(escapeSingle escapeChars xs)
85 | ('/' :: xs) => escapeSingle escapeChars ('/' :: xs)
87 | _ => pure $
x :: !(escapeSingle escapeChars xs)
88 | else Just $
x :: !(escapeSingle escapeChars xs)
90 | escape : IsMultiline -> String -> Maybe String
91 | escape Single str = Just $
pack $
!(escapeSingle ['\\'] $
unpack str)
92 | escape Multi str = Just $
str
95 | textLit : IsMultiline -> Rule String
97 | terminal "expected valid Text" $
99 | StringLit str => escape x str
103 | interpBegin : Rule ()
105 | terminal "expected ${" $
107 | InterpBegin => Just ()
111 | interpEnd : Rule ()
113 | terminal "expected }" $
115 | InterpEnd => Just ()
119 | identPart : Rule String
121 | terminal "expected name" $
127 | fieldName : Rule String
129 | terminal "expected fieldName" $
132 | Builtin x => Just x
136 | missingImport : Rule ()
138 | terminal "expected missing" $
140 | MissingImport => Just ()
144 | httpImport : Rule String
146 | terminal "expected http import" $
148 | HttpImport x => Just x
152 | envImport : Rule String
154 | terminal "expected env import" $
156 | EnvImport x => Just x
161 | arrow = tokenW $
symbol "->" <|> symbol "→"
164 | shaImport : Rule String
166 | terminal "expected sha import" $
172 | filePath : Rule Path
174 | terminal "expected import path" $
176 | RelImport x => Just $
Relative $
forget $
split (== '/') x
177 | AbsImport x => Just $
Absolute $
forget $
split (== '/') x
178 | HomeDirImport x => Just $
Home $
forget $
split (== '/') x
182 | naturalLit : Rule Nat
184 | terminal "expected natural" $
186 | TNatural x => Just x
190 | integerLit : Rule Integer
192 | terminal "expected integer" $
194 | TInteger x => Just x
198 | doubleLit : Rule Double
200 | terminal "expected double" $
202 | TDouble x => Just x
206 | dottedList : Rule (List1 FieldName)
208 | _ <- optional whitespace
209 | x <- sepBy1 (tokenW $
symbol ".") (identPart)
210 | pure $
map MkFieldName x
213 | dottedListRec : Rule (List1 FieldName)
215 | _ <- optional whitespace
216 | x <- sepBy1 (tokenW $
symbol ".") (fieldName)
217 | pure $
map MkFieldName x
221 | builtin : Rule String
223 | terminal "expected builtin" $
225 | Builtin x => Just x
229 | someBuiltin : Rule ()
231 | terminal "expected builtin" $
233 | Builtin "Some" => Just ()
237 | endOfInput : Rule ()
239 | terminal "expected builtin" $
241 | EndInput => Just ()