Index: runtime/vm/parser.cc |
diff --git a/runtime/vm/parser.cc b/runtime/vm/parser.cc |
index a33c4cce1f4400cb53017017785efef1431204ac..9062b139e8db772e2f69fafe59a75ffa37bea670 100644 |
--- a/runtime/vm/parser.cc |
+++ b/runtime/vm/parser.cc |
@@ -46,6 +46,8 @@ DEFINE_FLAG(bool, load_deferred_eagerly, false, |
DEFINE_FLAG(bool, trace_parser, false, "Trace parser operations."); |
DEFINE_FLAG(bool, warn_mixin_typedef, true, "Warning on legacy mixin typedef."); |
DEFINE_FLAG(bool, link_natives_lazily, false, "Link native calls lazily"); |
+DEFINE_FLAG(bool, conditional_directives, false, |
+ "Enable conditional directives"); |
DECLARE_FLAG(bool, lazy_dispatchers); |
DECLARE_FLAG(bool, load_deferred_eagerly); |
@@ -5832,6 +5834,48 @@ void Parser::ParseLibraryImportExport(intptr_t metadata_pos) { |
ConsumeToken(); |
CheckToken(Token::kSTRING, "library url expected"); |
AstNode* url_literal = ParseStringLiteral(false); |
+ if (FLAG_conditional_directives) { |
+ bool condition_triggered = false; |
+ while (CurrentToken() == Token::kIF) { |
+ // Conditional import: if (env == val) uri. |
+ ConsumeToken(); |
+ ExpectToken(Token::kLPAREN); |
+ // Parse dotted name. |
+ GrowableHandlePtrArray<const String> pieces(Z, 3); |
+ pieces.Add(*ExpectIdentifier("identifier expected")); |
+ while (CurrentToken() == Token::kPERIOD) { |
+ pieces.Add(Symbols::Dot()); |
+ ConsumeToken(); |
+ pieces.Add(*ExpectIdentifier("identifier expected")); |
+ } |
+ String& key = String::ZoneHandle(Z); |
Ivan Posva
2015/12/17 06:29:43
Why is this not a const String&? Also you do not n
floitsch
2015/12/17 17:08:43
Done.
|
+ key = Symbols::FromConcatAll(pieces); |
Ivan Posva
2015/12/17 06:29:43
The key does not need to be a Symbol and should on
floitsch
2015/12/17 17:08:43
There isn't a good way to concatenate strings exce
|
+ AstNode* valueNode = NULL; |
+ if (CurrentToken() == Token::kEQ) { |
+ ConsumeToken(); |
+ CheckToken(Token::kSTRING, "string literal expected"); |
+ valueNode = ParseStringLiteral(false); |
+ } |
+ ExpectToken(Token::kRPAREN); |
+ CheckToken(Token::kSTRING, "library url expected"); |
+ AstNode* conditional_url_literal = ParseStringLiteral(false); |
+ |
+ // If there was already a condition that triggered, don't try to match |
+ // again. |
+ if (condition_triggered) continue; |
Ivan Posva
2015/12/17 06:29:43
VM style is:
if (expr) {
statements;
}
floitsch
2015/12/17 17:08:43
Done.
|
+ // Check if this conditional line overrides the default import. |
+ const String& value = valueNode == NULL |
Ivan Posva
2015/12/17 06:29:43
(valueNode == NULL)
floitsch
2015/12/17 17:08:43
Done.
|
+ ? Symbols::True() |
+ : String::Cast(valueNode->AsLiteralNode()->literal()); |
Ivan Posva
2015/12/17 06:29:43
Please use the same assertions as below on line 58
floitsch
2015/12/17 17:08:43
Done.
|
+ // Call the embedder to supply us with the environment. |
+ const String& env_value = |
+ String::Handle(Api::CallEnvironmentCallback(isolate(), key)); |
+ if (!env_value.IsNull() && env_value.Equals(value)) { |
+ condition_triggered = true; |
+ url_literal = conditional_url_literal; |
+ } |
+ } |
+ } |
ASSERT(url_literal->IsLiteralNode()); |
ASSERT(url_literal->AsLiteralNode()->literal().IsString()); |
const String& url = String::Cast(url_literal->AsLiteralNode()->literal()); |