Skip to content

Commit fdad391

Browse files
committed
clean up/fix %include parsing
- allow spaces in filename, but strip trailing whitespace (including \r) - proper error for missing filename
1 parent 1c63c67 commit fdad391

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed

reader.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,13 @@ char *get_line() {
188188
if(strncmp(&line[0], "%include ", 9)==0) {
189189
char *name = line+9;
190190
while (isspace(*name)) name++;
191-
for(i=0; name[i]!='\n' && name[i]!=' '; i++);
191+
i = strlen(name);
192+
while (i > 0 && isspace(name[i-1])) --i;
192193
name[i] = 0;
193-
read_from_file(name);
194+
if (i == 0)
195+
error(input_file->lineno, 0, 0, "No file name with %include");
196+
else
197+
read_from_file(name);
194198
goto NextLine;
195199
}
196200

test/f 2.inc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
expr: expr op1 expr %prec LO
2+
| expr op2 expr %prec HI
3+
| unary expr %prec UNARY
4+
;
5+

test/f1.inc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
%left LO '+' '-'
2+
%left HI '*' '/' '%'
3+
%nonassoc UNARY

test/include.y

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* [test] btyacc
3+
* [test] cc -c -Wall -Werror
4+
*
5+
* testing trailing spaces/tabs on %include lines
6+
*/
7+
%include f1.inc
8+
9+
%%
10+
11+
%include f 2.inc
12+
13+
op1 : '+'
14+
| '-'
15+
;
16+
17+
op2 : '*'
18+
| '/'
19+
| '%'
20+
;
21+
22+
unary : '+' | '-' | '*' | '&' ;
23+

0 commit comments

Comments
 (0)