From: Gaius Mulley Date: Thu, 28 Mar 2024 16:49:44 +0000 (+0000) Subject: PR modula2/114520 Incorrect ordering of import/export statements cause confusion X-Git-Tag: basepoints/gcc-15~429 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=aeee63ffbf4f4fbc4d90d8aae808d6b67f0148a3;p=thirdparty%2Fgcc.git PR modula2/114520 Incorrect ordering of import/export statements cause confusion The error recovery causes misleading error messages to appear if an EXPORT and IMPORT statement are in the wrong order. This patch detects the incorrect order and issues an error message and prevents error recovery. The fix should be improved and made more general if another similar case is required. gcc/m2/ChangeLog: PR modula2/114520 * gm2-compiler/P0SyntaxCheck.bnf (DetectImport): New procedure. (EnableImportCheck): New boolean. (Expect): Call DetectImport. (Export): Set EnableImportCheck TRUE before ';' and FALSE afterwards. Signed-off-by: Gaius Mulley --- diff --git a/gcc/m2/gm2-compiler/P0SyntaxCheck.bnf b/gcc/m2/gm2-compiler/P0SyntaxCheck.bnf index c1c86c1827dc..07f861adac9c 100644 --- a/gcc/m2/gm2-compiler/P0SyntaxCheck.bnf +++ b/gcc/m2/gm2-compiler/P0SyntaxCheck.bnf @@ -82,9 +82,10 @@ CONST (* giving up. *) VAR - seenError : BOOLEAN ; - LastIdent : Name ; - InsertCount: CARDINAL ; + EnableImportCheck, + seenError : BOOLEAN ; + LastIdent : Name ; + InsertCount : CARDINAL ; PROCEDURE ErrorString (s: String) ; @@ -319,6 +320,21 @@ BEGIN END PeepToken ; +(* + DetectImport - checks whether the next token is an import or from and if so + generates an error message. This is called after an export + statement to notify the user that the ordering is incorrect. +*) + +PROCEDURE DetectImport ; +BEGIN + IF (currenttoken = importtok) OR (currenttoken = fromtok) + THEN + ErrorArray ('an {%AkIMPORT} statement must preceed an {%kEXPORT} statement') + END +END DetectImport ; + + (* Expect - *) @@ -328,6 +344,10 @@ BEGIN IF currenttoken=t THEN GetToken ; + IF EnableImportCheck + THEN + DetectImport + END ; IF Pass0 THEN PeepToken (stopset0, stopset1, stopset2) @@ -347,6 +367,7 @@ END Expect ; PROCEDURE CompilationUnit () : BOOLEAN ; BEGIN seenError := FALSE ; + EnableImportCheck := FALSE ; InsertCount := 0 ; FileUnit (SetOfStop0{eoftok}, SetOfStop1{}, SetOfStop2{}) ; RETURN NOT seenError @@ -883,7 +904,9 @@ Priority := "[" ConstExpression "]" =: Export := "EXPORT" ( "QUALIFIED" IdentList | "UNQUALIFIED" IdentList | IdentList - ) ";" =: + ) % EnableImportCheck := TRUE % + ";" % EnableImportCheck := FALSE % + =: Import := "FROM" Ident "IMPORT" IdentList ";" | "IMPORT" % PushTtok (ImportTok, GetTokenNo () -1)