2013-02-16 05:30:47 +08:00
|
|
|
/*
|
|
|
|
* [The "BSD license"]
|
|
|
|
* Copyright (c) 2013 Terence Parr
|
|
|
|
* Copyright (c) 2013 Sam Harwell
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
using System;
|
|
|
|
using Antlr4.Runtime;
|
|
|
|
using Antlr4.Runtime.Misc;
|
2014-04-28 20:22:22 +08:00
|
|
|
using Antlr4.Runtime.Sharpen;
|
2013-02-16 05:30:47 +08:00
|
|
|
|
|
|
|
namespace Antlr4.Runtime
|
|
|
|
{
|
2013-02-16 22:14:20 +08:00
|
|
|
[System.Serializable]
|
|
|
|
public class CommonToken : IWritableToken
|
|
|
|
{
|
|
|
|
private const long serialVersionUID = -6708843461296520577L;
|
2013-02-16 05:30:47 +08:00
|
|
|
|
2014-02-17 04:51:37 +08:00
|
|
|
/// <summary>
|
|
|
|
/// An empty
|
2014-04-27 13:03:50 +08:00
|
|
|
/// <see cref="Tuple{T1, T2}"/>
|
2014-02-17 04:51:37 +08:00
|
|
|
/// which is used as the default value of
|
2014-07-05 05:23:31 +08:00
|
|
|
/// <see cref="source"/>
|
2014-02-17 04:51:37 +08:00
|
|
|
/// for tokens that do not have a source.
|
|
|
|
/// </summary>
|
2014-02-17 03:33:54 +08:00
|
|
|
protected internal static readonly Tuple<ITokenSource, ICharStream> EmptySource = Tuple.Create<ITokenSource, ICharStream>(null, null);
|
2013-02-25 02:42:19 +08:00
|
|
|
|
2014-02-17 04:51:37 +08:00
|
|
|
/// <summary>
|
2014-04-27 13:03:50 +08:00
|
|
|
/// This is the backing field for the <see cref="Type"/> property.
|
2014-02-17 04:51:37 +08:00
|
|
|
/// </summary>
|
2013-02-16 22:14:20 +08:00
|
|
|
protected internal int type;
|
2013-02-16 05:30:47 +08:00
|
|
|
|
2014-02-17 04:51:37 +08:00
|
|
|
/// <summary>
|
2014-04-27 13:03:50 +08:00
|
|
|
/// This is the backing field for the <see cref="Line"/> property.
|
2014-02-17 04:51:37 +08:00
|
|
|
/// </summary>
|
2013-02-16 22:14:20 +08:00
|
|
|
protected internal int line;
|
2013-02-16 05:30:47 +08:00
|
|
|
|
2014-02-17 04:51:37 +08:00
|
|
|
/// <summary>
|
2014-04-27 13:03:50 +08:00
|
|
|
/// This is the backing field for the <see cref="Column"/> property.
|
2014-02-17 04:51:37 +08:00
|
|
|
/// </summary>
|
2013-02-16 22:14:20 +08:00
|
|
|
protected internal int charPositionInLine = -1;
|
2013-02-16 05:30:47 +08:00
|
|
|
|
2014-02-17 04:51:37 +08:00
|
|
|
/// <summary>
|
2014-04-27 13:03:50 +08:00
|
|
|
/// This is the backing field for the <see cref="Channel"/> property.
|
2014-02-17 04:51:37 +08:00
|
|
|
/// </summary>
|
2013-02-19 03:14:07 +08:00
|
|
|
protected internal int channel = TokenConstants.DefaultChannel;
|
2013-02-16 05:30:47 +08:00
|
|
|
|
2014-02-17 04:51:37 +08:00
|
|
|
/// <summary>
|
|
|
|
/// This is the backing field for
|
2014-07-05 05:23:31 +08:00
|
|
|
/// <see cref="TokenSource()"/>
|
2014-02-17 04:51:37 +08:00
|
|
|
/// and
|
2014-07-05 05:23:31 +08:00
|
|
|
/// <see cref="InputStream()"/>
|
2014-02-17 04:51:37 +08:00
|
|
|
/// .
|
|
|
|
/// <p>
|
|
|
|
/// These properties share a field to reduce the memory footprint of
|
2014-07-05 05:23:31 +08:00
|
|
|
/// <see cref="CommonToken"/>
|
2014-02-17 04:51:37 +08:00
|
|
|
/// . Tokens created by a
|
2014-07-05 05:23:31 +08:00
|
|
|
/// <see cref="CommonTokenFactory"/>
|
2014-02-17 04:51:37 +08:00
|
|
|
/// from
|
|
|
|
/// the same source and input stream share a reference to the same
|
2014-04-27 13:03:50 +08:00
|
|
|
/// <see cref="Tuple{T1, T2}"/>
|
2014-02-17 04:51:37 +08:00
|
|
|
/// containing these values.</p>
|
|
|
|
/// </summary>
|
|
|
|
[NotNull]
|
2013-02-16 22:14:20 +08:00
|
|
|
protected internal Tuple<ITokenSource, ICharStream> source;
|
2013-02-16 05:30:47 +08:00
|
|
|
|
2014-02-17 04:51:37 +08:00
|
|
|
/// <summary>
|
2014-04-27 13:03:50 +08:00
|
|
|
/// This is the backing field for the <see cref="Text"/> property.
|
2014-02-17 04:51:37 +08:00
|
|
|
/// </summary>
|
2014-04-27 13:03:50 +08:00
|
|
|
/// <seealso cref="Text"/>
|
2013-02-16 22:14:20 +08:00
|
|
|
protected internal string text;
|
2013-02-16 05:30:47 +08:00
|
|
|
|
2014-02-17 04:51:37 +08:00
|
|
|
/// <summary>
|
2014-04-27 13:03:50 +08:00
|
|
|
/// This is the backing field for the <see cref="TokenIndex"/> property.
|
2014-02-17 04:51:37 +08:00
|
|
|
/// </summary>
|
2013-02-16 22:14:20 +08:00
|
|
|
protected internal int index = -1;
|
2013-02-16 05:30:47 +08:00
|
|
|
|
2014-02-17 04:51:37 +08:00
|
|
|
/// <summary>
|
2014-04-27 13:03:50 +08:00
|
|
|
/// This is the backing field for the <see cref="StartIndex"/> property.
|
2014-02-17 04:51:37 +08:00
|
|
|
/// </summary>
|
2013-02-16 22:14:20 +08:00
|
|
|
protected internal int start;
|
2013-02-16 05:30:47 +08:00
|
|
|
|
2014-02-17 04:51:37 +08:00
|
|
|
/// <summary>
|
2014-04-27 13:03:50 +08:00
|
|
|
/// This is the backing field for the <see cref="StopIndex"/> property.
|
2014-02-17 04:51:37 +08:00
|
|
|
/// </summary>
|
2013-02-16 22:14:20 +08:00
|
|
|
protected internal int stop;
|
2013-02-16 05:30:47 +08:00
|
|
|
|
2014-02-17 04:51:37 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Constructs a new
|
2014-07-05 05:23:31 +08:00
|
|
|
/// <see cref="CommonToken"/>
|
2014-02-17 04:51:37 +08:00
|
|
|
/// with the specified token type.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="type">The token type.</param>
|
2013-02-16 22:14:20 +08:00
|
|
|
public CommonToken(int type)
|
|
|
|
{
|
|
|
|
// set to invalid position
|
|
|
|
this.type = type;
|
2014-02-17 04:51:37 +08:00
|
|
|
this.source = EmptySource;
|
2013-02-16 22:14:20 +08:00
|
|
|
}
|
2013-02-16 05:30:47 +08:00
|
|
|
|
2014-02-17 03:33:54 +08:00
|
|
|
public CommonToken(Tuple<ITokenSource, ICharStream> source, int type, int channel, int start, int stop)
|
2013-02-16 22:14:20 +08:00
|
|
|
{
|
|
|
|
this.source = source;
|
|
|
|
this.type = type;
|
|
|
|
this.channel = channel;
|
|
|
|
this.start = start;
|
|
|
|
this.stop = stop;
|
2013-02-17 04:34:47 +08:00
|
|
|
if (source.Item1 != null)
|
2013-02-16 22:14:20 +08:00
|
|
|
{
|
2013-02-17 04:34:47 +08:00
|
|
|
this.line = source.Item1.Line;
|
|
|
|
this.charPositionInLine = source.Item1.Column;
|
2013-02-16 22:14:20 +08:00
|
|
|
}
|
|
|
|
}
|
2013-02-16 05:30:47 +08:00
|
|
|
|
2014-02-17 04:51:37 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Constructs a new
|
2014-07-05 05:23:31 +08:00
|
|
|
/// <see cref="CommonToken"/>
|
2014-02-17 04:51:37 +08:00
|
|
|
/// with the specified token type and
|
|
|
|
/// text.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="type">The token type.</param>
|
|
|
|
/// <param name="text">The text of the token.</param>
|
2013-02-16 22:14:20 +08:00
|
|
|
public CommonToken(int type, string text)
|
|
|
|
{
|
|
|
|
this.type = type;
|
2013-02-19 03:14:07 +08:00
|
|
|
this.channel = TokenConstants.DefaultChannel;
|
2013-02-16 22:14:20 +08:00
|
|
|
this.text = text;
|
2013-02-25 02:42:19 +08:00
|
|
|
this.source = EmptySource;
|
2013-02-16 22:14:20 +08:00
|
|
|
}
|
2013-02-16 05:30:47 +08:00
|
|
|
|
2014-02-17 04:51:37 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Constructs a new
|
2014-07-05 05:23:31 +08:00
|
|
|
/// <see cref="CommonToken"/>
|
2014-02-17 04:51:37 +08:00
|
|
|
/// as a copy of another
|
2014-07-05 05:23:31 +08:00
|
|
|
/// <see cref="IToken"/>
|
2014-02-17 04:51:37 +08:00
|
|
|
/// .
|
|
|
|
/// <p>
|
|
|
|
/// If
|
2014-08-01 05:37:24 +08:00
|
|
|
/// <paramref name="oldToken"/>
|
2014-02-17 04:51:37 +08:00
|
|
|
/// is also a
|
2014-07-05 05:23:31 +08:00
|
|
|
/// <see cref="CommonToken"/>
|
2014-02-17 04:51:37 +08:00
|
|
|
/// instance, the newly
|
|
|
|
/// constructed token will share a reference to the
|
2014-07-05 05:23:31 +08:00
|
|
|
/// <see cref="text"/>
|
2014-02-17 04:51:37 +08:00
|
|
|
/// field and
|
|
|
|
/// the
|
2014-04-27 13:03:50 +08:00
|
|
|
/// <see cref="Tuple{T1, T2}"/>
|
2014-02-17 04:51:37 +08:00
|
|
|
/// stored in
|
2014-07-05 05:23:31 +08:00
|
|
|
/// <see cref="source"/>
|
2014-02-17 04:51:37 +08:00
|
|
|
/// . Otherwise,
|
2014-07-05 05:23:31 +08:00
|
|
|
/// <see cref="text"/>
|
2014-02-17 04:51:37 +08:00
|
|
|
/// will
|
|
|
|
/// be assigned the result of calling
|
2014-07-05 05:23:31 +08:00
|
|
|
/// <see cref="Text()"/>
|
2014-02-17 04:51:37 +08:00
|
|
|
/// , and
|
2014-07-05 05:23:31 +08:00
|
|
|
/// <see cref="source"/>
|
2014-02-17 04:51:37 +08:00
|
|
|
/// will be constructed from the result of
|
2014-07-05 05:23:31 +08:00
|
|
|
/// <see cref="IToken.TokenSource()"/>
|
2014-02-17 04:51:37 +08:00
|
|
|
/// and
|
2014-07-05 05:23:31 +08:00
|
|
|
/// <see cref="IToken.InputStream()"/>
|
2014-02-17 04:51:37 +08:00
|
|
|
/// .</p>
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="oldToken">The token to copy.</param>
|
2013-02-16 22:14:20 +08:00
|
|
|
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)
|
|
|
|
{
|
2014-02-17 04:51:37 +08:00
|
|
|
text = ((Antlr4.Runtime.CommonToken)oldToken).text;
|
2013-02-16 22:14:20 +08:00
|
|
|
source = ((Antlr4.Runtime.CommonToken)oldToken).source;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-02-17 04:51:37 +08:00
|
|
|
text = oldToken.Text;
|
2013-02-16 22:14:20 +08:00
|
|
|
source = Tuple.Create(oldToken.TokenSource, oldToken.InputStream);
|
|
|
|
}
|
|
|
|
}
|
2013-02-16 05:30:47 +08:00
|
|
|
|
2013-02-16 22:14:20 +08:00
|
|
|
public virtual int Type
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
set
|
|
|
|
{
|
|
|
|
int type = value;
|
|
|
|
this.type = type;
|
|
|
|
}
|
|
|
|
}
|
2013-02-16 22:09:48 +08:00
|
|
|
|
2013-02-16 22:14:20 +08:00
|
|
|
public virtual int Line
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
set
|
|
|
|
{
|
|
|
|
int line = value;
|
|
|
|
this.line = line;
|
|
|
|
}
|
|
|
|
}
|
2013-02-16 05:30:47 +08:00
|
|
|
|
2014-02-17 04:51:37 +08:00
|
|
|
/// <summary>Explicitly set the text for this token.</summary>
|
2013-02-16 22:14:20 +08:00
|
|
|
/// <remarks>
|
2014-02-17 04:51:37 +08:00
|
|
|
/// Explicitly set the text for this token. If {code text} is not
|
2014-08-01 05:37:24 +08:00
|
|
|
/// <see langword="null"/>
|
2014-02-17 04:51:37 +08:00
|
|
|
/// , then
|
2014-07-05 05:23:31 +08:00
|
|
|
/// <see cref="Text()"/>
|
2014-02-17 04:51:37 +08:00
|
|
|
/// will return this value rather than
|
|
|
|
/// extracting the text from the input.
|
2013-02-16 22:14:20 +08:00
|
|
|
/// </remarks>
|
2014-02-17 04:51:37 +08:00
|
|
|
/// <value>
|
|
|
|
/// The explicit text of the token, or
|
2014-08-01 05:37:24 +08:00
|
|
|
/// <see langword="null"/>
|
2014-02-17 04:51:37 +08:00
|
|
|
/// if the text
|
|
|
|
/// should be obtained from the input along with the start and stop indexes
|
|
|
|
/// of the token.
|
|
|
|
/// </value>
|
2013-02-16 22:14:20 +08:00
|
|
|
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 "<EOF>";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
set
|
|
|
|
{
|
|
|
|
string text = value;
|
|
|
|
this.text = text;
|
|
|
|
}
|
|
|
|
}
|
2013-02-16 05:30:47 +08:00
|
|
|
|
2013-02-16 22:14:20 +08:00
|
|
|
public virtual int Column
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return charPositionInLine;
|
|
|
|
}
|
|
|
|
set
|
|
|
|
{
|
|
|
|
int charPositionInLine = value;
|
|
|
|
this.charPositionInLine = charPositionInLine;
|
|
|
|
}
|
|
|
|
}
|
2013-02-16 05:30:47 +08:00
|
|
|
|
2013-02-16 22:14:20 +08:00
|
|
|
public virtual int Channel
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return channel;
|
|
|
|
}
|
|
|
|
set
|
|
|
|
{
|
|
|
|
int channel = value;
|
|
|
|
this.channel = channel;
|
|
|
|
}
|
|
|
|
}
|
2013-02-16 05:30:47 +08:00
|
|
|
|
2013-02-16 22:14:20 +08:00
|
|
|
public virtual int StartIndex
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return start;
|
|
|
|
}
|
2014-07-02 10:31:37 +08:00
|
|
|
set
|
|
|
|
{
|
|
|
|
int start = value;
|
|
|
|
this.start = start;
|
|
|
|
}
|
2013-02-16 22:14:20 +08:00
|
|
|
}
|
2013-02-16 05:30:47 +08:00
|
|
|
|
2013-02-16 22:14:20 +08:00
|
|
|
public virtual int StopIndex
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return stop;
|
|
|
|
}
|
2014-07-02 10:31:37 +08:00
|
|
|
set
|
|
|
|
{
|
|
|
|
int stop = value;
|
|
|
|
this.stop = stop;
|
|
|
|
}
|
2013-02-16 22:14:20 +08:00
|
|
|
}
|
2013-02-16 05:30:47 +08:00
|
|
|
|
2013-02-16 22:14:20 +08:00
|
|
|
public virtual int TokenIndex
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
set
|
|
|
|
{
|
|
|
|
int index = value;
|
|
|
|
this.index = index;
|
|
|
|
}
|
|
|
|
}
|
2013-02-16 05:30:47 +08:00
|
|
|
|
2013-02-16 22:14:20 +08:00
|
|
|
public virtual ITokenSource TokenSource
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
2013-02-17 04:34:47 +08:00
|
|
|
return source.Item1;
|
2013-02-16 22:14:20 +08:00
|
|
|
}
|
|
|
|
}
|
2013-02-16 05:30:47 +08:00
|
|
|
|
2013-02-16 22:14:20 +08:00
|
|
|
public virtual ICharStream InputStream
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
2013-02-17 04:34:47 +08:00
|
|
|
return source.Item2;
|
2013-02-16 22:14:20 +08:00
|
|
|
}
|
|
|
|
}
|
2013-02-16 05:30:47 +08:00
|
|
|
|
2013-02-16 22:14:20 +08:00
|
|
|
public override string ToString()
|
|
|
|
{
|
|
|
|
string channelStr = string.Empty;
|
|
|
|
if (channel > 0)
|
|
|
|
{
|
|
|
|
channelStr = ",channel=" + channel;
|
|
|
|
}
|
|
|
|
string txt = Text;
|
|
|
|
if (txt != null)
|
|
|
|
{
|
2013-02-25 02:42:19 +08:00
|
|
|
txt = txt.Replace("\n", "\\n");
|
|
|
|
txt = txt.Replace("\r", "\\r");
|
|
|
|
txt = txt.Replace("\t", "\\t");
|
2013-02-16 22:14:20 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
txt = "<no text>";
|
|
|
|
}
|
2014-02-17 03:33:54 +08:00
|
|
|
return "[@" + TokenIndex + "," + start + ":" + stop + "='" + txt + "',<" + type + ">" + channelStr + "," + line + ":" + Column + "]";
|
2013-02-16 22:14:20 +08:00
|
|
|
}
|
|
|
|
}
|
2013-02-16 05:30:47 +08:00
|
|
|
}
|