diff options
| author | Kevin Atkinson <kevina@cs.utah.edu> | 2012-01-25 16:38:09 -0700 |
|---|---|---|
| committer | Kevin Atkinson <kevina@cs.utah.edu> | 2012-02-03 20:23:49 -0700 |
| commit | 5ef53382aeac2de7e6dcc43c156312d2e68c15e4 (patch) | |
| tree | 0687cc4a0a4bdd20b5118541dd149a2a7a9ec8c0 /src/comp/syntax/parse | |
| parent | 75edd9ff69625292b1c4d5f05502d1fd28b39f55 (diff) | |
| download | rust-5ef53382aeac2de7e6dcc43c156312d2e68c15e4.tar.gz rust-5ef53382aeac2de7e6dcc43c156312d2e68c15e4.zip | |
Add support for parsing quasi-quotes, doesn't do anything useful yet.
Diffstat (limited to 'src/comp/syntax/parse')
| -rw-r--r-- | src/comp/syntax/parse/lexer.rs | 18 | ||||
| -rw-r--r-- | src/comp/syntax/parse/parser.rs | 19 | ||||
| -rw-r--r-- | src/comp/syntax/parse/token.rs | 11 |
3 files changed, 48 insertions, 0 deletions
diff --git a/src/comp/syntax/parse/lexer.rs b/src/comp/syntax/parse/lexer.rs index 4d7ee27eb9d..e023e5259fd 100644 --- a/src/comp/syntax/parse/lexer.rs +++ b/src/comp/syntax/parse/lexer.rs @@ -349,6 +349,7 @@ fn next_token_inner(rdr: reader) -> token::token { '#' { rdr.bump(); if rdr.curr == '<' { rdr.bump(); ret token::POUND_LT; } + if rdr.curr == '(' { rdr.bump(); ret token::POUND_LPAREN; } if rdr.curr == '{' { rdr.bump(); ret token::POUND_LBRACE; } ret token::POUND; } @@ -361,6 +362,23 @@ fn next_token_inner(rdr: reader) -> token::token { } else { ret token::COLON; } } + '$' { + rdr.bump(); + if is_dec_digit(rdr.curr) { + let val = dec_digit_val(rdr.curr) as uint; + while is_dec_digit(rdr.next()) { + rdr.bump(); + val = val * 10u + (dec_digit_val(rdr.curr) as uint); + } + rdr.bump(); + ret token::DOLLAR_NUM(val); + } else if c == '(' { + ret token::DOLLAR_LPAREN; + } else { + rdr.fatal("expected digit3"); + } + } + diff --git a/src/comp/syntax/parse/parser.rs b/src/comp/syntax/parse/parser.rs index 4a7b25f7c08..f2f1f7c14ed 100644 --- a/src/comp/syntax/parse/parser.rs +++ b/src/comp/syntax/parse/parser.rs @@ -628,6 +628,13 @@ fn parse_seq<T: copy>(bra: token::token, ket: token::token, ret spanned(lo, hi, result); } +fn have_dollar(p: parser) -> option::t<ast::mac_> { + alt p.token { + token::DOLLAR_NUM(num) {p.bump(); some(ast::mac_var(num))} + _ {none} + } +} + fn lit_from_token(p: parser, tok: token::token) -> ast::lit_ { alt tok { token::LIT_INT(i, it) { ast::lit_int(i, it) } @@ -755,6 +762,12 @@ fn parse_bottom_expr(p: parser) -> pexpr { let hi = p.span.hi; let ex: ast::expr_; + + alt have_dollar(p) { + some(x) {ret pexpr(mk_mac_expr(p, lo, p.span.hi, x));} + _ {} + } + if p.token == token::LPAREN { p.bump(); if p.token == token::RPAREN { @@ -843,6 +856,12 @@ fn parse_bottom_expr(p: parser) -> pexpr { } else if p.token == token::ELLIPSIS { p.bump(); ret pexpr(mk_mac_expr(p, lo, p.span.hi, ast::mac_ellipsis)); + } else if p.token == token::POUND_LPAREN { + p.bump(); + let e = parse_expr(p); + expect(p, token::RPAREN); + ret pexpr(mk_mac_expr(p, lo, p.span.hi, + ast::mac_qq(e.span, e))); } else if eat_word(p, "bind") { let e = parse_expr_res(p, RESTRICT_NO_CALL_EXPRS); fn parse_expr_opt(p: parser) -> option<@ast::expr> { diff --git a/src/comp/syntax/parse/token.rs b/src/comp/syntax/parse/token.rs index 3b38f314ecf..37ef677b6d6 100644 --- a/src/comp/syntax/parse/token.rs +++ b/src/comp/syntax/parse/token.rs @@ -53,9 +53,13 @@ enum token { LBRACE, RBRACE, POUND, + POUND_LPAREN, POUND_LBRACE, POUND_LT, + DOLLAR_LPAREN, + DOLLAR_NUM(uint), + /* Literals */ LIT_INT(i64, ast::int_ty), LIT_UINT(u64, ast::uint_ty), @@ -69,6 +73,7 @@ enum token { UNDERSCORE, BRACEQUOTE(str_num), EOF, + } fn binop_to_str(o: binop) -> str { @@ -123,9 +128,15 @@ fn to_str(r: reader, t: token) -> str { LBRACE { ret "{"; } RBRACE { ret "}"; } POUND { ret "#"; } + POUND_LPAREN { ret "#("; } POUND_LBRACE { ret "#{"; } POUND_LT { ret "#<"; } + DOLLAR_LPAREN { ret "$("; } + DOLLAR_NUM(u) { + ret "$" + uint::to_str(u as uint, 10u); + } + /* Literals */ LIT_INT(c, ast::ty_char) { // FIXME: escape. |
