Просмотр исходного кода

[parser] use syntax_error for `final var`

closes #9676
again
Simon Krajewski 2 лет назад
Родитель
Сommit
161dccb3c0

+ 11 - 10
src/syntax/grammar.mly

@@ -113,6 +113,12 @@ let semicolon s =
 		| [< s >] ->
 			syntax_error Missing_semicolon s (next_pos s)
 
+let check_redundant_var p1 = parser
+	| [< '(Kwd Var),p2; s >] ->
+		syntax_error (Custom "`final var` is not supported, use `final` instead") ~pos:(Some (punion p1 p2)) s ();
+	| [< >] ->
+		()
+
 let parsing_macro_cond = ref false
 
 let rec	parse_file s =
@@ -955,9 +961,8 @@ and parse_class_field tdecl s =
 				name,punion p1 p2,FVar (t,e),al,meta
 			end
 		| [< '(Kwd Final,p1) >] ->
+			check_redundant_var p1 s;
 			begin match s with parser
-			| [< '(Kwd Var),p2 >] ->
-				error (Custom "`final var` is not supported, use `final` instead") (punion p1 p2)
 			| [< opt,name = questionable_dollar_ident; t = popt parse_type_hint; e,p2 = parse_var_field_assignment >] ->
 				let meta = check_optional opt name in
 				name,punion p1 p2,FVar(t,e),(al @ [AFinal,p1]),meta
@@ -1140,9 +1145,8 @@ and parse_block_var = parser
 	| [< '(Kwd Var,p1); vl = parse_var_decls false p1; p2 = semicolon >] ->
 		(vl,punion p1 p2)
 	| [< '(Kwd Final,p1); s >] ->
+		check_redundant_var p1 s;
 		match s with parser
-		| [< '(Kwd Var),p2 >] ->
-			error (Custom "`final var` is not supported, use `final` instead") (punion p1 p2)
 		| [< vl = parse_var_decls true p1; p2 = semicolon >] ->
 			(vl,punion p1 p2)
 		| [< >] ->
@@ -1278,9 +1282,8 @@ and parse_macro_expr p = parser
 	| [< '(Kwd Var,p1); vl = psep Comma (parse_var_decl false) >] ->
 		reify_expr (EVars vl,p1) !in_macro
 	| [< '(Kwd Final,p1); s >] ->
+		check_redundant_var p1 s;
 		begin match s with parser
-		| [< '(Kwd Var),p2 >] ->
-			error (Custom "`final var` is not supported, use `final` instead") (punion p1 p2)
 		| [< vl = psep Comma (parse_var_decl true) >] ->
 			reify_expr (EVars vl,p1) !in_macro
 		| [< >] ->
@@ -1401,9 +1404,8 @@ and expr = parser
 		end
 	| [< '(Kwd Var,p1); v = parse_var_decl false >] -> (EVars [v],p1)
 	| [< '(Kwd Final,p1); s >] ->
+		check_redundant_var p1 s;
 		begin match s with parser
-			| [< '(Kwd Var),p2 >] ->
-				error (Custom "`final var` is not supported, use `final` instead") (punion p1 p2)
 			| [< v = parse_var_decl true >] ->
 				(EVars [v],p1)
 			| [< >] ->
@@ -1632,9 +1634,8 @@ and parse_guard = parser
 and expr_or_var = parser
 	| [< '(Kwd Var,p1); np = dollar_ident; >] -> EVars [mk_evar np],punion p1 (snd np)
 	| [< '(Kwd Final,p1); s >] ->
+		check_redundant_var p1 s;
 		begin match s with parser
-			| [< '(Kwd Var),p2 >] ->
-				error (Custom "`final var` is not supported, use `final` instead") (punion p1 p2)
 			| [< np = dollar_ident; >] ->
 				EVars [mk_evar ~final:true np],punion p1 (snd np)
 			| [< >] ->

+ 4 - 2
tests/misc/projects/Issue9676/MainField.hx

@@ -1,3 +1,5 @@
-class Main {
-	final var value:String;
+class MainField {
+	static final var value = "1";
+
+	static function main() {}
 }

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

@@ -1 +0,0 @@
-MainField.hx:2: characters 2-11 : `final var` is not supported, use `final` instead

+ 0 - 0
tests/misc/projects/Issue9676/compile-fail.hxml → tests/misc/projects/Issue9676/compile1-fail.hxml


+ 1 - 0
tests/misc/projects/Issue9676/compile1-fail.hxml.stderr

@@ -0,0 +1 @@
+MainField.hx:2: characters 9-18 : `final var` is not supported, use `final` instead

+ 2 - 0
tests/misc/projects/Issue9676/compile1D.hxml

@@ -0,0 +1,2 @@
+--main MainField
+--display "MainField.hx@0@diagnostics"

+ 1 - 0
tests/misc/projects/Issue9676/compile1D.hxml.stderr

@@ -0,0 +1 @@
+[{"file":"C:\\git\\haxe\\tests\\misc\\projects\\Issue9676\\MainField.hx","diagnostics":[{"kind":4,"severity":1,"range":{"start":{"line":1,"character":8},"end":{"line":1,"character":17}},"args":"`final var` is not supported, use `final` instead"}]}]

+ 2 - 0
tests/misc/projects/Issue9676/compile2D.hxml

@@ -0,0 +1,2 @@
+--main MainExpr
+--display "MainExpr.hx@0@diagnostics"

+ 1 - 0
tests/misc/projects/Issue9676/compile2D.hxml.stderr

@@ -0,0 +1 @@
+[{"file":"C:\\git\\haxe\\tests\\misc\\projects\\Issue9676\\MainExpr.hx","diagnostics":[{"kind":4,"severity":1,"range":{"start":{"line":1,"character":1},"end":{"line":1,"character":10}},"args":"`final var` is not supported, use `final` instead"}]}]

+ 2 - 0
tests/misc/projects/Issue9676/compile3D.hxml

@@ -0,0 +1,2 @@
+--main MainMacro
+--display "MainMacro.hx@0@diagnostics"

+ 1 - 0
tests/misc/projects/Issue9676/compile3D.hxml.stderr

@@ -0,0 +1 @@
+[{"file":"C:\\git\\haxe\\tests\\misc\\projects\\Issue9676\\MainMacro.hx","diagnostics":[{"kind":4,"severity":1,"range":{"start":{"line":1,"character":7},"end":{"line":1,"character":16}},"args":"`final var` is not supported, use `final` instead"}]}]