Skip to content

Commit

Permalink
Make scanning of C and C++ source even more consistent
Browse files Browse the repository at this point in the history
Use the same code for scanning all input that gets copied to the output.
  • Loading branch information
DemiMarie committed Dec 3, 2018
1 parent 20c91b4 commit a6a3160
Showing 1 changed file with 27 additions and 20 deletions.
47 changes: 27 additions & 20 deletions src/scan.l
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,24 @@ extern bool tablesverify, tablesext;
extern int trlcontxt; /* Set in parse.y for each rule. */
extern const char *escaped_qstart, *escaped_qend;


static bool write_to_buf;
#define M4QSTART "[""["
#define M4QEND "]""]"

#define ESCAPED_QSTART "[" M4QEND M4QSTART "[" M4QEND M4QSTART
#define ESCAPED_QEND M4QEND "]" M4QSTART M4QEND "]" M4QSTART

#define ACTION_ECHO do add_action(yytext); while(0)
#define ACTION_ECHO \
do \
if (strlen(yytext) != yyleng) \
synerr(_( "NUL character in input file")); \
else if (write_to_buf) \
buf_strnappend(&top_buf, yytext, yyleng); \
else \
add_action(yytext); \
while(0)

#define ACTION_IFDEF(def, should_define) \
do if (should_define) action_define(def, 1); while (0)

Expand Down Expand Up @@ -175,6 +186,7 @@ M4QEND "]""]"
++linenum;
buf_linedir( &top_buf, infilename?infilename:"<stdin>", linenum);
brace_depth = 1;
write_to_buf = true;
yy_push_state(CODEBLOCK_MATCH_BRACE);
}

Expand Down Expand Up @@ -226,19 +238,13 @@ M4QEND "]""]"
<COMMENT,CODE_COMMENT>{ /* */
[^\[\]\*\n]* ACTION_ECHO;
. ACTION_ECHO;

{NL} ++linenum; ACTION_ECHO;
}
<COMMENT>{
"*/" add_action("*/]""]"); yy_pop_state();
}
<CODE_COMMENT>{
"*"{INVALID_ESCAPED_NEWLINE}+"/" {
synerr(_("Escaped newline in comment end sequence\n"));
}

"*/" ACTION_ECHO; yy_pop_state();
{NL} ++linenum; ACTION_ECHO;
}
<COMMENT>"*/" add_action("*/]""]"); yy_pop_state();
<CODE_COMMENT>"*/" ACTION_ECHO; yy_pop_state();

<COMMENT_DISCARD>{
/* This is the same as COMMENT, but is discarded rather than output. */
Expand Down Expand Up @@ -291,6 +297,7 @@ M4QEND "]""]"
<CODEBLOCK_MATCH_BRACE>{
"}" {
if( --brace_depth == 0){
write_to_buf = false;
/* TODO: Matched. */
yy_pop_state();
}else
Expand All @@ -309,9 +316,6 @@ M4QEND "]""]"

{M4QSTART} buf_strnappend(&top_buf, escaped_qstart, (int) strlen(escaped_qstart));
{M4QEND} buf_strnappend(&top_buf, escaped_qend, (int) strlen(escaped_qend));
([^{}\r\n\[\]]+)|[^{}\r\n] {
buf_strnappend(&top_buf, yytext, yyleng);
}

<<EOF>> {
linenum = brace_start_line;
Expand Down Expand Up @@ -909,13 +913,6 @@ nmstr[yyleng - 2 - end_is_ws] = '\0'; /* chop trailing brace */
}
}
<CODEBLOCK,ACTION,PERCENT_BRACE_ACTION>{
"/"{INVALID_ESCAPED_NEWLINE}+"*" {
synerr(_("Invalid escaped newline in comment-open-sequence"));
ACTION_ECHO;
yy_push_state(CODE_COMMENT);
}
"/*" ACTION_ECHO; yy_push_state(CODE_COMMENT);

"reject" {
ACTION_ECHO;
CHECK_REJECT(yytext);
Expand All @@ -924,6 +921,16 @@ nmstr[yyleng - 2 - end_is_ws] = '\0'; /* chop trailing brace */
ACTION_ECHO;
CHECK_YYMORE(yytext);
}
}

<CODEBLOCK,ACTION,PERCENT_BRACE_ACTION,CODEBLOCK_MATCH_BRACE>{
"/"{INVALID_ESCAPED_NEWLINE}+"*" {
synerr(_("Invalid escaped newline in comment-open-sequence"));
ACTION_ECHO;
yy_push_state(CODE_COMMENT);
}
"/*" ACTION_ECHO; yy_push_state(CODE_COMMENT);

[^[:alpha:]_{}""''/\n\[\]]+ ACTION_ECHO;
{NAME} ACTION_ECHO;
"'"([^''\\\n]|\\.)"'" ACTION_ECHO; /* character constant */
Expand Down

0 comments on commit a6a3160

Please sign in to comment.