public override void Parse(TokensStack sTokens) // let ID = EXP; { Token tLet = sTokens.Pop(); //let if (!(tLet.ToString().Equals("let"))) { throw new SyntaxErrorException("Expected let vartype, recived " + tLet, tLet); } Token tName = sTokens.Pop(); //ID if (!(tName is Identifier)) { throw new SyntaxErrorException("Expected var name, received " + tName, tName); } Variable = ((Identifier)tName).Name; Token tEqual = sTokens.Pop(); if (!tEqual.ToString().Equals("=")) //= { throw new SyntaxErrorException("Expected symbol '=', received " + tEqual, tEqual); } Value = Expression.Create(sTokens); Value.Parse(sTokens); //EXP Token tEnd = sTokens.Pop(); //; if (!(tEnd is Separator)) { throw new SyntaxErrorException("Expected seperator ';', recived " + tEnd, tEnd); } }
public override void Parse(TokensStack sTokens) { Token token; token = sTokens.Pop(); // 'let' if (!(token is Statement) && ((Statement)token).Name != "let") { throw new SyntaxErrorException($"Expected 'let' but got {token}", token); } token = sTokens.Pop(); if (!(token is Identifier)) { throw new SyntaxErrorException($"Expected an identifier but got {token}", token); } Variable = ((Identifier)token).Name; token = sTokens.Pop(); // '=' if (!(token is Operator) || ((Operator)token).Name != '=') { throw new SyntaxErrorException($"Expected a '=' but saw '{token}'", token); } Expression value = Expression.Create(sTokens); value.Parse(sTokens); Value = value; token = sTokens.Pop(); // ';' if (!(token is Separator) || ((Separator)token).Name != ';') { throw new SyntaxErrorException($"Expected a ';' but saw '{token}'", token); } }
//parsing by the format functionname-(arguments){} public override void Parse(TokensStack sTokens) { Args = new List <Expression>(); Token tName = sTokens.Pop(); if (!(tName is Identifier)) { throw new SyntaxErrorException("Expected identifier name, received " + tName, tName); } FunctionName = ((Identifier)tName).Name; Token t = sTokens.Pop(); //( while (sTokens.Count > 0) //) { if (sTokens.Peek() is Parentheses && ((Parentheses)sTokens.Peek()).Name == ')') { break; } if (sTokens.Count < 1) { throw new SyntaxErrorException("Early termination ", t); } Expression tExpression = Expression.Create(sTokens); tExpression.Parse(sTokens); Args.Add(tExpression); if (sTokens.Count > 0 && sTokens.Peek() is Separator)//, { sTokens.Pop(); } } Token tEnd = sTokens.Pop();//} }
public override void Parse(TokensStack sTokens) { Token FName = sTokens.Pop(); if (!(FName is Identifier)) { throw new SyntaxErrorException("Expected function name, received " + FName, FName); } FunctionName = ((Identifier)FName).Name; Token par = sTokens.Pop(); if (!(par is Parentheses) || ((Parentheses)par).Name != '(') { throw new SyntaxErrorException("Expected function name, received " + par, par); } while (sTokens.Count > 0 && !(sTokens.Peek() is Parentheses))//) { Expression tmp = Expression.Create(sTokens); tmp.Parse(sTokens); Args.Add(tmp); //If there is a comma, then there is another argument if (sTokens.Count > 0 && sTokens.Peek() is Separator)//, { sTokens.Pop(); } } Token par1 = sTokens.Pop(); if (!(par1 is Parentheses) || ((Parentheses)par).Name != ')') { throw new SyntaxErrorException("Expected function name, received " + par1, par1); } }
public override void Parse(TokensStack sTokens) { Token token; token = sTokens.Pop(); // '(' if (!(token is Parentheses) || ((Parentheses)token).Name != '(') { throw new SyntaxErrorException($"Expected a '(' but saw '{token}'", token); } Expression op1 = Create(sTokens); op1.Parse(sTokens); Operand1 = op1; token = sTokens.Pop(); // binary operator if (!(token is Operator) || !Token.BinaryOperators.Contains(((Operator)token).Name)) { throw new SyntaxErrorException($"Expected a binary operator but saw '{token}'", token); } Operator = ((Operator)token).Name.ToString(); Expression op2 = Create(sTokens); op2.Parse(sTokens); Operand2 = op2; token = sTokens.Pop(); // ')' if (!(token is Parentheses) || ((Parentheses)token).Name != ')') { throw new SyntaxErrorException($"Expected a ')' but saw '{token}'", token); } }
public override void Parse(TokensStack sTokens) { Token tLet = sTokens.Pop(); if (!(tLet is Statement) || !((Statement)tLet).Name.Equals("let")) { throw new SyntaxErrorException("$Expected let", tLet); } Token tId = sTokens.Pop(); if (!(tId is Identifier)) { throw new SyntaxErrorException("$Expected identifier", tId); } else { this.Variable = ((Identifier)tId).ToString(); } Token tEqual = sTokens.Pop(); if (!(tEqual is Operator) || ((Operator)tEqual).Name != '=') { throw new SyntaxErrorException("$Expected =", tEqual); } Value = Expression.Create(sTokens); Value.Parse(sTokens); // Token tEnd = sTokens.Pop(); //if (!(tEnd is Separator) || ((Separator)tEnd).Name != ';') // throw new SyntaxErrorException("$Expected ;" + tEnd.ToString(), tEnd); }
//parsing by the format let variableName=expression public override void Parse(TokensStack sTokens) { if (sTokens.Count < 5) { throw new SyntaxErrorException("Early termination ", sTokens.LastPop); } Token tLet = sTokens.Pop(); if (!(tLet is Statement) || ((Statement)tLet).Name != "let") { throw new SyntaxErrorException("Expected let statment , received " + tLet, tLet); } Token tName = sTokens.Pop(); if (!(tName is Identifier)) { throw new SyntaxErrorException("Expected identifier name, received " + tName, tName); } Variable = ((Identifier)tName).Name; Token tEqual = sTokens.Pop(); if (!(tEqual is Operator) || ((Operator)tEqual).Name != '=') { throw new SyntaxErrorException("Expected = , received " + tEqual, tEqual); } Value = Expression.Create(sTokens); Value.Parse(sTokens); if (!(sTokens.Peek() is Separator) || ((Separator)sTokens.Peek()).Name != ';') { throw new SyntaxErrorException("Expected ; , received ", sTokens.Peek()); } sTokens.Pop(); }
public override void Parse(TokensStack sTokens) { Token token; token = sTokens.Pop(); // 'while' if (!(token is Statement) && ((Statement)token).Name != "while") { throw new SyntaxErrorException($"Expected 'while' but got {token}", token); } token = sTokens.Pop(); // '(' if (!(token is Parentheses) || ((Parentheses)token).Name != '(') { throw new SyntaxErrorException($"Expected a '(' but saw '{token}'", token); } Expression term = Expression.Create(sTokens); term.Parse(sTokens); Term = term; token = sTokens.Pop(); // ')' if (!(token is Parentheses) || ((Parentheses)token).Name != ')') { throw new SyntaxErrorException($"Expected a ')' but saw '{token}'", token); } Body = new List <StatetmentBase>(); ParseStatements(sTokens, Body); }
public override void Parse(TokensStack sTokens) { //First, we remove the "Let" token Token tRet = sTokens.Pop();//Let //Now, we create the correct Expression type based on the top token in the stack Token tid1 = sTokens.Pop(); if (!(tid1 is Identifier)) { throw new SyntaxErrorException("Expected function received: " + tid1, tid1); } Variable = ((Identifier)tid1).Name; //We transfer responsibility of the parsing to the created expression Token tid2 = sTokens.Pop(); if (!(tid2 is Symbol) || ((Symbol)tid2).Name != '=') { throw new SyntaxErrorException("Expected function received: " + tid2, tid2); } Expression tmp = Expression.Create(sTokens); tmp.Parse(sTokens); Value = tmp; //After the expression was parsed, we expect to see ; Token tEnd = sTokens.Pop();//; }
public override void Parse(TokensStack sTokens) { Token tRet = sTokens.Pop();//return Expression = Expression.Create(sTokens); Expression.Parse(sTokens); Token tEnd = sTokens.Pop();//; }
public override void Parse(TokensStack sTokens) { Token tFunc = sTokens.Pop(); if (!(tFunc is Statement) || ((Statement)tFunc).Name != "function") { throw new SyntaxErrorException("Expected function received: " + tFunc, tFunc); } Token tType = sTokens.Pop(); if (!(tType is VarType)) { throw new SyntaxErrorException("Expected var type, received " + tType, tType); } ReturnType = VarDeclaration.GetVarType(tType); Token tName = sTokens.Pop(); if (!(tName is Identifier)) { throw new SyntaxErrorException("Expected function name, received " + tType, tType); } Name = ((Identifier)tName).Name; Token t = sTokens.Pop(); //( while (sTokens.Count > 0 && !(sTokens.Peek() is Parentheses)) //) { if (sTokens.Count < 3) { throw new SyntaxErrorException("Early termination ", t); } Token tArgType = sTokens.Pop(); Token tArgName = sTokens.Pop(); VarDeclaration vc = new VarDeclaration(tArgType, tArgName); Args.Add(vc); if (sTokens.Count > 0 && sTokens.Peek() is Separator)//, { sTokens.Pop(); } } t = sTokens.Pop(); //) t = sTokens.Pop(); //{ while (sTokens.Count > 0 && (sTokens.Peek() is Statement) && (((Statement)sTokens.Peek()).Name == "var")) { VarDeclaration local = new VarDeclaration(); local.Parse(sTokens); Locals.Add(local); } while (sTokens.Count > 0 && !(sTokens.Peek() is Parentheses)) { StatetmentBase s = StatetmentBase.Create(sTokens.Peek()); s.Parse(sTokens); Body.Add(s); } Token tEnd = sTokens.Pop();//} }
public override void Parse(TokensStack sTokens) { /* * "(" + Operator + " " + Operand1 + " " + Operand2 + ")" */ Token t; StackIsNotEmpty(sTokens); if (sTokens.Peek() is Parentheses && sTokens.Peek().ToString() == "(") { t = sTokens.Pop(); //( } else { throw new SyntaxErrorException("Expected ( Parentheses received: " + sTokens.Peek().ToString(), sTokens.Peek()); } StackIsNotEmpty(sTokens); if (sTokens.Peek() is Operator) { Token tOperator = sTokens.Pop(); //Operator Operator = ((Operator)tOperator).Name.ToString(); } else { throw new SyntaxErrorException("Expected Operator received: " + sTokens.Peek().ToString(), sTokens.Peek()); } StackIsNotEmpty(sTokens); //Operand1 Operand1 = Expression.Create(sTokens); if (Operand1 == null) { Token tOp1 = new Token(); throw new SyntaxErrorException("Invalid Expression Exception", tOp1); } Operand1.Parse(sTokens); //Operand2 Operand2 = Expression.Create(sTokens); if (Operand2 == null) { Token tOp2 = new Token(); throw new SyntaxErrorException("Invalid Expression Exception", tOp2); } Operand2.Parse(sTokens); StackIsNotEmpty(sTokens); if (sTokens.Peek() is Parentheses && sTokens.Peek().ToString() == ")") { t = sTokens.Pop(); //) } else { throw new SyntaxErrorException("Expected ) Parentheses received: " + sTokens.Peek().ToString(), sTokens.Peek()); } }
public override void Parse(TokensStack sTokens) { /* * funcname '(' exprssion list ')' */ Args = new List <Expression>(); StackIsNotEmpty(sTokens); Token t; if (sTokens.Peek() is Identifier) { Token tFuncCallExp = sTokens.Pop(); //func name FunctionName = ((Identifier)tFuncCallExp).Name; } else { throw new SyntaxErrorException("Expected Function name, received " + sTokens.Peek().ToString(), sTokens.Peek()); } StackIsNotEmpty(sTokens); if (sTokens.Peek() is Parentheses && sTokens.Peek().ToString() == "(") { t = sTokens.Pop(); //( } else { throw new SyntaxErrorException("Expected ( Parentheses received: " + sTokens.Peek().ToString(), sTokens.Peek()); } //expression list while ((sTokens.Count > 0 && !(sTokens.Peek() is Parentheses) || (sTokens.Peek().ToString() != ")"))) { if (sTokens.Count < 3) { throw new SyntaxErrorException("Early termination ", t); } Expression exp = Expression.Create(sTokens); exp.Parse(sTokens); Args.Add(exp); if (sTokens.Count > 0 && sTokens.Peek() is Separator)//, { sTokens.Pop(); } } StackIsNotEmpty(sTokens); if (sTokens.Peek() is Parentheses && sTokens.Peek().ToString() == ")") { t = sTokens.Pop(); //) } else { throw new SyntaxErrorException("Expected ) Parentheses received: " + sTokens.Peek().ToString(), sTokens.Peek()); } }
public override void Parse(TokensStack sTokens) { /* * 'let' Variable '=' Value ';' */ StackIsNotEmpty(sTokens); if (sTokens.Peek() is Statement && sTokens.Peek().ToString() == "let") { Token tStateLet = sTokens.Pop(); //let } else { throw new SyntaxErrorException("Expected let statment received: " + sTokens.Peek().ToString(), sTokens.Peek()); } StackIsNotEmpty(sTokens); if (sTokens.Peek() is Identifier) { Token tVariable = sTokens.Pop(); //Variable Variable = tVariable.ToString(); } else { throw new SyntaxErrorException("Expected Variable name received: " + sTokens.Peek().ToString(), sTokens.Peek()); } StackIsNotEmpty(sTokens); if ((sTokens.Peek() is Operator) && sTokens.Peek().ToString() == "=") { Token tEqual = sTokens.Pop(); //= } else { throw new SyntaxErrorException("Expected = Operator, received " + sTokens.Peek().ToString(), sTokens.Peek()); } Value = Expression.Create(sTokens);//Value if (Value == null) { Token t = new Token(); throw new SyntaxErrorException("Invalid Exception", t); } Value.Parse(sTokens); StackIsNotEmpty(sTokens); if ((sTokens.Peek() is Separator) && sTokens.Peek().ToString() == ";") { Token tEndLet = sTokens.Pop(); //; } else { throw new SyntaxErrorException("Expected ; Separator received " + sTokens.Peek().ToString(), sTokens.Peek()); } }
public override void Parse(TokensStack sTokens) { Token tRet = sTokens.Pop();//return Expression = Expression.Create(sTokens); Expression.Parse(sTokens); Token tEnd = sTokens.Pop();//; if (((tEnd is Separator) && !(((Separator)tEnd).Name.Equals(';'))) || !(tEnd is Separator)) // { throw new SyntaxErrorException("token isn't ;", tEnd); } }
public override void Parse(TokensStack sTokens) { Args = new List <Expression>(); //function name Token token = sTokens.Pop(); if (!(token is Identifier)) { throw new SyntaxErrorException("Expected function name Identifier, received " + token, token); } FunctionName = ((Identifier)token).Name; //check for '(' token = sTokens.Pop(); if (!(token is Parentheses) || ((Parentheses)token).Name != '(') { throw new SyntaxErrorException("Expected '(' for function args, received " + token, token); } //define args until closing ')' for args token = sTokens.Peek(); while (!(token is Parentheses) || (((Parentheses)token).Name != ')')) { Expression expression = Expression.Create(sTokens); expression.Parse(sTokens); Args.Add(expression); //expect additional arg if theres a comma if (sTokens.Count > 0 && sTokens.Peek() is Separator)//, { token = sTokens.Pop(); if (((Separator)token).Name != ',') { throw new SyntaxErrorException(@"Expected , (comma), received " + token, token); } } token = sTokens.Peek(); } //check for ')' token = sTokens.Pop(); if (!(token is Parentheses) || ((Parentheses)token).Name != ')') { throw new SyntaxErrorException("Expected ')' for function args, received " + token, token); } }
//parsing by the format operand-expression-expression public override void Parse(TokensStack sTokens) { Token t = sTokens.Pop(); //( if (sTokens.Count < 3 || !(sTokens.Peek(0) is Operator)) { throw new SyntaxErrorException("Expected Operator, recieved ", sTokens.Peek(0)); } Operator = ((Operator)sTokens.Pop()).Name.ToString(); Operand1 = Expression.Create(sTokens); Operand1.Parse(sTokens); Operand2 = Expression.Create(sTokens); Operand2.Parse(sTokens); t = sTokens.Pop(); //) }
public override void Parse(TokensStack sTokens) { Token tVar = sTokens.Pop(); if (!(tVar is Statement) || (((Statement)tVar).Name != "var")) { throw new SyntaxErrorException("Expected var, received " + tVar, tVar); } SetTypeAndName(sTokens.Pop(), sTokens.Pop()); Token tEnd = sTokens.Pop(); if (!(tEnd is Separator) || (((Separator)tEnd).Name != ';')) { throw new SyntaxErrorException("Expected ; received " + tEnd, tEnd); } }
public override void Parse(TokensStack sTokens) { Identifier t = (Identifier)sTokens.Pop(); Name = t.Name; ToString(); }
public override void Parse(TokensStack sTokens) { Token token = sTokens.Pop(); if (token is Operator) { if (((Operator)token).Name == '!') { Operator = "!"; } else if (((Operator)token).Name == '-') { Operator = "-"; } else { throw new SyntaxErrorException("expected unary operator, received " + token, token); } Operand = Expression.Create(sTokens); Operand.Parse(sTokens); //Token tEnd = sTokens.Pop();// check for ";" //if (!(tEnd is Separator) || ((Separator)tEnd).Name != ';') throw new SyntaxErrorException("; expected, received " + tEnd, tEnd); } else { throw new SyntaxErrorException("expected unary operator, received " + token, token); } }
public override void Parse(TokensStack sTokens) { Token tFun = sTokens.Pop(); if (!(tFun is Identifier)) { throw new SyntaxErrorException("$Expected identifier", tFun); } else { this.FunctionName = ((Identifier)tFun).ToString(); } Token fun_open = sTokens.Pop(); if (!(fun_open is Parentheses) || ((Parentheses)fun_open).Name != '(') { throw new SyntaxErrorException("$Expected (", fun_open); } Args = new List <Expression>(); // problematic scope! while (sTokens.Count > 0 && !((sTokens.Peek() is Parentheses) && ((Parentheses)sTokens.Peek()).Name == ')')) { Expression arg = Expression.Create(sTokens); arg.Parse(sTokens); Args.Add(arg); if (sTokens.Count > 0 && sTokens.Peek() is Separator)//, { sTokens.Pop(); } } Token fun_close = sTokens.Pop(); if (!(fun_close is Parentheses) || ((Parentheses)fun_close).Name != ')') { throw new SyntaxErrorException("$Expected )" + fun_close.ToString() + " " + sTokens.Pop().ToString() + " " + sTokens.Pop().ToString() + " " + sTokens.Pop().ToString(), fun_close); } //Token tEnd = sTokens.Pop(); // if (!(tEnd is Separator) || ((Separator)tEnd).Name != ';') // throw new SyntaxErrorException("$Expected ;", tEnd); }
public override void Parse(TokensStack sTokens) { if (sTokens.Peek() is Parentheses && ((Parentheses)sTokens.Peek()).Name.Equals('(')) { Token t1 = sTokens.Pop(); } else { throw new SyntaxErrorException("missing ( ", sTokens.Peek()); } if (sTokens.Peek() is Operator) { Operator = "" + ((Operator)sTokens.Pop()).Name; } else { throw new SyntaxErrorException("no operator", sTokens.Peek()); } Operand1 = Expression.Create(sTokens); if (Operand1 != null) { Operand1.Parse(sTokens); } else { throw new SyntaxErrorException("Bad Ope1", new Token()); } Operand2 = Expression.Create(sTokens); if (Operand2 != null) { Operand2.Parse(sTokens); } else { throw new SyntaxErrorException("Bad Ope2", new Token()); } if (sTokens.Peek() is Parentheses && ((Parentheses)sTokens.Peek()).Name.Equals(')')) { Token t2 = sTokens.Pop(); } else { throw new SyntaxErrorException("misssing (", sTokens.Pop()); } }
public override void Parse(TokensStack sTokens) { Args = new List <Expression>(); Token tFuncName = sTokens.Pop(); if (!(tFuncName is Identifier)) { throw new SyntaxErrorException("Expected Identifier var, recived " + tFuncName, tFuncName); } FunctionName = tFuncName.ToString(); Token tParentheses = sTokens.Pop(); if (!(tParentheses is Parentheses) && (tParentheses).ToString().Equals("(")) { throw new SyntaxErrorException("Expected Parentheses '(', recived " + tParentheses, tParentheses); } while (sTokens.Count > 0 && !(sTokens.Peek() is Parentheses && (sTokens.Peek(0)).ToString().Equals(")"))) { if (sTokens.Peek() is Separator) { Token tComa = sTokens.Pop(); if (!(tComa.ToString().Equals(","))) { throw new SyntaxErrorException("Expected Seperator ',', recived " + tComa, tComa); } } else { Expression eArg = Expression.Create(sTokens); eArg.Parse(sTokens); Args.Add(eArg); } //else // throw new SyntaxErrorException("Expectes Expressions Args or Comas, recived " + sTokens.Peek(0), sTokens.Peek(0)); } if (sTokens.Count < 1) { throw new Exception("Code Terminated"); } Token eParentheses = sTokens.Pop(); if (!(eParentheses is Parentheses) && (eParentheses).ToString().Equals(")")) { throw new SyntaxErrorException("Expected Parentheses ')', recived " + eParentheses, eParentheses); } }
public override void Parse(TokensStack sTokens) { Token tNum = sTokens.Pop(); if (tNum is Number) { Value = ((Number)tNum).Value; } }
public override void Parse(TokensStack sTokens) { Token token = sTokens.Pop(); if (!(token is Identifier)) { throw new SyntaxErrorException("expected identifier, received " + token, token); } Name = ((Identifier)token).Name; }
public override void Parse(TokensStack sTokens) { Token open = sTokens.Pop(); if (!(open is Parentheses) || ((Parentheses)open).Name != '(') { throw new SyntaxErrorException("$Expected (" + open, open); } Operand1 = Expression.Create(sTokens); Operand1.Parse(sTokens); // while (!(sTokens.Peek() is Operator)) // { // sTokens.Pop(); // } Token tOperator = sTokens.Pop(); if (!(tOperator is Operator)) { throw new SyntaxErrorException("$Expected operator ", tOperator); } else { Operator = ((Operator)tOperator).Name.ToString(); } Operand2 = Expression.Create(sTokens); Operand2.Parse(sTokens); //while (!(sTokens.Peek() is Parentheses)) // { // sTokens.Pop(); // } Token close = sTokens.Pop(); if (!(close is Parentheses) || ((Parentheses)close).Name != ')') { throw new SyntaxErrorException("$Expected )", close); } }
public override void Parse(TokensStack sTokens) { Token tNum = sTokens.Pop(); if (!(tNum is Number)) { throw new SyntaxErrorException($"Expected a number but got {tNum}", tNum); } Value = ((Number)tNum).Value; }
public override void Parse(TokensStack sTokens) { Body = new List <StatetmentBase>(); if (sTokens.Count < 6) { throw new SyntaxErrorException("Early termination ", sTokens.LastPop); } Token tWhile = sTokens.Pop(); if (!(tWhile is Statement) || ((Statement)tWhile).Name != "while") { throw new SyntaxErrorException("Expected while statment , received " + tWhile, tWhile); } Token t = sTokens.Pop(); //( if (!(t is Parentheses) || ((Parentheses)t).Name != '(') { throw new SyntaxErrorException("Expected ( , received ", t); } Expression pTerm = (Expression.Create(sTokens)); pTerm.Parse(sTokens); Term = pTerm; t = sTokens.Pop(); //( if (!(t is Parentheses) || ((Parentheses)t).Name != ')') { throw new SyntaxErrorException("Expected ) , received ", t); } t = sTokens.Pop(); //( if (!(t is Parentheses) || ((Parentheses)t).Name != '{') { throw new SyntaxErrorException("Expected { , received ", t); } while (sTokens.Count > 0 && !(sTokens.Peek() is Parentheses)) { StatetmentBase s = StatetmentBase.Create(sTokens.Peek()); s.Parse(sTokens); Body.Add(s); } Token tEnd = sTokens.Pop();//} }
public override void Parse(TokensStack sTokens) { Token token; token = sTokens.Pop(); if (!(token is Identifier)) { throw new SyntaxErrorException($"Expected an identifier but got {token}", token); } FunctionName = ((Identifier)token).Name; token = sTokens.Pop(); // '(' if (!(token is Parentheses) || ((Parentheses)token).Name != '(') { throw new SyntaxErrorException($"Expected a '(' but saw '{token}'", token); } Args = new List <Expression>(); while (sTokens.Count > 0 && (!(sTokens.Peek() is Parentheses) || ((Parentheses)sTokens.Peek()).Name != ')')) { Expression expression = Create(sTokens); expression.Parse(sTokens); Args.Add(expression); //If there is a comma, then there is another argument if (sTokens.Count > 0 && sTokens.Peek() is Separator) //, { token = sTokens.Pop(); // ',' if (!(token is Separator) || ((Separator)token).Name != ',') { throw new SyntaxErrorException($"Expected a ',' but saw '{token}'", token); } } } token = sTokens.Pop(); // ')' if (!(token is Parentheses) || ((Parentheses)token).Name != ')') { throw new SyntaxErrorException($"Expected a ')' but saw '{token}'", token); } }
//This is an example of the implementation of the Parse method //You need to add here correctness checks. public override void Parse(TokensStack sTokens) { //First, we remove the "return" token Token tRet = sTokens.Pop();//return if (!(tRet is Keyword) || ((Keyword)tRet).Name != "return") { throw new SyntaxErrorException("expected 'return' keyword, received, " + tRet, tRet); } //Now, we create the correct Expression type based on the top token in the stack Expression = Expression.Create(sTokens); //We transfer responsibility of the parsing to the created expression Expression.Parse(sTokens); //After the expression was parsed, we expect to see ; Token tEnd = sTokens.Pop();//; if (!(tEnd is Separator) || ((Separator)tEnd).Name != ';') { throw new SyntaxErrorException("; expected, received " + tEnd, tEnd); } }