3434// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3535#endregion
3636
37+ using System ;
38+
3739using Antlr4 . Runtime ;
40+ using Antlr4 . Runtime . Misc ;
3841
3942using Org . Edgerunner . ANTLR4 . Tools . Common . Extensions ;
4043
@@ -44,55 +47,79 @@ namespace Org.Edgerunner.ANTLR4.Tools.Common.Grammar
4447 /// Struct that represents a lightweight view model for IToken instances
4548 /// </summary>
4649 /// <seealso cref="Antlr4.Runtime.IToken"/>
47- public struct SyntaxToken
50+ public class SyntaxToken : CommonToken
4851 {
52+ private string _DisplayText ;
53+ private string _TypeNameUpperCase ;
54+ private int ? _EndingLineNumber ;
55+ private int ? _EndingColumnPosition ;
56+ private Place ? _EndPlace ;
57+
58+ private string _TypeName ;
59+
60+ /// <summary>
61+ /// Initializes a new instance of the <see cref="SyntaxToken"/> class.
62+ /// </summary>
63+ /// <param name="source">The source.</param>
64+ /// <param name="type">The type.</param>
65+ /// <param name="channel">The channel.</param>
66+ /// <param name="start">The start.</param>
67+ /// <param name="stop">The stop.</param>
68+ public SyntaxToken ( [ NotNull ] Tuple < ITokenSource , ICharStream > source , int type , int channel , int start , int stop )
69+ : base ( source , type , channel , start , stop )
70+ {
71+ ColumnPosition = charPositionInLine + 1 ;
72+ Length = stop - start + 1 ;
73+
74+ //TypeName = parserToken.Type > -1 ? lexer.Vocabulary.GetDisplayName(parserToken.Type) : string.Empty;
75+ //TypeNameUpperCase = TypeName.ToUpperInvariant();
76+ }
77+
4978 /// <summary>
50- /// Initializes a new instance of the <see cref="SyntaxToken"/> struct .
79+ /// Initializes a new instance of the <see cref="SyntaxToken"/> class .
5180 /// </summary>
52- /// <param name="lexer">The lexer.</param>
53- /// <param name="parserToken">The token.</param>
54- public SyntaxToken ( Lexer lexer , IToken parserToken )
81+ /// <param name="type">The type.</param>
82+ /// <param name="text">The text.</param>
83+ public SyntaxToken ( int type , string text )
84+ : base ( type , text )
5585 {
56- ActualParserToken = parserToken ;
57- Text = FormatTokenText ( parserToken ) ;
58- Type = parserToken . Type > - 1 ? lexer . Vocabulary . GetDisplayName ( parserToken . Type ) : string . Empty ;
59- TypeUpperCase = Type . ToUpperInvariant ( ) ;
60- LineNumber = parserToken . Line ;
61- ColumnPosition = parserToken . Column + 1 ;
62- ChannelId = parserToken . Channel ;
63- Length = parserToken . StopIndex - parserToken . StartIndex + 1 ;
64- StartPosition = parserToken . StartIndex ;
65- StopPosition = parserToken . StopIndex ;
66-
67- var spot = parserToken . GetEndPlace ( ) ;
68- EndingLineNumber = spot . Line ;
69- EndingColumnPosition = spot . Position + 1 ;
7086 }
7187
7288 /// <summary>
73- /// Gets the token text .
89+ /// Initializes a new instance of the <see cref="SyntaxToken"/> class .
7490 /// </summary>
75- /// <value>The token text.</value>
76- public string Text { get ; }
91+ /// <param name="type">The type.</param>
92+ public SyntaxToken ( int type )
93+ : base ( type )
94+ {
95+ }
7796
7897 /// <summary>
79- /// Gets the token type .
98+ /// Gets the token display text .
8099 /// </summary>
81- /// <value>The token type .</value>
82- public string Type { get ; }
100+ /// <value>The token display text .</value>
101+ public string DisplayText => _DisplayText ?? ( _DisplayText = FormatTokenText ( Text ) ) ;
83102
84103 /// <summary>
85- /// Gets the upper case type.
104+ /// Gets or sets the token type.
86105 /// </summary>
87- /// <value>The upper case type.</value>
88- /// <remarks>Exists solely to avoid repeated upper casing of the Type property.</remarks>
89- public string TypeUpperCase { get ; }
106+ /// <value>The token type name.</value>
107+ public string TypeName
108+ {
109+ get => _TypeName ;
110+ set
111+ {
112+ _TypeNameUpperCase = null ;
113+ _TypeName = value ;
114+ }
115+ }
90116
91117 /// <summary>
92- /// Gets the line number where the token occurs .
118+ /// Gets the upper case type name .
93119 /// </summary>
94- /// <value>The line number.</value>
95- public int LineNumber { get ; }
120+ /// <value>The upper case type name.</value>
121+ /// <remarks>Exists solely to avoid repeated upper casing of the TypeName property.</remarks>
122+ public string TypeNameUpperCase => _TypeNameUpperCase ?? ( _TypeNameUpperCase = TypeName . ToUpperInvariant ( ) ) ;
96123
97124 /// <summary>
98125 /// Gets the column position of the token within the source line.
@@ -110,48 +137,50 @@ public SyntaxToken(Lexer lexer, IToken parserToken)
110137 /// Gets the line number for the end of token.
111138 /// </summary>
112139 /// <value>The ending line number.</value>
113- public int EndingLineNumber { get ; }
140+ public int EndingLineNumber
141+ {
142+ get
143+ {
144+ if ( ! _EndingLineNumber . HasValue )
145+ {
146+ if ( ! _EndPlace . HasValue )
147+ _EndPlace = this . GetEndPlace ( ) ;
148+ _EndingLineNumber = _EndPlace . Value . Line ;
149+ }
150+
151+ return _EndingLineNumber . Value ;
152+ }
153+ }
114154
115155 /// <summary>
116156 /// Gets the column position for the end of the token.
117157 /// </summary>
118158 /// <value>The ending column position.</value>
119- public int EndingColumnPosition { get ; }
120-
121- /// <summary>
122- /// Gets the start position of the token within the source.
123- /// </summary>
124- /// <value>The start position.</value>
125- public int StartPosition { get ; }
126-
127- /// <summary>
128- /// Gets the stop position of the token within the source.
129- /// </summary>
130- /// <value>The stop position.</value>
131- public int StopPosition { get ; }
132-
133- /// <summary>
134- /// Gets the channel id for the token.
135- /// </summary>
136- /// <value>The channel id.</value>
137- public int ChannelId { get ; }
138-
139- /// <summary>
140- /// Gets the actual parser token.
141- /// </summary>
142- /// <value>The actual parser token.</value>
143- public IToken ActualParserToken { get ; }
159+ public int EndingColumnPosition
160+ {
161+ get
162+ {
163+ if ( ! _EndingColumnPosition . HasValue )
164+ {
165+ if ( ! _EndPlace . HasValue )
166+ _EndPlace = this . GetEndPlace ( ) ;
167+ _EndingColumnPosition = _EndPlace . Value . Position + 1 ;
168+ }
169+
170+ return _EndingColumnPosition . Value ;
171+ }
172+ }
144173
145- private static string FormatTokenText ( IToken token )
174+ private static string FormatTokenText ( string text )
146175 {
147- switch ( token . Text )
176+ switch ( text )
148177 {
149178 case "\r " : return "\\ r" ;
150179 case "\n " : return "\\ n" ;
151180 case "\t " : return "\\ t" ;
152181 }
153182
154- return token . Text ;
183+ return text ;
155184 }
156185 }
157186}
0 commit comments