summaryrefslogtreecommitdiff
path: root/src/ast
diff options
context:
space:
mode:
authorJohn Hodge <tpg@mutabah.net>2016-02-21 21:19:31 +1100
committerJohn Hodge <tpg@mutabah.net>2016-02-21 21:19:31 +1100
commitea3291cbe5192fa20f1a5d4dc45d776e48c21b7d (patch)
tree65e465e32a2c38889b4fdda86509eb978250287f /src/ast
parente674cb99edf50de91f8f5aed3188e30ffb7c88d7 (diff)
downloadmrust-ea3291cbe5192fa20f1a5d4dc45d776e48c21b7d.tar.gz
Fixing parser deficiencies
Diffstat (limited to 'src/ast')
-rw-r--r--src/ast/ast.cpp3
-rw-r--r--src/ast/ast.hpp52
-rw-r--r--src/ast/pattern.cpp15
-rw-r--r--src/ast/pattern.hpp21
4 files changed, 67 insertions, 24 deletions
diff --git a/src/ast/ast.cpp b/src/ast/ast.cpp
index 7d6185e3..c8ad705f 100644
--- a/src/ast/ast.cpp
+++ b/src/ast/ast.cpp
@@ -500,7 +500,8 @@ SERIALISE_TYPE(ExternCrate::, "AST_ExternCrate", {
},{
})
-SERIALISE_TYPE_A(MacroItem::, "AST_MacroItem", {
+SERIALISE_TYPE_A(MacroInvocation::, "AST_MacroInvocation", {
+ s.item(m_attrs);
s.item(m_macro_name);
s.item(m_ident);
s.item(m_input);
diff --git a/src/ast/ast.hpp b/src/ast/ast.hpp
index 9c644bf6..a703da89 100644
--- a/src/ast/ast.hpp
+++ b/src/ast/ast.hpp
@@ -398,6 +398,7 @@ class Trait:
::std::vector<AST::Path> m_supertraits;
ItemList<TypeAlias> m_types;
ItemList<Function> m_functions;
+ ItemList<Static> m_statics;
public:
Trait() {}
Trait(MetaItems attrs, GenericParams params, ::std::vector<Path> supertraits):
@@ -412,6 +413,7 @@ public:
const ::std::vector<Path>& supertraits() const { return m_supertraits; }
const ItemList<Function>& functions() const { return m_functions; }
const ItemList<TypeAlias>& types() const { return m_types; }
+ const ItemList<Static>& statics() const { return m_statics; }
GenericParams& params() { return m_params; }
::std::vector<Path>& supertraits() { return m_supertraits; }
@@ -424,6 +426,9 @@ public:
void add_function(::std::string name, Function fcn) {
m_functions.push_back( Item<Function>(::std::move(name), ::std::move(fcn), true) );
}
+ void add_static(::std::string name, Static v) {
+ m_statics.push_back( Item<Static>(mv$(name), mv$(v), true) );
+ }
bool has_named_item(const ::std::string& name, bool& out_is_fcn) const {
for( const auto& f : m_functions )
@@ -450,25 +455,31 @@ struct EnumVariant:
MetaItems m_attrs;
::std::string m_name;
::std::vector<TypeRef> m_sub_types;
- int64_t m_value;
+ ::std::vector<StructItem> m_fields;
+ AST::Expr m_value;
- EnumVariant():
- m_value(0)
+ EnumVariant()
{
}
- EnumVariant(MetaItems attrs, ::std::string name, int64_t value):
+ EnumVariant(MetaItems attrs, ::std::string name, Expr&& value):
+ m_attrs( mv$(attrs) ),
+ m_name( mv$(name) ),
+ m_value( mv$(value) )
+ {
+ }
+
+ EnumVariant(MetaItems attrs, ::std::string name, ::std::vector<TypeRef> sub_types):
m_attrs( move(attrs) ),
m_name( ::std::move(name) ),
- m_value( value )
+ m_sub_types( ::std::move(sub_types) )
{
}
- EnumVariant(MetaItems attrs, ::std::string name, ::std::vector<TypeRef> sub_types):
+ EnumVariant(MetaItems attrs, ::std::string name, ::std::vector<StructItem> fields):
m_attrs( move(attrs) ),
m_name( ::std::move(name) ),
- m_sub_types( ::std::move(sub_types) ),
- m_value(0)
+ m_fields( ::std::move(fields) )
{
}
@@ -571,6 +582,7 @@ class Impl:
ItemList<TypeRef> m_types;
ItemList<Function> m_functions;
+ ItemList<Static> m_statics;
::std::vector< ::std::pair< ::std::vector<TypeRef>, Impl > > m_concrete_impls;
public:
@@ -586,6 +598,9 @@ public:
void add_type(bool is_public, ::std::string name, TypeRef type) {
m_types.push_back( Item<TypeRef>( ::std::move(name), ::std::move(type), is_public ) );
}
+ void add_static(bool is_public, ::std::string name, Static v) {
+ m_statics.push_back( Item<Static>( mv$(name), mv$(v), is_public ) );
+ }
const ImplDef& def() const { return m_def; }
const ItemList<Function>& functions() const { return m_functions; }
@@ -615,7 +630,7 @@ class Module;
typedef void fcn_visitor_t(const AST::Crate& crate, const AST::Module& mod, Function& fcn);
-class MacroItem:
+class MacroInvocation:
public Serialisable
{
MetaItems m_attrs;
@@ -623,11 +638,11 @@ class MacroItem:
::std::string m_ident;
TokenTree m_input;
public:
- MacroItem()
+ MacroInvocation()
{
}
- MacroItem(MetaItems attrs, ::std::string macro, ::std::string ident, TokenTree input):
+ MacroInvocation(MetaItems attrs, ::std::string macro, ::std::string ident, TokenTree input):
m_attrs( mv$(attrs) ),
m_macro_name( mv$(macro) ),
m_ident( mv$(ident) ),
@@ -635,7 +650,18 @@ public:
{
}
+ static ::std::unique_ptr<MacroInvocation> from_deserialiser(Deserialiser& s) {
+ auto i = new MacroInvocation;
+ s.item( *i );
+ return ::std::unique_ptr<MacroInvocation>(i);
+ }
+
SERIALISABLE_PROTOTYPES();
+
+ friend ::std::ostream& operator<<(::std::ostream& os, const MacroInvocation& x) {
+ os << x.m_attrs << x.m_macro_name << "! " << x.m_ident << x.m_input;
+ return os;
+ }
};
/// Representation of a parsed (and being converted) function
@@ -664,7 +690,7 @@ class Module:
itemlist_macros_t m_macros;
macro_imports_t m_macro_imports; // module => macro
::std::vector< ItemNS<const MacroRules*> > m_macro_import_res; // Vec of imported macros (not serialised)
- ::std::vector<MacroItem> m_macro_invocations;
+ ::std::vector<MacroInvocation> m_macro_invocations;
@@ -728,7 +754,7 @@ public:
m_macros.push_back( Item<MacroRules>( move(name), move(macro), is_exported ) );
}
void add_macro_import(const Crate& crate, ::std::string mod, ::std::string name);
- void add_macro_invocation(MacroItem item) {
+ void add_macro_invocation(MacroInvocation item) {
m_macro_invocations.push_back( mv$(item) );
}
diff --git a/src/ast/pattern.cpp b/src/ast/pattern.cpp
index f2279a55..7ebdb35d 100644
--- a/src/ast/pattern.cpp
+++ b/src/ast/pattern.cpp
@@ -15,12 +15,15 @@ namespace AST {
{
os << "Pattern(" << pat.m_binding << " @ ";
TU_MATCH(Pattern::Data, (pat.m_data), (ent),
- (Any,
- os << "_";
- ),
(MaybeBind,
os << "?";
),
+ (Macro,
+ os << *ent.inv;
+ ),
+ (Any,
+ os << "_";
+ ),
(Box,
os << "box " << *ent.sub;
),
@@ -62,6 +65,9 @@ SERIALISE_TYPE(Pattern::, "Pattern", {
),
(MaybeBind,
),
+ (Macro,
+ s.item( e.inv );
+ ),
(Box,
s << e.sub;
),
@@ -94,6 +100,9 @@ SERIALISE_TYPE(Pattern::, "Pattern", {
_D(Any, )
_D(MaybeBind,
)
+ _D(Macro,
+ s.item( ent.inv );
+ )
_D(Box,
s.item( ent.sub );
)
diff --git a/src/ast/pattern.hpp b/src/ast/pattern.hpp
index f28fe8e3..6cd78e91 100644
--- a/src/ast/pattern.hpp
+++ b/src/ast/pattern.hpp
@@ -11,6 +11,7 @@ namespace AST {
using ::std::unique_ptr;
using ::std::move;
+class MacroInvocation;
class ExprNode;
@@ -19,8 +20,9 @@ class Pattern:
{
public:
TAGGED_UNION(Data, Any,
- (Any, () ),
(MaybeBind, () ),
+ (Macro, (unique_ptr<::AST::MacroInvocation> inv;) ),
+ (Any, () ),
(Box, (unique_ptr<Pattern> sub;) ),
(Ref, (bool mut; unique_ptr<Pattern> sub;) ),
(Value, (unique_ptr<ExprNode> start; unique_ptr<ExprNode> end;) ),
@@ -36,6 +38,17 @@ public:
Pattern()
{}
+ struct TagMaybeBind {};
+ Pattern(TagMaybeBind, ::std::string name):
+ m_binding(name),
+ m_data( Data::make_MaybeBind({}) )
+ {}
+
+ struct TagMacro {};
+ Pattern(TagMacro, unique_ptr<::AST::MacroInvocation> inv):
+ m_data( Data::make_Macro({mv$(inv)}) )
+ {}
+
// Wildcard = '..', distinct from '_'
// TODO: Store wildcard as a different pattern type
struct TagWildcard {};
@@ -47,12 +60,6 @@ public:
m_binding(name)
{}
- struct TagMaybeBind {};
- Pattern(TagMaybeBind, ::std::string name):
- m_binding(name),
- m_data( Data::make_MaybeBind({}) )
- {}
-
struct TagBox {};
Pattern(TagBox, Pattern sub):
m_data( Data::make_Box({ unique_ptr<Pattern>(new Pattern(mv$(sub))) }) )