0 | module Text.ILex.Dependent.Bytes
  1 |
  2 | import Data.String
  3 | import Text.ILex.Char.UTF8
  4 | import public Data.ByteString
  5 | import public Text.ByteBounds
  6 | import public Text.ILex.RExp
  7 | import public Text.ILex.LTENat
  8 |
  9 | %default total
 10 |
 11 | ||| An interface for mutable parser stacks `s` that facilitates
 12 | ||| parsing string tokens containing escape sequences.
 13 | public export
 14 | data Bytes : (n : Nat) -> Type where
 15 |   [noHints]
 16 |   BS :
 17 |        {n : _}
 18 |     -- Remainder of the previous bytestring that should
 19 |     -- be used as the beginning of the current token.
 20 |     -> (prev : ByteString)
 21 |
 22 |     -- The byte vector currently being processed.
 23 |     -> (cur  : IBuffer n)
 24 |
 25 |     -- Absolute position of the first byte of `prev`.
 26 |     -> (prevOffset : Nat)
 27 |
 28 |     -- Absolute position of the first byte of `cur`
 29 |     -- (this equals `prevOffset + prev.size` but should be
 30 |     -- provided as a separate field for reasons of efficiency)
 31 |     -> (curOffset  : Nat)
 32 |     -> Bytes n
 33 |
 34 | ||| Initializes a new parser stack.
 35 | public export
 36 | init : {n : _} -> IBuffer n -> Bytes n
 37 | init buf = BS empty buf 0 0
 38 |
 39 | public export
 40 | newBytes : {n : _} -> (offset : Nat) -> ByteString -> IBuffer n -> Bytes n
 41 | newBytes o bs buf =
 42 |   BS
 43 |     { cur        = buf
 44 |     , prev       = bs
 45 |     , prevOffset = o
 46 |     , curOffset  = o + bs.size
 47 |     }
 48 |
 49 | public export
 50 | record In a where
 51 |   constructor I
 52 |   {0 size : Nat}
 53 |
 54 |   bytes : Bytes size
 55 |
 56 |   ||| Lower relative token bound (lower position in `cur`)
 57 |   from  : LTENat size
 58 |
 59 |   ||| Next relative token bound (upser position + 1 in `cur`)
 60 |   till  : LTENat size
 61 |
 62 |   val   : a
 63 |
 64 | ||| Returns the current substring of the byte vector
 65 | ||| (corresponding to the position and length of the current
 66 | ||| token).
 67 | |||
 68 | ||| The remainder of the previous bytestring is prefixed in case
 69 | ||| we are currently at position zero.
 70 | export %inline
 71 | getBytes : In a -> ByteString
 72 | getBytes (I (BS p c po co) rf rt _) =
 73 |   case rf.val of
 74 |     0 => p <+> bytesFromTill c rf rt
 75 |     _ => bytesFromTill c rf rt
 76 |
 77 | export %inline
 78 | getString : In a -> String
 79 | getString (I (BS p c po co) rf rt _) =
 80 |   case rf.val of
 81 |     0 => toString $ p <+> bytesFromTill c rf rt
 82 |     _ => stringFromTill c rf rt
 83 |
 84 | export %inline
 85 | toFinalPos' : Bytes n -> a -> In a
 86 | toFinalPos' bs@(BS {}) = I bs (last n) (last n)
 87 |
 88 | export %inline
 89 | toFinalPos : In a -> In a
 90 | toFinalPos (I bs@(BS {n} {}) _ _ v) = I bs (last n) (last n) v
 91 |
 92 | ||| Gets the absolute position of the
 93 | ||| first byte of the current token.
 94 | export %inline
 95 | startPos : In a -> BytePos
 96 | startPos (I (BS p c po co) (LN f) _ _) =
 97 |   case f of
 98 |     0 => BP po
 99 |     _ => BP $ co + f
100 |
101 | ||| Gets the absolute position of the last byte of the current token.
102 | export %inline
103 | endPos : In a -> BytePos
104 | endPos (I (BS p c po co) (LN rf) (LN rt) _) =
105 |   case rf of
106 |     0 => endPos po      (co+rt)
107 |     _ => endPos (co+rf) (co+rt)
108 |
109 | ||| Gets the bounds of the current token.
110 | export %inline
111 | bounds : In a -> ByteBounds
112 | bounds (I (BS p c po co) (LN rf) (LN rt) _) =
113 |   let till := co + rt
114 |    in case rf of
115 |         0 => let from := po      in BB (BP from) (endPos from till)
116 |         _ => let from := co + rf in BB (BP from) (endPos from till)
117 |
118 | ||| Computes the given value and pairs it with the token bounds.
119 | export %inline
120 | bounded : In a -> t -> ByteBounded t
121 | bounded x v = B v $ Bytes.bounds x
122 |
123 | ||| Like `failWith`, but generates the bounds of the error from the
124 | ||| current position and the bytes read until the error occurred.
125 | export %inline
126 | failHere : In a -> InnerError e -> BBErr e
127 | failHere = bounded
128 |
129 | export
130 | unexpectedInner : List String -> In a -> InnerError e
131 | unexpectedInner ss i =
132 |   case getBytes i of
133 |     BS 0 _  => EOI
134 |     BS 1 bv =>
135 |      let b := bv `at` 0
136 |          s := String.singleton (cast b)
137 |       in case isAscii b of
138 |            True  => Expected ss s
139 |            False => InvalidByte b
140 |     bs => Expected ss (toString bs)
141 |
142 | export
143 | unexpectedErr : List String -> In a -> BBErr e
144 | unexpectedErr ss i = failHere i $ unexpectedInner ss i
145 |
146 | export
147 | unclosedErr : ByteBounds -> String -> BBErr e
148 | unclosedErr bnds str = B (Unclosed str) bnds
149 |
150 | export
151 | failUnexpected : List String -> In a -> Either (BBErr e) x
152 | failUnexpected ss = Left . unexpectedErr ss
153 |
154 | export
155 | failUnclosed : ByteBounds -> String -> Either (BBErr e) x
156 | failUnclosed bnds = Left . unclosedErr bnds
157 |