ParseTreeDebugParser.cs :  » Inversion-of-Control-Dependency-Injection » Spring.net » Spring » Expressions » Parser » antlr » debug » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Inversion of Control Dependency Injection » Spring.net 
Spring.net » Spring » Expressions » Parser » antlr » debug » ParseTreeDebugParser.cs
namespace Spring.Expressions.Parser.antlr.debug{

  /* ANTLR Translator Generator
   * Project led by Terence Parr at http://www.jGuru.com
   * Software rights: http://www.antlr.org/license.html
   */

  //
  // ANTLR C# Code Generator by Micheal Jordan
  //                            Kunle Odutola       : kunle UNDERSCORE odutola AT hotmail DOT com
  //                            Anthony Oguntimehin
  //

  using System;
  using Stack = System.Collections.Stack;
  using antlr;
  using BitSet = antlr.collections.impl.BitSet;

  /// <summary>
  /// Specifies the behaviour required (i.e. parser modifications) 
  /// specifically to support parse tree debugging and derivation.
  /// </summary>
  /// <remarks>
  /// <para>
  /// Override the standard matching and rule entry/exit routines
  /// to build parse trees.  This class is useful for 2.7.3 where
  /// you can specify a superclass like
  /// </para>
  /// <para>
  /// class TinyCParser extends Parser(ParseTreeDebugParser);
  /// </para>
  /// </remarks>
  public class ParseTreeDebugParser : LLkParser 
  {
    /// <summary>
    /// Each new rule invocation must have it's own subtree. Tokens are
    /// added to the current root so we must have a stack of subtree roots.
    /// </summary>
    protected Stack currentParseTreeRoot = new Stack();

    /// <summary>
    /// Track most recently created parse subtree so that when parsing
    /// is finished, we can get to the root.
    /// </summary>
    protected ParseTreeRule mostRecentParseTreeRoot = null;

    /// <summary>
    /// For every rule replacement with a production, we bump up count.
    /// </summary>
    protected int numberOfDerivationSteps = 1; // n replacements plus step 0

    public ParseTreeDebugParser(int k_) : base(k_)
    {
    }

    public ParseTreeDebugParser(ParserSharedInputState state, int k_) : base(state, k_)
    {
    }

    public ParseTreeDebugParser(TokenBuffer tokenBuf, int k_) : base(tokenBuf, k_)
    {
    }

    public ParseTreeDebugParser(TokenStream lexer, int k_) : base(lexer,k_)
    {
    }

    public ParseTree getParseTree() 
    {
      return mostRecentParseTreeRoot;
    }

    public int getNumberOfDerivationSteps() 
    {
      return numberOfDerivationSteps;
    }

    public override void match(int i)       // throws MismatchedTokenException, TokenStreamException 
    {
      addCurrentTokenToParseTree();
      base.match(i);
    }

    public override void match(BitSet bitSet)   // throws MismatchedTokenException, TokenStreamException 
    {
      addCurrentTokenToParseTree();
      base.match(bitSet);
    }

    public override void matchNot(int i)     // throws MismatchedTokenException, TokenStreamException
    {
      addCurrentTokenToParseTree();
      base.matchNot(i);
    }

    /// <summary>
    /// Adds LT(1) to the current parse subtree.
    /// </summary>
    /// <remarks>
    /// <para>
    /// Note that the match() routines add the node before checking for 
    /// correct match.  This means that, upon mismatched token, there 
    /// will a token node in the tree corresponding to where that token 
    /// was expected.  For no viable alternative errors, no node will 
    /// be in the tree as nothing was matched() (the lookahead failed 
    /// to predict an alternative).
    /// </para>
    /// </remarks>
    protected void addCurrentTokenToParseTree()     // throws TokenStreamException 
    {
      if (inputState.guessing > 0) 
      {
        return;
      }
      ParseTreeRule root = (ParseTreeRule) currentParseTreeRoot.Peek();
      ParseTreeToken tokenNode = null;
      if ( LA(1) == Token.EOF_TYPE ) 
      {
        tokenNode = new ParseTreeToken(new antlr.CommonToken("EOF"));
      }
      else 
      {
        tokenNode = new ParseTreeToken(LT(1));
      }
      root.addChild(tokenNode);
    }

    /// <summary>
    /// Create a rule node, add to current tree, and make it current root
    /// </summary>
    /// <param name="s"></param>
    public override void traceIn(string s)         // throws TokenStreamException 
    {
      if (inputState.guessing > 0) 
      {
        return;
      }
      ParseTreeRule subRoot = new ParseTreeRule(s);
      if ( currentParseTreeRoot.Count > 0 ) 
      {
        ParseTreeRule oldRoot = (ParseTreeRule) currentParseTreeRoot.Peek();
        oldRoot.addChild(subRoot);
      }
      currentParseTreeRoot.Push(subRoot);
      numberOfDerivationSteps++;
    }

    /// <summary>
    /// Pop current root; back to adding to old root
    /// </summary>
    /// <param name="s"></param>
    public override void traceOut(string s)         // throws TokenStreamException
    {
      if (inputState.guessing > 0) 
      {
        return;
      }
      mostRecentParseTreeRoot = (ParseTreeRule) currentParseTreeRoot.Pop();
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.