rust/src/comp/syntax/parse/token.rs

219 lines
4.3 KiB
Rust
Raw Normal View History

import ast::ty_mach;
import ast_util::ty_mach_to_str;
import std::map::new_str_hash;
import util::interner;
import std::int;
import std::uint;
import std::str;
2010-08-18 11:35:12 -07:00
2011-05-09 14:17:28 -07:00
type str_num = uint;
tag binop {
PLUS;
MINUS;
STAR;
SLASH;
PERCENT;
CARET;
AND;
OR;
LSL;
LSR;
ASR;
}
tag token {
2011-07-27 14:19:39 +02:00
/* Expression-operator symbols. */
EQ;
LT;
LE;
EQEQ;
NE;
GE;
GT;
ANDAND;
OROR;
NOT;
TILDE;
BINOP(binop);
BINOPEQ(binop);
2011-07-27 14:19:39 +02:00
/* Structural symbols */
AT;
DOT;
2011-07-11 16:13:17 -07:00
ELLIPSIS;
COMMA;
SEMI;
COLON;
MOD_SEP;
QUES;
RARROW;
LARROW;
DARROW;
LPAREN;
RPAREN;
LBRACKET;
RBRACKET;
LBRACE;
RBRACE;
POUND;
POUND_LBRACE;
POUND_LT;
2011-07-27 14:19:39 +02:00
/* Literals */
LIT_INT(int);
LIT_UINT(uint);
LIT_MACH_INT(ty_mach, int);
2011-05-09 14:17:28 -07:00
LIT_FLOAT(str_num);
LIT_MACH_FLOAT(ty_mach, str_num);
LIT_STR(str_num);
LIT_CHAR(char);
LIT_BOOL(bool);
2011-07-27 14:19:39 +02:00
/* Name components */
IDENT(str_num, bool);
IDX(int);
UNDERSCORE;
2011-05-09 14:17:28 -07:00
BRACEQUOTE(str_num);
EOF;
}
2010-08-18 11:35:12 -07:00
fn binop_to_str(o: binop) -> istr {
2011-07-27 14:19:39 +02:00
alt o {
PLUS. { ret ~"+"; }
MINUS. { ret ~"-"; }
STAR. { ret ~"*"; }
SLASH. { ret ~"/"; }
PERCENT. { ret ~"%"; }
CARET. { ret ~"^"; }
AND. { ret ~"&"; }
OR. { ret ~"|"; }
LSL. { ret ~"<<"; }
LSR. { ret ~">>"; }
ASR. { ret ~">>>"; }
2010-08-20 15:57:59 -07:00
}
}
fn to_str(r: lexer::reader, t: token) -> istr {
2011-07-27 14:19:39 +02:00
alt t {
EQ. { ret ~"="; }
LT. { ret ~"<"; }
LE. { ret ~"<="; }
EQEQ. { ret ~"=="; }
NE. { ret ~"!="; }
GE. { ret ~">="; }
GT. { ret ~">"; }
NOT. { ret ~"!"; }
TILDE. { ret ~"~"; }
OROR. { ret ~"||"; }
ANDAND. { ret ~"&&"; }
2011-07-27 14:19:39 +02:00
BINOP(op) { ret binop_to_str(op); }
BINOPEQ(op) { ret binop_to_str(op) + ~"="; }
2011-07-27 14:19:39 +02:00
2011-07-27 14:19:39 +02:00
/* Structural symbols */
AT. {
ret ~"@";
2011-07-27 14:19:39 +02:00
}
DOT. { ret ~"."; }
ELLIPSIS. { ret ~"..."; }
COMMA. { ret ~","; }
SEMI. { ret ~";"; }
COLON. { ret ~":"; }
MOD_SEP. { ret ~"::"; }
QUES. { ret ~"?"; }
RARROW. { ret ~"->"; }
LARROW. { ret ~"<-"; }
DARROW. { ret ~"<->"; }
LPAREN. { ret ~"("; }
RPAREN. { ret ~")"; }
LBRACKET. { ret ~"["; }
RBRACKET. { ret ~"]"; }
LBRACE. { ret ~"{"; }
RBRACE. { ret ~"}"; }
POUND. { ret ~"#"; }
POUND_LBRACE. { ret ~"#{"; }
POUND_LT. { ret ~"#<"; }
2011-07-27 14:19:39 +02:00
2011-07-27 14:19:39 +02:00
/* Literals */
LIT_INT(i) {
ret int::to_str(i, 10u);
2011-07-27 14:19:39 +02:00
}
LIT_UINT(u) { ret uint::to_str(u, 10u); }
2011-07-27 14:19:39 +02:00
LIT_MACH_INT(tm, i) {
ret int::to_str(i, 10u) + ~"_" + ty_mach_to_str(tm);
2011-07-27 14:19:39 +02:00
}
LIT_MACH_FLOAT(tm, s) {
ret interner::get::<istr>(
*r.get_interner(), s) + ~"_" +
ty_mach_to_str(tm);
2011-08-27 00:07:03 -07:00
}
LIT_FLOAT(s) {
ret interner::get::<istr>(*r.get_interner(), s);
2011-07-27 14:19:39 +02:00
}
LIT_STR(s) { // FIXME: escape.
ret ~"\"" +
interner::get::<istr>(*r.get_interner(), s)
+ ~"\"";
2011-07-27 14:19:39 +02:00
}
LIT_CHAR(c) {
// FIXME: escape.
let tmp = ~"'";
str::push_char(tmp, c);
str::push_byte(tmp, '\'' as u8);
2011-07-27 14:19:39 +02:00
ret tmp;
}
LIT_BOOL(b) { if b { ret ~"true"; } else { ret ~"false"; } }
2011-07-27 14:19:39 +02:00
2011-07-27 14:19:39 +02:00
/* Name components */
IDENT(s, _) {
ret interner::get::<istr>(*r.get_interner(), s);
2011-07-27 14:19:39 +02:00
}
IDX(i) { ret ~"_" + int::to_str(i, 10u); }
UNDERSCORE. { ret ~"_"; }
BRACEQUOTE(_) { ret ~"<bracequote>"; }
EOF. { ret ~"<eof>"; }
}
}
pure fn can_begin_expr(t: token) -> bool {
2011-07-27 14:19:39 +02:00
alt t {
LPAREN. { true }
LBRACE. { true }
LBRACKET. { true }
IDENT(_, _) { true }
UNDERSCORE. { true }
TILDE. { true }
LIT_INT(_) { true }
LIT_UINT(_) { true }
LIT_MACH_INT(_, _) { true }
LIT_FLOAT(_) { true }
LIT_MACH_FLOAT(_, _) { true }
LIT_STR(_) { true }
LIT_CHAR(_) { true }
POUND. { true }
AT. { true }
NOT. { true }
BINOP(MINUS.) { true }
BINOP(STAR.) { true }
MOD_SEP. { true }
2011-07-27 14:19:39 +02:00
_ { false }
}
}
2010-08-18 11:35:12 -07:00
// Local Variables:
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
2010-08-18 11:35:12 -07:00
// End: