Răsfoiți Sursa

removed invalid field names from structure type

Nicolas Cannasse 13 ani în urmă
părinte
comite
76fc24a029
2 a modificat fișierele cu 21 adăugiri și 3 ștergeri
  1. 17 2
      parser.ml
  2. 4 1
      typer.ml

+ 17 - 2
parser.ml

@@ -43,6 +43,21 @@ let error_msg = function
 let error m p = raise (Error (m,p))
 let display_error : (error_msg -> pos -> unit) ref = ref (fun _ _ -> assert false)
 
+let quoted_ident_prefix = "@$__hx__"
+
+let quote_ident s =
+	try
+		for i = 0 to String.length s - 1 do
+			match String.unsafe_get s i with
+			| 'a'..'z' | 'A'..'Z' | '_' -> ()
+			| '0'..'9' when i > 0 -> ()
+			| _ -> raise Exit
+		done;
+		if Hashtbl.mem Lexer.keywords s then raise Exit;
+		s
+	with Exit ->
+		quoted_ident_prefix ^ s
+
 let cache = ref (DynArray.create())
 let doc = ref None
 let use_doc = ref false
@@ -514,7 +529,7 @@ and parse_class_herit = parser
 and block1 = parser
 	| [< '(Const (Ident name),p); s >] -> block2 name (Ident name) p s
 	| [< '(Const (Type name),p); s >] -> block2 name (Type name) p s
-	| [< '(Const (String name),p); s >] -> block2 name (String name) p s
+	| [< '(Const (String name),p); s >] -> block2 (quote_ident name) (String name) p s
 	| [< b = block [] >] -> EBlock b
 
 and block2 name ident p = parser
@@ -553,7 +568,7 @@ and parse_obj_decl = parser
 	| [< '(Comma,_); s >] ->
 		(match s with parser
 		| [< name, _ = any_ident; '(DblDot,_); e = expr; l = parse_obj_decl >] -> (name,e) :: l
-		| [< '(Const (String name),_); '(DblDot,_); e = expr; l = parse_obj_decl >] -> (name,e) :: l
+		| [< '(Const (String name),_); '(DblDot,_); e = expr; l = parse_obj_decl >] -> (quote_ident name,e) :: l
 		| [< >] -> [])
 	| [< >] -> []
 

+ 4 - 1
typer.ml

@@ -1389,11 +1389,14 @@ and type_expr ctx ?(need_val=true) (e,p) =
 		let e = type_expr ctx ~need_val e in
 		mk (TParenthesis e) e.etype p
 	| EObjectDecl fl ->
+		let pf = Parser.quoted_ident_prefix in
+		let pflen = String.length pf in
 		let rec loop (l,acc) (f,e) =
+			let f, add = if String.length f >= pflen && String.sub f 0 pflen = pf then String.sub f pflen (String.length f - pflen), false else f, true in
 			if PMap.mem f acc then error ("Duplicate field in object declaration : " ^ f) p;
 			let e = type_expr ctx e in
 			let cf = mk_field f e.etype e.epos in
-			((f,e) :: l, PMap.add f cf acc)
+			((f,e) :: l, if add then PMap.add f cf acc else acc)
 		in
 		let fields , types = List.fold_left loop ([],PMap.empty) fl in
 		let x = ref Const in