summaryrefslogtreecommitdiff
path: root/src/parse
diff options
context:
space:
mode:
authorJohn Hodge <tpg@mutabah.net>2015-03-25 21:37:29 +0800
committerJohn Hodge <tpg@mutabah.net>2015-03-25 21:37:29 +0800
commit66880a641295734c437feb4eb32fb60bf5cd9af5 (patch)
tree723f47d10888e737548031b0d183cf30c0393121 /src/parse
parent56601285b24450168e8b1853ae9c8b65f4576cb0 (diff)
downloadmrust-66880a641295734c437feb4eb32fb60bf5cd9af5.tar.gz
Macros updated with concat!, stringify!, and $crate
Diffstat (limited to 'src/parse')
-rw-r--r--src/parse/expr.cpp20
-rw-r--r--src/parse/lex.cpp147
-rw-r--r--src/parse/lex.hpp20
-rw-r--r--src/parse/pattern.cpp3
-rw-r--r--src/parse/root.cpp12
5 files changed, 201 insertions, 1 deletions
diff --git a/src/parse/expr.cpp b/src/parse/expr.cpp
index 222a6a3a..9b3ce04b 100644
--- a/src/parse/expr.cpp
+++ b/src/parse/expr.cpp
@@ -97,6 +97,26 @@ ExprNodeP Parse_ExprBlockNode(TokenStream& lex)
false ) );
break;
}
+ // - 'static'
+ case TOK_RWORD_STATIC:
+ keep_mod = true;
+ {
+ bool is_mut = false;
+ if( GET_TOK(tok, lex) == TOK_RWORD_MUT )
+ is_mut = true;
+ else
+ lex.putback(tok);
+ GET_CHECK_TOK(tok, lex, TOK_IDENT);
+ ::std::string name = tok.str();
+ GET_CHECK_TOK(tok, lex, TOK_COLON);
+ TypeRef type = Parse_Type(lex);
+ GET_CHECK_TOK(tok, lex, TOK_EQUAL);
+ auto val = Parse_Expr1(lex);
+ GET_CHECK_TOK(tok, lex, TOK_SEMICOLON);
+
+ local_mod->add_global(false, is_mut, ::std::move(name), ::std::move(type), ::std::move(val));
+ break;
+ }
// - 'struct'
case TOK_RWORD_STRUCT:
keep_mod = true;
diff --git a/src/parse/lex.cpp b/src/parse/lex.cpp
index d09bc2d8..34a2ff58 100644
--- a/src/parse/lex.cpp
+++ b/src/parse/lex.cpp
@@ -678,6 +678,153 @@ enum eTokenType Token::typefromstr(const ::std::string& s)
return TOK_NULL;
}
+::std::string Token::to_str() const
+{
+ switch(m_type)
+ {
+ case TOK_NULL: return "/*null*/";
+ case TOK_EOF: return "/*eof*/";
+
+ case TOK_NEWLINE: return "\n";
+ case TOK_WHITESPACE: return " ";
+ case TOK_COMMENT: return "/*" + m_str + "*/";
+ // Value tokens
+ case TOK_IDENT: return m_str;
+ case TOK_MACRO: return m_str + "!";
+ case TOK_LIFETIME: return "'" + m_str;
+ case TOK_INTEGER: return FMT(m_intval); // TODO: suffix for type
+ case TOK_CHAR: return FMT("'\\u{"<< ::std::hex << m_intval << "}");
+ case TOK_FLOAT: return FMT(m_floatval);
+ case TOK_STRING: return "\"" + m_str + "\"";
+ case TOK_CATTR_OPEN:return "#![";
+ case TOK_ATTR_OPEN: return "#[";
+ case TOK_UNDERSCORE:return "_";
+ // Symbols
+ case TOK_PAREN_OPEN: return "(";
+ case TOK_PAREN_CLOSE: return ")";
+ case TOK_BRACE_OPEN: return "{";
+ case TOK_BRACE_CLOSE: return "}";
+ case TOK_LT: return "<";
+ case TOK_GT: return ">";
+ case TOK_SQUARE_OPEN: return "[";
+ case TOK_SQUARE_CLOSE: return "]";
+ case TOK_COMMA: return ",";
+ case TOK_SEMICOLON: return ";";
+ case TOK_COLON: return ":";
+ case TOK_DOUBLE_COLON: return ":";
+ case TOK_STAR: return "*";
+ case TOK_AMP: return "&";
+ case TOK_PIPE: return "|";
+
+ case TOK_FATARROW: return "=>"; // =>
+ case TOK_THINARROW: return "->"; // ->
+
+ case TOK_PLUS: return "+";
+ case TOK_DASH: return "-";
+ case TOK_EXCLAM: return "!";
+ case TOK_PERCENT: return "%";
+ case TOK_SLASH: return "/";
+
+ case TOK_DOT: return ".";
+ case TOK_DOUBLE_DOT: return "...";
+ case TOK_TRIPLE_DOT: return "..";
+
+ case TOK_EQUAL: return "=";
+ case TOK_PLUS_EQUAL: return "+=";
+ case TOK_DASH_EQUAL: return "-";
+ case TOK_PERCENT_EQUAL: return "%=";
+ case TOK_SLASH_EQUAL: return "/=";
+ case TOK_STAR_EQUAL: return "*=";
+ case TOK_AMP_EQUAL: return "&=";
+ case TOK_PIPE_EQUAL: return "|=";
+
+ case TOK_DOUBLE_EQUAL: return "==";
+ case TOK_EXCLAM_EQUAL: return "!=";
+ case TOK_GTE: return ">=";
+ case TOK_LTE: return "<=";
+
+ case TOK_DOUBLE_AMP: return "&&";
+ case TOK_DOUBLE_PIPE: return "||";
+ case TOK_DOUBLE_LT: return "<<";
+ case TOK_DOUBLE_GT: return ">>";
+ case TOK_DOUBLE_LT_EQUAL: return "<=";
+ case TOK_DOUBLE_GT_EQUAL: return ">=";
+
+ case TOK_DOLLAR: return "$";
+
+ case TOK_QMARK: return "?";
+ case TOK_AT: return "@";
+ case TOK_TILDE: return "~";
+ case TOK_BACKSLASH: return "\\";
+ case TOK_CARET: return "^";
+ case TOK_CARET_EQUAL: return "^=";
+ case TOK_BACKTICK: return "`";
+
+ // Reserved Words
+ case TOK_RWORD_PUB: return "pub";
+ case TOK_RWORD_PRIV: return "priv";
+ case TOK_RWORD_MUT: return "mut";
+ case TOK_RWORD_CONST: return "const";
+ case TOK_RWORD_STATIC: return "static";
+ case TOK_RWORD_UNSAFE: return "unsafe";
+ case TOK_RWORD_EXTERN: return "extern";
+
+ case TOK_RWORD_CRATE: return "crate";
+ case TOK_RWORD_MOD: return "mod";
+ case TOK_RWORD_STRUCT: return "struct";
+ case TOK_RWORD_ENUM: return "enum";
+ case TOK_RWORD_TRAIT: return "trait";
+ case TOK_RWORD_FN: return "fn";
+ case TOK_RWORD_USE: return "use";
+ case TOK_RWORD_IMPL: return "impl";
+ case TOK_RWORD_TYPE: return "type";
+
+ case TOK_RWORD_WHERE: return "where";
+ case TOK_RWORD_AS: return "as";
+
+ case TOK_RWORD_LET: return "let";
+ case TOK_RWORD_MATCH: return "match";
+ case TOK_RWORD_IF: return "if";
+ case TOK_RWORD_ELSE: return "else";
+ case TOK_RWORD_LOOP: return "loop";
+ case TOK_RWORD_WHILE: return "while";
+ case TOK_RWORD_FOR: return "for";
+ case TOK_RWORD_IN: return "in";
+ case TOK_RWORD_DO: return "do";
+
+ case TOK_RWORD_CONTINUE:return "continue";
+ case TOK_RWORD_BREAK: return "break";
+ case TOK_RWORD_RETURN: return "return";
+ case TOK_RWORD_YIELD: return "yeild";
+ case TOK_RWORD_BOX: return "box";
+ case TOK_RWORD_REF: return "ref";
+
+ case TOK_RWORD_FALSE: return "false";
+ case TOK_RWORD_TRUE: return "true";
+ case TOK_RWORD_SELF: return "self";
+ case TOK_RWORD_SUPER: return "super";
+
+ case TOK_RWORD_PROC: return "proc";
+ case TOK_RWORD_MOVE: return "move";
+ case TOK_RWORD_ONCE: return "once";
+
+ case TOK_RWORD_ABSTRACT:return "abstract";
+ case TOK_RWORD_FINAL: return "final";
+ case TOK_RWORD_PURE: return "pure";
+ case TOK_RWORD_OVERRIDE:return "override";
+ case TOK_RWORD_VIRTUAL: return "virtual";
+
+ case TOK_RWORD_ALIGNOF: return "alignof";
+ case TOK_RWORD_OFFSETOF:return "offsetof";
+ case TOK_RWORD_SIZEOF: return "sizeof";
+ case TOK_RWORD_TYPEOF: return "typeof";
+
+ case TOK_RWORD_BE: return "be";
+ case TOK_RWORD_UNSIZED: return "unsized";
+ }
+ throw ParseError::BugCheck("Reached end of Token::to_str");
+}
+
void operator%(Serialiser& s, enum eTokenType c) {
s << Token::typestr(c);
}
diff --git a/src/parse/lex.hpp b/src/parse/lex.hpp
index a32a569b..aa5ed623 100644
--- a/src/parse/lex.hpp
+++ b/src/parse/lex.hpp
@@ -63,7 +63,27 @@ public:
enum eCoreType datatype() const { return m_datatype; }
uint64_t intval() const { return m_intval; }
double floatval() const { return m_floatval; }
+ bool operator==(const Token& r) const {
+ if(type() != r.type())
+ return false;
+ switch(type())
+ {
+ case TOK_STRING:
+ case TOK_IDENT:
+ case TOK_LIFETIME:
+ return str() == r.str();
+ case TOK_INTEGER:
+ return intval() == r.intval() && datatype() == r.datatype();
+ case TOK_FLOAT:
+ return floatval() == r.floatval() && datatype() == r.datatype();
+ default:
+ return true;
+ }
+ }
+ bool operator!=(const Token& r) { return !(*this == r); }
+ ::std::string to_str() const;
+
void set_pos(Position pos) { m_pos = pos; }
const Position& get_pos() const { return m_pos; }
diff --git a/src/parse/pattern.cpp b/src/parse/pattern.cpp
index 28e7ebaa..6d26cd4a 100644
--- a/src/parse/pattern.cpp
+++ b/src/parse/pattern.cpp
@@ -256,7 +256,8 @@ AST::Pattern Parse_PatternStruct(TokenStream& lex, AST::Path path)
CHECK_TOK(tok, TOK_COLON);
pat = Parse_Pattern(lex);
}
- // TODO: Append
+
+ subpats.push_back( ::std::make_pair(::std::move(field), ::std::move(pat)) );
} while( GET_TOK(tok, lex) == TOK_COMMA );
CHECK_TOK(tok, TOK_BRACE_CLOSE);
diff --git a/src/parse/root.cpp b/src/parse/root.cpp
index 759c65ac..39776689 100644
--- a/src/parse/root.cpp
+++ b/src/parse/root.cpp
@@ -825,6 +825,18 @@ void Parse_Use(TokenStream& lex, ::std::function<void(AST::Path, ::std::string)>
Token tok;
AST::Path path = AST::Path( AST::Path::TagAbsolute() );
+ // Leading :: is allowed and ignored for the $crate feature
+ if( LOOK_AHEAD(lex) == TOK_DOUBLE_COLON ) {
+ GET_TOK(tok, lex);
+ // HACK! mrustc emits $crate as `::"crate-name"`
+ if( LOOK_AHEAD(lex) == TOK_STRING )
+ {
+ GET_CHECK_TOK(tok, lex, TOK_STRING);
+ path.set_crate(tok.str());
+ GET_CHECK_TOK(tok, lex, TOK_DOUBLE_COLON);
+ }
+ }
+
switch( GET_TOK(tok, lex) )
{
case TOK_RWORD_SELF: