Browse Source

allowed arbitrary string fields in anonymous objects.

Nicolas Cannasse 13 năm trước cách đây
mục cha
commit
b514148c8d
3 tập tin đã thay đổi với 18 bổ sung4 xóa
  1. 1 0
      doc/CHANGES.txt
  2. 13 1
      genjs.ml
  3. 4 3
      parser.ml

+ 1 - 0
doc/CHANGES.txt

@@ -17,6 +17,7 @@
 	all : added documentation in --display infos + display overloads in completion
 	js : removed --js-namespace, added $hxClasses
 	flash : output traces to native trace() when using -D fdb or -D nativeTrace
+	all : allowed abitrary string fields in anonymous objects
 
 2011-09-25: 2.08
 	js : added js.JQuery

+ 13 - 1
genjs.ml

@@ -51,9 +51,21 @@ let kwds =
 	];
 	h
 
+let valid_js_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;
+		true
+	with Exit ->
+		false
+
 let field s = if Hashtbl.mem kwds s then "[\"" ^ s ^ "\"]" else "." ^ s
 let ident s = if Hashtbl.mem kwds s then "$" ^ s else s
-let anon_field s = if Hashtbl.mem kwds s then "'" ^ s ^ "'" else s
+let anon_field s = if Hashtbl.mem kwds s || not (valid_js_ident s) then "'" ^ s ^ "'" else s
 
 let spr ctx s = ctx.separator <- false; Buffer.add_string ctx.buf s
 let print ctx = ctx.separator <- false; Printf.kprintf (fun s -> Buffer.add_string ctx.buf s)

+ 4 - 3
parser.ml

@@ -512,13 +512,14 @@ and parse_class_herit = parser
 	| [< '(Kwd Implements,_); t = parse_type_path >] -> HImplements t
 
 and block1 = parser
-	| [< '(Const (Ident name),p); s >] -> block2 name true p s
-	| [< '(Const (Type name),p); s >] -> block2 name false p s
+	| [< '(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
 	| [< b = block [] >] -> EBlock b
 
 and block2 name ident p = parser
 	| [< '(DblDot,_); e = expr; l = parse_obj_decl >] -> EObjectDecl ((name,e) :: l)
-	| [< e = expr_next (EConst (if ident then Ident name else Type name),p); s >] ->
+	| [< e = expr_next (EConst ident,p); s >] ->
 		try
 			let _ = semicolon s in
 			let b = block [e] s in