Skip to content

Commit

Permalink
add __FILE__ and __LINE__ (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
Charlie Somerville committed Apr 16, 2013
1 parent 7668bde commit 342cfc4
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 0 deletions.
3 changes: 3 additions & 0 deletions inc/slash/lex.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ typedef enum sl_token_type {
SL_TOK_OPEN_RAW_ECHO_TAG,
SL_TOK_CLOSE_TAG,
SL_TOK_RAW,

SL_TOK_SPECIAL_FILE,
SL_TOK_SPECIAL_LINE,

SL_TOK_WHITESPACE,
SL_TOK_CONSTANT,
Expand Down
3 changes: 3 additions & 0 deletions src/lex.yy
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ HEX [0-9a-fA-F]
<SLASH>"\\" { ADD_TOKEN(sl_make_token(SL_TOK_LAMBDA)); }
<SLASH>"λ" { ADD_TOKEN(sl_make_token(SL_TOK_LAMBDA)); }
<SLASH>"__FILE__" { ADD_TOKEN(sl_make_token(SL_TOK_SPECIAL_FILE)); }
<SLASH>"__LINE__" { ADD_TOKEN(sl_make_token(SL_TOK_SPECIAL_LINE)); }
<SLASH>[A-Z]{IDT}? { ADD_TOKEN(sl_make_string_token(yyextra, SL_TOK_CONSTANT, yytext, yyleng)); }
<SLASH>{ID} { ADD_TOKEN(sl_make_string_token(yyextra, SL_TOK_IDENTIFIER, yytext, yyleng)); }
<SLASH>"@"{ID} { ADD_TOKEN(sl_make_string_token(yyextra, SL_TOK_IVAR, yytext + 1, yyleng - 1)); }
Expand Down
7 changes: 7 additions & 0 deletions src/parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,13 @@ primary_expression(sl_parse_state_t* ps)
error(ps, sl_make_cstring(ps->vm, "last invalid outside loop"), tok);
}
return sl_make_singleton_node(ps, SL_NODE_LAST);
case SL_TOK_SPECIAL_FILE:
next_token(ps);
return sl_make_immediate_node(ps,
sl_make_cstring(ps->vm, (char*)ps->filename));
case SL_TOK_SPECIAL_LINE:
return sl_make_immediate_node(ps,
sl_make_int(ps->vm, next_token(ps)->line));
case SL_TOK_RANGE_EX:
next_token(ps);
return sl_make_singleton_node(ps, SL_NODE_YADA_YADA);
Expand Down
11 changes: 11 additions & 0 deletions test/slash/special_constants.sl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<%
class SpecialConstantsTest extends Test {
def test_line {
assert_equal(5, __LINE__);
}
def test_file {
assert(%r{test/slash/special_constants\.sl}.match(__FILE__));
}
}.register;

0 comments on commit 342cfc4

Please sign in to comment.