All pastes #414618 Raw Edit

Indent visitor

public text v1 · immutable
#414618 ·published 2007-03-29 03:57 UTC
rendered paste body
using System;
using System.Collections.Generic;
using System.Text;
using DunnChurchill.Experimental.Compiler;
using PerCederberg.Grammatica.Parser;

namespace TestParser.Visitors
{
    //This visitor class manages indentation chars { and }
    public class IndentVisitor : Visitor
    {

        private int currentIndentLevel = 0;

        //Return the current indentation level
        public int CurrentIndentLevel
        {
            get { return currentIndentLevel; }
            set { currentIndentLevel = value; }
        }

        public override void Visit(Node node, Response response)
        {
            Node parentNode = node.GetParent();
            TemplateConstants parentNodeType = (TemplateConstants)parentNode.GetId();

            string result = (node as Token).GetImage();

            switch (parentNodeType)
            {
                //Indent chars are ok in text blocks
                case TemplateConstants.TEXTBLOCK:
                    response.Write(result);
                    break;
                //Code block texts change indent level but indent char isn't outputted
                case TemplateConstants.CODEBLOCK:
                    TemplateConstants nodeType = (TemplateConstants)node.GetId();
                    if (nodeType == TemplateConstants.STARTINDENT)
                        CurrentIndentLevel++;
                    if (nodeType == TemplateConstants.ENDINDENT)
                        currentIndentLevel--;
                break;
            }
        }

    }
}