analisadorSintatico.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. import { CommonTokenStream, InputStream } from 'antlr4/index';
  2. import { SyntaxError } from './SyntaxError';
  3. export class AnalisadorSintatico {
  4. constructor (input, lexerClass) {
  5. this.lexerClass = lexerClass;
  6. this.lexer = new lexerClass(new InputStream(input));
  7. this.tokenStream = new CommonTokenStream(this.lexer);
  8. this.tokenStream.fill();
  9. this.pos = 1;
  10. this.variableTypes = [this.lexerClass.PR_INTEIRO,
  11. this.lexerClass.PR_REAL,
  12. this.lexerClass.PR_LOGICO,
  13. this.lexerClass.PR_CADEIA
  14. ];
  15. this.functionTypes = this.variableTypes.push(this.lexerClass.PR_VAZIO);
  16. }
  17. parseTree () {
  18. return this.parseProgram();
  19. }
  20. getToken (index = this.pos) {
  21. // if(index === null)
  22. // index = this.pos;
  23. return this.tokenStream.LT(index);
  24. }
  25. isEOF () {
  26. this.getToken(this.pos);
  27. return this.tokenStream.fetchedEOF;
  28. }
  29. parseProgram () {
  30. const token = this.getToken();
  31. let globalVars = [];
  32. let functions = [];
  33. if(this.lexerClass.PR_PROGRAMA === token.type) {
  34. this.pos++;
  35. this.consumeNewLines();
  36. this.checkOpenCurly();
  37. this.pos++;
  38. while(true) {
  39. this.consumeNewLines();
  40. const token = this.getToken();
  41. if (token.type === this.lexerClass.PR_CONST || token.type === this.lexerClass.ID) {
  42. globalVars = globalVars.concat(this.parseGlobalVariables());
  43. } else if (token.type === this.lexerClass.PR_FUNCAO) {
  44. functions = functions.concat([]);
  45. } else {
  46. break;
  47. }
  48. }
  49. this.consumeNewLines();
  50. this.checkCloseCurly();
  51. this.pos++;
  52. this.consumeNewLines();
  53. if(!this.isEOF()) {
  54. throw new Error("No extra characters are allowed after 'program {...}'");
  55. }
  56. return {global: globalVars, functions: functions};
  57. } else {
  58. throw SyntaxError.createError(this.lexer.literalNames[this.lexerClass.PR_PROGRAMA], token);
  59. }
  60. }
  61. checkOpenCurly () {
  62. const token = this.getToken();
  63. if(this.lexerClass.ABRE_CHA !== token.type){
  64. throw SyntaxError.createError('{', token);
  65. }
  66. }
  67. checkCloseCurly () {
  68. const token = this.getToken();
  69. if(this.lexerClass.FECHA_CHA !== token.type){
  70. throw SyntaxError.createError('}', token);
  71. }
  72. }
  73. /* It checks if the current token at position pos is a ']'.
  74. * As a check function it doesn't increment pos.
  75. *
  76. * @params bool:attempt, indicates that the token is optional. Defaults: false
  77. *
  78. * @returns true if the attempt is true and current token is '[',
  79. * false is attempt is true and current token is not '['
  80. **/
  81. checkOpenBrace (attempt = false) {
  82. const token = this.getToken();
  83. if(this.lexerClass.ABRE_COL !== token.type){
  84. if (!attempt) {
  85. throw SyntaxError.createError('[', token);
  86. } else {
  87. return false;
  88. }
  89. }
  90. return true;
  91. }
  92. checkCloseBrace (attempt = false) {
  93. const token = this.getToken();
  94. if(this.lexerClass.FECHA_COL !== token.type){
  95. if (!attempt) {
  96. throw SyntaxError.createError(']', token);
  97. } else {
  98. return false;
  99. }
  100. }
  101. return true;
  102. }
  103. checkOpenParenthesis (attempt = false) {
  104. const token = this.getToken();
  105. if(this.lexerClass.ABRE_PAR !== token.type){
  106. if (!attempt) {
  107. throw SyntaxError.createError('(', token);
  108. } else {
  109. return false;
  110. }
  111. }
  112. return true;
  113. }
  114. checkCloseParenthesis (attempt = false) {
  115. const token = this.getToken();
  116. if(this.lexerClass.FECHA_PAR !== token.type){
  117. if (!attempt) {
  118. throw SyntaxError.createError(')', token);
  119. } else {
  120. return false;
  121. }
  122. }
  123. return true;
  124. }
  125. parseGlobalVariables () {
  126. let vars = [];
  127. while(true) {
  128. const decl = this.parseHasConst();
  129. const eosToken = this.getToken();
  130. if (decl !== null && eosToken.type !== this.lexerClass.EOS) {
  131. throw SyntaxError.createError('new line or \';\'', eosToken);
  132. }
  133. if (decl === null) {
  134. break;
  135. } else {
  136. vars = vars.concat(decl);
  137. this.pos++;
  138. }
  139. }
  140. return vars;
  141. }
  142. /*
  143. * Checks if the next token is PR_CONST. It's only available
  144. * at global variables declaration level
  145. * @returns Declararion(const, type, id, initVal?)
  146. **/
  147. parseHasConst () {
  148. const constToken = this.getToken();
  149. if(constToken.type === this.lexerClass.PR_CONST) {
  150. this.pos++;
  151. const typeString = this.parseType();
  152. return this.parseDeclararion(typeString, true);
  153. } else if(this.isVariableType(constToken)) {
  154. this.pos++;
  155. return this.parseDeclararion(constToken);
  156. } else {
  157. return null;
  158. }
  159. }
  160. /*
  161. * Parses a declarion of the form: type --- id --- (= --- EAnd)?
  162. * @returns a list of Declararion(const, type, id, initVal?)
  163. **/
  164. parseDeclararion (typeString, isConst = false) {
  165. let initial = null;
  166. let dim1 = null;
  167. let dim2 = null;
  168. const idString = this.parseID();
  169. // Check for array or vector
  170. // ID[int/IDi][int/IDj]
  171. if (this.checkOpenBrace(true)) {
  172. this.pos++;
  173. dim1 = this.getArrayDimension();
  174. this.checkCloseBrace();
  175. this.pos++;
  176. if(this.checkOpenBrace(true)) {
  177. this.pos++;
  178. dim2 = this.getArrayDimension();
  179. this.checkCloseBrace();
  180. this.pos++;
  181. }
  182. }
  183. const equalsToken = this.getToken();
  184. if(equalsToken.type === this.lexerClass.ATRIBUICAO) {
  185. //process Expression(EAnd) => initial != null
  186. console.log("= found");
  187. }
  188. const commaToken = this.getToken();
  189. if(commaToken.type === this.lexerClass.VIRGULA) {
  190. console.log("comma found");
  191. this.pos++;
  192. return [{
  193. isConst: isConst,
  194. tipo: typeString,
  195. id: idString,
  196. lines: dim1,
  197. columns: dim2,
  198. initial: initial
  199. }]
  200. .concat(this.parseDeclararion(typeString, isConst));
  201. } else {
  202. return [{
  203. isConst: isConst,
  204. tipo: typeString,
  205. id: idString,
  206. lines: dim1,
  207. columns: dim2,
  208. initial: initial
  209. }]
  210. }
  211. }
  212. consumeNewLines () {
  213. let token = this.getToken();
  214. while(token.type === this.lexerClass.EOS && token.text.match('[\r\n]+')) {
  215. this.pos++;
  216. token = this.getToken();
  217. }
  218. }
  219. isVariableType (token) {
  220. return this.variableTypes.find(v => v === token.type);
  221. }
  222. /*
  223. * Reads the next token of the stream to check if it is a Integer or an ID.
  224. * @returns Integer | ID
  225. **/
  226. getArrayDimension () {
  227. const dimToken = this.getToken();
  228. if(dimToken.type === this.lexerClass.INTEIRO) {
  229. //parse as int literal
  230. this.pos++;
  231. return this.parseIntLiteral(dimToken);
  232. } else if(dimToken.type === this.lexerClass.ID) {
  233. //parse as variable
  234. this.pos++;
  235. return this.parseVariable(dimToken);
  236. } else {
  237. throw SyntaxError.createError('int or ID', dimToken);
  238. }
  239. }
  240. /*
  241. * Returns an object {type: 'int', value: value}.
  242. * It checks for binary and hexadecimal integers.
  243. * @returns object with fields type and value
  244. **/
  245. parseIntLiteral (token) {
  246. const text = token.text;
  247. let val = null;
  248. if(text.match('^0b|^0B')) {
  249. val = parseInt(text.substring(2), 2);
  250. } else if (text.match('^0x|^0X')) {
  251. val = parseInt(text.substring(2), 16);
  252. } else {
  253. val = parseInt(text);
  254. }
  255. return {type: 'int', value: val};
  256. }
  257. parseRealLiteral (token) {
  258. return {type: 'real', value: parseFloat(token.text)};
  259. }
  260. /*
  261. * Returns an object {type: 'variable', value: value}.
  262. * @returns object with fields type and value
  263. **/
  264. parseVariable (token) {
  265. return {type: 'variable', value: token.text};
  266. }
  267. parseFunctions () {
  268. let list = [];
  269. while(true) {
  270. const func = this.parseFunction();
  271. if(func === null)
  272. break;
  273. else
  274. list.push(func);
  275. }
  276. return list;
  277. }
  278. /*
  279. * Returns an object representing a function. It has
  280. * four attributes: returnType, id, formalParams and block.
  281. * The block object has two attributes: declarations and commands
  282. **/
  283. parseFunction () {
  284. let formalParams = [];
  285. const token = this.getToken();
  286. if(token.type !== this.lexerClass.PR_FUNCAO) {
  287. //throw SyntaxError.createError(this.lexer.literalNames[this.lexerClass.PR_FUNCAO], token);
  288. return null;
  289. }
  290. this.pos++;
  291. this.consumeNewLines();
  292. const returnType = this.parseType(true);
  293. this.consumeNewLines();
  294. const functionID = this.parseID();
  295. this.consumeNewLines();
  296. this.checkOpenParenthesis();
  297. this.pos++;
  298. this.consumeNewLines();
  299. if (!this.checkCloseParenthesis(true)) {
  300. formalParams = this.parseFormalParameters(); // formal parameters
  301. this.consumeNewLines();
  302. this.checkCloseParenthesis();
  303. this.pos++;
  304. } else {
  305. this.pos++;
  306. }
  307. this.consumeNewLines();
  308. const commandsBlock = this.parseFunctionBody();
  309. return {returnType: returnType, id: functionID, formalParams: formalParams, block: commandsBlock};
  310. }
  311. /*
  312. * Parse the formal parameters of a function.
  313. * @returns a list of objects with the following attributes: type, id and dimensions.
  314. **/
  315. parseFormalParameters () {
  316. const list = [];
  317. while(true) {
  318. let dimensions = 0;
  319. this.consumeNewLines();
  320. const typeString = this.parseType();
  321. this.pos++;
  322. this.consumeNewLines();
  323. const idString = this.parseID();
  324. this.pos++;
  325. this.consumeNewLines();
  326. if (this.checkOpenBrace(true)) {
  327. this.pos++;
  328. dimensions++;
  329. this.checkCloseBrace();
  330. this.pos++;
  331. if(this.checkOpenBrace(true)) {
  332. this.pos++;
  333. dimensions++;
  334. this.checkCloseBrace();
  335. this.pos++;
  336. }
  337. }
  338. list.push({type: typeString, id: idString, dimensions: dimensions});
  339. this.consumeNewLines();
  340. const commaToken = this.getToken();
  341. if (commaToken.type !== this.lexerClass.VIRGULA)
  342. break;
  343. this.pos++;
  344. }
  345. return list;
  346. }
  347. parseID () {
  348. const token = this.getToken();
  349. if(token.type !== this.lexerClass.ID) {
  350. throw SyntaxError.createError('ID', token);
  351. }
  352. this.pos++;
  353. return token.text;
  354. }
  355. parseType (isFunction = false) {
  356. const token = this.getToken();
  357. if(token.type === this.lexerClass.ID && isFunction) {
  358. return 'void';
  359. } else if (token.type === this.lexerClass.PR_VAZIO && isFunction) {
  360. this.pos++;
  361. return 'void';
  362. } else if (this.isVariableType(token)) {
  363. this.pos++;
  364. switch(token.type) {
  365. case this.lexerClass.PR_INTEIRO:
  366. return 'int';
  367. case this.lexerClass.PR_LOGICO:
  368. return 'logic';
  369. case this.lexerClass.PR_REAL:
  370. return 'real';
  371. case this.lexerClass.PR_CADEIA:
  372. return 'string';
  373. default:
  374. break;
  375. }
  376. }
  377. throw SyntaxError.createError(this.getTypesAsString(isFunction), token);
  378. }
  379. parseFunctionBody () {
  380. let variablesDecl = [];
  381. this.checkOpenCurly();
  382. this.pos++;
  383. while(true) {
  384. this.consumeNewLines();
  385. const token = this.getToken();
  386. if (isVariableType(token)) {
  387. this.pos++;
  388. variablesDecl = variablesDecl.concat(this.parseDeclararion(token));
  389. } else if (token.type === this.lexerClass.ID) {
  390. this.pos++;
  391. const equalOrParenthesis = this.getToken();
  392. if (equalOrParenthesis.type === this.lexerClass.ATRIBUICAO) {
  393. } else if (equalOrParenthesis.type === this.lexerClass.ABRE_PAR) {
  394. } else {
  395. throw SyntaxError.createError("= or (", equalOrParenthesis);
  396. }
  397. }
  398. }
  399. }
  400. getTypesAsString (isFunction = false) {
  401. const types = isFunction ? this.functionTypes : this.variableTypes;
  402. return types.map( x => this.lexer.literalNames[x])
  403. .reduce((o, n) => {
  404. if (o.length <= 0)
  405. return n;
  406. else
  407. return o + ", " + n;
  408. }, '');
  409. }
  410. }