You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

GoParserBase.java 4.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package depends.extractor.golang;
  2. import org.antlr.v4.runtime.*;
  3. import java.util.List;
  4. /**
  5. * All parser methods that used in grammar (p, prev, notLineTerminator, etc.)
  6. * should start with lower case char similar to parser rules.
  7. */
  8. public abstract class GoParserBase extends Parser
  9. {
  10. protected GoParserBase(TokenStream input) {
  11. super(input);
  12. }
  13. /**
  14. * Returns {@code true} iff on the current index of the parser's
  15. * token stream a token exists on the {@code HIDDEN} channel which
  16. * either is a line terminator, or is a multi line comment that
  17. * contains a line terminator.
  18. *
  19. * @return {@code true} iff on the current index of the parser's
  20. * token stream a token exists on the {@code HIDDEN} channel which
  21. * either is a line terminator, or is a multi line comment that
  22. * contains a line terminator.
  23. */
  24. protected boolean lineTerminatorAhead() {
  25. // Get the token ahead of the current index.
  26. int possibleIndexEosToken = this.getCurrentToken().getTokenIndex() - 1;
  27. if (possibleIndexEosToken == -1)
  28. {
  29. return true;
  30. }
  31. Token ahead = _input.get(possibleIndexEosToken);
  32. if (ahead.getChannel() != Lexer.HIDDEN) {
  33. // We're only interested in tokens on the HIDDEN channel.
  34. return false;
  35. }
  36. if (ahead.getType() == GoLexer.TERMINATOR) {
  37. // There is definitely a line terminator ahead.
  38. return true;
  39. }
  40. if (ahead.getType() == GoLexer.WS) {
  41. // Get the token ahead of the current whitespaces.
  42. possibleIndexEosToken = this.getCurrentToken().getTokenIndex() - 2;
  43. ahead = _input.get(possibleIndexEosToken);
  44. }
  45. // Get the token's text and type.
  46. String text = ahead.getText();
  47. int type = ahead.getType();
  48. // Check if the token is, or contains a line terminator.
  49. return (type == GoLexer.COMMENT && (text.contains("\r") || text.contains("\n"))) ||
  50. (type == GoLexer.TERMINATOR);
  51. }
  52. /**
  53. * Returns {@code true} if no line terminator exists between the specified
  54. * token offset and the prior one on the {@code HIDDEN} channel.
  55. *
  56. * @return {@code true} if no line terminator exists between the specified
  57. * token offset and the prior one on the {@code HIDDEN} channel.
  58. */
  59. protected boolean noTerminatorBetween(int tokenOffset) {
  60. BufferedTokenStream stream = (BufferedTokenStream)_input;
  61. List<Token> tokens = stream.getHiddenTokensToLeft(stream.LT(tokenOffset).getTokenIndex());
  62. if (tokens == null) {
  63. return true;
  64. }
  65. for (Token token : tokens) {
  66. if (token.getText().contains("\n"))
  67. return false;
  68. }
  69. return true;
  70. }
  71. /**
  72. * Returns {@code true} if no line terminator exists after any encounterd
  73. * parameters beyond the specified token offset and the next on the
  74. * {@code HIDDEN} channel.
  75. *
  76. * @return {@code true} if no line terminator exists after any encounterd
  77. * parameters beyond the specified token offset and the next on the
  78. * {@code HIDDEN} channel.
  79. */
  80. protected boolean noTerminatorAfterParams(int tokenOffset) {
  81. BufferedTokenStream stream = (BufferedTokenStream)_input;
  82. int leftParams = 1;
  83. int rightParams = 0;
  84. int valueType;
  85. if (stream.LT(tokenOffset).getType() == GoLexer.L_PAREN) {
  86. // Scan past parameters
  87. while (leftParams != rightParams) {
  88. tokenOffset++;
  89. valueType = stream.LT(tokenOffset).getType();
  90. if (valueType == GoLexer.L_PAREN){
  91. leftParams++;
  92. }
  93. else if (valueType == GoLexer.R_PAREN) {
  94. rightParams++;
  95. }
  96. }
  97. tokenOffset++;
  98. return noTerminatorBetween(tokenOffset);
  99. }
  100. return true;
  101. }
  102. protected boolean checkPreviousTokenText(String text)
  103. {
  104. BufferedTokenStream stream = (BufferedTokenStream)_input;
  105. String prevTokenText = stream.LT(1).getText();
  106. if (prevTokenText == null)
  107. return false;
  108. return prevTokenText.equals(text);
  109. }
  110. }

人工智能研发终端

Contributors (2)