ソースを参照

[parser] disallow empty `var` and `try` without `catch` (closes #3699)

Simon Krajewski 10 年 前
コミット
b84ca37d1d

+ 8 - 2
parser.ml

@@ -1074,7 +1074,10 @@ and block acc s =
 			block acc s
 			block acc s
 
 
 and parse_block_elt = parser
 and parse_block_elt = parser
-	| [< '(Kwd Var,p1); vl = psep Comma parse_var_decl; p2 = semicolon >] -> (EVars vl,punion p1 p2)
+	| [< '(Kwd Var,p1); vl = psep Comma parse_var_decl; p2 = semicolon >] ->
+		(match vl with
+			| [] -> error (Custom "Missing variable identifier") p1
+			| _ -> (EVars vl,punion p1 p2))
 	| [< e = expr; _ = semicolon >] -> e
 	| [< e = expr; _ = semicolon >] -> e
 
 
 and parse_obj_decl = parser
 and parse_obj_decl = parser
@@ -1218,7 +1221,10 @@ and expr = parser
 			Display e -> display (EWhile (cond,e,NormalWhile),punion p1 (pos e)))
 			Display e -> display (EWhile (cond,e,NormalWhile),punion p1 (pos e)))
 	| [< '(Kwd Do,p1); e = expr; '(Kwd While,_); '(POpen,_); cond = expr; '(PClose,_); s >] -> (EWhile (cond,e,DoWhile),punion p1 (pos e))
 	| [< '(Kwd Do,p1); e = expr; '(Kwd While,_); '(POpen,_); cond = expr; '(PClose,_); s >] -> (EWhile (cond,e,DoWhile),punion p1 (pos e))
 	| [< '(Kwd Switch,p1); e = expr; '(BrOpen,_); cases , def = parse_switch_cases e []; '(BrClose,p2); s >] -> (ESwitch (e,cases,def),punion p1 p2)
 	| [< '(Kwd Switch,p1); e = expr; '(BrOpen,_); cases , def = parse_switch_cases e []; '(BrClose,p2); s >] -> (ESwitch (e,cases,def),punion p1 p2)
-	| [< '(Kwd Try,p1); e = expr; cl = plist (parse_catch e); s >] -> (ETry (e,cl),p1)
+	| [< '(Kwd Try,p1); e = expr; cl = plist (parse_catch e); >] ->
+		(match cl with
+			| [] -> error (Custom "Expected catch after try") p1
+			| _ -> (ETry (e,cl),p1))
 	| [< '(IntInterval i,p1); e2 = expr >] -> make_binop OpInterval (EConst (Int i),p1) e2
 	| [< '(IntInterval i,p1); e2 = expr >] -> make_binop OpInterval (EConst (Int i),p1) e2
 	| [< '(Kwd Untyped,p1); e = expr >] -> (EUntyped e,punion p1 (pos e))
 	| [< '(Kwd Untyped,p1); e = expr >] -> (EUntyped e,punion p1 (pos e))
 	| [< '(Dollar v,p); s >] -> expr_next (EConst (Ident ("$"^v)),p) s
 	| [< '(Dollar v,p); s >] -> expr_next (EConst (Ident ("$"^v)),p) s

+ 5 - 0
tests/misc/projects/Issue3699/MainTry.hx

@@ -0,0 +1,5 @@
+class MainTry {
+	static public function main() {
+		try { "foo"; }
+	}
+}

+ 5 - 0
tests/misc/projects/Issue3699/MainVar.hx

@@ -0,0 +1,5 @@
+class MainVar {
+	static public function main() {
+		var tmp = ;
+	}
+}

+ 1 - 0
tests/misc/projects/Issue3699/compile-try-fail.hxml

@@ -0,0 +1 @@
+--run MainTry

+ 1 - 0
tests/misc/projects/Issue3699/compile-try-fail.hxml.stderr

@@ -0,0 +1 @@
+MainTry.hx:3: characters 2-5 : Expected catch after try

+ 1 - 0
tests/misc/projects/Issue3699/compile-var-fail.hxml

@@ -0,0 +1 @@
+--run MainVar

+ 1 - 0
tests/misc/projects/Issue3699/compile-var-fail.hxml.stderr

@@ -0,0 +1 @@
+MainVar.hx:3: characters 2-5 : Missing variable identifier