Exemplo n.º 1
0
        private NodeCollection parseStatement()
        {
            NodeCollection nc;

            // statement = "if", expression, statement-list
            //if (Grammar.isControl(currentScannerToken))
            //{
            //    StatementList sl;
            //    GingerToken controlToken = currentScannerToken;

            //    nextScannerToken();

            //    Node conditionExpression = parseExpression();

            //    nextScannerToken();
            //    if (currentScannerToken == GingerToken.OpenStatementList)
            //    {
            //        nextScannerToken();
            //        sl = parseStatementList(GingerToken.CloseStatementList);
            //    }
            //    else
            //    {
            //        throw new ParseException(scanner.row, scanner.col, $"Expected '{GingerToken.OpenStatementList.ToString()}', found '{currentScannerToken.ToString()}'", ExceptionLevel.ERROR);
            //    }

            //    nc = new If(conditionExpression, sl);
            //}
            // statement = variable
            if (Grammar.isType(currentScannerToken))
            {
                nc = parseVariableDeclaration();
            }
            // statement = identifier, assignment, expression
            else if (currentScannerToken == GingerToken.Identifier)
            {
                //Identifier identifier = new Identifier(scanner.row, scanner.col, new string(scanner.tokenValue));
                Identifier identifier = parseIdentifier(true);
                nextScannerToken();
                if (currentScannerToken == GingerToken.Assignment)
                {
                    nextScannerToken();
                    nc = new Assign(identifier, parseExpression());
                }
                else if (currentScannerToken == GingerToken.OpenPrecedent)
                {
                    ExpressionList exprList = parseExpressionList();

                    // close-precedent
                    if (currentScannerToken == GingerToken.ClosePrecedent)
                    {
                        nc = new Invocation(identifier, exprList);
                    }
                    else
                    {
                        throw new ParseException(scanner.row, scanner.col, $"Expected '{GingerToken.ClosePrecedent.ToString()}', found '{currentScannerToken.ToString()}'", ExceptionLevel.ERROR);
                    }
                }
                else
                {
                    throw new ParseException(scanner.row, scanner.col, $"Expected '{GingerToken.Assignment.ToString()} or {GingerToken.OpenPrecedent.ToString()}', found '{currentScannerToken.ToString()}'", ExceptionLevel.ERROR);
                }
            }
            // statement = return [expression]
            else if (currentScannerToken == GingerToken.Return)
            {
                Node expression = null;
                spyScannerToken();
                if (spiedScannerToken.GetValueOrDefault() != GingerToken.CloseList)
                {
                    nextScannerToken();
                    expression = parseExpression();
                }

                nc = new Return(expression);
            }
            // component = (cont | imp), identifdier, {, function-decs ,}
            //else if (Grammar.isComponent(currentScannerToken))
            //{
            //    Component comp;
            //    Variable componentName;

            //    GingerToken componentType = currentScannerToken;
            //    nextScannerToken();

            //    if (currentScannerToken == GingerToken.Identifier)
            //    {
            //        componentName = new Variable(new Identifier(scanner.row, scanner.col, new string(scanner.tokenValue)));

            //        nextScannerToken();
            //        if (currentScannerToken == GingerToken.OpenList)
            //        {

            //        }
            //        else
            //        {
            //            throw new ParseException(scanner.row, scanner.col, $"Expected '{GingerToken.Identifier.ToString()}', found '{currentScannerToken.ToString()}'", ExceptionLevel.ERROR);
            //        }
            //    }
            //    else
            //    {
            //        throw new ParseException(scanner.row, scanner.col, $"Expected '{GingerToken.Identifier.ToString()}', found '{currentScannerToken.ToString()}'", ExceptionLevel.ERROR);
            //    }

            //}
            // statement = function-declaration = function, type, identifier, open-precedent, variable-list, close-precedent, statement-list
            //else if (currentScannerToken == GingerToken.Function)
            //{
            //    if (inFunction)
            //    {
            //        throw new ParseException(scanner.row, scanner.col, "Cannot declare function in function", ExceptionLevel.ERROR);
            //    }

            //    Variable functionName;
            //    VarList formalArgsList = new VarList();
            //    //Type functionReturnType;
            //    StatementList sl;

            //    // returns
            //    //nextScannerToken();
            //    //if (Grammar.isFunctionType(currentScannerToken))
            //    //{
            //    //    functionReturnType = parseType();
            //    //}
            //    //else
            //    //{
            //    //    throw new ParseException(scanner.row, scanner.col, $"Expected type, found '{currentScannerToken.ToString()}'", ExceptionLevel.ERROR);
            //    //}

            //    // identifier
            //    nextScannerToken();
            //    if (currentScannerToken == GingerToken.Identifier)
            //    {
            //        functionName = new Variable(new Identifier(scanner.row, scanner.col, new string(scanner.tokenValue)));

            //        // open-precedent
            //        nextScannerToken();
            //        if (currentScannerToken == GingerToken.OpenPrecedent)
            //        {
            //            // variable-list
            //            formalArgsList = parseVarList();

            //            // close-precedent
            //            if (currentScannerToken == GingerToken.ClosePrecedent)
            //            {
            //                nextScannerToken();
            //            }
            //            else
            //            {
            //                throw new ParseException(scanner.row, scanner.col, $"Expected '{GingerToken.ClosePrecedent.ToString()}', found '{currentScannerToken.ToString()}'", ExceptionLevel.ERROR);
            //            }

            //            // statement list
            //            if (currentScannerToken == GingerToken.OpenList)
            //            {
            //                nextScannerToken();
            //                sl = parseStatementList(GingerToken.CloseList, true);
            //                //nc = new Function(functionName, functionVariableList, functionReturnType, sl);
            //                nc = new Function(functionName, formalArgsList, sl);
            //            }
            //            else
            //            {
            //                throw new ParseException(scanner.row, scanner.col, $"Expected '{GingerToken.OpenList.ToString()}', found '{currentScannerToken.ToString()}'", ExceptionLevel.ERROR);
            //            }
            //        }
            //        else
            //        {
            //            throw new ParseException(scanner.row, scanner.col, $"Expected '{GingerToken.OpenPrecedent.ToString()}', found '{currentScannerToken.ToString()}'", ExceptionLevel.ERROR);
            //        }
            //    }
            //    else
            //    {
            //        throw new ParseException(scanner.row, scanner.col, $"Expected '{GingerToken.Identifier.ToString()}', found '{currentScannerToken.ToString()}'", ExceptionLevel.ERROR);
            //    }
            //}
            else if (currentScannerToken == GingerToken.Sink)
            {
                nextScannerToken();
                if (currentScannerToken == GingerToken.OpenPrecedent)
                {
                    Node expr;

                    nextScannerToken();
                    expr = parseExpression();
                    nextScannerToken();
                    if (currentScannerToken == GingerToken.ClosePrecedent)
                    {
                        if (currentScannerToken == GingerToken.ClosePrecedent)
                        {
                            nc = new Sink(parseAnnotation(GingerToken.Low), expr);
                        }
                        else
                        {
                            throw new ParseException(scanner.row, scanner.col, $"Expected '{GingerToken.ClosePrecedent.ToString()} or {GingerToken.Sink.ToString()}', found '{currentScannerToken.ToString()}'", ExceptionLevel.ERROR);
                        }
                    }
                    else
                    {
                        throw new ParseException(scanner.row, scanner.col, $"Expected '{GingerToken.ClosePrecedent.ToString()}', found '{currentScannerToken.ToString()}'", ExceptionLevel.ERROR);
                    }
                }
                else
                {
                    throw new ParseException(scanner.row, scanner.col, $"Expected '{GingerToken.OpenList.ToString()}', found '{currentScannerToken.ToString()}'", ExceptionLevel.ERROR);
                }
            }
            // statement = component = comp, identifier, as, component-type, statement-list
            //else if (currentScannerToken == GingerToken.Component)
            //{
            //    Identifier compName;
            //    Type compType;
            //    StatementList sl;

            //    // identifier
            //    nextScannerToken();
            //    if (currentScannerToken == GingerToken.Identifier)
            //    {
            //        compName = new Identifier(scanner.row, scanner.col, new string(scanner.tokenValue));
            //    }
            //    else
            //    {
            //        throw new ParseException(scanner.row, scanner.col, $"Expected '{GingerToken.Identifier.ToString()}', found '{currentScannerToken.ToString()}'", ExceptionLevel.ERROR);
            //    }

            //    // component type
            //    nextScannerToken();
            //    if (currentScannerToken == GingerToken.As)
            //    {
            //        nextScannerToken();
            //        if (Grammar.isComponentType(currentScannerToken))
            //        {
            //            compType = parseType();
            //        }
            //        else
            //        {
            //            throw new ParseException(scanner.row, scanner.col, $"Expected component type, found '{currentScannerToken.ToString()}'", ExceptionLevel.ERROR);
            //        }
            //    }
            //    else
            //    {
            //        throw new ParseException(scanner.row, scanner.col, $"Expected '{GingerToken.As.ToString()}', found '{currentScannerToken.ToString()}'", ExceptionLevel.ERROR);
            //    }

            //    // statement list
            //    nextScannerToken();
            //    if (currentScannerToken == GingerToken.OpenStatementList)
            //    {
            //        nextScannerToken();
            //        sl = parseStatementList(GingerToken.CloseStatementList);
            //        nc = new Component(compName, compType, sl);
            //    }
            //    else
            //    {
            //        throw new ParseException(scanner.row, scanner.col, $"Expected '{GingerToken.OpenStatementList.ToString()}', found '{currentScannerToken.ToString()}'", ExceptionLevel.ERROR);
            //    }
            //}
            else
            {
                //throw new ParseException(scanner.row, scanner.col, $"Expected '{GingerToken.If.ToString()}', '{GingerToken.Int.ToString()}', or '{GingerToken.Identifier.ToString()}', found '{currentScannerToken.ToString()}'", ExceptionLevel.ERROR);
                spyScannerToken();
                throw new ParseException(scanner.row, scanner.col, $"Expected '{GingerToken.Identifier.ToString()}', found '{currentScannerToken.ToString()}' near {spiedScannerToken.ToString()}", ExceptionLevel.ERROR);
            }

            return(nc);
        }
Exemplo n.º 2
0
        private NodeCollection parseStatement()
        {
            NodeCollection nc;
            // statement = ("if" | "while"), expression, "{", statement-list, "}"
            if (Grammar.isControl(currentScannerToken))
            {
                StatementList sl;
                GingerToken controlToken = currentScannerToken;

                nextScannerToken();

                Node conditionExpression = parseExpression();

                nextScannerToken();
                if (currentScannerToken == GingerToken.OpenStatementList)
                {
                    nextScannerToken();
                    sl = parseStatementList(GingerToken.CloseStatementList);
                }
                else
                {
                    throw new ParseException(scanner.row, scanner.col, $"Expected '{GingerToken.OpenStatementList.ToString()}', found '{currentScannerToken.ToString()}'", ExceptionLevel.ERROR);
                }

                if (controlToken == GingerToken.While)
                {
                    nc = new While(conditionExpression, sl);
                }
                else
                {
                    nc = new If(conditionExpression, sl);
                }
            }
            // statement = type, identifier
            else if (Grammar.isType(currentScannerToken))
            {
                Node type;
                if (currentScannerToken == GingerToken.Int)
                {
                    type = new Integer(scanner.row, scanner.col);
                }
                else
                {
                    type = new Boolean(scanner.row, scanner.col);
                }

                nextScannerToken();
                if (currentScannerToken != GingerToken.Identifier)
                {
                    throw new ParseException(scanner.row, scanner.col, $"Expected '{GingerToken.Identifier.ToString()}', found '{currentScannerToken.ToString()}'", ExceptionLevel.ERROR);
                }
                else
                {
                    nc = new Declaration(type, new Identifier(scanner.row, scanner.col, new string(scanner.tokenValue)));
                }
            }
            // statement = identifier, "=", expression
            else if (currentScannerToken == GingerToken.Identifier)
            {
                Identifier identifier = new Identifier(scanner.row, scanner.col, new string(scanner.tokenValue));
                nextScannerToken();
                if (currentScannerToken == GingerToken.Assignment)
                {
                    nextScannerToken();
                    nc = new Assign(identifier, parseExpression());
                }
                else
                {
                    throw new ParseException(scanner.row, scanner.col, $"Expected '{GingerToken.Assignment.ToString()}', found '{currentScannerToken.ToString()}'", ExceptionLevel.ERROR);
                }
            }
            else
            {
                throw new ParseException(scanner.row, scanner.col, $"Expected '{GingerToken.If.ToString()}', '{GingerToken.While.ToString()}', '{GingerToken.Int.ToString()}', '{GingerToken.Bool.ToString()}', or '{GingerToken.Identifier.ToString()}', found '{currentScannerToken.ToString()}'", ExceptionLevel.ERROR);
            }

            return nc;
        }