-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: share some pcre parser bits (#254)
In particular, share the basic building block for tracking our position within the parsed string and looking for specific chars. Signed-off-by: Rudi Grinberg <[email protected]>
- Loading branch information
Showing
5 changed files
with
107 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
type t = | ||
{ str : string | ||
; mutable pos : int | ||
} | ||
|
||
exception Parse_error | ||
|
||
let create str = { str ; pos = 0 } | ||
|
||
let unget t = t.pos <- t.pos - 1 | ||
|
||
let junk t = t.pos <- t.pos + 1 | ||
|
||
let eos t = t.pos = String.length t.str | ||
|
||
let test t c = not (eos t) && t.str.[t.pos] = c | ||
|
||
let test2 t c c' = | ||
t.pos + 1 < String.length t.str && t.str.[t.pos] = c && t.str.[t.pos + 1] = c' | ||
|
||
let accept t c = | ||
let r = test t c in | ||
if r then t.pos <- t.pos + 1; | ||
r | ||
|
||
let accept2 t c c' = | ||
let r = test2 t c c' in | ||
if r then t.pos <- t.pos + 2; | ||
r | ||
|
||
let get t = | ||
let r = t.str.[t.pos] in | ||
t.pos <- t.pos + 1; | ||
r | ||
|
||
let accept_s t s' = | ||
let len = String.length s' in | ||
try | ||
for j = 0 to len - 1 do | ||
try if s'.[j] <> t.str.[t.pos + j] then raise_notrace Exit | ||
with _ -> raise_notrace Exit | ||
done; | ||
t.pos <- t.pos + len; | ||
true | ||
with Exit -> false | ||
|
||
let rec integer' t i = | ||
if eos t then | ||
Some i | ||
else | ||
match get t with | ||
| '0'..'9' as d -> | ||
let i' = 10 * i + (Char.code d - Char.code '0') in | ||
if i' < i then raise Parse_error; | ||
integer' t i' | ||
| _ -> | ||
unget t; Some i | ||
|
||
let integer t = | ||
if eos t then | ||
None | ||
else | ||
match get t with | ||
| '0'..'9' as d -> integer' t (Char.code d - Char.code '0') | ||
| _ -> unget t; None | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
type t | ||
|
||
exception Parse_error | ||
|
||
val create : string -> t | ||
val junk : t -> unit | ||
val unget : t -> unit | ||
val eos : t -> bool | ||
val test : t -> char -> bool | ||
val test2 : t -> char -> char -> bool | ||
val get : t -> char | ||
val accept : t -> char -> bool | ||
val accept2 : t -> char -> char -> bool | ||
val accept_s : t -> string -> bool | ||
val integer : t -> int option |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters