ivprogParser.js 40 KB

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