Package com.google.dart.engine.ast

Examples of com.google.dart.engine.ast.StringLiteral


  @Override
  public Void visitImportDirective(ImportDirective directive) {

    // Don't bother showing a suggestion if there is a more important issue to be solved.
    StringLiteral uriLiteral = directive.getUri();
    if (uriLiteral == null) {
      return null;
    }
    String uriContent = uriLiteral.getStringValue();
    if (uriContent == null) {
      return null;
    }
    uriContent = uriContent.trim();
View Full Code Here


      // Don't report errors already reported by ParseDartTask#resolveDirective
      if (directive.validate() != null) {
        return;
      }
    }
    StringLiteral uriLiteral = directive.getUri();
    errorListener.onError(new AnalysisError(
        librarySource,
        uriLiteral.getOffset(),
        uriLiteral.getLength(),
        CompileTimeErrorCode.URI_DOES_NOT_EXIST,
        directive.getUriContent()));
  }
View Full Code Here

   * @param errorListener the error listener to which errors should be reported
   * @return the result of resolving the URI against the URI of the library
   */
  public static Source resolveDirective(AnalysisContext context, Source librarySource,
      UriBasedDirective directive, AnalysisErrorListener errorListener) {
    StringLiteral uriLiteral = directive.getUri();
    String uriContent = uriLiteral.getStringValue();
    if (uriContent != null) {
      uriContent = uriContent.trim();
      directive.setUriContent(uriContent);
    }
    UriValidationCode code = directive.validate();
    if (code == null) {
      String encodedUriContent = UriUtilities.encode(uriContent);
      Source source = context.getSourceFactory().resolveUri(librarySource, encodedUriContent);
      directive.setSource(source);
      return source;
    }
    if (code == UriValidationCode.URI_WITH_DART_EXT_SCHEME) {
      return null;
    }
    if (code == UriValidationCode.URI_WITH_INTERPOLATION) {
      errorListener.onError(new AnalysisError(
          librarySource,
          uriLiteral.getOffset(),
          uriLiteral.getLength(),
          CompileTimeErrorCode.URI_WITH_INTERPOLATION));
      return null;
    }
    if (code == UriValidationCode.INVALID_URI) {
      errorListener.onError(new AnalysisError(
          librarySource,
          uriLiteral.getOffset(),
          uriLiteral.getLength(),
          CompileTimeErrorCode.INVALID_URI,
          uriContent));
      return null;
    }
    throw new RuntimeException("Failed to handle validation code: " + code);
View Full Code Here

          if (importedSource != null) {
            // The imported source will be null if the URI in the import directive was invalid.
            Library importedLibrary = libraryMap.get(importedSource);
            if (importedLibrary != null) {
              ImportElementImpl importElement = new ImportElementImpl(directive.getOffset());
              StringLiteral uriLiteral = importDirective.getUri();
              importElement.setUriOffset(uriLiteral.getOffset());
              importElement.setUriEnd(uriLiteral.getEnd());
              importElement.setUri(uriContent);
              importElement.setDeferred(importDirective.getDeferredToken() != null);
              importElement.setCombinators(buildCombinators(importDirective));
              LibraryElement importedLibraryElement = importedLibrary.getLibraryElement();
              if (importedLibraryElement != null) {
                importElement.setImportedLibrary(importedLibraryElement);
              }
              SimpleIdentifier prefixNode = ((ImportDirective) directive).getPrefix();
              if (prefixNode != null) {
                importElement.setPrefixOffset(prefixNode.getOffset());
                String prefixName = prefixNode.getName();
                PrefixElementImpl prefix = nameToPrefixMap.get(prefixName);
                if (prefix == null) {
                  prefix = new PrefixElementImpl(prefixNode);
                  nameToPrefixMap.put(prefixName, prefix);
                }
                importElement.setPrefix(prefix);
                prefixNode.setStaticElement(prefix);
              }
              directive.setElement(importElement);
              imports.add(importElement);

              if (analysisContext.computeKindOf(importedSource) != SourceKind.LIBRARY) {
                ErrorCode errorCode = importElement.isDeferred()
                    ? StaticWarningCode.IMPORT_OF_NON_LIBRARY
                    : CompileTimeErrorCode.IMPORT_OF_NON_LIBRARY;
                errorListener.onError(new AnalysisError(
                    library.getLibrarySource(),
                    uriLiteral.getOffset(),
                    uriLiteral.getLength(),
                    errorCode,
                    uriLiteral.toSource()));
              }
            }
          }
        } else if (directive instanceof ExportDirective) {
          ExportDirective exportDirective = (ExportDirective) directive;
          Source exportedSource = exportDirective.getSource();
          if (exportedSource != null) {
            // The exported source will be null if the URI in the export directive was invalid.
            Library exportedLibrary = libraryMap.get(exportedSource);
            if (exportedLibrary != null) {
              ExportElementImpl exportElement = new ExportElementImpl();
              StringLiteral uriLiteral = exportDirective.getUri();
              exportElement.setUriOffset(uriLiteral.getOffset());
              exportElement.setUriEnd(uriLiteral.getEnd());
              exportElement.setUri(exportDirective.getUriContent());
              exportElement.setCombinators(buildCombinators(exportDirective));
              LibraryElement exportedLibraryElement = exportedLibrary.getLibraryElement();
              if (exportedLibraryElement != null) {
                exportElement.setExportedLibrary(exportedLibraryElement);
              }
              directive.setElement(exportElement);
              exports.add(exportElement);

              if (analysisContext.computeKindOf(exportedSource) != SourceKind.LIBRARY) {
                errorListener.onError(new AnalysisError(
                    library.getLibrarySource(),
                    uriLiteral.getOffset(),
                    uriLiteral.getLength(),
                    CompileTimeErrorCode.EXPORT_OF_NON_LIBRARY,
                    uriLiteral.toSource()));
              }
            }
          }
        }
      }
View Full Code Here

   * @param librarySource the source representing the library containing the directive
   * @param directive the directive which URI should be resolved
   * @return the result of resolving the URI against the URI of the library
   */
  private Source resolveSource(Source librarySource, UriBasedDirective directive) {
    StringLiteral uriLiteral = directive.getUri();
    if (uriLiteral instanceof StringInterpolation) {
      return null;
    }
    String uriContent = uriLiteral.getStringValue().trim();
    if (uriContent == null || uriContent.isEmpty()) {
      return null;
    }
    uriContent = UriUtilities.encode(uriContent);
    return analysisContext.getSourceFactory().resolveUri(librarySource, uriContent);
View Full Code Here

          libraryNameNode = ((LibraryDirective) directive).getName();
          directivesToResolve.add(directive);
        }
      } else if (directive instanceof PartDirective) {
        PartDirective partDirective = (PartDirective) directive;
        StringLiteral partUri = partDirective.getUri();
        Source partSource = partDirective.getSource();
        if (analysisContext.exists(partSource)) {
          hasPartDirective = true;
          CompilationUnit partUnit = library.getAST(partSource);
          CompilationUnitElementImpl part = builder.buildCompilationUnit(partSource, partUnit);
          part.setUriOffset(partUri.getOffset());
          part.setUriEnd(partUri.getEnd());
          part.setUri(partDirective.getUriContent());
          //
          // Validate that the part contains a part-of directive with the same name as the library.
          //
          String partLibraryName = getPartLibraryName(partSource, partUnit, directivesToResolve);
          if (partLibraryName == null) {
            errorListener.onError(new AnalysisError(
                librarySource,
                partUri.getOffset(),
                partUri.getLength(),
                CompileTimeErrorCode.PART_OF_NON_PART,
                partUri.toSource()));
          } else if (libraryNameNode == null) {
            // TODO(brianwilkerson) Collect the names declared by the part. If they are all the same
            // then we can use that name as the inferred name of the library and present it in a
            // quick-fix.
            // partLibraryNames.add(partLibraryName);
          } else if (!libraryNameNode.getName().equals(partLibraryName)) {
            errorListener.onError(new AnalysisError(
                librarySource,
                partUri.getOffset(),
                partUri.getLength(),
                StaticWarningCode.PART_OF_DIFFERENT_LIBRARY,
                libraryNameNode.getName(),
                partLibraryName));
          }
          if (entryPoint == null) {
View Full Code Here

          libraryNameNode = ((LibraryDirective) directive).getName();
          directivesToResolve.add(directive);
        }
      } else if (directive instanceof PartDirective) {
        PartDirective partDirective = (PartDirective) directive;
        StringLiteral partUri = partDirective.getUri();
        Source partSource = partDirective.getSource();
        if (analysisContext.exists(partSource)) {
          hasPartDirective = true;
          CompilationUnit partUnit = library.getAST(partSource);
          if (partUnit != null) {
            CompilationUnitElementImpl part = builder.buildCompilationUnit(partSource, partUnit);
            part.setUriOffset(partUri.getOffset());
            part.setUriEnd(partUri.getEnd());
            part.setUri(partDirective.getUriContent());
            //
            // Validate that the part contains a part-of directive with the same name as the library.
            //
            String partLibraryName = getPartLibraryName(partSource, partUnit, directivesToResolve);
            if (partLibraryName == null) {
              errorListener.onError(new AnalysisError(
                  librarySource,
                  partUri.getOffset(),
                  partUri.getLength(),
                  CompileTimeErrorCode.PART_OF_NON_PART,
                  partUri.toSource()));
            } else if (libraryNameNode == null) {
              // TODO(brianwilkerson) Collect the names declared by the part. If they are all the same
              // then we can use that name as the inferred name of the library and present it in a
              // quick-fix.
              // partLibraryNames.add(partLibraryName);
            } else if (!libraryNameNode.getName().equals(partLibraryName)) {
              errorListener.onError(new AnalysisError(
                  librarySource,
                  partUri.getOffset(),
                  partUri.getLength(),
                  StaticWarningCode.PART_OF_DIFFERENT_LIBRARY,
                  libraryNameNode.getName(),
                  partLibraryName));
            }
            if (entryPoint == null) {
View Full Code Here

          if (importedSource != null && analysisContext.exists(importedSource)) {
            // The imported source will be null if the URI in the import directive was invalid.
            ResolvableLibrary importedLibrary = libraryMap.get(importedSource);
            if (importedLibrary != null) {
              ImportElementImpl importElement = new ImportElementImpl(directive.getOffset());
              StringLiteral uriLiteral = importDirective.getUri();
              if (uriLiteral != null) {
                importElement.setUriOffset(uriLiteral.getOffset());
                importElement.setUriEnd(uriLiteral.getEnd());
              }
              importElement.setUri(uriContent);
              importElement.setDeferred(importDirective.getDeferredToken() != null);
              importElement.setCombinators(buildCombinators(importDirective));
              LibraryElement importedLibraryElement = importedLibrary.getLibraryElement();
              if (importedLibraryElement != null) {
                importElement.setImportedLibrary(importedLibraryElement);
              }
              SimpleIdentifier prefixNode = ((ImportDirective) directive).getPrefix();
              if (prefixNode != null) {
                importElement.setPrefixOffset(prefixNode.getOffset());
                String prefixName = prefixNode.getName();
                PrefixElementImpl prefix = nameToPrefixMap.get(prefixName);
                if (prefix == null) {
                  prefix = new PrefixElementImpl(prefixNode);
                  nameToPrefixMap.put(prefixName, prefix);
                }
                importElement.setPrefix(prefix);
                prefixNode.setStaticElement(prefix);
              }
              directive.setElement(importElement);
              imports.add(importElement);

              if (analysisContext.computeKindOf(importedSource) != SourceKind.LIBRARY) {
                ErrorCode errorCode = importElement.isDeferred()
                    ? StaticWarningCode.IMPORT_OF_NON_LIBRARY
                    : CompileTimeErrorCode.IMPORT_OF_NON_LIBRARY;
                errorListener.onError(new AnalysisError(
                    library.getLibrarySource(),
                    uriLiteral.getOffset(),
                    uriLiteral.getLength(),
                    errorCode,
                    uriLiteral.toSource()));
              }
            }
          }
        } else if (directive instanceof ExportDirective) {
          ExportDirective exportDirective = (ExportDirective) directive;
          Source exportedSource = exportDirective.getSource();
          if (exportedSource != null && analysisContext.exists(exportedSource)) {
            // The exported source will be null if the URI in the export directive was invalid.
            ResolvableLibrary exportedLibrary = libraryMap.get(exportedSource);
            if (exportedLibrary != null) {
              ExportElementImpl exportElement = new ExportElementImpl();
              StringLiteral uriLiteral = exportDirective.getUri();
              if (uriLiteral != null) {
                exportElement.setUriOffset(uriLiteral.getOffset());
                exportElement.setUriEnd(uriLiteral.getEnd());
              }
              exportElement.setUri(exportDirective.getUriContent());
              exportElement.setCombinators(buildCombinators(exportDirective));
              LibraryElement exportedLibraryElement = exportedLibrary.getLibraryElement();
              if (exportedLibraryElement != null) {
                exportElement.setExportedLibrary(exportedLibraryElement);
              }
              directive.setElement(exportElement);
              exports.add(exportElement);

              if (analysisContext.computeKindOf(exportedSource) != SourceKind.LIBRARY) {
                errorListener.onError(new AnalysisError(
                    library.getLibrarySource(),
                    uriLiteral.getOffset(),
                    uriLiteral.getLength(),
                    CompileTimeErrorCode.EXPORT_OF_NON_LIBRARY,
                    uriLiteral.toSource()));
              }
            }
          }
        }
      }
View Full Code Here

   *
   * @param directive the directive which URI should be resolved
   * @return the result of resolving the URI against the URI of the library
   */
  public Source getSource(UriBasedDirective directive) {
    StringLiteral uriLiteral = directive.getUri();
    if (uriLiteral instanceof StringInterpolation) {
      errorListener.onError(new AnalysisError(
          librarySource,
          uriLiteral.getOffset(),
          uriLiteral.getLength(),
          CompileTimeErrorCode.URI_WITH_INTERPOLATION));
      return null;
    }
    String uriContent = uriLiteral.getStringValue().trim();
    directiveUris.put(directive, uriContent);
    uriContent = UriUtilities.encode(uriContent);
    if (directive instanceof ImportDirective && uriContent.startsWith(DART_EXT_SCHEME)) {
      libraryElement.setHasExtUri(true);
      return null;
    }
    try {
      new URI(uriContent);
      Source source = analysisContext.getSourceFactory().resolveUri(librarySource, uriContent);
      if (!analysisContext.exists(source)) {
        errorListener.onError(new AnalysisError(
            librarySource,
            uriLiteral.getOffset(),
            uriLiteral.getLength(),
            CompileTimeErrorCode.URI_DOES_NOT_EXIST,
            uriContent));
      }
      return source;
    } catch (URISyntaxException exception) {
      errorListener.onError(new AnalysisError(
          librarySource,
          uriLiteral.getOffset(),
          uriLiteral.getLength(),
          CompileTimeErrorCode.INVALID_URI,
          uriContent));
    }
    return null;
  }
View Full Code Here

TOP

Related Classes of com.google.dart.engine.ast.StringLiteral

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.