ivprogParser.js 42 KB

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