Skip to content

Commit b18c5d0

Browse files
authored
Merge pull request antlr#1387 from Marti2203/master
Made a working LexerAdaptor
2 parents b79ea7d + ef88cab commit b18c5d0

File tree

1 file changed

+42
-41
lines changed

1 file changed

+42
-41
lines changed

antlr4/LexerAdaptor.cs

+42-41
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,30 @@
1-
namespace GrammarGrammar
1+
using System;
2+
using System.Reflection;
3+
namespace GrammarGrammar
24
{
3-
5+
using System.IO;
46
using Antlr4.Runtime;
57
using Antlr4.Runtime.Misc;
68

79
#pragma warning disable CA1012 // Abstract types should not have constructors
810
public abstract class LexerAdaptor : Lexer
9-
#pragma warning restore CA1012 // But Lexer demands it
11+
#pragma warning restore CA1012 // But Lexer demands it - old
1012
{
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)
1319
{
20+
stream = input;
1421
}
1522

23+
protected LexerAdaptor(ICharStream input, TextWriter output, TextWriter errorOutput)
24+
: base(input, output, errorOutput)
25+
{
26+
stream = input;
27+
}
1628
/**
1729
* Track whether we are inside of a rule and whether it is lexical parser. _currentRuleType==TokenConstants.InvalidType
1830
* 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)
2537
* The whole point of this state information is to distinguish between [..arg actions..] and [charsets]. Char sets
2638
* can only occur in lexical rules and arg actions cannot occur.
2739
*/
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;
3941

4042
protected void handleBeginArgument()
4143
{
42-
if (inLexerRule())
44+
if (InLexerRule)
4345
{
4446
PushMode(ANTLRv4Lexer.LexerCharSet);
4547
More();
@@ -53,58 +55,57 @@ protected void handleBeginArgument()
5355
protected void handleEndArgument()
5456
{
5557
PopMode();
56-
if (_modeStack.Count > 0)
58+
if (ModeStack.Count > 0)
5759
{
58-
setCurrentRuleType(ANTLRv4Lexer.ARGUMENT_CONTENT);
60+
CurrentRuleType = (ANTLRv4Lexer.ARGUMENT_CONTENT);
5961
}
6062
}
6163

6264
protected void handleEndAction()
6365
{
6466
PopMode();
65-
if (_modeStack.Count > 0)
67+
if (ModeStack.Count > 0)
6668
{
67-
setCurrentRuleType(ANTLRv4Lexer.ACTION_CONTENT);
69+
CurrentRuleType = (ANTLRv4Lexer.ACTION_CONTENT);
6870
}
6971
}
7072

7173

7274
public override IToken NextToken()
7375
{
74-
if (_type == ANTLRv4Lexer.ID)
76+
var token = base.NextToken();
77+
if (Type == ANTLRv4Lexer.ID)
7578
{
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))
7881
{
79-
_type = ANTLRv4Lexer.TOKEN_REF;
82+
Type = ANTLRv4Lexer.TOKEN_REF;
83+
tokenInput.SetValue(token, ANTLRv4Lexer.TOKEN_REF);
8084
}
81-
else
85+
if (char.IsLower(firstChar))
8286
{
83-
_type = ANTLRv4Lexer.RULE_REF;
87+
88+
Type = ANTLRv4Lexer.RULE_REF;
89+
tokenInput.SetValue(token, ANTLRv4Lexer.RULE_REF);
8490
}
8591

86-
if (_currentRuleType == TokenConstants.InvalidType)
92+
if (CurrentRuleType == TokenConstants.InvalidType)
8793
{ // 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
8995
}
9096
}
91-
else if (_type == ANTLRv4Lexer.SEMI)
97+
else if (Type == ANTLRv4Lexer.SEMI)
9298
{ // exit rule def
93-
_currentRuleType = TokenConstants.InvalidType;
99+
CurrentRuleType = TokenConstants.InvalidType;
94100
}
95101

96-
return base.NextToken();
102+
return token;
97103
}
98104

99-
private bool inLexerRule()
100-
{
101-
return _currentRuleType == ANTLRv4Lexer.TOKEN_REF;
102-
}
105+
private bool InLexerRule => CurrentRuleType == ANTLRv4Lexer.TOKEN_REF;
103106

104107

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+
109110
}
110-
}
111+
}

0 commit comments

Comments
 (0)