parser.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. 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 (token === undefined && type === '') ||
  202. token && token.type === type;
  203. },
  204. match: function(token1, token2) {
  205. return this.matchToken(token1, 0) &&
  206. (!token2 || this.matchToken(token2, 1));
  207. },
  208. };
  209. return Parser;
  210. });