Skip to content

Commit

Permalink
Make the hash-consing domain-safe
Browse files Browse the repository at this point in the history
  • Loading branch information
kit-ty-kate committed Dec 4, 2024
1 parent 847d6a0 commit 3d575a9
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# opam-file-format - Parser and printer for the opam file syntax

This library provides the parser and printer for the opam file syntax as a
library with no dependencies but [Dune](https://dune.build) >= 2.0.
library with no dependencies but [Dune](https://dune.build) >= 3.13.

Opam was created and is maintained by [OCamlPro](http://www.ocamlpro.com).

Expand Down
2 changes: 1 addition & 1 deletion dune-project
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
(lang dune 2.0)
(lang dune 3.13)
(using menhir 2.0)
2 changes: 1 addition & 1 deletion opam-file-format.opam
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ build: ["dune" "build" "-p" name "-j" jobs]
run-test: ["dune" "runtest" "-p" name "-j" jobs]
depends: [
"ocaml" {>= "4.02"}
"dune" {>= "2.0"}
"dune" {>= "3.13"}
"menhir" {>= "20211230"}
"alcotest" {with-test & >= "0.4.8"}
"fmt" {with-test}
Expand Down
4 changes: 4 additions & 0 deletions src/compat/domain.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module DLS = struct
let new_key f = f ()
let get x = x
end
6 changes: 6 additions & 0 deletions src/compat/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
(rule
(enabled_if (< %{ocaml_version} "5.0"))
(action (with-stdout-to compat.sexp (echo "(\"domain\")"))))
(rule
(enabled_if (>= %{ocaml_version} "5.0"))
(action (with-stdout-to compat.sexp (echo "()"))))
5 changes: 5 additions & 0 deletions src/dune
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@
(public_name opam-file-format)
(synopsis "Parser and printer for the opam file syntax")
(wrapped false)
(private_modules (:include compat/compat.sexp))
(flags :standard (:include flags.sexp)))

(rule
(enabled_if (< %{ocaml_version} "5.0"))
(action (copy compat/domain.ml domain.ml)))

(rule
(enabled_if (< %{ocaml_version} "4.03"))
(action (with-stdout-to flags.sexp (echo "(-w -50)"))))
Expand Down
6 changes: 3 additions & 3 deletions src/opamLexer.mll
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ let char_for_hexadecimal_code lexbuf i =
(* Some hash-consing for strings *)
module HS =
Weak.Make(struct include String let hash = Hashtbl.hash let equal = (=) end)
let hm = HS.create 317
let hm = Domain.DLS.new_key (fun () -> HS.create 317)


let buffer_rule r lb =
Expand All @@ -93,7 +93,7 @@ let buffer_rule r lb =
r b lb ;
(* buffer start position, instead of last lexem position *)
lb.Lexing.lex_start_p <- pos;
HS.merge hm (Buffer.contents b)
HS.merge (Domain.DLS.get hm) (Buffer.contents b)
}

let eol = '\r'? '\n'
Expand Down Expand Up @@ -131,7 +131,7 @@ rule token = parse
| "true" { BOOL true }
| "false"{ BOOL false }
| int { INT (int_of_string (Lexing.lexeme lexbuf)) }
| ident { IDENT (HS.merge hm (Lexing.lexeme lexbuf)) }
| ident { IDENT (HS.merge (Domain.DLS.get hm) (Lexing.lexeme lexbuf)) }
| relop { RELOP (FullPos.relop (Lexing.lexeme lexbuf)) }
| '&' { AND }
| '|' { OR }
Expand Down

0 comments on commit 3d575a9

Please sign in to comment.