]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
PR modula2/114520 Incorrect ordering of import/export statements cause confusion
authorGaius Mulley <gaiusmod2@gmail.com>
Thu, 28 Mar 2024 16:49:44 +0000 (16:49 +0000)
committerGaius Mulley <gaiusmod2@gmail.com>
Thu, 28 Mar 2024 16:49:44 +0000 (16:49 +0000)
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 <gaiusmod2@gmail.com>
gcc/m2/gm2-compiler/P0SyntaxCheck.bnf

index c1c86c1827dcd336efe6c8d8ae92c5b25a58fbab..07f861adac9c48dcdeb1af5b5044e19a90ead49a 100644 (file)
@@ -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)