Skip to content

Commit

Permalink
OCaml: use unsafe_get instead of get as the default YYPEEK implem…
Browse files Browse the repository at this point in the history
…entation.

The generated code already handles the end of input, so it should use
`unsafe_get` to avoid redundant bounds checks. If the user wants to use `get`,
they can always do so by overriding YYPEEK.

Update all examples and remove '\x00' character at the end of string literals,
as they are already NULL-terminated.
  • Loading branch information
skvadrik committed Dec 25, 2024
1 parent 54d0eaa commit e2d249f
Show file tree
Hide file tree
Showing 33 changed files with 604 additions and 604 deletions.
2 changes: 1 addition & 1 deletion bootstrap/src/default_syntax_ocaml.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ static constexpr const char* DEFAULT_SYNTAX_OCAML =
"re2c:YYMAXNMATCH = \"yymaxnmatch\";\n"
"re2c:YYMTAGN = \"yymatgn\";\n"
"re2c:YYMTAGP = \"yymtagp\";\n"
"re2c:YYPEEK = (.api.record ? \"get\" : \"yypeek\");\n"
"re2c:YYPEEK = (.api.record ? \"unsafe_get\" : \"yypeek\");\n"
"re2c:YYRESTORE = \"yyrestore\";\n"
"re2c:YYRESTORECTX = \"yyrestorectx\";\n"
"re2c:YYRESTORETAG = \"yyrestoretag\";\n"
Expand Down
6 changes: 3 additions & 3 deletions examples/ocaml/01_basic.ml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type state = {


let rec yy0 (yyrecord : state) : bool =
let yych = get yyrecord.yyinput yyrecord.yycursor in
let yych = unsafe_get yyrecord.yyinput yyrecord.yycursor in
yyrecord.yycursor <- yyrecord.yycursor + 1;
match yych with
| '1'..'9' -> (yy2 [@tailcall]) yyrecord
Expand All @@ -20,7 +20,7 @@ and yy1 (yyrecord : state) : bool =
false

and yy2 (yyrecord : state) : bool =
let yych = get yyrecord.yyinput yyrecord.yycursor in
let yych = unsafe_get yyrecord.yyinput yyrecord.yycursor in
match yych with
| '0'..'9' ->
yyrecord.yycursor <- yyrecord.yycursor + 1;
Expand All @@ -36,7 +36,7 @@ and lex (yyrecord : state) : bool =


let main () =
let st = {yyinput = "1234\x00"; yycursor = 0}
let st = {yyinput = "1234"; yycursor = 0}
in if not (lex st) then raise (Failure "error")

let _ = main ()
2 changes: 1 addition & 1 deletion examples/ocaml/01_basic.re
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type state = {
%}

let main () =
let st = {yyinput = "1234\x00"; yycursor = 0}
let st = {yyinput = "1234"; yycursor = 0}
in if not (lex st) then raise (Failure "error")

let _ = main ()
16 changes: 8 additions & 8 deletions examples/ocaml/conditions/parse_u32_blocks.ml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ let add (num: int option) (dgt: int) (base: int) : int option =


let rec yy0 (st : state) (num : int option) : int option =
let yych = get st.yyinput st.yycursor in
let yych = unsafe_get st.yyinput st.yycursor in
st.yycursor <- st.yycursor + 1;
match yych with
| '0'..'1' -> (yy2 [@tailcall]) st num
Expand All @@ -41,7 +41,7 @@ and parse_bin (st : state) (num : int option) : int option =


let rec yy3 (st : state) (num : int option) : int option =
let yych = get st.yyinput st.yycursor in
let yych = unsafe_get st.yyinput st.yycursor in
st.yycursor <- st.yycursor + 1;
match yych with
| '0'..'7' -> (yy5 [@tailcall]) st num
Expand All @@ -60,7 +60,7 @@ and parse_oct (st : state) (num : int option) : int option =


let rec yy6 (st : state) (num : int option) : int option =
let yych = get st.yyinput st.yycursor in
let yych = unsafe_get st.yyinput st.yycursor in
st.yycursor <- st.yycursor + 1;
match yych with
| '0'..'9' -> (yy8 [@tailcall]) st num
Expand All @@ -79,7 +79,7 @@ and parse_dec (st : state) (num : int option) : int option =


let rec yy9 (st : state) (num : int option) : int option =
let yych = get st.yyinput st.yycursor in
let yych = unsafe_get st.yyinput st.yycursor in
st.yycursor <- st.yycursor + 1;
match yych with
| '0'..'9' -> (yy11 [@tailcall]) st num
Expand All @@ -106,7 +106,7 @@ and parse_hex (st : state) (num : int option) : int option =


let rec yy14 (st : state) : int option =
let yych = get st.yyinput st.yycursor in
let yych = unsafe_get st.yyinput st.yycursor in
st.yycursor <- st.yycursor + 1;
match yych with
| '0' -> (yy16 [@tailcall]) st
Expand All @@ -118,7 +118,7 @@ and yy15 (st : state) : int option =

and yy16 (st : state) : int option =
st.yymarker <- st.yycursor;
let yych = get st.yyinput st.yycursor in
let yych = unsafe_get st.yyinput st.yycursor in
match yych with
| 'B'
| 'b' ->
Expand All @@ -138,7 +138,7 @@ and yy18 (st : state) : int option =
parse_dec st (Some 0)

and yy19 (st : state) : int option =
let yych = get st.yyinput st.yycursor in
let yych = unsafe_get st.yyinput st.yycursor in
match yych with
| '0'..'1' ->
st.yycursor <- st.yycursor + 1;
Expand All @@ -150,7 +150,7 @@ and yy20 (st : state) : int option =
(yy17 [@tailcall]) st

and yy21 (st : state) : int option =
let yych = get st.yyinput st.yycursor in
let yych = unsafe_get st.yyinput st.yycursor in
match yych with
| '0'..'9'
| 'A'..'F'
Expand Down
16 changes: 8 additions & 8 deletions examples/ocaml/conditions/parse_u32_conditions.ml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ let add (num: int option) (dgt: int) (base: int) : int option =


let rec yy1 (st : state) (num : int option) : int option =
let yych = get st.yyinput st.yycursor in
let yych = unsafe_get st.yyinput st.yycursor in
st.yycursor <- st.yycursor + 1;
match yych with
| '0' -> (yy3 [@tailcall]) st num
Expand All @@ -36,7 +36,7 @@ and yy2 (st : state) (num : int option) : int option =

and yy3 (st : state) (num : int option) : int option =
st.yymarker <- st.yycursor;
let yych = get st.yyinput st.yycursor in
let yych = unsafe_get st.yyinput st.yycursor in
match yych with
| 'B'
| 'b' ->
Expand All @@ -58,7 +58,7 @@ and yy5 (st : state) (num : int option) : int option =
(yyfndec [@tailcall]) st num

and yy6 (st : state) (num : int option) : int option =
let yych = get st.yyinput st.yycursor in
let yych = unsafe_get st.yyinput st.yycursor in
match yych with
| '0'..'1' ->
st.yycursor <- st.yycursor + 1;
Expand All @@ -70,7 +70,7 @@ and yy7 (st : state) (num : int option) : int option =
(yy4 [@tailcall]) st num

and yy8 (st : state) (num : int option) : int option =
let yych = get st.yyinput st.yycursor in
let yych = unsafe_get st.yyinput st.yycursor in
match yych with
| '0'..'9'
| 'A'..'F'
Expand All @@ -93,7 +93,7 @@ and yyfninit (st : state) (num : int option) : int option =
(yy1 [@tailcall]) st num

and yy11 (st : state) (num : int option) : int option =
let yych = get st.yyinput st.yycursor in
let yych = unsafe_get st.yyinput st.yycursor in
st.yycursor <- st.yycursor + 1;
match yych with
| '0'..'1' -> (yy13 [@tailcall]) st num
Expand All @@ -109,7 +109,7 @@ and yyfnbin (st : state) (num : int option) : int option =
(yy11 [@tailcall]) st num

and yy14 (st : state) (num : int option) : int option =
let yych = get st.yyinput st.yycursor in
let yych = unsafe_get st.yyinput st.yycursor in
st.yycursor <- st.yycursor + 1;
match yych with
| '0'..'7' -> (yy16 [@tailcall]) st num
Expand All @@ -125,7 +125,7 @@ and yyfnoct (st : state) (num : int option) : int option =
(yy14 [@tailcall]) st num

and yy17 (st : state) (num : int option) : int option =
let yych = get st.yyinput st.yycursor in
let yych = unsafe_get st.yyinput st.yycursor in
st.yycursor <- st.yycursor + 1;
match yych with
| '0'..'9' -> (yy19 [@tailcall]) st num
Expand All @@ -141,7 +141,7 @@ and yyfndec (st : state) (num : int option) : int option =
(yy17 [@tailcall]) st num

and yy20 (st : state) (num : int option) : int option =
let yych = get st.yyinput st.yycursor in
let yych = unsafe_get st.yyinput st.yycursor in
st.yycursor <- st.yycursor + 1;
match yych with
| '0'..'9' -> (yy22 [@tailcall]) st num
Expand Down
Loading

0 comments on commit e2d249f

Please sign in to comment.