parser.ts 5.2 KB

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