LinkedToken.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import { monacoTypes } from '@grafana/ui';
  2. import { TokenTypes } from './types';
  3. export class LinkedToken {
  4. constructor(
  5. public type: string,
  6. public value: string,
  7. public range: monacoTypes.IRange,
  8. public previous: LinkedToken | null,
  9. public next: LinkedToken | null,
  10. public tokenTypes: TokenTypes
  11. ) {}
  12. isKeyword(): boolean {
  13. return this.type === this.tokenTypes.Keyword;
  14. }
  15. isWhiteSpace(): boolean {
  16. return this.type === this.tokenTypes.Whitespace;
  17. }
  18. isParenthesis(): boolean {
  19. return this.type === this.tokenTypes.Parenthesis;
  20. }
  21. isIdentifier(): boolean {
  22. return this.type === this.tokenTypes.Identifier;
  23. }
  24. isString(): boolean {
  25. return this.type === this.tokenTypes.String;
  26. }
  27. isDoubleQuotedString(): boolean {
  28. return this.type === this.tokenTypes.Type;
  29. }
  30. isVariable(): boolean {
  31. return this.type === this.tokenTypes.Variable;
  32. }
  33. isFunction(): boolean {
  34. return this.type === this.tokenTypes.Function;
  35. }
  36. isNumber(): boolean {
  37. return this.type === this.tokenTypes.Number;
  38. }
  39. is(type: string, value?: string | number | boolean): boolean {
  40. const isType = this.type === type;
  41. return value !== undefined ? isType && this.value === value : isType;
  42. }
  43. endsWith(value: string | number | boolean): boolean {
  44. return this.value === value || this.value[this.value.length - 1] === value;
  45. }
  46. getPreviousNonWhiteSpaceToken(): LinkedToken | null {
  47. let curr = this.previous;
  48. while (curr != null) {
  49. if (!curr.isWhiteSpace()) {
  50. return curr;
  51. }
  52. curr = curr.previous;
  53. }
  54. return null;
  55. }
  56. getPreviousOfType(type: string, value?: string): LinkedToken | null {
  57. let curr = this.previous;
  58. while (curr != null) {
  59. const isType = curr.type === type;
  60. if (value !== undefined ? isType && curr.value === value : isType) {
  61. return curr;
  62. }
  63. curr = curr.previous;
  64. }
  65. return null;
  66. }
  67. getPreviousUntil(type: string, ignoreTypes: string[], value?: string): LinkedToken[] | null {
  68. let tokens: LinkedToken[] = [];
  69. let curr = this.previous;
  70. while (curr != null) {
  71. if (ignoreTypes.some((t) => t === curr?.type)) {
  72. curr = curr.previous;
  73. continue;
  74. }
  75. const isType = curr.type === type;
  76. if (value !== undefined ? isType && curr.value === value : isType) {
  77. return tokens;
  78. }
  79. if (!curr.isWhiteSpace()) {
  80. tokens.push(curr);
  81. }
  82. curr = curr.previous;
  83. }
  84. return tokens;
  85. }
  86. getNextUntil(type: string, ignoreTypes: string[], value?: string): LinkedToken[] | null {
  87. let tokens: LinkedToken[] = [];
  88. let curr = this.next;
  89. while (curr != null) {
  90. if (ignoreTypes.some((t) => t === curr?.type)) {
  91. curr = curr.next;
  92. continue;
  93. }
  94. const isType = curr.type === type;
  95. if (value !== undefined ? isType && curr.value === value : isType) {
  96. return tokens;
  97. }
  98. if (!curr.isWhiteSpace()) {
  99. tokens.push(curr);
  100. }
  101. curr = curr.next;
  102. }
  103. return tokens;
  104. }
  105. getPreviousKeyword(): LinkedToken | null {
  106. let curr = this.previous;
  107. while (curr != null) {
  108. if (curr.isKeyword()) {
  109. return curr;
  110. }
  111. curr = curr.previous;
  112. }
  113. return null;
  114. }
  115. getNextNonWhiteSpaceToken(): LinkedToken | null {
  116. let curr = this.next;
  117. while (curr != null) {
  118. if (!curr.isWhiteSpace()) {
  119. return curr;
  120. }
  121. curr = curr.next;
  122. }
  123. return null;
  124. }
  125. getNextOfType(type: string, value?: string): LinkedToken | null {
  126. let curr = this.next;
  127. while (curr != null) {
  128. const isType = curr.type === type;
  129. if (value !== undefined ? isType && curr.value === value : isType) {
  130. return curr;
  131. }
  132. curr = curr.next;
  133. }
  134. return null;
  135. }
  136. }