Add C99 lineno test.

This commit is contained in:
Eric S. Raymond 2020-10-17 14:52:05 -04:00
parent 07440122ca
commit f735bad052
4 changed files with 123 additions and 0 deletions

2
tests/.gitignore vendored
View File

@ -67,6 +67,8 @@ lineno_nr.one
lineno_nr.c
lineno_r.one
lineno_r.c
lineno_c99.one
lineno_c99.c
lineno_trailing.one
lineno_trailing.c
mem_nr

View File

@ -133,6 +133,7 @@ PTHREAD_TESTS = \
ONE_TESTS = \
lineno_nr.one \
lineno_r.one \
lineno_c99.one \
lineno_trailing.one
quote_in_comment_cpp_SOURCES = quote_in_comment_cpp.l
@ -179,6 +180,7 @@ include_by_push_direct_SOURCES = include_by_push.direct.l
include_by_reentrant_direct_SOURCES = include_by_reentrant.direct.l
lineno_nr_one_SOURCES = lineno_nr.l
lineno_r_one_SOURCES = lineno_r.l
lineno_r_one_SOURCES = lineno_c99.l
lineno_trailing_one_SOURCES = lineno_trailing.l
mem_nr_SOURCES = mem_nr.l
mem_r_SOURCES = mem_r.l
@ -263,6 +265,7 @@ CLEANFILES = \
include_by_reentrant.direct.c \
lineno_nr.c \
lineno_r.c \
lineno_c99.c \
lineno_trailing.c \
mem_nr.c \
mem_r.c \
@ -347,6 +350,7 @@ EXTRA_DIST = \
include_by_reentrant.direct_3.txt \
lineno_nr.one.txt \
lineno_r.one.txt \
lineno_c99.one.txt \
lineno_trailing.one.txt \
mem_nr.txt \
mem_r.txt \

98
tests/lineno_c99.l Normal file
View File

@ -0,0 +1,98 @@
/*
* This file is part of flex.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE.
*/
%{
/* A template scanner file to build "scanner.c".
Run as:
test-lineno-r # report flex's yylineno
test-lineno-r 1 # report count_newlines(stdin)
*/
#include <stdio.h>
#include <stdlib.h>
#include "config.h"
%}
%option emit="c99"
%option 8bit prefix="test"
%option nounput nomain noyywrap noinput yylineno reentrant
%option warn
WORD [[:alpha:]]+
DIGIT [[:digit:]]
%%
/* The goal here is to test the yylineno processing by:
- providing some rules than CAN match newlines and
other rules that can NOT match newlines,
- matching several newlines in one rule,
- directly modifying yylineno.
*/
"yylineno++" yylineno++;
"yylineno--" yylineno--;
[[:blank:]]+
{WORD}
{DIGIT}+(\n{DIGIT}+)*
\n
.
<<EOF>> { printf("%d\n", yylineno);
yyterminate();
}
%%
/* returns number of '\n' characters in input, plus one.
This is what flex does, essentially. */
static int
count_newlines (FILE* in)
{
int n=1,c;
while ((c=fgetc(in)) != EOF)
if( c == '\n')
n++;
return n;
}
int main ( int argc, char** argv );
int
main (int argc, char **argv)
{
(void)argv;
if( argc > 1 )
printf("%d\n", count_newlines(stdin));
else{
yyscan_t s;
testlex_init(&s);
testset_in(stdin,s);
testset_out(stdout,s);
testlex(s);
testlex_destroy(s);
}
return 0;
}

19
tests/lineno_c99.one.txt Normal file
View File

@ -0,0 +1,19 @@
These words
are separated
by newlines
and sometimes
spaces
too.
The next three lines are numbers with only intervening newlines
01123
581321
34
And now for some blank lines....
Finally, we directly modify yylineno, but then change it back afterwards
(see scanner.l):
yylineno++
yylineno--