コード例 #1
0
        private static FlowNode ParseIf(RuleContext rule)
        {
            //IF expression INDENT statements DEDENT (ELSE INDENT statements DEDENT)?;
            ExpressionNode expression = ExpressionParser.ParseExpression((RuleContext)rule.GetChild(1));
            BlockNode      trueBlock  = ParseBlockNode((RuleContext)rule.GetChild(3));

            //Pure if
            if (rule.ChildCount == 5)
            {
                return(CrawlSyntaxNode.If(rule.SourceInterval, expression, trueBlock));
            }
            //If Else
            else if (rule.ChildCount == 9)
            {
                BlockNode falseBlock = ParseBlockNode((RuleContext)rule.GetChild(7));
                return(CrawlSyntaxNode.IfElse(rule.SourceInterval, expression, trueBlock, falseBlock));
            }
            //Else if
            else if (rule.ChildCount == 7)
            {
                RuleContext            chain         = (RuleContext)rule.GetChild(6);
                List <CrawlSyntaxNode> blockContents = new List <CrawlSyntaxNode>()
                {
                    ParseIf(chain)
                };
                BlockNode falseBlock = CrawlSyntaxNode.Block(chain.SourceInterval, blockContents);
                return(CrawlSyntaxNode.IfElse(rule.SourceInterval, expression, trueBlock, falseBlock));
            }

            throw new NotImplementedException("if statement with strange argument counts (this is probably else if)");
        }
コード例 #2
0
        //public static ConstructNode ParseConstruct(RuleContext rule)


        public static BlockNode ParseBlockNode(RuleContext rule)
        {
            System.Collections.IEnumerable meaningfullContent;

            if (rule.RuleIndex == CrawlParser.RULE_statements)
            {
                meaningfullContent = rule.AsIEnumerable();
            }
            else if (rule.RuleIndex == CrawlParser.RULE_class_body)
            {
                meaningfullContent = rule.AsEdgeTrimmedIEnumerable();
            }
            else
            {
                throw new NotImplementedException("Probably broken");
            }

            IEnumerable <CrawlSyntaxNode> contents =
                meaningfullContent
                .OfType <RuleContext>()    //FIXME! This can contain raw NEWLINE and END_OF_STATEMENT tokens which is TerminalNodeImpl not RuleContext. Not happy about discarding that trivia
                .Select(ParseStatement).ToList();

            return(CrawlSyntaxNode.Block(rule.SourceInterval, contents));
        }