analisadorSintatico.js 9.8 KB

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