aboutsummaryrefslogtreecommitdiff
path: root/gcc/m2
diff options
context:
space:
mode:
authorGaius Mulley <gaiusmod2@gmail.com>2023-01-24 19:21:20 +0000
committerGaius Mulley <gaiusmod2@gmail.com>2023-01-24 19:21:20 +0000
commit96fd01679011379d6da0777e435078212c6bb325 (patch)
tree8f0b186c25fc0f5f613607158b9f673c20b3f3b0 /gcc/m2
parentb061fc94d70dc82f554eeb94434cf03cd5af03f7 (diff)
downloadgcc-96fd01679011379d6da0777e435078212c6bb325.zip
gcc-96fd01679011379d6da0777e435078212c6bb325.tar.gz
gcc-96fd01679011379d6da0777e435078212c6bb325.tar.bz2
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>
Diffstat (limited to 'gcc/m2')
-rw-r--r--gcc/m2/m2.flex70
1 files changed, 69 insertions, 1 deletions
diff --git a/gcc/m2/m2.flex b/gcc/m2/m2.flex
index 15f3caf..7b8f4f1 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(); }
@@ -437,6 +459,19 @@ static void endOfComment (void)
}
/*
+ * 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.
*/
@@ -505,6 +540,39 @@ static void assert_location (location_t location ATTRIBUTE_UNUSED)
}
/*
+ * 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.
*/