浏览代码

create fake ident nodes when iterating/mapping FIdent segments

Dan Korostelev 8 年之前
父节点
当前提交
d1a38b3905
共有 2 个文件被更改,包括 24 次插入4 次删除
  1. 15 2
      src/syntax/ast.ml
  2. 9 2
      std/haxe/macro/ExprTools.hx

+ 15 - 2
src/syntax/ast.ml

@@ -596,7 +596,16 @@ let map_expr loop (e,p) =
 	let e = (match e with
 	| EConst _ -> e
 	| EFormat parts ->
-		let parts = List.map (fun p -> match fst p with FmtRaw _ | FmtIdent _ -> p | FmtExpr e -> (FmtExpr (loop e), snd p)) parts in
+		let parts = List.map (fun p ->
+			match fst p with
+			| FmtRaw _ -> p
+			| FmtIdent i ->
+				let fake_expr = (EConst (Ident i),snd p) in
+				(match loop fake_expr with
+				| (EConst (Ident i),new_pos) -> (FmtIdent i,new_pos)
+				| (_,new_pos) as expr -> (FmtExpr expr,new_pos))
+			| FmtExpr e -> (FmtExpr (loop e), snd p)
+		) parts in
 		EFormat parts
 	| EArray (e1,e2) ->
 		let e1 = loop e1 in
@@ -706,7 +715,11 @@ let iter_expr loop (e,p) =
 		) cases;
 		(match def with None -> () | Some (e,_) -> opt e);
 	| EFormat parts ->
-		List.iter (fun p -> match fst p with FmtRaw _ | FmtIdent _ -> () | FmtExpr e1 -> loop e1) parts
+		List.iter (fun p -> match fst p with
+			| FmtRaw _ -> ()
+			| FmtIdent i -> loop (EConst (Ident i),snd p)
+			| FmtExpr e1 -> loop e1
+		) parts
 	| EFunction(_,f) ->
 		List.iter (fun (_,_,_,_,eo) -> opt eo) f.f_args;
 		opt f.f_expr

+ 9 - 2
std/haxe/macro/ExprTools.hx

@@ -130,7 +130,8 @@ class ExprTools {
 					f(edef);
 			case EFormat(parts):
 				for (part in parts) switch part.kind {
-					case FRaw(_) | FIdent(_):
+					case FRaw(_):
+					case FIdent(i): f({pos: part.pos, expr: EConst(CIdent(i))});
 					case FExpr(e): f(e);
 				}
 		}
@@ -215,7 +216,13 @@ class ExprTools {
 			case EMeta(m, e): EMeta(m, f(e));
 			case EFormat(parts):
 				EFormat([for (part in parts) switch part.kind {
-					case FRaw(_) | FIdent(_): part;
+					case FRaw(_): part;
+					case FIdent(i):
+						var e = f({pos: part.pos, expr: EConst(CIdent(i))});
+						switch e.expr {
+							case EConst(CIdent(i)): {kind: FIdent(i), pos: e.pos};
+							case _: {kind: FExpr(e), pos: e.pos};
+						}
 					case FExpr(e): {kind: FExpr(f(e)), pos: part.pos};
 				}]);
 		}};