Skip to content

Commit

Permalink
Merge pull request #752 from braddr/cleanup-backend2
Browse files Browse the repository at this point in the history
Change lexer to support # as a token, preserving #line's original behavior
  • Loading branch information
WalterBright committed Feb 21, 2012
2 parents 027fdbc + 0bdc831 commit 2cb061a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
26 changes: 17 additions & 9 deletions src/lexer.c
Expand Up @@ -1224,9 +1224,21 @@ void Lexer::scan(Token *t)
#undef DOUBLE

case '#':
{
p++;
pragma();
continue;
Token *n = peek(&token);
if (n->value == TOKidentifier && n->ident == Id::line)
{
nextToken();
poundLine();
continue;
}
else
{
t->value = TOKpound;
return;
}
}

default:
{ unsigned c = *p;
Expand Down Expand Up @@ -2562,22 +2574,17 @@ __body
}

/*********************************************
* Do pragma.
* Currently, the only pragma supported is:
* parse:
* #line linnum [filespec]
*/

void Lexer::pragma()
void Lexer::poundLine()
{
Token tok;
int linnum;
char *filespec = NULL;
Loc loc = this->loc;

scan(&tok);
if (tok.value != TOKidentifier || tok.ident != Id::line)
goto Lerr;

scan(&tok);
if (tok.value == TOKint32v || tok.value == TOKint64v)
{ linnum = tok.uns64value - 1;
Expand Down Expand Up @@ -3160,6 +3167,7 @@ void Lexer::initKeywords()
Token::tochars[TOKpow] = "^^";
Token::tochars[TOKpowass] = "^^=";
Token::tochars[TOKgoesto] = "=>";
Token::tochars[TOKpound] = "#";
#endif

// For debugging
Expand Down
3 changes: 2 additions & 1 deletion src/lexer.h
Expand Up @@ -170,6 +170,7 @@ enum TOK
TOKpowass,
TOKgoesto,
TOKvector,
TOKpound,
#endif

TOKMAX
Expand Down Expand Up @@ -305,7 +306,7 @@ struct Lexer
void error(const char *format, ...);
void error(Loc loc, const char *format, ...);
void verror(Loc loc, const char *format, va_list ap);
void pragma();
void poundLine();
unsigned decodeUTF();
void getDocComment(Token *t, unsigned lineComment);

Expand Down
21 changes: 21 additions & 0 deletions test/compilable/line.d
@@ -0,0 +1,21 @@
module line;

static assert(__LINE__ == 3);

int #line 10
x;

static assert(__LINE__ == 12);
static assert(__FILE__ == "compilable/line.d");

#line 100 "newfile.d"

static assert(__LINE__ == 101);
static assert(__FILE__ == "newfile.d");

# line 200

static assert(__LINE__ == 201);
static assert(__FILE__ == "newfile.d");


0 comments on commit 2cb061a

Please sign in to comment.