Exemple #1
1
  /**
   * <strong>[11.8.5] Regular Expression Literals</strong>
   *
   * <pre>
   * RegularExpressionLiteral ::
   *     / RegularExpressionBody / RegularExpressionFlags
   * RegularExpressionBody ::
   *     RegularExpressionFirstChar RegularExpressionChars
   * RegularExpressionChars ::
   *     [empty]
   *     RegularExpressionChars RegularExpressionChar
   * RegularExpressionFirstChar ::
   *     RegularExpressionNonTerminator but not one of * or \ or / or [
   *     RegularExpressionBackslashSequence
   *     RegularExpressionClass
   * RegularExpressionChar ::
   *     RegularExpressionNonTerminator but not one of \ or / or [
   *     RegularExpressionBackslashSequence
   *     RegularExpressionClass
   * RegularExpressionBackslashSequence ::
   *     \ RegularExpressionNonTerminator
   * RegularExpressionNonTerminator ::
   *     SourceCharacter but not LineTerminator
   * RegularExpressionClass ::
   *     [ RegularExpressionClassChars ]
   * RegularExpressionClassChars ::
   *     [empty]
   *     RegularExpressionClassChars RegularExpressionClassChar
   * RegularExpressionClassChar ::
   *     RegularExpressionNonTerminator but not one of ] or \
   *     RegularExpressionBackslashSequence
   * RegularExpressionFlags ::
   *     [empty]
   *     RegularExpressionFlags IdentifierPart
   * </pre>
   */
  public String[] readRegularExpression(Token start) {
    assert start == Token.DIV || start == Token.ASSIGN_DIV;
    assert next == null : "regular expression in lookahead";

    final int EOF = TokenStreamInput.EOF;
    TokenStreamInput input = this.input;
    StringBuffer buffer = buffer();
    if (start == Token.ASSIGN_DIV) {
      buffer.add('=');
    } else {
      int c = input.peek(0);
      if (c == '/' || c == '*') {
        throw error(Messages.Key.InvalidRegExpLiteral);
      }
    }
    boolean inClass = false;
    for (; ; ) {
      int c = input.get();
      if (c == '\\') {
        // escape sequence
        buffer.add(c);
        c = input.get();
      } else if (c == '[') {
        inClass = true;
      } else if (c == ']') {
        inClass = false;
      } else if (c == '/' && !inClass) {
        break;
      }
      if (c == EOF || isLineTerminator(c)) {
        throw error(Messages.Key.UnterminatedRegExpLiteral);
      }
      buffer.add(c);
    }
    String regexp = buffer.toString();

    buffer.clear();
    for (; ; ) {
      int c = input.get();
      if (!isIdentifierPart(c)) {
        if (c == '\\' && match('u')) {
          readUnicode();
          throw error(Messages.Key.UnicodeEscapeInRegExpFlags);
        }
        input.unget(c);
        break;
      }
      buffer.add(c);
    }

    String flags = buffer.toString();
    return new String[] {regexp, flags};
  }
Exemple #2
1
 private StringBuffer buffer() {
   StringBuffer buffer = this.buffer;
   buffer.clear();
   return buffer;
 }