21 | module Text.Parse.Manual
23 | import public Data.Bool.Rewrite
24 | import public Data.List1
25 | import public Data.List.Shift
26 | import public Data.List.Suffix
27 | import public Data.List.Suffix.Result
28 | import public Data.List.Suffix.Result0
30 | import public Text.Bounds
31 | import public Text.FC
32 | import public Text.ParseError
34 | import public Text.Lex.Manual
35 | import public Text.Lex.Shift
43 | -> (ts : List $
Bounded t)
46 | Res b t ts e a = Result0 b (Bounded t) ts (Bounded $
InnerError e) a
49 | 0 Grammar : (strict : Bool) -> (t,e,a : Type) -> Type
50 | Grammar strict t e a = (ts : List $
Bounded t) -> Res strict t ts e a
53 | 0 AccGrammar : (strict : Bool) -> (t,e,a : Type) -> Type
54 | AccGrammar strict t e a =
55 | (ts : List $
Bounded t)
56 | -> (0 acc : SuffixAcc ts)
57 | -> Res strict t ts e a
61 | public export %inline
62 | acc : AccGrammar b t e a -> Grammar b t e a
63 | acc f ts = f ts suffixAcc
69 | public export %inline
70 | swapOr : {0 x,y : _} -> Grammar (x || y) t e a -> Grammar (y || x) t e a
71 | swapOr = swapOr (\k => Grammar k t e a)
73 | public export %inline
74 | orSame : {0 x : _} -> Grammar (x || x) t e a -> Grammar x t e a
75 | orSame = orSame (\k => Grammar k t e a)
77 | public export %inline
78 | orTrue : {0 x : _} -> Grammar (x || True) t e a -> Grammar True t e a
79 | orTrue = orTrue (\k => Grammar k t e a)
81 | public export %inline
82 | orFalse : {0 x : _} -> Grammar (x || False) t e a -> Grammar x t e a
83 | orFalse = orFalse (\k => Grammar k t e a)
85 | public export %inline
86 | swapAnd : {0 x,y : _} -> Grammar (x && y) t e a -> Grammar (y && x) t e a
87 | swapAnd = swapAnd (\k => Grammar k t e a)
89 | public export %inline
90 | andSame : {0 x : _} -> Grammar (x && x) t e a -> Grammar x t e a
91 | andSame = andSame (\k => Grammar k t e a)
93 | public export %inline
94 | andTrue : {0 x : _} -> Grammar (x && True) t e a -> Grammar x t e a
95 | andTrue = andTrue (\k => Grammar k t e a)
97 | public export %inline
98 | andFalse : {0 x : _} -> Grammar (x && False) t e a -> Grammar False t e a
99 | andFalse = andFalse (\k => Grammar k t e a)
109 | terminal : Interpolation t => (t -> Maybe a) -> Grammar True t e a
110 | terminal f (B v b :: xs) = case f v of
111 | Just r => Succ0 r xs
112 | Nothing => unexpected (B v b)
113 | terminal _ [] = eoi
117 | terminalE : (t -> Either e a) -> Grammar True t e a
118 | terminalE f (B v b :: xs) = case f v of
119 | Right r => Succ0 r xs
120 | Left err => custom b err
121 | terminalE _ [] = eoi
127 | exact : Interpolation t => Eq t => t -> Grammar True t e ()
128 | exact v (x::xs) = if v == x.val then Succ0 () xs else expected x.bounds v "\{x.val}"
129 | exact v [] = expected NoBounds v ""
133 | peek : Grammar False t e t
134 | peek (x::xs) = Succ0 x.val (x::xs)
139 | nextIs : Interpolation t => (t -> Bool) -> Grammar False t e t
140 | nextIs f (x::xs) = if f x.val then Succ0 x.val (x :: xs) else unexpected x
145 | nextEquals : Interpolation t => Eq t => t -> Grammar False t e t
146 | nextEquals v (x::xs) =
147 | if v == x.val then Succ0 x.val (x::xs) else expected x.bounds v "\{x.val}"
148 | nextEquals v [] = expected NoBounds v ""
159 | -> {auto _ : Interpolation e}
160 | -> {auto _ : Interpolation t}
161 | -> (Origin -> String -> Either (ParseError e) a)
164 | testParse f s = case f Virtual s of
165 | Right res => putStrLn "Success: \{show res}"
166 | Left err => putStrLn $
interpolate err