Esempio n. 1
0
        private void ProcessNode(IAstNode node)
        {
            Context context;

            switch (node.AstNodeType)
            {
            case AstNodeType.Syntax:
                context = new Context(node.AstNodeType);
                _stack.Push(context);

                PrintUsings();
                PrintNamespaceHeader();
                PrintClassHeader();
                PrintClassProperties();
                PrintConstructor();
                PrintMatchMethod();
                PrintMatchOneOfMethod();

                PrintParseGoal(node.AsSyntax().Statements.First().ProdName,
                               node.AsSyntax().PreActionNode?.ActionName,
                               node.AsSyntax().PostActionNode?.ActionName);
                break;

            case AstNodeType.Statement:
                context = new Context(node.AstNodeType);
                _stack.Push(context);

                context.Properties.Add("PreActionName", node.AsStatement().PreActionNode?.ActionName);
                context.Properties.Add("PostActionName", node.AsStatement().PostActionNode?.ActionName);

                PrintMethodHeader(node.AsStatement().ProdName, context.Properties["PreActionName"]?.ToString());

                if (node.FirstSet.AsEnumerable().Count() > 1)
                {
                    PrintFirstSet(node.FirstSet, node.NodeId);
                    PrintMatchOneOf(node.NodeId);
                }
                else
                {
                    PrintMatchNonTerminal(node.FirstSet.AsEnumerable().First());
                }
                break;

            case AstNodeType.Expression:
                _stack.Push(new Context(node.AstNodeType));
                _stack.Peek().GenerateSwitch = node.AsExpression().TermCount > 1;

                if (node.AsExpression().PreActionNode != null)
                {
                    PrintAction(node.AsExpression().PreActionNode.ActionName);
                }

                if (node.AsExpression().TermCount > 1)
                {
                    PrintTermSwitch();
                }
                break;

            case AstNodeType.Term:
                context = new Context(node.AstNodeType);
                context.GenerateSwitch = _stack.Peek().GenerateSwitch;
                _stack.Push(context);

                if (context.GenerateSwitch)
                {
                    PrintTermCase(node.FirstSet);
                    Indent();
                }
                break;

            case AstNodeType.Factor:
                context = new Context(node.AstNodeType);
                context.Properties.Add("PostActionName", node.AsFactor().PostActionNode?.ActionName);
                _stack.Push(context);

                if (node.AsFactor().PreActionNode != null)
                {
                    PrintAction(node.AsFactor().PreActionNode.ActionName);
                }
                break;

            case AstNodeType.ProdRef:
                _stack.Push(new Context(node.AstNodeType));
                PrintProdRef(node.AsProdRef().ProdName);
                break;

            case AstNodeType.Terminal:
                _stack.Push(new Context(node.AstNodeType));
                PrintMatchTerminal(node.AsTerminal().TermName);
                break;

            case AstNodeType.Action:
                _stack.Push(new Context(node.AstNodeType));
                break;

            case AstNodeType.Paren:
                _stack.Push(new Context(node.AstNodeType));
                break;

            case AstNodeType.Option:
                _stack.Push(new Context(node.AstNodeType));
                PrintOption(node.FirstSet, node.NodeId);
                break;

            case AstNodeType.KleeneStar:
                _stack.Push(new Context(node.AstNodeType));
                PrintKleene(node.FirstSet, node.NodeId);
                break;
            }
        }
Esempio n. 2
0
        public void Traverse(IAstNode astNode)
        {
            ProcessNode?.Invoke(astNode);

            _tracer.BeginTrace(astNode.AstNodeType.ToString());

            switch (astNode.AstNodeType)
            {
            case AstNodeType.Syntax:
                if (astNode.AsSyntax().PreActionNode != null)
                {
                    Traverse(astNode.AsSyntax().PreActionNode);
                }

                foreach (var stmt in astNode.AsSyntax().Statements)
                {
                    Traverse(stmt);
                }

                if (astNode.AsSyntax().PostActionNode != null)
                {
                    Traverse(astNode.AsSyntax().PostActionNode);
                }

                break;

            case AstNodeType.Statement:
                if (astNode.AsStatement().PreActionNode != null)
                {
                    Traverse(astNode.AsStatement().PreActionNode);
                }

                CheckForValue(astNode, "Expression", astNode.AsStatement().Expression);
                Traverse(astNode.AsStatement().Expression);

                if (astNode.AsStatement().PostActionNode != null)
                {
                    Traverse(astNode.AsStatement().PostActionNode);
                }

                break;

            case AstNodeType.Expression:
                if (astNode.AsExpression().PreActionNode != null)
                {
                    Traverse(astNode.AsExpression().PreActionNode);
                }

                foreach (var term in astNode.AsExpression().Terms)
                {
                    Traverse(term);
                }

                if (astNode.AsExpression().PostActionNode != null)
                {
                    Traverse(astNode.AsExpression().PreActionNode);
                }

                break;

            case AstNodeType.Term:
                if (astNode.AsTerm().PreActionNode != null)
                {
                    Traverse(astNode.AsTerm().PreActionNode);
                }

                foreach (var factor in astNode.AsTerm().Factors)
                {
                    Traverse(factor);
                }

                if (astNode.AsTerm().PostActionNode != null)
                {
                    Traverse(astNode.AsTerm().PreActionNode);
                }

                break;

            case AstNodeType.Factor:
                if (astNode.AsFactor().PreActionNode != null)
                {
                    Traverse(astNode.AsFactor().PreActionNode);
                }

                CheckForValue(astNode, "FactorExpr", astNode.AsFactor().FactorExpr);
                Traverse(astNode.AsFactor().FactorExpr);

                if (astNode.AsFactor().PostActionNode != null)
                {
                    Traverse(astNode.AsFactor().PostActionNode);
                }

                break;

            case AstNodeType.ProdRef:
                _tracer.TraceLine($"{astNode.AsProdRef().ProdName}");

                break;

            case AstNodeType.Terminal:
                _tracer.TraceLine($"{astNode.AsTerminal().TermName}");

                break;

            case AstNodeType.Action:
                _tracer.TraceLine($"{astNode.AsAction().ActionName}");

                break;

            case AstNodeType.Paren:
                CheckForValue(astNode, "Expression", astNode.AsParen().Expression);

                Traverse(astNode.AsParen().Expression);

                break;

            case AstNodeType.Option:
                CheckForValue(astNode, "Expression", astNode.AsOption().Expression);
                Traverse(astNode.AsOption().Expression);

                break;

            case AstNodeType.KleeneStar:
                CheckForValue(astNode, "Expression", astNode.AsKleene().Expression);
                Traverse(astNode.AsKleene().Expression);

                break;
            }

            _tracer.EndTrace(astNode.AstNodeType.ToString());

            PostProcessNode?.Invoke();
        }