grammar.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  1. // Generated by CoffeeScript 1.12.6
  2. (function() {
  3. var Parser, alt, alternatives, grammar, name, o, operators, token, tokens, unwrap;
  4. Parser = require('jison').Parser;
  5. unwrap = /^function\s*\(\)\s*\{\s*return\s*([\s\S]*);\s*\}/;
  6. o = function(patternString, action, options) {
  7. var addLocationDataFn, match, patternCount;
  8. patternString = patternString.replace(/\s{2,}/g, ' ');
  9. patternCount = patternString.split(' ').length;
  10. if (!action) {
  11. return [patternString, '$$ = $1;', options];
  12. }
  13. action = (match = unwrap.exec(action)) ? match[1] : "(" + action + "())";
  14. action = action.replace(/\bnew /g, '$&yy.');
  15. action = action.replace(/\b(?:Block\.wrap|extend)\b/g, 'yy.$&');
  16. addLocationDataFn = function(first, last) {
  17. if (!last) {
  18. return "yy.addLocationDataFn(@" + first + ")";
  19. } else {
  20. return "yy.addLocationDataFn(@" + first + ", @" + last + ")";
  21. }
  22. };
  23. action = action.replace(/LOC\(([0-9]*)\)/g, addLocationDataFn('$1'));
  24. action = action.replace(/LOC\(([0-9]*),\s*([0-9]*)\)/g, addLocationDataFn('$1', '$2'));
  25. return [patternString, "$$ = " + (addLocationDataFn(1, patternCount)) + "(" + action + ");", options];
  26. };
  27. grammar = {
  28. Root: [
  29. o('', function() {
  30. return new Block;
  31. }), o('Body')
  32. ],
  33. Body: [
  34. o('Line', function() {
  35. return Block.wrap([$1]);
  36. }), o('Body TERMINATOR Line', function() {
  37. return $1.push($3);
  38. }), o('Body TERMINATOR')
  39. ],
  40. Line: [o('Expression'), o('Statement'), o('YieldReturn')],
  41. Statement: [
  42. o('Return'), o('Comment'), o('STATEMENT', function() {
  43. return new StatementLiteral($1);
  44. }), o('Import'), o('Export')
  45. ],
  46. Expression: [o('Value'), o('Invocation'), o('Code'), o('Operation'), o('Assign'), o('If'), o('Try'), o('While'), o('For'), o('Switch'), o('Class'), o('Throw'), o('Yield')],
  47. Yield: [
  48. o('YIELD', function() {
  49. return new Op($1, new Value(new Literal('')));
  50. }), o('YIELD Expression', function() {
  51. return new Op($1, $2);
  52. }), o('YIELD FROM Expression', function() {
  53. return new Op($1.concat($2), $3);
  54. })
  55. ],
  56. Block: [
  57. o('INDENT OUTDENT', function() {
  58. return new Block;
  59. }), o('INDENT Body OUTDENT', function() {
  60. return $2;
  61. })
  62. ],
  63. Identifier: [
  64. o('IDENTIFIER', function() {
  65. return new IdentifierLiteral($1);
  66. })
  67. ],
  68. Property: [
  69. o('PROPERTY', function() {
  70. return new PropertyName($1);
  71. })
  72. ],
  73. AlphaNumeric: [
  74. o('NUMBER', function() {
  75. return new NumberLiteral($1);
  76. }), o('String')
  77. ],
  78. String: [
  79. o('STRING', function() {
  80. return new StringLiteral($1);
  81. }), o('STRING_START Body STRING_END', function() {
  82. return new StringWithInterpolations($2);
  83. })
  84. ],
  85. Regex: [
  86. o('REGEX', function() {
  87. return new RegexLiteral($1);
  88. }), o('REGEX_START Invocation REGEX_END', function() {
  89. return new RegexWithInterpolations($2.args);
  90. })
  91. ],
  92. Literal: [
  93. o('AlphaNumeric'), o('JS', function() {
  94. return new PassthroughLiteral($1);
  95. }), o('Regex'), o('UNDEFINED', function() {
  96. return new UndefinedLiteral;
  97. }), o('NULL', function() {
  98. return new NullLiteral;
  99. }), o('BOOL', function() {
  100. return new BooleanLiteral($1);
  101. }), o('INFINITY', function() {
  102. return new InfinityLiteral($1);
  103. }), o('NAN', function() {
  104. return new NaNLiteral;
  105. })
  106. ],
  107. Assign: [
  108. o('Assignable = Expression', function() {
  109. return new Assign($1, $3);
  110. }), o('Assignable = TERMINATOR Expression', function() {
  111. return new Assign($1, $4);
  112. }), o('Assignable = INDENT Expression OUTDENT', function() {
  113. return new Assign($1, $4);
  114. })
  115. ],
  116. AssignObj: [
  117. o('ObjAssignable', function() {
  118. return new Value($1);
  119. }), o('ObjAssignable : Expression', function() {
  120. return new Assign(LOC(1)(new Value($1)), $3, 'object', {
  121. operatorToken: LOC(2)(new Literal($2))
  122. });
  123. }), o('ObjAssignable : INDENT Expression OUTDENT', function() {
  124. return new Assign(LOC(1)(new Value($1)), $4, 'object', {
  125. operatorToken: LOC(2)(new Literal($2))
  126. });
  127. }), o('SimpleObjAssignable = Expression', function() {
  128. return new Assign(LOC(1)(new Value($1)), $3, null, {
  129. operatorToken: LOC(2)(new Literal($2))
  130. });
  131. }), o('SimpleObjAssignable = INDENT Expression OUTDENT', function() {
  132. return new Assign(LOC(1)(new Value($1)), $4, null, {
  133. operatorToken: LOC(2)(new Literal($2))
  134. });
  135. }), o('Comment')
  136. ],
  137. SimpleObjAssignable: [o('Identifier'), o('Property'), o('ThisProperty')],
  138. ObjAssignable: [o('SimpleObjAssignable'), o('AlphaNumeric')],
  139. Return: [
  140. o('RETURN Expression', function() {
  141. return new Return($2);
  142. }), o('RETURN', function() {
  143. return new Return;
  144. })
  145. ],
  146. YieldReturn: [
  147. o('YIELD RETURN Expression', function() {
  148. return new YieldReturn($3);
  149. }), o('YIELD RETURN', function() {
  150. return new YieldReturn;
  151. })
  152. ],
  153. Comment: [
  154. o('HERECOMMENT', function() {
  155. return new Comment($1);
  156. })
  157. ],
  158. Code: [
  159. o('PARAM_START ParamList PARAM_END FuncGlyph Block', function() {
  160. return new Code($2, $5, $4);
  161. }), o('FuncGlyph Block', function() {
  162. return new Code([], $2, $1);
  163. })
  164. ],
  165. FuncGlyph: [
  166. o('->', function() {
  167. return 'func';
  168. }), o('=>', function() {
  169. return 'boundfunc';
  170. })
  171. ],
  172. OptComma: [o(''), o(',')],
  173. ParamList: [
  174. o('', function() {
  175. return [];
  176. }), o('Param', function() {
  177. return [$1];
  178. }), o('ParamList , Param', function() {
  179. return $1.concat($3);
  180. }), o('ParamList OptComma TERMINATOR Param', function() {
  181. return $1.concat($4);
  182. }), o('ParamList OptComma INDENT ParamList OptComma OUTDENT', function() {
  183. return $1.concat($4);
  184. })
  185. ],
  186. Param: [
  187. o('ParamVar', function() {
  188. return new Param($1);
  189. }), o('ParamVar ...', function() {
  190. return new Param($1, null, true);
  191. }), o('ParamVar = Expression', function() {
  192. return new Param($1, $3);
  193. }), o('...', function() {
  194. return new Expansion;
  195. })
  196. ],
  197. ParamVar: [o('Identifier'), o('ThisProperty'), o('Array'), o('Object')],
  198. Splat: [
  199. o('Expression ...', function() {
  200. return new Splat($1);
  201. })
  202. ],
  203. SimpleAssignable: [
  204. o('Identifier', function() {
  205. return new Value($1);
  206. }), o('Value Accessor', function() {
  207. return $1.add($2);
  208. }), o('Invocation Accessor', function() {
  209. return new Value($1, [].concat($2));
  210. }), o('ThisProperty')
  211. ],
  212. Assignable: [
  213. o('SimpleAssignable'), o('Array', function() {
  214. return new Value($1);
  215. }), o('Object', function() {
  216. return new Value($1);
  217. })
  218. ],
  219. Value: [
  220. o('Assignable'), o('Literal', function() {
  221. return new Value($1);
  222. }), o('Parenthetical', function() {
  223. return new Value($1);
  224. }), o('Range', function() {
  225. return new Value($1);
  226. }), o('This')
  227. ],
  228. Accessor: [
  229. o('. Property', function() {
  230. return new Access($2);
  231. }), o('?. Property', function() {
  232. return new Access($2, 'soak');
  233. }), o(':: Property', function() {
  234. return [LOC(1)(new Access(new PropertyName('prototype'))), LOC(2)(new Access($2))];
  235. }), o('?:: Property', function() {
  236. return [LOC(1)(new Access(new PropertyName('prototype'), 'soak')), LOC(2)(new Access($2))];
  237. }), o('::', function() {
  238. return new Access(new PropertyName('prototype'));
  239. }), o('Index')
  240. ],
  241. Index: [
  242. o('INDEX_START IndexValue INDEX_END', function() {
  243. return $2;
  244. }), o('INDEX_SOAK Index', function() {
  245. return extend($2, {
  246. soak: true
  247. });
  248. })
  249. ],
  250. IndexValue: [
  251. o('Expression', function() {
  252. return new Index($1);
  253. }), o('Slice', function() {
  254. return new Slice($1);
  255. })
  256. ],
  257. Object: [
  258. o('{ AssignList OptComma }', function() {
  259. return new Obj($2, $1.generated);
  260. })
  261. ],
  262. AssignList: [
  263. o('', function() {
  264. return [];
  265. }), o('AssignObj', function() {
  266. return [$1];
  267. }), o('AssignList , AssignObj', function() {
  268. return $1.concat($3);
  269. }), o('AssignList OptComma TERMINATOR AssignObj', function() {
  270. return $1.concat($4);
  271. }), o('AssignList OptComma INDENT AssignList OptComma OUTDENT', function() {
  272. return $1.concat($4);
  273. })
  274. ],
  275. Class: [
  276. o('CLASS', function() {
  277. return new Class;
  278. }), o('CLASS Block', function() {
  279. return new Class(null, null, $2);
  280. }), o('CLASS EXTENDS Expression', function() {
  281. return new Class(null, $3);
  282. }), o('CLASS EXTENDS Expression Block', function() {
  283. return new Class(null, $3, $4);
  284. }), o('CLASS SimpleAssignable', function() {
  285. return new Class($2);
  286. }), o('CLASS SimpleAssignable Block', function() {
  287. return new Class($2, null, $3);
  288. }), o('CLASS SimpleAssignable EXTENDS Expression', function() {
  289. return new Class($2, $4);
  290. }), o('CLASS SimpleAssignable EXTENDS Expression Block', function() {
  291. return new Class($2, $4, $5);
  292. })
  293. ],
  294. Import: [
  295. o('IMPORT String', function() {
  296. return new ImportDeclaration(null, $2);
  297. }), o('IMPORT ImportDefaultSpecifier FROM String', function() {
  298. return new ImportDeclaration(new ImportClause($2, null), $4);
  299. }), o('IMPORT ImportNamespaceSpecifier FROM String', function() {
  300. return new ImportDeclaration(new ImportClause(null, $2), $4);
  301. }), o('IMPORT { } FROM String', function() {
  302. return new ImportDeclaration(new ImportClause(null, new ImportSpecifierList([])), $5);
  303. }), o('IMPORT { ImportSpecifierList OptComma } FROM String', function() {
  304. return new ImportDeclaration(new ImportClause(null, new ImportSpecifierList($3)), $7);
  305. }), o('IMPORT ImportDefaultSpecifier , ImportNamespaceSpecifier FROM String', function() {
  306. return new ImportDeclaration(new ImportClause($2, $4), $6);
  307. }), o('IMPORT ImportDefaultSpecifier , { ImportSpecifierList OptComma } FROM String', function() {
  308. return new ImportDeclaration(new ImportClause($2, new ImportSpecifierList($5)), $9);
  309. })
  310. ],
  311. ImportSpecifierList: [
  312. o('ImportSpecifier', function() {
  313. return [$1];
  314. }), o('ImportSpecifierList , ImportSpecifier', function() {
  315. return $1.concat($3);
  316. }), o('ImportSpecifierList OptComma TERMINATOR ImportSpecifier', function() {
  317. return $1.concat($4);
  318. }), o('INDENT ImportSpecifierList OptComma OUTDENT', function() {
  319. return $2;
  320. }), o('ImportSpecifierList OptComma INDENT ImportSpecifierList OptComma OUTDENT', function() {
  321. return $1.concat($4);
  322. })
  323. ],
  324. ImportSpecifier: [
  325. o('Identifier', function() {
  326. return new ImportSpecifier($1);
  327. }), o('Identifier AS Identifier', function() {
  328. return new ImportSpecifier($1, $3);
  329. }), o('DEFAULT', function() {
  330. return new ImportSpecifier(new Literal($1));
  331. }), o('DEFAULT AS Identifier', function() {
  332. return new ImportSpecifier(new Literal($1), $3);
  333. })
  334. ],
  335. ImportDefaultSpecifier: [
  336. o('Identifier', function() {
  337. return new ImportDefaultSpecifier($1);
  338. })
  339. ],
  340. ImportNamespaceSpecifier: [
  341. o('IMPORT_ALL AS Identifier', function() {
  342. return new ImportNamespaceSpecifier(new Literal($1), $3);
  343. })
  344. ],
  345. Export: [
  346. o('EXPORT { }', function() {
  347. return new ExportNamedDeclaration(new ExportSpecifierList([]));
  348. }), o('EXPORT { ExportSpecifierList OptComma }', function() {
  349. return new ExportNamedDeclaration(new ExportSpecifierList($3));
  350. }), o('EXPORT Class', function() {
  351. return new ExportNamedDeclaration($2);
  352. }), o('EXPORT Identifier = Expression', function() {
  353. return new ExportNamedDeclaration(new Assign($2, $4, null, {
  354. moduleDeclaration: 'export'
  355. }));
  356. }), o('EXPORT Identifier = TERMINATOR Expression', function() {
  357. return new ExportNamedDeclaration(new Assign($2, $5, null, {
  358. moduleDeclaration: 'export'
  359. }));
  360. }), o('EXPORT Identifier = INDENT Expression OUTDENT', function() {
  361. return new ExportNamedDeclaration(new Assign($2, $5, null, {
  362. moduleDeclaration: 'export'
  363. }));
  364. }), o('EXPORT DEFAULT Expression', function() {
  365. return new ExportDefaultDeclaration($3);
  366. }), o('EXPORT EXPORT_ALL FROM String', function() {
  367. return new ExportAllDeclaration(new Literal($2), $4);
  368. }), o('EXPORT { ExportSpecifierList OptComma } FROM String', function() {
  369. return new ExportNamedDeclaration(new ExportSpecifierList($3), $7);
  370. })
  371. ],
  372. ExportSpecifierList: [
  373. o('ExportSpecifier', function() {
  374. return [$1];
  375. }), o('ExportSpecifierList , ExportSpecifier', function() {
  376. return $1.concat($3);
  377. }), o('ExportSpecifierList OptComma TERMINATOR ExportSpecifier', function() {
  378. return $1.concat($4);
  379. }), o('INDENT ExportSpecifierList OptComma OUTDENT', function() {
  380. return $2;
  381. }), o('ExportSpecifierList OptComma INDENT ExportSpecifierList OptComma OUTDENT', function() {
  382. return $1.concat($4);
  383. })
  384. ],
  385. ExportSpecifier: [
  386. o('Identifier', function() {
  387. return new ExportSpecifier($1);
  388. }), o('Identifier AS Identifier', function() {
  389. return new ExportSpecifier($1, $3);
  390. }), o('Identifier AS DEFAULT', function() {
  391. return new ExportSpecifier($1, new Literal($3));
  392. }), o('DEFAULT', function() {
  393. return new ExportSpecifier(new Literal($1));
  394. }), o('DEFAULT AS Identifier', function() {
  395. return new ExportSpecifier(new Literal($1), $3);
  396. })
  397. ],
  398. Invocation: [
  399. o('Value OptFuncExist String', function() {
  400. return new TaggedTemplateCall($1, $3, $2);
  401. }), o('Value OptFuncExist Arguments', function() {
  402. return new Call($1, $3, $2);
  403. }), o('Invocation OptFuncExist Arguments', function() {
  404. return new Call($1, $3, $2);
  405. }), o('Super')
  406. ],
  407. Super: [
  408. o('SUPER', function() {
  409. return new SuperCall;
  410. }), o('SUPER Arguments', function() {
  411. return new SuperCall($2);
  412. })
  413. ],
  414. OptFuncExist: [
  415. o('', function() {
  416. return false;
  417. }), o('FUNC_EXIST', function() {
  418. return true;
  419. })
  420. ],
  421. Arguments: [
  422. o('CALL_START CALL_END', function() {
  423. return [];
  424. }), o('CALL_START ArgList OptComma CALL_END', function() {
  425. return $2;
  426. })
  427. ],
  428. This: [
  429. o('THIS', function() {
  430. return new Value(new ThisLiteral);
  431. }), o('@', function() {
  432. return new Value(new ThisLiteral);
  433. })
  434. ],
  435. ThisProperty: [
  436. o('@ Property', function() {
  437. return new Value(LOC(1)(new ThisLiteral), [LOC(2)(new Access($2))], 'this');
  438. })
  439. ],
  440. Array: [
  441. o('[ ]', function() {
  442. return new Arr([]);
  443. }), o('[ ArgList OptComma ]', function() {
  444. return new Arr($2);
  445. })
  446. ],
  447. RangeDots: [
  448. o('..', function() {
  449. return 'inclusive';
  450. }), o('...', function() {
  451. return 'exclusive';
  452. })
  453. ],
  454. Range: [
  455. o('[ Expression RangeDots Expression ]', function() {
  456. return new Range($2, $4, $3);
  457. })
  458. ],
  459. Slice: [
  460. o('Expression RangeDots Expression', function() {
  461. return new Range($1, $3, $2);
  462. }), o('Expression RangeDots', function() {
  463. return new Range($1, null, $2);
  464. }), o('RangeDots Expression', function() {
  465. return new Range(null, $2, $1);
  466. }), o('RangeDots', function() {
  467. return new Range(null, null, $1);
  468. })
  469. ],
  470. ArgList: [
  471. o('Arg', function() {
  472. return [$1];
  473. }), o('ArgList , Arg', function() {
  474. return $1.concat($3);
  475. }), o('ArgList OptComma TERMINATOR Arg', function() {
  476. return $1.concat($4);
  477. }), o('INDENT ArgList OptComma OUTDENT', function() {
  478. return $2;
  479. }), o('ArgList OptComma INDENT ArgList OptComma OUTDENT', function() {
  480. return $1.concat($4);
  481. })
  482. ],
  483. Arg: [
  484. o('Expression'), o('Splat'), o('...', function() {
  485. return new Expansion;
  486. })
  487. ],
  488. SimpleArgs: [
  489. o('Expression'), o('SimpleArgs , Expression', function() {
  490. return [].concat($1, $3);
  491. })
  492. ],
  493. Try: [
  494. o('TRY Block', function() {
  495. return new Try($2);
  496. }), o('TRY Block Catch', function() {
  497. return new Try($2, $3[0], $3[1]);
  498. }), o('TRY Block FINALLY Block', function() {
  499. return new Try($2, null, null, $4);
  500. }), o('TRY Block Catch FINALLY Block', function() {
  501. return new Try($2, $3[0], $3[1], $5);
  502. })
  503. ],
  504. Catch: [
  505. o('CATCH Identifier Block', function() {
  506. return [$2, $3];
  507. }), o('CATCH Object Block', function() {
  508. return [LOC(2)(new Value($2)), $3];
  509. }), o('CATCH Block', function() {
  510. return [null, $2];
  511. })
  512. ],
  513. Throw: [
  514. o('THROW Expression', function() {
  515. return new Throw($2);
  516. })
  517. ],
  518. Parenthetical: [
  519. o('( Body )', function() {
  520. return new Parens($2);
  521. }), o('( INDENT Body OUTDENT )', function() {
  522. return new Parens($3);
  523. })
  524. ],
  525. WhileSource: [
  526. o('WHILE Expression', function() {
  527. return new While($2);
  528. }), o('WHILE Expression WHEN Expression', function() {
  529. return new While($2, {
  530. guard: $4
  531. });
  532. }), o('UNTIL Expression', function() {
  533. return new While($2, {
  534. invert: true
  535. });
  536. }), o('UNTIL Expression WHEN Expression', function() {
  537. return new While($2, {
  538. invert: true,
  539. guard: $4
  540. });
  541. })
  542. ],
  543. While: [
  544. o('WhileSource Block', function() {
  545. return $1.addBody($2);
  546. }), o('Statement WhileSource', function() {
  547. return $2.addBody(LOC(1)(Block.wrap([$1])));
  548. }), o('Expression WhileSource', function() {
  549. return $2.addBody(LOC(1)(Block.wrap([$1])));
  550. }), o('Loop', function() {
  551. return $1;
  552. })
  553. ],
  554. Loop: [
  555. o('LOOP Block', function() {
  556. return new While(LOC(1)(new BooleanLiteral('true'))).addBody($2);
  557. }), o('LOOP Expression', function() {
  558. return new While(LOC(1)(new BooleanLiteral('true'))).addBody(LOC(2)(Block.wrap([$2])));
  559. })
  560. ],
  561. For: [
  562. o('Statement ForBody', function() {
  563. return new For($1, $2);
  564. }), o('Expression ForBody', function() {
  565. return new For($1, $2);
  566. }), o('ForBody Block', function() {
  567. return new For($2, $1);
  568. })
  569. ],
  570. ForBody: [
  571. o('FOR Range', function() {
  572. return {
  573. source: LOC(2)(new Value($2))
  574. };
  575. }), o('FOR Range BY Expression', function() {
  576. return {
  577. source: LOC(2)(new Value($2)),
  578. step: $4
  579. };
  580. }), o('ForStart ForSource', function() {
  581. $2.own = $1.own;
  582. $2.ownTag = $1.ownTag;
  583. $2.name = $1[0];
  584. $2.index = $1[1];
  585. return $2;
  586. })
  587. ],
  588. ForStart: [
  589. o('FOR ForVariables', function() {
  590. return $2;
  591. }), o('FOR OWN ForVariables', function() {
  592. $3.own = true;
  593. $3.ownTag = LOC(2)(new Literal($2));
  594. return $3;
  595. })
  596. ],
  597. ForValue: [
  598. o('Identifier'), o('ThisProperty'), o('Array', function() {
  599. return new Value($1);
  600. }), o('Object', function() {
  601. return new Value($1);
  602. })
  603. ],
  604. ForVariables: [
  605. o('ForValue', function() {
  606. return [$1];
  607. }), o('ForValue , ForValue', function() {
  608. return [$1, $3];
  609. })
  610. ],
  611. ForSource: [
  612. o('FORIN Expression', function() {
  613. return {
  614. source: $2
  615. };
  616. }), o('FOROF Expression', function() {
  617. return {
  618. source: $2,
  619. object: true
  620. };
  621. }), o('FORIN Expression WHEN Expression', function() {
  622. return {
  623. source: $2,
  624. guard: $4
  625. };
  626. }), o('FOROF Expression WHEN Expression', function() {
  627. return {
  628. source: $2,
  629. guard: $4,
  630. object: true
  631. };
  632. }), o('FORIN Expression BY Expression', function() {
  633. return {
  634. source: $2,
  635. step: $4
  636. };
  637. }), o('FORIN Expression WHEN Expression BY Expression', function() {
  638. return {
  639. source: $2,
  640. guard: $4,
  641. step: $6
  642. };
  643. }), o('FORIN Expression BY Expression WHEN Expression', function() {
  644. return {
  645. source: $2,
  646. step: $4,
  647. guard: $6
  648. };
  649. }), o('FORFROM Expression', function() {
  650. return {
  651. source: $2,
  652. from: true
  653. };
  654. }), o('FORFROM Expression WHEN Expression', function() {
  655. return {
  656. source: $2,
  657. guard: $4,
  658. from: true
  659. };
  660. })
  661. ],
  662. Switch: [
  663. o('SWITCH Expression INDENT Whens OUTDENT', function() {
  664. return new Switch($2, $4);
  665. }), o('SWITCH Expression INDENT Whens ELSE Block OUTDENT', function() {
  666. return new Switch($2, $4, $6);
  667. }), o('SWITCH INDENT Whens OUTDENT', function() {
  668. return new Switch(null, $3);
  669. }), o('SWITCH INDENT Whens ELSE Block OUTDENT', function() {
  670. return new Switch(null, $3, $5);
  671. })
  672. ],
  673. Whens: [
  674. o('When'), o('Whens When', function() {
  675. return $1.concat($2);
  676. })
  677. ],
  678. When: [
  679. o('LEADING_WHEN SimpleArgs Block', function() {
  680. return [[$2, $3]];
  681. }), o('LEADING_WHEN SimpleArgs Block TERMINATOR', function() {
  682. return [[$2, $3]];
  683. })
  684. ],
  685. IfBlock: [
  686. o('IF Expression Block', function() {
  687. return new If($2, $3, {
  688. type: $1
  689. });
  690. }), o('IfBlock ELSE IF Expression Block', function() {
  691. return $1.addElse(LOC(3, 5)(new If($4, $5, {
  692. type: $3
  693. })));
  694. })
  695. ],
  696. If: [
  697. o('IfBlock'), o('IfBlock ELSE Block', function() {
  698. return $1.addElse($3);
  699. }), o('Statement POST_IF Expression', function() {
  700. return new If($3, LOC(1)(Block.wrap([$1])), {
  701. type: $2,
  702. statement: true
  703. });
  704. }), o('Expression POST_IF Expression', function() {
  705. return new If($3, LOC(1)(Block.wrap([$1])), {
  706. type: $2,
  707. statement: true
  708. });
  709. })
  710. ],
  711. Operation: [
  712. o('UNARY Expression', function() {
  713. return new Op($1, $2);
  714. }), o('UNARY_MATH Expression', function() {
  715. return new Op($1, $2);
  716. }), o('- Expression', (function() {
  717. return new Op('-', $2);
  718. }), {
  719. prec: 'UNARY_MATH'
  720. }), o('+ Expression', (function() {
  721. return new Op('+', $2);
  722. }), {
  723. prec: 'UNARY_MATH'
  724. }), o('-- SimpleAssignable', function() {
  725. return new Op('--', $2);
  726. }), o('++ SimpleAssignable', function() {
  727. return new Op('++', $2);
  728. }), o('SimpleAssignable --', function() {
  729. return new Op('--', $1, null, true);
  730. }), o('SimpleAssignable ++', function() {
  731. return new Op('++', $1, null, true);
  732. }), o('Expression ?', function() {
  733. return new Existence($1);
  734. }), o('Expression + Expression', function() {
  735. return new Op('+', $1, $3);
  736. }), o('Expression - Expression', function() {
  737. return new Op('-', $1, $3);
  738. }), o('Expression MATH Expression', function() {
  739. return new Op($2, $1, $3);
  740. }), o('Expression ** Expression', function() {
  741. return new Op($2, $1, $3);
  742. }), o('Expression SHIFT Expression', function() {
  743. return new Op($2, $1, $3);
  744. }), o('Expression COMPARE Expression', function() {
  745. return new Op($2, $1, $3);
  746. }), o('Expression & Expression', function() {
  747. return new Op($2, $1, $3);
  748. }), o('Expression ^ Expression', function() {
  749. return new Op($2, $1, $3);
  750. }), o('Expression | Expression', function() {
  751. return new Op($2, $1, $3);
  752. }), o('Expression && Expression', function() {
  753. return new Op($2, $1, $3);
  754. }), o('Expression || Expression', function() {
  755. return new Op($2, $1, $3);
  756. }), o('Expression BIN? Expression', function() {
  757. return new Op($2, $1, $3);
  758. }), o('Expression RELATION Expression', function() {
  759. if ($2.charAt(0) === '!') {
  760. return new Op($2.slice(1), $1, $3).invert();
  761. } else {
  762. return new Op($2, $1, $3);
  763. }
  764. }), o('SimpleAssignable COMPOUND_ASSIGN Expression', function() {
  765. return new Assign($1, $3, $2);
  766. }), o('SimpleAssignable COMPOUND_ASSIGN INDENT Expression OUTDENT', function() {
  767. return new Assign($1, $4, $2);
  768. }), o('SimpleAssignable COMPOUND_ASSIGN TERMINATOR Expression', function() {
  769. return new Assign($1, $4, $2);
  770. }), o('SimpleAssignable EXTENDS Expression', function() {
  771. return new Extends($1, $3);
  772. })
  773. ]
  774. };
  775. operators = [['left', '.', '?.', '::', '?::'], ['left', 'CALL_START', 'CALL_END'], ['nonassoc', '++', '--'], ['left', '?'], ['right', 'UNARY'], ['right', '**'], ['right', 'UNARY_MATH'], ['left', 'MATH'], ['left', '+', '-'], ['left', 'SHIFT'], ['left', 'RELATION'], ['left', 'COMPARE'], ['left', '&'], ['left', '^'], ['left', '|'], ['left', '&&'], ['left', '||'], ['left', 'BIN?'], ['nonassoc', 'INDENT', 'OUTDENT'], ['right', 'YIELD'], ['right', '=', ':', 'COMPOUND_ASSIGN', 'RETURN', 'THROW', 'EXTENDS'], ['right', 'FORIN', 'FOROF', 'FORFROM', 'BY', 'WHEN'], ['right', 'IF', 'ELSE', 'FOR', 'WHILE', 'UNTIL', 'LOOP', 'SUPER', 'CLASS', 'IMPORT', 'EXPORT'], ['left', 'POST_IF']];
  776. tokens = [];
  777. for (name in grammar) {
  778. alternatives = grammar[name];
  779. grammar[name] = (function() {
  780. var i, j, len, len1, ref, results;
  781. results = [];
  782. for (i = 0, len = alternatives.length; i < len; i++) {
  783. alt = alternatives[i];
  784. ref = alt[0].split(' ');
  785. for (j = 0, len1 = ref.length; j < len1; j++) {
  786. token = ref[j];
  787. if (!grammar[token]) {
  788. tokens.push(token);
  789. }
  790. }
  791. if (name === 'Root') {
  792. alt[1] = "return " + alt[1];
  793. }
  794. results.push(alt);
  795. }
  796. return results;
  797. })();
  798. }
  799. exports.parser = new Parser({
  800. tokens: tokens.join(' '),
  801. bnf: grammar,
  802. operators: operators.reverse(),
  803. startSymbol: 'Root'
  804. });
  805. }).call(this);