예제 #1
0
  private static String buildMessage(List<ParseError> errors) {
    if (errors == null || errors.size() < 0) {
      throw new IllegalArgumentException(
          "Cannot build a CSSParseException without a list of errors");
    }

    StringBuilder sb = new StringBuilder();
    for (ParseError pe : errors) {
      Position pos = pe.getInputBuffer().getPosition(pe.getStartIndex());
      String message =
          pe.getErrorMessage() != null
              ? pe.getErrorMessage()
              : pe instanceof InvalidInputError
                  ? new DefaultInvalidInputErrorFormatter().format((InvalidInputError) pe)
                  : pe.getClass().getSimpleName();
      sb.append(message)
          .append(" (line ")
          .append(pos.line)
          .append(", column ")
          .append(pos.column)
          .append(")");
      sb.append('\n');
    }
    sb.setLength(sb.length() - 1);
    return sb.toString();
  }
예제 #2
0
  /**
   * Parse the provided domain string and return the constructed representation.
   *
   * @param <E> The structured type.
   * @param domain The string to parse.
   * @return A {@code TypeList} is returned by default, but if the type is defined to consist of
   *     {@code Numeric} types, a {@code Vector} instance is returned.
   */
  public static <E extends StructuredType<? extends Type>> E parse(String domain) {
    final ReportingParseRunner<String> expander =
        new ReportingParseRunner<String>(EXPANDING_PARSER.Expansion());
    final ParsingResult<String> d = expander.run(domain.replaceAll(" ", ""));

    if (d.hasErrors()) {
      StringBuilder strBuilder = new StringBuilder();
      for (ParseError e : d.parseErrors) {
        strBuilder.append(e.getInputBuffer().extract(e.getStartIndex(), e.getEndIndex()));
      }
      throw new RuntimeException(
          "Error in expanding domain: " + domain + " near: " + strBuilder.toString());
    }

    final String expanded = Joiner.on(",").join(d.valueStack);
    final ReportingParseRunner<?> runner = new ReportingParseRunner(DOMAIN_PARSER.Domain());
    final ParsingResult<Type> result = (ParsingResult<Type>) runner.run(expanded);

    if (result.hasErrors()) {
      StringBuilder strBuilder = new StringBuilder();
      for (ParseError e : result.parseErrors) {
        strBuilder.append(e.getInputBuffer().extract(e.getStartIndex(), e.getEndIndex()));
      }
      throw new RuntimeException(
          "Error in parsing domain: "
              + expanded
              + ". Ensure that the domain is a valid domain string and contains no whitespace.\nError occured near: "
              + strBuilder.toString());
    }

    List<Type> l = Lists.newArrayList(result.valueStack);

    if (isVector(l)) {
      @SuppressWarnings("unchecked")
      E vector = (E) toVector(l);
      return vector;
    }

    return (E) toTypeList(l);
  }
예제 #3
0
 // @SuppressWarnings("unchecked")
 protected AbcNode(
     Node node,
     InputBuffer parseInputBuffer,
     List<ParseError> parseErrors,
     AbcInputBuffer abcInputBuffer) {
   super(null);
   if (node != null) {
     this.label = node.getLabel();
     this.value = parseInputBuffer.extract(node.getStartIndex(), node.getEndIndex());
     Position pos = parseInputBuffer.getPosition(node.getStartIndex());
     int sourceStartIndex = abcInputBuffer.getIndex(pos);
     int sourceEndIndex = sourceStartIndex + value.length();
     setCharStreamPosition(
         new CharStreamPosition(pos.line, pos.column, sourceStartIndex, sourceEndIndex));
     this.childs = new ArrayList(node.getChildren().size());
     Iterator it = node.getChildren().iterator();
     while (it.hasNext()) {
       AbcNode abcn = new AbcNode((Node) it.next(), parseInputBuffer, parseErrors, abcInputBuffer);
       abcn.parent = this;
       childs.add(abcn);
     }
     if (!hasError() /*(childs.size() == 0)*/ && node.hasError()) {
       this.errors = new ArrayList();
       it = parseErrors.iterator();
       while (it.hasNext()) {
         ParseError pe = (ParseError) it.next();
         String peValue = pe.getInputBuffer().extract(pe.getStartIndex(), pe.getEndIndex());
         String peMsg = pe.getErrorMessage();
         Position pePos = pe.getInputBuffer().getPosition(pe.getStartIndex());
         int peIndex = abcInputBuffer.getIndex(pePos);
         CharStreamPosition csp =
             new CharStreamPosition(
                 pePos.line,
                 pePos.column,
                 peIndex,
                 peIndex + (peValue.length() > 0 ? peValue.length() : 1));
         // if ((pe.getStartIndex() >= node.getStartIndex())
         //		&& (pe.getStartIndex() </*=*/ node.getEndIndex())) {
         if ((peIndex >= sourceStartIndex)
             && ((peIndex < sourceEndIndex) || (sourceStartIndex == sourceEndIndex))) {
           errors.add(new AbcParseError(peMsg, peValue, csp));
         }
       }
     }
   } else {
     this.label = "AbcFile-Error";
     this.value = "";
     int nbL = parseInputBuffer.getLineCount();
     for (int i = 1; i <= nbL; i++) this.value += parseInputBuffer.extractLine(i) + "\n";
     this.childs = new ArrayList(0);
     setCharStreamPosition(new CharStreamPosition(1, 1, 0, 1));
     if (parseErrors != null) {
       this.errors = new ArrayList();
       Iterator it = parseErrors.iterator();
       while (it.hasNext()) {
         ParseError pe = (ParseError) it.next();
         errors.add(new AbcParseError(pe.getErrorMessage(), value, getCharStreamPosition()));
       }
     }
   }
 }