ivprogParser.js 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293
  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++;
  317. initial = this.parseArrayLiteral(typeString);
  318. }
  319. if(dimensions === 1) {
  320. if(initial == null && dim1 == null) {
  321. // cannot infer dimension
  322. throw new Error("cannot infer dimension 1");
  323. }
  324. } else {
  325. if(initial == null && (dim1 == null || dim2 == null)) {
  326. // cannot infer dimension
  327. throw new Error("cannot infer dimension 2");
  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. throw SyntaxErrorFactory.token_missing_list(`Array dimensions exceed maximum size of 2 at line ${beginArray.line}`);
  419. }
  420. this.pos += 1;
  421. this.parsingArrayDimension += 1;
  422. this.consumeNewLines();
  423. let data = null;
  424. const maybeCurlyOpen = this.checkOpenCurly(true);
  425. if(maybeCurlyOpen) {
  426. // This is potentially a list of vectors
  427. data = this.parseVectorList(typeString);
  428. } else {
  429. data = this.parseExpressionList();
  430. }
  431. this.consumeNewLines();
  432. this.checkCloseCurly()
  433. const endArray = this.getToken();
  434. this.pos += 1;
  435. this.parsingArrayDimension -= 1;
  436. const sourceInfo = SourceInfo.createSourceInfoFromList(beginArray, endArray);
  437. let dataDim = 1;
  438. if(data[0] instanceof Expressions.ArrayLiteral) {
  439. dataDim += 1;
  440. }
  441. const type = new CompoundType(typeString, dataDim);
  442. const exp = new Expressions.ArrayLiteral(type, data);
  443. exp.sourceInfo = sourceInfo;
  444. if(!exp.isValid) {
  445. // invalid array
  446. throw new Error("invalid array");
  447. }
  448. return exp;
  449. }
  450. /**
  451. * Returns a list of ArrayLiterals. Helper function for parsing matrices
  452. */
  453. parseVectorList (typeString) {
  454. let list = [];
  455. let lastSize = null;
  456. while(true) {
  457. this.checkOpenCurly();
  458. const beginArray = this.getToken();
  459. if (this.parsingArrayDimension >= 2) {
  460. throw SyntaxErrorFactory.token_missing_list(`Array dimensions exceed maximum size of 2 at line ${beginArray.line}`);
  461. }
  462. this.pos += 1;
  463. this.parsingArrayDimension += 1;
  464. this.consumeNewLines();
  465. const data = this.parseExpressionList();
  466. this.consumeNewLines();
  467. this.checkCloseCurly()
  468. const endArray = this.getToken();
  469. this.pos += 1;
  470. this.parsingArrayDimension -= 1;
  471. if(lastSize == null) {
  472. lastSize = data.length;
  473. } else if (lastSize !== data.length) {
  474. const expString = this.inputStream.getText(beginArray.start, endArray.stop);
  475. throw new Error("Invalid size: " + expString);
  476. }
  477. const sourceInfo = SourceInfo.createSourceInfoFromList(beginArray, endArray);
  478. const type = new CompoundType(typeString, 1);
  479. const exp = new Expressions.ArrayLiteral(type, data);
  480. exp.sourceInfo = sourceInfo;
  481. list.push(exp);
  482. const commaToken = this.getToken();
  483. if(commaToken.type !== this.lexerClass.COMMA) {
  484. break;
  485. }
  486. this.pos += 1;
  487. this.consumeNewLines();
  488. }
  489. return list
  490. }
  491. /*
  492. * Returns an object {type: 'variable', value: value}.
  493. * @returns object with fields type and value
  494. **/
  495. parseVariable (token) {
  496. const sourceInfo = SourceInfo.createSourceInfo(token);
  497. const exp = new Expressions.VariableLiteral(token.text);
  498. exp.sourceInfo = sourceInfo;
  499. return exp;
  500. }
  501. /*
  502. * Returns an object representing a function. It has
  503. * four attributes: returnType, id, formalParams and block.
  504. * The block object has two attributes: declarations and commands
  505. **/
  506. parseFunction () {
  507. this.pushScope(IVProgParser.FUNCTION);
  508. let formalParams = [];
  509. const token = this.getToken();
  510. if(token.type !== this.lexerClass.RK_FUNCTION) {
  511. //throw SyntaxError.createError(this.lexer.literalNames[this.lexerClass.PR_FUNCAO], token);
  512. return null;
  513. }
  514. this.pos++;
  515. const funType = this.parseType();
  516. let dimensions = 0;
  517. if(this.checkOpenBrace(true)) {
  518. this.pos++;
  519. this.checkCloseBrace();
  520. this.pos++;
  521. dimensions++;
  522. if(this.checkOpenBrace(true)) {
  523. this.pos++;
  524. this.checkCloseBrace();
  525. this.pos++;
  526. dimensions++;
  527. }
  528. }
  529. const funcIDToken = this.getToken();
  530. const functionID = this.parseID();
  531. this.checkFunctionDuplicate(functionID, funcIDToken);
  532. this.checkOpenParenthesis();
  533. this.pos++;
  534. this.consumeNewLines();
  535. if (!this.checkCloseParenthesis(true)) {
  536. formalParams = this.parseFormalParameters(); // formal parameters
  537. this.consumeNewLines();
  538. this.checkCloseParenthesis();
  539. this.pos++;
  540. } else {
  541. this.pos++;
  542. }
  543. this.consumeNewLines();
  544. const commandsBlock = this.parseCommandBlock();
  545. let returnType = funType;
  546. if(dimensions > 0) {
  547. returnType = new CompoundType(funType, dimensions);
  548. }
  549. const func = new Commands.Function(functionID, returnType, formalParams, commandsBlock);
  550. if (functionID === null && !func.isMain) {
  551. throw SyntaxErrorFactory.invalid_main_return(LanguageDefinedFunction.getMainFunctionName(),
  552. this.lexer.literalNames[this.lexerClass.RK_VOID],
  553. token.line);
  554. } else if (func.isMain && formalParams.length !== 0) {
  555. throw SyntaxErrorFactory.main_parameters();
  556. }
  557. this.popScope();
  558. return func;
  559. }
  560. /*
  561. * Parse the formal parameters of a function.
  562. * @returns a list of objects with the following attributes: type, id and dimensions.
  563. **/
  564. parseFormalParameters () {
  565. const list = [];
  566. while(true) {
  567. let dimensions = 0;
  568. const typeString = this.parseType();
  569. const idToken = this.getToken();
  570. const idString = this.parseID();
  571. this.checkVariableDuplicate(idString, idToken);
  572. if (this.checkOpenBrace(true)) {
  573. this.pos++;
  574. dimensions++;
  575. this.checkCloseBrace();
  576. this.pos++;
  577. if(this.checkOpenBrace(true)) {
  578. this.pos++;
  579. dimensions++;
  580. this.checkCloseBrace();
  581. this.pos++;
  582. }
  583. }
  584. let type = null;
  585. if(dimensions > 0) {
  586. type = new CompoundType(typeString, dimensions);
  587. } else {
  588. type = typeString;
  589. }
  590. list.push(new Commands.FormalParameter(type, idString));
  591. const commaToken = this.getToken();
  592. if (commaToken.type !== this.lexerClass.COMMA)
  593. break;
  594. this.pos++;
  595. this.consumeNewLines();
  596. }
  597. return list;
  598. }
  599. parseID () {
  600. const token = this.getToken();
  601. if(token.type !== this.lexerClass.ID) {
  602. throw SyntaxErrorFactory.id_missing(token);
  603. }
  604. this.pos++;
  605. if (this.insideScope(IVProgParser.FUNCTION)) {
  606. if (token.text === LanguageDefinedFunction.getMainFunctionName()){
  607. return null;
  608. }
  609. }
  610. return token.text;
  611. }
  612. parseMaybeLibID () {
  613. const token = this.getToken();
  614. if(token.type !== this.lexerClass.ID && token.type !== this.lexerClass.LIB_ID) {
  615. throw SyntaxErrorFactory.id_missing(token);
  616. }
  617. this.pos++;
  618. return token.text;
  619. }
  620. parseType () {
  621. const token = this.getToken();
  622. if(token.type === this.lexerClass.ID && this.insideScope(IVProgParser.FUNCTION)) {
  623. return Types.VOID;
  624. } else if (token.type === this.lexerClass.RK_VOID && this.insideScope(IVProgParser.FUNCTION)) {
  625. this.pos++;
  626. return Types.VOID;
  627. } else if (this.isVariableType(token)) {
  628. this.pos++;
  629. switch(token.type) {
  630. case this.lexerClass.RK_INTEGER:
  631. return Types.INTEGER;
  632. case this.lexerClass.RK_BOOLEAN:
  633. return Types.BOOLEAN;
  634. case this.lexerClass.RK_REAL:
  635. return Types.REAL;
  636. case this.lexerClass.RK_STRING:
  637. return Types.STRING;
  638. default:
  639. break;
  640. }
  641. }
  642. throw SyntaxErrorFactory.invalid_type(this.getTypeArray(), token);
  643. }
  644. parseCommandBlock (optionalCurly = false) {
  645. let variablesDecl = [];
  646. const commands = [];
  647. let hasOpen = false;
  648. if (this.checkOpenCurly(optionalCurly)) {
  649. this.pos++;
  650. hasOpen = true;
  651. }
  652. this.consumeNewLines();
  653. let parsedCommand = false;
  654. while(true) {
  655. const cmd = this.parseCommand();
  656. if (cmd === null)
  657. break;
  658. if(cmd !== -1) {
  659. if (cmd instanceof Array) {
  660. if(parsedCommand) {
  661. const lastToken = this.getToken(this.pos - 1);
  662. throw SyntaxErrorFactory.invalid_var_declaration(lastToken);
  663. }
  664. variablesDecl = variablesDecl.concat(cmd);
  665. } else {
  666. parsedCommand = true;
  667. commands.push(cmd);
  668. }
  669. }
  670. }
  671. this.consumeNewLines();
  672. if (hasOpen) {
  673. this.checkCloseCurly()
  674. this.pos++;
  675. this.consumeNewLines();
  676. }
  677. return new Commands.CommandBlock(variablesDecl, commands);
  678. }
  679. parseCommand () {
  680. const token = this.getToken();
  681. if (this.isVariableType(token)) {
  682. if(!this.insideScope(IVProgParser.FUNCTION)) {
  683. throw SyntaxErrorFactory.invalid_var_declaration(token);
  684. }
  685. this.pushScope(IVProgParser.BASE);
  686. const varType = this.parseType();
  687. this.popScope();
  688. const cmd = this.parseDeclaration(varType);
  689. this.checkEOS();
  690. this.pos++;
  691. return cmd;
  692. } else if (token.type === this.lexerClass.ID) {
  693. return this.parseIDCommand();
  694. } else if (token.type === this.lexerClass.LIB_ID) {
  695. return this.parseIDCommand();
  696. } else if (token.type === this.lexerClass.RK_RETURN) {
  697. return this.parseReturn();
  698. } else if (token.type === this.lexerClass.RK_WHILE) {
  699. return this.parseWhile();
  700. } else if (token.type === this.lexerClass.RK_FOR) {
  701. return this.parseFor();
  702. } else if (token.type === this.lexerClass.RK_BREAK ) {
  703. if(!this.insideScope(IVProgParser.BREAKABLE)) {
  704. throw SyntaxErrorFactory.invalid_break_command(
  705. this.lexer.literalNames[this.lexerClass.RK_BREAK],
  706. token
  707. );
  708. }
  709. return this.parseBreak();
  710. } else if (token.type === this.lexerClass.RK_SWITCH) {
  711. return this.parseSwitchCase();
  712. } else if (token.type === this.lexerClass.RK_DO) {
  713. return this.parseDoWhile();
  714. } else if (token.type === this.lexerClass.RK_IF) {
  715. return this.parseIfThenElse();
  716. } else if (this.checkEOS(true)){
  717. this.pos++;
  718. return -1;
  719. } else {
  720. return null;
  721. }
  722. }
  723. parseSwitchCase () {
  724. this.pushScope(IVProgParser.BREAKABLE);
  725. this.pos++;
  726. this.checkOpenParenthesis();
  727. this.pos++;
  728. this.consumeNewLines();
  729. const exp = this.parseExpressionOR();
  730. this.consumeNewLines();
  731. this.checkCloseParenthesis();
  732. this.pos++;
  733. this.consumeNewLines();
  734. this.checkOpenCurly();
  735. this.pos++;
  736. this.consumeNewLines();
  737. const casesList = this.parseCases();
  738. this.consumeNewLines();
  739. this.checkCloseCurly();
  740. this.pos++;
  741. this.consumeNewLines();
  742. this.popScope();
  743. return new Commands.Switch(exp, casesList);
  744. }
  745. parseDoWhile () {
  746. this.pos++;
  747. this.consumeNewLines();
  748. this.pushScope(IVProgParser.BREAKABLE);
  749. const commandsBlock = this.parseCommandBlock();
  750. this.consumeNewLines(); //Maybe not...
  751. const whileToken = this.getToken();
  752. if (whileToken.type !== this.lexerClass.RK_WHILE) {
  753. throw SyntaxErrorFactory.token_missing_one(this.lexer.literalNames[this.lexerClass.RK_WHILE], whileToken);
  754. }
  755. this.pos++;
  756. this.checkOpenParenthesis();
  757. this.pos++;
  758. this.consumeNewLines();
  759. const condition = this.parseExpressionOR();
  760. this.consumeNewLines();
  761. this.checkCloseParenthesis();
  762. this.pos++;
  763. this.checkEOS();
  764. this.popScope();
  765. return new Commands.DoWhile(condition, commandsBlock);
  766. }
  767. parseIfThenElse () {
  768. if(this.insideScope(IVProgParser.BREAKABLE)) {
  769. this.pushScope(IVProgParser.BREAKABLE);
  770. } else {
  771. this.pushScope(IVProgParser.COMMAND);
  772. }
  773. const token = this.getToken();
  774. this.pos++;
  775. this.checkOpenParenthesis();
  776. this.pos++;
  777. this.consumeNewLines();
  778. const logicalExpression = this.parseExpressionOR();
  779. this.consumeNewLines();
  780. this.checkCloseParenthesis();
  781. this.pos++;
  782. this.consumeNewLines();
  783. const cmdBlocks = this.parseCommandBlock();
  784. const maybeElse = this.getToken();
  785. if(maybeElse.type === this.lexerClass.RK_ELSE) {
  786. this.pos++;
  787. this.consumeNewLines();
  788. const maybeIf = this.getToken();
  789. let elseBlock = null;
  790. if(this.checkOpenCurly(true)) {
  791. elseBlock = this.parseCommandBlock();
  792. } else if(maybeIf.type === this.lexerClass.RK_IF) {
  793. elseBlock = this.parseIfThenElse();
  794. } else {
  795. throw SyntaxErrorFactory.token_missing_list([this.lexer.literalNames[this.lexerClass.RK_IF], '{'], maybeIf);
  796. }
  797. this.popScope();
  798. const cmd = new Commands.IfThenElse(logicalExpression, cmdBlocks, elseBlock);
  799. cmd.sourceInfo = SourceInfo.createSourceInfo(token);
  800. return cmd;
  801. }
  802. this.popScope();
  803. const cmd = new Commands.IfThenElse(logicalExpression, cmdBlocks, null);
  804. cmd.sourceInfo = SourceInfo.createSourceInfo(token);
  805. return cmd;
  806. }
  807. parseFor () {
  808. this.pushScope(IVProgParser.BREAKABLE);
  809. this.pos++;
  810. this.checkOpenParenthesis();
  811. this.pos++;
  812. this.consumeNewLines();
  813. const attribution = this.parseForAssign();
  814. this.consumeNewLines();
  815. const condition = this.parseExpressionOR();
  816. this.consumeForSemiColon();
  817. const increment = this.parseForAssign(true);
  818. this.checkCloseParenthesis()
  819. this.pos++;
  820. this.consumeNewLines();
  821. const commandsBlock = this.parseCommandBlock();
  822. this.popScope();
  823. return new Commands.For(attribution, condition, increment, commandsBlock);
  824. }
  825. parseWhile () {
  826. this.pushScope(IVProgParser.BREAKABLE);
  827. const token = this.getToken();
  828. this.pos++;
  829. this.checkOpenParenthesis();
  830. this.pos++;
  831. this.consumeNewLines();
  832. const logicalExpression = this.parseExpressionOR();
  833. this.consumeNewLines();
  834. this.checkCloseParenthesis();
  835. this.pos++;
  836. this.consumeNewLines();
  837. const cmdBlocks = this.parseCommandBlock();
  838. this.popScope();
  839. const cmd = new Commands.While(logicalExpression, cmdBlocks);
  840. cmd.sourceInfo = SourceInfo.createSourceInfo(token);
  841. return cmd;
  842. }
  843. parseBreak () {
  844. this.pos++;
  845. this.checkEOS();
  846. this.pos++;
  847. return new Commands.Break();
  848. }
  849. parseReturn () {
  850. this.pos++;
  851. let exp = null;
  852. if(!this.checkEOS(true)) {
  853. exp = this.parseExpressionOR();
  854. this.checkEOS();
  855. }
  856. this.pos++;
  857. return new Commands.Return(exp);
  858. }
  859. parseIDCommand () {
  860. const refToken = this.getToken();
  861. const isID = refToken.type === this.lexerClass.ID;
  862. const id = this.parseMaybeLibID();
  863. if(this.checkOpenBrace(true)) {
  864. this.pos++;
  865. let lineExpression = null;
  866. let columnExpression = null;
  867. this.consumeNewLines();
  868. lineExpression = this.parseExpression()
  869. this.consumeNewLines();
  870. this.checkCloseBrace();
  871. this.pos++;
  872. if (this.checkOpenBrace(true)) {
  873. this.pos++
  874. this.consumeNewLines();
  875. columnExpression = this.parseExpression();
  876. this.consumeNewLines();
  877. this.checkCloseBrace();
  878. this.pos++;
  879. }
  880. const equalToken = this.getToken();
  881. if (equalToken.type !== this.lexerClass.EQUAL) {
  882. throw SyntaxErrorFactory.token_missing_one('=', equalToken);
  883. }
  884. this.pos++;
  885. const exp = this.parseExpressionOR();
  886. this.checkEOS();
  887. this.pos++;
  888. const cmd = new Commands.ArrayIndexAssign(id, lineExpression, columnExpression, exp);
  889. cmd.sourceInfo = SourceInfo.createSourceInfo(equalToken);
  890. return cmd;
  891. }
  892. const equalOrParenthesis = this.getToken();
  893. if (isID && equalOrParenthesis.type === this.lexerClass.EQUAL) {
  894. this.pos++
  895. const exp = this.parseExpressionOR();
  896. this.checkEOS();
  897. this.pos++;
  898. const cmd = new Commands.Assign(id, exp);
  899. cmd.sourceInfo = SourceInfo.createSourceInfo(equalOrParenthesis);
  900. return cmd;
  901. } else if (equalOrParenthesis.type === this.lexerClass.OPEN_PARENTHESIS) {
  902. const funcCall = this.parseFunctionCallCommand(id);
  903. this.checkEOS();
  904. this.pos++;
  905. return funcCall;
  906. } else if (isID) {
  907. throw SyntaxErrorFactory.token_missing_list(['=','('], equalOrParenthesis);
  908. } else {
  909. throw SyntaxErrorFactory.invalid_id_format(refToken);
  910. }
  911. }
  912. parseForAssign (isLast = false) {
  913. if(!isLast)
  914. this.consumeNewLines();
  915. if(this.checkEOS(true)) {
  916. return null;
  917. }
  918. const id = this.parseID();
  919. const equal = this.getToken();
  920. if (equal.type !== this.lexerClass.EQUAL) {
  921. throw SyntaxErrorFactory.token_missing_one('=', equal);
  922. }
  923. this.pos++
  924. const exp = this.parseExpressionOR();
  925. if(!isLast) {
  926. this.consumeForSemiColon();
  927. }
  928. const sourceInfo = SourceInfo.createSourceInfo(equal);
  929. const cmd = new Commands.Assign(id, exp);
  930. cmd.sourceInfo = sourceInfo;
  931. return cmd;
  932. }
  933. parseCases () {
  934. const token = this.getToken();
  935. if(token.type !== this.lexerClass.RK_CASE) {
  936. throw SyntaxErrorFactory.token_missing_one(this.lexer.literalNames[this.lexerClass.RK_CASE], token);
  937. }
  938. this.pos++;
  939. const nextToken = this.getToken();
  940. if(nextToken.type === this.lexerClass.RK_DEFAULT) {
  941. this.pos++;
  942. const colonToken = this.getToken();
  943. if (colonToken.type !== this.lexerClass.COLON) {
  944. throw SyntaxErrorFactory.token_missing_one(':', colonToken);
  945. }
  946. this.pos++;
  947. this.consumeNewLines();
  948. const block = this.parseCommandBlock(true);
  949. const defaultCase = new Commands.Case(null);
  950. defaultCase.setCommands(block.commands);
  951. return [defaultCase];
  952. } else {
  953. const exp = this.parseExpressionOR();
  954. const colonToken = this.getToken();
  955. if (colonToken.type !== this.lexerClass.COLON) {
  956. throw SyntaxErrorFactory.token_missing_one(':', colonToken);
  957. }
  958. this.pos++;
  959. this.consumeNewLines();
  960. const block = this.parseCommandBlock(true);
  961. const aCase = new Commands.Case(exp);
  962. aCase.setCommands(block.commands);
  963. const caseToken = this.getToken();
  964. if(caseToken.type === this.lexerClass.RK_CASE) {
  965. return [aCase].concat(this.parseCases());
  966. } else {
  967. return [aCase];
  968. }
  969. }
  970. }
  971. /*
  972. * Parses an Expression following the structure:
  973. *
  974. * EOR => EAnd ( 'or' EOR)? #expression and
  975. *
  976. * EOR => ENot ('and' EOR)? #expression or
  977. *
  978. * ENot => 'not'? ER #expression not
  979. *
  980. * ER => E ((>=, <=, ==, >, <) E)? #expression relational
  981. *
  982. * E => factor ((+, -) E)? #expression
  983. *
  984. * factor=> term ((*, /, %) factor)?
  985. *
  986. * term => literal || arrayAccess || FuncCall || ID || '('EAnd')'
  987. **/
  988. parseExpressionOR () {
  989. let exp1 = this.parseExpressionAND();
  990. while (this.getToken().type === this.lexerClass.OR_OPERATOR) {
  991. const opToken = this.getToken();
  992. this.pos++;
  993. const or = convertFromString('or');
  994. this.consumeNewLines();
  995. const exp2 = this.parseExpressionAND();
  996. const finalExp = new Expressions.InfixApp(or, exp1, exp2);
  997. finalExp.sourceInfo = SourceInfo.createSourceInfo(opToken);
  998. exp1 = finalExp
  999. }
  1000. return exp1;
  1001. }
  1002. parseExpressionAND () {
  1003. let exp1 = this.parseExpressionNot();
  1004. while (this.getToken().type === this.lexerClass.AND_OPERATOR) {
  1005. const opToken = this.getToken();
  1006. this.pos++;
  1007. const and = convertFromString('and');
  1008. this.consumeNewLines();
  1009. const exp2 = this.parseExpressionNot();
  1010. const finalExp = new Expressions.InfixApp(and, exp1, exp2);
  1011. finalExp.sourceInfo = SourceInfo.createSourceInfo(opToken);
  1012. exp1 = finalExp;
  1013. }
  1014. return exp1;
  1015. }
  1016. parseExpressionNot () {
  1017. const maybeNotToken = this.getToken();
  1018. if (maybeNotToken.type === this.lexerClass.NOT_OPERATOR) {
  1019. const opToken = this.getToken();
  1020. this.pos++;
  1021. const not = convertFromString('not');
  1022. const exp1 = this.parseExpressionRel();
  1023. const finalExp = new Expressions.UnaryApp(not, exp1);
  1024. finalExp.sourceInfo = SourceInfo.createSourceInfo(opToken);
  1025. return finalExp;
  1026. } else {
  1027. return this.parseExpressionRel();
  1028. }
  1029. }
  1030. parseExpressionRel () {
  1031. let exp1 = this.parseExpression();
  1032. while (this.getToken().type === this.lexerClass.RELATIONAL_OPERATOR) {
  1033. const relToken = this.getToken();
  1034. this.pos++;
  1035. const rel = convertFromString(relToken.text);
  1036. const exp2 = this.parseExpression();
  1037. const finalExp = new Expressions.InfixApp(rel, exp1, exp2);
  1038. finalExp.sourceInfo = SourceInfo.createSourceInfo(relToken);
  1039. exp1 = finalExp;
  1040. }
  1041. return exp1;
  1042. }
  1043. parseExpression () {
  1044. let factor = this.parseFactor();
  1045. while (this.getToken().type === this.lexerClass.SUM_OP) {
  1046. const sumOpToken = this.getToken();
  1047. this.pos++;
  1048. const op = convertFromString(sumOpToken.text);
  1049. const factor2 = this.parseFactor();
  1050. const finalExp = new Expressions.InfixApp(op, factor, factor2);
  1051. finalExp.sourceInfo = SourceInfo.createSourceInfo(sumOpToken);
  1052. factor = finalExp;
  1053. }
  1054. return factor;
  1055. }
  1056. parseFactor () {
  1057. let term = this.parseTerm();
  1058. while (this.getToken().type === this.lexerClass.MULTI_OP) {
  1059. const multOpToken = this.getToken();
  1060. this.pos++;
  1061. const op = convertFromString(multOpToken.text);
  1062. const term2 =this.parseTerm();
  1063. const finalExp = new Expressions.InfixApp(op, term, term2);
  1064. finalExp.sourceInfo = SourceInfo.createSourceInfo(multOpToken);
  1065. term = finalExp;
  1066. }
  1067. return term;
  1068. }
  1069. parseTerm () {
  1070. const token = this.getToken();
  1071. let sourceInfo = null;
  1072. switch(token.type) {
  1073. case this.lexerClass.SUM_OP:
  1074. this.pos++;
  1075. sourceInfo = SourceInfo.createSourceInfo(token);
  1076. const exp = new Expressions.UnaryApp(convertFromString(token.text), this.parseTerm());
  1077. exp.sourceInfo = sourceInfo;
  1078. return exp;
  1079. case this.lexerClass.INTEGER:
  1080. this.pos++;
  1081. return this.getIntLiteral(token);
  1082. case this.lexerClass.REAL:
  1083. this.pos++;
  1084. return this.getRealLiteral(token);
  1085. case this.lexerClass.STRING:
  1086. this.pos++;
  1087. return this.getStringLiteral(token);
  1088. case this.lexerClass.RK_TRUE:
  1089. case this.lexerClass.RK_FALSE:
  1090. this.pos++;
  1091. return this.getBoolLiteral(token);
  1092. case this.lexerClass.OPEN_CURLY:
  1093. // No more annonymous array
  1094. // return this.parseArrayLiteral();
  1095. throw SyntaxErrorFactory.annonymous_array_literal(token);
  1096. case this.lexerClass.ID:
  1097. case this.lexerClass.LIB_ID:
  1098. return this.parseIDTerm();
  1099. case this.lexerClass.OPEN_PARENTHESIS:
  1100. return this.parseParenthesisExp();
  1101. default:
  1102. throw SyntaxErrorFactory.invalid_terminal(token);
  1103. }
  1104. }
  1105. parseIDTerm () {
  1106. const tokenA = this.getToken();
  1107. const id = this.parseMaybeLibID();
  1108. const isID = tokenA.type === this.lexerClass.ID;
  1109. if(isID && this.checkOpenBrace(true)) {
  1110. let tokenB = null;
  1111. this.pos++;
  1112. const firstIndex = this.parseExpression();
  1113. let secondIndex = null;
  1114. this.consumeNewLines();
  1115. this.checkCloseBrace();
  1116. tokenB = this.getToken();
  1117. this.pos++;
  1118. if(this.checkOpenBrace(true)){
  1119. this.pos++;
  1120. secondIndex = this.parseExpression();
  1121. this.consumeNewLines();
  1122. this.checkCloseBrace();
  1123. tokenB = this.getToken();
  1124. this.pos++;
  1125. }
  1126. const sourceInfo = SourceInfo.createSourceInfoFromList(tokenA, tokenB);
  1127. const exp = new Expressions.ArrayAccess(id, firstIndex, secondIndex);
  1128. exp.sourceInfo = sourceInfo;
  1129. return exp;
  1130. } else if (this.checkOpenParenthesis(true)) {
  1131. return this.parseFunctionCallExpression(id);
  1132. } else if (isID) {
  1133. const sourceInfo = SourceInfo.createSourceInfo(tokenA);
  1134. const exp = new Expressions.VariableLiteral(id);
  1135. exp.sourceInfo = sourceInfo;
  1136. return exp;
  1137. } else {
  1138. throw SyntaxErrorFactory.invalid_id_format(tokenA);
  1139. }
  1140. }
  1141. getFunctionName (id) {
  1142. const name = LanguageDefinedFunction.getInternalName(id);
  1143. if (name === null) {
  1144. if (id === LanguageDefinedFunction.getMainFunctionName()) {
  1145. return null;
  1146. }
  1147. return id;
  1148. } else {
  1149. return name;
  1150. }
  1151. }
  1152. parseFunctionCallExpression (id) {
  1153. const tokenA = this.getToken(this.pos - 1);
  1154. const actualParameters = this.parseActualParameters();
  1155. const tokenB = this.getToken(this.pos - 1);
  1156. const funcName = this.getFunctionName(id);
  1157. const sourceInfo = SourceInfo.createSourceInfoFromList(tokenA, tokenB);
  1158. const cmd = new Expressions.FunctionCall(funcName, actualParameters);
  1159. cmd.sourceInfo = sourceInfo;
  1160. return cmd;
  1161. }
  1162. parseFunctionCallCommand (id) {
  1163. return this.parseFunctionCallExpression(id);
  1164. }
  1165. parseParenthesisExp () {
  1166. this.checkOpenParenthesis();
  1167. const tokenA = this.getToken();
  1168. this.pos++;
  1169. this.consumeNewLines();
  1170. const exp = this.parseExpressionOR();
  1171. this.consumeNewLines();
  1172. this.checkCloseParenthesis();
  1173. const tokenB = this.getToken();
  1174. const sourceInfo = SourceInfo.createSourceInfoFromList(tokenA, tokenB);
  1175. this.pos++;
  1176. exp.sourceInfo = sourceInfo;
  1177. return exp;
  1178. }
  1179. parseActualParameters () {
  1180. this.checkOpenParenthesis();
  1181. this.pos++;
  1182. if(this.checkCloseParenthesis(true)) {
  1183. this.pos++;
  1184. return [];
  1185. }
  1186. this.consumeNewLines();
  1187. const list = this.parseExpressionList();
  1188. this.consumeNewLines();
  1189. this.checkCloseParenthesis();
  1190. this.pos++;
  1191. return list;
  1192. }
  1193. parseExpressionList () {
  1194. const list = [];
  1195. while(true) {
  1196. const exp = this.parseExpressionOR();
  1197. list.push(exp);
  1198. const maybeToken = this.getToken();
  1199. if (maybeToken.type !== this.lexerClass.COMMA) {
  1200. break;
  1201. } else {
  1202. this.pos++;
  1203. this.consumeNewLines();
  1204. }
  1205. }
  1206. return list;
  1207. }
  1208. getTypeArray () {
  1209. const types = this.insideScope(IVProgParser.FUNCTION) ? this.functionTypes : this.variableTypes;
  1210. return types.map( x => this.lexer.literalNames[x]);
  1211. }
  1212. }