parser.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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')) {
  57. return {
  58. type: 'segment',
  59. value: this.consumeToken().value
  60. };
  61. }
  62. if (!this.match('templateStart')) {
  63. this.errorMark('Expected metric identifier');
  64. }
  65. this.consumeToken();
  66. if (!this.match('identifier')) {
  67. this.errorMark('Expected identifier after templateStart');
  68. }
  69. var node = {
  70. type: 'template',
  71. value: this.consumeToken().value
  72. };
  73. if (!this.match('templateEnd')) {
  74. this.errorMark('Expected templateEnd');
  75. }
  76. this.consumeToken();
  77. return node;
  78. },
  79. metricExpression: function() {
  80. if (!this.match('templateStart') && !this.match('identifier')) {
  81. return null;
  82. }
  83. var node = {
  84. type: 'metric',
  85. segments: []
  86. };
  87. node.segments.push(this.metricSegment());
  88. while(this.match('.')) {
  89. this.consumeToken();
  90. var segment = this.metricSegment();
  91. if (!segment) {
  92. this.errorMark('Expected metric identifier');
  93. }
  94. node.segments.push(segment);
  95. }
  96. return node;
  97. },
  98. functionCall: function() {
  99. if (!this.match('identifier', '(')) {
  100. return null;
  101. }
  102. var node = {
  103. type: 'function',
  104. name: this.consumeToken().value,
  105. };
  106. // consume left paranthesis
  107. this.consumeToken();
  108. node.params = this.functionParameters();
  109. if (!this.match(')')) {
  110. this.errorMark('Expected closing paranthesis');
  111. }
  112. this.consumeToken();
  113. return node;
  114. },
  115. functionParameters: function () {
  116. if (this.match(')') || this.match('')) {
  117. return [];
  118. }
  119. var param =
  120. this.functionCall() ||
  121. this.metricExpression() ||
  122. this.numericLiteral() ||
  123. this.stringLiteral();
  124. if (!this.match(',')) {
  125. return [param];
  126. }
  127. this.consumeToken();
  128. return [param].concat(this.functionParameters());
  129. },
  130. numericLiteral: function () {
  131. if (!this.match('number')) {
  132. return null;
  133. }
  134. return {
  135. type: 'number',
  136. value: parseInt(this.consumeToken().value, 10)
  137. };
  138. },
  139. stringLiteral: function () {
  140. if (!this.match('string')) {
  141. return null;
  142. }
  143. var token = this.consumeToken();
  144. if (token.isUnclosed) {
  145. throw { message: 'Unclosed string parameter', pos: token.pos };
  146. }
  147. return {
  148. type: 'string',
  149. value: token.value
  150. };
  151. },
  152. errorMark: function(text) {
  153. var currentToken = this.tokens[this.index];
  154. var type = currentToken ? currentToken.type : 'end of string';
  155. throw {
  156. message: text + " instead found " + type,
  157. pos: currentToken ? currentToken.pos : this.lexer.char
  158. };
  159. },
  160. // returns token value and incre
  161. consumeToken: function() {
  162. this.index++;
  163. return this.tokens[this.index-1];
  164. },
  165. matchToken: function(type, index) {
  166. var token = this.tokens[this.index + index];
  167. return (token === undefined && type === '') ||
  168. token && token.type === type;
  169. },
  170. match: function(token1, token2) {
  171. return this.matchToken(token1, 0) &&
  172. (!token2 || this.matchToken(token2, 1));
  173. },
  174. };
  175. return Parser;
  176. });