public static IEnumerable <Statement> GetSubStatements(this Statement statement)
 {
     if (statement is IfStatement)
     {
         IfStatement ifStatement = (IfStatement)statement;
         return(ifStatement.ThenStatements.GetStatements()
                .Concat(ifStatement.ElseIfStatements.GetStatements())
                .Concat(ifStatement.ElseStatements.GetStatements()));
     }
     else if (statement is ElseIfStatement)
     {
         ElseIfStatement elseIfStatement = (ElseIfStatement)statement;
         return(elseIfStatement.ThenStatements.GetStatements());
     }
     else if (statement is ForStatement)
     {
         ForStatement forStatement = (ForStatement)statement;
         return(forStatement.ForBody.GetStatements());
     }
     else if (statement is WhileStatement)
     {
         WhileStatement whileStatement = (WhileStatement)statement;
         return(whileStatement.WhileBody.GetStatements());
     }
     else if (statement is SubroutineStatement)
     {
         SubroutineStatement subroutineStatement = (SubroutineStatement)statement;
         return(subroutineStatement.SubroutineBody.GetStatements());
     }
     return(new Statement[0]);
 }
Beispiel #2
0
        private IfStatement DoIfStatement()
        {
            TakeKeyword("if");
            Take(LexemeKind.LeftParenthesis, "condition opening");

            var condition = DoExpression();

            Take(LexemeKind.RightParenthesis, "condition closing");

            var           body  = DoStatementBlock();
            ElseStatement @else = null;

            if (TakeKeyword("else", out _))
            {
                if (TakeKeyword("if", out _))
                {
                    Back();
                    var @if = DoIfStatement();
                    @else = new ElseIfStatement(@if.Condition, @if.Body, @if.Else);
                }
                else
                {
                    @else = new ElseStatement(DoStatementBlock());
                }
            }

            return(new IfStatement(condition, body, @else));
        }
Beispiel #3
0
        /// <summary>
        /// Dit is weer reachable
        /// Expressie gevonden?
        /// Staat er een if bovenop de stack of een elseif?
        /// Eraf halen en ElseIf erop zetten
        /// </summary>
        /// <param name="l"></param>
        public void Visit(ElseIfStatement l)
        {
            Reachable();
            if (CheckInclude(l))
            {
                return;
            }

            if (CheckGeneric(l))
            {
                return;
            }

            // Expressie gevonden?
            if (l.Param == String.Empty)
            {
                _errors.Add(new IfMissingConditionException(l.SourceCodeContext));
            }

            if (_tomatch.Count > 0 &&
                (_tomatch[_tomatch.Count - 1] is IfStatement ||
                 _tomatch[_tomatch.Count - 1] is ElseIfStatement))
            {
                _tomatch[_tomatch.Count - 1] = l;
            }
            else
            {
                _errors.Add(new ElseIfNotMatchedException(l.SourceCodeContext));
            }
        }
Beispiel #4
0
        private ElseIfStatement ParseElseIfStatement()
        {
            // parse statement declaration
            ElseIfStatement stm = new ElseIfStatement(current.Line, current.Column);

            Consume(TokenType.ElseIf);

            if (Current.TokenType != TokenType.Attribute)
            {
                throw new ParserException("Expected tag attribute", stm.Line, stm.Column);
            }

            // look for "test" attribute
            if (Current.Data == "test")
            {
                Consume(TokenType.Attribute);
                Consume(TokenType.Assign);
                stm.Condition = ParseExpression();
            }

            // check statement
            if (stm.Condition == null)
            {
                throw new ParserException("\"test\" attribute was not specified", stm.Line, stm.Column);
            }

            // parse body
            while (true)
            {
                if (Current.TokenType == TokenType.ElseIf ||
                    Current.TokenType == TokenType.Else ||
                    Current.TokenType == TokenType.EndIf)
                {
                    break;
                }
                else
                {
                    Statement blockStm = ParseStatement();
                    if (blockStm == null)
                    {
                        throw new ParserException("Expected <ad:elseif>, <ad:else> or </ad:if> tags", stm.Line, stm.Column);
                    }

                    // parse statement
                    stm.TrueStatements.Add(blockStm);
                }
            }

            return(stm);
        }
Beispiel #5
0
 public virtual T Visit(ElseIfStatement stmt) => Visit(stmt as Statement);
Beispiel #6
0
        public static CodeLine BuildCodeLine(string filename, int linenr, string codeline)
        {
            CodeLine result = null;
            Match    m      = line_regex.Match(codeline);

            if (m.Success)
            {
                string sCommand = m.Groups["command"].Value;
                int    pos      = m.Length;
                switch (sCommand)
                {
                case DoStatement.keyword:
                    result = new DoStatement(linenr, m, codeline);
                    break;

                case WhileStatement.keyword:
                    result = new WhileStatement(linenr, m, codeline);
                    break;

                case IfStatement.keyword:
                    result = new IfStatement(linenr, m, codeline);
                    break;

                case ElseStatement.keyword:
                    result = new ElseStatement(linenr, m, codeline);
                    break;

                case ElseIfStatement.keyword:
                    result = new ElseIfStatement(linenr, m, codeline);
                    break;

                case EndIfStatement.keyword:
                    result = new EndIfStatement(linenr, m, codeline);
                    break;

                case ErrorMessage.keyword:
                    result = new ErrorMessage(linenr, m, codeline);
                    break;

                case InfoMessage.keyword:
                    result = new InfoMessage(linenr, m, codeline);
                    break;

                case LoopStatement.keyword:
                    result = new LoopStatement(linenr, m, codeline);
                    break;

                case BreakStatement.keyword:
                    result = new BreakStatement(linenr, m, codeline);
                    break;

                case ReturnStatement.keyword:
                    result = new ReturnStatement(linenr, m, codeline);
                    break;

                case ExitStatement.keyword:
                    result = new ExitStatement(linenr, m, codeline);
                    break;

                case VarDeclaration.keyword:
                    result = new VarDeclaration(linenr, m, codeline);
                    break;

                case GlobalVarDeclaration.keyword:
                    result = new GlobalVarDeclaration(linenr, m, codeline);
                    break;

                case FunctionDeclaration.keyword:
                    result = new FunctionDeclaration(linenr, m, codeline);
                    break;

                case EndFunctionDeclaration.keyword:
                    result = new EndFunctionDeclaration(linenr, m, codeline);
                    break;

                case Include.keyword:
                    result = new Include(linenr, m, codeline);
                    break;
                }
            }
            // Misschien is het een comment?
            if (result == null)
            {
                m = cmmt_regex.Match(codeline);
                if (m.Success)
                {
                    result = new Comment(linenr, m, codeline);
                }
            }
            if (result == null)
            {
                m = ass_regex.Match(codeline);
                if (m.Success)
                {
                    result = new Assignment(linenr, m, codeline);
                }
            }
            if (result == null)
            {
                m = vfc_regex.Match(codeline);
                if (m.Success)
                {
                    result = new VoidFunctionCall(linenr, m, codeline);
                }
            }
            if (result == null && codeline.Trim() == "@")
            {
                m = cmm2_regex.Match(codeline);
                if (m.Success)
                {
                    result = new Comment(linenr, m, codeline);
                }
            }
            if (result == null)
            {
                // Anders zal het een gewone tekst regel zijn? Met evt een paar expressies erin
                result = new CodeLine(linenr, codeline);
            }
            result._filename = filename;
            return(result);
        }
Beispiel #7
0
        /// <summary>
        /// Parses the 'if' statement signature into a code block object
        /// </summary>
        /// <param name="templateContent">The template content</param>
        /// <param name="positionOffSet">The position offset index</param>
        /// <param name="signature">The 'if' signature</param>
        /// <returns>The parsed if statement</returns>
        public override CodeBlock Parse
        (
            ref string templateContent,
            ref int positionOffSet,
            string signature
        )
        {
            var signatureBody = UnwrapSignatureBody
                                (
                signature
                                );

            var conditionSignature = signatureBody.RightOf
                                     (
                "{0} ".With
                (
                    this.TagName
                )
                                     );

            if (String.IsNullOrWhiteSpace(conditionSignature))
            {
                throw new NettleParseException
                      (
                          "The if statements condition must be specified.",
                          positionOffSet
                      );
            }

            var expression = _expressionParser.Parse
                             (
                conditionSignature
                             );

            var ifContent = ExtractNestedBody
                            (
                ref templateContent,
                ref positionOffSet,
                signature
                            );

            var ifSignature   = ifContent.Signature;
            var elseIfBlocks  = new List <ElseIfStatement>();
            var elseContent   = default(NestableCodeBlock);
            var elseFindCount = 0;

            var elseFound = templateContent.StartsWith
                            (
                @"{{else"
                            );

            while (elseFound)
            {
                if (templateContent.StartsWith(@"{{else if "))
                {
                    var elseSignature = String.Empty;
                    var closureFound  = false;

                    // Scan until the tag ending }} brackets are reached
                    foreach (var c in templateContent)
                    {
                        elseSignature += c;

                        if (elseSignature.EndsWith(@"}}"))
                        {
                            closureFound = true;
                            break;
                        }
                    }

                    if (false == closureFound)
                    {
                        var message = "The else if tag '{0}' is invalid.";

                        throw new NettleParseException
                              (
                                  message.With(elseSignature),
                                  positionOffSet
                              );
                    }

                    // Unwrap the 'else if' signature body
                    var elseSignatureBody = UnwrapSignatureBody
                                            (
                        elseSignature
                                            );

                    // Extract the boolean expression and parse
                    var elseConditionSignature = elseSignatureBody.RightOf
                                                 (
                        "else if "
                                                 );

                    var elseExpression = _expressionParser.Parse
                                         (
                        elseConditionSignature
                                         );

                    // Extract the 'else if' body content
                    var elseIfContent = ExtractNestedBody
                                        (
                        ref templateContent,
                        ref positionOffSet,
                        elseSignature
                                        );

                    var elseIfBlock = new ElseIfStatement()
                    {
                        ConditionExpression = elseExpression,
                        Body          = elseIfContent.Body,
                        Blocks        = elseIfContent.Blocks,
                        Signature     = elseIfContent.Signature,
                        StartPosition = elseIfContent.StartPosition,
                        EndPosition   = elseIfContent.EndPosition
                    };

                    elseIfBlocks.Add(elseIfBlock);

                    ifSignature += elseIfContent.Signature;
                }
                else
                {
                    // Ensure only one else block is defined
                    if (elseFindCount > 0)
                    {
                        throw new NettleParseException
                              (
                                  "Only one else tag is allowed.",
                                  positionOffSet
                              );
                    }

                    // Ensure it's a valid else block
                    if (false == templateContent.StartsWith(@"{{else}}"))
                    {
                        throw new NettleParseException
                              (
                                  "The else tag is invalid.",
                                  positionOffSet
                              );
                    }

                    elseContent = ExtractNestedBody
                                  (
                        ref templateContent,
                        ref positionOffSet,
                        @"{{else}}"
                                  );

                    elseContent.EndPosition =
                        (
                            elseContent.StartPosition + elseContent.Signature.Length
                        );

                    elseFindCount++;
                    ifSignature += elseContent.Signature;
                }

                elseFound = templateContent.StartsWith
                            (
                    @"{{else"
                            );
            }

            var ifEndPosition =
                (
                    ifContent.StartPosition + ifSignature.Length
                );

            return(new IfStatement()
            {
                Signature = ifSignature,
                StartPosition = ifContent.StartPosition,
                EndPosition = ifEndPosition,
                ConditionExpression = expression,
                Body = ifContent.Body,
                Blocks = ifContent.Blocks,
                ElseIfConditions = elseIfBlocks,
                ElseContent = elseContent
            });
        }
        private static string Dump(this IEnumerable <Statement> statements, string ident = "")
        {
            StringWriter writer = new StringWriter();

            foreach (Statement statement in statements)
            {
                writer.WriteLine($"{ident}{statement.GetType().Name} --> {(statement.HasCompiler() ? statement.Compiler().ToString() : "NO COMPILER")}");
                if (statement is EmptyStatement)
                {
                    writer.WriteLine($"{ident}-----> Comment: {((EmptyStatement)statement).EndingComment.Dump()}");
                }
                else if (statement is AssignmentStatement)
                {
                    var leftValue  = ((AssignmentStatement)statement).LeftValue;
                    var rightValue = ((AssignmentStatement)statement).RightValue;
                    writer.WriteLine($"{ident}-----> LEFT: {leftValue.Dump()}");
                    if (leftValue is ArrayExpression)
                    {
                        ArrayExpression arrayExpression = (ArrayExpression)leftValue;
                        writer.WriteLine($"{ident}----->   ARRAY LeftHand: ({arrayExpression.LeftHand.Dump()}");
                        writer.WriteLine($"{ident}----->   ARRAY Indexer: ({arrayExpression.Indexer.Dump()}");
                        DumpValueExpression(writer, arrayExpression.Indexer, ident);
                    }
                    writer.WriteLine($"{ident}-----> RIGHT: {rightValue.Dump()}");
                    DumpValueExpression(writer, rightValue, ident);
                }
                else if (statement is MethodCallStatement)
                {
                    writer.WriteLine($"{ident}-----> Expression: {((MethodCallStatement)statement).MethodCallExpression.Dump()}");
                    DumpValueExpression(writer, ((MethodCallStatement)statement).MethodCallExpression, ident);
                }
                else if (statement is IfStatement)
                {
                    IfStatement ifStatement = (IfStatement)statement;
                    writer.WriteLine($"{ident}-----> Condition: {ifStatement.Condition} {ifStatement.Condition.Dump()}");
                    DumpValueExpression(writer, ifStatement.Condition, ident);
                    writer.WriteLine($"{ident}-----> ThenStatements <-----");
                    writer.Write(ifStatement.ThenStatements.Dump(ident + "    "));
                    writer.WriteLine($"{ident}-----> ElseIfStatements <-----");
                    writer.Write(ifStatement.ElseIfStatements.Dump(ident + "    "));
                    writer.WriteLine($"{ident}-----> ElseStatements <-----");
                    writer.Write(ifStatement.ElseStatements.Dump(ident + "    "));
                    writer.WriteLine($"{ident}-----> EndIf <-----");
                }
                else if (statement is ElseIfStatement)
                {
                    ElseIfStatement elseIfStatement = (ElseIfStatement)statement;
                    writer.WriteLine($"{ident}-----> Condition: {elseIfStatement.Condition} {elseIfStatement.Condition.Dump()}");
                    DumpValueExpression(writer, elseIfStatement.Condition, ident);
                    writer.WriteLine($"{ident}-----> ElseIfStatements <-----");
                    writer.Write(elseIfStatement.ThenStatements.Dump(ident + "    "));
                }
                else if (statement is ForStatement)
                {
                    ForStatement forStatement = (ForStatement)statement;
                    writer.WriteLine($"{ident}-----> Iterator: {forStatement.Iterator} {forStatement.Iterator.Dump()}");
                    writer.WriteLine($"{ident}-----> ForBody <-----");
                    writer.Write(forStatement.ForBody.Dump(ident + "    "));
                    writer.WriteLine($"{ident}-----> EndFor <-----");
                }
                else if (statement is WhileStatement)
                {
                    WhileStatement whileStatement = (WhileStatement)statement;
                    writer.WriteLine($"{ident}-----> Condition: {whileStatement.Condition} {whileStatement.Condition.Dump()}");
                    DumpValueExpression(writer, whileStatement.Condition, ident);
                    writer.WriteLine($"{ident}-----> WhileBody <-----");
                    writer.Write(whileStatement.WhileBody.Dump(ident + "    "));
                    writer.WriteLine($"{ident}-----> EndWhile <-----");
                }
                else if (statement is SubroutineStatement)
                {
                    SubroutineStatement subroutineStatement = (SubroutineStatement)statement;
                    writer.WriteLine($"{ident}-----> SubroutineName: {subroutineStatement.SubroutineName}");
                    writer.WriteLine($"{ident}-----> SubroutineBody <-----");
                    writer.Write(subroutineStatement.SubroutineBody.Dump(ident + "    "));
                    writer.WriteLine($"{ident}-----> EndSubroutine <-----");
                }
                else
                {
                    writer.WriteLine($"{ident}-----> {statement.ToString().Trim('\n', '\r')}");
                }
            }

            return(writer.ToString());
        }
Beispiel #9
0
 public void Visit(ElseIfStatement l)
 {
     _level -= _templatetabs;
     Format(l);
     _level += _templatetabs;
 }
Beispiel #10
0
 public void Visit(ElseIfStatement l)
 {
     VisitStatementExpressionComment(l);
 }