Skip to content

Commit

Permalink
Fix M4 quotation in section 2 prologue and refactor duplicated code
Browse files Browse the repository at this point in the history
  • Loading branch information
DemiMarie authored and westes committed Oct 23, 2016
1 parent 802cd0a commit b02489f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 22 deletions.
44 changes: 25 additions & 19 deletions src/scan.l
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ extern const char *escaped_qstart, *escaped_qend;
#define M4QSTART "[""["
#define M4QEND "]""]"

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

#define ACTION_ECHO add_action( yytext )
#define ACTION_IFDEF(def, should_define) \
Expand All @@ -51,8 +51,8 @@ extern const char *escaped_qstart, *escaped_qend;
action_define( def, 1 ); \
}

#define ACTION_ECHO_QSTART add_action (SECT3_ESCAPED_QSTART)
#define ACTION_ECHO_QEND add_action (SECT3_ESCAPED_QEND)
#define ACTION_ECHO_QSTART add_action (ESCAPED_QSTART)
#define ACTION_ECHO_QEND add_action (ESCAPED_QEND)

#define ACTION_M4_IFDEF(def, should_define) \
do{ \
Expand All @@ -75,13 +75,13 @@ extern const char *escaped_qstart, *escaped_qend;
if(yyleng < MAXLINE) \
{ \
strcpy( nmstr, yytext ); \
return NAME; \
} \
else \
{ \
do { \
synerr(_("Input line too long\n")); \
FLEX_EXIT(EXIT_FAILURE); \
} \
return NAME;
} while (0)

#define PUT_BACK_STRING(str, start) \
{ size_t i = strlen( str ); \
Expand All @@ -101,10 +101,21 @@ extern const char *escaped_qstart, *escaped_qend;
if ( getenv("POSIXLY_CORRECT") ) \
posix_compat = true;

#define START_CODEBLOCK do { add_action(M4QSTART); BEGIN(CODEBLOCK); } while(0)
#define END_CODEBLOCK do { add_action(M4QEND); BEGIN(INITIAL); } while (0)
#define CODEBLOCK_QSTART "[]""][""[""[]""][""["
#define CODEBLOCK_QEND "]""]""][""[""]""]""][""["
#define START_CODEBLOCK(x) do { \
/* Emit the needed line directive... */\
if (indented_code == false) { \
linenum++; \
line_directive_out(NULL, 1); \
} \
add_action(M4QSTART); \
yy_push_state(CODEBLOCK); \
if ((indented_code = x)) ACTION_ECHO; \
} while(0)
#define END_CODEBLOCK do { \
yy_pop_state();\
add_action(M4QEND); \
if (!indented_code) line_directive_out(NULL, 0);\
} while (0)
%}

%option caseless nodefault noreject stack noyy_top_state
Expand Down Expand Up @@ -153,17 +164,12 @@ M4QEND "]""]"


<INITIAL>{
^{WS} indented_code = true; START_CODEBLOCK;
^{WS} START_CODEBLOCK(true);
^"/*" add_action("/*[""["); yy_push_state( COMMENT );
^#{OPTWS}line{WS} yy_push_state( LINEDIR );
^"%s"{NAME}? return SCDECL;
^"%x"{NAME}? return XSCDECL;
^"%{".*{NL} {
++linenum;
line_directive_out(NULL, 1);
indented_code = false;
START_CODEBLOCK;
}
^"%{".*{NL} START_CODEBLOCK(false);
^"%top"[[:blank:]]*"{"[[:blank:]]*{NL} {
brace_start_line = linenum;
++linenum;
Expand Down Expand Up @@ -478,7 +484,7 @@ COMMENT,CODE_COMMENT>{
^"%{".* ++bracelevel; yyless( 2 ); /* eat only %{ */
^"%}".* --bracelevel; yyless( 2 ); /* eat only %} */

^{WS}.* ACTION_ECHO; /* indented code in prolog */
^{WS} START_CODEBLOCK(true); /* indented code in prolog */

^{NOT_WS}.* { /* non-indented code */
if ( bracelevel <= 0 )
Expand Down
10 changes: 7 additions & 3 deletions tests/quotes.l
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ static int foo (int i){


%%

/* indented code [ 1 ] */
/* indented code [[ 2 ]] */
/* indented code [[[ 3 ]]] */
/* indented code [[[[ 4 ]]]] */
/* indented code ]] unmatched [[ */
a /* action comment [ 1 ] */ ;
b /* action comment [[ 2 ]] */ ;
c /* action comment [[[ 3 ]]] */ ;
Expand All @@ -76,7 +80,7 @@ f return 1+foo(a[b[c[0]]]);
/* action block [[[[ 4 ]]]] TEST_XXX */
/* action block ]] unmatched [[ TEST_XXX */
assert(!strcmp("m4_define(alpha, beta)", "m4_""define(alpha, beta)"));
return 1+foo(a[b[c[0]]]); // TEST_XXX
return 1+foo(a[b[c[0]]]); /* TEST_XXX */
}
%%

Expand All @@ -94,7 +98,7 @@ int main(void);
int
main (void)
{
// m4_m4exit(100)
/* m4_m4exit(100) */
FILE *M4_YY_NOT_IN_HEADER = stdin;
yyin = CONCAT_IDENTS(M4_, YY_NOT_IN_HEADER);
yyout = stdout;
Expand Down

0 comments on commit b02489f

Please sign in to comment.