MismatchedTokenException.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 » MismatchedTokenException.cs
using System;
using Spring.Expressions.Parser.antlr.collections;
using Spring.Expressions.Parser.antlr.collections.impl;
using StringBuilderSystem.Text.StringBuilder;

using BitSetSpring.Expressions.Parser.antlr.collections.impl.BitSet;
using ASTSpring.Expressions.Parser.antlr.collections.AST;

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.
  //
  
  [Serializable]
  public class MismatchedTokenException : RecognitionException
  {
    // Token names array for formatting
    internal string[] tokenNames;
    // The token that was encountered
    public IToken token;
    // The offending AST node if tree walking
    public AST node;
    
    internal string tokenText = null; // taken from node or token object
    
    // Types of tokens
    public enum TokenTypeEnum
    {
      TokenType = 1,
      NotTokenType = 2,
      RangeType = 3,
      NotRangeType = 4,
      SetType = 5,
      NotSetType = 6
    }
    // One of the above
    public TokenTypeEnum mismatchType;
    
    // For TOKEN/NOT_TOKEN and RANGE/NOT_RANGE
    public int expecting;
    
    // For RANGE/NOT_RANGE (expecting is lower bound of range)
    public int upper;
    
    // For SET/NOT_SET
    public BitSet bset;
    
    /*Looking for AST wildcard, didn't find it */
    public MismatchedTokenException() : base("Mismatched Token: expecting any AST node", "<AST>", - 1, - 1)
    {
    }
    
    // Expected range / not range
    public MismatchedTokenException(string[] tokenNames_, AST node_, int lower, int upper_, bool matchNot) : 
          base("Mismatched Token", "<AST>", - 1, - 1)
    {
      tokenNames = tokenNames_;
      node = node_;
      if (node_ == null)
      {
        tokenText = "<empty tree>";
      }
      else
      {
        tokenText = node_.ToString();
      }
      mismatchType = matchNot ? TokenTypeEnum.NotRangeType : TokenTypeEnum.RangeType;
      expecting = lower;
      upper = upper_;
    }
    
    // Expected token / not token
    public MismatchedTokenException(string[] tokenNames_, AST node_, int expecting_, bool matchNot) :
          base("Mismatched Token", "<AST>", - 1, - 1)
    {
      tokenNames = tokenNames_;
      node = node_;
      if (node_ == null)
      {
        tokenText = "<empty tree>";
      }
      else
      {
        tokenText = node_.ToString();
      }
      mismatchType = matchNot ? TokenTypeEnum.NotTokenType : TokenTypeEnum.TokenType;
      expecting = expecting_;
    }
    
    // Expected BitSet / not BitSet
    public MismatchedTokenException(string[] tokenNames_, AST node_, BitSet set_, bool matchNot) :
          base("Mismatched Token", "<AST>", - 1, - 1)
    {
      tokenNames = tokenNames_;
      node = node_;
      if (node_ == null)
      {
        tokenText = "<empty tree>";
      }
      else
      {
        tokenText = node_.ToString();
      }
      mismatchType = matchNot ? TokenTypeEnum.NotSetType : TokenTypeEnum.SetType;
      bset = set_;
    }
    
    // Expected range / not range
    public MismatchedTokenException(string[] tokenNames_, IToken token_, int lower, int upper_, bool matchNot, string fileName_) : 
          base("Mismatched Token", fileName_, token_.getLine(), token_.getColumn())
    {
      tokenNames = tokenNames_;
      token = token_;
      tokenText = token_.getText();
      mismatchType = matchNot ? TokenTypeEnum.NotRangeType : TokenTypeEnum.RangeType;
      expecting = lower;
      upper = upper_;
    }
    
    // Expected token / not token
    public MismatchedTokenException(string[] tokenNames_, IToken token_, int expecting_, bool matchNot, string fileName_) :
          base("Mismatched Token", fileName_, token_.getLine(), token_.getColumn())
    {
      tokenNames = tokenNames_;
      token = token_;
      tokenText = token_.getText();
      mismatchType = matchNot ? TokenTypeEnum.NotTokenType : TokenTypeEnum.TokenType;
      expecting = expecting_;
    }
    
    // Expected BitSet / not BitSet
    public MismatchedTokenException(string[] tokenNames_, IToken token_, BitSet set_, bool matchNot, string fileName_) :
          base("Mismatched Token", fileName_, token_.getLine(), token_.getColumn())
    {
      tokenNames = tokenNames_;
      token = token_;
      tokenText = token_.getText();
      mismatchType = matchNot ? TokenTypeEnum.NotSetType : TokenTypeEnum.SetType;
      bset = set_;
    }
    
    /*
    * Returns a clean error message (no line number/column information)
    */
    override public string Message
    {
      get 
      {
        StringBuilder sb = new StringBuilder();
      
        switch (mismatchType)
        {
          case TokenTypeEnum.TokenType: 
            sb.Append("expecting " + tokenName(expecting) + ", found '" + tokenText + "'");
            break;
        
          case TokenTypeEnum.NotTokenType: 
            sb.Append("expecting anything but " + tokenName(expecting) + "; got it anyway");
            break;
        
          case TokenTypeEnum.RangeType: 
            sb.Append("expecting token in range: " + tokenName(expecting) + ".." + tokenName(upper) + ", found '" + tokenText + "'");
            break;
        
          case TokenTypeEnum.NotRangeType: 
            sb.Append("expecting token NOT in range: " + tokenName(expecting) + ".." + tokenName(upper) + ", found '" + tokenText + "'");
            break;
        
          case TokenTypeEnum.SetType: case TokenTypeEnum.NotSetType: 
            sb.Append("expecting " + (mismatchType == TokenTypeEnum.NotSetType ? "NOT " : "") + "one of (");
            int[] elems = bset.toArray();
            for (int i = 0; i < elems.Length; i++)
            {
              sb.Append(" ");
              sb.Append(tokenName(elems[i]));
            }
            sb.Append("), found '" + tokenText + "'");
            break;
        
          default: 
            sb.Append(base.Message);
            break;        
        }      
        return sb.ToString();
      }
    }
    
    private string tokenName(int tokenType)
    {
      if (tokenType == Token.INVALID_TYPE)
      {
        return "<Set of tokens>";
      }
      else if (tokenType < 0 || tokenType >= tokenNames.Length)
      {
        return "<" + tokenType.ToString() + ">";
      }
      else
      {
        return tokenNames[tokenType];
      }
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.