12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364 |
- import { CommonTokenStream, InputStream } from 'antlr4/index';
- import * as Expressions from './expressions';
- import * as Commands from './commands';
- import * as AstHelpers from "./ast_helpers";
- import * as Parsers from '../typeSystem/parsers';
- import { Types } from "../typeSystem/types";
- import { ArrayType } from "../typeSystem/array_type";
- import { SourceInfo } from './sourceInfo';
- import { convertFromString } from './operators';
- import { SyntaxErrorFactory } from './error/syntaxErrorFactory';
- import { LanguageDefinedFunction } from '../processor/definedFunctions';
- import { LanguageService } from '../services/languageService';
- export class IVProgParser {
- static createParser (input) {
- const lexerClass = LanguageService.getCurrentLexer();
- return new IVProgParser(input, lexerClass);
- }
- // <BEGIN scope consts>
- static get BASE () {
- return 0;
- }
- static get FUNCTION () {
- return 1;
- }
- static get COMMAND () {
- return 2;
- }
- static get BREAKABLE () {
- return 4;
- }
- // </ END scope consts>
- constructor (input, lexerClass) {
- this.lexerClass = lexerClass;
- this.inputStream = new InputStream(input)
- this.lexer = new lexerClass(this.inputStream);
- this.lexer.recover = AstHelpers.recover.bind(this.lexer);
- this.tokenStream = new CommonTokenStream(this.lexer);
- this.tokenStream.fill();
- this.pos = 1;
- this.variableTypes = [this.lexerClass.RK_INTEGER,
- this.lexerClass.RK_REAL,
- this.lexerClass.RK_BOOLEAN,
- this.lexerClass.RK_STRING,
- this.lexerClass.RK_CHARACTER
- ];
- this.functionTypes = this.variableTypes.concat(this.lexerClass.RK_VOID);
- this.parsingArrayDimension = 0;
- this.scope = [];
- this.langFuncs = LanguageService.getCurrentLangFuncs();
- this.definedFuncsNameList = [];
- this.definedVariablesStack = [];
- }
- parseTree () {
- return this.parseProgram();
- }
- getToken (index = this.pos) {
- // if(index === null)
- // index = this.pos;
- return this.tokenStream.LT(index);
- }
- insideScope (scope) {
- if(this.scope.length <= 0) {
- return IVProgParser.BASE === scope;
- } else {
- return this.scope[this.scope.length-1] === scope;
- }
- }
- pushScope (scope) {
- this.scope.push(scope);
- }
- pushVariableStack () {
- this.definedVariablesStack.push([]);
- }
- popScope () {
- return this.scope.pop();
- }
- popVariableStack () {
- return this.definedVariablesStack.pop();
- }
- getCurrentVariableStack () {
- return this.definedVariablesStack[this.definedVariablesStack.length - 1];
- }
- isEOF () {
- this.getToken(this.pos);
- return this.tokenStream.fetchedEOF;
- }
- parseProgram () {
- this.consumeNewLines();
- const token = this.getToken();
- let globalVars = [];
- let functions = [];
- if(this.lexerClass.RK_PROGRAM === token.type) {
- this.pos++;
- this.consumeNewLines();
- this.checkOpenCurly();
- this.pos++;
- this.pushVariableStack();
- for(;;) {
- this.consumeNewLines();
- const token = this.getToken();
- if (token.type === this.lexerClass.RK_CONST || this.isVariableType(token)) {
- globalVars = globalVars.concat(this.parseGlobalVariables());
- } else if (token.type === this.lexerClass.RK_FUNCTION) {
- this.pushVariableStack();
- functions = functions.concat(this.parseFunction());
- this.popVariableStack();
- } else {
- break;
- }
- }
- this.consumeNewLines();
- this.checkCloseCurly();
- this.pos++;
- this.consumeNewLines();
- if(!this.isEOF()) {
- throw SyntaxErrorFactory.extra_lines();
- }
- this.popVariableStack();
- return {global: globalVars, functions: functions};
- } else {
- throw SyntaxErrorFactory.token_missing_one(this.lexer.literalNames[this.lexerClass.RK_PROGRAM],
- token);
- }
- }
- checkOpenCurly (attempt = false) {
- const token = this.getToken();
- if(this.lexerClass.OPEN_CURLY !== token.type){
- if(!attempt)
- throw SyntaxErrorFactory.token_missing_one('{', token);
- else
- return false;
- }
- return true;
- }
- checkCloseCurly (attempt = false) {
- const token = this.getToken();
- if(this.lexerClass.CLOSE_CURLY !== token.type){
- if(!attempt)
- throw SyntaxErrorFactory.token_missing_one('}', token);
- else
- return false;
- }
- return true;
- }
- /* It checks if the current token at position pos is a ']'.
- * As a check function it doesn't increment pos.
- *
- * @params bool:attempt, indicates that the token is optional. Defaults: false
- *
- * @returns true if the attempt is true and current token is '[',
- * false is attempt is true and current token is not '['
- **/
- checkOpenBrace (attempt = false) {
- const token = this.getToken();
- if(this.lexerClass.OPEN_BRACE !== token.type){
- if (!attempt) {
- throw SyntaxErrorFactory.token_missing_one('[', token);
- } else {
- return false;
- }
- }
- return true;
- }
- checkCloseBrace (attempt = false) {
- const token = this.getToken();
- if(this.lexerClass.CLOSE_BRACE !== token.type){
- if (!attempt) {
- throw SyntaxErrorFactory.token_missing_one(']', token);
- } else {
- return false;
- }
- }
- return true;
- }
- checkOpenParenthesis (attempt = false) {
- const token = this.getToken();
- if(this.lexerClass.OPEN_PARENTHESIS !== token.type){
- if (!attempt) {
- throw SyntaxErrorFactory.token_missing_one('(', token);
- } else {
- return false;
- }
- }
- return true;
- }
- checkCloseParenthesis (attempt = false) {
- const token = this.getToken();
- if(this.lexerClass.CLOSE_PARENTHESIS !== token.type){
- if (!attempt) {
- throw SyntaxErrorFactory.token_missing_one(')', token);
- } else {
- return false;
- }
- }
- return true;
- }
- checkEOS (attempt = false) {
- const eosToken = this.getToken();
- if (eosToken.type !== this.lexerClass.EOS) {
- if (!attempt)
- throw SyntaxErrorFactory.eos_missing(eosToken);
- else
- return false;
- }
- return true;
- }
- checkFunctionDuplicate (functionID, funcIDToken) {
- const id = functionID === null ? "$main" : functionID;
- const index = this.definedFuncsNameList.indexOf(id);
- if(index !== -1) {
- throw SyntaxErrorFactory.duplicate_function(funcIDToken);
- }
- this.definedFuncsNameList.push(id);
- }
- checkVariableDuplicate (variableID, sourceInfo) {
- const index = this.getCurrentVariableStack().indexOf(variableID);
- if(index !== -1) {
- throw SyntaxErrorFactory.duplicate_variable(sourceInfo);
- }
- this.getCurrentVariableStack().push(variableID);
- }
- consumeForSemiColon () {
- const eosToken = this.getToken();
- if (eosToken.type === this.lexerClass.EOS && eosToken.text.match(';')) {
- this.pos++;
- return;
- }
- throw SyntaxErrorFactory.token_missing_one(';', eosToken);
- }
- parseGlobalVariables () {
- const decl = this.parseMaybeConst();
- this.checkEOS();
- this.pos++;
- return decl;
- }
- /*
- * Checks if the next token is PR_CONST. It's only available
- * at global variables declaration level
- * @returns Declararion(const, type, id, initVal?)
- **/
- parseMaybeConst () {
- const constToken = this.getToken();
- if(constToken.type === this.lexerClass.RK_CONST) {
- this.pos++;
- const typeString = this.parseType();
- return this.parseDeclaration(typeString, true);
- } else if(this.isVariableType(constToken)) {
- const typeString = this.parseType();
- return this.parseDeclaration(typeString);
- } else {
- throw SyntaxErrorFactory.token_missing_list(
- [this.lexer.literalNames[this.lexerClass.RK_CONST]].concat(this.getTypeArray()), constToken);
- }
- }
- /*
- * Parses a declarion of the form: type --- id --- (= --- EAnd)?
- * @returns a list of Declararion(const, type, id, initVal?)
- **/
- parseDeclaration (typeString, isConst = false) {
- let initial = null;
- let dim1 = null;
- let dim2 = null;
- let dimensions = 0;
- const sourceInfo = SourceInfo.createSourceInfo(this.getToken())
- const idString = this.parseID();
- this.checkVariableDuplicate(idString, sourceInfo);
- // Check for array or vector
- // ID[int/IDi?][int/IDj?]
- if (this.checkOpenBrace(true)) {
- this.pos += 1;
- this.consumeNewLines();
- dim1 = this.parseArrayDimension();
- this.consumeNewLines();
- this.checkCloseBrace();
- this.pos += 1;
- dimensions += 1
- if(this.checkOpenBrace(true)) {
- this.pos += 1;
- this.consumeNewLines();
- dim2 = this.parseArrayDimension();
- this.consumeNewLines();
- this.checkCloseBrace();
- this.pos += 1;
- dimensions += 1
- }
- return this.parseArrayDeclaration(typeString, isConst, idString, sourceInfo, dimensions, dim1, dim2);
- } else {
- const equalsToken = this.getToken();
- if(isConst && equalsToken.type !== this.lexerClass.EQUAL ) {
- throw SyntaxErrorFactory.const_not_init(sourceInfo);
- }
- if(equalsToken.type === this.lexerClass.EQUAL) {
- this.pos++;
- initial = this.parseExpressionOR();
- }
- const declaration = new Commands.Declaration(idString, typeString, initial, isConst);
- declaration.sourceInfo = sourceInfo;
- const commaToken = this.getToken();
- if(commaToken.type === this.lexerClass.COMMA) {
- this.pos++;
- this.consumeNewLines();
- return [declaration]
- .concat(this.parseDeclaration(typeString, isConst));
- } else {
- return [declaration]
- }
- }
- }
- parseArrayDeclaration (typeString, isConst, idString, sourceInfo, dimensions, dim1, dim2) {
- const equalsToken = this.getToken();
- let n_lines = dim1;
- let n_columns = dim2;
- let initial = null;
- let dim_is_id = false;
- if(dim1 instanceof Expressions.VariableLiteral || dim2 instanceof Expressions.VariableLiteral) {
- dim_is_id = true;
- if(dimensions > 1 && (dim1 == null || dim2 == null)) {
- throw SyntaxErrorFactory.invalid_matrix_id_dimension(SourceInfo.createSourceInfo(equalsToken));
- }
- }
- if(isConst && equalsToken.type !== this.lexerClass.EQUAL ) {
- throw SyntaxErrorFactory.const_not_init(sourceInfo);
- }
- if(equalsToken.type === this.lexerClass.EQUAL) {
- if(dim_is_id) {
- if(dimensions == 1) {
- throw SyntaxErrorFactory.invalid_vector_init(SourceInfo.createSourceInfo(equalsToken));
- } else {
- throw SyntaxErrorFactory.invalid_matrix_init(SourceInfo.createSourceInfo(equalsToken));
- }
- }
- this.pos += 1
- initial = this.parseArrayLiteral(typeString);
- }
- if(initial == null && dim1 == null) {
- if(dimensions > 1) {
- throw SyntaxErrorFactory.cannot_infer_matrix_line(idString, sourceInfo);
- }
- throw SyntaxErrorFactory.cannot_infer_vector_size(idString, sourceInfo);
- }
- if(dimensions > 1) {
- if(initial == null && dim2 == null) {
- throw SyntaxErrorFactory.cannot_infer_matrix_column(idString, sourceInfo);
- }
- }
- if(dimensions === 1 && initial != null && !initial.isVector) {
- const expString = initial.toString();
- throw SyntaxErrorFactory.matrix_to_vector_literal_attr(idString, expString, initial.sourceInfo);
- } else if (dimensions > 1 && initial != null && initial.isVector) {
- const expString = initial.toString();
- throw SyntaxErrorFactory.vector_to_matrix_literal_attr(idString, expString, initial.sourceInfo);
- }
- if(dim1 == null) {
- n_lines = new Expressions.IntLiteral(Parsers.toInt(initial.lines));
- n_lines.sourceInfo = sourceInfo;
- }
- if (dimensions > 1) {
- if(dim2 == null) {
- n_columns = new Expressions.IntLiteral(Parsers.toInt(initial.columns));
- n_columns.sourceInfo = sourceInfo;
- }
- }
- const declaration = new Commands.ArrayDeclaration(idString,
- new ArrayType(typeString, dimensions), n_lines, n_columns, initial, isConst);
- declaration.sourceInfo = sourceInfo;
- const commaToken = this.getToken();
- if(commaToken.type === this.lexerClass.COMMA) {
- this.pos++;
- this.consumeNewLines();
- return [declaration]
- .concat(this.parseDeclaration(typeString, isConst));
- } else {
- return [declaration]
- }
- }
- consumeNewLines () {
- let token = this.getToken();
- while(token.type === this.lexerClass.EOS && token.text.match('[\r\n]+')) {
- this.pos++;
- token = this.getToken();
- }
- }
- isVariableType (token) {
- return this.variableTypes.find(v => v === token.type);
- }
- /*
- * Reads the next token of the stream to check if it is a Integer or an ID.
- * @returns Integer | ID
- **/
- parseArrayDimension () {
- const dimToken = this.getToken();
- if(dimToken.type === this.lexerClass.INTEGER) {
- //parse as int literal
- this.pos++;
- return this.getIntLiteral(dimToken);
- } else if(dimToken.type === this.lexerClass.ID) {
- //parse as variable
- this.pos++;
- return this.parseVariable(dimToken);
- } else if(dimToken.type === this.lexerClass.CLOSE_BRACE) {
- return null;
- } else {
- throw SyntaxErrorFactory.invalid_array_dimension(this.lexer.literalNames[this.lexerClass.RK_INTEGER], dimToken);
- }
- }
- /*
- * Returns an object {type: 'int', value: value}.
- * It checks for binary and hexadecimal integers.
- * @returns object with fields type and value
- **/
- getIntLiteral (token) {
- const text = token.text;
- const sourceInfo = SourceInfo.createSourceInfo(token);
- const exp = new Expressions.IntLiteral(Parsers.toInt(text));
- exp.sourceInfo = sourceInfo;
- return exp;
- }
- getRealLiteral (token) {
- const sourceInfo = SourceInfo.createSourceInfo(token);
- const exp = new Expressions.RealLiteral(Parsers.toReal(token.text));
- exp.sourceInfo = sourceInfo;
- return exp;
- }
- getStringLiteral (token) {
- const text = token.text;
- const sourceInfo = SourceInfo.createSourceInfo(token);
- const exp = new Expressions.StringLiteral(Parsers.toString(text));
- exp.sourceInfo = sourceInfo;
- return exp;
- }
- getCharLiteral (token) {
- const text = token.text;
- const exp = new Expressions.CharLiteral(Parsers.toChar(text));
- exp.sourceInfo = SourceInfo.createSourceInfo(token);
- return exp;
- }
- getBoolLiteral (token) {
- const val = Parsers.toBool(token.text);
- const exp = new Expressions.BoolLiteral(val);
- exp.sourceInfo = SourceInfo.createSourceInfo(token);
- return exp;
- }
- parseArrayLiteral (typeString) {
- const openCurly = this.checkOpenCurly(true);
- if(!openCurly) {
- const invalid_token = this.getToken();
- throw SyntaxErrorFactory.array_init_not_literal(SourceInfo.createSourceInfo(invalid_token));
- }
- const beginArray = this.getToken();
- if (this.parsingArrayDimension >= 2) {
- throw SyntaxErrorFactory.array_exceeds_2d(SourceInfo.createSourceInfo(beginArray));
- }
- this.pos += 1;
- this.parsingArrayDimension += 1;
- this.consumeNewLines();
- let data = null;
- const maybeCurlyOpen = this.checkOpenCurly(true);
- if(maybeCurlyOpen) {
- // This is potentially a list of vectors
- data = this.parseVectorList(typeString);
- } else {
- data = this.parseExpressionList();
- }
- this.consumeNewLines();
- this.checkCloseCurly()
- const endArray = this.getToken();
- this.pos += 1;
- this.parsingArrayDimension -= 1;
- const sourceInfo = SourceInfo.createSourceInfoFromList(beginArray, endArray);
- let dataDim = 1;
- if(data[0] instanceof Expressions.ArrayLiteral) {
- dataDim += 1;
- } else if(data.length == 1) {
- console.log("Talvez uma variável seja uma melhor opção");
- }
- const type = new ArrayType(typeString, dataDim);
- const exp = new Expressions.ArrayLiteral(type, data);
- exp.sourceInfo = sourceInfo;
- return exp;
- }
- /**
- * Returns a list of ArrayLiterals. Helper function for parsing matrices
- */
- parseVectorList (typeString) {
- const list = [];
- let lastSize = null;
- for(;;) {
- this.checkOpenCurly();
- const beginArray = this.getToken();
- if (this.parsingArrayDimension >= 2) {
- throw SyntaxErrorFactory.array_exceeds_2d(SourceInfo.createSourceInfo(beginArray));
- }
- this.pos += 1;
- this.parsingArrayDimension += 1;
- this.consumeNewLines();
- const data = this.parseExpressionList();
- this.consumeNewLines();
- this.checkCloseCurly()
- const endArray = this.getToken();
- this.pos += 1;
- this.parsingArrayDimension -= 1;
- const sourceInfo = SourceInfo.createSourceInfoFromList(beginArray, endArray);
- if(lastSize == null) {
- lastSize = data.length;
- } else if (lastSize !== data.length) {
- const expString = this.inputStream.getText(beginArray.start, endArray.stop);
- throw SyntaxErrorFactory.invalid_matrix_literal_line(expString, sourceInfo);
- }
- const type = new ArrayType(typeString, 1);
- const exp = new Expressions.ArrayLiteral(type, data);
- exp.sourceInfo = sourceInfo;
- list.push(exp);
- const commaToken = this.getToken();
- if(commaToken.type !== this.lexerClass.COMMA) {
- break;
- }
- this.pos += 1;
- this.consumeNewLines();
- }
- if(list.length == 1) {
- console.log("Talvez um vetor seja uma melhor opção");
- }
- return list
- }
- /*
- * Returns an object {type: 'variable', value: value}.
- * @returns object with fields type and value
- **/
- parseVariable (token) {
- const sourceInfo = SourceInfo.createSourceInfo(token);
- const exp = new Expressions.VariableLiteral(token.text);
- exp.sourceInfo = sourceInfo;
- return exp;
- }
- /*
- * Returns an object representing a function. It has
- * four attributes: returnType, id, formalParams and block.
- * The block object has two attributes: declarations and commands
- **/
- parseFunction () {
- this.pushScope(IVProgParser.FUNCTION);
- let formalParams = [];
- const token = this.getToken();
- if(token.type !== this.lexerClass.RK_FUNCTION) {
- //throw SyntaxError.createError(this.lexer.literalNames[this.lexerClass.PR_FUNCAO], token);
- return null;
- }
- this.pos++;
- const funType = this.parseType();
- let dimensions = 0;
- if(this.checkOpenBrace(true)) {
- this.pos++;
- this.checkCloseBrace();
- this.pos++;
- dimensions++;
- if(this.checkOpenBrace(true)) {
- this.pos++;
- this.checkCloseBrace();
- this.pos++;
- dimensions++;
- }
- }
- const funcIDToken = this.getToken();
- const functionID = this.parseID();
- this.checkFunctionDuplicate(functionID, funcIDToken);
- this.checkOpenParenthesis();
- this.pos++;
- this.consumeNewLines();
- if (!this.checkCloseParenthesis(true)) {
- formalParams = this.parseFormalParameters(); // formal parameters
- this.consumeNewLines();
- this.checkCloseParenthesis();
- this.pos++;
- } else {
- this.pos++;
- }
- this.consumeNewLines();
- const commandsBlock = this.parseCommandBlock();
- let returnType = funType;
- if(dimensions > 0) {
- returnType = new ArrayType(funType, dimensions);
- }
- const func = new Commands.Function(functionID, returnType, formalParams, commandsBlock);
- if (functionID === null && !func.isMain) {
- throw SyntaxErrorFactory.invalid_main_return(LanguageDefinedFunction.getMainFunctionName(),
- this.lexer.literalNames[this.lexerClass.RK_VOID],
- token.line);
- } else if (func.isMain && formalParams.length !== 0) {
- throw SyntaxErrorFactory.main_parameters();
- }
- this.popScope();
- return func;
- }
- /*
- * Parse the formal parameters of a function.
- * @returns a list of objects with the following attributes: type, id and dimensions.
- **/
- parseFormalParameters () {
- const list = [];
- for(;;) {
- let dimensions = 0;
- let reference = false;
- const typeString = this.parseType();
- let maybeIDToken = this.getToken();
- if(maybeIDToken.type === this.lexerClass.RK_REFERENCE) {
- reference = true;
- this.pos += 1;
- maybeIDToken = this.getToken();
- }
- const idString = this.parseID();
- this.checkVariableDuplicate(idString, maybeIDToken);
- if (this.checkOpenBrace(true)) {
- this.pos += 1;
- dimensions += 1;
- this.checkCloseBrace();
- this.pos += 1;
- if(this.checkOpenBrace(true)) {
- this.pos += 1;
- dimensions += 1;
- this.checkCloseBrace();
- this.pos += 1;
- }
- }
- let type = null;
- if(dimensions > 0) {
- type = new ArrayType(typeString, dimensions);
- } else {
- type = typeString;
- }
- const parameter = new Commands.FormalParameter(type, idString, reference);
- parameter.sourceInfo = SourceInfo.createSourceInfo(maybeIDToken);
- list.push(parameter);
- const commaToken = this.getToken();
- if (commaToken.type !== this.lexerClass.COMMA)
- break;
- this.pos++;
- this.consumeNewLines();
- }
- return list;
- }
- parseID () {
- const token = this.getToken();
- if(token.type !== this.lexerClass.ID) {
- throw SyntaxErrorFactory.id_missing(token);
- }
- this.pos++;
- if (this.insideScope(IVProgParser.FUNCTION)) {
- if (token.text === LanguageDefinedFunction.getMainFunctionName()){
- return null;
- }
- }
- return token.text;
- }
- parseMaybeLibID () {
- const token = this.getToken();
- if(token.type !== this.lexerClass.ID && token.type !== this.lexerClass.LIB_ID) {
- throw SyntaxErrorFactory.id_missing(token);
- }
- this.pos++;
- return token.text;
- }
- parseType () {
- const token = this.getToken();
- if(token.type === this.lexerClass.ID && this.insideScope(IVProgParser.FUNCTION)) {
- return Types.VOID;
- } else if (token.type === this.lexerClass.RK_VOID && this.insideScope(IVProgParser.FUNCTION)) {
- this.pos++;
- return Types.VOID;
- } else if (this.isVariableType(token)) {
- this.pos++;
- switch(token.type) {
- case this.lexerClass.RK_INTEGER:
- return Types.INTEGER;
- case this.lexerClass.RK_BOOLEAN:
- return Types.BOOLEAN;
- case this.lexerClass.RK_REAL:
- return Types.REAL;
- case this.lexerClass.RK_STRING:
- return Types.STRING;
- case this.lexerClass.RK_CHARACTER:
- return Types.CHAR;
- default:
- break;
- }
- }
-
- throw SyntaxErrorFactory.invalid_type(this.getTypeArray(), token);
- }
- parseCommandBlock (optionalCurly = false) {
- let variablesDecl = [];
- const commands = [];
- let hasOpen = false;
- if (this.checkOpenCurly(optionalCurly)) {
- this.pos++;
- hasOpen = true;
- }
- this.consumeNewLines();
- let parsedCommand = false;
- for(;;) {
- const cmd = this.parseCommand();
- if (cmd === null)
- break;
- if(cmd !== -1) {
- if (cmd instanceof Array) {
- if(parsedCommand) {
- const lastToken = this.getToken(this.pos - 1);
- throw SyntaxErrorFactory.invalid_var_declaration(lastToken);
- }
- variablesDecl = variablesDecl.concat(cmd);
- } else {
- parsedCommand = true;
- commands.push(cmd);
- }
- }
- }
- this.consumeNewLines();
- if (hasOpen) {
- this.checkCloseCurly()
- this.pos++;
- this.consumeNewLines();
- }
- return new Commands.CommandBlock(variablesDecl, commands);
- }
- parseCommand () {
- const token = this.getToken();
- if (this.isVariableType(token)) {
- if(!this.insideScope(IVProgParser.FUNCTION)) {
- throw SyntaxErrorFactory.invalid_var_declaration(token);
- }
- this.pushScope(IVProgParser.BASE);
- const varType = this.parseType();
- this.popScope();
- const cmd = this.parseDeclaration(varType);
- this.checkEOS();
- this.pos++;
- return cmd;
- } else if (token.type === this.lexerClass.ID) {
- return this.parseIDCommand();
- } else if (token.type === this.lexerClass.LIB_ID) {
- return this.parseIDCommand();
- } else if (token.type === this.lexerClass.RK_RETURN) {
- return this.parseReturn();
- } else if (token.type === this.lexerClass.RK_WHILE || token.type === this.lexerClass.RK_WHILE_ALT) {
- return this.parseWhile();
- } else if (token.type === this.lexerClass.RK_FOR || token.type === this.lexerClass.RK_FOR_ALT) {
- return this.parseFor();
- } else if (token.type === this.lexerClass.RK_BREAK ) {
- if(!this.insideScope(IVProgParser.BREAKABLE)) {
- throw SyntaxErrorFactory.invalid_break_command(
- this.lexer.literalNames[this.lexerClass.RK_BREAK],
- token
- );
- }
- return this.parseBreak();
- } else if (token.type === this.lexerClass.RK_SWITCH) {
- return this.parseSwitchCase();
- } else if (token.type === this.lexerClass.RK_DO) {
- return this.parseRepeatUntil();
- } else if (token.type === this.lexerClass.RK_IF) {
- return this.parseIfThenElse();
- } else if (this.checkEOS(true)){
- this.pos++;
- return -1;
- } else {
- return null;
- }
- }
- parseSwitchCase () {
- this.pushScope(IVProgParser.BREAKABLE);
- this.pos++;
- this.checkOpenParenthesis();
- this.pos++;
- this.consumeNewLines();
- const exp = this.parseExpressionOR();
- this.consumeNewLines();
- this.checkCloseParenthesis();
- this.pos++;
- this.consumeNewLines();
- this.checkOpenCurly();
- this.pos++;
- this.consumeNewLines();
- const casesList = this.parseCases();
- this.consumeNewLines();
- this.checkCloseCurly();
- this.pos++;
- this.consumeNewLines();
- this.popScope();
- return new Commands.Switch(exp, casesList);
- }
- parseRepeatUntil () {
- this.pos++;
- this.consumeNewLines();
- this.pushScope(IVProgParser.BREAKABLE);
- const commandsBlock = this.parseCommandBlock();
- this.consumeNewLines(); //Maybe not...
- const whileToken = this.getToken();
- if (whileToken.type !== this.lexerClass.RK_DO_UNTIL) {
- throw SyntaxErrorFactory.token_missing_one(this.lexer.literalNames[this.lexerClass.RK_DO_UNTIL], whileToken);
- }
- this.pos++;
- this.checkOpenParenthesis();
- this.pos++;
- this.consumeNewLines();
- const condition = this.parseExpressionOR();
- this.consumeNewLines();
- this.checkCloseParenthesis();
- this.pos++;
- this.checkEOS();
- this.popScope();
- return new Commands.RepeatUntil(condition, commandsBlock);
- }
- parseIfThenElse () {
- if(this.insideScope(IVProgParser.BREAKABLE)) {
- this.pushScope(IVProgParser.BREAKABLE);
- } else {
- this.pushScope(IVProgParser.COMMAND);
- }
- const token = this.getToken();
- this.pos++;
- this.checkOpenParenthesis();
- this.pos++;
- this.consumeNewLines();
- const logicalExpression = this.parseExpressionOR();
- this.consumeNewLines();
- this.checkCloseParenthesis();
- this.pos++;
- this.consumeNewLines();
- const cmdBlocks = this.parseCommandBlock();
- const maybeElse = this.getToken();
- if(maybeElse.type === this.lexerClass.RK_ELSE) {
- this.pos++;
- this.consumeNewLines();
- const maybeIf = this.getToken();
- let elseBlock = null;
- if(this.checkOpenCurly(true)) {
- elseBlock = this.parseCommandBlock();
- } else if(maybeIf.type === this.lexerClass.RK_IF) {
- elseBlock = this.parseIfThenElse();
- } else {
- throw SyntaxErrorFactory.token_missing_list([this.lexer.literalNames[this.lexerClass.RK_IF], '{'], maybeIf);
- }
- this.popScope();
- const cmd = new Commands.IfThenElse(logicalExpression, cmdBlocks, elseBlock);
- cmd.sourceInfo = SourceInfo.createSourceInfo(token);
- return cmd;
- }
- this.popScope();
- const cmd = new Commands.IfThenElse(logicalExpression, cmdBlocks, null);
- cmd.sourceInfo = SourceInfo.createSourceInfo(token);
- return cmd;
- }
- parseFor () {
- this.pushScope(IVProgParser.BREAKABLE);
- const for_token = this.getToken();
- this.pos += 1;
- // parse ID
- const id_token = this.getToken();
- const id = this.parseID();
- const for_id = new Expressions.VariableLiteral(id);
- for_id.sourceInfo = SourceInfo.createSourceInfo(id_token);
- // END parse ID
- const for_from = this.parseForParameters(this.lexerClass.RK_FOR_FROM);
- const for_to = this.parseForParameters(this.lexerClass.RK_FOR_TO);
- const maybePass = this.parseForParameters(this.lexerClass.RK_FOR_PASS);
- this.consumeNewLines();
- const commandsBlock = this.parseCommandBlock();
- this.popScope();
- const cmd = new Commands.For(for_id, for_from, for_to, maybePass, commandsBlock);
- cmd.sourceInfo = SourceInfo.createSourceInfo(for_token);
- return cmd;
- }
- parseWhile () {
- this.pushScope(IVProgParser.BREAKABLE);
- const token = this.getToken();
- this.pos++;
- this.checkOpenParenthesis();
- this.pos++;
- this.consumeNewLines();
- const logicalExpression = this.parseExpressionOR();
- this.consumeNewLines();
- this.checkCloseParenthesis();
- this.pos++;
- this.consumeNewLines();
- const cmdBlocks = this.parseCommandBlock();
- this.popScope();
- const cmd = new Commands.While(logicalExpression, cmdBlocks);
- cmd.sourceInfo = SourceInfo.createSourceInfo(token);
- return cmd;
- }
- parseBreak () {
- this.pos++;
- this.checkEOS();
- this.pos++;
- return new Commands.Break();
- }
- parseReturn () {
- this.pos++;
- let exp = null;
- if(!this.checkEOS(true)) {
- exp = this.parseExpressionOR();
- this.checkEOS();
- }
- this.pos++;
- return new Commands.Return(exp);
- }
- parseIDCommand () {
- const refToken = this.getToken();
- const isID = refToken.type === this.lexerClass.ID;
- const id = this.parseMaybeLibID();
- if(this.checkOpenBrace(true)) {
- this.pos++;
- let lineExpression = null;
- let columnExpression = null;
- this.consumeNewLines();
- lineExpression = this.parseExpression()
- this.consumeNewLines();
- this.checkCloseBrace();
- this.pos++;
- if (this.checkOpenBrace(true)) {
- this.pos++
- this.consumeNewLines();
- columnExpression = this.parseExpression();
- this.consumeNewLines();
- this.checkCloseBrace();
- this.pos++;
- }
- const equalToken = this.getToken();
- if (equalToken.type !== this.lexerClass.EQUAL) {
- throw SyntaxErrorFactory.token_missing_one('=', equalToken);
- }
- this.pos++;
- const exp = this.parseExpressionOR();
- this.checkEOS();
- this.pos++;
- const cmd = new Commands.ArrayIndexAssign(id, lineExpression, columnExpression, exp);
- cmd.sourceInfo = SourceInfo.createSourceInfo(equalToken);
- return cmd;
- }
- const equalOrParenthesis = this.getToken();
- if (isID && equalOrParenthesis.type === this.lexerClass.EQUAL) {
- this.pos++
- const exp = this.parseExpressionOR();
- this.checkEOS();
- this.pos++;
- const cmd = new Commands.Assign(id, exp);
- cmd.sourceInfo = SourceInfo.createSourceInfo(equalOrParenthesis);
- return cmd;
- } else if (equalOrParenthesis.type === this.lexerClass.OPEN_PARENTHESIS) {
- const funcCall = this.parseFunctionCallCommand(id);
- this.checkEOS();
- this.pos++;
- return funcCall;
- } else if (isID) {
- throw SyntaxErrorFactory.token_missing_list(['=','('], equalOrParenthesis);
- } else {
- throw SyntaxErrorFactory.invalid_id_format(refToken);
- }
- }
- parseForParameters (keyword_code) {
- if(keyword_code === this.lexerClass.RK_FOR_PASS) {
- if(this.checkOpenCurly(true)) {
- return null;
- }
- }
- const from_token = this.getToken();
- if (from_token.type !== keyword_code) {
- // TODO better error message
- const keyword = this.lexer.literalNames[keyword_code];
- throw new Error("Error de sintaxe no comando repita_para: esperava-se "+keyword+" mas encontrou "+from_token.text);
- }
- this.pos += 1;
- let int_or_id = this.getToken();
- let is_unary_op = false;
- let op = null;
- if(int_or_id.type === this.lexerClass.SUM_OP) {
- is_unary_op = true;
- op = int_or_id.text;
- this.pos += 1;
- int_or_id = this.getToken();
- }
- let for_from = null;
- if (int_or_id.type === this.lexerClass.ID) {
- for_from = new Expressions.VariableLiteral(this.parseID());
- for_from.sourceInfo = SourceInfo.createSourceInfo(int_or_id);
- } else if (int_or_id.type === this.lexerClass.INTEGER) {
- this.pos += 1;
- for_from = this.getIntLiteral(int_or_id);
- }
- if (for_from == null) {
- // TODO better error message
- const keyword = this.lexer.literalNames[keyword_code];
- 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.");
- }
- if (is_unary_op) {
- for_from = new Expressions.UnaryApp(convertFromString(op), for_from);
- }
- return for_from;
- }
- parseCases () {
- const token = this.getToken();
- if(token.type !== this.lexerClass.RK_CASE) {
- throw SyntaxErrorFactory.token_missing_one(this.lexer.literalNames[this.lexerClass.RK_CASE], token);
- }
- this.pos++;
- const nextToken = this.getToken();
- if(nextToken.type === this.lexerClass.RK_DEFAULT) {
- this.pos++;
- const colonToken = this.getToken();
- if (colonToken.type !== this.lexerClass.COLON) {
- throw SyntaxErrorFactory.token_missing_one(':', colonToken);
- }
- this.pos++;
- this.consumeNewLines();
- const block = this.parseCommandBlock(true);
- const defaultCase = new Commands.Case(null);
- defaultCase.setCommands(block.commands);
- return [defaultCase];
- } else {
- const exp = this.parseExpressionOR();
- const colonToken = this.getToken();
- if (colonToken.type !== this.lexerClass.COLON) {
- throw SyntaxErrorFactory.token_missing_one(':', colonToken);
- }
- this.pos++;
- this.consumeNewLines();
- const block = this.parseCommandBlock(true);
- const aCase = new Commands.Case(exp);
- aCase.setCommands(block.commands);
- const caseToken = this.getToken();
- if(caseToken.type === this.lexerClass.RK_CASE) {
- return [aCase].concat(this.parseCases());
- } else {
- return [aCase];
- }
- }
- }
- /*
- * Parses an Expression following the structure:
- *
- * EOR => EAnd ( 'or' EOR)? #expression and
- *
- * EOR => ENot ('and' EOR)? #expression or
- *
- * ENot => 'not'? ER #expression not
- *
- * ER => E ((>=, <=, ==, >, <) E)? #expression relational
- *
- * E => factor ((+, -) E)? #expression
- *
- * factor=> term ((*, /, %) factor)?
- *
- * term => literal || arrayAccess || FuncCall || ID || '('EAnd')'
- **/
- parseExpressionOR () {
- let exp1 = this.parseExpressionAND();
- while (this.getToken().type === this.lexerClass.OR_OPERATOR) {
- const opToken = this.getToken();
- this.pos++;
- const or = convertFromString('or');
- this.consumeNewLines();
- const exp2 = this.parseExpressionAND();
- const finalExp = new Expressions.InfixApp(or, exp1, exp2);
- finalExp.sourceInfo = SourceInfo.createSourceInfo(opToken);
- exp1 = finalExp
- }
- return exp1;
- }
- parseExpressionAND () {
- let exp1 = this.parseExpressionNot();
- while (this.getToken().type === this.lexerClass.AND_OPERATOR) {
- const opToken = this.getToken();
- this.pos++;
- const and = convertFromString('and');
- this.consumeNewLines();
- const exp2 = this.parseExpressionNot();
- const finalExp = new Expressions.InfixApp(and, exp1, exp2);
- finalExp.sourceInfo = SourceInfo.createSourceInfo(opToken);
- exp1 = finalExp;
- }
- return exp1;
- }
- parseExpressionNot () {
- const maybeNotToken = this.getToken();
- if (maybeNotToken.type === this.lexerClass.NOT_OPERATOR) {
- const opToken = this.getToken();
- this.pos++;
- const not = convertFromString('not');
- const exp1 = this.parseExpressionRel();
- const finalExp = new Expressions.UnaryApp(not, exp1);
- finalExp.sourceInfo = SourceInfo.createSourceInfo(opToken);
- return finalExp;
-
- } else {
- return this.parseExpressionRel();
- }
- }
- parseExpressionRel () {
- let exp1 = this.parseExpression();
- while (this.getToken().type === this.lexerClass.RELATIONAL_OPERATOR) {
- const relToken = this.getToken();
- this.pos++;
- const rel = convertFromString(relToken.text);
- const exp2 = this.parseExpression();
- const finalExp = new Expressions.InfixApp(rel, exp1, exp2);
- finalExp.sourceInfo = SourceInfo.createSourceInfo(relToken);
- exp1 = finalExp;
- }
- return exp1;
- }
- parseExpression () {
- let factor = this.parseFactor();
- while (this.getToken().type === this.lexerClass.SUM_OP) {
- const sumOpToken = this.getToken();
- this.pos++;
- const op = convertFromString(sumOpToken.text);
- const factor2 = this.parseFactor();
- const finalExp = new Expressions.InfixApp(op, factor, factor2);
- finalExp.sourceInfo = SourceInfo.createSourceInfo(sumOpToken);
- factor = finalExp;
- }
- return factor;
- }
- parseFactor () {
- let term = this.parseTerm();
- while (this.getToken().type === this.lexerClass.MULTI_OP) {
- const multOpToken = this.getToken();
- this.pos++;
- const op = convertFromString(multOpToken.text);
- const term2 =this.parseTerm();
- const finalExp = new Expressions.InfixApp(op, term, term2);
- finalExp.sourceInfo = SourceInfo.createSourceInfo(multOpToken);
- term = finalExp;
- }
- return term;
- }
- parseTerm () {
- const token = this.getToken();
- let sourceInfo = null;
- let exp = null;
- switch(token.type) {
- case this.lexerClass.SUM_OP:
- this.pos++;
- sourceInfo = SourceInfo.createSourceInfo(token);
- exp = new Expressions.UnaryApp(convertFromString(token.text), this.parseTerm());
- exp.sourceInfo = sourceInfo;
- return exp;
- case this.lexerClass.INTEGER:
- this.pos++;
- return this.getIntLiteral(token);
- case this.lexerClass.REAL:
- this.pos++;
- return this.getRealLiteral(token);
- case this.lexerClass.STRING:
- this.pos++;
- return this.getStringLiteral(token);
- case this.lexerClass.CHARACTER:
- this.pos++;
- return this.getCharLiteral(token);
- case this.lexerClass.RK_TRUE:
- case this.lexerClass.RK_FALSE:
- this.pos++;
- return this.getBoolLiteral(token);
- case this.lexerClass.OPEN_CURLY:
- // No more annonymous array
- // return this.parseArrayLiteral();
- throw SyntaxErrorFactory.annonymous_array_literal(token);
- case this.lexerClass.ID:
- case this.lexerClass.LIB_ID:
- return this.parseIDTerm();
- case this.lexerClass.OPEN_PARENTHESIS:
- return this.parseParenthesisExp();
- default:
- throw SyntaxErrorFactory.invalid_terminal(token);
- }
- }
- parseIDTerm () {
- const tokenA = this.getToken();
- const id = this.parseMaybeLibID();
- const isID = tokenA.type === this.lexerClass.ID;
- if(isID && this.checkOpenBrace(true)) {
- let tokenB = null;
- this.pos++;
- const firstIndex = this.parseExpression();
- let secondIndex = null;
- this.consumeNewLines();
- this.checkCloseBrace();
- tokenB = this.getToken();
- this.pos++;
- if(this.checkOpenBrace(true)){
- this.pos++;
- secondIndex = this.parseExpression();
- this.consumeNewLines();
- this.checkCloseBrace();
- tokenB = this.getToken();
- this.pos++;
- }
- const sourceInfo = SourceInfo.createSourceInfoFromList(tokenA, tokenB);
- const exp = new Expressions.ArrayAccess(id, firstIndex, secondIndex);
- exp.sourceInfo = sourceInfo;
- return exp;
- } else if (this.checkOpenParenthesis(true)) {
- return this.parseFunctionCallExpression(id);
- } else if (isID) {
- const sourceInfo = SourceInfo.createSourceInfo(tokenA);
- const exp = new Expressions.VariableLiteral(id);
- exp.sourceInfo = sourceInfo;
- return exp;
- } else {
- throw SyntaxErrorFactory.invalid_id_format(tokenA);
- }
- }
- getFunctionName (id) {
- const name = LanguageDefinedFunction.getInternalName(id);
- if (name === null) {
- if (id === LanguageDefinedFunction.getMainFunctionName()) {
- return null;
- }
- return id;
- } else {
- return name;
- }
- }
- parseFunctionCallExpression (id) {
- const tokenA = this.getToken(this.pos - 1);
- const actualParameters = this.parseActualParameters();
- const tokenB = this.getToken(this.pos - 1);
- const funcName = this.getFunctionName(id);
- const sourceInfo = SourceInfo.createSourceInfoFromList(tokenA, tokenB);
- const cmd = new Expressions.FunctionCall(funcName, actualParameters);
- cmd.sourceInfo = sourceInfo;
- return cmd;
- }
- parseFunctionCallCommand (id) {
- return this.parseFunctionCallExpression(id);
- }
- parseParenthesisExp () {
- this.checkOpenParenthesis();
- const tokenA = this.getToken();
- this.pos += 1;
- this.consumeNewLines();
- const exp = this.parseExpressionOR();
- this.consumeNewLines();
- this.checkCloseParenthesis();
- const tokenB = this.getToken();
- this.pos += 1;
- exp.sourceInfo = SourceInfo.createSourceInfoFromList(tokenA, tokenB);
- exp.parenthesis = true;
- return exp;
- }
- parseActualParameters () {
- this.checkOpenParenthesis();
- this.pos++;
- if(this.checkCloseParenthesis(true)) {
- this.pos++;
- return [];
- }
- this.consumeNewLines();
- const list = this.parseExpressionList();
- this.consumeNewLines();
- this.checkCloseParenthesis();
- this.pos++;
- return list;
- }
- parseExpressionList () {
- const list = [];
- for(;;) {
- const exp = this.parseExpressionOR();
- list.push(exp);
- const maybeToken = this.getToken();
- if (maybeToken.type !== this.lexerClass.COMMA) {
- break;
- } else {
- this.pos++;
- this.consumeNewLines();
- }
- }
- return list;
- }
- getTypeArray () {
- const types = this.insideScope(IVProgParser.FUNCTION) ? this.functionTypes : this.variableTypes;
- return types.map( x => this.lexer.literalNames[x]);
- }
- }
|