Fix Atom definition of Usexp

Fix the is_valid function to only validate atoms that don't contain spaces,
parens, comment characters.
This commit is contained in:
Rudi Grinberg 2018-03-17 02:33:25 +08:00 committed by Jérémie Dimino
parent 154b405e51
commit f39b302a8d
3 changed files with 17 additions and 16 deletions

View File

@ -15,17 +15,18 @@ module A = Parser_automaton_internal
module Atom = struct module Atom = struct
type t = Sexp_ast.atom = A of string [@@unboxed] type t = Sexp_ast.atom = A of string [@@unboxed]
let is_valid s = let is_valid str =
if s = "" then false let len = String.length str in
else len = 0 ||
try let rec loop ix =
for i = 0 to String.length s - 1 do match str.[ix] with
match String.unsafe_get s i with | '"' | '(' | ')' | ';' | '\\' -> true
| ' ' .. '~' -> () | '|' -> ix > 0 && let next = ix - 1 in Char.equal str.[next] '#' || loop next
| _ -> raise Exit | '#' -> ix > 0 && let next = ix - 1 in Char.equal str.[next] '|' || loop next
done; | '\000' .. '\032' | '\127' .. '\255' -> true
true | _ -> ix > 0 && loop (ix - 1)
with Exit -> false in
not (loop (len - 1))
(* XXX eventually we want to report a nice error message to the user (* XXX eventually we want to report a nice error message to the user
at the point the conversion is made. *) at the point the conversion is made. *)

View File

@ -2,8 +2,8 @@
module Atom : sig module Atom : sig
type t = private A of string [@@unboxed] type t = private A of string [@@unboxed]
(** Acceptable atoms are composed of chars in the range [' ' .. '~'] (** Acceptable atoms are composed of chars in the range ['!' .. '~'] excluding
and must be nonempty. *) [' ' '"' '(' ')' ';' '\\'], and must be nonempty. *)
val is_valid : string -> bool val is_valid : string -> bool
(** [is_valid s] checks that [s] respects the constraints to be an atom. *) (** [is_valid s] checks that [s] respects the constraints to be an atom. *)

View File

@ -35,11 +35,11 @@
-)) -))
(generate_runner (generate_runner
((progn ((progn
(echo let () = print_int 41) (echo "let () = print_int 41")
(echo "\n") (echo "\n")
(echo let () = print_int 42) (echo "let () = print_int 42")
(echo "\n") (echo "\n")
(echo let () = print_int 43;;)))) (echo "let () = print_int 43;;"))))
(extends ()))))) (extends ())))))
run alias dune-file/runtest run alias dune-file/runtest
414243 414243