parser.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. import { Lexer } from "./lexer";
  2. export function Parser(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. var 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. var 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. var 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. var 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 (
  85. !this.match("templateStart") &&
  86. !this.match("identifier") &&
  87. !this.match("number") &&
  88. !this.match("{")
  89. ) {
  90. return null;
  91. }
  92. var node = {
  93. type: "metric",
  94. segments: []
  95. };
  96. node.segments.push(this.metricSegment());
  97. while (this.match(".")) {
  98. this.consumeToken();
  99. var segment = this.metricSegment();
  100. if (!segment) {
  101. this.errorMark("Expected metric identifier");
  102. }
  103. node.segments.push(segment);
  104. }
  105. return node;
  106. },
  107. functionCall: function() {
  108. if (!this.match("identifier", "(")) {
  109. return null;
  110. }
  111. var node: any = {
  112. type: "function",
  113. name: this.consumeToken().value
  114. };
  115. // consume left parenthesis
  116. this.consumeToken();
  117. node.params = this.functionParameters();
  118. if (!this.match(")")) {
  119. this.errorMark("Expected closing parenthesis");
  120. }
  121. this.consumeToken();
  122. return node;
  123. },
  124. boolExpression: function() {
  125. if (!this.match("bool")) {
  126. return null;
  127. }
  128. return {
  129. type: "bool",
  130. value: this.consumeToken().value === "true"
  131. };
  132. },
  133. functionParameters: function() {
  134. if (this.match(")") || this.match("")) {
  135. return [];
  136. }
  137. var param =
  138. this.functionCall() ||
  139. this.numericLiteral() ||
  140. this.seriesRefExpression() ||
  141. this.boolExpression() ||
  142. this.metricExpression() ||
  143. this.stringLiteral();
  144. if (!this.match(",")) {
  145. return [param];
  146. }
  147. this.consumeToken();
  148. return [param].concat(this.functionParameters());
  149. },
  150. seriesRefExpression: function() {
  151. if (!this.match("identifier")) {
  152. return null;
  153. }
  154. var value = this.tokens[this.index].value;
  155. if (!value.match(/\#[A-Z]/)) {
  156. return null;
  157. }
  158. var token = this.consumeToken();
  159. return {
  160. type: "series-ref",
  161. value: token.value
  162. };
  163. },
  164. numericLiteral: function() {
  165. if (!this.match("number")) {
  166. return null;
  167. }
  168. return {
  169. type: "number",
  170. value: parseFloat(this.consumeToken().value)
  171. };
  172. },
  173. stringLiteral: function() {
  174. if (!this.match("string")) {
  175. return null;
  176. }
  177. var token = this.consumeToken();
  178. if (token.isUnclosed) {
  179. throw { message: "Unclosed string parameter", pos: token.pos };
  180. }
  181. return {
  182. type: "string",
  183. value: token.value
  184. };
  185. },
  186. errorMark: function(text) {
  187. var currentToken = this.tokens[this.index];
  188. var type = currentToken ? currentToken.type : "end of string";
  189. throw {
  190. message: text + " instead found " + type,
  191. pos: currentToken ? currentToken.pos : this.lexer.char
  192. };
  193. },
  194. // returns token value and incre
  195. consumeToken: function() {
  196. this.index++;
  197. return this.tokens[this.index - 1];
  198. },
  199. matchToken: function(type, index) {
  200. var token = this.tokens[this.index + index];
  201. return (
  202. (token === undefined && type === "") || (token && token.type === type)
  203. );
  204. },
  205. match: function(token1, token2) {
  206. return (
  207. this.matchToken(token1, 0) && (!token2 || this.matchToken(token2, 1))
  208. );
  209. }
  210. };