Browse Source

s_expr: proper indent

Gama11 10 years ago
parent
commit
1c8e805c37
1 changed files with 84 additions and 81 deletions
  1. 84 81
      ast.ml

+ 84 - 81
ast.ml

@@ -746,108 +746,111 @@ let map_expr loop (e,p) =
 	(e,p)
 
 let s_expr e =
-	let rec s_expr_inner (e,_) =
+	let rec s_expr_inner tabs (e,_) =
 		match e with
 		| EConst c -> s_constant c
-		| EArray (e1,e2) -> s_expr_inner e1 ^ "[" ^ s_expr_inner e2 ^ "]"
-		| EBinop (op,e1,e2) -> s_expr_inner e1 ^ s_binop op ^ s_expr_inner e2
-		| EField (e,f) -> s_expr_inner e ^ "." ^ f
-		| EParenthesis e -> "(" ^ (s_expr_inner e) ^ ")"
-		| EObjectDecl fl -> "{" ^ (String.concat "," (List.map (fun (n,e) -> n ^ ":" ^ (s_expr_inner e)) fl)) ^ "}"
-		| EArrayDecl el -> "[" ^ s_expr_list el ", " ^ "]"
-		| ECall (e,el) -> s_expr_inner e ^ "(" ^ s_expr_list el ", " ^ ")"
-		| ENew (t,el) -> "new " ^ s_complex_type_path t ^ "(" ^ s_expr_list el ", " ^ ")"
-		| EUnop (op,Postfix,e) -> s_expr_inner e ^ s_unop op
-		| EUnop (op,Prefix,e) -> s_unop op ^ s_expr_inner e
-		| EFunction (Some n,f) -> "function " ^ n ^ s_func f
-		| EFunction (None,f) -> "function" ^ s_func f
-		| EVars vl -> "var " ^ String.concat ", " (List.map s_var vl)
+		| EArray (e1,e2) -> s_expr_inner tabs e1 ^ "[" ^ s_expr_inner tabs e2 ^ "]"
+		| EBinop (op,e1,e2) -> s_expr_inner tabs e1 ^ s_binop op ^ s_expr_inner tabs e2
+		| EField (e,f) -> s_expr_inner tabs e ^ "." ^ f
+		| EParenthesis e -> "(" ^ (s_expr_inner tabs e) ^ ")"
+		| EObjectDecl fl -> "{" ^ (String.concat "," (List.map (fun (n,e) -> n ^ ":" ^ (s_expr_inner tabs e)) fl)) ^ "}"
+		| EArrayDecl el -> "[" ^ s_expr_list tabs el ", " ^ "]"
+		| ECall (e,el) -> s_expr_inner tabs e ^ "(" ^ s_expr_list tabs el ", " ^ ")"
+		| ENew (t,el) -> "new " ^ s_complex_type_path tabs t ^ "(" ^ s_expr_list tabs el ", " ^ ")"
+		| EUnop (op,Postfix,e) -> s_expr_inner tabs e ^ s_unop op
+		| EUnop (op,Prefix,e) -> s_unop op ^ s_expr_inner tabs e
+		| EFunction (Some n,f) -> "function " ^ n ^ s_func tabs f
+		| EFunction (None,f) -> "function" ^ s_func tabs f
+		| EVars vl -> "var " ^ String.concat ", " (List.map (s_var tabs) vl)
 		| EBlock [] -> "{ }"
-		| EBlock el -> "{\n\t" ^ s_expr_list el ";\n\t" ^ ";\n\t}"
-		| EFor (e1,e2) -> "for (" ^ s_expr_inner e1 ^ ") " ^ s_expr_inner e2
-		| EIn (e1,e2) -> s_expr_inner e1 ^ " in " ^ s_expr_inner e2
-		| EIf (e,e1,None) -> "if (" ^ s_expr_inner e ^ ") " ^ s_expr_inner e1
-		| EIf (e,e1,Some e2) -> "if (" ^ s_expr_inner e ^ ") " ^ s_expr_inner e1 ^ " else " ^ s_expr_inner e2
-		| EWhile (econd,e,NormalWhile) -> "while (" ^ s_expr_inner econd ^ ") " ^ s_expr_inner e
-		| EWhile (econd,e,DoWhile) -> "do " ^ s_expr_inner e ^ " while (" ^ s_expr_inner econd ^ ")"
-		| ESwitch (e,cases,def) -> "switch " ^ s_expr_inner e ^ "{\n\t" ^ String.concat "\n\t" (List.map s_case cases) ^
-			(match def with None -> "" | Some def -> "\n\tdefault:" ^ (match def with None -> "" | Some def -> s_expr_inner def ^ ";")) ^ "\n\t}" 
-		| ETry (e,catches) -> "try " ^ s_expr_inner e ^ String.concat "" (List.map s_catch catches)
-		| EReturn e -> "return" ^ s_opt_expr e " "
+		| EBlock el -> "{\n\t" ^ tabs ^ (s_expr_list (tabs ^ "\t") el (";\n\t" ^ tabs)) ^ ";\n" ^ tabs ^"}"
+		| EFor (e1,e2) -> "for (" ^ s_expr_inner tabs e1 ^ ") " ^ s_expr_inner tabs e2
+		| EIn (e1,e2) -> s_expr_inner tabs e1 ^ " in " ^ s_expr_inner tabs e2
+		| EIf (e,e1,None) -> "if (" ^ s_expr_inner tabs e ^ ") " ^ s_expr_inner tabs e1
+		| EIf (e,e1,Some e2) -> "if (" ^ s_expr_inner tabs e ^ ") " ^ s_expr_inner tabs e1 ^ " else " ^ s_expr_inner tabs e2
+		| EWhile (econd,e,NormalWhile) -> "while (" ^ s_expr_inner tabs econd ^ ") " ^ s_expr_inner tabs e
+		| EWhile (econd,e,DoWhile) -> "do " ^ s_expr_inner tabs e ^ " while (" ^ s_expr_inner tabs econd ^ ")"
+		| ESwitch (e,cases,def) -> "switch " ^ s_expr_inner tabs e ^ " {\n\t" ^ tabs ^ String.concat ("\n\t" ^ tabs) (List.map (s_case tabs) cases) ^
+			(match def with None -> "" | Some def -> "\n\t" ^ tabs ^ "default:" ^
+			(match def with None -> "" | Some def -> s_expr_inner (tabs ^ "\t") def ^ ";")) ^ "\n" ^ tabs ^ "}" 
+		| ETry (e,catches) -> "try " ^ s_expr_inner tabs e ^ String.concat "" (List.map (s_catch tabs) catches)
+		| EReturn e -> "return" ^ s_opt_expr tabs e " "
 		| EBreak -> "break"
 		| EContinue -> "continue"
-		| EUntyped e -> "untyped " ^ s_expr_inner e
-		| EThrow e -> "throw " ^ s_expr_inner e
-		| ECast (e,Some t) -> "cast (" ^ s_expr_inner e ^ ", " ^ s_complex_type t ^ ")"
-		| ECast (e,None) -> "cast " ^ s_expr_inner e
-		| ETernary (e1,e2,e3) -> s_expr_inner e1 ^ " ? " ^ s_expr_inner e2 ^ " : " ^ s_expr_inner e3
-		| ECheckType (e,t) -> "(" ^ s_expr_inner e ^ " : " ^ s_complex_type t ^ ")"
-		| EMeta (m,e) -> s_metadata m ^ " " ^ s_expr_inner e
+		| EUntyped e -> "untyped " ^ s_expr_inner tabs e
+		| EThrow e -> "throw " ^ s_expr_inner tabs e
+		| ECast (e,Some t) -> "cast (" ^ s_expr_inner tabs e ^ ", " ^ s_complex_type tabs t ^ ")"
+		| ECast (e,None) -> "cast " ^ s_expr_inner tabs e
+		| ETernary (e1,e2,e3) -> s_expr_inner tabs e1 ^ " ? " ^ s_expr_inner tabs e2 ^ " : " ^ s_expr_inner tabs e3
+		| ECheckType (e,t) -> "(" ^ s_expr_inner tabs e ^ " : " ^ s_complex_type tabs t ^ ")"
+		| EMeta (m,e) -> s_metadata tabs m ^ " " ^ s_expr_inner tabs e
 		| _ -> ""
-	and s_expr_list el sep =
-		(String.concat sep (List.map s_expr_inner el))
-	and s_complex_type_path t =	
+	and s_expr_list tabs el sep =
+		(String.concat sep (List.map (s_expr_inner tabs) el))
+	and s_complex_type_path tabs t =	
 		(String.concat "." t.tpackage) ^ if List.length t.tpackage > 0 then "." else "" ^
 		t.tname ^
 		match t.tsub with
 		| Some s -> "." ^ s
 		| None -> "" ^
-		s_type_param_or_consts t.tparams
-	and s_type_param_or_consts pl =
+		s_type_param_or_consts tabs t.tparams
+	and s_type_param_or_consts tabs pl =
 		if List.length pl > 0
-		then "<" ^ (String.concat "," (List.map s_type_param_or_const pl)) ^ ">"
+		then "<" ^ (String.concat "," (List.map (s_type_param_or_const tabs) pl)) ^ ">"
 		else ""
-	and s_type_param_or_const = function
-		| TPType t -> s_complex_type t
-		| TPExpr e -> s_expr_inner e
-	and s_complex_type = function
-		| CTPath t -> s_complex_type_path t
-		| CTFunction (cl,c) -> if List.length cl > 0 then String.concat " -> " (List.map s_complex_type cl) else "Void" ^ " -> " ^ s_complex_type c
-		| CTAnonymous fl -> "{ " ^ String.concat "; " (List.map s_class_field fl) ^ "}";
-		| CTParent t -> "(" ^ s_complex_type t ^ ")"
-		| CTOptional t -> "?" ^ s_complex_type t
-		| CTExtend (tl, fl) -> "{> " ^ String.concat " >, " (List.map s_complex_type_path tl) ^ ", " ^ String.concat ", " (List.map s_class_field fl) ^ " }"
-	and s_class_field f =
+	and s_type_param_or_const tabs p =
+		match p with
+		| TPType t -> s_complex_type tabs t
+		| TPExpr e -> s_expr_inner tabs e
+	and s_complex_type tabs ct =
+		match ct with
+		| CTPath t -> s_complex_type_path tabs t
+		| CTFunction (cl,c) -> if List.length cl > 0 then String.concat " -> " (List.map (s_complex_type tabs) cl) else "Void" ^ " -> " ^ s_complex_type tabs c
+		| CTAnonymous fl -> "{ " ^ String.concat "; " (List.map (s_class_field tabs) fl) ^ "}";
+		| CTParent t -> "(" ^ s_complex_type tabs t ^ ")"
+		| CTOptional t -> "?" ^ s_complex_type tabs t
+		| CTExtend (tl, fl) -> "{> " ^ String.concat " >, " (List.map (s_complex_type_path tabs) tl) ^ ", " ^ String.concat ", " (List.map (s_class_field tabs) fl) ^ " }"
+	and s_class_field tabs f =
 		match f.cff_doc with
-		| Some s -> "/**\n\t" ^ s ^ "\n**/\n"
+		| Some s -> "/**\n\t" ^ tabs ^ s ^ "\n**/\n"
 		| None -> "" ^
-		if List.length f.cff_meta > 0 then String.concat "\n\t" (List.map s_metadata f.cff_meta) else "" ^
+		if List.length f.cff_meta > 0 then String.concat ("\n" ^ tabs) (List.map (s_metadata tabs) f.cff_meta) else "" ^
 		if List.length f.cff_access > 0 then String.concat " " (List.map s_access f.cff_access) else "" ^
 		match f.cff_kind with
-		| FVar (t,e) -> "var " ^ f.cff_name ^ s_opt_complex_type t " : " ^ s_opt_expr e " = "
-		| FProp (get,set,t,e) -> "var " ^ f.cff_name ^ "(" ^ get ^ "," ^ set ^ ")" ^ s_opt_complex_type t " : " ^ s_opt_expr e " = "
-		| FFun func -> "function " ^ f.cff_name ^ s_func func
-	and s_metadata (s,e,_) =
-		"@" ^ Meta.to_string s ^ if List.length e > 0 then "(" ^ s_expr_list e ", " ^ ")" else ""
-	and s_opt_complex_type t pre =
+		| FVar (t,e) -> "var " ^ f.cff_name ^ s_opt_complex_type tabs t " : " ^ s_opt_expr tabs e " = "
+		| FProp (get,set,t,e) -> "var " ^ f.cff_name ^ "(" ^ get ^ "," ^ set ^ ")" ^ s_opt_complex_type tabs t " : " ^ s_opt_expr tabs e " = "
+		| FFun func -> "function " ^ f.cff_name ^ s_func tabs func
+	and s_metadata tabs (s,e,_) =
+		"@" ^ Meta.to_string s ^ if List.length e > 0 then "(" ^ s_expr_list tabs e ", " ^ ")" else ""
+	and s_opt_complex_type tabs t pre =
 		match t with
-		| Some s -> pre ^ s_complex_type s
+		| Some s -> pre ^ s_complex_type tabs s
 		| None -> ""
-	and s_opt_expr e pre =
+	and s_opt_expr tabs e pre =
 		match e with
-		| Some s -> pre ^ s_expr_inner s
+		| Some s -> pre ^ s_expr_inner tabs s
 		| None -> ""
-	and s_func f =
-		s_type_param_list f.f_params ^
-		"(" ^ String.concat ", " (List.map s_func_arg f.f_args) ^ ")" ^
-		s_opt_complex_type f.f_type ":" ^
-		s_opt_expr f.f_expr " "
-	and s_type_param t =
-		t.tp_name ^ s_type_param_list t.tp_params ^
-		if List.length t.tp_constraints > 0 then ":(" ^ String.concat ", " (List.map s_complex_type t.tp_constraints) ^ ")" else ""
-	and s_type_param_list tl =
-		if List.length tl > 0 then "<" ^ String.concat ", " (List.map s_type_param tl) ^ ">" else ""
-	and s_func_arg (n,o,t,e) =
-		if o then "?" else "" ^ n ^ s_opt_complex_type t ":" ^ s_opt_expr e " = "
-	and s_var (n,t,e) =
-		n ^ s_opt_complex_type t ":" ^ s_opt_expr e " = "
-	and s_case (el,e1,e2) =
-		"case " ^ s_expr_list el ", " ^
-		(match e1 with None -> ":" | Some e -> " if (" ^ s_expr_inner e ^ "):") ^
-		(match e2 with None -> "" | Some e -> s_expr_inner e ^ ";")
-	and s_catch (n,t,e) =
-		" catch(" ^ n ^ ":" ^ s_complex_type t ^ ")" ^ s_expr_inner e
-	in s_expr_inner e
+	and s_func tabs f =
+		s_type_param_list tabs f.f_params ^
+		"(" ^ String.concat ", " (List.map (s_func_arg tabs) f.f_args) ^ ")" ^
+		s_opt_complex_type tabs f.f_type ":" ^
+		s_opt_expr tabs f.f_expr " "
+	and s_type_param tabs t =
+		t.tp_name ^ s_type_param_list tabs t.tp_params ^
+		if List.length t.tp_constraints > 0 then ":(" ^ String.concat ", " (List.map (s_complex_type tabs) t.tp_constraints) ^ ")" else ""
+	and s_type_param_list tabs tl =
+		if List.length tl > 0 then "<" ^ String.concat ", " (List.map (s_type_param tabs) tl) ^ ">" else ""
+	and s_func_arg tabs (n,o,t,e) =
+		if o then "?" else "" ^ n ^ s_opt_complex_type tabs t ":" ^ s_opt_expr tabs e " = "
+	and s_var tabs (n,t,e) =
+		n ^ s_opt_complex_type tabs t ":" ^ s_opt_expr tabs e " = "
+	and s_case tabs (el,e1,e2) =
+		"case " ^ s_expr_list tabs el ", " ^
+		(match e1 with None -> ":" | Some e -> " if (" ^ s_expr_inner tabs e ^ "):") ^
+		(match e2 with None -> "" | Some e -> s_expr_inner (tabs ^ "\t") e ^ ";")
+	and s_catch tabs (n,t,e) =
+		" catch(" ^ n ^ ":" ^ s_complex_type tabs t ^ ")" ^ s_expr_inner tabs e
+	in s_expr_inner "" e
 
 let get_value_meta meta =
 	try