Index: runtime/vm/parser.cc |
diff --git a/runtime/vm/parser.cc b/runtime/vm/parser.cc |
index a33c4cce1f4400cb53017017785efef1431204ac..bce58fd5108121f331349452c137b0f103e52346 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,46 @@ 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); |
+ key = Symbols::FromConcatAll(pieces); |
+ AstNode* valueNode = NULL; |
+ if (CurrentToken() == Token::kEQ) { |
+ ConsumeToken(); |
+ valueNode = ParseStringLiteral(false); |
hausner
2015/10/13 23:48:36
Can't just assume that the next token is indeed a
floitsch
2015/10/14 13:20:55
good catch. thanks.
I mistakenly assumed the "Pars
|
+ } |
+ ExpectToken(Token::kRPAREN); |
+ AstNode* conditional_url_literal = ParseStringLiteral(false); |
hausner
2015/10/13 23:48:36
Ditto.
floitsch
2015/10/14 13:20:55
Done.
|
+ |
+ // If there was already a condition that triggered, don't try to match |
+ // again. |
+ if (condition_triggered) continue; |
+ // Check if this conditional line overrides the default import. |
+ const String& value = valueNode == NULL |
hausner
2015/10/13 23:48:36
Instead of computing the value here, you could dec
floitsch
2015/10/14 13:20:55
Tried to do it, but then I had to allocate a new h
|
+ ? Symbols::True() |
+ : String::Cast(valueNode->AsLiteralNode()->literal()); |
+ // 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()); |