Exemplo n.º 1
0
  /**
   * Parse the specified source. Any exceptions thrown by the {@link DartParser} will be logged and
   * a {@link DartModelException} thrown.
   *
   * @param sourceRef the Dart source being parsed
   * @param source the source to be parsed (not <code>null</code>)
   * @param preserveComments <code>true</code> if comments are to be preserved
   * @param parseErrors a collection to which parse errors are appended or <code>null</code> if
   *     parse errors should be ignored
   * @return the parse result
   */
  public static DartUnit parseSource(
      DartSource sourceRef,
      String source,
      boolean preserveComments,
      Collection<DartCompilationError> parseErrors)
      throws DartModelException {

    long start = System.currentTimeMillis();

    ParserRunnable runnable = new ParserRunnable(sourceRef, source, preserveComments, parseErrors);
    runnable.runSafe();

    long elapsed = System.currentTimeMillis() - start;
    Instrumentation.metric("DartCompilerUtils-parseSource", elapsed).log();
    Instrumentation.operation("DartCompilerUtils-parseSource", elapsed)
        .with("sourceRef.Name", sourceRef.getName())
        .with("source", source)
        .with("parseErrors", parseErrors != null ? parseErrors.size() : 0)
        .log();

    if (runnable.exception != null) {
      throw new DartModelException(
          new CoreException(
              new Status(
                  IStatus.ERROR,
                  DartCore.PLUGIN_ID,
                  "Failed to parse " + sourceRef.getName(),
                  runnable.exception)));
    }
    return runnable.result;
  }
Exemplo n.º 2
0
  /**
   * Process the given resource within the context of the given working set in order to record the
   * data and relationships found within the resource.
   */
  public void indexResource(File libraryFile, File sourceFile, DartUnit dartUnit)
      throws DartModelException {

    // Get the LibrarySource

    LibrarySource librarySource = dartUnit.getLibrary().getSource();

    // Get the DartLibrary

    DartLibraryImpl library;
    IResource resource = ResourceUtil.getResource(libraryFile);
    if (resource == null) {
      library = new DartLibraryImpl(librarySource);
    } else {
      DartElement element = DartCore.create(resource);
      if (element instanceof CompilationUnitImpl) {
        element = ((CompilationUnitImpl) element).getLibrary();
      }
      if (!(element instanceof DartLibrary)) {
        DartCore.logError("Expected library to be associated with " + libraryFile);
        return;
      }
      library = (DartLibraryImpl) element;
    }

    // Get the CompilationUnit

    DartSource unitSource = (DartSource) dartUnit.getSourceInfo().getSource();
    CompilationUnit compilationUnit;
    IResource res = ResourceUtil.getResource(sourceFile);
    if (res != null) {
      DefaultWorkingCopyOwner workingCopy = DefaultWorkingCopyOwner.getInstance();
      compilationUnit = new CompilationUnitImpl(library, (IFile) res, workingCopy);
    } else {
      String relPath = unitSource.getRelativePath();
      compilationUnit = new ExternalCompilationUnitImpl(library, relPath, unitSource);
    }

    URI unitUri = unitSource.getUri();
    Resource indexerResource;
    if (PackageLibraryManager.isDartUri(unitUri)) {
      indexerResource =
          new Resource(
              ResourceFactory.composeResourceId(
                  librarySource.getUri().toString(), unitUri.toString()));
    } else if (PackageLibraryManager.isPackageUri(unitUri)) {
      indexerResource =
          new Resource(
              ResourceFactory.composeResourceId(
                  libraryFile.toURI().toString(), sourceFile.toURI().toString()));
    } else {
      indexerResource = ResourceFactory.getResource(compilationUnit);
    }

    // Queue the resource to be indexed

    indexResource(indexerResource, libraryFile, compilationUnit, dartUnit);
  }
Exemplo n.º 3
0
 private static Map<URI, DartUnit> createMap(Collection<DartUnit> suppliedUnits) {
   if (suppliedUnits == null || suppliedUnits.isEmpty()) {
     return null;
   }
   Map<URI, DartUnit> parsedUnits = new HashMap<URI, DartUnit>(suppliedUnits.size());
   for (DartUnit unit : suppliedUnits) {
     DartSource src = (DartSource) unit.getSourceInfo().getSource();
     URI uri = src.getUri();
     parsedUnits.put(uri, unit);
   }
   return parsedUnits;
 }
Exemplo n.º 4
0
 @Override
 public Void visitStringLiteral(DartStringLiteral node) {
   if (foundElement == null) {
     int start = node.getSourceInfo().getOffset();
     int length = node.getSourceInfo().getLength();
     int end = start + length;
     if (end == 0) {
       return null;
     }
     if (start <= startOffset && end >= endOffset) {
       wordRegion = computeInternalStringRegion(start, length);
       DartNode parent = node.getParent();
       if (parent instanceof DartSourceDirective
           && ((DartSourceDirective) parent).getSourceUri() == node) {
         // resolvedElement = ((DartSourceDirective) parent).getElement();
         DartLibrary library = compilationUnit.getLibrary();
         String fileName = getFileName(library, node.getValue());
         CompilationUnit sourcedUnit = library.getCompilationUnit(fileName);
         if (sourcedUnit != null && sourcedUnit.exists()) {
           foundElement = sourcedUnit;
         }
       } else if (parent instanceof DartResourceDirective
           && ((DartResourceDirective) parent).getResourceUri() == node) {
         // resolvedElement = ((DartResourceDirective) parent).getElement();
         DartLibrary library = compilationUnit.getLibrary();
         try {
           DartSource unitSource = compilationUnit.getSourceRef();
           if (unitSource != null) {
             LibrarySource librarySource = unitSource.getLibrary();
             if (librarySource != null) {
               DartSource resourceSource = librarySource.getSourceFor(node.getValue());
               if (resourceSource != null) {
                 URI resourceUri = resourceSource.getUri();
                 DartResource resource = library.getResource(resourceUri);
                 if (resource != null && resource.exists()) {
                   foundElement = resource;
                 }
               }
             }
           }
         } catch (DartModelException exception) {
           foundElement = null;
         }
       }
       throw new DartElementFoundException();
     }
   }
   return null;
 }
Exemplo n.º 5
0
  /** A synchronized call to {@link DartParser#parseUnit(DartSource)} */
  public static DartUnit secureParseUnit(DartParser parser, DartSource sourceRef) {

    long start = System.currentTimeMillis();

    // All calls to DartC must be synchronized
    synchronized (compilerLock) {
      DartUnit ret = parser.parseUnit();

      long elapsed = System.currentTimeMillis() - start;
      Instrumentation.metric("DartCompilerUtils-secureParseUnit", elapsed).log();

      Instrumentation.operation("DartCompilerUtils-secureParseUnit", elapsed)
          .with("sourceRef.Name", sourceRef != null ? sourceRef.getName() : "null")
          .log();

      return ret;
    }
  }
Exemplo n.º 6
0
  public static DartNode analyzeDelta(
      LibrarySource library,
      String sourceString,
      DartUnit suppliedUnit,
      DartNode completionNode,
      int completionLocation,
      final Collection<DartCompilationError> parseErrors)
      throws DartModelException {

    long start = System.currentTimeMillis();

    if (cachedLibraries.get(library) == null) {
      Collection<DartUnit> parsedUnits = new ArrayList<DartUnit>();
      parsedUnits.add(suppliedUnit);
      LibraryUnit resolvedLib = resolveLibrary(library, parsedUnits, parseErrors);
      synchronized (cachedLibraries) {
        LibraryUnit newLib = cachedLibraries.get(library);
        if (newLib == null) {
          cachedLibraries.put(library, resolvedLib);
        }
      }

      long elapsed = System.currentTimeMillis() - start;
      Instrumentation.metric("DartCompilerUtils-analyzeDelta", elapsed)
          .with("Cached", "false")
          .with("ParsedUnits", parsedUnits.size())
          .with("ParseErrors", parseErrors != null ? parseErrors.size() : 0)
          .with("sourceString-length", sourceString.length())
          .with("CachedLibraries", "false")
          .log();

      Instrumentation.operation("DartCompilerUtils-analyzeDelta", elapsed)
          .with("analyzeDelta.Name", library.getName())
          .with("librarySource.LastModified", library.getLastModified())
          .with("sourceString", sourceString)
          .with("sourceString-length", sourceString.length())
          .with("CachedLibraries", "true")
          .log();

      return completionNode;
    }
    DartSource src = (DartSource) suppliedUnit.getSourceInfo().getSource();
    URI unitUri = src.getUri();
    Map<URI, String> suppliedSources = new HashMap<URI, String>();
    suppliedSources.put(unitUri, sourceString);
    DeltaAnalysisRunnable runnable =
        new DeltaAnalysisRunnable(
            library,
            unitUri,
            suppliedSources,
            suppliedUnit,
            completionNode,
            completionLocation,
            parseErrors);
    runnable.runSafe();

    long elapsed = System.currentTimeMillis() - start;
    Instrumentation.metric("DartCompilerUtils-analyzeDelta", elapsed)
        .with("Cached", "false")
        .with("ParseErrors", parseErrors != null ? parseErrors.size() : 0)
        .with("sourceString-length", sourceString.length())
        .with("CachedLibraries", "false")
        .log();

    Instrumentation.operation("DartCompilerUtils-analyzeDelta", elapsed)
        .with("analyzeDelta.Name", library.getName())
        .with("librarySource.LastModified", library.getLastModified())
        .with("sourceString", sourceString)
        .with("sourceString-length", sourceString.length())
        .with("CachedLibraries", "true")
        .log();

    if (runnable.exception != null) {
      throw new DartModelException(
          new CoreException(
              new Status(
                  IStatus.ERROR,
                  DartCore.PLUGIN_ID,
                  "Failed to parse " + library.getName(),
                  runnable.exception)));
    }
    return runnable.analyzedNode;
  }