You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1755 lines
48 KiB

4 years ago
  1. function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
  2. function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
  3. import { codeFrameFromSource } from "@webassemblyjs/helper-code-frame";
  4. import * as t from "@webassemblyjs/ast";
  5. import { parse32I } from "./number-literals";
  6. import { parseString } from "./string-literals";
  7. import { tokens, keywords } from "./tokenizer";
  8. function hasPlugin(name) {
  9. if (name !== "wast") throw new Error("unknow plugin");
  10. return true;
  11. }
  12. function isKeyword(token, id) {
  13. return token.type === tokens.keyword && token.value === id;
  14. }
  15. function tokenToString(token) {
  16. if (token.type === "keyword") {
  17. return "keyword (".concat(token.value, ")");
  18. }
  19. return token.type;
  20. }
  21. function identifierFromToken(token) {
  22. var _token$loc = token.loc,
  23. end = _token$loc.end,
  24. start = _token$loc.start;
  25. return t.withLoc(t.identifier(token.value), end, start);
  26. }
  27. export function parse(tokensList, source) {
  28. var current = 0;
  29. var getUniqueName = t.getUniqueNameGenerator();
  30. var state = {
  31. registredExportedElements: []
  32. }; // But this time we're going to use recursion instead of a `while` loop. So we
  33. // define a `walk` function.
  34. function walk() {
  35. var token = tokensList[current];
  36. function eatToken() {
  37. token = tokensList[++current];
  38. }
  39. function getEndLoc() {
  40. var currentToken = token;
  41. if (typeof currentToken === "undefined") {
  42. var lastToken = tokensList[tokensList.length - 1];
  43. currentToken = lastToken;
  44. }
  45. return currentToken.loc.end;
  46. }
  47. function getStartLoc() {
  48. return token.loc.start;
  49. }
  50. function eatTokenOfType(type) {
  51. if (token.type !== type) {
  52. throw new Error("\n" + codeFrameFromSource(source, token.loc) + "Assertion error: expected token of type " + type + ", given " + tokenToString(token));
  53. }
  54. eatToken();
  55. }
  56. function parseExportIndex(token) {
  57. if (token.type === tokens.identifier) {
  58. var index = identifierFromToken(token);
  59. eatToken();
  60. return index;
  61. } else if (token.type === tokens.number) {
  62. var _index = t.numberLiteralFromRaw(token.value);
  63. eatToken();
  64. return _index;
  65. } else {
  66. throw function () {
  67. return new Error("\n" + codeFrameFromSource(source, token.loc) + "\n" + "unknown export index" + ", given " + tokenToString(token));
  68. }();
  69. }
  70. }
  71. function lookaheadAndCheck() {
  72. var len = arguments.length;
  73. for (var i = 0; i < len; i++) {
  74. var tokenAhead = tokensList[current + i];
  75. var expectedToken = i < 0 || arguments.length <= i ? undefined : arguments[i];
  76. if (tokenAhead.type === "keyword") {
  77. if (isKeyword(tokenAhead, expectedToken) === false) {
  78. return false;
  79. }
  80. } else if (expectedToken !== tokenAhead.type) {
  81. return false;
  82. }
  83. }
  84. return true;
  85. } // TODO(sven): there is probably a better way to do this
  86. // can refactor it if it get out of hands
  87. function maybeIgnoreComment() {
  88. if (typeof token === "undefined") {
  89. // Ignore
  90. return;
  91. }
  92. while (token.type === tokens.comment) {
  93. eatToken();
  94. if (typeof token === "undefined") {
  95. // Hit the end
  96. break;
  97. }
  98. }
  99. }
  100. /**
  101. * Parses a memory instruction
  102. *
  103. * WAST:
  104. *
  105. * memory: ( memory <name>? <memory_sig> )
  106. * ( memory <name>? ( export <string> ) <...> )
  107. * ( memory <name>? ( import <string> <string> ) <memory_sig> )
  108. * ( memory <name>? ( export <string> )* ( data <string>* )
  109. * memory_sig: <nat> <nat>?
  110. *
  111. */
  112. function parseMemory() {
  113. var id = t.identifier(getUniqueName("memory"));
  114. var limits = t.limit(0);
  115. if (token.type === tokens.string || token.type === tokens.identifier) {
  116. id = t.identifier(token.value);
  117. eatToken();
  118. } else {
  119. id = t.withRaw(id, ""); // preserve anonymous
  120. }
  121. /**
  122. * Maybe data
  123. */
  124. if (lookaheadAndCheck(tokens.openParen, keywords.data)) {
  125. eatToken(); // (
  126. eatToken(); // data
  127. // TODO(sven): do something with the data collected here
  128. var stringInitializer = token.value;
  129. eatTokenOfType(tokens.string); // Update limits accordingly
  130. limits = t.limit(stringInitializer.length);
  131. eatTokenOfType(tokens.closeParen);
  132. }
  133. /**
  134. * Maybe export
  135. */
  136. if (lookaheadAndCheck(tokens.openParen, keywords.export)) {
  137. eatToken(); // (
  138. eatToken(); // export
  139. if (token.type !== tokens.string) {
  140. throw function () {
  141. return new Error("\n" + codeFrameFromSource(source, token.loc) + "\n" + "Expected string in export" + ", given " + tokenToString(token));
  142. }();
  143. }
  144. var _name = token.value;
  145. eatToken();
  146. state.registredExportedElements.push({
  147. exportType: "Memory",
  148. name: _name,
  149. id: id
  150. });
  151. eatTokenOfType(tokens.closeParen);
  152. }
  153. /**
  154. * Memory signature
  155. */
  156. if (token.type === tokens.number) {
  157. limits = t.limit(parse32I(token.value));
  158. eatToken();
  159. if (token.type === tokens.number) {
  160. limits.max = parse32I(token.value);
  161. eatToken();
  162. }
  163. }
  164. return t.memory(limits, id);
  165. }
  166. /**
  167. * Parses a data section
  168. * https://webassembly.github.io/spec/core/text/modules.html#data-segments
  169. *
  170. * WAST:
  171. *
  172. * data: ( data <index>? <offset> <string> )
  173. */
  174. function parseData() {
  175. // optional memory index
  176. var memidx = 0;
  177. if (token.type === tokens.number) {
  178. memidx = token.value;
  179. eatTokenOfType(tokens.number); // .
  180. }
  181. eatTokenOfType(tokens.openParen);
  182. var offset;
  183. if (token.type === tokens.valtype) {
  184. eatTokenOfType(tokens.valtype); // i32
  185. eatTokenOfType(tokens.dot); // .
  186. if (token.value !== "const") {
  187. throw new Error("constant expression required");
  188. }
  189. eatTokenOfType(tokens.name); // const
  190. var numberLiteral = t.numberLiteralFromRaw(token.value, "i32");
  191. offset = t.objectInstruction("const", "i32", [numberLiteral]);
  192. eatToken();
  193. eatTokenOfType(tokens.closeParen);
  194. } else {
  195. eatTokenOfType(tokens.name); // get_global
  196. var _numberLiteral = t.numberLiteralFromRaw(token.value, "i32");
  197. offset = t.instruction("get_global", [_numberLiteral]);
  198. eatToken();
  199. eatTokenOfType(tokens.closeParen);
  200. }
  201. var byteArray = parseString(token.value);
  202. eatToken(); // "string"
  203. return t.data(t.memIndexLiteral(memidx), offset, t.byteArray(byteArray));
  204. }
  205. /**
  206. * Parses a table instruction
  207. *
  208. * WAST:
  209. *
  210. * table: ( table <name>? <table_type> )
  211. * ( table <name>? ( export <string> ) <...> )
  212. * ( table <name>? ( import <string> <string> ) <table_type> )
  213. * ( table <name>? ( export <string> )* <elem_type> ( elem <var>* ) )
  214. *
  215. * table_type: <nat> <nat>? <elem_type>
  216. * elem_type: anyfunc
  217. *
  218. * elem: ( elem <var>? (offset <instr>* ) <var>* )
  219. * ( elem <var>? <expr> <var>* )
  220. */
  221. function parseTable() {
  222. var name = t.identifier(getUniqueName("table"));
  223. var limit = t.limit(0);
  224. var elemIndices = [];
  225. var elemType = "anyfunc";
  226. if (token.type === tokens.string || token.type === tokens.identifier) {
  227. name = identifierFromToken(token);
  228. eatToken();
  229. } else {
  230. name = t.withRaw(name, ""); // preserve anonymous
  231. }
  232. while (token.type !== tokens.closeParen) {
  233. /**
  234. * Maybe export
  235. */
  236. if (lookaheadAndCheck(tokens.openParen, keywords.elem)) {
  237. eatToken(); // (
  238. eatToken(); // elem
  239. while (token.type === tokens.identifier) {
  240. elemIndices.push(t.identifier(token.value));
  241. eatToken();
  242. }
  243. eatTokenOfType(tokens.closeParen);
  244. } else if (lookaheadAndCheck(tokens.openParen, keywords.export)) {
  245. eatToken(); // (
  246. eatToken(); // export
  247. if (token.type !== tokens.string) {
  248. throw function () {
  249. return new Error("\n" + codeFrameFromSource(source, token.loc) + "\n" + "Expected string in export" + ", given " + tokenToString(token));
  250. }();
  251. }
  252. var exportName = token.value;
  253. eatToken();
  254. state.registredExportedElements.push({
  255. exportType: "Table",
  256. name: exportName,
  257. id: name
  258. });
  259. eatTokenOfType(tokens.closeParen);
  260. } else if (isKeyword(token, keywords.anyfunc)) {
  261. // It's the default value, we can ignore it
  262. eatToken(); // anyfunc
  263. } else if (token.type === tokens.number) {
  264. /**
  265. * Table type
  266. */
  267. var min = parseInt(token.value);
  268. eatToken();
  269. if (token.type === tokens.number) {
  270. var max = parseInt(token.value);
  271. eatToken();
  272. limit = t.limit(min, max);
  273. } else {
  274. limit = t.limit(min);
  275. }
  276. eatToken();
  277. } else {
  278. throw function () {
  279. return new Error("\n" + codeFrameFromSource(source, token.loc) + "\n" + "Unexpected token" + ", given " + tokenToString(token));
  280. }();
  281. }
  282. }
  283. if (elemIndices.length > 0) {
  284. return t.table(elemType, limit, name, elemIndices);
  285. } else {
  286. return t.table(elemType, limit, name);
  287. }
  288. }
  289. /**
  290. * Parses an import statement
  291. *
  292. * WAST:
  293. *
  294. * import: ( import <string> <string> <imkind> )
  295. * imkind: ( func <name>? <func_sig> )
  296. * ( global <name>? <global_sig> )
  297. * ( table <name>? <table_sig> )
  298. * ( memory <name>? <memory_sig> )
  299. *
  300. * global_sig: <type> | ( mut <type> )
  301. */
  302. function parseImport() {
  303. if (token.type !== tokens.string) {
  304. throw new Error("Expected a string, " + token.type + " given.");
  305. }
  306. var moduleName = token.value;
  307. eatToken();
  308. if (token.type !== tokens.string) {
  309. throw new Error("Expected a string, " + token.type + " given.");
  310. }
  311. var name = token.value;
  312. eatToken();
  313. eatTokenOfType(tokens.openParen);
  314. var descr;
  315. if (isKeyword(token, keywords.func)) {
  316. eatToken(); // keyword
  317. var fnParams = [];
  318. var fnResult = [];
  319. var typeRef;
  320. var fnName = t.identifier(getUniqueName("func"));
  321. if (token.type === tokens.identifier) {
  322. fnName = identifierFromToken(token);
  323. eatToken();
  324. }
  325. while (token.type === tokens.openParen) {
  326. eatToken();
  327. if (lookaheadAndCheck(keywords.type) === true) {
  328. eatToken();
  329. typeRef = parseTypeReference();
  330. } else if (lookaheadAndCheck(keywords.param) === true) {
  331. eatToken();
  332. fnParams.push.apply(fnParams, _toConsumableArray(parseFuncParam()));
  333. } else if (lookaheadAndCheck(keywords.result) === true) {
  334. eatToken();
  335. fnResult.push.apply(fnResult, _toConsumableArray(parseFuncResult()));
  336. } else {
  337. throw function () {
  338. return new Error("\n" + codeFrameFromSource(source, token.loc) + "\n" + "Unexpected token in import of type" + ", given " + tokenToString(token));
  339. }();
  340. }
  341. eatTokenOfType(tokens.closeParen);
  342. }
  343. if (typeof fnName === "undefined") {
  344. throw new Error("Imported function must have a name");
  345. }
  346. descr = t.funcImportDescr(fnName, typeRef !== undefined ? typeRef : t.signature(fnParams, fnResult));
  347. } else if (isKeyword(token, keywords.global)) {
  348. eatToken(); // keyword
  349. if (token.type === tokens.openParen) {
  350. eatToken(); // (
  351. eatTokenOfType(tokens.keyword); // mut keyword
  352. var valtype = token.value;
  353. eatToken();
  354. descr = t.globalType(valtype, "var");
  355. eatTokenOfType(tokens.closeParen);
  356. } else {
  357. var _valtype = token.value;
  358. eatTokenOfType(tokens.valtype);
  359. descr = t.globalType(_valtype, "const");
  360. }
  361. } else if (isKeyword(token, keywords.memory) === true) {
  362. eatToken(); // Keyword
  363. descr = parseMemory();
  364. } else if (isKeyword(token, keywords.table) === true) {
  365. eatToken(); // Keyword
  366. descr = parseTable();
  367. } else {
  368. throw new Error("Unsupported import type: " + tokenToString(token));
  369. }
  370. eatTokenOfType(tokens.closeParen);
  371. return t.moduleImport(moduleName, name, descr);
  372. }
  373. /**
  374. * Parses a block instruction
  375. *
  376. * WAST:
  377. *
  378. * expr: ( block <name>? <block_sig> <instr>* )
  379. * instr: block <name>? <block_sig> <instr>* end <name>?
  380. * block_sig : ( result <type>* )*
  381. *
  382. */
  383. function parseBlock() {
  384. var label = t.identifier(getUniqueName("block"));
  385. var blockResult = null;
  386. var instr = [];
  387. if (token.type === tokens.identifier) {
  388. label = identifierFromToken(token);
  389. eatToken();
  390. } else {
  391. label = t.withRaw(label, ""); // preserve anonymous
  392. }
  393. while (token.type === tokens.openParen) {
  394. eatToken();
  395. if (lookaheadAndCheck(keywords.result) === true) {
  396. eatToken();
  397. blockResult = token.value;
  398. eatToken();
  399. } else if (lookaheadAndCheck(tokens.name) === true || lookaheadAndCheck(tokens.valtype) === true || token.type === "keyword" // is any keyword
  400. ) {
  401. // Instruction
  402. instr.push(parseFuncInstr());
  403. } else {
  404. throw function () {
  405. return new Error("\n" + codeFrameFromSource(source, token.loc) + "\n" + "Unexpected token in block body of type" + ", given " + tokenToString(token));
  406. }();
  407. }
  408. maybeIgnoreComment();
  409. eatTokenOfType(tokens.closeParen);
  410. }
  411. return t.blockInstruction(label, instr, blockResult);
  412. }
  413. /**
  414. * Parses a if instruction
  415. *
  416. * WAST:
  417. *
  418. * expr:
  419. * ( if <name>? <block_sig> ( then <instr>* ) ( else <instr>* )? )
  420. * ( if <name>? <block_sig> <expr>+ ( then <instr>* ) ( else <instr>* )? )
  421. *
  422. * instr:
  423. * if <name>? <block_sig> <instr>* end <name>?
  424. * if <name>? <block_sig> <instr>* else <name>? <instr>* end <name>?
  425. *
  426. * block_sig : ( result <type>* )*
  427. *
  428. */
  429. function parseIf() {
  430. var blockResult = null;
  431. var label = t.identifier(getUniqueName("if"));
  432. var testInstrs = [];
  433. var consequent = [];
  434. var alternate = [];
  435. if (token.type === tokens.identifier) {
  436. label = identifierFromToken(token);
  437. eatToken();
  438. } else {
  439. label = t.withRaw(label, ""); // preserve anonymous
  440. }
  441. while (token.type === tokens.openParen) {
  442. eatToken(); // (
  443. /**
  444. * Block signature
  445. */
  446. if (isKeyword(token, keywords.result) === true) {
  447. eatToken();
  448. blockResult = token.value;
  449. eatTokenOfType(tokens.valtype);
  450. eatTokenOfType(tokens.closeParen);
  451. continue;
  452. }
  453. /**
  454. * Then
  455. */
  456. if (isKeyword(token, keywords.then) === true) {
  457. eatToken(); // then
  458. while (token.type === tokens.openParen) {
  459. eatToken(); // Instruction
  460. if (lookaheadAndCheck(tokens.name) === true || lookaheadAndCheck(tokens.valtype) === true || token.type === "keyword" // is any keyword
  461. ) {
  462. consequent.push(parseFuncInstr());
  463. } else {
  464. throw function () {
  465. return new Error("\n" + codeFrameFromSource(source, token.loc) + "\n" + "Unexpected token in consequent body of type" + ", given " + tokenToString(token));
  466. }();
  467. }
  468. eatTokenOfType(tokens.closeParen);
  469. }
  470. eatTokenOfType(tokens.closeParen);
  471. continue;
  472. }
  473. /**
  474. * Alternate
  475. */
  476. if (isKeyword(token, keywords.else)) {
  477. eatToken(); // else
  478. while (token.type === tokens.openParen) {
  479. eatToken(); // Instruction
  480. if (lookaheadAndCheck(tokens.name) === true || lookaheadAndCheck(tokens.valtype) === true || token.type === "keyword" // is any keyword
  481. ) {
  482. alternate.push(parseFuncInstr());
  483. } else {
  484. throw function () {
  485. return new Error("\n" + codeFrameFromSource(source, token.loc) + "\n" + "Unexpected token in alternate body of type" + ", given " + tokenToString(token));
  486. }();
  487. }
  488. eatTokenOfType(tokens.closeParen);
  489. }
  490. eatTokenOfType(tokens.closeParen);
  491. continue;
  492. }
  493. /**
  494. * Test instruction
  495. */
  496. if (lookaheadAndCheck(tokens.name) === true || lookaheadAndCheck(tokens.valtype) === true || token.type === "keyword" // is any keyword
  497. ) {
  498. testInstrs.push(parseFuncInstr());
  499. eatTokenOfType(tokens.closeParen);
  500. continue;
  501. }
  502. throw function () {
  503. return new Error("\n" + codeFrameFromSource(source, token.loc) + "\n" + "Unexpected token in if body" + ", given " + tokenToString(token));
  504. }();
  505. }
  506. return t.ifInstruction(label, testInstrs, blockResult, consequent, alternate);
  507. }
  508. /**
  509. * Parses a loop instruction
  510. *
  511. * WAT:
  512. *
  513. * blockinstr :: 'loop' I:label rt:resulttype (in:instr*) 'end' id?
  514. *
  515. * WAST:
  516. *
  517. * instr :: loop <name>? <block_sig> <instr>* end <name>?
  518. * expr :: ( loop <name>? <block_sig> <instr>* )
  519. * block_sig :: ( result <type>* )*
  520. *
  521. */
  522. function parseLoop() {
  523. var label = t.identifier(getUniqueName("loop"));
  524. var blockResult;
  525. var instr = [];
  526. if (token.type === tokens.identifier) {
  527. label = identifierFromToken(token);
  528. eatToken();
  529. } else {
  530. label = t.withRaw(label, ""); // preserve anonymous
  531. }
  532. while (token.type === tokens.openParen) {
  533. eatToken();
  534. if (lookaheadAndCheck(keywords.result) === true) {
  535. eatToken();
  536. blockResult = token.value;
  537. eatToken();
  538. } else if (lookaheadAndCheck(tokens.name) === true || lookaheadAndCheck(tokens.valtype) === true || token.type === "keyword" // is any keyword
  539. ) {
  540. // Instruction
  541. instr.push(parseFuncInstr());
  542. } else {
  543. throw function () {
  544. return new Error("\n" + codeFrameFromSource(source, token.loc) + "\n" + "Unexpected token in loop body" + ", given " + tokenToString(token));
  545. }();
  546. }
  547. eatTokenOfType(tokens.closeParen);
  548. }
  549. return t.loopInstruction(label, blockResult, instr);
  550. }
  551. function parseCallIndirect() {
  552. var typeRef;
  553. var params = [];
  554. var results = [];
  555. var instrs = [];
  556. while (token.type !== tokens.closeParen) {
  557. if (lookaheadAndCheck(tokens.openParen, keywords.type)) {
  558. eatToken(); // (
  559. eatToken(); // type
  560. typeRef = parseTypeReference();
  561. } else if (lookaheadAndCheck(tokens.openParen, keywords.param)) {
  562. eatToken(); // (
  563. eatToken(); // param
  564. /**
  565. * Params can be empty:
  566. * (params)`
  567. */
  568. if (token.type !== tokens.closeParen) {
  569. params.push.apply(params, _toConsumableArray(parseFuncParam()));
  570. }
  571. } else if (lookaheadAndCheck(tokens.openParen, keywords.result)) {
  572. eatToken(); // (
  573. eatToken(); // result
  574. /**
  575. * Results can be empty:
  576. * (result)`
  577. */
  578. if (token.type !== tokens.closeParen) {
  579. results.push.apply(results, _toConsumableArray(parseFuncResult()));
  580. }
  581. } else {
  582. eatTokenOfType(tokens.openParen);
  583. instrs.push(parseFuncInstr());
  584. }
  585. eatTokenOfType(tokens.closeParen);
  586. }
  587. return t.callIndirectInstruction(typeRef !== undefined ? typeRef : t.signature(params, results), instrs);
  588. }
  589. /**
  590. * Parses an export instruction
  591. *
  592. * WAT:
  593. *
  594. * export: ( export <string> <exkind> )
  595. * exkind: ( func <var> )
  596. * ( global <var> )
  597. * ( table <var> )
  598. * ( memory <var> )
  599. * var: <nat> | <name>
  600. *
  601. */
  602. function parseExport() {
  603. if (token.type !== tokens.string) {
  604. throw new Error("Expected string after export, got: " + token.type);
  605. }
  606. var name = token.value;
  607. eatToken();
  608. var moduleExportDescr = parseModuleExportDescr();
  609. return t.moduleExport(name, moduleExportDescr);
  610. }
  611. function parseModuleExportDescr() {
  612. var startLoc = getStartLoc();
  613. var type = "";
  614. var index;
  615. eatTokenOfType(tokens.openParen);
  616. while (token.type !== tokens.closeParen) {
  617. if (isKeyword(token, keywords.func)) {
  618. type = "Func";
  619. eatToken();
  620. index = parseExportIndex(token);
  621. } else if (isKeyword(token, keywords.table)) {
  622. type = "Table";
  623. eatToken();
  624. index = parseExportIndex(token);
  625. } else if (isKeyword(token, keywords.global)) {
  626. type = "Global";
  627. eatToken();
  628. index = parseExportIndex(token);
  629. } else if (isKeyword(token, keywords.memory)) {
  630. type = "Memory";
  631. eatToken();
  632. index = parseExportIndex(token);
  633. }
  634. eatToken();
  635. }
  636. if (type === "") {
  637. throw new Error("Unknown export type");
  638. }
  639. if (index === undefined) {
  640. throw new Error("Exported function must have a name");
  641. }
  642. var node = t.moduleExportDescr(type, index);
  643. var endLoc = getEndLoc();
  644. eatTokenOfType(tokens.closeParen);
  645. return t.withLoc(node, endLoc, startLoc);
  646. }
  647. function parseModule() {
  648. var name = null;
  649. var isBinary = false;
  650. var isQuote = false;
  651. var moduleFields = [];
  652. if (token.type === tokens.identifier) {
  653. name = token.value;
  654. eatToken();
  655. }
  656. if (hasPlugin("wast") && token.type === tokens.name && token.value === "binary") {
  657. eatToken();
  658. isBinary = true;
  659. }
  660. if (hasPlugin("wast") && token.type === tokens.name && token.value === "quote") {
  661. eatToken();
  662. isQuote = true;
  663. }
  664. if (isBinary === true) {
  665. var blob = [];
  666. while (token.type === tokens.string) {
  667. blob.push(token.value);
  668. eatToken();
  669. maybeIgnoreComment();
  670. }
  671. eatTokenOfType(tokens.closeParen);
  672. return t.binaryModule(name, blob);
  673. }
  674. if (isQuote === true) {
  675. var string = [];
  676. while (token.type === tokens.string) {
  677. string.push(token.value);
  678. eatToken();
  679. }
  680. eatTokenOfType(tokens.closeParen);
  681. return t.quoteModule(name, string);
  682. }
  683. while (token.type !== tokens.closeParen) {
  684. moduleFields.push(walk());
  685. if (state.registredExportedElements.length > 0) {
  686. state.registredExportedElements.forEach(function (decl) {
  687. moduleFields.push(t.moduleExport(decl.name, t.moduleExportDescr(decl.exportType, decl.id)));
  688. });
  689. state.registredExportedElements = [];
  690. }
  691. token = tokensList[current];
  692. }
  693. eatTokenOfType(tokens.closeParen);
  694. return t.module(name, moduleFields);
  695. }
  696. /**
  697. * Parses the arguments of an instruction
  698. */
  699. function parseFuncInstrArguments(signature) {
  700. var args = [];
  701. var namedArgs = {};
  702. var signaturePtr = 0;
  703. while (token.type === tokens.name || isKeyword(token, keywords.offset)) {
  704. var key = token.value;
  705. eatToken();
  706. eatTokenOfType(tokens.equal);
  707. var value = void 0;
  708. if (token.type === tokens.number) {
  709. value = t.numberLiteralFromRaw(token.value);
  710. } else {
  711. throw new Error("Unexpected type for argument: " + token.type);
  712. }
  713. namedArgs[key] = value;
  714. eatToken();
  715. } // $FlowIgnore
  716. var signatureLength = signature.vector ? Infinity : signature.length;
  717. while (token.type !== tokens.closeParen && ( // $FlowIgnore
  718. token.type === tokens.openParen || signaturePtr < signatureLength)) {
  719. if (token.type === tokens.identifier) {
  720. args.push(t.identifier(token.value));
  721. eatToken();
  722. } else if (token.type === tokens.valtype) {
  723. // Handle locals
  724. args.push(t.valtypeLiteral(token.value));
  725. eatToken();
  726. } else if (token.type === tokens.string) {
  727. args.push(t.stringLiteral(token.value));
  728. eatToken();
  729. } else if (token.type === tokens.number) {
  730. args.push( // TODO(sven): refactor the type signature handling
  731. // https://github.com/xtuc/webassemblyjs/pull/129 is a good start
  732. t.numberLiteralFromRaw(token.value, // $FlowIgnore
  733. signature[signaturePtr] || "f64")); // $FlowIgnore
  734. if (!signature.vector) {
  735. ++signaturePtr;
  736. }
  737. eatToken();
  738. } else if (token.type === tokens.openParen) {
  739. /**
  740. * Maybe some nested instructions
  741. */
  742. eatToken(); // Instruction
  743. if (lookaheadAndCheck(tokens.name) === true || lookaheadAndCheck(tokens.valtype) === true || token.type === "keyword" // is any keyword
  744. ) {
  745. // $FlowIgnore
  746. args.push(parseFuncInstr());
  747. } else {
  748. throw function () {
  749. return new Error("\n" + codeFrameFromSource(source, token.loc) + "\n" + "Unexpected token in nested instruction" + ", given " + tokenToString(token));
  750. }();
  751. }
  752. if (token.type === tokens.closeParen) {
  753. eatToken();
  754. }
  755. } else {
  756. throw function () {
  757. return new Error("\n" + codeFrameFromSource(source, token.loc) + "\n" + "Unexpected token in instruction argument" + ", given " + tokenToString(token));
  758. }();
  759. }
  760. }
  761. return {
  762. args: args,
  763. namedArgs: namedArgs
  764. };
  765. }
  766. /**
  767. * Parses an instruction
  768. *
  769. * WAT:
  770. *
  771. * instr :: plaininst
  772. * blockinstr
  773. *
  774. * blockinstr :: 'block' I:label rt:resulttype (in:instr*) 'end' id?
  775. * 'loop' I:label rt:resulttype (in:instr*) 'end' id?
  776. * 'if' I:label rt:resulttype (in:instr*) 'else' id? (in2:intr*) 'end' id?
  777. *
  778. * plaininst :: 'unreachable'
  779. * 'nop'
  780. * 'br' l:labelidx
  781. * 'br_if' l:labelidx
  782. * 'br_table' l*:vec(labelidx) ln:labelidx
  783. * 'return'
  784. * 'call' x:funcidx
  785. * 'call_indirect' x, I:typeuse
  786. *
  787. * WAST:
  788. *
  789. * instr:
  790. * <expr>
  791. * <op>
  792. * block <name>? <block_sig> <instr>* end <name>?
  793. * loop <name>? <block_sig> <instr>* end <name>?
  794. * if <name>? <block_sig> <instr>* end <name>?
  795. * if <name>? <block_sig> <instr>* else <name>? <instr>* end <name>?
  796. *
  797. * expr:
  798. * ( <op> )
  799. * ( <op> <expr>+ )
  800. * ( block <name>? <block_sig> <instr>* )
  801. * ( loop <name>? <block_sig> <instr>* )
  802. * ( if <name>? <block_sig> ( then <instr>* ) ( else <instr>* )? )
  803. * ( if <name>? <block_sig> <expr>+ ( then <instr>* ) ( else <instr>* )? )
  804. *
  805. * op:
  806. * unreachable
  807. * nop
  808. * br <var>
  809. * br_if <var>
  810. * br_table <var>+
  811. * return
  812. * call <var>
  813. * call_indirect <func_sig>
  814. * drop
  815. * select
  816. * get_local <var>
  817. * set_local <var>
  818. * tee_local <var>
  819. * get_global <var>
  820. * set_global <var>
  821. * <type>.load((8|16|32)_<sign>)? <offset>? <align>?
  822. * <type>.store(8|16|32)? <offset>? <align>?
  823. * current_memory
  824. * grow_memory
  825. * <type>.const <value>
  826. * <type>.<unop>
  827. * <type>.<binop>
  828. * <type>.<testop>
  829. * <type>.<relop>
  830. * <type>.<cvtop>/<type>
  831. *
  832. * func_type: ( type <var> )? <param>* <result>*
  833. */
  834. function parseFuncInstr() {
  835. var startLoc = getStartLoc();
  836. maybeIgnoreComment();
  837. /**
  838. * A simple instruction
  839. */
  840. if (token.type === tokens.name || token.type === tokens.valtype) {
  841. var _name2 = token.value;
  842. var object;
  843. eatToken();
  844. if (token.type === tokens.dot) {
  845. object = _name2;
  846. eatToken();
  847. if (token.type !== tokens.name) {
  848. throw new TypeError("Unknown token: " + token.type + ", name expected");
  849. }
  850. _name2 = token.value;
  851. eatToken();
  852. }
  853. if (token.type === tokens.closeParen) {
  854. var _endLoc = token.loc.end;
  855. if (typeof object === "undefined") {
  856. return t.withLoc(t.instruction(_name2), _endLoc, startLoc);
  857. } else {
  858. return t.withLoc(t.objectInstruction(_name2, object, []), _endLoc, startLoc);
  859. }
  860. }
  861. var signature = t.signatureForOpcode(object || "", _name2);
  862. var _parseFuncInstrArgume = parseFuncInstrArguments(signature),
  863. _args = _parseFuncInstrArgume.args,
  864. _namedArgs = _parseFuncInstrArgume.namedArgs;
  865. var endLoc = token.loc.end;
  866. if (typeof object === "undefined") {
  867. return t.withLoc(t.instruction(_name2, _args, _namedArgs), endLoc, startLoc);
  868. } else {
  869. return t.withLoc(t.objectInstruction(_name2, object, _args, _namedArgs), endLoc, startLoc);
  870. }
  871. } else if (isKeyword(token, keywords.loop)) {
  872. /**
  873. * Else a instruction with a keyword (loop or block)
  874. */
  875. eatToken(); // keyword
  876. return parseLoop();
  877. } else if (isKeyword(token, keywords.block)) {
  878. eatToken(); // keyword
  879. return parseBlock();
  880. } else if (isKeyword(token, keywords.call_indirect)) {
  881. eatToken(); // keyword
  882. return parseCallIndirect();
  883. } else if (isKeyword(token, keywords.call)) {
  884. eatToken(); // keyword
  885. var index;
  886. if (token.type === tokens.identifier) {
  887. index = identifierFromToken(token);
  888. eatToken();
  889. } else if (token.type === tokens.number) {
  890. index = t.indexLiteral(token.value);
  891. eatToken();
  892. }
  893. var instrArgs = []; // Nested instruction
  894. while (token.type === tokens.openParen) {
  895. eatToken();
  896. instrArgs.push(parseFuncInstr());
  897. eatTokenOfType(tokens.closeParen);
  898. }
  899. if (typeof index === "undefined") {
  900. throw new Error("Missing argument in call instruciton");
  901. }
  902. if (instrArgs.length > 0) {
  903. return t.callInstruction(index, instrArgs);
  904. } else {
  905. return t.callInstruction(index);
  906. }
  907. } else if (isKeyword(token, keywords.if)) {
  908. eatToken(); // Keyword
  909. return parseIf();
  910. } else if (isKeyword(token, keywords.module) && hasPlugin("wast")) {
  911. eatToken(); // In WAST you can have a module as an instruction's argument
  912. // we will cast it into a instruction to not break the flow
  913. // $FlowIgnore
  914. var module = parseModule();
  915. return module;
  916. } else {
  917. throw function () {
  918. return new Error("\n" + codeFrameFromSource(source, token.loc) + "\n" + "Unexpected instruction in function body" + ", given " + tokenToString(token));
  919. }();
  920. }
  921. }
  922. /*
  923. * Parses a function
  924. *
  925. * WAT:
  926. *
  927. * functype :: ( 'func' t1:vec(param) t2:vec(result) )
  928. * param :: ( 'param' id? t:valtype )
  929. * result :: ( 'result' t:valtype )
  930. *
  931. * WAST:
  932. *
  933. * func :: ( func <name>? <func_sig> <local>* <instr>* )
  934. * ( func <name>? ( export <string> ) <...> )
  935. * ( func <name>? ( import <string> <string> ) <func_sig> )
  936. * func_sig :: ( type <var> )? <param>* <result>*
  937. * param :: ( param <type>* ) | ( param <name> <type> )
  938. * result :: ( result <type>* )
  939. * local :: ( local <type>* ) | ( local <name> <type> )
  940. *
  941. */
  942. function parseFunc() {
  943. var fnName = t.identifier(getUniqueName("func"));
  944. var typeRef;
  945. var fnBody = [];
  946. var fnParams = [];
  947. var fnResult = []; // name
  948. if (token.type === tokens.identifier) {
  949. fnName = identifierFromToken(token);
  950. eatToken();
  951. } else {
  952. fnName = t.withRaw(fnName, ""); // preserve anonymous
  953. }
  954. maybeIgnoreComment();
  955. while (token.type === tokens.openParen || token.type === tokens.name || token.type === tokens.valtype) {
  956. // Instructions without parens
  957. if (token.type === tokens.name || token.type === tokens.valtype) {
  958. fnBody.push(parseFuncInstr());
  959. continue;
  960. }
  961. eatToken();
  962. if (lookaheadAndCheck(keywords.param) === true) {
  963. eatToken();
  964. fnParams.push.apply(fnParams, _toConsumableArray(parseFuncParam()));
  965. } else if (lookaheadAndCheck(keywords.result) === true) {
  966. eatToken();
  967. fnResult.push.apply(fnResult, _toConsumableArray(parseFuncResult()));
  968. } else if (lookaheadAndCheck(keywords.export) === true) {
  969. eatToken();
  970. parseFuncExport(fnName);
  971. } else if (lookaheadAndCheck(keywords.type) === true) {
  972. eatToken();
  973. typeRef = parseTypeReference();
  974. } else if (lookaheadAndCheck(tokens.name) === true || lookaheadAndCheck(tokens.valtype) === true || token.type === "keyword" // is any keyword
  975. ) {
  976. // Instruction
  977. fnBody.push(parseFuncInstr());
  978. } else {
  979. throw function () {
  980. return new Error("\n" + codeFrameFromSource(source, token.loc) + "\n" + "Unexpected token in func body" + ", given " + tokenToString(token));
  981. }();
  982. }
  983. eatTokenOfType(tokens.closeParen);
  984. }
  985. return t.func(fnName, typeRef !== undefined ? typeRef : t.signature(fnParams, fnResult), fnBody);
  986. }
  987. /**
  988. * Parses shorthand export in func
  989. *
  990. * export :: ( export <string> )
  991. */
  992. function parseFuncExport(funcId) {
  993. if (token.type !== tokens.string) {
  994. throw function () {
  995. return new Error("\n" + codeFrameFromSource(source, token.loc) + "\n" + "Function export expected a string" + ", given " + tokenToString(token));
  996. }();
  997. }
  998. var name = token.value;
  999. eatToken();
  1000. /**
  1001. * Func export shorthand, we trait it as a syntaxic sugar.
  1002. * A export ModuleField will be added later.
  1003. *
  1004. * We give the anonymous function a generated name and export it.
  1005. */
  1006. var id = t.identifier(funcId.value);
  1007. state.registredExportedElements.push({
  1008. exportType: "Func",
  1009. name: name,
  1010. id: id
  1011. });
  1012. }
  1013. /**
  1014. * Parses a type instruction
  1015. *
  1016. * WAST:
  1017. *
  1018. * typedef: ( type <name>? ( func <param>* <result>* ) )
  1019. */
  1020. function parseType() {
  1021. var id;
  1022. var params = [];
  1023. var result = [];
  1024. if (token.type === tokens.identifier) {
  1025. id = identifierFromToken(token);
  1026. eatToken();
  1027. }
  1028. if (lookaheadAndCheck(tokens.openParen, keywords.func)) {
  1029. eatToken(); // (
  1030. eatToken(); // func
  1031. if (token.type === tokens.closeParen) {
  1032. eatToken(); // function with an empty signature, we can abort here
  1033. return t.typeInstruction(id, t.signature([], []));
  1034. }
  1035. if (lookaheadAndCheck(tokens.openParen, keywords.param)) {
  1036. eatToken(); // (
  1037. eatToken(); // param
  1038. params = parseFuncParam();
  1039. eatTokenOfType(tokens.closeParen);
  1040. }
  1041. if (lookaheadAndCheck(tokens.openParen, keywords.result)) {
  1042. eatToken(); // (
  1043. eatToken(); // result
  1044. result = parseFuncResult();
  1045. eatTokenOfType(tokens.closeParen);
  1046. }
  1047. eatTokenOfType(tokens.closeParen);
  1048. }
  1049. return t.typeInstruction(id, t.signature(params, result));
  1050. }
  1051. /**
  1052. * Parses a function result
  1053. *
  1054. * WAST:
  1055. *
  1056. * result :: ( result <type>* )
  1057. */
  1058. function parseFuncResult() {
  1059. var results = [];
  1060. while (token.type !== tokens.closeParen) {
  1061. if (token.type !== tokens.valtype) {
  1062. throw function () {
  1063. return new Error("\n" + codeFrameFromSource(source, token.loc) + "\n" + "Unexpected token in func result" + ", given " + tokenToString(token));
  1064. }();
  1065. }
  1066. var valtype = token.value;
  1067. eatToken();
  1068. results.push(valtype);
  1069. }
  1070. return results;
  1071. }
  1072. /**
  1073. * Parses a type reference
  1074. *
  1075. */
  1076. function parseTypeReference() {
  1077. var ref;
  1078. if (token.type === tokens.identifier) {
  1079. ref = identifierFromToken(token);
  1080. eatToken();
  1081. } else if (token.type === tokens.number) {
  1082. ref = t.numberLiteralFromRaw(token.value);
  1083. eatToken();
  1084. }
  1085. return ref;
  1086. }
  1087. /**
  1088. * Parses a global instruction
  1089. *
  1090. * WAST:
  1091. *
  1092. * global: ( global <name>? <global_sig> <instr>* )
  1093. * ( global <name>? ( export <string> ) <...> )
  1094. * ( global <name>? ( import <string> <string> ) <global_sig> )
  1095. *
  1096. * global_sig: <type> | ( mut <type> )
  1097. *
  1098. */
  1099. function parseGlobal() {
  1100. var name = t.identifier(getUniqueName("global"));
  1101. var type; // Keep informations in case of a shorthand import
  1102. var importing = null;
  1103. maybeIgnoreComment();
  1104. if (token.type === tokens.identifier) {
  1105. name = identifierFromToken(token);
  1106. eatToken();
  1107. } else {
  1108. name = t.withRaw(name, ""); // preserve anonymous
  1109. }
  1110. /**
  1111. * maybe export
  1112. */
  1113. if (lookaheadAndCheck(tokens.openParen, keywords.export)) {
  1114. eatToken(); // (
  1115. eatToken(); // export
  1116. var exportName = token.value;
  1117. eatTokenOfType(tokens.string);
  1118. state.registredExportedElements.push({
  1119. exportType: "Global",
  1120. name: exportName,
  1121. id: name
  1122. });
  1123. eatTokenOfType(tokens.closeParen);
  1124. }
  1125. /**
  1126. * maybe import
  1127. */
  1128. if (lookaheadAndCheck(tokens.openParen, keywords.import)) {
  1129. eatToken(); // (
  1130. eatToken(); // import
  1131. var moduleName = token.value;
  1132. eatTokenOfType(tokens.string);
  1133. var _name3 = token.value;
  1134. eatTokenOfType(tokens.string);
  1135. importing = {
  1136. module: moduleName,
  1137. name: _name3,
  1138. descr: undefined
  1139. };
  1140. eatTokenOfType(tokens.closeParen);
  1141. }
  1142. /**
  1143. * global_sig
  1144. */
  1145. if (token.type === tokens.valtype) {
  1146. type = t.globalType(token.value, "const");
  1147. eatToken();
  1148. } else if (token.type === tokens.openParen) {
  1149. eatToken(); // (
  1150. if (isKeyword(token, keywords.mut) === false) {
  1151. throw function () {
  1152. return new Error("\n" + codeFrameFromSource(source, token.loc) + "\n" + "Unsupported global type, expected mut" + ", given " + tokenToString(token));
  1153. }();
  1154. }
  1155. eatToken(); // mut
  1156. type = t.globalType(token.value, "var");
  1157. eatToken();
  1158. eatTokenOfType(tokens.closeParen);
  1159. }
  1160. if (type === undefined) {
  1161. throw function () {
  1162. return new Error("\n" + codeFrameFromSource(source, token.loc) + "\n" + "Could not determine global type" + ", given " + tokenToString(token));
  1163. }();
  1164. }
  1165. maybeIgnoreComment();
  1166. var init = [];
  1167. if (importing != null) {
  1168. importing.descr = type;
  1169. init.push(t.moduleImport(importing.module, importing.name, importing.descr));
  1170. }
  1171. /**
  1172. * instr*
  1173. */
  1174. while (token.type === tokens.openParen) {
  1175. eatToken();
  1176. init.push(parseFuncInstr());
  1177. eatTokenOfType(tokens.closeParen);
  1178. }
  1179. return t.global(type, init, name);
  1180. }
  1181. /**
  1182. * Parses a function param
  1183. *
  1184. * WAST:
  1185. *
  1186. * param :: ( param <type>* ) | ( param <name> <type> )
  1187. */
  1188. function parseFuncParam() {
  1189. var params = [];
  1190. var id;
  1191. var valtype;
  1192. if (token.type === tokens.identifier) {
  1193. id = token.value;
  1194. eatToken();
  1195. }
  1196. if (token.type === tokens.valtype) {
  1197. valtype = token.value;
  1198. eatToken();
  1199. params.push({
  1200. id: id,
  1201. valtype: valtype
  1202. });
  1203. /**
  1204. * Shorthand notation for multiple anonymous parameters
  1205. * @see https://webassembly.github.io/spec/core/text/types.html#function-types
  1206. * @see https://github.com/xtuc/webassemblyjs/issues/6
  1207. */
  1208. if (id === undefined) {
  1209. while (token.type === tokens.valtype) {
  1210. valtype = token.value;
  1211. eatToken();
  1212. params.push({
  1213. id: undefined,
  1214. valtype: valtype
  1215. });
  1216. }
  1217. }
  1218. } else {// ignore
  1219. }
  1220. return params;
  1221. }
  1222. /**
  1223. * Parses an element segments instruction
  1224. *
  1225. * WAST:
  1226. *
  1227. * elem: ( elem <var>? (offset <instr>* ) <var>* )
  1228. * ( elem <var>? <expr> <var>* )
  1229. *
  1230. * var: <nat> | <name>
  1231. */
  1232. function parseElem() {
  1233. var tableIndex = t.indexLiteral(0);
  1234. var offset = [];
  1235. var funcs = [];
  1236. if (token.type === tokens.identifier) {
  1237. tableIndex = identifierFromToken(token);
  1238. eatToken();
  1239. }
  1240. if (token.type === tokens.number) {
  1241. tableIndex = t.indexLiteral(token.value);
  1242. eatToken();
  1243. }
  1244. while (token.type !== tokens.closeParen) {
  1245. if (lookaheadAndCheck(tokens.openParen, keywords.offset)) {
  1246. eatToken(); // (
  1247. eatToken(); // offset
  1248. while (token.type !== tokens.closeParen) {
  1249. eatTokenOfType(tokens.openParen);
  1250. offset.push(parseFuncInstr());
  1251. eatTokenOfType(tokens.closeParen);
  1252. }
  1253. eatTokenOfType(tokens.closeParen);
  1254. } else if (token.type === tokens.identifier) {
  1255. funcs.push(t.identifier(token.value));
  1256. eatToken();
  1257. } else if (token.type === tokens.number) {
  1258. funcs.push(t.indexLiteral(token.value));
  1259. eatToken();
  1260. } else if (token.type === tokens.openParen) {
  1261. eatToken(); // (
  1262. offset.push(parseFuncInstr());
  1263. eatTokenOfType(tokens.closeParen);
  1264. } else {
  1265. throw function () {
  1266. return new Error("\n" + codeFrameFromSource(source, token.loc) + "\n" + "Unsupported token in elem" + ", given " + tokenToString(token));
  1267. }();
  1268. }
  1269. }
  1270. return t.elem(tableIndex, offset, funcs);
  1271. }
  1272. /**
  1273. * Parses the start instruction in a module
  1274. *
  1275. * WAST:
  1276. *
  1277. * start: ( start <var> )
  1278. * var: <nat> | <name>
  1279. *
  1280. * WAT:
  1281. * start ::= ( start x:funcidx )
  1282. */
  1283. function parseStart() {
  1284. if (token.type === tokens.identifier) {
  1285. var index = identifierFromToken(token);
  1286. eatToken();
  1287. return t.start(index);
  1288. }
  1289. if (token.type === tokens.number) {
  1290. var _index2 = t.indexLiteral(token.value);
  1291. eatToken();
  1292. return t.start(_index2);
  1293. }
  1294. throw new Error("Unknown start, token: " + tokenToString(token));
  1295. }
  1296. if (token.type === tokens.openParen) {
  1297. eatToken();
  1298. var startLoc = getStartLoc();
  1299. if (isKeyword(token, keywords.export)) {
  1300. eatToken();
  1301. var node = parseExport();
  1302. var _endLoc2 = getEndLoc();
  1303. return t.withLoc(node, _endLoc2, startLoc);
  1304. }
  1305. if (isKeyword(token, keywords.loop)) {
  1306. eatToken();
  1307. var _node = parseLoop();
  1308. var _endLoc3 = getEndLoc();
  1309. return t.withLoc(_node, _endLoc3, startLoc);
  1310. }
  1311. if (isKeyword(token, keywords.func)) {
  1312. eatToken();
  1313. var _node2 = parseFunc();
  1314. var _endLoc4 = getEndLoc();
  1315. maybeIgnoreComment();
  1316. eatTokenOfType(tokens.closeParen);
  1317. return t.withLoc(_node2, _endLoc4, startLoc);
  1318. }
  1319. if (isKeyword(token, keywords.module)) {
  1320. eatToken();
  1321. var _node3 = parseModule();
  1322. var _endLoc5 = getEndLoc();
  1323. return t.withLoc(_node3, _endLoc5, startLoc);
  1324. }
  1325. if (isKeyword(token, keywords.import)) {
  1326. eatToken();
  1327. var _node4 = parseImport();
  1328. var _endLoc6 = getEndLoc();
  1329. eatTokenOfType(tokens.closeParen);
  1330. return t.withLoc(_node4, _endLoc6, startLoc);
  1331. }
  1332. if (isKeyword(token, keywords.block)) {
  1333. eatToken();
  1334. var _node5 = parseBlock();
  1335. var _endLoc7 = getEndLoc();
  1336. eatTokenOfType(tokens.closeParen);
  1337. return t.withLoc(_node5, _endLoc7, startLoc);
  1338. }
  1339. if (isKeyword(token, keywords.memory)) {
  1340. eatToken();
  1341. var _node6 = parseMemory();
  1342. var _endLoc8 = getEndLoc();
  1343. eatTokenOfType(tokens.closeParen);
  1344. return t.withLoc(_node6, _endLoc8, startLoc);
  1345. }
  1346. if (isKeyword(token, keywords.data)) {
  1347. eatToken();
  1348. var _node7 = parseData();
  1349. var _endLoc9 = getEndLoc();
  1350. eatTokenOfType(tokens.closeParen);
  1351. return t.withLoc(_node7, _endLoc9, startLoc);
  1352. }
  1353. if (isKeyword(token, keywords.table)) {
  1354. eatToken();
  1355. var _node8 = parseTable();
  1356. var _endLoc10 = getEndLoc();
  1357. eatTokenOfType(tokens.closeParen);
  1358. return t.withLoc(_node8, _endLoc10, startLoc);
  1359. }
  1360. if (isKeyword(token, keywords.global)) {
  1361. eatToken();
  1362. var _node9 = parseGlobal();
  1363. var _endLoc11 = getEndLoc();
  1364. eatTokenOfType(tokens.closeParen);
  1365. return t.withLoc(_node9, _endLoc11, startLoc);
  1366. }
  1367. if (isKeyword(token, keywords.type)) {
  1368. eatToken();
  1369. var _node10 = parseType();
  1370. var _endLoc12 = getEndLoc();
  1371. eatTokenOfType(tokens.closeParen);
  1372. return t.withLoc(_node10, _endLoc12, startLoc);
  1373. }
  1374. if (isKeyword(token, keywords.start)) {
  1375. eatToken();
  1376. var _node11 = parseStart();
  1377. var _endLoc13 = getEndLoc();
  1378. eatTokenOfType(tokens.closeParen);
  1379. return t.withLoc(_node11, _endLoc13, startLoc);
  1380. }
  1381. if (isKeyword(token, keywords.elem)) {
  1382. eatToken();
  1383. var _node12 = parseElem();
  1384. var _endLoc14 = getEndLoc();
  1385. eatTokenOfType(tokens.closeParen);
  1386. return t.withLoc(_node12, _endLoc14, startLoc);
  1387. }
  1388. var instruction = parseFuncInstr();
  1389. var endLoc = getEndLoc();
  1390. maybeIgnoreComment();
  1391. if (_typeof(instruction) === "object") {
  1392. if (typeof token !== "undefined") {
  1393. eatTokenOfType(tokens.closeParen);
  1394. }
  1395. return t.withLoc(instruction, endLoc, startLoc);
  1396. }
  1397. }
  1398. if (token.type === tokens.comment) {
  1399. var _startLoc = getStartLoc();
  1400. var builder = token.opts.type === "leading" ? t.leadingComment : t.blockComment;
  1401. var _node13 = builder(token.value);
  1402. eatToken(); // comment
  1403. var _endLoc15 = getEndLoc();
  1404. return t.withLoc(_node13, _endLoc15, _startLoc);
  1405. }
  1406. throw function () {
  1407. return new Error("\n" + codeFrameFromSource(source, token.loc) + "\n" + "Unknown token" + ", given " + tokenToString(token));
  1408. }();
  1409. }
  1410. var body = [];
  1411. while (current < tokensList.length) {
  1412. body.push(walk());
  1413. }
  1414. return t.program(body);
  1415. }