Added basic pragma parsing
Actual contents of the pragma are currently just discarded. This supports both #pragma and _Pragma() definitions, but the pragma must end the line it is on and cannot be in the middle of a statement.
This commit is contained in:
parent
a96902717c
commit
9abf00e2d3
19
c-tests/pragma.c
Normal file
19
c-tests/pragma.c
Normal file
|
@ -0,0 +1,19 @@
|
|||
int a;
|
||||
|
||||
#pragma test1
|
||||
|
||||
int b;
|
||||
|
||||
_Pragma( "test2" )
|
||||
|
||||
int main(void) {
|
||||
a = 0;
|
||||
|
||||
#pragma test3
|
||||
|
||||
b = 1;
|
||||
|
||||
_Pragma("test4")
|
||||
|
||||
return a;
|
||||
}
|
|
@ -175,7 +175,9 @@ enum LexToken {
|
|||
TokenEndOfLine,
|
||||
TokenEndOfFunction,
|
||||
TokenBackSlash,
|
||||
TokenVolatileType
|
||||
TokenVolatileType,
|
||||
TokenHashPragma,
|
||||
TokenUnderscorePragma
|
||||
};
|
||||
|
||||
/* used in dynamic memory allocation */
|
||||
|
|
6
lex.c
6
lex.c
|
@ -94,7 +94,9 @@ static struct ReservedWord ReservedWords[] = {
|
|||
{"unsigned", TokenUnsignedType},
|
||||
{"void", TokenVoidType},
|
||||
{"while", TokenWhile},
|
||||
{"volatile", TokenVolatileType}
|
||||
{"volatile", TokenVolatileType},
|
||||
{"#pragma", TokenHashPragma},
|
||||
{"_Pragma", TokenUnderscorePragma}
|
||||
};
|
||||
|
||||
|
||||
|
@ -849,7 +851,7 @@ enum LexToken LexGetRawToken(struct ParseState *Parser, struct Value **Value,
|
|||
#ifdef DEBUG_LEXER
|
||||
printf("Got token=%02x inc=%d pos=%d\n", Token, IncPos, Parser->CharacterPos);
|
||||
#endif
|
||||
assert(Token >= TokenNone && Token <= TokenVolatileType);
|
||||
assert(Token >= TokenNone && Token <= TokenUnderscorePragma);
|
||||
return Token;
|
||||
}
|
||||
|
||||
|
|
14
parse.c
14
parse.c
|
@ -12,6 +12,7 @@ static void ParseDeclarationAssignment(struct ParseState *Parser,
|
|||
struct Value *NewVariable, int DoAssignment);
|
||||
static int ParseDeclaration(struct ParseState *Parser, enum LexToken Token);
|
||||
static void ParseMacroDefinition(struct ParseState *Parser);
|
||||
static void ParsePragma(struct ParseState *Parser);
|
||||
static void ParseFor(struct ParseState *Parser);
|
||||
static enum RunMode ParseBlock(struct ParseState *Parser, int AbsorbOpenBrace,
|
||||
int Condition);
|
||||
|
@ -451,6 +452,14 @@ void ParseMacroDefinition(struct ParseState *Parser)
|
|||
ProgramFail(Parser, "'%s' is already defined", MacroNameStr);
|
||||
}
|
||||
|
||||
/* parse a pragma */
|
||||
void ParsePragma(struct ParseState *Parser)
|
||||
{
|
||||
/* consume tokens until we hit the end of a line */
|
||||
/* (not ideal for _Pragma() but it'll do for now) */
|
||||
LexToEndOfMacro(Parser);
|
||||
}
|
||||
|
||||
/* copy the entire parser state */
|
||||
void ParserCopy(struct ParseState *To, struct ParseState *From)
|
||||
{
|
||||
|
@ -853,6 +862,11 @@ enum ParseResult ParseStatement(struct ParseState *Parser,
|
|||
}
|
||||
break;
|
||||
}
|
||||
case TokenHashPragma:
|
||||
case TokenUnderscorePragma:
|
||||
ParsePragma(Parser);
|
||||
CheckTrailingSemicolon = false;
|
||||
break;
|
||||
default:
|
||||
*Parser = PreState;
|
||||
return ParseResultError;
|
||||
|
|
4
stats.c
4
stats.c
|
@ -112,7 +112,9 @@ struct LexTokenStat LexTokenStats[NO_TOKENS] = {
|
|||
{"TokenEndOfLine", {0, 0, 0, 0, 0, 0, 0}},
|
||||
{"TokenEndOfFunction", {0, 0, 0, 0, 0, 0, 0}},
|
||||
{"TokenBackSlash", {0, 0, 0, 0, 0, 0, 0}},
|
||||
{"TokenVolatileType", {0, 0, 0, 0, 0, 0, 0}}
|
||||
{"TokenVolatileType", {0, 0, 0, 0, 0, 0, 0}},
|
||||
{"TokenHashPragma", {0, 0, 0, 0, 0, 0, 0}},
|
||||
{"TokenUnderscorePragma", {0, 0, 0, 0, 0, 0, 0}}
|
||||
};
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue