From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1499) id 57A443858C39; Tue, 24 Jan 2023 19:21:45 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 57A443858C39 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1674588105; bh=haxXJfxpJxXzZyap1H9vMwi5pImC59asXkn7+ClIo1I=; h=From:To:Subject:Date:From; b=rXOxBxdeFVFQ7KGCTQNCdrRQN1Utl3nm7df/yC9yILzmPnW5f91hHaBkCf3rz5ZQJ KcELjveqYWoWcLchapPx8K2dir9/fonfgVOhbOHWDAoFP9rgqWNBn+P29tAMKpkHsh io0CT0tHWcTPqswfOdq+5xJsJtqwX4NDC81+eGl0= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Gaius Mulley To: gcc-cvs@gcc.gnu.org Subject: [gcc r13-5329] Change m2 lexical analysis to optionally consume C comments. X-Act-Checkin: gcc X-Git-Author: Gaius Mulley X-Git-Refname: refs/heads/master X-Git-Oldrev: b061fc94d70dc82f554eeb94434cf03cd5af03f7 X-Git-Newrev: 96fd01679011379d6da0777e435078212c6bb325 Message-Id: <20230124192145.57A443858C39@sourceware.org> Date: Tue, 24 Jan 2023 19:21:45 +0000 (GMT) List-Id: https://gcc.gnu.org/g:96fd01679011379d6da0777e435078212c6bb325 commit r13-5329-g96fd01679011379d6da0777e435078212c6bb325 Author: Gaius Mulley 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. (.): New rule to skip a character. (\n.*): New rule to consume the line. ("*/"): New rule to call endOfCComment. Signed-off-by: Gaius Mulley 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); <> { poperrorskip("unterminated source code directive, missing *>"); BEGIN COMMENT; } <> { 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 (); + } +. { updatepos(); skippos(); } +\n.* { consumeLine(); } +"*/" { endOfCComment(); } ^\#.* { consumeLine(); /* printf("found: %s\n", currentLine->linebuf); */ BEGIN LINE0; } \n\#.* { consumeLine(); /* printf("found: %s\n", currentLine->linebuf); */ BEGIN 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.