https://gcc.gnu.org/g:aeee63ffbf4f4fbc4d90d8aae808d6b67f0148a3
commit r14-9710-gaeee63ffbf4f4fbc4d90d8aae808d6b67f0148a3 Author: Gaius Mulley <gaiusm...@gmail.com> Date: Thu Mar 28 16:49:44 2024 +0000 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 <gaiusm...@gmail.com> Diff: --- gcc/m2/gm2-compiler/P0SyntaxCheck.bnf | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/gcc/m2/gm2-compiler/P0SyntaxCheck.bnf b/gcc/m2/gm2-compiler/P0SyntaxCheck.bnf index c1c86c1827d..07f861adac9 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)