ivprogParser.js 22 KB

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