grammar.js 27 KB

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