Skip to content

Commit 11927e4

Browse files
committed
Added a check for new Spin2 keywords in syntax errors
1 parent 8f425c6 commit 11927e4

3 files changed

Lines changed: 49 additions & 3 deletions

File tree

Changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
Version 7.6.0
22
- Added new Spin2_v52 keywords
3+
- Added a warning for missing {Spin2_vXX} comment (very incomplete)
34
- Fixed spin2 \ interaction with ABORT
45
- Fixed initialization of packed structures
56
- Fixed some incorrect file names in zip files

frontends/lexer.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ struct preprocess gl_pp;
5656

5757
// used for error messages
5858
AST *last_ast;
59+
AST *prev_last_ast;
5960

6061
// accumulated comments
6162
static AST *comment_chain;
@@ -1030,6 +1031,7 @@ parseSpinIdentifier(LexStream *L, AST **ast_ptr, const char *prefix)
10301031
if (sym->kind == SYM_TYPEDEF) {
10311032
ast = (AST *)sym->v.ptr;
10321033
*ast_ptr = ast;
1034+
prev_last_ast = last_ast;
10331035
last_ast = AstIdentifier(idstr);
10341036
return SP_TYPENAME;
10351037
}
@@ -1044,6 +1046,7 @@ parseSpinIdentifier(LexStream *L, AST **ast_ptr, const char *prefix)
10441046
open_obj = NULL;
10451047
if (typ->kind == AST_TYPEDEF) {
10461048
*ast_ptr = typ;
1049+
prev_last_ast = last_ast;
10471050
last_ast = AstIdentifier(idstr);
10481051
return SP_TYPENAME;
10491052
}
@@ -1082,6 +1085,22 @@ parseSpinIdentifier(LexStream *L, AST **ast_ptr, const char *prefix)
10821085
return SP_IDENTIFIER;
10831086
}
10841087

1088+
/* helper function for syntax errors; see if a string might be
1089+
a reserved keyword in some version of Spin2
1090+
*/
1091+
bool Spin2NewKeyword(LexStream *L, const char *idstr) {
1092+
Symbol *sym;
1093+
1094+
if (L->language != LANG_SPIN_SPIN2)
1095+
return false;
1096+
sym = FindSymbol(&spin2SoftReservedWords, idstr);
1097+
if (!sym) {
1098+
return false;
1099+
}
1100+
/* could do a more sophisticated check here... */
1101+
return true;
1102+
}
1103+
10851104
/* parse the rest of the line as a string */
10861105
static void
10871106
parseLineAsString(LexStream *L, AST **ast_ptr)
@@ -2012,6 +2031,7 @@ getSpinToken(LexStream *L, AST **ast_ptr)
20122031
case BACKTICK_STATE_ESCAPE_PREFIX:
20132032
lexungetc(L, c);
20142033
L->backtick_state = BACKTICK_STATE_PREFIX_COMMA_DONE;
2034+
prev_last_ast = last_ast;
20152035
*ast_ptr = last_ast = NULL;
20162036
return ',';
20172037
case BACKTICK_STATE_PREFIX_COMMA_DONE:
@@ -2020,6 +2040,7 @@ getSpinToken(LexStream *L, AST **ast_ptr)
20202040
parseBacktickInBacktick(L, &ast);
20212041
c = SP_IDENTIFIER;
20222042
L->backtick_state = BACKTICK_STATE_ESCAPE_PARAMS;
2043+
prev_last_ast = last_ast;
20232044
*ast_ptr = last_ast = ast;
20242045
return c;
20252046
case BACKTICK_STATE_ESCAPE_PARAMS:
@@ -2030,28 +2051,33 @@ getSpinToken(LexStream *L, AST **ast_ptr)
20302051
if (c == ')') {
20312052
// done with the backtick escape
20322053
L->backtick_state = BACKTICK_STATE_INSERT_COMMA;
2054+
prev_last_ast = last_ast;
20332055
*ast_ptr = last_ast = NULL;
20342056
return c;
20352057
}
20362058
break;
20372059
case BACKTICK_STATE_INSERT_COMMA:
20382060
if (c == ')') {
20392061
L->backtick_state = BACKTICK_STATE_NONE;
2062+
prev_last_ast = last_ast;
20402063
*ast_ptr = last_ast = NULL;
20412064
return c;
20422065
}
20432066
lexungetc(L, c);
2067+
prev_last_ast = last_ast;
20442068
*ast_ptr = last_ast = NULL;
20452069
L->backtick_state = BACKTICK_STATE_STRING;
20462070
return ',';
20472071
case BACKTICK_STATE_STRING:
20482072
if (c == ')') {
20492073
L->backtick_state = BACKTICK_STATE_NONE;
2074+
prev_last_ast = last_ast;
20502075
*ast_ptr = last_ast = NULL;
20512076
return c;
20522077
}
20532078
lexungetc(L, c);
20542079
c = parseBacktickString(L, &ast, 0);
2080+
prev_last_ast = last_ast;
20552081
*ast_ptr = last_ast = ast;
20562082
return c;
20572083
case BACKTICK_STATE_NONE:
@@ -2072,6 +2098,7 @@ getSpinToken(LexStream *L, AST **ast_ptr)
20722098

20732099
// printf("L->linecounter=%d\n", L->lineCounter);
20742100
if (c >= 127) {
2101+
prev_last_ast = last_ast;
20752102
*ast_ptr = last_ast = ast;
20762103
return c;
20772104
} else if (safe_isdigit(c) || (c == '.' && safe_isdigit(lexpeekc(L)))) {
@@ -2256,6 +2283,7 @@ getSpinToken(LexStream *L, AST **ast_ptr)
22562283
} else if (c == ')') {
22572284
PopExprState(L);
22582285
}
2286+
prev_last_ast = last_ast;
22592287
*ast_ptr = last_ast = ast;
22602288

22612289
if (c != SP_TYPENAME && c != SP_IDENTIFIER && c != '.')
@@ -5260,3 +5288,4 @@ cgramyylex(CGRAMYYSTYPE *yval)
52605288
return 0;
52615289
return c;
52625290
}
5291+

frontends/spin/spin.y

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@
2323

2424
extern int gl_errors;
2525

26-
extern AST *last_ast;
27-
26+
extern AST *last_ast, *prev_last_ast;
27+
extern bool Spin2NewKeyword(LexStream *L, const char *ident);
28+
2829
AST *
2930
SpinRetType(AST *funcdef)
3031
{
@@ -2998,6 +2999,7 @@ spinyyerror(const char *msg)
29982999
{
29993000
extern int saved_spinyychar;
30003001
int yychar = saved_spinyychar;
3002+
const char *new_keyword = NULL;
30013003

30023004
SETCOLOR(PRINT_ERROR);
30033005
ERRORHEADER(current->Lptr->fileName, current->Lptr->lineCounter, "error");
@@ -3012,6 +3014,9 @@ spinyyerror(const char *msg)
30123014
if (!strncmp(msg, "unexpected identifier", strlen("unexpected identifier")) && last_ast && last_ast->kind == AST_IDENTIFIER) {
30133015
fprintf(stderr, "unexpected identifier `%s'", last_ast->d.string);
30143016
msg += strlen("unexpected identifier");
3017+
if (Spin2NewKeyword(current->Lptr, last_ast->d.string)) {
3018+
new_keyword = last_ast->d.string;
3019+
}
30153020
}
30163021
// if we get a stray character in source, sometimes bison tries to treat it as a token for
30173022
// error purposes, resulting in $undefined as the token
@@ -3028,7 +3033,18 @@ spinyyerror(const char *msg)
30283033
msg++;
30293034
}
30303035
}
3031-
fprintf(stderr, "\n");
3036+
fprintf(stderr, "\n");
30323037
gl_errors++;
30333038
RESETCOLOR();
3039+
if (!new_keyword && prev_last_ast && prev_last_ast->kind == AST_IDENTIFIER) {
3040+
if (Spin2NewKeyword(current->Lptr, prev_last_ast->d.string)) {
3041+
new_keyword = prev_last_ast->d.string;
3042+
}
3043+
}
3044+
if (new_keyword) {
3045+
SETCOLOR(PRINT_WARNING);
3046+
ERRORHEADER(current->Lptr->fileName, current->Lptr->lineCounter, "note");
3047+
fprintf(stderr, "`%s' is a keyword in some Spin2 versions; did you forget a {Spin2_vXX} comment?\n", new_keyword);
3048+
RESETCOLOR();
3049+
}
30343050
}

0 commit comments

Comments
 (0)