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();//} }
//This is an example of the implementation of the Parse method public override void Parse(TokensStack sTokens) { //We check that the first token is "function" Token tFunc = sTokens.Pop(); if (!(tFunc is Statement) || ((Statement)tFunc).Name != "function") { throw new SyntaxErrorException("Expected function received: " + tFunc, tFunc); } //Now there should be the return type. We pop it from the stack, check for errors, and then set the field Token tType = sTokens.Pop(); if (!(tType is VarType)) { throw new SyntaxErrorException("Expected var type, received " + tType, tType); } ReturnType = VarDeclaration.GetVarType(tType); //Next is the function name Token tName = sTokens.Pop(); if (!(tName is Identifier)) { throw new SyntaxErrorException("Expected function name, received " + tType, tType); } Name = ((Identifier)tName).Name; //After the name there should be opening paranthesis for the arguments Token t = sTokens.Pop(); //( if (!(t is Parentheses) || ((Parentheses)t).Name != '(') { throw new SyntaxErrorException($"Expected a '(' but saw '{t}'", t); } //Now we extract the arguments from the stack until we see a closing parathesis while (sTokens.Count > 0 && !(sTokens.Peek() is Parentheses))//) { //For each argument there should be a type, and a name 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 there is a comma, then there is another argument if (sTokens.Count > 0 && sTokens.Peek() is Separator) //, { t = sTokens.Pop(); if (!(t is Separator) || ((Separator)t).Name != ',') { throw new SyntaxErrorException($"Expected a ',' but saw '{t}'", t); } } } //Now we pop out the ) and the {. Note that you need to check that the stack contains the correct symbols here. t = sTokens.Pop();//) if (!(t is Parentheses) || ((Parentheses)t).Name != ')') { throw new SyntaxErrorException($"Expected a ')' but saw '{t}'", t); } t = sTokens.Pop();//{ if (!(t is Parentheses) || ((Parentheses)t).Name != '{') { throw new SyntaxErrorException($"Expected a '{{' but saw '{t}'", t); } //Now we parse the list of local variable declarations while (sTokens.Count > 0 && (sTokens.Peek() is Statement) && (((Statement)sTokens.Peek()).Name == "var")) { VarDeclaration local = new VarDeclaration(); //We call the Parse method of the VarDeclaration, which is responsible to parsing the elements of the variable declaration local.Parse(sTokens); Locals.Add(local); } //Now we parse the list of statements while (sTokens.Count > 0 && !(sTokens.Peek() is Parentheses)) { //We create the correct Statement type (if, while, return, let) based on the top token in the stack StatetmentBase s = StatetmentBase.Create(sTokens.Peek()); //And call the Parse method of the statement to parse the different parts of the statement s.Parse(sTokens); Body.Add(s); } bool hasReturn = false; if (Body.Count > 0) { StatetmentBase lastStatetment = Body[Body.Count - 1]; Body.RemoveAt(Body.Count - 1); if (lastStatetment is ReturnStatement) { Return = (ReturnStatement)lastStatetment; hasReturn = true; } else { hasReturn = false; } } //Need to check here that the last statement is a return statement //Finally, the function should end with } Token tEnd = sTokens.Pop();//} if (!hasReturn) { throw new SyntaxErrorException("Missing a return statement.", tEnd); } if (!(tEnd is Parentheses) || ((Parentheses)tEnd).Name != '}') { throw new SyntaxErrorException($"Expected a '}}' but saw '{t}'", t); } }
public override void Parse(TokensStack sTokens) { /* * Function Type ID( args ){ * varDec* * Statments* * Return Statment * } * * check more errors */ Token t; StackIsNotEmpty(sTokens); Token tFunc = sTokens.Pop(); if (!(tFunc is Statement) || ((Statement)tFunc).Name != "function") { throw new SyntaxErrorException("Expected function received: " + tFunc, tFunc); } StackIsNotEmpty(sTokens); Token tType = sTokens.Pop(); if (!(tType is VarType)) { throw new SyntaxErrorException("Expected var type, received " + tType, tType); } ReturnType = VarDeclaration.GetVarType(tType); StackIsNotEmpty(sTokens); Token tName = sTokens.Pop(); if (!(tName is Identifier)) { throw new SyntaxErrorException("Expected function name, received " + tType, tType); } Name = ((Identifier)tName).Name; 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); 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(); } } 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 Parentheses && sTokens.Peek().ToString() == "{") { t = sTokens.Pop(); //{ } else { throw new SyntaxErrorException("Expected { Parentheses received: " + sTokens.Peek().ToString(), sTokens.Peek()); } StackIsNotEmpty(sTokens); while (sTokens.Count > 0 && (sTokens.Peek() is Statement) && (((Statement)sTokens.Peek()).Name == "var")) { VarDeclaration local = new VarDeclaration(); local.Parse(sTokens); Locals.Add(local); } StackIsNotEmpty(sTokens); while (sTokens.Count > 0 && !(sTokens.Peek() is Parentheses)) { StatetmentBase s = StatetmentBase.Create(sTokens.Peek()); s.Parse(sTokens); Body.Add(s); } 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()); } }