2011-07-05 04:48:19 -05:00
|
|
|
import util::interner;
|
2012-04-15 05:44:32 -05:00
|
|
|
import util::interner::interner;
|
2012-04-17 23:14:40 -05:00
|
|
|
import std::map::{hashmap, str_hash};
|
2012-05-21 12:45:56 -05:00
|
|
|
import std::serialization::{serializer,
|
|
|
|
deserializer,
|
|
|
|
serialize_uint,
|
|
|
|
deserialize_uint,
|
|
|
|
serialize_i64,
|
|
|
|
deserialize_i64,
|
|
|
|
serialize_u64,
|
|
|
|
deserialize_u64,
|
|
|
|
serialize_bool,
|
|
|
|
deserialize_bool};
|
2010-08-18 13:35:12 -05:00
|
|
|
|
2012-05-21 12:45:56 -05:00
|
|
|
#[auto_serialize]
|
2011-05-09 16:17:28 -05:00
|
|
|
type str_num = uint;
|
|
|
|
|
2012-05-21 12:45:56 -05:00
|
|
|
#[auto_serialize]
|
2012-01-19 16:24:03 -06:00
|
|
|
enum binop {
|
2012-01-19 19:56:05 -06:00
|
|
|
PLUS,
|
|
|
|
MINUS,
|
|
|
|
STAR,
|
|
|
|
SLASH,
|
|
|
|
PERCENT,
|
|
|
|
CARET,
|
|
|
|
AND,
|
|
|
|
OR,
|
2012-05-22 16:59:15 -05:00
|
|
|
SHL,
|
|
|
|
SHR,
|
2010-09-09 17:59:29 -05:00
|
|
|
}
|
|
|
|
|
2012-05-21 12:45:56 -05:00
|
|
|
#[auto_serialize]
|
2012-01-19 16:24:03 -06:00
|
|
|
enum token {
|
2010-09-09 17:59:29 -05:00
|
|
|
/* Expression-operator symbols. */
|
2012-01-19 19:56:05 -06:00
|
|
|
EQ,
|
|
|
|
LT,
|
|
|
|
LE,
|
|
|
|
EQEQ,
|
|
|
|
NE,
|
|
|
|
GE,
|
|
|
|
GT,
|
|
|
|
ANDAND,
|
|
|
|
OROR,
|
|
|
|
NOT,
|
|
|
|
TILDE,
|
|
|
|
BINOP(binop),
|
|
|
|
BINOPEQ(binop),
|
2010-09-09 17:59:29 -05:00
|
|
|
|
|
|
|
/* Structural symbols */
|
2012-01-19 19:56:05 -06:00
|
|
|
AT,
|
|
|
|
DOT,
|
|
|
|
ELLIPSIS,
|
|
|
|
COMMA,
|
|
|
|
SEMI,
|
|
|
|
COLON,
|
|
|
|
MOD_SEP,
|
|
|
|
RARROW,
|
|
|
|
LARROW,
|
|
|
|
DARROW,
|
2012-06-04 20:34:10 -05:00
|
|
|
FAT_ARROW,
|
2012-01-19 19:56:05 -06:00
|
|
|
LPAREN,
|
|
|
|
RPAREN,
|
|
|
|
LBRACKET,
|
|
|
|
RBRACKET,
|
|
|
|
LBRACE,
|
|
|
|
RBRACE,
|
|
|
|
POUND,
|
2012-04-22 18:58:04 -05:00
|
|
|
DOLLAR,
|
2012-01-25 17:38:09 -06:00
|
|
|
|
2010-09-09 17:59:29 -05:00
|
|
|
/* Literals */
|
2012-01-19 19:56:05 -06:00
|
|
|
LIT_INT(i64, ast::int_ty),
|
|
|
|
LIT_UINT(u64, ast::uint_ty),
|
2012-06-14 21:41:40 -05:00
|
|
|
LIT_INT_UNSUFFIXED(i64),
|
2012-01-19 19:56:05 -06:00
|
|
|
LIT_FLOAT(str_num, ast::float_ty),
|
|
|
|
LIT_STR(str_num),
|
2010-09-09 17:59:29 -05:00
|
|
|
|
|
|
|
/* Name components */
|
2012-01-19 19:56:05 -06:00
|
|
|
IDENT(str_num, bool),
|
|
|
|
UNDERSCORE,
|
2012-06-12 12:50:17 -05:00
|
|
|
|
|
|
|
//ACTUALLY(whole_nonterminal),
|
|
|
|
|
2012-06-30 05:54:54 -05:00
|
|
|
DOC_COMMENT(str_num),
|
2012-01-19 19:56:05 -06:00
|
|
|
EOF,
|
2010-09-09 17:59:29 -05:00
|
|
|
}
|
2010-08-18 13:35:12 -05:00
|
|
|
|
2012-06-12 12:50:17 -05:00
|
|
|
#[auto_serialize]
|
|
|
|
#[doc = "For interpolation during macro expansion."]
|
2012-06-12 12:59:50 -05:00
|
|
|
enum whole_nt {
|
2012-06-12 12:50:17 -05:00
|
|
|
w_item(@ast::item),
|
|
|
|
w_block(ast::blk),
|
|
|
|
w_stmt(@ast::stmt),
|
|
|
|
w_pat( @ast::pat),
|
|
|
|
w_expr(@ast::expr),
|
|
|
|
w_ty( @ast::ty),
|
|
|
|
w_ident(ast::ident),
|
|
|
|
w_path(@ast::path),
|
|
|
|
}
|
|
|
|
|
2011-09-02 17:34:58 -05:00
|
|
|
fn binop_to_str(o: binop) -> str {
|
2011-07-27 07:19:39 -05:00
|
|
|
alt o {
|
2012-06-11 18:49:35 -05:00
|
|
|
PLUS { "+" }
|
|
|
|
MINUS { "-" }
|
|
|
|
STAR { "*" }
|
|
|
|
SLASH { "/" }
|
|
|
|
PERCENT { "%" }
|
|
|
|
CARET { "^" }
|
|
|
|
AND { "&" }
|
|
|
|
OR { "|" }
|
|
|
|
SHL { "<<" }
|
|
|
|
SHR { ">>" }
|
2010-08-20 17:57:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-09 02:53:34 -05:00
|
|
|
fn to_str(in: interner<@str>, t: token) -> str {
|
2011-07-27 07:19:39 -05:00
|
|
|
alt t {
|
2012-06-11 18:49:35 -05:00
|
|
|
EQ { "=" }
|
|
|
|
LT { "<" }
|
|
|
|
LE { "<=" }
|
|
|
|
EQEQ { "==" }
|
|
|
|
NE { "!=" }
|
|
|
|
GE { ">=" }
|
|
|
|
GT { ">" }
|
|
|
|
NOT { "!" }
|
|
|
|
TILDE { "~" }
|
|
|
|
OROR { "||" }
|
|
|
|
ANDAND { "&&" }
|
|
|
|
BINOP(op) { binop_to_str(op) }
|
|
|
|
BINOPEQ(op) { binop_to_str(op) + "=" }
|
2011-09-02 17:34:58 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
/* Structural symbols */
|
2012-06-11 18:49:35 -05:00
|
|
|
AT { "@" }
|
|
|
|
DOT { "." }
|
|
|
|
ELLIPSIS { "..." }
|
|
|
|
COMMA { "," }
|
|
|
|
SEMI { "" }
|
|
|
|
COLON { ":" }
|
|
|
|
MOD_SEP { "::" }
|
|
|
|
RARROW { "->" }
|
|
|
|
LARROW { "<-" }
|
|
|
|
DARROW { "<->" }
|
|
|
|
FAT_ARROW { "=>" }
|
|
|
|
LPAREN { "(" }
|
|
|
|
RPAREN { ")" }
|
|
|
|
LBRACKET { "[" }
|
|
|
|
RBRACKET { "]" }
|
|
|
|
LBRACE { "{" }
|
|
|
|
RBRACE { "}" }
|
|
|
|
POUND { "#" }
|
|
|
|
DOLLAR { "$" }
|
2012-01-25 17:38:09 -06:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
/* Literals */
|
2012-01-19 00:37:22 -06:00
|
|
|
LIT_INT(c, ast::ty_char) {
|
2012-06-11 18:49:35 -05:00
|
|
|
"'" + char::escape_default(c as char) + "'"
|
2011-12-07 14:06:12 -06:00
|
|
|
}
|
|
|
|
LIT_INT(i, t) {
|
2012-06-11 18:49:35 -05:00
|
|
|
int::to_str(i as int, 10u) + ast_util::int_ty_to_str(t)
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2011-12-07 14:06:12 -06:00
|
|
|
LIT_UINT(u, t) {
|
2012-06-11 18:49:35 -05:00
|
|
|
uint::to_str(u as uint, 10u) + ast_util::uint_ty_to_str(t)
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2012-06-14 21:41:40 -05:00
|
|
|
LIT_INT_UNSUFFIXED(i) {
|
|
|
|
int::to_str(i as int, 10u)
|
2012-06-11 18:31:03 -05:00
|
|
|
}
|
2011-12-07 14:06:12 -06:00
|
|
|
LIT_FLOAT(s, t) {
|
2012-06-11 18:49:35 -05:00
|
|
|
*interner::get(in, s) +
|
|
|
|
ast_util::float_ty_to_str(t)
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2012-05-31 17:31:13 -05:00
|
|
|
LIT_STR(s) {
|
2012-06-11 18:49:35 -05:00
|
|
|
"\""
|
2012-06-09 02:53:34 -05:00
|
|
|
+ str::escape_default(*interner::get(in, s))
|
2012-06-11 18:49:35 -05:00
|
|
|
+ "\""
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2012-06-30 05:54:54 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
/* Name components */
|
|
|
|
IDENT(s, _) {
|
2012-06-11 18:49:35 -05:00
|
|
|
*interner::get(in, s)
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2012-06-11 18:49:35 -05:00
|
|
|
UNDERSCORE { "_" }
|
2012-06-30 05:54:54 -05:00
|
|
|
|
|
|
|
/* Other */
|
|
|
|
DOC_COMMENT(s) { *interner::get(in, s) }
|
2012-06-11 18:49:35 -05:00
|
|
|
EOF { "<eof>" }
|
2010-08-20 13:41:34 -05:00
|
|
|
}
|
|
|
|
}
|
2011-07-03 13:48:14 -05:00
|
|
|
|
2011-08-24 15:41:50 -05:00
|
|
|
pure fn can_begin_expr(t: token) -> bool {
|
2011-07-27 07:19:39 -05:00
|
|
|
alt t {
|
2012-01-19 00:37:22 -06:00
|
|
|
LPAREN { true }
|
|
|
|
LBRACE { true }
|
|
|
|
LBRACKET { true }
|
2011-07-27 07:19:39 -05:00
|
|
|
IDENT(_, _) { true }
|
2012-01-19 00:37:22 -06:00
|
|
|
UNDERSCORE { true }
|
|
|
|
TILDE { true }
|
2011-12-07 14:06:12 -06:00
|
|
|
LIT_INT(_, _) { true }
|
|
|
|
LIT_UINT(_, _) { true }
|
2012-06-14 21:41:40 -05:00
|
|
|
LIT_INT_UNSUFFIXED(_) { true }
|
2011-12-07 14:06:12 -06:00
|
|
|
LIT_FLOAT(_, _) { true }
|
2011-07-27 07:19:39 -05:00
|
|
|
LIT_STR(_) { true }
|
2012-01-19 00:37:22 -06:00
|
|
|
POUND { true }
|
|
|
|
AT { true }
|
|
|
|
NOT { true }
|
|
|
|
BINOP(MINUS) { true }
|
|
|
|
BINOP(STAR) { true }
|
2012-03-23 18:15:58 -05:00
|
|
|
BINOP(AND) { true }
|
2012-06-30 23:20:36 -05:00
|
|
|
BINOP(OR) { true } // in lambda syntax
|
|
|
|
OROR { true } // in lambda syntax
|
2012-01-19 00:37:22 -06:00
|
|
|
MOD_SEP { true }
|
2011-07-27 07:19:39 -05:00
|
|
|
_ { false }
|
2011-07-03 13:48:14 -05:00
|
|
|
}
|
|
|
|
}
|
2011-10-07 09:22:53 -05:00
|
|
|
|
2012-06-11 18:49:35 -05:00
|
|
|
fn is_lit(t: token) -> bool {
|
|
|
|
alt t {
|
|
|
|
LIT_INT(_, _) { true }
|
|
|
|
LIT_UINT(_, _) { true }
|
2012-06-14 21:41:40 -05:00
|
|
|
LIT_INT_UNSUFFIXED(_) { true }
|
2012-06-11 18:49:35 -05:00
|
|
|
LIT_FLOAT(_, _) { true }
|
|
|
|
LIT_STR(_) { true }
|
|
|
|
_ { false }
|
|
|
|
}
|
2012-04-22 16:59:04 -05:00
|
|
|
}
|
|
|
|
|
2012-06-11 18:49:35 -05:00
|
|
|
pure fn is_ident(t: token) -> bool {
|
|
|
|
alt t { IDENT(_, _) { true } _ { false } }
|
2012-04-17 23:14:40 -05:00
|
|
|
}
|
|
|
|
|
2012-06-11 18:49:35 -05:00
|
|
|
pure fn is_plain_ident(t: token) -> bool {
|
|
|
|
alt t { IDENT(_, false) { true } _ { false } }
|
2012-04-17 23:14:40 -05:00
|
|
|
}
|
|
|
|
|
2012-06-11 18:49:35 -05:00
|
|
|
pure fn is_bar(t: token) -> bool {
|
|
|
|
alt t { BINOP(OR) | OROR { true } _ { false } }
|
2012-04-17 23:14:40 -05:00
|
|
|
}
|
|
|
|
|
2012-04-19 18:44:24 -05:00
|
|
|
#[doc = "
|
2012-04-25 00:33:49 -05:00
|
|
|
All the valid words that have meaning in the Rust language.
|
|
|
|
|
|
|
|
Rust keywords are either 'contextual' or 'restricted'. Contextual
|
|
|
|
keywords may be used as identifiers because their appearance in
|
|
|
|
the grammar is unambiguous. Restricted keywords may not appear
|
|
|
|
in positions that might otherwise contain _value identifiers_.
|
2012-04-19 18:44:24 -05:00
|
|
|
"]
|
|
|
|
fn keyword_table() -> hashmap<str, ()> {
|
|
|
|
let keywords = str_hash();
|
2012-06-30 18:19:07 -05:00
|
|
|
for contextual_keyword_table().each_key |word| {
|
2012-04-25 00:33:49 -05:00
|
|
|
keywords.insert(word, ());
|
|
|
|
}
|
2012-06-30 18:19:07 -05:00
|
|
|
for restricted_keyword_table().each_key |word| {
|
2012-04-19 18:44:24 -05:00
|
|
|
keywords.insert(word, ());
|
|
|
|
}
|
2012-06-11 18:49:35 -05:00
|
|
|
keywords
|
2012-04-25 00:33:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[doc = "Keywords that may be used as identifiers"]
|
|
|
|
fn contextual_keyword_table() -> hashmap<str, ()> {
|
|
|
|
let words = str_hash();
|
2012-06-29 18:26:56 -05:00
|
|
|
let keys = ~[
|
2012-04-19 18:44:24 -05:00
|
|
|
"as",
|
|
|
|
"else",
|
|
|
|
"move",
|
|
|
|
"of",
|
2012-05-08 09:06:24 -05:00
|
|
|
"priv", "pub",
|
2012-04-25 00:33:49 -05:00
|
|
|
"self", "send", "static",
|
2012-04-19 18:44:24 -05:00
|
|
|
"to",
|
|
|
|
"use",
|
2012-06-12 12:59:50 -05:00
|
|
|
"with",
|
|
|
|
/* temp */
|
|
|
|
"sep", "many", "at_least_one", "parse"
|
2012-06-29 18:26:56 -05:00
|
|
|
];
|
2012-06-30 18:19:07 -05:00
|
|
|
for keys.each |word| {
|
2012-04-25 00:33:49 -05:00
|
|
|
words.insert(word, ());
|
2012-04-19 18:44:24 -05:00
|
|
|
}
|
2012-04-25 00:33:49 -05:00
|
|
|
words
|
2012-04-19 18:44:24 -05:00
|
|
|
}
|
|
|
|
|
2012-04-17 23:14:40 -05:00
|
|
|
#[doc = "
|
2012-04-25 00:33:49 -05:00
|
|
|
Keywords that may not appear in any position that might otherwise contain a
|
|
|
|
_value identifier_. Restricted keywords may still be used as other types of
|
|
|
|
identifiers.
|
|
|
|
|
|
|
|
Reasons:
|
|
|
|
|
|
|
|
* For some (most?), if used at the start of a line, they will cause the line
|
|
|
|
to be interpreted as a specific kind of statement, which would be confusing.
|
|
|
|
|
|
|
|
* `true` or `false` as identifiers would always be shadowed by
|
|
|
|
the boolean constants
|
2012-04-17 23:14:40 -05:00
|
|
|
"]
|
2012-04-25 00:33:49 -05:00
|
|
|
fn restricted_keyword_table() -> hashmap<str, ()> {
|
2012-04-17 23:14:40 -05:00
|
|
|
let words = str_hash();
|
2012-06-29 18:26:56 -05:00
|
|
|
let keys = ~[
|
2012-04-25 00:33:49 -05:00
|
|
|
"alt",
|
|
|
|
"assert",
|
2012-06-27 15:14:04 -05:00
|
|
|
"break",
|
2012-07-03 18:34:40 -05:00
|
|
|
"check", "claim", "class", "const", "cont", "copy",
|
2012-06-18 19:42:09 -05:00
|
|
|
"do", "drop",
|
2012-06-26 18:18:37 -05:00
|
|
|
"else", "enum", "export", "extern",
|
2012-04-25 00:33:49 -05:00
|
|
|
"fail", "false", "fn", "for",
|
|
|
|
"if", "iface", "impl", "import",
|
|
|
|
"let", "log", "loop",
|
|
|
|
"mod", "mut",
|
2012-07-03 18:34:40 -05:00
|
|
|
"new",
|
2012-06-24 17:09:57 -05:00
|
|
|
"pure", "ret",
|
2012-04-25 00:33:49 -05:00
|
|
|
"true", "trait", "type",
|
|
|
|
"unchecked", "unsafe",
|
|
|
|
"while"
|
2012-06-29 18:26:56 -05:00
|
|
|
];
|
2012-06-30 18:19:07 -05:00
|
|
|
for keys.each |word| {
|
2012-04-17 23:14:40 -05:00
|
|
|
words.insert(word, ());
|
|
|
|
}
|
|
|
|
words
|
|
|
|
}
|
|
|
|
|
2010-08-18 13:35:12 -05:00
|
|
|
// Local Variables:
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
|
|
|
// End:
|