ivprogParser.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  1. import { CommonTokenStream, InputStream } from 'antlr4/index';
  2. import * as Expressions from './expressions/';
  3. import * as Commands from './commands/';
  4. import { SyntaxError } from './SyntaxError';
  5. export class IVProgParser {
  6. static get BASE () {
  7. return 0;
  8. }
  9. static get FUNCTION () {
  10. return 1;
  11. }
  12. static get COMMAND () {
  13. return 2;
  14. }
  15. static get LOOP () {
  16. return 4;
  17. }
  18. constructor (input, lexerClass) {
  19. this.lexerClass = lexerClass;
  20. this.lexer = new lexerClass(new InputStream(input));
  21. this.tokenStream = new CommonTokenStream(this.lexer);
  22. this.tokenStream.fill();
  23. this.pos = 1;
  24. this.variableTypes = [this.lexerClass.RK_INTEGER,
  25. this.lexerClass.RK_REAL,
  26. this.lexerClass.RK_BOOLEAN,
  27. this.lexerClass.RK_STRING
  28. ];
  29. this.functionTypes = this.variableTypes.concat(this.lexerClass.RK_VOID);
  30. this.parsingArrayDimension = 0;
  31. }
  32. parseTree () {
  33. return this.parseProgram();
  34. }
  35. getToken (index = this.pos) {
  36. // if(index === null)
  37. // index = this.pos;
  38. return this.tokenStream.LT(index);
  39. }
  40. isEOF () {
  41. this.getToken(this.pos);
  42. return this.tokenStream.fetchedEOF;
  43. }
  44. parseProgram () {
  45. const token = this.getToken();
  46. let globalVars = [];
  47. let functions = [];
  48. if(this.lexerClass.RK_PROGRAM === token.type) {
  49. this.pos++;
  50. this.consumeNewLines();
  51. this.checkOpenCurly();
  52. this.pos++;
  53. while(true) {
  54. this.consumeNewLines();
  55. const token = this.getToken();
  56. if (token.type === this.lexerClass.RK_CONST || token.type === this.lexerClass.ID) {
  57. globalVars = globalVars.concat(this.parseGlobalVariables());
  58. } else if (token.type === this.lexerClass.RK_FUNCTION) {
  59. functions = functions.concat(this.parseFunction());
  60. } else {
  61. break;
  62. }
  63. }
  64. this.consumeNewLines();
  65. this.checkCloseCurly();
  66. this.pos++;
  67. this.consumeNewLines();
  68. if(!this.isEOF()) {
  69. throw new Error("No extra characters are allowed after 'program {...}'");
  70. }
  71. return {global: globalVars, functions: functions};
  72. } else {
  73. throw SyntaxError.createError(this.lexer.literalNames[this.lexerClass.RK_PROGRAM], token);
  74. }
  75. }
  76. checkOpenCurly () {
  77. const token = this.getToken();
  78. if(this.lexerClass.OPEN_CURLY !== token.type){
  79. throw SyntaxError.createError('{', token);
  80. }
  81. }
  82. checkCloseCurly () {
  83. const token = this.getToken();
  84. if(this.lexerClass.CLOSE_CURLY !== token.type){
  85. throw SyntaxError.createError('}', token);
  86. }
  87. }
  88. /* It checks if the current token at position pos is a ']'.
  89. * As a check function it doesn't increment pos.
  90. *
  91. * @params bool:attempt, indicates that the token is optional. Defaults: false
  92. *
  93. * @returns true if the attempt is true and current token is '[',
  94. * false is attempt is true and current token is not '['
  95. **/
  96. checkOpenBrace (attempt = false) {
  97. const token = this.getToken();
  98. if(this.lexerClass.OPEN_BRACE !== token.type){
  99. if (!attempt) {
  100. throw SyntaxError.createError('[', token);
  101. } else {
  102. return false;
  103. }
  104. }
  105. return true;
  106. }
  107. checkCloseBrace (attempt = false) {
  108. const token = this.getToken();
  109. if(this.lexerClass.CLOSE_BRACE !== token.type){
  110. if (!attempt) {
  111. throw SyntaxError.createError(']', token);
  112. } else {
  113. return false;
  114. }
  115. }
  116. return true;
  117. }
  118. checkOpenParenthesis (attempt = false) {
  119. const token = this.getToken();
  120. if(this.lexerClass.OPEN_PARENTHESIS !== token.type){
  121. if (!attempt) {
  122. throw SyntaxError.createError('(', token);
  123. } else {
  124. return false;
  125. }
  126. }
  127. return true;
  128. }
  129. checkCloseParenthesis (attempt = false) {
  130. const token = this.getToken();
  131. if(this.lexerClass.CLOSE_PARENTHESIS !== token.type){
  132. if (!attempt) {
  133. throw SyntaxError.createError(')', token);
  134. } else {
  135. return false;
  136. }
  137. }
  138. return true;
  139. }
  140. checkEOS (attempt = false) {
  141. const eosToken = this.getToken();
  142. if (eosToken.type !== this.lexerClass.EOS) {
  143. if (!attempt)
  144. throw SyntaxError.createError('new line or \';\'', eosToken);
  145. else
  146. return false;
  147. }
  148. return true;
  149. }
  150. parseGlobalVariables () {
  151. const decl = this.parseMaybeConst();
  152. const eosToken = this.getToken();
  153. this.checkEOS();
  154. this.pos++;
  155. return decl;
  156. }
  157. /*
  158. * Checks if the next token is PR_CONST. It's only available
  159. * at global variables declaration level
  160. * @returns Declararion(const, type, id, initVal?)
  161. **/
  162. parseMaybeConst () {
  163. const constToken = this.getToken();
  164. if(constToken.type === this.lexerClass.RK_CONST) {
  165. this.pos++;
  166. const typeString = this.parseType();
  167. return this.parseDeclararion(typeString, true);
  168. } else if(this.isVariableType(constToken)) {
  169. this.pos++;
  170. return this.parseDeclararion(constToken);
  171. } else {
  172. throw SyntaxError.createError(this.lexer.literalNames[this.lexerClass.RK_CONST] + ' or ' + this.getTypesAsString(), constToken);
  173. }
  174. }
  175. /*
  176. * Parses a declarion of the form: type --- id --- (= --- EAnd)?
  177. * @returns a list of Declararion(const, type, id, initVal?)
  178. **/
  179. parseDeclararion (typeString, isConst = false) {
  180. let initial = null;
  181. let dim1 = null;
  182. let dim2 = null;
  183. const idString = this.parseID();
  184. // Check for array or vector
  185. // ID[int/IDi][int/IDj]
  186. if (this.checkOpenBrace(true)) {
  187. this.pos++;
  188. this.consumeNewLines();
  189. dim1 = this.parseArrayDimension();
  190. this.consumeNewLines();
  191. this.checkCloseBrace();
  192. this.pos++;
  193. if(this.checkOpenBrace(true)) {
  194. this.pos++;
  195. this.consumeNewLines();
  196. dim2 = this.parseArrayDimension();
  197. this.consumeNewLines();
  198. this.checkCloseBrace();
  199. this.pos++;
  200. }
  201. }
  202. const equalsToken = this.getToken();
  203. if(equalsToken.type === this.lexerClass.EQUAL) {
  204. this.pos++;
  205. initial = this.parseExpressionOR();
  206. }
  207. let declaration = null;
  208. if (dim1 !== null) {
  209. declaration = new Commands.ArrayDeclaration(idString,
  210. typeString, dim1, dim2, initial, isConst);
  211. } else {
  212. declaration = new Commands.Declaration(idString, typeString, initial, isConst);
  213. }
  214. const commaToken = this.getToken();
  215. if(commaToken.type === this.lexerClass.COMMA) {
  216. console.log("comma found");
  217. this.pos++;
  218. this.consumeNewLines();
  219. return [declaration]
  220. .concat(this.parseDeclararion(typeString, isConst));
  221. } else {
  222. return [declaration]
  223. }
  224. }
  225. consumeNewLines () {
  226. let token = this.getToken();
  227. while(token.type === this.lexerClass.EOS && token.text.match('[\r\n]+')) {
  228. this.pos++;
  229. token = this.getToken();
  230. }
  231. }
  232. isVariableType (token) {
  233. return this.variableTypes.find(v => v === token.type);
  234. }
  235. /*
  236. * Reads the next token of the stream to check if it is a Integer or an ID.
  237. * @returns Integer | ID
  238. **/
  239. parseArrayDimension () {
  240. const dimToken = this.getToken();
  241. if(dimToken.type === this.lexerClass.INTEGER) {
  242. //parse as int literal
  243. this.pos++;
  244. return this.getIntLiteral(dimToken);
  245. } else if(dimToken.type === this.lexerClass.ID) {
  246. //parse as variable
  247. this.pos++;
  248. return this.parseVariable(dimToken);
  249. } else {
  250. throw SyntaxError.createError('int or ID', dimToken);
  251. }
  252. }
  253. /*
  254. * Returns an object {type: 'int', value: value}.
  255. * It checks for binary and hexadecimal integers.
  256. * @returns object with fields type and value
  257. **/
  258. getIntLiteral (token) {
  259. const text = token.text;
  260. let val = null;
  261. if(text.match('^0b|^0B')) {
  262. val = parseInt(text.substring(2), 2);
  263. } else if (text.match('^0x|^0X')) {
  264. val = parseInt(text.substring(2), 16);
  265. } else {
  266. val = parseInt(text);
  267. }
  268. return new Expressions.IntLiteral(val);
  269. }
  270. getRealLiteral (token) {
  271. return new Expressions.RealLiteral(parseFloat(token.text));
  272. }
  273. getStringLiteral (token) {
  274. const text = token.text;
  275. let value = text.replace("\\b", "\b");
  276. value = value.replace("\\t", "\t");
  277. value = value.replace("\\n", "\n");
  278. value = value.replace("\\r", "\r");
  279. value = value.replace("\\\"", "\"");
  280. value = value.replace("\\\'", "\'");
  281. value = value.replace("\\\\", "\\");
  282. return new Expressions.StringLiteral(value);
  283. }
  284. getBoolLiteral (token) {
  285. const val = token.type === this.lexerClass.RK_True ? true : false;
  286. return new Expressions.BoolLiteral(val);
  287. }
  288. parseArrayLiteral () {
  289. this.checkOpenCurly();
  290. const beginArray = this.getToken();
  291. if (this.parsingArrayDimension >= 2) {
  292. // TODO: better error message
  293. throw new Error(`Array dimensions exceed maximum size of 2 at line ${beginArray.line}`);
  294. }
  295. this.pos++;
  296. this.parsingArrayDimension++;
  297. this.consumeNewLines();
  298. const data = this.parseExpressionList();
  299. this.consumeNewLines();
  300. this.checkCloseCurly()
  301. this.pos++;
  302. this.parsingArrayDimension--;
  303. if (this.parsingArrayDimension === 0) {
  304. if (!data.isValid) {
  305. // TODO: better error message
  306. console.log('invalid array');
  307. throw new Error(`Invalid array at line ${beginArray.line}`);
  308. }
  309. }
  310. return new Expressions.ArrayLiteral(data);
  311. }
  312. /*
  313. * Returns an object {type: 'variable', value: value}.
  314. * @returns object with fields type and value
  315. **/
  316. parseVariable (token) {
  317. return new Expressions.VariableLiteral(token.text);
  318. }
  319. /*
  320. * Returns an object representing a function. It has
  321. * four attributes: returnType, id, formalParams and block.
  322. * The block object has two attributes: declarations and commands
  323. **/
  324. parseFunction () {
  325. let formalParams = [];
  326. const token = this.getToken();
  327. if(token.type !== this.lexerClass.RK_FUNCTION) {
  328. //throw SyntaxError.createError(this.lexer.literalNames[this.lexerClass.PR_FUNCAO], token);
  329. return null;
  330. }
  331. this.pos++;
  332. const returnType = this.parseType(true);
  333. const functionID = this.parseID();
  334. this.checkOpenParenthesis();
  335. this.pos++;
  336. this.consumeNewLines();
  337. if (!this.checkCloseParenthesis(true)) {
  338. formalParams = this.parseFormalParameters(); // formal parameters
  339. this.consumeNewLines();
  340. this.checkCloseParenthesis();
  341. this.pos++;
  342. } else {
  343. this.pos++;
  344. }
  345. this.consumeNewLines();
  346. const commandsBlock = this.parseCommandBlock();
  347. return {returnType: returnType, id: functionID, formalParams: formalParams, block: commandsBlock};
  348. }
  349. /*
  350. * Parse the formal parameters of a function.
  351. * @returns a list of objects with the following attributes: type, id and dimensions.
  352. **/
  353. parseFormalParameters () {
  354. const list = [];
  355. while(true) {
  356. let dimensions = 0;
  357. const typeString = this.parseType();
  358. const idString = this.parseID();
  359. if (this.checkOpenBrace(true)) {
  360. this.pos++;
  361. dimensions++;
  362. this.checkCloseBrace();
  363. this.pos++;
  364. if(this.checkOpenBrace(true)) {
  365. this.pos++;
  366. dimensions++;
  367. this.checkCloseBrace();
  368. this.pos++;
  369. }
  370. }
  371. list.push({type: typeString, id: idString, dimensions: dimensions});
  372. const commaToken = this.getToken();
  373. if (commaToken.type !== this.lexerClass.COMMA)
  374. break;
  375. this.pos++;
  376. this.consumeNewLines();
  377. }
  378. return list;
  379. }
  380. parseID () {
  381. const token = this.getToken();
  382. if(token.type !== this.lexerClass.ID) {
  383. throw SyntaxError.createError('ID', token);
  384. }
  385. this.pos++;
  386. return token.text;
  387. }
  388. parseType (isFunction = false) {
  389. const token = this.getToken();
  390. if(token.type === this.lexerClass.ID && isFunction) {
  391. return 'void';
  392. } else if (token.type === this.lexerClass.RK_VOID && isFunction) {
  393. this.pos++;
  394. return 'void';
  395. } else if (this.isVariableType(token)) {
  396. this.pos++;
  397. switch(token.type) {
  398. case this.lexerClass.RK_INTEGER:
  399. return 'int';
  400. case this.lexerClass.RK_LOGIC:
  401. return 'bool';
  402. case this.lexerClass.RK_REAL:
  403. return 'real';
  404. case this.lexerClass.RK_STRING:
  405. return 'string';
  406. default:
  407. break;
  408. }
  409. }
  410. throw SyntaxError.createError(this.getTypesAsString(isFunction), token);
  411. }
  412. parseCommandBlock (scope = IVProgParser.FUNCTION) {
  413. let variablesDecl = [];
  414. const commands = [];
  415. this.checkOpenCurly();
  416. this.pos++;
  417. this.consumeNewLines();
  418. while(true) {
  419. const token = this.getToken();
  420. let cmd = null;
  421. if (this.isVariableType(token)) {
  422. if(scope !== IVProgParser.FUNCTION) {
  423. // TODO better error message
  424. throw new Error(`Cannot declare variable here (line ${token.line})`);
  425. }
  426. this.pos++;
  427. variablesDecl = variablesDecl.concat(this.parseDeclararion(token));
  428. this.checkEOS();
  429. this.pos++;
  430. cmd = -1;
  431. } else if (token.type === this.lexerClass.ID) {
  432. cmd = this.parseIDCommand();
  433. } else if (token.type === this.lexerClass.RK_RETURN) {
  434. cmd = this.parseReturn();
  435. } else if (token.type === this.lexerClass.RK_WHILE) {
  436. cmd = this.parseWhile();
  437. } else if (token.type === this.lexerClass.RK_FOR) {
  438. cmd = this.parseFor();
  439. } else if (token.type === this.lexerClass.RK_BREAK ) {
  440. if(scope !== IVProgParser.LOOP) {
  441. // TODO better error message
  442. throw new Error("Break cannot be used outside of a loop.");
  443. }
  444. cmd = this.parseBreak();
  445. } else if (token.type === this.lexerClass.RK_SWITCH) {
  446. } else if (token.type === this.lexerClass.RK_DO) {
  447. } else if (token.type === this.lexerClass.RK_IF) {
  448. }
  449. if (cmd === null)
  450. break;
  451. if(cmd !== -1)
  452. commands.push(cmd);
  453. }
  454. this.consumeNewLines();
  455. this.checkCloseCurly();
  456. this.pos++;
  457. this.consumeNewLines();
  458. return {variables: variablesDecl, commands: commands};
  459. }
  460. parseFor () {
  461. this.pos++;
  462. this.checkOpenParenthesis();
  463. this.pos++;
  464. this.consumeNewLines();
  465. const attribution = this.parseForAttribution();
  466. this.consumeNewLines();
  467. const condition = this.parseExpressionOR();
  468. this.checkEOS();
  469. this.pos++;
  470. const increment = this.parseForAttribution(true);
  471. this.checkCloseParenthesis()
  472. this.pos++;
  473. this.consumeNewLines();
  474. const commandsBlock = this.parseCommandBlock(IVProgParser.LOOP);
  475. return new Commands.For(attribution, condition, increment, commandsBlock);
  476. }
  477. parseWhile () {
  478. this.pos++;
  479. this.checkOpenParenthesis();
  480. this.pos++;
  481. this.consumeNewLines();
  482. const logicalExpression = this.parseExpressionOR();
  483. this.consumeNewLines();
  484. this.checkCloseParenthesis();
  485. this.pos++;
  486. this.consumeNewLines();
  487. const cmdBlocks = this.parseCommandBlock(IVProgParser.LOOP);
  488. return new Commands.While(logicalExpression, cmdBlocks);
  489. }
  490. parseBreak () {
  491. this.pos++;
  492. this.checkEOS();
  493. this.pos++;
  494. return (new Commands.Break());
  495. }
  496. parseReturn () {
  497. this.pos++;
  498. let exp = null;
  499. if(!this.checkEOS(true)) {
  500. const exp = this.parseExpressionOR();
  501. this.checkEOS();
  502. }
  503. this.pos++;
  504. return new Commands.Return(exp);
  505. }
  506. parseIDCommand () {
  507. const id = this.parseID();
  508. const equalOrParenthesis = this.getToken();
  509. if (equalOrParenthesis.type === this.lexerClass.EQUAL) {
  510. this.pos++
  511. const exp = this.parseExpressionOR();
  512. this.checkEOS();
  513. this.pos++;
  514. return (new Commands.Attribution(id, exp));
  515. } else if (equalOrParenthesis.type === this.lexerClass.OPEN_PARENTHESIS) {
  516. const actualParameters = this.parseActualParameters();
  517. this.checkEOS();
  518. this.pos++;
  519. return (new Expressions.FunctionCall(id, actualParameters));
  520. } else {
  521. throw SyntaxError.createError("= or (", equalOrParenthesis);
  522. }
  523. }
  524. parseForAttribution (isLast = false) {
  525. if(!isLast)
  526. this.consumeNewLines();
  527. if(this.checkEOS(true)) {
  528. return null;
  529. }
  530. const id = this.parseID();
  531. const equal = this.getToken();
  532. if (equal.type !== this.lexerClass.EQUAL) {
  533. throw SyntaxError.createError('=', equal);
  534. }
  535. this.pos++
  536. const exp = this.parseExpressionOR();
  537. this.checkEOS();
  538. this.pos++;
  539. return new Commands.Attribution(id, exp);
  540. }
  541. /*
  542. * Parses an Expression following the structure:
  543. *
  544. * EOR => EAnd ( 'or' EOR)? #expression and
  545. *
  546. * EOR => ENot ('and' EOR)? #expression or
  547. *
  548. * ENot => 'not'? ER #expression not
  549. *
  550. * ER => E ((>=, <=, ==, >, <) E)? #expression relational
  551. *
  552. * E => factor ((+, -) E)? #expression
  553. *
  554. * factor=> term ((*, /, %) factor)?
  555. *
  556. * term => literal || arrayAccess || FuncCall || ID || '('EAnd')'
  557. **/
  558. parseExpressionOR () {
  559. const exp1 = this.parseExpressionAND();
  560. let exp2 = null;
  561. let or = null;
  562. const maybeAnd = this.getToken();
  563. if (maybeAnd.type === this.lexerClass.OR_OPERATOR) {
  564. this.pos++;
  565. or = 'or';
  566. this.consumeNewLines();
  567. exp2 = this.parseExpressionOR();
  568. }
  569. return {left: exp1, op:or, right: exp2};
  570. }
  571. parseExpressionAND () {
  572. const exp1 = this.parseExpressionNot();
  573. let and = null;
  574. let exp2 = null;
  575. const andToken = this.getToken();
  576. if (andToken.type === this.lexerClass.AND_OPERATOR) {
  577. this.pos++;
  578. and = 'and';
  579. this.consumeNewLines();
  580. exp2 = this.parseExpressionAND();
  581. }
  582. return {left: exp1, op: and, right: exp2};
  583. }
  584. parseExpressionNot () {
  585. let not = null;
  586. const maybeNotToken = this.getToken();
  587. if (maybeNotToken.type === this.lexerClass.NOT_OPERATOR) {
  588. this.pos++;
  589. not = 'not';
  590. }
  591. const eRel = this.parseExpressionRel();
  592. return {left: null, op: not, right: eRel};
  593. }
  594. parseExpressionRel () {
  595. const exp1 = this.parseExpression();
  596. let rel = null;
  597. let exp2 = null;
  598. const relToken = this.getToken();
  599. if(relToken.type === this.lexerClass.RELATIONAL_OPERATOR) {
  600. this.pos++;
  601. rel = relToken.text; // TODO: source code line/column information
  602. exp2 = this.parseExpression();
  603. }
  604. return {left: exp1, op: rel, right: exp2};
  605. }
  606. parseExpression () {
  607. const factor = this.parseFactor();
  608. let op = null;
  609. let exp = null;
  610. const sumOpToken = this.getToken();
  611. if(sumOpToken.type === this.lexerClass.SUM_OP) {
  612. this.pos++;
  613. op = sumOpToken.text; // TODO: source code line/column information
  614. exp = this.parseExpression();
  615. }
  616. return {left: factor, op: op, right: exp};
  617. }
  618. parseFactor () {
  619. const term = this.parseTerm();
  620. let op = null;
  621. let factor = null;
  622. const multOpToken = this.getToken();
  623. if(multOpToken.type === this.lexerClass.MULTI_OP) {
  624. this.pos++;
  625. op = multOpToken.text; // TODO: source code line/column information
  626. factor = this.parseFactor();
  627. }
  628. return {left: term, op: op, right: factor};
  629. }
  630. parseTerm () {
  631. const token = this.getToken();
  632. switch(token.type) {
  633. case this.lexerClass.INTEGER:
  634. this.pos++;
  635. return this.getIntLiteral(token);
  636. case this.lexerClass.REAL:
  637. this.pos++;
  638. return this.getRealLiteral(token);
  639. case this.lexerClass.STRING:
  640. this.pos++;
  641. return this.getStringLiteral(token);
  642. case this.lexerClass.RK_TRUE:
  643. case this.lexerClass.RK_FALSE:
  644. this.pos++;
  645. return this.getBoolLiteral(token);
  646. case this.lexerClass.OPEN_CURLY:
  647. return this.parseArrayLiteral();
  648. case this.lexerClass.ID:
  649. return this.parseIDTerm();
  650. case this.lexerClass.OPEN_PARENTHESIS:
  651. return this.parseParenthesisExp();
  652. default:
  653. throw SyntaxError.createError('Terminal', token);
  654. }
  655. }
  656. parseIDTerm () {
  657. const id = this.parseID();
  658. const last = this.pos;
  659. if(this.checkOpenBrace(true)) {
  660. this.pos++;
  661. const firstIndex = this.parseExpression();
  662. let secondIndex = null;
  663. this.consumeNewLines();
  664. this.checkCloseBrace();
  665. this.pos++;
  666. if(this.checkOpenBrace(true)){
  667. this.pos++;
  668. secondIndex = this.parseExpression();
  669. this.consumeNewLines();
  670. this.checkCloseBrace();
  671. this.pos++;
  672. } else {
  673. this.pos--;
  674. }
  675. return new Expressions.ArrayAccess(id, firstIndex, secondIndex);
  676. } else if (this.checkOpenParenthesis(true)) {
  677. this.pos++;
  678. this.consumeNewLines();
  679. let actualParameters = [];
  680. if(!this.checkCloseParenthesis(true)) {
  681. actualParameters = this.parseActualParameters();
  682. this.consumeNewLines();
  683. this.checkCloseParenthesis();
  684. this.pos++;
  685. } else {
  686. this.pos++;
  687. }
  688. return new Expressions.FunctionCall(id, actualParameters);
  689. } else {
  690. this.pos = last;
  691. return id;
  692. }
  693. }
  694. parseParenthesisExp () {
  695. this.checkOpenParenthesis();
  696. this.pos++;
  697. this.consumeNewLines();
  698. const exp = this.parseExpressionOR();
  699. this.consumeNewLines();
  700. this.checkCloseParenthesis();
  701. this.pos++;
  702. return exp;
  703. }
  704. parseActualParameters () {
  705. this.checkOpenParenthesis();
  706. this.pos++;
  707. this.consumeNewLines();
  708. list = this.parseExpressionList();
  709. this.consumeNewLines();
  710. this.checkCloseParenthesis();
  711. this.pos++;
  712. return list;
  713. }
  714. parseExpressionList () {
  715. const list = [];
  716. while(true) {
  717. const exp = this.parseExpressionOR();
  718. list.push(exp);
  719. const maybeToken = this.getToken();
  720. if (maybeToken.type !== this.lexerClass.COMMA) {
  721. break;
  722. } else {
  723. this.pos++;
  724. this.consumeNewLines();
  725. }
  726. }
  727. return list;
  728. }
  729. getTypesAsString (isFunction = false) {
  730. const types = isFunction ? this.functionTypes : this.variableTypes;
  731. return types.map( x => this.lexer.literalNames[x])
  732. .reduce((o, n) => {
  733. if (o.length <= 0)
  734. return n;
  735. else
  736. return o + ", " + n;
  737. }, '');
  738. }
  739. }