ivprogParser.js 39 KB

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