RustLexerBase.java 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package de.jplag.rust.grammar;
  2. import org.antlr.v4.runtime.*;
  3. public abstract class RustLexerBase extends Lexer {
  4. protected RustLexerBase(CharStream input) {
  5. super(input);
  6. }
  7. Token lt1;
  8. Token lt2;
  9. @Override
  10. public Token nextToken() {
  11. Token next = super.nextToken();
  12. if (next.getChannel() == Token.DEFAULT_CHANNEL) {
  13. // Keep track of the last token on the default channel.
  14. this.lt2 = this.lt1;
  15. this.lt1 = next;
  16. }
  17. return next;
  18. }
  19. public boolean SOF() {
  20. return _input.LA(-1) <= 0;
  21. }
  22. public boolean next(char expect) {
  23. return _input.LA(1) == expect;
  24. }
  25. public boolean floatDotPossible() {
  26. int next = _input.LA(1);
  27. // only block . _ identifier after float
  28. if (next == '.' || next == '_')
  29. return false;
  30. if (next == 'f') {
  31. // 1.f32
  32. if (_input.LA(2) == '3' && _input.LA(3) == '2')
  33. return true;
  34. // 1.f64
  35. if (_input.LA(2) == '6' && _input.LA(3) == '4')
  36. return true;
  37. return false;
  38. }
  39. if (next >= 'a' && next <= 'z')
  40. return false;
  41. if (next >= 'A' && next <= 'Z')
  42. return false;
  43. return true;
  44. }
  45. public boolean floatLiteralPossible() {
  46. if (this.lt1 == null || this.lt2 == null)
  47. return true;
  48. if (this.lt1.getType() != RustLexer.DOT)
  49. return true;
  50. switch (this.lt2.getType()) {
  51. case RustLexer.CHAR_LITERAL:
  52. case RustLexer.STRING_LITERAL:
  53. case RustLexer.RAW_STRING_LITERAL:
  54. case RustLexer.BYTE_LITERAL:
  55. case RustLexer.BYTE_STRING_LITERAL:
  56. case RustLexer.RAW_BYTE_STRING_LITERAL:
  57. case RustLexer.INTEGER_LITERAL:
  58. case RustLexer.DEC_LITERAL:
  59. case RustLexer.HEX_LITERAL:
  60. case RustLexer.OCT_LITERAL:
  61. case RustLexer.BIN_LITERAL:
  62. case RustLexer.KW_SUPER:
  63. case RustLexer.KW_SELFVALUE:
  64. case RustLexer.KW_SELFTYPE:
  65. case RustLexer.KW_CRATE:
  66. case RustLexer.KW_DOLLARCRATE:
  67. case RustLexer.GT:
  68. case RustLexer.RCURLYBRACE:
  69. case RustLexer.RSQUAREBRACKET:
  70. case RustLexer.RPAREN:
  71. case RustLexer.KW_AWAIT:
  72. case RustLexer.NON_KEYWORD_IDENTIFIER:
  73. case RustLexer.RAW_IDENTIFIER:
  74. case RustLexer.KW_MACRORULES:
  75. return false;
  76. default:
  77. return true;
  78. }
  79. }
  80. }