ivprogParser.js 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291
  1. import { CommonTokenStream, InputStream } from 'antlr4/index';
  2. import * as Expressions from './expressions/';
  3. import * as Commands from './commands/';
  4. import * as AstHelpers from "./ast_helpers";
  5. import { toInt, toString, toBool, toReal } from './../typeSystem/parsers';
  6. import { Types } from "./../typeSystem/types";
  7. import { CompoundType } from "./../typeSystem/compoundType";
  8. import { SourceInfo } from './sourceInfo';
  9. import { convertFromString } from './operators';
  10. import { SyntaxErrorFactory } from './error/syntaxErrorFactory';
  11. import { LanguageDefinedFunction } from './../processor/definedFunctions';
  12. import { LanguageService } from '../services/languageService';
  13. export class IVProgParser {
  14. static createParser (input) {
  15. const lexerClass = LanguageService.getCurrentLexer();
  16. return new IVProgParser(input, lexerClass);
  17. }
  18. // <BEGIN scope consts>
  19. static get BASE () {
  20. return 0;
  21. }
  22. static get FUNCTION () {
  23. return 1;
  24. }
  25. static get COMMAND () {
  26. return 2;
  27. }
  28. static get BREAKABLE () {
  29. return 4;
  30. }
  31. // </ END scope consts>
  32. constructor (input, lexerClass) {
  33. this.lexerClass = lexerClass;
  34. this.inputStream = new InputStream(input)
  35. this.lexer = new lexerClass(this.inputStream);
  36. this.lexer.recover = AstHelpers.recover.bind(this.lexer);
  37. this.tokenStream = new CommonTokenStream(this.lexer);
  38. this.tokenStream.fill();
  39. this.pos = 1;
  40. this.variableTypes = [this.lexerClass.RK_INTEGER,
  41. this.lexerClass.RK_REAL,
  42. this.lexerClass.RK_BOOLEAN,
  43. this.lexerClass.RK_STRING
  44. ];
  45. this.functionTypes = this.variableTypes.concat(this.lexerClass.RK_VOID);
  46. this.parsingArrayDimension = 0;
  47. this.scope = [];
  48. this.langFuncs = LanguageService.getCurrentLangFuncs();
  49. this.definedFuncsNameList = [];
  50. this.definedVariablesStack = [];
  51. }
  52. parseTree () {
  53. return this.parseProgram();
  54. }
  55. getToken (index = this.pos) {
  56. // if(index === null)
  57. // index = this.pos;
  58. return this.tokenStream.LT(index);
  59. }
  60. insideScope (scope) {
  61. if(this.scope.length <= 0) {
  62. return IVProgParser.BASE === scope;
  63. } else {
  64. return this.scope[this.scope.length-1] === scope;
  65. }
  66. }
  67. pushScope (scope) {
  68. this.scope.push(scope);
  69. }
  70. pushVariableStack () {
  71. this.definedVariablesStack.push([]);
  72. }
  73. popScope () {
  74. return this.scope.pop();
  75. }
  76. popVariableStack () {
  77. return this.definedVariablesStack.pop();
  78. }
  79. getCurrentVariableStack () {
  80. return this.definedVariablesStack[this.definedVariablesStack.length - 1];
  81. }
  82. isEOF () {
  83. this.getToken(this.pos);
  84. return this.tokenStream.fetchedEOF;
  85. }
  86. parseProgram () {
  87. this.consumeNewLines();
  88. const token = this.getToken();
  89. let globalVars = [];
  90. let functions = [];
  91. if(this.lexerClass.RK_PROGRAM === token.type) {
  92. this.pos++;
  93. this.consumeNewLines();
  94. this.checkOpenCurly();
  95. this.pos++;
  96. this.pushVariableStack();
  97. while(true) {
  98. this.consumeNewLines();
  99. const token = this.getToken();
  100. if (token.type === this.lexerClass.RK_CONST || this.isVariableType(token)) {
  101. globalVars = globalVars.concat(this.parseGlobalVariables());
  102. } else if (token.type === this.lexerClass.RK_FUNCTION) {
  103. this.pushVariableStack();
  104. functions = functions.concat(this.parseFunction());
  105. this.popVariableStack();
  106. } else {
  107. break;
  108. }
  109. }
  110. this.consumeNewLines();
  111. this.checkCloseCurly();
  112. this.pos++;
  113. this.consumeNewLines();
  114. if(!this.isEOF()) {
  115. throw SyntaxErrorFactory.extra_lines();
  116. }
  117. this.popVariableStack();
  118. return {global: globalVars, functions: functions};
  119. } else {
  120. throw SyntaxErrorFactory.token_missing_one(this.lexer.literalNames[this.lexerClass.RK_PROGRAM], token);
  121. }
  122. }
  123. checkOpenCurly (attempt = false) {
  124. const token = this.getToken();
  125. if(this.lexerClass.OPEN_CURLY !== token.type){
  126. if(!attempt)
  127. throw SyntaxErrorFactory.token_missing_one('{', token);
  128. else
  129. return false;
  130. }
  131. return true;
  132. }
  133. checkCloseCurly (attempt = false) {
  134. const token = this.getToken();
  135. if(this.lexerClass.CLOSE_CURLY !== token.type){
  136. if(!attempt)
  137. throw SyntaxErrorFactory.token_missing_one('}', token);
  138. else
  139. return false;
  140. }
  141. return true;
  142. }
  143. /* It checks if the current token at position pos is a ']'.
  144. * As a check function it doesn't increment pos.
  145. *
  146. * @params bool:attempt, indicates that the token is optional. Defaults: false
  147. *
  148. * @returns true if the attempt is true and current token is '[',
  149. * false is attempt is true and current token is not '['
  150. **/
  151. checkOpenBrace (attempt = false) {
  152. const token = this.getToken();
  153. if(this.lexerClass.OPEN_BRACE !== token.type){
  154. if (!attempt) {
  155. throw SyntaxErrorFactory.token_missing_one('[', token);
  156. } else {
  157. return false;
  158. }
  159. }
  160. return true;
  161. }
  162. checkCloseBrace (attempt = false) {
  163. const token = this.getToken();
  164. if(this.lexerClass.CLOSE_BRACE !== token.type){
  165. if (!attempt) {
  166. throw SyntaxErrorFactory.token_missing_one(']', token);
  167. } else {
  168. return false;
  169. }
  170. }
  171. return true;
  172. }
  173. checkOpenParenthesis (attempt = false) {
  174. const token = this.getToken();
  175. if(this.lexerClass.OPEN_PARENTHESIS !== token.type){
  176. if (!attempt) {
  177. throw SyntaxErrorFactory.token_missing_one('(', token);
  178. } else {
  179. return false;
  180. }
  181. }
  182. return true;
  183. }
  184. checkCloseParenthesis (attempt = false) {
  185. const token = this.getToken();
  186. if(this.lexerClass.CLOSE_PARENTHESIS !== token.type){
  187. if (!attempt) {
  188. throw SyntaxErrorFactory.token_missing_one(')', token);
  189. } else {
  190. return false;
  191. }
  192. }
  193. return true;
  194. }
  195. checkEOS (attempt = false) {
  196. const eosToken = this.getToken();
  197. if (eosToken.type !== this.lexerClass.EOS) {
  198. if (!attempt)
  199. throw SyntaxErrorFactory.eos_missing(eosToken);
  200. else
  201. return false;
  202. }
  203. return true;
  204. }
  205. checkFunctionDuplicate (functionID, funcIDToken) {
  206. const id = functionID === null ? "$main" : functionID;
  207. const index = this.definedFuncsNameList.indexOf(id);
  208. if(index !== -1) {
  209. throw SyntaxErrorFactory.duplicate_function(funcIDToken);
  210. }
  211. this.definedFuncsNameList.push(id);
  212. }
  213. checkVariableDuplicate (variableID, sourceInfo) {
  214. const index = this.getCurrentVariableStack().indexOf(variableID);
  215. if(index !== -1) {
  216. throw SyntaxErrorFactory.duplicate_variable(sourceInfo);
  217. }
  218. this.getCurrentVariableStack().push(variableID);
  219. }
  220. consumeForSemiColon () {
  221. const eosToken = this.getToken();
  222. if (eosToken.type === this.lexerClass.EOS && eosToken.text.match(';')) {
  223. this.pos++;
  224. return;
  225. }
  226. throw SyntaxErrorFactory.token_missing_one(';', eosToken);
  227. }
  228. parseGlobalVariables () {
  229. const decl = this.parseMaybeConst();
  230. this.checkEOS();
  231. this.pos++;
  232. return decl;
  233. }
  234. /*
  235. * Checks if the next token is PR_CONST. It's only available
  236. * at global variables declaration level
  237. * @returns Declararion(const, type, id, initVal?)
  238. **/
  239. parseMaybeConst () {
  240. const constToken = this.getToken();
  241. if(constToken.type === this.lexerClass.RK_CONST) {
  242. this.pos++;
  243. const typeString = this.parseType();
  244. return this.parseDeclaration(typeString, true);
  245. } else if(this.isVariableType(constToken)) {
  246. const typeString = this.parseType();
  247. return this.parseDeclaration(typeString);
  248. } else {
  249. throw SyntaxErrorFactory.token_missing_list(
  250. [this.lexer.literalNames[this.lexerClass.RK_CONST]].concat(this.getTypeArray()), constToken);
  251. }
  252. }
  253. /*
  254. * Parses a declarion of the form: type --- id --- (= --- EAnd)?
  255. * @returns a list of Declararion(const, type, id, initVal?)
  256. **/
  257. parseDeclaration (typeString, isConst = false) {
  258. let initial = null;
  259. let dim1 = null;
  260. let dim2 = null;
  261. let dimensions = 0;
  262. const sourceInfo = SourceInfo.createSourceInfo(this.getToken())
  263. const idString = this.parseID();
  264. this.checkVariableDuplicate(idString, sourceInfo);
  265. // Check for array or vector
  266. // ID[int/IDi?][int/IDj?]
  267. if (this.checkOpenBrace(true)) {
  268. this.pos += 1;
  269. this.consumeNewLines();
  270. dim1 = this.parseArrayDimension();
  271. this.consumeNewLines();
  272. this.checkCloseBrace();
  273. this.pos += 1;
  274. dimensions += 1
  275. if(this.checkOpenBrace(true)) {
  276. this.pos += 1;
  277. this.consumeNewLines();
  278. dim2 = this.parseArrayDimension();
  279. this.consumeNewLines();
  280. this.checkCloseBrace();
  281. this.pos += 1;
  282. dimensions += 1
  283. }
  284. return this.parseArrayDeclaration(typeString, isConst, idString, sourceInfo, dimensions, dim1, dim2);
  285. } else {
  286. const equalsToken = this.getToken();
  287. if(isConst && equalsToken.type !== this.lexerClass.EQUAL ) {
  288. throw SyntaxErrorFactory.const_not_init(sourceInfo);
  289. }
  290. if(equalsToken.type === this.lexerClass.EQUAL) {
  291. this.pos++;
  292. initial = this.parseExpressionOR();
  293. }
  294. let declaration = declaration = new Commands.Declaration(idString, typeString, initial, isConst);
  295. declaration.sourceInfo = sourceInfo;
  296. const commaToken = this.getToken();
  297. if(commaToken.type === this.lexerClass.COMMA) {
  298. this.pos++;
  299. this.consumeNewLines();
  300. return [declaration]
  301. .concat(this.parseDeclaration(typeString, isConst));
  302. } else {
  303. return [declaration]
  304. }
  305. }
  306. }
  307. parseArrayDeclaration (typeString, isConst, idString, sourceInfo, dimensions, dim1, dim2) {
  308. const equalsToken = this.getToken();
  309. let n_lines = dim1;
  310. let n_columns = dim2;
  311. let initial = null;
  312. if(isConst && equalsToken.type !== this.lexerClass.EQUAL ) {
  313. throw SyntaxErrorFactory.const_not_init(sourceInfo);
  314. }
  315. if(equalsToken.type === this.lexerClass.EQUAL) {
  316. this.pos += 1
  317. initial = this.parseArrayLiteral(typeString);
  318. }
  319. if(initial == null && dim1 == null) {
  320. if(dimensions > 1) {
  321. throw SyntaxErrorFactory.cannot_infer_matrix_line(idString, sourceInfo);
  322. }
  323. throw SyntaxErrorFactory.cannot_infer_vector_size(idString, sourceInfo);
  324. }
  325. if(dimensions > 1) {
  326. if(initial == null && dim2 == null) {
  327. throw SyntaxErrorFactory.cannot_infer_matrix_column(idString, sourceInfo);
  328. }
  329. }
  330. if(dim1 == null) {
  331. n_lines = new Expressions.IntLiteral(toInt(initial.lines));
  332. n_lines.sourceInfo = sourceInfo;
  333. }
  334. if (dimensions > 1) {
  335. if(dim2 == null) {
  336. n_columns = new Expressions.IntLiteral(toInt(initial.columns));
  337. n_columns.sourceInfo = sourceInfo;
  338. }
  339. }
  340. const declaration = new Commands.ArrayDeclaration(idString,
  341. new CompoundType(typeString, dimensions), n_lines, n_columns, initial, isConst);
  342. declaration.sourceInfo = sourceInfo;
  343. const commaToken = this.getToken();
  344. if(commaToken.type === this.lexerClass.COMMA) {
  345. this.pos++;
  346. this.consumeNewLines();
  347. return [declaration]
  348. .concat(this.parseDeclaration(typeString, isConst));
  349. } else {
  350. return [declaration]
  351. }
  352. }
  353. consumeNewLines () {
  354. let token = this.getToken();
  355. while(token.type === this.lexerClass.EOS && token.text.match('[\r\n]+')) {
  356. this.pos++;
  357. token = this.getToken();
  358. }
  359. }
  360. isVariableType (token) {
  361. return this.variableTypes.find(v => v === token.type);
  362. }
  363. /*
  364. * Reads the next token of the stream to check if it is a Integer or an ID.
  365. * @returns Integer | ID
  366. **/
  367. parseArrayDimension () {
  368. const dimToken = this.getToken();
  369. if(dimToken.type === this.lexerClass.INTEGER) {
  370. //parse as int literal
  371. this.pos++;
  372. return this.getIntLiteral(dimToken);
  373. } else if(dimToken.type === this.lexerClass.ID) {
  374. //parse as variable
  375. this.pos++;
  376. return this.parseVariable(dimToken);
  377. } else if(dimToken.type === this.lexerClass.CLOSE_BRACE) {
  378. return null;
  379. } else {
  380. throw SyntaxErrorFactory.invalid_array_dimension(this.lexer.literalNames[this.lexerClass.RK_INTEGER], dimToken);
  381. }
  382. }
  383. /*
  384. * Returns an object {type: 'int', value: value}.
  385. * It checks for binary and hexadecimal integers.
  386. * @returns object with fields type and value
  387. **/
  388. getIntLiteral (token) {
  389. const text = token.text;
  390. const sourceInfo = SourceInfo.createSourceInfo(token);
  391. const exp = new Expressions.IntLiteral(toInt(text));
  392. exp.sourceInfo = sourceInfo;
  393. return exp;
  394. }
  395. getRealLiteral (token) {
  396. const sourceInfo = SourceInfo.createSourceInfo(token);
  397. const exp = new Expressions.RealLiteral(toReal(token.text));
  398. exp.sourceInfo = sourceInfo;
  399. return exp;
  400. }
  401. getStringLiteral (token) {
  402. const text = token.text;
  403. const sourceInfo = SourceInfo.createSourceInfo(token);
  404. const exp = new Expressions.StringLiteral(toString(text));
  405. exp.sourceInfo = sourceInfo;
  406. return exp;
  407. }
  408. getBoolLiteral (token) {
  409. const val = toBool(token.text);
  410. const exp = new Expressions.BoolLiteral(val);
  411. exp.sourceInfo = SourceInfo.createSourceInfo(token);;
  412. return exp;
  413. }
  414. parseArrayLiteral (typeString) {
  415. this.checkOpenCurly();
  416. const beginArray = this.getToken();
  417. if (this.parsingArrayDimension >= 2) {
  418. // TODO better message
  419. throw SyntaxErrorFactory.token_missing_list(`Array dimensions exceed maximum size of 2 at line ${beginArray.line}`);
  420. }
  421. this.pos += 1;
  422. this.parsingArrayDimension += 1;
  423. this.consumeNewLines();
  424. let data = null;
  425. const maybeCurlyOpen = this.checkOpenCurly(true);
  426. if(maybeCurlyOpen) {
  427. // This is potentially a list of vectors
  428. data = this.parseVectorList(typeString);
  429. } else {
  430. data = this.parseExpressionList();
  431. }
  432. this.consumeNewLines();
  433. this.checkCloseCurly()
  434. const endArray = this.getToken();
  435. this.pos += 1;
  436. this.parsingArrayDimension -= 1;
  437. const sourceInfo = SourceInfo.createSourceInfoFromList(beginArray, endArray);
  438. let dataDim = 1;
  439. if(data[0] instanceof Expressions.ArrayLiteral) {
  440. dataDim += 1;
  441. }
  442. const type = new CompoundType(typeString, dataDim);
  443. const exp = new Expressions.ArrayLiteral(type, data);
  444. exp.sourceInfo = sourceInfo;
  445. return exp;
  446. }
  447. /**
  448. * Returns a list of ArrayLiterals. Helper function for parsing matrices
  449. */
  450. parseVectorList (typeString) {
  451. let list = [];
  452. let lastSize = null;
  453. while(true) {
  454. this.checkOpenCurly();
  455. const beginArray = this.getToken();
  456. if (this.parsingArrayDimension >= 2) {
  457. // TODO better message
  458. throw SyntaxErrorFactory.token_missing_list(`Array dimensions exceed maximum size of 2 at line ${beginArray.line}`);
  459. }
  460. this.pos += 1;
  461. this.parsingArrayDimension += 1;
  462. this.consumeNewLines();
  463. const data = this.parseExpressionList();
  464. this.consumeNewLines();
  465. this.checkCloseCurly()
  466. const endArray = this.getToken();
  467. this.pos += 1;
  468. this.parsingArrayDimension -= 1;
  469. const sourceInfo = SourceInfo.createSourceInfoFromList(beginArray, endArray);
  470. if(lastSize == null) {
  471. lastSize = data.length;
  472. } else if (lastSize !== data.length) {
  473. const expString = this.inputStream.getText(beginArray.start, endArray.stop);
  474. throw SyntaxErrorFactory.invalid_matrix_literal_line(expString, sourceInfo);
  475. }
  476. const type = new CompoundType(typeString, 1);
  477. const exp = new Expressions.ArrayLiteral(type, data);
  478. exp.sourceInfo = sourceInfo;
  479. list.push(exp);
  480. const commaToken = this.getToken();
  481. if(commaToken.type !== this.lexerClass.COMMA) {
  482. break;
  483. }
  484. this.pos += 1;
  485. this.consumeNewLines();
  486. }
  487. return list
  488. }
  489. /*
  490. * Returns an object {type: 'variable', value: value}.
  491. * @returns object with fields type and value
  492. **/
  493. parseVariable (token) {
  494. const sourceInfo = SourceInfo.createSourceInfo(token);
  495. const exp = new Expressions.VariableLiteral(token.text);
  496. exp.sourceInfo = sourceInfo;
  497. return exp;
  498. }
  499. /*
  500. * Returns an object representing a function. It has
  501. * four attributes: returnType, id, formalParams and block.
  502. * The block object has two attributes: declarations and commands
  503. **/
  504. parseFunction () {
  505. this.pushScope(IVProgParser.FUNCTION);
  506. let formalParams = [];
  507. const token = this.getToken();
  508. if(token.type !== this.lexerClass.RK_FUNCTION) {
  509. //throw SyntaxError.createError(this.lexer.literalNames[this.lexerClass.PR_FUNCAO], token);
  510. return null;
  511. }
  512. this.pos++;
  513. const funType = this.parseType();
  514. let dimensions = 0;
  515. if(this.checkOpenBrace(true)) {
  516. this.pos++;
  517. this.checkCloseBrace();
  518. this.pos++;
  519. dimensions++;
  520. if(this.checkOpenBrace(true)) {
  521. this.pos++;
  522. this.checkCloseBrace();
  523. this.pos++;
  524. dimensions++;
  525. }
  526. }
  527. const funcIDToken = this.getToken();
  528. const functionID = this.parseID();
  529. this.checkFunctionDuplicate(functionID, funcIDToken);
  530. this.checkOpenParenthesis();
  531. this.pos++;
  532. this.consumeNewLines();
  533. if (!this.checkCloseParenthesis(true)) {
  534. formalParams = this.parseFormalParameters(); // formal parameters
  535. this.consumeNewLines();
  536. this.checkCloseParenthesis();
  537. this.pos++;
  538. } else {
  539. this.pos++;
  540. }
  541. this.consumeNewLines();
  542. const commandsBlock = this.parseCommandBlock();
  543. let returnType = funType;
  544. if(dimensions > 0) {
  545. returnType = new CompoundType(funType, dimensions);
  546. }
  547. const func = new Commands.Function(functionID, returnType, formalParams, commandsBlock);
  548. if (functionID === null && !func.isMain) {
  549. throw SyntaxErrorFactory.invalid_main_return(LanguageDefinedFunction.getMainFunctionName(),
  550. this.lexer.literalNames[this.lexerClass.RK_VOID],
  551. token.line);
  552. } else if (func.isMain && formalParams.length !== 0) {
  553. throw SyntaxErrorFactory.main_parameters();
  554. }
  555. this.popScope();
  556. return func;
  557. }
  558. /*
  559. * Parse the formal parameters of a function.
  560. * @returns a list of objects with the following attributes: type, id and dimensions.
  561. **/
  562. parseFormalParameters () {
  563. const list = [];
  564. while(true) {
  565. let dimensions = 0;
  566. const typeString = this.parseType();
  567. const idToken = this.getToken();
  568. const idString = this.parseID();
  569. this.checkVariableDuplicate(idString, idToken);
  570. if (this.checkOpenBrace(true)) {
  571. this.pos++;
  572. dimensions++;
  573. this.checkCloseBrace();
  574. this.pos++;
  575. if(this.checkOpenBrace(true)) {
  576. this.pos++;
  577. dimensions++;
  578. this.checkCloseBrace();
  579. this.pos++;
  580. }
  581. }
  582. let type = null;
  583. if(dimensions > 0) {
  584. type = new CompoundType(typeString, dimensions);
  585. } else {
  586. type = typeString;
  587. }
  588. list.push(new Commands.FormalParameter(type, idString));
  589. const commaToken = this.getToken();
  590. if (commaToken.type !== this.lexerClass.COMMA)
  591. break;
  592. this.pos++;
  593. this.consumeNewLines();
  594. }
  595. return list;
  596. }
  597. parseID () {
  598. const token = this.getToken();
  599. if(token.type !== this.lexerClass.ID) {
  600. throw SyntaxErrorFactory.id_missing(token);
  601. }
  602. this.pos++;
  603. if (this.insideScope(IVProgParser.FUNCTION)) {
  604. if (token.text === LanguageDefinedFunction.getMainFunctionName()){
  605. return null;
  606. }
  607. }
  608. return token.text;
  609. }
  610. parseMaybeLibID () {
  611. const token = this.getToken();
  612. if(token.type !== this.lexerClass.ID && token.type !== this.lexerClass.LIB_ID) {
  613. throw SyntaxErrorFactory.id_missing(token);
  614. }
  615. this.pos++;
  616. return token.text;
  617. }
  618. parseType () {
  619. const token = this.getToken();
  620. if(token.type === this.lexerClass.ID && this.insideScope(IVProgParser.FUNCTION)) {
  621. return Types.VOID;
  622. } else if (token.type === this.lexerClass.RK_VOID && this.insideScope(IVProgParser.FUNCTION)) {
  623. this.pos++;
  624. return Types.VOID;
  625. } else if (this.isVariableType(token)) {
  626. this.pos++;
  627. switch(token.type) {
  628. case this.lexerClass.RK_INTEGER:
  629. return Types.INTEGER;
  630. case this.lexerClass.RK_BOOLEAN:
  631. return Types.BOOLEAN;
  632. case this.lexerClass.RK_REAL:
  633. return Types.REAL;
  634. case this.lexerClass.RK_STRING:
  635. return Types.STRING;
  636. default:
  637. break;
  638. }
  639. }
  640. throw SyntaxErrorFactory.invalid_type(this.getTypeArray(), token);
  641. }
  642. parseCommandBlock (optionalCurly = false) {
  643. let variablesDecl = [];
  644. const commands = [];
  645. let hasOpen = false;
  646. if (this.checkOpenCurly(optionalCurly)) {
  647. this.pos++;
  648. hasOpen = true;
  649. }
  650. this.consumeNewLines();
  651. let parsedCommand = false;
  652. while(true) {
  653. const cmd = this.parseCommand();
  654. if (cmd === null)
  655. break;
  656. if(cmd !== -1) {
  657. if (cmd instanceof Array) {
  658. if(parsedCommand) {
  659. const lastToken = this.getToken(this.pos - 1);
  660. throw SyntaxErrorFactory.invalid_var_declaration(lastToken);
  661. }
  662. variablesDecl = variablesDecl.concat(cmd);
  663. } else {
  664. parsedCommand = true;
  665. commands.push(cmd);
  666. }
  667. }
  668. }
  669. this.consumeNewLines();
  670. if (hasOpen) {
  671. this.checkCloseCurly()
  672. this.pos++;
  673. this.consumeNewLines();
  674. }
  675. return new Commands.CommandBlock(variablesDecl, commands);
  676. }
  677. parseCommand () {
  678. const token = this.getToken();
  679. if (this.isVariableType(token)) {
  680. if(!this.insideScope(IVProgParser.FUNCTION)) {
  681. throw SyntaxErrorFactory.invalid_var_declaration(token);
  682. }
  683. this.pushScope(IVProgParser.BASE);
  684. const varType = this.parseType();
  685. this.popScope();
  686. const cmd = this.parseDeclaration(varType);
  687. this.checkEOS();
  688. this.pos++;
  689. return cmd;
  690. } else if (token.type === this.lexerClass.ID) {
  691. return this.parseIDCommand();
  692. } else if (token.type === this.lexerClass.LIB_ID) {
  693. return this.parseIDCommand();
  694. } else if (token.type === this.lexerClass.RK_RETURN) {
  695. return this.parseReturn();
  696. } else if (token.type === this.lexerClass.RK_WHILE) {
  697. return this.parseWhile();
  698. } else if (token.type === this.lexerClass.RK_FOR) {
  699. return this.parseFor();
  700. } else if (token.type === this.lexerClass.RK_BREAK ) {
  701. if(!this.insideScope(IVProgParser.BREAKABLE)) {
  702. throw SyntaxErrorFactory.invalid_break_command(
  703. this.lexer.literalNames[this.lexerClass.RK_BREAK],
  704. token
  705. );
  706. }
  707. return this.parseBreak();
  708. } else if (token.type === this.lexerClass.RK_SWITCH) {
  709. return this.parseSwitchCase();
  710. } else if (token.type === this.lexerClass.RK_DO) {
  711. return this.parseDoWhile();
  712. } else if (token.type === this.lexerClass.RK_IF) {
  713. return this.parseIfThenElse();
  714. } else if (this.checkEOS(true)){
  715. this.pos++;
  716. return -1;
  717. } else {
  718. return null;
  719. }
  720. }
  721. parseSwitchCase () {
  722. this.pushScope(IVProgParser.BREAKABLE);
  723. this.pos++;
  724. this.checkOpenParenthesis();
  725. this.pos++;
  726. this.consumeNewLines();
  727. const exp = this.parseExpressionOR();
  728. this.consumeNewLines();
  729. this.checkCloseParenthesis();
  730. this.pos++;
  731. this.consumeNewLines();
  732. this.checkOpenCurly();
  733. this.pos++;
  734. this.consumeNewLines();
  735. const casesList = this.parseCases();
  736. this.consumeNewLines();
  737. this.checkCloseCurly();
  738. this.pos++;
  739. this.consumeNewLines();
  740. this.popScope();
  741. return new Commands.Switch(exp, casesList);
  742. }
  743. parseDoWhile () {
  744. this.pos++;
  745. this.consumeNewLines();
  746. this.pushScope(IVProgParser.BREAKABLE);
  747. const commandsBlock = this.parseCommandBlock();
  748. this.consumeNewLines(); //Maybe not...
  749. const whileToken = this.getToken();
  750. if (whileToken.type !== this.lexerClass.RK_WHILE) {
  751. throw SyntaxErrorFactory.token_missing_one(this.lexer.literalNames[this.lexerClass.RK_WHILE], whileToken);
  752. }
  753. this.pos++;
  754. this.checkOpenParenthesis();
  755. this.pos++;
  756. this.consumeNewLines();
  757. const condition = this.parseExpressionOR();
  758. this.consumeNewLines();
  759. this.checkCloseParenthesis();
  760. this.pos++;
  761. this.checkEOS();
  762. this.popScope();
  763. return new Commands.DoWhile(condition, commandsBlock);
  764. }
  765. parseIfThenElse () {
  766. if(this.insideScope(IVProgParser.BREAKABLE)) {
  767. this.pushScope(IVProgParser.BREAKABLE);
  768. } else {
  769. this.pushScope(IVProgParser.COMMAND);
  770. }
  771. const token = this.getToken();
  772. this.pos++;
  773. this.checkOpenParenthesis();
  774. this.pos++;
  775. this.consumeNewLines();
  776. const logicalExpression = this.parseExpressionOR();
  777. this.consumeNewLines();
  778. this.checkCloseParenthesis();
  779. this.pos++;
  780. this.consumeNewLines();
  781. const cmdBlocks = this.parseCommandBlock();
  782. const maybeElse = this.getToken();
  783. if(maybeElse.type === this.lexerClass.RK_ELSE) {
  784. this.pos++;
  785. this.consumeNewLines();
  786. const maybeIf = this.getToken();
  787. let elseBlock = null;
  788. if(this.checkOpenCurly(true)) {
  789. elseBlock = this.parseCommandBlock();
  790. } else if(maybeIf.type === this.lexerClass.RK_IF) {
  791. elseBlock = this.parseIfThenElse();
  792. } else {
  793. throw SyntaxErrorFactory.token_missing_list([this.lexer.literalNames[this.lexerClass.RK_IF], '{'], maybeIf);
  794. }
  795. this.popScope();
  796. const cmd = new Commands.IfThenElse(logicalExpression, cmdBlocks, elseBlock);
  797. cmd.sourceInfo = SourceInfo.createSourceInfo(token);
  798. return cmd;
  799. }
  800. this.popScope();
  801. const cmd = new Commands.IfThenElse(logicalExpression, cmdBlocks, null);
  802. cmd.sourceInfo = SourceInfo.createSourceInfo(token);
  803. return cmd;
  804. }
  805. parseFor () {
  806. this.pushScope(IVProgParser.BREAKABLE);
  807. this.pos++;
  808. this.checkOpenParenthesis();
  809. this.pos++;
  810. this.consumeNewLines();
  811. const attribution = this.parseForAssign();
  812. this.consumeNewLines();
  813. const condition = this.parseExpressionOR();
  814. this.consumeForSemiColon();
  815. const increment = this.parseForAssign(true);
  816. this.checkCloseParenthesis()
  817. this.pos++;
  818. this.consumeNewLines();
  819. const commandsBlock = this.parseCommandBlock();
  820. this.popScope();
  821. return new Commands.For(attribution, condition, increment, commandsBlock);
  822. }
  823. parseWhile () {
  824. this.pushScope(IVProgParser.BREAKABLE);
  825. const token = this.getToken();
  826. this.pos++;
  827. this.checkOpenParenthesis();
  828. this.pos++;
  829. this.consumeNewLines();
  830. const logicalExpression = this.parseExpressionOR();
  831. this.consumeNewLines();
  832. this.checkCloseParenthesis();
  833. this.pos++;
  834. this.consumeNewLines();
  835. const cmdBlocks = this.parseCommandBlock();
  836. this.popScope();
  837. const cmd = new Commands.While(logicalExpression, cmdBlocks);
  838. cmd.sourceInfo = SourceInfo.createSourceInfo(token);
  839. return cmd;
  840. }
  841. parseBreak () {
  842. this.pos++;
  843. this.checkEOS();
  844. this.pos++;
  845. return new Commands.Break();
  846. }
  847. parseReturn () {
  848. this.pos++;
  849. let exp = null;
  850. if(!this.checkEOS(true)) {
  851. exp = this.parseExpressionOR();
  852. this.checkEOS();
  853. }
  854. this.pos++;
  855. return new Commands.Return(exp);
  856. }
  857. parseIDCommand () {
  858. const refToken = this.getToken();
  859. const isID = refToken.type === this.lexerClass.ID;
  860. const id = this.parseMaybeLibID();
  861. if(this.checkOpenBrace(true)) {
  862. this.pos++;
  863. let lineExpression = null;
  864. let columnExpression = null;
  865. this.consumeNewLines();
  866. lineExpression = this.parseExpression()
  867. this.consumeNewLines();
  868. this.checkCloseBrace();
  869. this.pos++;
  870. if (this.checkOpenBrace(true)) {
  871. this.pos++
  872. this.consumeNewLines();
  873. columnExpression = this.parseExpression();
  874. this.consumeNewLines();
  875. this.checkCloseBrace();
  876. this.pos++;
  877. }
  878. const equalToken = this.getToken();
  879. if (equalToken.type !== this.lexerClass.EQUAL) {
  880. throw SyntaxErrorFactory.token_missing_one('=', equalToken);
  881. }
  882. this.pos++;
  883. const exp = this.parseExpressionOR();
  884. this.checkEOS();
  885. this.pos++;
  886. const cmd = new Commands.ArrayIndexAssign(id, lineExpression, columnExpression, exp);
  887. cmd.sourceInfo = SourceInfo.createSourceInfo(equalToken);
  888. return cmd;
  889. }
  890. const equalOrParenthesis = this.getToken();
  891. if (isID && equalOrParenthesis.type === this.lexerClass.EQUAL) {
  892. this.pos++
  893. const exp = this.parseExpressionOR();
  894. this.checkEOS();
  895. this.pos++;
  896. const cmd = new Commands.Assign(id, exp);
  897. cmd.sourceInfo = SourceInfo.createSourceInfo(equalOrParenthesis);
  898. return cmd;
  899. } else if (equalOrParenthesis.type === this.lexerClass.OPEN_PARENTHESIS) {
  900. const funcCall = this.parseFunctionCallCommand(id);
  901. this.checkEOS();
  902. this.pos++;
  903. return funcCall;
  904. } else if (isID) {
  905. throw SyntaxErrorFactory.token_missing_list(['=','('], equalOrParenthesis);
  906. } else {
  907. throw SyntaxErrorFactory.invalid_id_format(refToken);
  908. }
  909. }
  910. parseForAssign (isLast = false) {
  911. if(!isLast)
  912. this.consumeNewLines();
  913. if(this.checkEOS(true)) {
  914. return null;
  915. }
  916. const id = this.parseID();
  917. const equal = this.getToken();
  918. if (equal.type !== this.lexerClass.EQUAL) {
  919. throw SyntaxErrorFactory.token_missing_one('=', equal);
  920. }
  921. this.pos++
  922. const exp = this.parseExpressionOR();
  923. if(!isLast) {
  924. this.consumeForSemiColon();
  925. }
  926. const sourceInfo = SourceInfo.createSourceInfo(equal);
  927. const cmd = new Commands.Assign(id, exp);
  928. cmd.sourceInfo = sourceInfo;
  929. return cmd;
  930. }
  931. parseCases () {
  932. const token = this.getToken();
  933. if(token.type !== this.lexerClass.RK_CASE) {
  934. throw SyntaxErrorFactory.token_missing_one(this.lexer.literalNames[this.lexerClass.RK_CASE], token);
  935. }
  936. this.pos++;
  937. const nextToken = this.getToken();
  938. if(nextToken.type === this.lexerClass.RK_DEFAULT) {
  939. this.pos++;
  940. const colonToken = this.getToken();
  941. if (colonToken.type !== this.lexerClass.COLON) {
  942. throw SyntaxErrorFactory.token_missing_one(':', colonToken);
  943. }
  944. this.pos++;
  945. this.consumeNewLines();
  946. const block = this.parseCommandBlock(true);
  947. const defaultCase = new Commands.Case(null);
  948. defaultCase.setCommands(block.commands);
  949. return [defaultCase];
  950. } else {
  951. const exp = this.parseExpressionOR();
  952. const colonToken = this.getToken();
  953. if (colonToken.type !== this.lexerClass.COLON) {
  954. throw SyntaxErrorFactory.token_missing_one(':', colonToken);
  955. }
  956. this.pos++;
  957. this.consumeNewLines();
  958. const block = this.parseCommandBlock(true);
  959. const aCase = new Commands.Case(exp);
  960. aCase.setCommands(block.commands);
  961. const caseToken = this.getToken();
  962. if(caseToken.type === this.lexerClass.RK_CASE) {
  963. return [aCase].concat(this.parseCases());
  964. } else {
  965. return [aCase];
  966. }
  967. }
  968. }
  969. /*
  970. * Parses an Expression following the structure:
  971. *
  972. * EOR => EAnd ( 'or' EOR)? #expression and
  973. *
  974. * EOR => ENot ('and' EOR)? #expression or
  975. *
  976. * ENot => 'not'? ER #expression not
  977. *
  978. * ER => E ((>=, <=, ==, >, <) E)? #expression relational
  979. *
  980. * E => factor ((+, -) E)? #expression
  981. *
  982. * factor=> term ((*, /, %) factor)?
  983. *
  984. * term => literal || arrayAccess || FuncCall || ID || '('EAnd')'
  985. **/
  986. parseExpressionOR () {
  987. let exp1 = this.parseExpressionAND();
  988. while (this.getToken().type === this.lexerClass.OR_OPERATOR) {
  989. const opToken = this.getToken();
  990. this.pos++;
  991. const or = convertFromString('or');
  992. this.consumeNewLines();
  993. const exp2 = this.parseExpressionAND();
  994. const finalExp = new Expressions.InfixApp(or, exp1, exp2);
  995. finalExp.sourceInfo = SourceInfo.createSourceInfo(opToken);
  996. exp1 = finalExp
  997. }
  998. return exp1;
  999. }
  1000. parseExpressionAND () {
  1001. let exp1 = this.parseExpressionNot();
  1002. while (this.getToken().type === this.lexerClass.AND_OPERATOR) {
  1003. const opToken = this.getToken();
  1004. this.pos++;
  1005. const and = convertFromString('and');
  1006. this.consumeNewLines();
  1007. const exp2 = this.parseExpressionNot();
  1008. const finalExp = new Expressions.InfixApp(and, exp1, exp2);
  1009. finalExp.sourceInfo = SourceInfo.createSourceInfo(opToken);
  1010. exp1 = finalExp;
  1011. }
  1012. return exp1;
  1013. }
  1014. parseExpressionNot () {
  1015. const maybeNotToken = this.getToken();
  1016. if (maybeNotToken.type === this.lexerClass.NOT_OPERATOR) {
  1017. const opToken = this.getToken();
  1018. this.pos++;
  1019. const not = convertFromString('not');
  1020. const exp1 = this.parseExpressionRel();
  1021. const finalExp = new Expressions.UnaryApp(not, exp1);
  1022. finalExp.sourceInfo = SourceInfo.createSourceInfo(opToken);
  1023. return finalExp;
  1024. } else {
  1025. return this.parseExpressionRel();
  1026. }
  1027. }
  1028. parseExpressionRel () {
  1029. let exp1 = this.parseExpression();
  1030. while (this.getToken().type === this.lexerClass.RELATIONAL_OPERATOR) {
  1031. const relToken = this.getToken();
  1032. this.pos++;
  1033. const rel = convertFromString(relToken.text);
  1034. const exp2 = this.parseExpression();
  1035. const finalExp = new Expressions.InfixApp(rel, exp1, exp2);
  1036. finalExp.sourceInfo = SourceInfo.createSourceInfo(relToken);
  1037. exp1 = finalExp;
  1038. }
  1039. return exp1;
  1040. }
  1041. parseExpression () {
  1042. let factor = this.parseFactor();
  1043. while (this.getToken().type === this.lexerClass.SUM_OP) {
  1044. const sumOpToken = this.getToken();
  1045. this.pos++;
  1046. const op = convertFromString(sumOpToken.text);
  1047. const factor2 = this.parseFactor();
  1048. const finalExp = new Expressions.InfixApp(op, factor, factor2);
  1049. finalExp.sourceInfo = SourceInfo.createSourceInfo(sumOpToken);
  1050. factor = finalExp;
  1051. }
  1052. return factor;
  1053. }
  1054. parseFactor () {
  1055. let term = this.parseTerm();
  1056. while (this.getToken().type === this.lexerClass.MULTI_OP) {
  1057. const multOpToken = this.getToken();
  1058. this.pos++;
  1059. const op = convertFromString(multOpToken.text);
  1060. const term2 =this.parseTerm();
  1061. const finalExp = new Expressions.InfixApp(op, term, term2);
  1062. finalExp.sourceInfo = SourceInfo.createSourceInfo(multOpToken);
  1063. term = finalExp;
  1064. }
  1065. return term;
  1066. }
  1067. parseTerm () {
  1068. const token = this.getToken();
  1069. let sourceInfo = null;
  1070. switch(token.type) {
  1071. case this.lexerClass.SUM_OP:
  1072. this.pos++;
  1073. sourceInfo = SourceInfo.createSourceInfo(token);
  1074. const exp = new Expressions.UnaryApp(convertFromString(token.text), this.parseTerm());
  1075. exp.sourceInfo = sourceInfo;
  1076. return exp;
  1077. case this.lexerClass.INTEGER:
  1078. this.pos++;
  1079. return this.getIntLiteral(token);
  1080. case this.lexerClass.REAL:
  1081. this.pos++;
  1082. return this.getRealLiteral(token);
  1083. case this.lexerClass.STRING:
  1084. this.pos++;
  1085. return this.getStringLiteral(token);
  1086. case this.lexerClass.RK_TRUE:
  1087. case this.lexerClass.RK_FALSE:
  1088. this.pos++;
  1089. return this.getBoolLiteral(token);
  1090. case this.lexerClass.OPEN_CURLY:
  1091. // No more annonymous array
  1092. // return this.parseArrayLiteral();
  1093. throw SyntaxErrorFactory.annonymous_array_literal(token);
  1094. case this.lexerClass.ID:
  1095. case this.lexerClass.LIB_ID:
  1096. return this.parseIDTerm();
  1097. case this.lexerClass.OPEN_PARENTHESIS:
  1098. return this.parseParenthesisExp();
  1099. default:
  1100. throw SyntaxErrorFactory.invalid_terminal(token);
  1101. }
  1102. }
  1103. parseIDTerm () {
  1104. const tokenA = this.getToken();
  1105. const id = this.parseMaybeLibID();
  1106. const isID = tokenA.type === this.lexerClass.ID;
  1107. if(isID && this.checkOpenBrace(true)) {
  1108. let tokenB = null;
  1109. this.pos++;
  1110. const firstIndex = this.parseExpression();
  1111. let secondIndex = null;
  1112. this.consumeNewLines();
  1113. this.checkCloseBrace();
  1114. tokenB = this.getToken();
  1115. this.pos++;
  1116. if(this.checkOpenBrace(true)){
  1117. this.pos++;
  1118. secondIndex = this.parseExpression();
  1119. this.consumeNewLines();
  1120. this.checkCloseBrace();
  1121. tokenB = this.getToken();
  1122. this.pos++;
  1123. }
  1124. const sourceInfo = SourceInfo.createSourceInfoFromList(tokenA, tokenB);
  1125. const exp = new Expressions.ArrayAccess(id, firstIndex, secondIndex);
  1126. exp.sourceInfo = sourceInfo;
  1127. return exp;
  1128. } else if (this.checkOpenParenthesis(true)) {
  1129. return this.parseFunctionCallExpression(id);
  1130. } else if (isID) {
  1131. const sourceInfo = SourceInfo.createSourceInfo(tokenA);
  1132. const exp = new Expressions.VariableLiteral(id);
  1133. exp.sourceInfo = sourceInfo;
  1134. return exp;
  1135. } else {
  1136. throw SyntaxErrorFactory.invalid_id_format(tokenA);
  1137. }
  1138. }
  1139. getFunctionName (id) {
  1140. const name = LanguageDefinedFunction.getInternalName(id);
  1141. if (name === null) {
  1142. if (id === LanguageDefinedFunction.getMainFunctionName()) {
  1143. return null;
  1144. }
  1145. return id;
  1146. } else {
  1147. return name;
  1148. }
  1149. }
  1150. parseFunctionCallExpression (id) {
  1151. const tokenA = this.getToken(this.pos - 1);
  1152. const actualParameters = this.parseActualParameters();
  1153. const tokenB = this.getToken(this.pos - 1);
  1154. const funcName = this.getFunctionName(id);
  1155. const sourceInfo = SourceInfo.createSourceInfoFromList(tokenA, tokenB);
  1156. const cmd = new Expressions.FunctionCall(funcName, actualParameters);
  1157. cmd.sourceInfo = sourceInfo;
  1158. return cmd;
  1159. }
  1160. parseFunctionCallCommand (id) {
  1161. return this.parseFunctionCallExpression(id);
  1162. }
  1163. parseParenthesisExp () {
  1164. this.checkOpenParenthesis();
  1165. const tokenA = this.getToken();
  1166. this.pos++;
  1167. this.consumeNewLines();
  1168. const exp = this.parseExpressionOR();
  1169. this.consumeNewLines();
  1170. this.checkCloseParenthesis();
  1171. const tokenB = this.getToken();
  1172. const sourceInfo = SourceInfo.createSourceInfoFromList(tokenA, tokenB);
  1173. this.pos++;
  1174. exp.sourceInfo = sourceInfo;
  1175. return exp;
  1176. }
  1177. parseActualParameters () {
  1178. this.checkOpenParenthesis();
  1179. this.pos++;
  1180. if(this.checkCloseParenthesis(true)) {
  1181. this.pos++;
  1182. return [];
  1183. }
  1184. this.consumeNewLines();
  1185. const list = this.parseExpressionList();
  1186. this.consumeNewLines();
  1187. this.checkCloseParenthesis();
  1188. this.pos++;
  1189. return list;
  1190. }
  1191. parseExpressionList () {
  1192. const list = [];
  1193. while(true) {
  1194. const exp = this.parseExpressionOR();
  1195. list.push(exp);
  1196. const maybeToken = this.getToken();
  1197. if (maybeToken.type !== this.lexerClass.COMMA) {
  1198. break;
  1199. } else {
  1200. this.pos++;
  1201. this.consumeNewLines();
  1202. }
  1203. }
  1204. return list;
  1205. }
  1206. getTypeArray () {
  1207. const types = this.insideScope(IVProgParser.FUNCTION) ? this.functionTypes : this.variableTypes;
  1208. return types.map( x => this.lexer.literalNames[x]);
  1209. }
  1210. }