TokenBuffer.cs :  » Inversion-of-Control-Dependency-Injection » Spring.net » Spring » Expressions » Parser » antlr » 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 » TokenBuffer.cs
using System;

namespace Spring.Expressions.Parser.antlr{
  /*ANTLR Translator Generator
  * Project led by Terence Parr at http://www.jGuru.com
  * Software rights: http://www.antlr.org/license.html
  *
  * $Id:$
  */
  
  //
  // ANTLR C# Code Generator by Micheal Jordan
  //                            Kunle Odutola       : kunle UNDERSCORE odutola AT hotmail DOT com
  //                            Anthony Oguntimehin
  //
  // With many thanks to Eric V. Smith from the ANTLR list.
  //
  
  /*A Stream of Token objects fed to the parser from a Tokenizer that can
  * be rewound via mark()/rewind() methods.
  * <p>
  * A dynamic array is used to buffer up all the input tokens.  Normally,
  * "k" tokens are stored in the buffer.  More tokens may be stored during
  * guess mode (testing syntactic predicate), or when LT(i>k) is referenced.
  * Consumption of tokens is deferred.  In other words, reading the next
  * token is not done by conume(), but deferred until needed by LA or LT.
  * <p>
  *
  * @see antlr.Token
  * @see antlr.Tokenizer
  * @see antlr.TokenQueue
  */
  
  public class TokenBuffer
  {
    
    // Token source
    protected internal TokenStream input;
    
    // Number of active markers
    protected internal int nMarkers = 0;
    
    // Additional offset used when markers are active
    protected internal int markerOffset = 0;
    
    // Number of calls to consume() since last LA() or LT() call
    protected internal int numToConsume = 0;
    
    // Circular queue
    internal TokenQueue queue;
    
    /*Create a token buffer */
    public TokenBuffer(TokenStream input_)
    {
      input = input_;
      queue = new TokenQueue(1);
    }
    
    /*Reset the input buffer to empty state */
    public virtual void  reset()
    {
      nMarkers = 0;
      markerOffset = 0;
      numToConsume = 0;
      queue.reset();
    }
    
    /*Mark another token for deferred consumption */
    public virtual void  consume()
    {
      numToConsume++;
    }
    
    /*Ensure that the token buffer is sufficiently full */
    protected virtual void  fill(int amount)
    {
      syncConsume();
      // Fill the buffer sufficiently to hold needed tokens
      while (queue.nbrEntries < (amount + markerOffset))
      {
        // Append the next token
        queue.append(input.nextToken());
      }
    }
    
    /*return the Tokenizer (needed by ParseView) */
    public virtual TokenStream getInput()
    {
      return input;
    }
    
    /*Get a lookahead token value */
    public virtual int LA(int i)
    {
      fill(i);
      return queue.elementAt(markerOffset + i - 1).Type;
    }
    
    /*Get a lookahead token */
    public virtual IToken LT(int i)
    {
      fill(i);
      return queue.elementAt(markerOffset + i - 1);
    }
    
    /*Return an integer marker that can be used to rewind the buffer to
    * its current state.
    */
    public virtual int mark()
    {
      syncConsume();
      nMarkers++;
      return markerOffset;
    }
    
    /*Rewind the token buffer to a marker.
    * @param mark Marker returned previously from mark()
    */
    public virtual void  rewind(int mark)
    {
      syncConsume();
      markerOffset = mark;
      nMarkers--;
    }
    
    /*Sync up deferred consumption */
    protected virtual void  syncConsume()
    {
      while (numToConsume > 0)
      {
        if (nMarkers > 0)
        {
          // guess mode -- leave leading tokens and bump offset.
          markerOffset++;
        }
        else
        {
          // normal mode -- remove first token
          queue.removeFirst();
        }
        numToConsume--;
      }
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.