All Downloads are FREE. Search and download functionalities are using the official Maven repository.

CSharp.src.CommonToken.cs Maven / Gradle / Ivy

There is a newer version: 4.13.2
Show newest version
/* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
 * Use of this file is governed by the BSD 3-clause license that
 * can be found in the LICENSE.txt file in the project root.
 */
using System;
using Antlr4.Runtime;
using Antlr4.Runtime.Misc;
using Antlr4.Runtime.Sharpen;

namespace Antlr4.Runtime
{
    [System.Serializable]
    public class CommonToken : IWritableToken
    {
        private const long serialVersionUID = -6708843461296520577L;

        /// 
        /// An empty
        /// 
        /// which is used as the default value of
        /// 
        /// for tokens that do not have a source.
        /// 
        protected internal static readonly Tuple EmptySource = Tuple.Create(null, null);

        /// 
        /// This is the backing field for the  property.
        /// 
        private int _type;

        /// 
        /// This is the backing field for the  property.
        /// 
        private int _line;

        /// 
        /// This is the backing field for the  property.
        /// 
        protected internal int charPositionInLine = -1;

        /// 
        /// This is the backing field for the  property.
        /// 
        private int _channel = TokenConstants.DefaultChannel;

        /// 
        /// This is the backing field for
        /// 
        /// and
        /// 
        /// .
        /// 

/// These properties share a field to reduce the memory footprint of /// /// . Tokens created by a /// /// from /// the same source and input stream share a reference to the same /// /// containing these values.

///
[NotNull] protected internal Tuple source; /// /// This is the backing field for the property. /// /// private string _text; /// /// This is the backing field for the property. /// protected internal int index = -1; /// /// This is the backing field for the property. /// protected internal int start; /// /// This is the backing field for the property. /// protected internal int stop; /// /// Constructs a new /// /// with the specified token type. /// /// The token type. public CommonToken(int type) { // set to invalid position this._type = type; this.source = EmptySource; } public CommonToken(Tuple source, int type, int channel, int start, int stop) { this.source = source; this._type = type; this._channel = channel; this.start = start; this.stop = stop; if (source.Item1 != null) { this._line = source.Item1.Line; this.charPositionInLine = source.Item1.Column; } } /// /// Constructs a new /// /// with the specified token type and /// text. /// /// The token type. /// The text of the token. public CommonToken(int type, string text) { this._type = type; this._channel = TokenConstants.DefaultChannel; this._text = text; this.source = EmptySource; } /// /// Constructs a new /// /// as a copy of another /// /// . ///

/// If /// /// is also a /// /// instance, the newly /// constructed token will share a reference to the /// /// field and /// the /// /// stored in /// /// . Otherwise, /// /// will /// be assigned the result of calling /// /// , and /// /// will be constructed from the result of /// /// and /// /// .

///
/// The token to copy. public CommonToken(IToken oldToken) { _type = oldToken.Type; _line = oldToken.Line; index = oldToken.TokenIndex; charPositionInLine = oldToken.Column; _channel = oldToken.Channel; start = oldToken.StartIndex; stop = oldToken.StopIndex; if (oldToken is Antlr4.Runtime.CommonToken) { _text = ((Antlr4.Runtime.CommonToken)oldToken)._text; source = ((Antlr4.Runtime.CommonToken)oldToken).source; } else { _text = oldToken.Text; source = Tuple.Create(oldToken.TokenSource, oldToken.InputStream); } } public virtual int Type { get { return _type; } set { this._type = value; } } public virtual int Line { get { return _line; } set { this._line = value; } } /// Explicitly set the text for this token. /// /// Explicitly set the text for this token. If {code text} is not /// /// , then /// /// will return this value rather than /// extracting the text from the input. /// /// /// The explicit text of the token, or /// /// if the text /// should be obtained from the input along with the start and stop indexes /// of the token. /// public virtual string Text { get { if (_text != null) { return _text; } ICharStream input = InputStream; if (input == null) { return null; } int n = input.Size; if (start < n && stop < n) { return input.GetText(Interval.Of(start, stop)); } else { return ""; } } set { this._text = value; } } public virtual int Column { get { return charPositionInLine; } set { int charPositionInLine = value; this.charPositionInLine = charPositionInLine; } } public virtual int Channel { get { return _channel; } set { this._channel = value; } } public virtual int StartIndex { get { return start; } set { int start = value; this.start = start; } } public virtual int StopIndex { get { return stop; } set { int stop = value; this.stop = stop; } } public virtual int TokenIndex { get { return index; } set { int index = value; this.index = index; } } public virtual ITokenSource TokenSource { get { return source.Item1; } } public virtual ICharStream InputStream { get { return source.Item2; } } public override string ToString() { string channelStr = string.Empty; if (_channel > 0) { channelStr = ",channel=" + _channel; } string txt = Text; if (txt != null) { txt = txt.Replace("\n", "\\n"); txt = txt.Replace("\r", "\\r"); txt = txt.Replace("\t", "\\t"); } else { txt = ""; } return "[@" + TokenIndex + "," + start + ":" + stop + "='" + txt + "',<" + _type + ">" + channelStr + "," + _line + ":" + Column + "]"; } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy