From 8dbff1e87c19fcdfec1ae08964b1318244132a03 Mon Sep 17 00:00:00 2001 From: "scheglov@google.com" Date: Mon, 8 Oct 2012 20:15:24 +0000 Subject: [PATCH] Ignore 'dart-ext:' scheme, allow 'native' in such files R=danrubel@google.com BUG= Review URL: https://codereview.chromium.org//11088009 git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@13371 260f80e4-7a28-3924-810f-c04153c831b5 --- .../google/dart/compiler/DartCompiler.java | 4 ++ .../dart/compiler/parser/DartParser.java | 23 +++++++--- .../inc/IncrementalCompilation2Test.java | 42 ++++++++++++++++++- .../end2end/inc/MemoryLibrarySource.java | 4 ++ 4 files changed, 67 insertions(+), 6 deletions(-) diff --git a/compiler/java/com/google/dart/compiler/DartCompiler.java b/compiler/java/com/google/dart/compiler/DartCompiler.java index 00d0bcbb261..751c7f6f010 100644 --- a/compiler/java/com/google/dart/compiler/DartCompiler.java +++ b/compiler/java/com/google/dart/compiler/DartCompiler.java @@ -44,6 +44,7 @@ import com.google.dart.compiler.resolver.SupertypeResolver; import com.google.dart.compiler.resolver.TopLevelElementBuilder; import com.google.dart.compiler.type.TypeAnalyzer; import com.google.dart.compiler.util.DefaultTextOutput; +import com.google.dart.compiler.util.apache.StringUtils; import org.kohsuke.args4j.CmdLineException; import org.kohsuke.args4j.CmdLineParser; @@ -880,6 +881,9 @@ public class DartCompiler { private void reportMissingSource(DartCompilerContext context, LibrarySource libSrc, LibraryNode libNode) { + if (libNode != null && StringUtils.startsWith(libNode.getText(), "dart-ext:")) { + return; + } DartCompilationError event = new DartCompilationError(libNode, DartCompilerErrorCode.MISSING_SOURCE, libNode.getText()); diff --git a/compiler/java/com/google/dart/compiler/parser/DartParser.java b/compiler/java/com/google/dart/compiler/parser/DartParser.java index 42a5ca52864..1880f3c35f8 100644 --- a/compiler/java/com/google/dart/compiler/parser/DartParser.java +++ b/compiler/java/com/google/dart/compiler/parser/DartParser.java @@ -104,6 +104,7 @@ import com.google.dart.compiler.ast.Modifiers; import com.google.dart.compiler.metrics.CompilerMetrics; import com.google.dart.compiler.parser.DartScanner.Location; import com.google.dart.compiler.util.Lists; +import com.google.dart.compiler.util.apache.StringUtils; import java.io.IOException; import java.io.Reader; @@ -124,7 +125,7 @@ public class DartParser extends CompletionHooksParserBase { private final String sourceCode; private final boolean isDietParse; private final Set prefixes; - private final boolean corelibParse; + private boolean allowNativeKeyword; private final Set errorHistory = new HashSet(); private boolean isParsingInterface; private boolean isTopLevelAbstract; @@ -231,7 +232,7 @@ public class DartParser extends CompletionHooksParserBase { this.sourceCode = sourceCode; this.isDietParse = isDietParse; this.prefixes = prefixes; - this.corelibParse = source != null && PackageLibraryManager.isDartUri(source.getUri()); + this.allowNativeKeyword = source != null && PackageLibraryManager.isDartUri(source.getUri()); } public static String read(Source source) throws IOException { @@ -647,7 +648,12 @@ public class DartParser extends CompletionHooksParserBase { beginLiteral(); expect(Token.STRING); DartStringLiteral libUri = done(DartStringLiteral.get(ctx.getTokenString())); - + + // allow "native" if we have "dart-ext:" import + if (StringUtils.startsWith(libUri.getValue(), "dart-ext:")) { + allowNativeKeyword = true; + } + DartIdentifier prefix = null; if (optional(Token.AS)) { prefix = parseIdentifier(); @@ -696,9 +702,16 @@ public class DartParser extends CompletionHooksParserBase { protected DartImportDirective parseObsoleteImportDirective() { expect(Token.IMPORT); expect(Token.LPAREN); + beginLiteral(); expect(Token.STRING); DartStringLiteral libUri = done(DartStringLiteral.get(ctx.getTokenString())); + + // allow "native" if we have "dart-ext:" import + if (StringUtils.startsWith(libUri.getValue(), "dart-ext:")) { + allowNativeKeyword = true; + } + DartBooleanLiteral export = null; List combinators = new ArrayList(); DartStringLiteral prefix = null; @@ -953,7 +966,7 @@ public class DartParser extends CompletionHooksParserBase { if (isParsingInterface) { reportError(position(), ParserErrorCode.NATIVE_ONLY_CLASS); } - if (!corelibParse) { + if (!allowNativeKeyword) { reportError(position(), ParserErrorCode.NATIVE_ONLY_CORE_LIB); } beginLiteral(); @@ -1768,7 +1781,7 @@ public class DartParser extends CompletionHooksParserBase { if (!optionalPseudoKeyword(NATIVE_KEYWORD)) { throw new AssertionError(); } - if (!corelibParse) { + if (!allowNativeKeyword) { reportError(position(), ParserErrorCode.NATIVE_ONLY_CORE_LIB); } DartExpression body = null; diff --git a/compiler/javatests/com/google/dart/compiler/end2end/inc/IncrementalCompilation2Test.java b/compiler/javatests/com/google/dart/compiler/end2end/inc/IncrementalCompilation2Test.java index 109aa5f5ff8..c13c42aa6b0 100644 --- a/compiler/javatests/com/google/dart/compiler/end2end/inc/IncrementalCompilation2Test.java +++ b/compiler/javatests/com/google/dart/compiler/end2end/inc/IncrementalCompilation2Test.java @@ -1262,7 +1262,7 @@ public class IncrementalCompilation2Test extends CompilerTestCase { compile(); ErrorExpectation.assertErrors(errors); } - + /** * Investigation of failing "language/ct_const4_test". */ @@ -1287,6 +1287,46 @@ public class IncrementalCompilation2Test extends CompilerTestCase { assertErrors(errors); } + /** + * Internals of Dart use "dart-ext:" import scheme, and these libraries are allowed to use + * "native". New import syntax. + */ + public void test_useNative_withDartExt_new() throws Exception { + appSource.setContent( + APP, + makeCode( + "// filler filler filler filler filler filler filler filler filler filler filler", + "library A;", + "import 'dart-ext:test_extension';", + "class A {", + " static int ifNull(a, b) native 'TestExtension_IfNull';", + "}", + "")); + // do compile, no errors expected + compile(); + assertErrors(errors); + } + + /** + * Internals of Dart use "dart-ext:" import scheme, and these libraries are allowed to use + * "native". Obsolete import syntax. + */ + public void test_useNative_withDartExt_obsolete() throws Exception { + appSource.setContent( + APP, + makeCode( + "// filler filler filler filler filler filler filler filler filler filler filler", + "#library('A');", + "#import('dart-ext:test_extension');", + "class A {", + " static int ifNull(a, b) native 'TestExtension_IfNull';", + "}", + "")); + // do compile, no errors expected + compile(); + assertErrors(errors); + } + private void assertAppBuilt() { didWrite(APP, EXTENSION_DEPS); } diff --git a/compiler/javatests/com/google/dart/compiler/end2end/inc/MemoryLibrarySource.java b/compiler/javatests/com/google/dart/compiler/end2end/inc/MemoryLibrarySource.java index 4d7aa25052c..a2fd2c447f8 100644 --- a/compiler/javatests/com/google/dart/compiler/end2end/inc/MemoryLibrarySource.java +++ b/compiler/javatests/com/google/dart/compiler/end2end/inc/MemoryLibrarySource.java @@ -9,6 +9,7 @@ import com.google.dart.compiler.LibrarySource; import com.google.dart.compiler.Source; import com.google.dart.compiler.UrlDartSource; +import java.io.FileNotFoundException; import java.io.IOException; import java.io.Reader; import java.io.StringReader; @@ -67,6 +68,9 @@ public class MemoryLibrarySource implements LibrarySource { if (IO_EXCEPTION_CONTENT.equals(content)) { throw new IOException("simulated"); } + if (content == null) { + throw new FileNotFoundException(libName); + } return new StringReader(content); }