Source: mvc/parser/parsingResult.js

  1. /**
  2. * Dependencies:
  3. *
  4. * * parser.element (parserModule.js)
  5. * * OPTIONALLY: ANTLR TokenStream (antlr3-all.js)
  6. * (if present, the constructor may be able to automatically derive start/end properties from given argument)
  7. *
  8. *
  9. * @requires antlr3-all.js as <code>org</code> (see comment above)
  10. *
  11. */
  12. define(['mmirf/parserModule'],
  13. //this comment is needed by jsdoc2 [copy of comment for: function ParsingResult(...]
  14. /**
  15. * ParsingResult represents an element that was detected during parsing.
  16. *
  17. * <p>
  18. * The detected element is referenced by the properties <code>start</code> and <code>end</code>
  19. * that refer to the start-index and end-index within the parsed text.
  20. *
  21. * <p>
  22. * The ParsingResult has a <code>type</code> property which refers to the kind of element
  23. * that was detected (see constants in {@link mmir.parser.Element}).
  24. *
  25. * <p>
  26. * In addition, the ParsingResult may have several properties that depend of its type. In general,
  27. * these properties refer to detected parts of the element (e.g. for a invocation-statement, these
  28. * may refer to its arguments).
  29. *
  30. *
  31. * <p>
  32. * Properties for <strong>INCLUDE_SCRIPT</strong> type:
  33. * <ul>
  34. * <li><strong>scriptPath</strong>: the path / URL to the resource</li>
  35. * <li><strong>scriptPathType</strong>: the type of the <tt>scriptPath</tt>
  36. * field: one of <code>StringLiteral</code>, <code>Identifier</code>, <code>IdentifierNameAmpersatStart</code></li>
  37. * </ul>
  38. * <p>
  39. * Properties for <strong>INCLUDE_STYLE</strong> type:
  40. * <ul>
  41. * <li><strong>stylePath</strong>: </li>
  42. * <li><strong>stylePathType</strong>: the type of the <tt>stylePath</tt>
  43. * field: one of <code>StringLiteral</code>, <code>Identifier</code>, <code>IdentifierNameAmpersatStart</code></li>
  44. * </ul>
  45. * <p>
  46. * Properties for <strong>LOCALIZE</strong> type:
  47. * <ul>
  48. * <li><strong>name</strong>: the name/identifier for the localized String</li>
  49. * <li><strong>nameType</strong>: the type of the <tt>name</tt>
  50. * field: one of <code>StringLiteral</code>, <code>Identifier</code>, <code>IdentifierNameAmpersatStart</code></li>
  51. * </ul>
  52. * <p>
  53. * Properties for <strong>YIELD_DECLARATION</strong> type:
  54. * <ul>
  55. * <li><strong>name</strong>: the name/identifier for the yield section</li>
  56. * <li><strong>nameType</strong>: the type of the <tt>name</tt>
  57. * field: one of <code>StringLiteral</code>, <code>Identifier</code>, <code>IdentifierNameAmpersatStart</code></li>
  58. * </ul>
  59. * Properties for <strong>BLOCK</strong> type:
  60. * <ul>
  61. * <li><strong>scriptContent</strong> {String}: <tt>OPTIONALLY</tt> the script code as a String</li>
  62. * <li><strong>scriptEval</strong> {Function}: the compiled script code in form of a function. The
  63. * function takes one argument: the current data-object.</li>
  64. * </ul>
  65. * <p>
  66. * Properties for <strong>STATEMENT</strong> type:
  67. * <ul>
  68. * <li><strong>scriptContent</strong> {String}: <tt>OPTIONALLY</tt> the script code as a String</li>
  69. * <li><strong>scriptEval</strong> {Function}: the compiled script code in form of a function. The
  70. * function takes one argument: the current data-object.</li>
  71. * </ul>
  72. * <p>
  73. * Properties for <strong>HELPER</strong> type:
  74. * <ul>
  75. * <li><strong>helper</strong>: the name of the helper function</li>
  76. * <li><strong>helperType</strong>: the type of the <tt>helper</tt>
  77. * field: one of <code>StringLiteral</code>, <code>Identifier</code>, <code>IdentifierNameAmpersatStart</code></li>
  78. * <li><strong>argsEval</strong> {Function}: OPTIONALLY compiled getter Function for retrieving the current ARGS
  79. * (optional argument) of the helper expression. The function takes one argument: the current data-object.</li>
  80. * </ul>
  81. * <p>
  82. * Properties for <strong>IF</strong> type:
  83. * <ul>
  84. * <li><strong>ifEval</strong> {Function}: the condition statement, that was compiled into a Function. The
  85. * function takes one argument: the current data-object.</li>
  86. * <li><strong>content</strong> {ContentElement}: the HTML / template content that should be render, in case the
  87. * if-expression evaluates to <code>true</code>.</li>
  88. * <li><strong>elseContent</strong> {@link mmir.parser.ParsingResult}: OPTIONALLY a ParsingResult
  89. * representing an else-expression, see {@link mmir.parser.Element.ELSE}.</li>
  90. * </ul>
  91. * <p>
  92. * Properties for <strong>ELSE</strong> type:
  93. * <ul>
  94. * <li><strong>content</strong> {ContentElement}: the HTML / template content that should be render, in case the
  95. * if-expression (to which the else-expression belongs) evaluates to <code>false</code>.</li>
  96. * </ul>
  97. * <p>
  98. * Properties for <strong>FOR</strong> type:
  99. * <ul>
  100. * <li><strong>forControlType</strong> {String}: the type of for-loop, either <code>FORITER</code> or <code>FORSTEP</code></li>
  101. * <li><strong>forInitEval</strong> {Function}: the initialization statement of the for-expression, compiled into
  102. * a Function. The function takes one argument: the current data-object.</li>
  103. *
  104. * <li><code>FORITER</code>: <code>@for(PROP in OBJ){ ... }@</code>
  105. * <ul>
  106. * <li><strong>forIterator</strong> {Object}: an iterator object with functions <code>hasNext() : Boolean</code> and
  107. * <code>next() : String</code> (which returns the name of the property currently iterated).</li>
  108. * <li><strong>forPropName</strong> {String}: the variable name for the property which is currently iterated over.</li>
  109. * </ul>
  110. * </li>
  111. * <li><code>FORSTEP</code>: <code>@for(INIT; CONDITION; INCREMENT){ ... }@</code>
  112. * <ul>
  113. * <li><strong>forConditionEval</strong> {Function}: the condition statement of the for-expression, compiled into
  114. * a Function. The function takes one argument: the current data-object.</li>
  115. * <li><strong>forIncrementEval</strong> {Function}: the increment statement of the for-expression, compiled into
  116. * a Function. The function takes one argument: the current data-object.</li>
  117. * </ul>
  118. * </li>
  119. * <li><strong>content</strong> {ContentElement}: the HTML / template content that should be rendered
  120. * during each iteration of the for-loop.</li>
  121. * </ul>
  122. * <p>
  123. * Properties for <strong>RENDER</strong> type:
  124. * <ul>
  125. * <li><strong>partial</strong>: the name of the partial view</li>
  126. * <li><strong>partialType</strong>: the type of the <tt>partial</tt>
  127. * field: one of <code>StringLiteral</code>, <code>Identifier</code>, <code>IdentifierNameAmpersatStart</code></li>
  128. * <li><strong>controller</strong>: the name of the controller, to which the partial view definition belongs</li>
  129. * <li><strong>controllerType</strong>: the type of the <tt>controller</tt>
  130. * field: one of <code>StringLiteral</code>, <code>Identifier</code>, <code>IdentifierNameAmpersatStart</code></li>
  131. * <li><strong>argsEval</strong> {Function}: OPTIONALLY compiled getter Function for retrieving the current ARGS
  132. * (optional argument) of the render expression. The function takes one argument: the current data-object.</li>
  133. * </ul>
  134. * <p>
  135. * Properties for <strong>ESCAPE_ENTER</strong> type:
  136. * <ul>
  137. * <li><strong>text</strong> {String}: the text that will be rendered (i.e. without the escape-character(s) itself).</li>
  138. * </ul>
  139. * <p>
  140. * Properties for <strong>ESCAPE_EXIT</strong> type:
  141. * <ul>
  142. * <li><strong>text</strong> {String}: the text that will be rendered (i.e. without the escape-character(s) itself).</li>
  143. * </ul>
  144. * <p>
  145. * Properties for <strong>VAR_DECLARATION</strong> type:
  146. * <ul>
  147. * <li><strong>name</strong>: the name for the variable (without the leading <tt>@</tt> of template variables)</li>
  148. * <li><strong>nameType</strong>: the type of the <tt>name</tt> field: <code>StringLiteral</code></li>
  149. * </ul>
  150. *
  151. * <p>
  152. *
  153. * @param {org.antlr.runtime.CommonTokenStream|org.antlr.runtime.Token} [thetokens] OPTIONAL
  154. * if <code>org.antlr.runtime.CommonTokenStream</code>:
  155. * the TokenStream that corresponds to this parsed element;
  156. * when provided, the TokenStream is used to set the start- and end-
  157. * property of the new instance.<br>
  158. * if <code>org.antlr.runtime.Token</code>:
  159. * if the parameter is a single Token object, then the start- and end-
  160. * property for the new instance is set by this token object
  161. * @class
  162. * @constructor
  163. * @name ParsingResult
  164. * @memberOf mmir.parser
  165. */
  166. function(parser){
  167. var _antrl3;
  168. //set to @ignore in order to avoid doc-duplication in jsdoc3
  169. /**
  170. * ParsingResult represents an element that was detected during parsing.
  171. *
  172. * <p>
  173. * The detected element is referenced by the properties <code>start</code> and <code>end</code>
  174. * that refer to the start-index and end-index within the parsed text.
  175. *
  176. * <p>
  177. * The ParsingResult has a <code>type</code> property which refers to the kind of element
  178. * that was detected (see constants in {@link mmir.parser.Element}).
  179. *
  180. * <p>
  181. * In addition, the ParsingResult may have several properties that depend of its type. In general,
  182. * these properties refer to detected parts of the element (e.g. for a invocation-statement, these
  183. * may refer to its arguments).
  184. *
  185. *
  186. * <p>
  187. * Properties for <strong>INCLUDE_SCRIPT</strong> type:
  188. * <ul>
  189. * <li><strong>scriptPath</strong>: the path / URL to the resource</li>
  190. * <li><strong>scriptPathType</strong>: the type of the <tt>scriptPath</tt>
  191. * field: one of <code>StringLiteral</code>, <code>Identifier</code>, <code>IdentifierNameAmpersatStart</code></li>
  192. * </ul>
  193. * <p>
  194. * Properties for <strong>INCLUDE_STYLE</strong> type:
  195. * <ul>
  196. * <li><strong>stylePath</strong>: </li>
  197. * <li><strong>stylePathType</strong>: the type of the <tt>stylePath</tt>
  198. * field: one of <code>StringLiteral</code>, <code>Identifier</code>, <code>IdentifierNameAmpersatStart</code></li>
  199. * </ul>
  200. * <p>
  201. * Properties for <strong>LOCALIZE</strong> type:
  202. * <ul>
  203. * <li><strong>name</strong>: the name/identifier for the localized String</li>
  204. * <li><strong>nameType</strong>: the type of the <tt>name</tt>
  205. * field: one of <code>StringLiteral</code>, <code>Identifier</code>, <code>IdentifierNameAmpersatStart</code></li>
  206. * </ul>
  207. * <p>
  208. * Properties for <strong>YIELD_DECLARATION</strong> type:
  209. * <ul>
  210. * <li><strong>name</strong>: the name/identifier for the yield section</li>
  211. * <li><strong>nameType</strong>: the type of the <tt>name</tt>
  212. * field: one of <code>StringLiteral</code>, <code>Identifier</code>, <code>IdentifierNameAmpersatStart</code></li>
  213. * </ul>
  214. * Properties for <strong>BLOCK</strong> type:
  215. * <ul>
  216. * <li><strong>scriptContent</strong> {String}: <tt>OPTIONALLY</tt> the script code as a String</li>
  217. * <li><strong>scriptEval</strong> {Function}: the compiled script code in form of a function. The
  218. * function takes one argument: the current data-object.</li>
  219. * </ul>
  220. * <p>
  221. * Properties for <strong>STATEMENT</strong> type:
  222. * <ul>
  223. * <li><strong>scriptContent</strong> {String}: <tt>OPTIONALLY</tt> the script code as a String</li>
  224. * <li><strong>scriptEval</strong> {Function}: the compiled script code in form of a function. The
  225. * function takes one argument: the current data-object.</li>
  226. * </ul>
  227. * <p>
  228. * Properties for <strong>HELPER</strong> type:
  229. * <ul>
  230. * <li><strong>helper</strong>: the name of the helper function</li>
  231. * <li><strong>helperType</strong>: the type of the <tt>helper</tt>
  232. * field: one of <code>StringLiteral</code>, <code>Identifier</code>, <code>IdentifierNameAmpersatStart</code></li>
  233. * <li><strong>argsEval</strong> {Function}: OPTIONALLY compiled getter Function for retrieving the current ARGS
  234. * (optional argument) of the helper expression. The function takes one argument: the current data-object.</li>
  235. * </ul>
  236. * <p>
  237. * Properties for <strong>IF</strong> type:
  238. * <ul>
  239. * <li><strong>ifEval</strong> {Function}: the condition statement, that was compiled into a Function. The
  240. * function takes one argument: the current data-object.</li>
  241. * <li><strong>content</strong> {ContentElement}: the HTML / template content that should be render, in case the
  242. * if-expression evaluates to <code>true</code>.</li>
  243. * <li><strong>elseContent</strong> {@link mmir.parser.ParsingResult}: OPTIONALLY a ParsingResult
  244. * representing an else-expression, see {@link mmir.parser.Element.ELSE}.</li>
  245. * </ul>
  246. * <p>
  247. * Properties for <strong>ELSE</strong> type:
  248. * <ul>
  249. * <li><strong>content</strong> {ContentElement}: the HTML / template content that should be render, in case the
  250. * if-expression (to which the else-expression belongs) evaluates to <code>false</code>.</li>
  251. * </ul>
  252. * <p>
  253. * Properties for <strong>FOR</strong> type:
  254. * <ul>
  255. * <li><strong>forControlType</strong> {String}: the type of for-loop, either <code>FORITER</code> or <code>FORSTEP</code></li>
  256. * <li><strong>forInitEval</strong> {Function}: the initialization statement of the for-expression, compiled into
  257. * a Function. The function takes one argument: the current data-object.</li>
  258. *
  259. * <li><code>FORITER</code>: <code>@for(PROP in OBJ){ ... }@</code>
  260. * <ul>
  261. * <li><strong>forIterator</strong> {Object}: an iterator object with functions <code>hasNext() : Boolean</code> and
  262. * <code>next() : String</code> (which returns the name of the property currently iterated).</li>
  263. * <li><strong>forPropName</strong> {String}: the variable name for the property which is currently iterated over.</li>
  264. * </ul>
  265. * </li>
  266. * <li><code>FORSTEP</code>: <code>@for(INIT; CONDITION; INCREMENT){ ... }@</code>
  267. * <ul>
  268. * <li><strong>forConditionEval</strong> {Function}: the condition statement of the for-expression, compiled into
  269. * a Function. The function takes one argument: the current data-object.</li>
  270. * <li><strong>forIncrementEval</strong> {Function}: the increment statement of the for-expression, compiled into
  271. * a Function. The function takes one argument: the current data-object.</li>
  272. * </ul>
  273. * </li>
  274. * <li><strong>content</strong> {ContentElement}: the HTML / template content that should be rendered
  275. * during each iteration of the for-loop.</li>
  276. * </ul>
  277. * <p>
  278. * Properties for <strong>RENDER</strong> type:
  279. * <ul>
  280. * <li><strong>partial</strong>: the name of the partial view</li>
  281. * <li><strong>partialType</strong>: the type of the <tt>partial</tt>
  282. * field: one of <code>StringLiteral</code>, <code>Identifier</code>, <code>IdentifierNameAmpersatStart</code></li>
  283. * <li><strong>controller</strong>: the name of the controller, to which the partial view definition belongs</li>
  284. * <li><strong>controllerType</strong>: the type of the <tt>controller</tt>
  285. * field: one of <code>StringLiteral</code>, <code>Identifier</code>, <code>IdentifierNameAmpersatStart</code></li>
  286. * <li><strong>argsEval</strong> {Function}: OPTIONALLY compiled getter Function for retrieving the current ARGS
  287. * (optional argument) of the render expression. The function takes one argument: the current data-object.</li>
  288. * </ul>
  289. * <p>
  290. * Properties for <strong>ESCAPE_ENTER</strong> type:
  291. * <ul>
  292. * <li><strong>text</strong> {String}: the text that will be rendered (i.e. without the escape-character(s) itself).</li>
  293. * </ul>
  294. * <p>
  295. * Properties for <strong>ESCAPE_EXIT</strong> type:
  296. * <ul>
  297. * <li><strong>text</strong> {String}: the text that will be rendered (i.e. without the escape-character(s) itself).</li>
  298. * </ul>
  299. * <p>
  300. * Properties for <strong>VAR_DECLARATION</strong> type:
  301. * <ul>
  302. * <li><strong>name</strong>: the name for the variable (without the leading <tt>@</tt> of template variables)</li>
  303. * <li><strong>nameType</strong>: the type of the <tt>name</tt> field: <code>StringLiteral</code></li>
  304. * </ul>
  305. *
  306. * <p>
  307. *
  308. * @constructs ParsingResult
  309. * @param {org.antlr.runtime.CommonTokenStream|org.antlr.runtime.Token} [thetokens] OPTIONAL
  310. * if <code>org.antlr.runtime.CommonTokenStream</code>:
  311. * the TokenStream that corresponds to this parsed element;
  312. * when provided, the TokenStream is used to set the start- and end-
  313. * property of the new instance.<br>
  314. * if <code>org.antlr.runtime.Token</code>:
  315. * if the parameter is a single Token object, then the start- and end-
  316. * property for the new instance is set by this token object
  317. *
  318. * @ignore
  319. */
  320. function ParsingResult (thetokens){
  321. var isSet = false;
  322. //try to extract start-/end-indexes from the argument:
  323. if(thetokens){
  324. if(typeof org !== 'undefined'){
  325. //NOTE: must invoke getTokens() for initializing size() etc.!
  326. if(thetokens instanceof org.antlr.runtime.CommonTokenStream && thetokens.getTokens() && thetokens.size() > 0){
  327. this.start = thetokens.getTokens()[0].getStartIndex();
  328. this.end = thetokens.getTokens()[thetokens.size()-1].getStopIndex();
  329. isSet = true;
  330. }
  331. else if(thetokens instanceof org.antlr.runtime.CommonToken || thetokens instanceof org.antlr.runtime.Token){
  332. this.start = thetokens.getStartIndex();
  333. this.end = thetokens.getStopIndex();
  334. isSet = true;
  335. }
  336. }
  337. //if not already set, try...
  338. if( isSet === false && typeof thetokens.getToken !== 'undefined' && (typeof thetokens.getToken().getStartIndex !== 'undefined' && typeof thetokens.getToken().getStopIndex !== 'undefined')){
  339. this.start = thetokens.getToken().getStartIndex();
  340. this.end = thetokens.getToken().getStopIndex();
  341. isSet = true;
  342. }
  343. else if(typeof thetokens.getStartIndex !== 'undefined' && typeof thetokens.getStopIndex !== 'undefined'){
  344. this.start = thetokens.getStartIndex();
  345. this.end = thetokens.getStopIndex();
  346. isSet = true;
  347. }
  348. else if(isSet === false) {
  349. var type = Object.prototype.toString.call(thetokens);//.match(/^\[object (.*)\]$/)[1];
  350. console.warn('unknown argument type: '+type);//debug
  351. }
  352. }
  353. if(isSet === false) {
  354. this.start = -1;
  355. this.end = -1;
  356. }
  357. };
  358. /**
  359. * Set the start position (index) for this parsed element with regard to the TokenStream of the complete input.
  360. *
  361. * @function
  362. * @param {org.antlr.runtime.CommonTokenStream} thetokens (optional) the TokenStream that corresponds to this parsed element;
  363. * when provided, the TokenStream is used to set the start-property of this object.
  364. *
  365. * @public
  366. * @memberOf mmir.parser.ParsingResult
  367. */
  368. ParsingResult.prototype.setStartFrom = function(thetokens){
  369. //NOTE: must invoke getTokens() for initializing size() etc.!
  370. if(thetokens.getTokens() && thetokens.size() > 0){
  371. this.start = thetokens.getTokens()[0].getStartIndex();
  372. }
  373. else {
  374. this.start = -1;
  375. }
  376. };
  377. /**
  378. * Set the end position (index) for this parsed element with regard to the TokenStream of the complete input.
  379. *
  380. * @function
  381. * @param {org.antlr.runtime.CommonTokenStream} thetokens (optional) the TokenStream that corresponds to this parsed element;
  382. * when provided, the TokenStream is used to set the end-property of this object.
  383. *
  384. * @public
  385. * @memberOf mmir.parser.ParsingResult
  386. */
  387. ParsingResult.prototype.setEndFrom = function(thetokens){
  388. //NOTE: must invoke getTokens() for initializing size() etc.!
  389. if(thetokens.getTokens() && thetokens.size() > 0){
  390. this.end = thetokens.getTokens()[thetokens.size()-1].getStopIndex();
  391. }
  392. else {
  393. this.end = -1;
  394. }
  395. };
  396. /** @memberOf mmir.parser.ParsingResult */
  397. ParsingResult.prototype.getStart = function(){
  398. return this.start;
  399. };
  400. /** @memberOf mmir.parser.ParsingResult */
  401. ParsingResult.prototype.getEnd = function(){
  402. return this.end;
  403. };
  404. /**
  405. * Get the type of this parsed element, i.e. as which type this element was parsed.
  406. *
  407. * The type corresponds to one of the type defined in {mmir.parser.Element}.
  408. *
  409. * @function
  410. * @return {mmir.parser.Element} the type for this ParsingResult
  411. *
  412. * @public
  413. * @memberOf mmir.parser.ParsingResult
  414. */
  415. ParsingResult.prototype.getType = function(){
  416. return this.type;
  417. };
  418. /**
  419. * helper function for converting properties to the correct value.
  420. * By default, the ParsingResult only contains "raw" property values.
  421. * Which properties are available, depends on the type of the ParsingResult (see templateProcessor.js)
  422. *
  423. * @memberOf mmir.parser.ParsingResult
  424. */
  425. ParsingResult.prototype.getValue = function(rawPropertyValue, proptertyType, data){
  426. if(proptertyType === 'StringLiteral'){
  427. return rawPropertyValue.substring(1, rawPropertyValue.length-1);
  428. }
  429. else if(proptertyType === 'Identifier'){
  430. if(data){
  431. return data['@' + rawPropertyValue];
  432. }
  433. else {
  434. //just return variable name
  435. return rawPropertyValue;
  436. }
  437. }
  438. else if(proptertyType === 'IdentifierNameAmpersatStart'){
  439. if(data){
  440. return data[rawPropertyValue];
  441. }
  442. else {
  443. //just return variable name, but remove leading @:
  444. return rawPropertyValue.substring(1);
  445. }
  446. }
  447. else if(proptertyType === 'OBJECT'){
  448. return rawPropertyValue;//TODO
  449. }
  450. else if(proptertyType === 'DecimalLiteral'){
  451. return parseFloat(rawPropertyValue);
  452. }
  453. // else if(typeof proptertyType === 'undefined'){
  454. // return rawPropertyValue;
  455. // }
  456. else
  457. return rawPropertyValue;
  458. };
  459. /** @memberOf mmir.parser.ParsingResult */
  460. ParsingResult.prototype.hasVarReferences = function(){
  461. return false;//TODO implement this
  462. };
  463. /** @memberOf mmir.parser.ParsingResult */
  464. ParsingResult.prototype.isScriptTag = function(){
  465. if( parser.element.INCLUDE_SCRIPT === this.getType() ){
  466. return true;
  467. }
  468. return false;
  469. };
  470. /** @memberOf mmir.parser.ParsingResult */
  471. ParsingResult.prototype.isStyleTag = function(){
  472. if( parser.element.INCLUDE_STYLE === this.getType() ){
  473. return true;
  474. }
  475. return false;
  476. };
  477. /** @memberOf mmir.parser.ParsingResult */
  478. ParsingResult.prototype.isLocalize = function(){
  479. if( parser.element.LOCALIZE === this.getType() ){
  480. return true;
  481. }
  482. return false;
  483. };
  484. /** @memberOf mmir.parser.ParsingResult */
  485. ParsingResult.prototype.isYield = function(){
  486. if( parser.element.YIELD_DECLARATION === this.getType() ){
  487. return true;
  488. }
  489. return false;
  490. };
  491. /** @memberOf mmir.parser.ParsingResult */
  492. ParsingResult.prototype.isYieldContent = function(){
  493. if( parser.element.YIELD_CONTENT === this.getType() ){
  494. return true;
  495. }
  496. return false;
  497. };
  498. /** @memberOf mmir.parser.ParsingResult */
  499. ParsingResult.prototype.isScriptBlock = function(){
  500. if( parser.element.BLOCK === this.getType() ){
  501. return true;
  502. }
  503. return false;
  504. };
  505. /** @memberOf mmir.parser.ParsingResult */
  506. ParsingResult.prototype.isScriptStatement = function(){
  507. if( parser.element.STATEMENT === this.getType() ){
  508. return true;
  509. }
  510. return false;
  511. };
  512. /** @memberOf mmir.parser.ParsingResult */
  513. ParsingResult.prototype.isHelper = function(){
  514. if( parser.element.HELPER === this.getType() ){
  515. return true;
  516. }
  517. return false;
  518. };
  519. /** @memberOf mmir.parser.ParsingResult */
  520. ParsingResult.prototype.isIf = function(){
  521. if( parser.element.IF === this.getType() ){
  522. return true;
  523. }
  524. return false;
  525. };
  526. /** @memberOf mmir.parser.ParsingResult */
  527. ParsingResult.prototype.hasElse = function(){
  528. if(this.isIf() && typeof this.elseContent != 'undefined'){
  529. return true;
  530. }
  531. return false;
  532. };
  533. /** @memberOf mmir.parser.ParsingResult */
  534. ParsingResult.prototype.isElse = function(){
  535. if( parser.element.ELSE === this.getType() ){
  536. return true;
  537. }
  538. return false;
  539. };
  540. /** @memberOf mmir.parser.ParsingResult */
  541. ParsingResult.prototype.isFor = function(){
  542. if( parser.element.FOR === this.getType() ){
  543. return true;
  544. }
  545. return false;
  546. };
  547. /** @memberOf mmir.parser.ParsingResult */
  548. ParsingResult.prototype.isRender = function(){
  549. if( parser.element.RENDER === this.getType() ){
  550. return true;
  551. }
  552. return false;
  553. };
  554. /** @memberOf mmir.parser.ParsingResult */
  555. ParsingResult.prototype.isEscapeEnter = function(){
  556. if( parser.element.ESCAPE_ENTER === this.getType() ){
  557. return true;
  558. }
  559. return false;
  560. };
  561. /** @memberOf mmir.parser.ParsingResult */
  562. ParsingResult.prototype.isEscapeExit = function(){
  563. if( parser.element.ESCAPE_EXIT === this.getType() ){
  564. return true;
  565. }
  566. return false;
  567. };
  568. /** @memberOf mmir.parser.ParsingResult */
  569. ParsingResult.prototype.isEscape = function(){
  570. return this.isEscapeEnter() || this.isEscapeExit();
  571. };
  572. /**
  573. * WARNING: do use sparingly -- an invocation triggers a list evaluation.
  574. *
  575. * @returns {String} a String representation (name) for this ParsingResult's type
  576. *
  577. * @memberOf mmir.parser.ParsingResult
  578. */
  579. ParsingResult.prototype.getTypeName = function(){
  580. if(this.typeName){
  581. return this.typeName;/////////////////// EARLY EXIT //////////////////////////
  582. }
  583. for(var prop in parser.element){
  584. if(parser.element.hasOwnProperty(prop) && parser.element[prop] === this.getType()){
  585. this.typeName = prop;
  586. return prop;/////////////////// EARLY EXIT //////////////////////////
  587. }
  588. }
  589. return void(0);
  590. };
  591. /** @memberOf mmir.parser.ParsingResult */
  592. ParsingResult.prototype.hasCallData = function(){
  593. return typeof this.dataPos !== 'undefined';
  594. };
  595. /** @memberOf mmir.parser.ParsingResult */
  596. ParsingResult.prototype.getCallDataStart = function(){
  597. return this.dataPos.start;
  598. };
  599. /** @memberOf mmir.parser.ParsingResult */
  600. ParsingResult.prototype.getCallDataEnd = function(){
  601. return this.dataPos.end;
  602. };
  603. /** @memberOf mmir.parser.ParsingResult */
  604. ParsingResult.prototype.getCallDataType = function(){
  605. return this.dataType;
  606. };
  607. /** @memberOf mmir.parser.ParsingResult */
  608. ParsingResult.prototype.stringify = function(disableStrictMode){
  609. //TODO use constants for lists
  610. //typed properties:
  611. // for each typed properties '<prop>' there is an additional property with name '<prop>Type'
  612. // (both of these properties are Strings themselves)
  613. var typedPropList = [
  614. 'scriptPath',
  615. 'stylePath',
  616. 'name',
  617. 'helper',
  618. 'partial',
  619. 'controller'
  620. ];
  621. //primitive-type properties:
  622. // write values 'as is' for these properties
  623. var propList = [
  624. 'start',
  625. 'end',
  626. 'type',
  627. 'dataType',
  628. 'text',
  629. 'forControlType',
  630. 'forPropName'
  631. ];
  632. //"stringify-able" object properties:
  633. var strPropList = [
  634. 'content',
  635. 'elseContent'
  636. ];
  637. //function properties:
  638. var funcPropList = [
  639. //DISABLED these are now generated/initialized in the owning ContentElment's initEvalFunctions() function (NOTE that this may include the forInitEval too, which then will overwrite this function, that may be exported due to this list)
  640. // 'scriptEval',
  641. // 'argsEval',
  642. // 'ifEval',
  643. // 'forConditionEval',
  644. // 'forIncrementEval',
  645. 'forInitEval',//TODO include this in ContentElement.initEvalFunctions
  646. 'forIterator'//TODO include this in ContentElement.initEvalFunctions
  647. ];
  648. //default function properties:
  649. // these functions from the prototype may have been overwritten
  650. // -> if one is overwritten, i.e. not the default implementation, then store that one too
  651. var overwrittenFuncPropList = [
  652. 'getEnd',
  653. 'getStart',
  654. 'getType',
  655. 'getValue',
  656. 'getTypeName',
  657. 'getCallDataStart',
  658. 'getCallDataEnd',
  659. 'getCallDataType'
  660. ];
  661. //function for iterating over the property-list and generating JSON-like entries in the string-buffer
  662. var appendStringified = parser.appendStringified;
  663. var sb = ['require("mmirf/storageUtils").restoreObject({ classConstructor: "mmirf/parsingResult"', ','];
  664. //TODO property dataPos: {start: Number, end: Number}
  665. if(this['dataPos']){
  666. sb.push( 'dataPos:{start:' );
  667. sb.push( this.dataPos.start );
  668. sb.push( ',end:' );
  669. sb.push( this.dataPos.end );
  670. sb.push( '}' );
  671. //NOTE: need to add comma in a separate entry
  672. // (-> in order to not break the removal method of last comma, see below)
  673. sb.push( ',' );
  674. }
  675. appendStringified(this, typedPropList, sb);
  676. appendStringified(this, typedPropList, sb, 'Type');
  677. appendStringified(this, propList, sb);
  678. //non-primitives with stringify() function:
  679. appendStringified(this, strPropList, sb, null, function stringifyableExtractor(name, value){
  680. return value.stringify(disableStrictMode);
  681. });
  682. //function definitions
  683. appendStringified(this, funcPropList, sb, null, function functionExtractor(name, value){
  684. return value.toString();
  685. });
  686. //add "overwritten" function definitions
  687. appendStringified(this, overwrittenFuncPropList, sb, null, function nonDefaultFunctionExtractor(name, value){
  688. var instanceImpl = value.toString();
  689. var defaultImpl = ParsingResult.prototype[name].toString();
  690. if(instanceImpl !== defaultImpl){
  691. return instanceImpl;
  692. }
  693. //DEFAULT: return void (will omit this from storage -> use default impl. of the prototype)
  694. return;
  695. });
  696. //if last element is a comma, remove it
  697. if(sb[sb.length - 1] === ','){
  698. sb.splice( sb.length - 1, 1);
  699. }
  700. sb.push(' })');
  701. return sb.join('');
  702. };
  703. /**
  704. * HELPER for making the namespace of the core-parser (i.e. ANTLR3) available
  705. *
  706. * This is necessary in environments where the namespace is not automatically
  707. * exported into the global namespace, e.g. when runnin in nodejs.
  708. *
  709. * @static
  710. * @private
  711. * @param {String} parserNamerspace
  712. * the namespace object of the ANTLR parser (should be the object org which is exported by antlr3 module/shim)
  713. */
  714. ParsingResult._nsParserInit = function(parserNamerspace){
  715. _antrl3 = parserNamerspace;
  716. if(typeof org === 'undefined'){
  717. org = parserNamerspace;
  718. }
  719. };
  720. parser.ParsingResult = ParsingResult;
  721. return parser.ParsingResult;
  722. });//END: define(..., function(){