summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Hodge <tpg@mutabah.net>2016-10-03 08:19:44 +0800
committerJohn Hodge <tpg@mutabah.net>2016-10-03 08:19:44 +0800
commit45e899cb3091ac75000f6848d4752cbf4b1aa040 (patch)
tree43e339a03ae293d51a548f854c8c20be71f29aca
parent10ce63e0225d65fcff3070f75607dedcf282ca02 (diff)
downloadmrust-45e899cb3091ac75000f6848d4752cbf4b1aa040.tar.gz
HIR+AST - Handle bytestring patterns
-rw-r--r--src/ast/pattern.cpp4
-rw-r--r--src/ast/pattern.hpp1
-rw-r--r--src/hir/from_ast.cpp3
-rw-r--r--src/hir/pattern.cpp6
-rw-r--r--src/hir/pattern.hpp1
-rw-r--r--src/hir/visitor.cpp2
-rw-r--r--src/hir_typeck/expr_cs.cpp3
-rw-r--r--src/mir/from_hir_match.cpp1
-rw-r--r--src/parse/pattern.cpp3
9 files changed, 22 insertions, 2 deletions
diff --git a/src/ast/pattern.cpp b/src/ast/pattern.cpp
index 06b96b48..df1d0caf 100644
--- a/src/ast/pattern.cpp
+++ b/src/ast/pattern.cpp
@@ -51,6 +51,9 @@ namespace AST {
(String,
os << "\"" << e << "\"";
),
+ (ByteString,
+ os << "b\"" << e.v << "\"";
+ ),
(Named,
os << e;
)
@@ -184,6 +187,7 @@ AST::Pattern AST::Pattern::clone() const
(Integer, return Value(e);),
(Float, return Value(e);),
(String, return Value(e);),
+ (ByteString, return Value(e);),
(Named, return Value::make_Named( AST::Path(e) );)
)
throw "";
diff --git a/src/ast/pattern.hpp b/src/ast/pattern.hpp
index f6cf6ea8..3a1dacc5 100644
--- a/src/ast/pattern.hpp
+++ b/src/ast/pattern.hpp
@@ -57,6 +57,7 @@ public:
double value;
}),
(String, ::std::string),
+ (ByteString, struct { ::std::string v; }),
(Named, Path)
);
diff --git a/src/hir/from_ast.cpp b/src/hir/from_ast.cpp
index 74697784..06102fe5 100644
--- a/src/hir/from_ast.cpp
+++ b/src/hir/from_ast.cpp
@@ -391,6 +391,9 @@
(String,
return ::HIR::Pattern::Value::make_String(e);
),
+ (ByteString,
+ return ::HIR::Pattern::Value::make_ByteString({e.v});
+ ),
(Named,
return ::HIR::Pattern::Value::make_Named( {LowerHIR_Path(sp, e), nullptr} );
)
diff --git a/src/hir/pattern.cpp b/src/hir/pattern.cpp
index a2c9ef27..5a6cc9c1 100644
--- a/src/hir/pattern.cpp
+++ b/src/hir/pattern.cpp
@@ -21,6 +21,9 @@ namespace HIR {
(String,
os << "\"" << e << "\"";
),
+ (ByteString,
+ os << "b\"" << e.v << "\"";
+ ),
(Named,
os << e.path;
)
@@ -163,6 +166,9 @@ namespace {
(String,
return ::HIR::Pattern::Value::make_String(e);
),
+ (ByteString,
+ return ::HIR::Pattern::Value(e);
+ ),
(Named,
return ::HIR::Pattern::Value::make_Named({ e.path.clone(), e.binding });
)
diff --git a/src/hir/pattern.hpp b/src/hir/pattern.hpp
index 17f06a29..de691b3a 100644
--- a/src/hir/pattern.hpp
+++ b/src/hir/pattern.hpp
@@ -60,6 +60,7 @@ struct Pattern
double value;
}),
(String, ::std::string),
+ (ByteString, struct { ::std::string v; }),
(Named, struct {
Path path;
const ::HIR::Constant* binding;
diff --git a/src/hir/visitor.cpp b/src/hir/visitor.cpp
index 55cc9e66..ab7e67de 100644
--- a/src/hir/visitor.cpp
+++ b/src/hir/visitor.cpp
@@ -391,6 +391,8 @@ void ::HIR::Visitor::visit_pattern_val(::HIR::Pattern::Value& val)
),
(String,
),
+ (ByteString,
+ ),
(Named,
this->visit_path(e.path, ::HIR::Visitor::PathContext::VALUE);
)
diff --git a/src/hir_typeck/expr_cs.cpp b/src/hir_typeck/expr_cs.cpp
index 7d219e4c..2dd0f164 100644
--- a/src/hir_typeck/expr_cs.cpp
+++ b/src/hir_typeck/expr_cs.cpp
@@ -3004,6 +3004,9 @@ void Context::add_binding(const Span& sp, ::HIR::Pattern& pat, const ::HIR::Type
(String,
context.equate_types(sp, type, ::HIR::TypeRef::new_borrow( ::HIR::BorrowType::Shared, ::HIR::TypeRef(::HIR::CoreType::Str) ));
),
+ (ByteString,
+ context.equate_types(sp, type, ::HIR::TypeRef::new_borrow( ::HIR::BorrowType::Shared, ::HIR::TypeRef::new_slice(::HIR::CoreType::U8) ));
+ ),
(Named,
// TODO: Get type of the value and equate it
)
diff --git a/src/mir/from_hir_match.cpp b/src/mir/from_hir_match.cpp
index 66625753..083dc0ce 100644
--- a/src/mir/from_hir_match.cpp
+++ b/src/mir/from_hir_match.cpp
@@ -1294,6 +1294,7 @@ void PatternRulesetBuilder::append_from(const Span& sp, const ::HIR::Pattern& pa
this->append_from( sp, *pe.sub, *e.inner );
),
(Value,
+ // TODO: Check type? Also handle named values and byte strings.
if( pe.val.is_String() ) {
const auto& s = pe.val.as_String();
this->push_rule( PatternRule::make_Value(s) );
diff --git a/src/parse/pattern.cpp b/src/parse/pattern.cpp
index 3adce90d..13b94753 100644
--- a/src/parse/pattern.cpp
+++ b/src/parse/pattern.cpp
@@ -230,8 +230,7 @@ AST::Pattern Parse_PatternReal1(TokenStream& lex, bool is_refutable)
case TOK_STRING:
return AST::Pattern( AST::Pattern::TagValue(), AST::Pattern::Value::make_String( mv$(tok.str()) ) );
case TOK_BYTESTRING:
- // TODO: Differentiate byte and UTF-8 strings
- return AST::Pattern( AST::Pattern::TagValue(), AST::Pattern::Value::make_String( mv$(tok.str()) ) );
+ return AST::Pattern( AST::Pattern::TagValue(), AST::Pattern::Value::make_ByteString({ mv$(tok.str()) }) );
case TOK_PAREN_OPEN:
return AST::Pattern( AST::Pattern::TagTuple(), Parse_PatternTuple(lex, is_refutable) );
case TOK_SQUARE_OPEN: