1
- namespace GrammarGrammar
1
+ using System ;
2
+ using System . Reflection ;
3
+ namespace GrammarGrammar
2
4
{
3
-
5
+ using System . IO ;
4
6
using Antlr4 . Runtime ;
5
7
using Antlr4 . Runtime . Misc ;
6
8
7
9
#pragma warning disable CA1012 // Abstract types should not have constructors
8
10
public abstract class LexerAdaptor : Lexer
9
- #pragma warning restore CA1012 // But Lexer demands it
11
+ #pragma warning restore CA1012 // But Lexer demands it - old
10
12
{
11
- public LexerAdaptor ( ICharStream input )
12
- : base ( input )
13
+ // I copy a reference to the stream, so It can be used as a Char Stream, not as a IISStream
14
+ readonly ICharStream stream ;
15
+ // Tokens are read only so I hack my way
16
+ readonly FieldInfo tokenInput = typeof ( CommonToken ) . GetField ( "_type" , BindingFlags . NonPublic | BindingFlags . Instance ) ;
17
+ protected LexerAdaptor ( ICharStream input )
18
+ : base ( input , Console . Out , Console . Error )
13
19
{
20
+ stream = input ;
14
21
}
15
22
23
+ protected LexerAdaptor ( ICharStream input , TextWriter output , TextWriter errorOutput )
24
+ : base ( input , output , errorOutput )
25
+ {
26
+ stream = input ;
27
+ }
16
28
/**
17
29
* Track whether we are inside of a rule and whether it is lexical parser. _currentRuleType==TokenConstants.InvalidType
18
30
* means that we are outside of a rule. At the first sign of a rule name reference and _currentRuleType==invalid, we
@@ -25,21 +37,11 @@ public LexerAdaptor(ICharStream input)
25
37
* The whole point of this state information is to distinguish between [..arg actions..] and [charsets]. Char sets
26
38
* can only occur in lexical rules and arg actions cannot occur.
27
39
*/
28
- private int _currentRuleType = TokenConstants . InvalidType ;
29
-
30
- public int getCurrentRuleType ( )
31
- {
32
- return _currentRuleType ;
33
- }
34
-
35
- public void setCurrentRuleType ( int ruleType )
36
- {
37
- this . _currentRuleType = ruleType ;
38
- }
40
+ private int CurrentRuleType { get ; set ; } = TokenConstants . InvalidType ;
39
41
40
42
protected void handleBeginArgument ( )
41
43
{
42
- if ( inLexerRule ( ) )
44
+ if ( InLexerRule )
43
45
{
44
46
PushMode ( ANTLRv4Lexer . LexerCharSet ) ;
45
47
More ( ) ;
@@ -53,58 +55,57 @@ protected void handleBeginArgument()
53
55
protected void handleEndArgument ( )
54
56
{
55
57
PopMode ( ) ;
56
- if ( _modeStack . Count > 0 )
58
+ if ( ModeStack . Count > 0 )
57
59
{
58
- setCurrentRuleType ( ANTLRv4Lexer . ARGUMENT_CONTENT ) ;
60
+ CurrentRuleType = ( ANTLRv4Lexer . ARGUMENT_CONTENT ) ;
59
61
}
60
62
}
61
63
62
64
protected void handleEndAction ( )
63
65
{
64
66
PopMode ( ) ;
65
- if ( _modeStack . Count > 0 )
67
+ if ( ModeStack . Count > 0 )
66
68
{
67
- setCurrentRuleType ( ANTLRv4Lexer . ACTION_CONTENT ) ;
69
+ CurrentRuleType = ( ANTLRv4Lexer . ACTION_CONTENT ) ;
68
70
}
69
71
}
70
72
71
73
72
74
public override IToken NextToken ( )
73
75
{
74
- if ( _type == ANTLRv4Lexer . ID )
76
+ var token = base . NextToken ( ) ;
77
+ if ( Type == ANTLRv4Lexer . ID )
75
78
{
76
- string firstChar = _input . GetText ( Interval . Of ( _tokenStartCharIndex , _tokenStartCharIndex ) ) ;
77
- if ( char . IsUpper ( firstChar [ 0 ] ) )
79
+ char firstChar = stream . GetText ( Interval . Of ( TokenStartCharIndex , TokenStartCharIndex ) ) [ 0 ] ;
80
+ if ( char . IsUpper ( firstChar ) )
78
81
{
79
- _type = ANTLRv4Lexer . TOKEN_REF ;
82
+ Type = ANTLRv4Lexer . TOKEN_REF ;
83
+ tokenInput . SetValue ( token , ANTLRv4Lexer . TOKEN_REF ) ;
80
84
}
81
- else
85
+ if ( char . IsLower ( firstChar ) )
82
86
{
83
- _type = ANTLRv4Lexer . RULE_REF ;
87
+
88
+ Type = ANTLRv4Lexer . RULE_REF ;
89
+ tokenInput . SetValue ( token , ANTLRv4Lexer . RULE_REF ) ;
84
90
}
85
91
86
- if ( _currentRuleType == TokenConstants . InvalidType )
92
+ if ( CurrentRuleType == TokenConstants . InvalidType )
87
93
{ // if outside of rule def
88
- _currentRuleType = _type ; // set to inside lexer or parser rule
94
+ CurrentRuleType = Type ; // set to inside lexer or parser rule
89
95
}
90
96
}
91
- else if ( _type == ANTLRv4Lexer . SEMI )
97
+ else if ( Type == ANTLRv4Lexer . SEMI )
92
98
{ // exit rule def
93
- _currentRuleType = TokenConstants . InvalidType ;
99
+ CurrentRuleType = TokenConstants . InvalidType ;
94
100
}
95
101
96
- return base . NextToken ( ) ;
102
+ return token ;
97
103
}
98
104
99
- private bool inLexerRule ( )
100
- {
101
- return _currentRuleType == ANTLRv4Lexer . TOKEN_REF ;
102
- }
105
+ private bool InLexerRule => CurrentRuleType == ANTLRv4Lexer . TOKEN_REF ;
103
106
104
107
105
- private bool inParserRule ( )
106
- { // not used, but added for clarity
107
- return _currentRuleType == ANTLRv4Lexer . RULE_REF ;
108
- }
108
+ private bool InParserRule => CurrentRuleType == ANTLRv4Lexer . RULE_REF ;
109
+
109
110
}
110
- }
111
+ }
0 commit comments