parser.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. define([
  2. './lexer'
  3. ], function (Lexer) {
  4. 'use strict';
  5. function Parser(expression) {
  6. this.expression = expression;
  7. this.lexer = new Lexer(expression);
  8. this.tokens = this.lexer.tokenize();
  9. this.index = 0;
  10. }
  11. Parser.prototype = {
  12. getAst: function () {
  13. return this.start();
  14. },
  15. start: function () {
  16. try {
  17. return this.functionCall() || this.metricExpression();
  18. }
  19. catch (e) {
  20. return {
  21. type: 'error',
  22. message: e.message,
  23. pos: e.pos
  24. };
  25. }
  26. },
  27. curlyBraceSegment: function() {
  28. if (this.match('identifier', '{') || this.match('{')) {
  29. var 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. }
  47. else {
  48. return null;
  49. }
  50. },
  51. metricSegment: function() {
  52. var curly = this.curlyBraceSegment();
  53. if (curly) {
  54. return curly;
  55. }
  56. if (this.match('identifier') || this.match('number')) {
  57. // hack to handle float numbers in metric segments
  58. var parts = this.consumeToken().value.split('.');
  59. if (parts.length === 2) {
  60. this.tokens.splice(this.index, 0, { type: '.' });
  61. this.tokens.splice(this.index + 1, 0, { type: 'number', value: parts[1] });
  62. }
  63. return {
  64. type: 'segment',
  65. value: parts[0]
  66. };
  67. }
  68. if (!this.match('templateStart')) {
  69. this.errorMark('Expected metric identifier');
  70. }
  71. this.consumeToken();
  72. if (!this.match('identifier')) {
  73. this.errorMark('Expected identifier after templateStart');
  74. }
  75. var node = {
  76. type: 'template',
  77. value: this.consumeToken().value
  78. };
  79. if (!this.match('templateEnd')) {
  80. this.errorMark('Expected templateEnd');
  81. }
  82. this.consumeToken();
  83. return node;
  84. },
  85. metricExpression: function() {
  86. if (!this.match('templateStart') &&
  87. !this.match('identifier') &&
  88. !this.match('number') &&
  89. !this.match('{')) {
  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 = {
  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. functionParameters: function () {
  125. if (this.match(')') || this.match('')) {
  126. return [];
  127. }
  128. var param =
  129. this.functionCall() ||
  130. this.numericLiteral() ||
  131. this.seriesRefExpression() ||
  132. this.metricExpression() ||
  133. this.stringLiteral();
  134. if (!this.match(',')) {
  135. return [param];
  136. }
  137. this.consumeToken();
  138. return [param].concat(this.functionParameters());
  139. },
  140. seriesRefExpression: function() {
  141. if (!this.match('identifier')) {
  142. return null;
  143. }
  144. var value = this.tokens[this.index].value;
  145. if (!value.match(/\#[A-Z]/)) {
  146. return null;
  147. }
  148. var token = this.consumeToken();
  149. return {
  150. type: 'series-ref',
  151. value: token.value
  152. };
  153. },
  154. numericLiteral: function () {
  155. if (!this.match('number')) {
  156. return null;
  157. }
  158. return {
  159. type: 'number',
  160. value: parseFloat(this.consumeToken().value)
  161. };
  162. },
  163. stringLiteral: function () {
  164. if (!this.match('string')) {
  165. return null;
  166. }
  167. var token = this.consumeToken();
  168. if (token.isUnclosed) {
  169. throw { message: 'Unclosed string parameter', pos: token.pos };
  170. }
  171. return {
  172. type: 'string',
  173. value: token.value
  174. };
  175. },
  176. errorMark: function(text) {
  177. var currentToken = this.tokens[this.index];
  178. var type = currentToken ? currentToken.type : 'end of string';
  179. throw {
  180. message: text + " instead found " + type,
  181. pos: currentToken ? currentToken.pos : this.lexer.char
  182. };
  183. },
  184. // returns token value and incre
  185. consumeToken: function() {
  186. this.index++;
  187. return this.tokens[this.index - 1];
  188. },
  189. matchToken: function(type, index) {
  190. var token = this.tokens[this.index + index];
  191. return (token === undefined && type === '') ||
  192. token && token.type === type;
  193. },
  194. match: function(token1, token2) {
  195. return this.matchToken(token1, 0) &&
  196. (!token2 || this.matchToken(token2, 1));
  197. },
  198. };
  199. return Parser;
  200. });