public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Gaius Mulley <gaius@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r13-5329] Change m2 lexical analysis to optionally consume C comments.
Date: Tue, 24 Jan 2023 19:21:45 +0000 (GMT)	[thread overview]
Message-ID: <20230124192145.57A443858C39@sourceware.org> (raw)

https://gcc.gnu.org/g:96fd01679011379d6da0777e435078212c6bb325

commit r13-5329-g96fd01679011379d6da0777e435078212c6bb325
Author: Gaius Mulley <gaiusmod2@gmail.com>
Date:   Tue Jan 24 19:21:20 2023 +0000

    Change m2 lexical analysis to optionally consume C comments.
    
    This patch allows a subsequent patch to turn on/off the consuming
    of C comments.
    
    gcc/m2/ChangeLog:
    
            * m2.flex (cpreprocessor): Add temporary variable
            which is initialized to 0.
            (commentCLevel): New variable.
            (endOfCComment): New function.
            (splitSlashStar): New function to split /* into / and *
            tokens.
            (COMMENTC): New flex state.
            ("/*"): New rule to test whether we should treat /*
            as a single token or as two tokens.
            (<COMMENTC>.): New rule to skip a character.
            (<COMMENTC>\n.*): New rule to consume the line.
            (<COMMENTC>"*/"): New rule to call endOfCComment.
    
    Signed-off-by: Gaius Mulley <gaiusmod2@gmail.com>

Diff:
---
 gcc/m2/m2.flex | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 69 insertions(+), 1 deletion(-)

diff --git a/gcc/m2/m2.flex b/gcc/m2/m2.flex
index 15f3caf900a..7b8f4f17f42 100644
--- a/gcc/m2/m2.flex
+++ b/gcc/m2/m2.flex
@@ -27,6 +27,7 @@ along with GNU Modula-2; see the file COPYING3.  If not see
 #include "input.h"
 #include "m2options.h"
 
+static int cpreprocessor = 0;  /* Replace this with correct getter.  */
 
 #if defined(GM2USEGGC)
 #  include "ggc.h"
@@ -70,6 +71,7 @@ along with GNU Modula-2; see the file COPYING3.  If not see
   static int                  lineno      =1;   /* a running count of the file line number */
   static char                *filename    =NULL;
   static int                  commentLevel=0;
+  static int                  commentCLevel=0;
   static struct lineInfo     *currentLine=NULL;
   static struct functionInfo *currentFunction=NULL;
   static int                  seenFunctionStart=FALSE;
@@ -87,6 +89,8 @@ static  void updatepos                (void);
 static  void skippos                  (void);
 static  void poperrorskip             (const char *);
 static  void endOfComment             (void);
+static  void endOfCComment            (void);
+static  void splitSlashStar           (void);
 static  void handleDate               (void);
 static  void handleLine               (void);
 static  void handleFile               (void);
@@ -117,7 +121,7 @@ extern  void  yylex                   (void);
 %}
 
 %option nounput
-%x COMMENT COMMENT1 LINE0 LINE1 LINE2
+%x COMMENT COMMENT1 COMMENTC LINE0 LINE1 LINE2
 
 %%
 
@@ -145,6 +149,24 @@ extern  void  yylex                   (void);
 <COMMENT1><<EOF>>          { poperrorskip("unterminated source code directive, missing *>"); BEGIN COMMENT; }
 <COMMENT><<EOF>>           { poperrorskip("unterminated comment found at the end of the file, missing *)"); BEGIN INITIAL; }
 
+"/*"                       { /* Possibly handle C preprocessor comment.  */
+                             if (cpreprocessor)
+			       {
+				 updatepos ();
+				 commentCLevel++;
+				 if (commentCLevel == 1)
+				   {
+				     pushLine ();
+				     skippos ();
+				   }
+				 BEGIN COMMENTC;
+			       }
+			     else
+			       splitSlashStar ();
+                           }
+<COMMENTC>.                { updatepos(); skippos(); }
+<COMMENTC>\n.*             { consumeLine(); }
+<COMMENTC>"*/"             { endOfCComment(); }
 ^\#.*                      { consumeLine(); /* printf("found: %s\n", currentLine->linebuf); */ BEGIN LINE0; }
 \n\#.*                     { consumeLine(); /* printf("found: %s\n", currentLine->linebuf); */ BEGIN LINE0; }
 <LINE0>\#[ \t]*            { updatepos(); }
@@ -436,6 +458,19 @@ static void endOfComment (void)
     popLine();
 }
 
+/*
+ *  endOfCComment - handles the end of C comment.
+ */
+
+static void endOfCComment (void)
+{
+  commentCLevel = 0;
+  updatepos();
+  skippos();
+  BEGIN INITIAL;
+  finishedLine();
+}
+
 /*
  *  m2flex_M2Error - displays the error message, s, after the code line and pointer
  *                   to the erroneous token.
@@ -504,6 +539,39 @@ static void assert_location (location_t location ATTRIBUTE_UNUSED)
 #endif
 }
 
+/*
+ *  splitSlashStar - called if we are not tokenizing source code after it
+ *                   has been preprocessed by cpp.  It is only called
+ *                   if the current token was /* and therefore it will
+ *                   be split into two m2 tokens:  / and *.
+ */
+
+static void splitSlashStar (void)
+{
+  seenFunctionStart    = FALSE;
+  seenEnd              = FALSE;
+  seenModuleStart      = FALSE;
+  currentLine->nextpos = currentLine->tokenpos+1;  /* "/".  */
+  currentLine->toklen  = 1;
+  currentLine->column = currentLine->tokenpos+1;
+  currentLine->location =
+    M2Options_OverrideLocation (GET_LOCATION (currentLine->column,
+                                              currentLine->column+currentLine->toklen-1));
+  assert_location (GET_LOCATION (currentLine->column,
+                                 currentLine->column+currentLine->toklen-1));
+  M2LexBuf_AddTok (M2Reserved_dividetok);
+  currentLine->nextpos = currentLine->tokenpos+1;  /* "*".  */
+  currentLine->toklen  = 1;
+  currentLine->column = currentLine->tokenpos+1;
+  currentLine->location =
+    M2Options_OverrideLocation (GET_LOCATION (currentLine->column,
+                                              currentLine->column+currentLine->toklen-1));
+  assert_location (GET_LOCATION (currentLine->column,
+                                 currentLine->column+currentLine->toklen-1));
+  M2LexBuf_AddTok (M2Reserved_timestok);
+}
+
+
 /*
  *  updatepos - updates the current token position.
  *              Should be used when a rule matches a token.

                 reply	other threads:[~2023-01-24 19:21 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230124192145.57A443858C39@sourceware.org \
    --to=gaius@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).