Package com.google.caja.reporting

Examples of com.google.caja.reporting.Message


        }
        case THROW:
        {
          tq.advance();
          if (semicolonInserted()) {
            throw new ParseException(new Message(
                MessageType.EXPECTED_TOKEN,
                FilePosition.endOf(tq.lastPosition()),
                MessagePart.Factory.valueOf("<expression>"),
                MessagePart.Factory.valueOf("<newline>")));
          }
View Full Code Here


          // It introduces no problem since there are no right-associative
          // binary operators with precedence 2 or 5.
          left = parseOp(opprec + 1, insertionProtected);
        } else {
          throw new ParseException(
              new Message(MessageType.UNEXPECTED_TOKEN, t.pos,
                          MessagePart.Factory.valueOf(t.text)));
        }
        if (op == Operator.CONSTRUCTOR && tq.checkToken(Punctuation.LPAREN)) {
          List<Expression> operands = Lists.newArrayList();
          operands.add(left);
          if (!tq.checkToken(Punctuation.RPAREN)) {
            do {
              operands.add(parseExpressionPart(true));
            } while (tq.checkToken(Punctuation.COMMA));
            tq.expectToken(Punctuation.RPAREN);
          }
          left = new SpecialOperation(posFrom(m), op, operands);
        } else {
          try {
            left = Operation.create(posFrom(m), op, left);
          } catch (IllegalArgumentException e) {
            throw new ParseException(
                new Message(MessageType.ASSIGN_TO_NON_LVALUE, t.pos,
                    MessagePart.Factory.valueOf(t.text)));
          }
        }
        finish(left, m);
        // Not pulling multiple operators off the stack means that
        // some prefix operator nestings are impossible.  This is intended.
        // This prevents such things as (new (++i)).
        // This only affects the new operator though since it is the only
        // prefix operator with a precedence != 4.
      }
      if (null == left) {
        left = parseExpressionAtom();
      }
    }

    // Parse binary operators, except comma.
    while (!tq.isEmpty()) {
      Token<JsTokenType> t = tq.peek();
      // If it is a binary op then we should consider using it
      Operator op = Operator.lookupOperation(t.text, OperatorType.INFIX);
      if (null == op) {
        op = Operator.lookupOperation(t.text, OperatorType.BRACKET);
        if (null == op) {
          op = Operator.lookupOperation(t.text, OperatorType.TERNARY);
          // Check for semicolon insertion since postfix operators are
          // "restricted productions" according to ES3 or ES5 S7.9.1.
          if (null == op) {
            if (!semicolonInserted()) {
              op = Operator.lookupOperation(t.text, OperatorType.POSTFIX);
            }
            if (null == op) { break; }
          }
        }
      } else if (Operator.COMMA == op) {
        break;
      }
      int opprec = op.getPrecedence();
      if (!(opprec < precedence
            || (opprec == precedence
                && Associativity.RIGHT == op.getAssociativity()))) {
        break;
      }

      if (op.getType() == OperatorType.BRACKET) {
        checkForMissingSemicolon();
      }

      Mark opStart = tq.mark();
      int nMessages = mq.getMessages().size();
      tq.advance()// Consume the operator token

      Expression right;
      try {
        // Recurse to parse operator arguments.
        if (OperatorType.BRACKET == op.getType()) {
          if (Operator.FUNCTION_CALL == op) {
            List<Expression> actuals;
            if (tq.checkToken(op.getClosingSymbol())) {
              actuals = Collections.<Expression>emptyList();
            } else {
              actuals = Lists.newArrayList();
              do {
                actuals.add(parseExpressionPart(true));
              } while (tq.checkToken(Punctuation.COMMA));
              tq.expectToken(op.getClosingSymbol());
            }

            right = new ActualList(actuals);
          } else {
            right = parseExpressionInt(true);
            tq.expectToken(op.getClosingSymbol());
          }
        } else if (OperatorType.POSTFIX == op.getType()) {
          right = null;
        } else if (OperatorType.TERNARY == op.getType()) {
          right = parseExpressionPart(insertionProtected);
        } else if (Operator.MEMBER_ACCESS != op) {
          right = parseOp(opprec, insertionProtected);
        } else {
          // The . operator only accepts a reference on the right.
          // No a.b.4 or a.b.(c.d)
          right = parseReference(true);
        }
      } catch (ParseException ex) {
        // According to
        // http://www.mozilla.org/js/language/js20/rationale/syntax.html
        // semicolon insertion requires that we reconsider the decision to
        // treat op as a binary op if it could be a prefix op.

        // Line-Break Semicolon Insertion
        // If the first through the nth tokens of a JavaScript program form
        // are grammatically valid but the first through the n+1st tokens
        // are not and there is a line break between the nth tokens and the
        // n+1st tokens, then the parser tries to parse the program again
        // after inserting a VirtualSemicolon token between the nth and the
        // n+1st tokens.
        if ((Operator.FUNCTION_CALL == op
             || null != Operator.lookupOperation(
                 op.getOpeningSymbol(), OperatorType.PREFIX))
            && !insertionProtected) {
          Mark m3 = tq.mark();
          tq.rewind(opStart);
          if (semicolonInserted()) {
            List<Message> messages = mq.getMessages();
            if (nMessages < messages.size()) {
              messages.subList(nMessages, messages.size()).clear();
            }
            FilePosition semiPoint = FilePosition.endOf(tq.lastPosition());
            messages.add(new Message(
                             MessageType.SEMICOLON_INSERTED, semiPoint));
            return left;
          } else {
            tq.rewind(m3);
          }
        }
        throw ex;
      }
      switch (op.getType()) {
        case TERNARY:
          {
            tq.expectToken(op.getClosingSymbol());
            Expression farRight = parseExpressionPart(insertionProtected);
            left = Operation.create(posFrom(left), op, left, right, farRight);
          }
          break;
          case BRACKET:
            if (Operator.FUNCTION_CALL == op) {
              // Function calls can take nothing or multiple on the right, so
              // we wrap function calls up in an ActualList.
              ActualList actuals = (ActualList) right;
              List<? extends Expression> params = actuals.children();
              Expression[] operands = new Expression[params.size() + 1];
              operands[0] = left;
              for (int i = 1; i < operands.length; ++i) {
                operands[i] = params.get(i - 1);
              }
              left = Operation.create(posFrom(left), op, operands);
            } else {
              left = Operation.create(posFrom(left), op, left, right);
            }
            break;
          case INFIX:
            if (op.getCategory() == OperatorCategory.ASSIGNMENT
                && !left.isLeftHandSide()) {
              throw new ParseException(
                  new Message(MessageType.ASSIGN_TO_NON_LVALUE,
                              t.pos, MessagePart.Factory.valueOf(t.text)));
            }
            left = Operation.create(posFrom(left), op, left, right);
            break;
          case POSTFIX:
            if (op.getCategory() == OperatorCategory.ASSIGNMENT
                && !left.isLeftHandSide()) {
              throw new ParseException(
                  new Message(MessageType.ASSIGN_TO_NON_LVALUE,
                              t.pos, MessagePart.Factory.valueOf(t.text)));
            }
            left = Operation.create(posFrom(left), op, left);
            break;
          default:
View Full Code Here

  private String floatToString(Token<JsTokenType> t) throws ParseException {
    try {
      return NumberLiteral.numberToString(new BigDecimal(t.text));
    } catch (NumberFormatException e) {
      throw new ParseException(
          new Message(
              MessageType.MALFORMED_NUMBER, t.pos,
              MessagePart.Factory.valueOf(t.text)));
    }
  }
View Full Code Here

        mq.addMessage(MessageType.PLACEHOLDER_INSERTED, pos);
        Identifier idNode = new Identifier(pos, "_");
        e = new Reference(idNode);
      } else {
        throw new ParseException(
            new Message(
                MessageType.UNEXPECTED_TOKEN, t.pos,
                MessagePart.Factory.valueOf(t.text)));
      }
    }
View Full Code Here

                tq.currentPosition(), k);
          }
        }
        if (!isIdentifier(s)) {
          throw new ParseException(
              new Message(MessageType.INVALID_IDENTIFIER,
                          tq.currentPosition(), MessagePart.Factory.valueOf(s))
              );
        }
        break;
      case KEYWORD:
        if (!allowReservedWords) {
          mq.addMessage(MessageType.RESERVED_WORD_USED_AS_IDENTIFIER,
                        tq.currentPosition(), Keyword.fromString(s));
        }
        break;
      default:
        throw new ParseException(
            new Message(MessageType.EXPECTED_TOKEN, tq.currentPosition(),
                        MessagePart.Factory.valueOf("an identifier"),
                        MessagePart.Factory.valueOf(s)));
    }
    tq.advance();
    return decodeIdentifier(s);
View Full Code Here

              break;
          }
          ++end;
        } while (state != 3);
        if (state != 3) {
          throw new ParseException(new Message(
              MessageType.UNTERMINATED_COMMENT_TOKEN,
              cp.filePositionForOffsets(start, end)));
        }
        type = CssTokenType.COMMENT;
      } else if (end < limit && buf[end] == '/') {
        do {
          if (++end == limit) { break; }
          ch = buf[end];
          // Line comment does not contain the newline character that ends it
          // since we don't want to break \r\n sequences across two tokens,
          // and for consistency with JavaScript conventions which exclude the
          // newline from the line comment token.
          if (ch == '\r' || ch == '\n') { break; }
        } while (true);
        type = CssTokenType.COMMENT;
        FilePosition commentPos = cp.filePositionForOffsets(start, end);
        mq.addMessage(MessageType.INVALID_CSS_COMMENT, commentPos);
      } else {
        //               *yytext
        type = CssTokenType.PUNCTUATION;
      }
    } else if ('~' == ch || '|' == ch) {
      if (end < limit && '=' == buf[end]) {
        // "~="          INCLUDES
        // "|="          DASHMATCH
        ++end;
      } else {
        //        .      *yytext
      }
      type = CssTokenType.PUNCTUATION;

    } else if (ch == '\'' || ch == '"') {
      end = parseString(cp, start);
      type = CssTokenType.STRING;

    } else if (ch == '@') {

      identEnd = parseIdent(cp, end);
      if (identEnd != -1) {
        // "@import"       IMPORT_SYM
        // "@page"         PAGE_SYM
        // "@media"        MEDIA_SYM
        // "@font-face"    FONT_FACE_SYM
        // "@charset "      CHARSET_SYM
        // "@"{ident}      ATKEYWORD
        type = CssTokenType.SYMBOL;
        end = identEnd;
        // In http://www.w3.org/TR/CSS21/grammar.html, the CHARSET_SYM is
        // allowed to match only "@charset "
        if ((end - start) == 8 && parseMatch(cp, start, "@charset ") > 0) {
          ++end;
        }
      } else {
        //        .        *yytext
        type = CssTokenType.PUNCTUATION;
      }
    } else if (ch == '!') {
      // "!{w}important" IMPORTANT_SYM
      // handled by token joining at a later pass

      //          .      *yytext

      type = CssTokenType.PUNCTUATION;
    } else if (ch == '#') {
      int nameEnd = parseName(cp, end);
      if (nameEnd >= 0) {
        // "#"{name}       HASH
        type = CssTokenType.HASH;
        end = nameEnd;
      } else {
        //          .      *yytext
        type = CssTokenType.PUNCTUATION;
      }

    } else if (ch == '<' || ch == '-') {
      // "<!--"          CDO
      // "-->"           CDC

      int tailEnd = parseMatch(cp, end, ch == '<' ? "!--" : "->");
      if (tailEnd >= 0) { end = tailEnd; }
      type = CssTokenType.PUNCTUATION;

    } else if ((ch >= '0' && ch <= '9') || '.' == ch) {
      // {num}em         EMS
      // {num}ex         EXS
      // {num}px         LENGTH
      // {num}cm         LENGTH
      // {num}mm         LENGTH
      // {num}in         LENGTH
      // {num}pt         LENGTH
      // {num}pc         LENGTH
      // {num}deg        ANGLE
      // {num}rad        ANGLE
      // {num}grad       ANGLE
      // {num}ms         TIME
      // {num}s          TIME
      // {num}Hz         FREQ
      // {num}kHz        FREQ
      // {num}{ident}    DIMEN
      // {num}%          PERCENTAGE
      // {num}           NUMBER
      boolean isNum;
      if ('.' == ch) {
        int numEnd = parseInt(cp, end);
        isNum = numEnd >= 0;
        if (isNum) { end = numEnd; }
      } else {
        isNum = true;
        end = parseNum(cp, start);
      }

      if (isNum) {
        identEnd = parseIdent(cp, end);
        if (identEnd >= 0) {
          end = identEnd;
        } else if (end < limit && '%' == buf[end]) {
          ++end;
        }
        type = CssTokenType.QUANTITY;
      } else {
        // lone .
        //          .      *yytext
        type = CssTokenType.PUNCTUATION;
      }

    } else if ((identEnd = parseIdent(cp, start)) >= 0) {
      end = identEnd;
      if (end - start == 1 && 'U' == ch && end < limit && '+' == buf[end]) {
        // U\+{range}      UNICODERANGE
        // U\+{h}{1,6}-{h}{1,6}    UNICODERANGE
        // range         \?{1,6}|{h}(\?{0,5}|{h}(\?{0,4}|{h}\
        //               (\?{0,3}|{h}(\?{0,2}|{h}(\??|{h})))))

        type = CssTokenType.UNICODE_RANGE;
        ++end;
        end = parseRange(cp, end);
      } else if (end < limit && '(' == buf[end]) {
        ++end;
        if (end - start == 4 && parseMatch(cp, start, "url(") >= 0) {
          // "url("{w}{string}{w}")" URI
          // "url("{w}{url}{w}")"    URI
          end = parseWhitespace(buf, end, limit);
          int stringEnd = parseString(cp, end);
          int uriEnd = stringEnd < 0 ? parseUri(cp, end) : -1;
          if (stringEnd < 0 && uriEnd < 0) {
            throw new ParseException(new Message(
                MessageType.EXPECTED_TOKEN,
                cp.filePositionForOffsets(end, end),
                MessagePart.Factory.valueOf("{url}"), toMessagePart(cp, end)));
          }
          end = stringEnd >= 0 ? stringEnd : uriEnd;
          end = parseWhitespace(buf, end, limit);
          if (end == limit || ')' != buf[end]) {
            throw new ParseException(new Message(
                MessageType.EXPECTED_TOKEN,
                cp.filePositionForOffsets(end, end),
                MessagePart.Factory.valueOf(")"), toMessagePart(cp, end)));
          }
          ++end;
          type = CssTokenType.URI;
        } else {
          // {ident}"("      FUNCTION
          type = CssTokenType.FUNCTION;
        }
      } else {
        // {ident}         IDENT
        type = CssTokenType.IDENT;
      }

    } else if (ch == '$' && allowSubstitutions) {
      // ${<javascript tokens>}

      if (end < limit && buf[end] != '{') {
        type = CssTokenType.PUNCTUATION;
      } else {
        // 0 - non string
        // 1 - quoted string
        // 2 - saw \ in string
        // 3 - saw close paren
        int state = 0;
        // number of parenthetical blocks entered and not exited
        int nOpen = 0;
        char delim = 0;
        do {
          if (end == limit) { break; }
          ch = buf[end];
          switch (state) {
            case 0:
              if (ch == '"' || ch == '\'') {
                delim = ch;
                state = 1;
              } else if (ch == '{') {
                ++nOpen;
              } else if (ch == '}') {
                if (--nOpen == 0) {
                  state = 3;
                }
              }
              break;
            case 1:
              if (ch == delim) {
                state = 0;
              } else if (ch == '\\') {
                state = 2;
              }
              break;
            case 2:
              state = 1;
              break;
          }
          ++end;
        } while (state != 3);
        if (state != 3) {
          throw new ParseException(new Message(
              MessageType.UNTERMINATED_STRING_TOKEN,
              cp.filePositionForOffsets(start, end)));
        }

        identEnd = parseIdent(cp, end);
View Full Code Here

          }
        } else {
          end = parseEscapeBody(cp, end);
        }
      } else if (isLineBreak(ch)) {
        throw new ParseException(new Message(
            MessageType.MALFORMED_STRING,
            cp.filePositionForOffsets(end - 1, end - 1),
            MessagePart.Factory.valueOf("" + ch)));
      }
    }
    throw new ParseException(new Message(
        MessageType.UNTERMINATED_STRING_TOKEN,
        cp.filePositionForOffsets(start, end)));
  }
View Full Code Here

    if (end < limit && '.' == buf[end]) {
      ++end;
      char ch;
      // By CSS rules, 0. is an invalid number.
      if (end == limit || (ch = buf[end]) < '0' || ch > '9') {
        throw new ParseException(new Message(
            MessageType.MALFORMED_NUMBER, cp.filePositionForOffsets(start, end),
            MessagePart.Factory.valueOf(cp.toString(start, end))));
      }
      return parseInt(cp, end);
    }
View Full Code Here

    // escape     {unicode}|\\[^\r\n\f0-9a-f]
    int limit = cp.getLimit();
    char[] buf = cp.getBuffer();
    if (start == limit) {
      throw new ParseException(
          new Message(MessageType.EXPECTED_TOKEN,
                      cp.filePositionForOffsets(start, start),
                      MessagePart.Factory.valueOf("<hex-digit>"),
                      MessagePart.Factory.valueOf("<end-of-input>")));
    }
    char ch = buf[start];
    if (CssLexer.isHexChar(ch)) {
      int end = start + 1;
      for (int i = 5; --i >= 0; ++end) {
        if (end == limit) { break; }
        ch = buf[end];
        if (!CssLexer.isHexChar(ch)) { break; }
      }
      if (end < limit && CssLexer.isSpaceChar(ch = buf[end])) {
        ++end;

        if ('\r' == ch && end < limit && '\n' == buf[end]) {
          ++end;
        }
      }
      return end;
    } else if (isLineBreak(ch)) {
      throw new ParseException(
          new Message(
              MessageType.UNRECOGNIZED_ESCAPE,
              cp.filePositionForOffsets(start, start),
              MessagePart.Factory.valueOf(String.valueOf(ch))));
    } else {
      return start + 1;
View Full Code Here

    }
    while (end < limit && CssLexer.isHexChar(buf[end]) && --len >= 0) { ++end; }
    if (!isRange) {
      if (end == limit || '-' != buf[end]) {
        throw new ParseException(
            new Message(
                MessageType.EXPECTED_TOKEN,
                cp.filePositionForOffsets(end, end),
                MessagePart.Factory.valueOf("-"), toMessagePart(cp, end)));
      }
      ++end;
View Full Code Here

TOP

Related Classes of com.google.caja.reporting.Message

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.