parser.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. import { Lexer } from './lexer';
  2. export function Parser(this: any, expression) {
  3. this.expression = expression;
  4. this.lexer = new Lexer(expression);
  5. this.tokens = this.lexer.tokenize();
  6. this.index = 0;
  7. }
  8. Parser.prototype = {
  9. getAst: function() {
  10. return this.start();
  11. },
  12. start: function() {
  13. try {
  14. return this.functionCall() || this.metricExpression();
  15. } catch (e) {
  16. return {
  17. type: 'error',
  18. message: e.message,
  19. pos: e.pos,
  20. };
  21. }
  22. },
  23. curlyBraceSegment: function() {
  24. if (this.match('identifier', '{') || this.match('{')) {
  25. let curlySegment = '';
  26. while (!this.match('') && !this.match('}')) {
  27. curlySegment += this.consumeToken().value;
  28. }
  29. if (!this.match('}')) {
  30. this.errorMark("Expected closing '}'");
  31. }
  32. curlySegment += this.consumeToken().value;
  33. // if curly segment is directly followed by identifier
  34. // include it in the segment
  35. if (this.match('identifier')) {
  36. curlySegment += this.consumeToken().value;
  37. }
  38. return {
  39. type: 'segment',
  40. value: curlySegment,
  41. };
  42. } else {
  43. return null;
  44. }
  45. },
  46. metricSegment: function() {
  47. const curly = this.curlyBraceSegment();
  48. if (curly) {
  49. return curly;
  50. }
  51. if (this.match('identifier') || this.match('number')) {
  52. // hack to handle float numbers in metric segments
  53. const parts = this.consumeToken().value.split('.');
  54. if (parts.length === 2) {
  55. this.tokens.splice(this.index, 0, { type: '.' });
  56. this.tokens.splice(this.index + 1, 0, {
  57. type: 'number',
  58. value: parts[1],
  59. });
  60. }
  61. return {
  62. type: 'segment',
  63. value: parts[0],
  64. };
  65. }
  66. if (!this.match('templateStart')) {
  67. this.errorMark('Expected metric identifier');
  68. }
  69. this.consumeToken();
  70. if (!this.match('identifier')) {
  71. this.errorMark('Expected identifier after templateStart');
  72. }
  73. const node = {
  74. type: 'template',
  75. value: this.consumeToken().value,
  76. };
  77. if (!this.match('templateEnd')) {
  78. this.errorMark('Expected templateEnd');
  79. }
  80. this.consumeToken();
  81. return node;
  82. },
  83. metricExpression: function() {
  84. if (!this.match('templateStart') && !this.match('identifier') && !this.match('number') && !this.match('{')) {
  85. return null;
  86. }
  87. const node = {
  88. type: 'metric',
  89. segments: [],
  90. };
  91. node.segments.push(this.metricSegment());
  92. while (this.match('.')) {
  93. this.consumeToken();
  94. const segment = this.metricSegment();
  95. if (!segment) {
  96. this.errorMark('Expected metric identifier');
  97. }
  98. node.segments.push(segment);
  99. }
  100. return node;
  101. },
  102. functionCall: function() {
  103. if (!this.match('identifier', '(')) {
  104. return null;
  105. }
  106. const node: any = {
  107. type: 'function',
  108. name: this.consumeToken().value,
  109. };
  110. // consume left parenthesis
  111. this.consumeToken();
  112. node.params = this.functionParameters();
  113. if (!this.match(')')) {
  114. this.errorMark('Expected closing parenthesis');
  115. }
  116. this.consumeToken();
  117. return node;
  118. },
  119. boolExpression: function() {
  120. if (!this.match('bool')) {
  121. return null;
  122. }
  123. return {
  124. type: 'bool',
  125. value: this.consumeToken().value === 'true',
  126. };
  127. },
  128. functionParameters: function() {
  129. if (this.match(')') || this.match('')) {
  130. return [];
  131. }
  132. const param =
  133. this.functionCall() ||
  134. this.numericLiteral() ||
  135. this.seriesRefExpression() ||
  136. this.boolExpression() ||
  137. this.metricExpression() ||
  138. this.stringLiteral();
  139. if (!this.match(',')) {
  140. return [param];
  141. }
  142. this.consumeToken();
  143. return [param].concat(this.functionParameters());
  144. },
  145. seriesRefExpression: function() {
  146. if (!this.match('identifier')) {
  147. return null;
  148. }
  149. const value = this.tokens[this.index].value;
  150. if (!value.match(/\#[A-Z]/)) {
  151. return null;
  152. }
  153. const token = this.consumeToken();
  154. return {
  155. type: 'series-ref',
  156. value: token.value,
  157. };
  158. },
  159. numericLiteral: function() {
  160. if (!this.match('number')) {
  161. return null;
  162. }
  163. return {
  164. type: 'number',
  165. value: parseFloat(this.consumeToken().value),
  166. };
  167. },
  168. stringLiteral: function() {
  169. if (!this.match('string')) {
  170. return null;
  171. }
  172. const token = this.consumeToken();
  173. if (token.isUnclosed) {
  174. throw { message: 'Unclosed string parameter', pos: token.pos };
  175. }
  176. return {
  177. type: 'string',
  178. value: token.value,
  179. };
  180. },
  181. errorMark: function(text) {
  182. const currentToken = this.tokens[this.index];
  183. const type = currentToken ? currentToken.type : 'end of string';
  184. throw {
  185. message: text + ' instead found ' + type,
  186. pos: currentToken ? currentToken.pos : this.lexer.char,
  187. };
  188. },
  189. // returns token value and incre
  190. consumeToken: function() {
  191. this.index++;
  192. return this.tokens[this.index - 1];
  193. },
  194. matchToken: function(type, index) {
  195. const token = this.tokens[this.index + index];
  196. return (token === undefined && type === '') || (token && token.type === type);
  197. },
  198. match: function(token1, token2) {
  199. return this.matchToken(token1, 0) && (!token2 || this.matchToken(token2, 1));
  200. },
  201. };