@Override
  public boolean consume(CodeReader code, Lexer lexer) {
    if (!Character.isJavaIdentifierStart(code.peek())) {
      return false;
    }

    int line = code.getCursor().getLine();
    int column = code.getCursor().getColumn();
    while (Character.isJavaIdentifierPart(code.peek())) {
      tmpBuilder.append((char) code.pop());
    }

    String word = tmpBuilder.toString();

    TokenType keywordType = keywordsMap.get(word);
    Token token =
        tokenBuilder
            .setType(keywordType == null ? GenericTokenType.IDENTIFIER : keywordType)
            .setValueAndOriginalValue(word, word)
            .setURI(lexer.getURI())
            .setLine(line)
            .setColumn(column)
            .build();

    lexer.addToken(token);

    tmpBuilder.delete(0, tmpBuilder.length());

    return true;
  }
示例#2
0
 public boolean hasNextToken(CodeReader code, HtmlCodeBuilder codeBuilder) {
   return code.peek() != '\n'
       && code.peek() != '\r'
       && (isCommentStartedOnPreviousLine(codeBuilder)
           || (code.peek() == startToken[0]
               && Arrays.equals(code.peek(startToken.length), startToken)));
 }
 @Override
 public boolean consume(CodeReader code, HtmlCodeBuilder codeBuilder) {
   if (code.peek() == '@') {
     codeBuilder.appendWithoutTransforming(tagBefore);
     code.popTo(END_TOKEN_MATCHER, codeBuilder);
     codeBuilder.appendWithoutTransforming(tagAfter);
     return true;
   } else {
     return false;
   }
 }
  @Test
  public void shouldConsume() {
    BlackHoleTokenChannel channel = new BlackHoleTokenChannel("ABC");
    TokenQueue output = mock(TokenQueue.class);
    CodeReader codeReader = new CodeReader("ABCD");

    assertThat(channel.consume(codeReader, output)).isTrue();
    assertThat(codeReader.getLinePosition()).isEqualTo(1);
    assertThat(codeReader.getColumnPosition()).isEqualTo(3);
    verifyZeroInteractions(output);
  }
 @Override
 public boolean consume(CodeReader code, HtmlCodeBuilder codeBuilder) {
   if (code.getColumnPosition() == COLUMN_INDEX && code.popTo(matcher, tmpBuilder) > 0) {
     codeBuilder.appendWithoutTransforming(tagBefore);
     codeBuilder.append(tmpBuilder);
     codeBuilder.appendWithoutTransforming(tagAfter);
     tmpBuilder.delete(0, tmpBuilder.length());
     return true;
   }
   return false;
 }
示例#6
0
 @Override
 public boolean consume(CodeReader code, HtmlCodeBuilder codeBuilder) {
   if (code.peek() == '\'' || code.peek() == '\"') {
     codeBuilder.appendWithoutTransforming(tagBefore);
     int firstChar = code.peek();
     code.popTo(new EndCommentMatcher(firstChar, code), codeBuilder);
     codeBuilder.appendWithoutTransforming(tagAfter);
     return true;
   } else {
     return false;
   }
 }
示例#7
0
 @Override
 public boolean consume(CodeReader code, HtmlCodeBuilder codeBuilder) {
   if (hasNextToken(code, codeBuilder)) {
     codeBuilder.appendWithoutTransforming(tagBefore);
     code.popTo(new MultilineEndTokenMatcher(code, codeBuilder), codeBuilder);
     codeBuilder.appendWithoutTransforming(tagAfter);
     return true;
   } else {
     return false;
   }
 }
示例#8
0
  @Override
  public boolean consume(CodeReader codeReader, List<Node> nodeList) {
    Node node = createNode();

    setStartPosition(codeReader, node);

    StringBuilder stringBuilder = new StringBuilder();
    codeReader.popTo(endTokenMatcher, stringBuilder);
    node.setCode(stringBuilder.toString());
    setEndPosition(codeReader, node);

    nodeList.add(node);

    return true;
  }
示例#9
0
    @Override
    public boolean match(int endFlag) {
      commentSize++;
      if (commentSize >= endToken.length + startToken.length
          || (commentSize >= endToken.length && isCommentStartedOnPreviousLine(codeBuilder))) {
        boolean matches = true;
        for (int i = 1; i <= endToken.length; i++) {
          if (code.charAt(-i) != endToken[endToken.length - i]) {
            matches = false;
            break;
          }
        }
        if (matches) {
          setCommentStartedOnPreviousLine(codeBuilder, Boolean.FALSE);
          return true;
        }
      }

      if (endFlag == '\r' || endFlag == '\n') {
        setCommentStartedOnPreviousLine(codeBuilder, Boolean.TRUE);
        return true;
      }
      return false;
    }
示例#10
0
 public boolean match(int endFlag) {
   literalValue.append((char) endFlag);
   return (code.lastChar() == firstChar
       && evenNumberOfBackSlashBeforeDelimiter()
       && literalValue.length() > 1);
 }