public boolean equals(Object obj) {
   if (obj == this) {
     return true;
   }
   if (obj instanceof Token) {
     Token t2 = (Token) obj;
     return type == t2.getType()
         && lexeme.equals(t2.getLexeme())
         && line == t2.getLine()
         && column == t2.getColumn()
         && invalid == t2.isInvalid();
   }
   return false;
 }
Exemple #2
0
  private boolean step() throws LexicalError, SyntaticError, SemanticError {
    if (currentToken == null) {
      int pos = 0;
      if (previousToken != null)
        pos = previousToken.getPosition() + previousToken.getLexeme().length();

      currentToken = new Token(DOLLAR, "$", pos);
    }

    int x = ((Integer) stack.pop()).intValue();
    int a = currentToken.getId();

    if (x == EPSILON) {
      return false;
    } else if (isTerminal(x)) {
      if (x == a) {
        if (stack.empty()) return true;
        else {
          previousToken = currentToken;
          currentToken = scanner.nextToken();
          return false;
        }
      } else {
        throw new SyntaticError(PARSER_ERROR[x], currentToken.getPosition());
      }
    } else if (isNonTerminal(x)) {
      if (pushProduction(x, a)) return false;
      else throw new SyntaticError(PARSER_ERROR[x], currentToken.getPosition());
    } else // isSemanticAction(x)
    {
      if (executaAcoesSemanticas) {
        semanticAnalyser.executeAction(x - FIRST_SEMANTIC_ACTION, previousToken);
      }
      return false;
    }
  }
  /** {@inheritDoc} */
  public void markOccurrences(
      RSyntaxDocument doc,
      Token t,
      RSyntaxTextAreaHighlighter h,
      MarkOccurrencesHighlightPainter p) {

    char[] lexeme = t.getLexeme().toCharArray();
    int tokenOffs = t.getOffset();
    Element root = doc.getDefaultRootElement();
    int lineCount = root.getElementCount();
    int curLine = root.getElementIndex(t.getOffset());

    // For now, we only check for tags on the current line, for
    // simplicity.  Tags spanning multiple lines aren't common anyway.
    boolean found = false;
    boolean forward = true;
    t = doc.getTokenListForLine(curLine);
    while (t != null && t.isPaintable()) {
      if (t.getType() == Token.MARKUP_TAG_DELIMITER) {
        if (t.isSingleChar('<') && t.getOffset() + 1 == tokenOffs) {
          found = true;
          break;
        } else if (t.is(CLOSE_TAG_START) && t.getOffset() + 2 == tokenOffs) {
          found = true;
          forward = false;
          break;
        }
      }
      t = t.getNextToken();
    }

    if (!found) {
      return;
    }

    if (forward) {

      int depth = 0;
      t = t.getNextToken().getNextToken();

      do {

        while (t != null && t.isPaintable()) {
          if (t.getType() == Token.MARKUP_TAG_DELIMITER) {
            if (t.isSingleChar('<')) {
              depth++;
            } else if (t.is(TAG_SELF_CLOSE)) {
              if (depth > 0) {
                depth--;
              } else {
                return; // No match for this tag
              }
            } else if (t.is(CLOSE_TAG_START)) {
              if (depth > 0) {
                depth--;
              } else {
                Token match = t.getNextToken();
                if (match != null && match.is(lexeme)) {
                  try {
                    int end = match.getOffset() + match.length();
                    h.addMarkedOccurrenceHighlight(match.getOffset(), end, p);
                    end = tokenOffs + match.length();
                    h.addMarkedOccurrenceHighlight(tokenOffs, end, p);
                  } catch (BadLocationException ble) {
                    ble.printStackTrace(); // Never happens
                  }
                }
                return; // We're done!
              }
            }
          }
          t = t.getNextToken();
        }

        if (++curLine < lineCount) {
          t = doc.getTokenListForLine(curLine);
        }

      } while (curLine < lineCount);

    } else { // !forward

      Stack<Token> matches = new Stack<Token>();
      boolean inPossibleMatch = false;
      t = doc.getTokenListForLine(curLine);
      final int endBefore = tokenOffs - 2; // Stop before "</".

      do {

        while (t != null && t.getOffset() < endBefore && t.isPaintable()) {
          if (t.getType() == Token.MARKUP_TAG_DELIMITER) {
            if (t.isSingleChar('<')) {
              Token next = t.getNextToken();
              if (next != null) {
                if (next.is(lexeme)) {
                  matches.push(next);
                  inPossibleMatch = true;
                } else {
                  inPossibleMatch = false;
                }
                t = next;
              }
            } else if (t.isSingleChar('>')) {
              inPossibleMatch = false;
            } else if (inPossibleMatch && t.is(TAG_SELF_CLOSE)) {
              matches.pop();
            } else if (t.is(CLOSE_TAG_START)) {
              Token next = t.getNextToken();
              if (next != null) {
                // Invalid XML might not have a match
                if (next.is(lexeme) && !matches.isEmpty()) {
                  matches.pop();
                }
                t = next;
              }
            }
          }
          t = t.getNextToken();
        }

        if (!matches.isEmpty()) {
          try {
            Token match = matches.pop();
            int end = match.getOffset() + match.length();
            h.addMarkedOccurrenceHighlight(match.getOffset(), end, p);
            end = tokenOffs + match.length();
            h.addMarkedOccurrenceHighlight(tokenOffs, end, p);
          } catch (BadLocationException ble) {
            ble.printStackTrace(); // Never happens
          }
          return;
        }

        if (--curLine >= 0) {
          t = doc.getTokenListForLine(curLine);
        }

      } while (curLine >= 0);
    }
  }