com.sonar.sslr.api.Token类的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(8.8k)|赞(0)|评价(0)|浏览(124)

本文整理了Java中com.sonar.sslr.api.Token类的一些代码示例,展示了Token类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Token类的具体详情如下:
包路径:com.sonar.sslr.api.Token
类名称:Token

Token介绍

[英]Tokens are string of character like an identifier, a literal, an integer, ... which are produced by the lexer to feed the parser. By definition, comments and preprocessing directives should not be seen by the parser that's why such Trivia, when they exist, are attached to the next token.
[中]标记是字符串,如标识符、文字、整数等。。。它们由lexer生成,提供给解析器。根据定义,解析器不应该看到注释和预处理指令,这就是为什么这些琐事存在时会附加到下一个标记。

代码示例

代码示例来源:origin: felipebz/sonar-plsql

@Override
public void visitToken(Token token) {
  String[] tokenLines = token.getValue().split("\n", -1);
  for (int line = token.getLine(); line < token.getLine() + tokenLines.length; line++) {
   linesOfCode.add(line);
  }
  for (Trivia trivia : token.getTrivia()) {
    if (trivia.isComment()) {
      visitComment(trivia);
    }
  }
}

代码示例来源:origin: SonarSource/sonar-java

private static List<SyntaxTrivia> createTrivias(List<Trivia> trivias) {
 List<SyntaxTrivia> result = Lists.newArrayList();
 for (Trivia trivia : trivias) {
  Token trivialToken = trivia.getToken();
  result.add(InternalSyntaxTrivia.create(trivialToken.getValue(), trivialToken.getLine(), trivialToken.getColumn()));
 }
 return result;
}

代码示例来源:origin: org.codehaus.sonar-plugins.python/python-checks

private void addSimpleField(AstNode test) {
 Token token = test.getToken();
 if (test.getNumberOfChildren() == 1
   && test.getFirstChild().is(PythonGrammar.ATOM)
   && token.getType().equals(GenericTokenType.IDENTIFIER) && !CheckUtils.containsValue(symbols, token.getValue())) {
  symbols.add(token);
 }
}

代码示例来源:origin: org.sonarsource.sonar-plugins.javascript/javascript-squid

public int startOffset(Token token) {
 int lineStartOffset = lineStartOffsets.get(token.getLine() - 1);
 int column = token.getColumn();
 return lineStartOffset + column;
}

代码示例来源:origin: org.sonarsource.sonar-plugins.javascript/javascript-squid

/**
 * {@inheritDoc}
 */
@Override
public void visitToken(Token token) {
 if (token.getType() != EOF) {
  lines.add(token.getLine());
 }
}

代码示例来源:origin: org.sonarsource.flex/sonar-flex-plugin

public TokenLocation(Token token) {
 this.startLine = token.getLine();
 this.startCharacter = token.getColumn();
 final String[] lines = token.getOriginalValue().split("\r\n|\n|\r", -1);
 if (lines.length > 1) {
  this.endLine = token.getLine() + lines.length - 1;
  this.endCharacter = lines[lines.length - 1].length();
 } else {
  this.endLine = startLine;
  this.endCharacter = startCharacter + token.getOriginalValue().length();
 }
}

代码示例来源:origin: org.sonarsource.flex/flex-checks

@Override
public void visitToken(Token token) {
 for (Trivia trivia : token.getTrivia()) {
  String[] lines = regexpToDivideStringByLine.split(FlexCommentAnalyser.getContents(trivia.getToken().getOriginalValue()));
  for (int lineOffset = 0; lineOffset < lines.length; lineOffset++) {
   if (codeRecognizer.isLineOfCode(lines[lineOffset])) {
    addIssueAtLine("Sections of code should not be \"commented out\".", trivia.getToken().getLine() + lineOffset);
    break;
   }
  }
 }
}

代码示例来源:origin: org.codehaus.sonar-plugins.flex/flex-squid

public void visitToken(Token token) {
 if (token.getType().equals(GenericTokenType.EOF)) {
  return;
 }
 linesOfCode.add(token.getLine());
 List<Trivia> trivias = token.getTrivia();
 for (Trivia trivia : trivias) {
  if (trivia.isComment()) {
   linesOfComments.add(trivia.getToken().getLine());
  }
 }
}

代码示例来源:origin: org.codehaus.sonar-plugins.php/php-checks

private int getLineStartingColumn(AstNode node) {
 int line = node.getTokenLine();
 AstNode previousNode = node.getPreviousAstNode();
 int column = node.getToken().getColumn();
 while (previousNode != null && previousNode.getToken().getLine() == line) {
  column = previousNode.getToken().getColumn();
  previousNode = previousNode.getParent();
 }
 return column;
}

代码示例来源:origin: org.sonarsource.sonar-plugins.javascript/javascript-squid

@Override
 public SourceCode createSourceCode(SourceCode parentSourceCode, AstNode astNode) {
  AstNode identifier = astNode.getFirstChild(EcmaScriptTokenType.IDENTIFIER, EcmaScriptGrammar.PROPERTY_NAME, Kind.IDENTIFIER);
  final String functionName = identifier == null ? "anonymous" : identifier.getTokenValue();
  final String fileKey = parentSourceCode.isType(SourceFile.class) ? parentSourceCode.getKey() : parentSourceCode.getParent(SourceFile.class).getKey();
  SourceFunction function = new SourceFunction(fileKey + ":" + functionName + ":" + astNode.getToken().getLine() + ":" + astNode.getToken().getColumn());
  function.setStartAtLine(astNode.getTokenLine());
  return function;
 }
}, FUNCTION_NODES));

代码示例来源:origin: org.codehaus.sonar-plugins.php/php-checks

/**
 * Check that else, catch or finally keywords are on the same line as the previous closing curly brace.
 */
private void checkRCurlyBraceOnSameLine(FormattingStandardCheck formattingCheck, AstNode node) {
 Token previsouToken = node.getPreviousAstNode().getLastToken();
 String keyword = node.getFirstChild(PHPKeyword.ELSE, PHPKeyword.CATCH, PHPKeyword.FINALLY).getTokenOriginalValue();
 if (previsouToken.getType().equals(PHPPunctuator.RCURLYBRACE) && previsouToken.getLine() != node.getTokenLine()) {
  formattingCheck.reportIssue("Move this \"" + keyword + "\" to the same line as the previous closing curly brace.", node);
 }
}

代码示例来源:origin: org.codehaus.sonar-plugins.erlang/erlang-squid

@Override
 public SourceCode createSourceCode(SourceCode parentSourceCode, AstNode astNode) {
  String className = astNode.getFirstDescendant(ErlangGrammarImpl.moduleAttr).getFirstChild(ErlangGrammarImpl.atom).getTokenValue();
  SourceClass cls = new SourceClass(className + ":"
   + astNode.getToken().getLine());
  cls.setStartAtLine(astNode.getTokenLine());
  return cls;
 }
}, ErlangGrammarImpl.module));

代码示例来源:origin: org.codehaus.sonar-plugins.php/php-checks

private void retrieveTypeFromDoc(AstNode varDeclaration) {
 Token varDecToken = varDeclaration.getToken();
 for (Trivia comment : varDecToken.getTrivia()) {
  for (String line : comment.getToken().getValue().split("[" + LexicalConstant.LINE_TERMINATOR + "]++")) {
   retrieveTypeFromCommentLine(line);
  }
 }
}

代码示例来源:origin: org.sonarsource.sslr/sslr-core

private void appendNodecontent(AstNode node) throws IOException {
 writer.append(node.getName());
 if (node.getTokenValue() != null) {
  writer.append(" tokenValue=\"" + node.getTokenValue() + "\"");
 }
 if (node.hasToken()) {
  writer.append(" tokenLine=\"" + node.getTokenLine() + "\" tokenColumn=\"" + node.getToken().getColumn() + "\"");
 }
}

代码示例来源:origin: org.codehaus.sonar-plugins.php/php-checks

@Override
public void visitNode(AstNode astNode) {
 Token token = astNode.getToken();
 if (token.getColumn() != 0 || token.getLine() != 1 || !OPENING_TAG.matcher(token.getOriginalValue()).matches()) {
  getContext().createLineViolation(this, "Remove the extra characters before the open tag.", astNode);
 }
}

代码示例来源:origin: org.codehaus.sonar-plugins.php/php-checks

private boolean isFirstOnline(AstNode curlyBrace) {
 Token previousToken = curlyBrace.getPreviousAstNode().getLastToken();
 // In one case, clonsing parenthesis can be on the same line as the opening curly brace
 if (previousToken.getType().equals(PHPPunctuator.RPARENTHESIS)) {
  previousToken = curlyBrace.getPreviousAstNode().getPreviousAstNode().getLastToken();
 }
 return previousToken.getLine() != curlyBrace.getTokenLine();
}

代码示例来源:origin: org.sonarsource.sslr/sslr-core

@Override
public String toString() {
 StringBuilder result = new StringBuilder();
 result.append(name);
 if (token != null) {
  result.append(" tokenValue='").append(token.getValue()).append("'");
  result.append(" tokenLine=").append(token.getLine());
  result.append(" tokenColumn=").append(token.getColumn());
 }
 return result.toString();
}

代码示例来源:origin: org.codehaus.sonar-plugins.python/python-checks

private void visitComment(Trivia trivia) {
  String comment = trivia.getToken().getValue();
  Matcher matcher = patternTodoFixme.matcher(comment);
  if (matcher.find()) {
   String tail = comment.substring(matcher.end());
   if (!patternPersonReference.matcher(tail).find()) {
    getContext().createLineViolation(this, MESSAGE, trivia.getToken().getLine());
   }
  }
 }
}

代码示例来源:origin: org.codehaus.sonar-plugins.php/php-checks

private boolean isReturningBoolean(AstNode methodDec) {
 Token functionToken = methodDec.getToken();
 for (Trivia comment : functionToken.getTrivia()) {
  for (String line : comment.getToken().getOriginalValue().split(LexicalConstant.LINE_TERMINATOR)) {
   if (StringUtils.containsIgnoreCase(line, RETURN_TAG)) {
    return returnsBoolean(line);
   }
  }
 }
 return false;
}

代码示例来源:origin: org.codehaus.sonar-plugins.php/php-checks

/**
 * Check there is not space between a function's name and the opening parenthesis.
 */
private void checkSpaceAfterFunctionName(FormattingStandardCheck formattingCheck, AstNode node) {
 Token lParenToken = node.getFirstChild(PHPPunctuator.LPARENTHESIS).getToken();
 Token funcNameToken = node.is(PHPGrammar.FUNCTION_CALL_PARAMETER_LIST) ?
  node.getPreviousAstNode().getLastToken() : node.getFirstChild(PHPGrammar.IDENTIFIER).getToken();
 if (getNbSpaceBetween(funcNameToken, lParenToken) != 0) {
  formattingCheck.reportIssue("Remove all space between the method name \"" + funcNameToken.getOriginalValue() + "\" and the opening parenthesis.", node);
 }
}

相关文章