Improve parsing of % followed immediately by identifier

If it's not a recognised directive the scanner now emits MODULO and then
rescans what follows, and if the parser then gives a syntax error we
report it as an unknown directive. This means that `a%b` is now allowed
in an expression, and that things like `%std::vector<std::string>` now
give an error rather than being quietly ignored.

Fixes #300
Fixes #368
This commit is contained in:
Olly Betts 2022-02-15 15:03:36 +13:00
commit a7ff0da1f3
7 changed files with 43 additions and 7 deletions

View file

@ -1031,8 +1031,23 @@ int yylex(void) {
if (strcmp(yytext, "%warn") == 0)
return (WARN);
/* Note down the apparently unknown directive for error reporting. */
/* Note down the apparently unknown directive for error reporting - if
* we end up reporting a generic syntax error we'll instead report an
* error for his as an unknown directive. Then we treat it as MODULO
* (`%`) followed by an identifier and if that parses OK then
* `cparse_unknown_directive` doesn't get used.
*
* This allows `a%b` to be handled in expressions without a space after
* the operator.
*/
cparse_unknown_directive = NewString(yytext);
String *stext = NewString(yytext + 1);
Seek(stext,0,SEEK_SET);
Setfile(stext,cparse_file);
Setline(stext,cparse_line);
Scanner_push(scan,stext);
Delete(stext);
return (MODULO);
}
/* Have an unknown identifier, as a last step, we'll do a typedef lookup on it. */