ivprogParser.js 39 KB

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