();
+ for (int i = 0; i < Count; i++)
+ {
+ result.Put(keys[i], values[i]);
+ }
+ return result;
}
- return result;
}
}
}
diff --git a/runtime/CSharp/Antlr4.Runtime/IToken.cs b/runtime/CSharp/Antlr4.Runtime/IToken.cs
index fa09681bc..b180d76f7 100644
--- a/runtime/CSharp/Antlr4.Runtime/IToken.cs
+++ b/runtime/CSharp/Antlr4.Runtime/IToken.cs
@@ -182,5 +182,25 @@ namespace Antlr4.Runtime
/// by parser.
///
public const int HiddenChannel = 1;
+
+ ///
+ /// This is the minimum constant value which can be assigned to a
+ /// user-defined token channel.
+ ///
+ ///
+ /// This is the minimum constant value which can be assigned to a
+ /// user-defined token channel.
+ ///
+ /// The non-negative numbers less than
+ ///
+ /// are
+ /// assigned to the predefined channels
+ ///
+ /// and
+ ///
+ /// .
+ ///
+ ///
+ public const int MinUserChannelValue = 2;
}
}
diff --git a/runtime/CSharp/Antlr4.Runtime/IVocabulary.cs b/runtime/CSharp/Antlr4.Runtime/IVocabulary.cs
new file mode 100644
index 000000000..a4f7262aa
--- /dev/null
+++ b/runtime/CSharp/Antlr4.Runtime/IVocabulary.cs
@@ -0,0 +1,198 @@
+/*
+ * [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 Antlr4.Runtime;
+using Antlr4.Runtime.Misc;
+using Antlr4.Runtime.Sharpen;
+
+namespace Antlr4.Runtime
+{
+ ///
+ /// This interface provides information about the vocabulary used by a
+ /// recognizer.
+ ///
+ ///
+ /// This interface provides information about the vocabulary used by a
+ /// recognizer.
+ ///
+ ///
+ /// Sam Harwell
+ public interface IVocabulary
+ {
+ /// Gets the string literal associated with a token type.
+ ///
+ /// Gets the string literal associated with a token type. The string returned
+ /// by this method, when not
+ ///
+ /// , can be used unaltered in a parser
+ /// grammar to represent this token type.
+ /// The following table shows examples of lexer rules and the literal
+ /// names assigned to the corresponding token types.
+ ///
+ ///
+ /// Rule |
+ /// Literal Name |
+ /// Java String Literal |
+ ///
+ ///
+ ///
+ /// THIS : 'this';
+ /// |
+ ///
+ /// 'this'
+ /// |
+ ///
+ /// "'this'"
+ /// |
+ ///
+ ///
+ ///
+ /// SQUOTE : '\'';
+ /// |
+ ///
+ /// '\''
+ /// |
+ ///
+ /// "'\\''"
+ /// |
+ ///
+ ///
+ ///
+ /// ID : [A-Z]+;
+ /// |
+ /// n/a |
+ ///
+ ///
+ /// |
+ ///
+ ///
+ ///
+ /// The token type.
+ ///
+ /// The string literal associated with the specified token type, or
+ ///
+ /// if no string literal is associated with the type.
+ ///
+ [Nullable]
+ string GetLiteralName(int tokenType);
+
+ /// Gets the symbolic name associated with a token type.
+ ///
+ /// Gets the symbolic name associated with a token type. The string returned
+ /// by this method, when not
+ ///
+ /// , can be used unaltered in a parser
+ /// grammar to represent this token type.
+ /// This method supports token types defined by any of the following
+ /// methods:
+ ///
+ /// - Tokens created by lexer rules.
+ /// - Tokens defined in a
+ ///
+ /// tokens
+ /// block in a lexer or parser
+ /// grammar.
+ /// - The implicitly defined
+ /// EOF
+ /// token, which has the token type
+ ///
+ /// .
+ ///
+ /// The following table shows examples of lexer rules and the literal
+ /// names assigned to the corresponding token types.
+ ///
+ ///
+ /// Rule |
+ /// Symbolic Name |
+ ///
+ ///
+ ///
+ /// THIS : 'this';
+ /// |
+ ///
+ /// THIS
+ /// |
+ ///
+ ///
+ ///
+ /// SQUOTE : '\'';
+ /// |
+ ///
+ /// SQUOTE
+ /// |
+ ///
+ ///
+ ///
+ /// ID : [A-Z]+;
+ /// |
+ ///
+ /// ID
+ /// |
+ ///
+ ///
+ ///
+ /// The token type.
+ ///
+ /// The symbolic name associated with the specified token type, or
+ ///
+ /// if no symbolic name is associated with the type.
+ ///
+ [Nullable]
+ string GetSymbolicName(int tokenType);
+
+ /// Gets the display name of a token type.
+ ///
+ /// Gets the display name of a token type.
+ /// ANTLR provides a default implementation of this method, but
+ /// applications are free to override the behavior in any manner which makes
+ /// sense for the application. The default implementation returns the first
+ /// result from the following list which produces a non-
+ ///
+ /// result.
+ ///
+ /// - The result of
+ ///
+ ///
+ /// - The result of
+ ///
+ ///
+ /// - The result of
+ ///
+ ///
+ ///
+ ///
+ /// The token type.
+ ///
+ /// The display name of the token type, for use in error reporting or
+ /// other user-visible messages which reference specific token types.
+ ///
+ [NotNull]
+ string GetDisplayName(int tokenType);
+ }
+}
diff --git a/runtime/CSharp/Antlr4.Runtime/LexerInterpreter.cs b/runtime/CSharp/Antlr4.Runtime/LexerInterpreter.cs
index 20f3acb7c..dd1d339d8 100644
--- a/runtime/CSharp/Antlr4.Runtime/LexerInterpreter.cs
+++ b/runtime/CSharp/Antlr4.Runtime/LexerInterpreter.cs
@@ -31,6 +31,7 @@ using System;
using System.Collections.Generic;
using Antlr4.Runtime;
using Antlr4.Runtime.Atn;
+using Antlr4.Runtime.Misc;
using Antlr4.Runtime.Sharpen;
namespace Antlr4.Runtime
@@ -41,13 +42,23 @@ namespace Antlr4.Runtime
protected internal readonly ATN atn;
+ [Obsolete]
protected internal readonly string[] tokenNames;
protected internal readonly string[] ruleNames;
protected internal readonly string[] modeNames;
+ [NotNull]
+ private readonly IVocabulary vocabulary;
+
+ [Obsolete]
public LexerInterpreter(string grammarFileName, ICollection tokenNames, ICollection ruleNames, ICollection modeNames, ATN atn, ICharStream input)
+ : this(grammarFileName, Antlr4.Runtime.Vocabulary.FromTokenNames(Sharpen.Collections.ToArray(tokenNames, new string[tokenNames.Count])), ruleNames, modeNames, atn, input)
+ {
+ }
+
+ public LexerInterpreter(string grammarFileName, IVocabulary vocabulary, ICollection ruleNames, ICollection modeNames, ATN atn, ICharStream input)
: base(input)
{
if (atn.grammarType != ATNType.Lexer)
@@ -56,9 +67,14 @@ namespace Antlr4.Runtime
}
this.grammarFileName = grammarFileName;
this.atn = atn;
- this.tokenNames = Sharpen.Collections.ToArray(tokenNames, new string[tokenNames.Count]);
+ this.tokenNames = new string[atn.maxTokenType];
+ for (int i = 0; i < tokenNames.Length; i++)
+ {
+ tokenNames[i] = vocabulary.GetDisplayName(i);
+ }
this.ruleNames = Sharpen.Collections.ToArray(ruleNames, new string[ruleNames.Count]);
this.modeNames = Sharpen.Collections.ToArray(modeNames, new string[modeNames.Count]);
+ this.vocabulary = vocabulary;
this._interp = new LexerATNSimulator(this, atn);
}
@@ -101,5 +117,17 @@ namespace Antlr4.Runtime
return modeNames;
}
}
+
+ public override IVocabulary Vocabulary
+ {
+ get
+ {
+ if (vocabulary != null)
+ {
+ return vocabulary;
+ }
+ return base.Vocabulary;
+ }
+ }
}
}
diff --git a/runtime/CSharp/Antlr4.Runtime/Misc/IntervalSet.cs b/runtime/CSharp/Antlr4.Runtime/Misc/IntervalSet.cs
index f9c421f10..9189b2126 100644
--- a/runtime/CSharp/Antlr4.Runtime/Misc/IntervalSet.cs
+++ b/runtime/CSharp/Antlr4.Runtime/Misc/IntervalSet.cs
@@ -703,7 +703,13 @@ namespace Antlr4.Runtime.Misc
return buf.ToString();
}
+ [System.ObsoleteAttribute(@"Use ToString(Antlr4.Runtime.IVocabulary) instead.")]
public virtual string ToString(string[] tokenNames)
+ {
+ return ToString(Vocabulary.FromTokenNames(tokenNames));
+ }
+
+ public virtual string ToString(IVocabulary vocabulary)
{
StringBuilder buf = new StringBuilder();
if (this.intervals == null || this.intervals.IsEmpty())
@@ -722,7 +728,7 @@ namespace Antlr4.Runtime.Misc
int b = I.b;
if (a == b)
{
- buf.Append(ElementName(tokenNames, a));
+ buf.Append(ElementName(vocabulary, a));
}
else
{
@@ -732,7 +738,7 @@ namespace Antlr4.Runtime.Misc
{
buf.Append(", ");
}
- buf.Append(ElementName(tokenNames, i));
+ buf.Append(ElementName(vocabulary, i));
}
}
if (iter.HasNext())
@@ -747,7 +753,14 @@ namespace Antlr4.Runtime.Misc
return buf.ToString();
}
+ [System.ObsoleteAttribute(@"Use ElementName(Antlr4.Runtime.IVocabulary, int) instead.")]
protected internal virtual string ElementName(string[] tokenNames, int a)
+ {
+ return ElementName(Vocabulary.FromTokenNames(tokenNames), a);
+ }
+
+ [NotNull]
+ protected internal virtual string ElementName(IVocabulary vocabulary, int a)
{
if (a == TokenConstants.Eof)
{
@@ -761,7 +774,7 @@ namespace Antlr4.Runtime.Misc
}
else
{
- return tokenNames[a];
+ return vocabulary.GetDisplayName(a);
}
}
}
diff --git a/runtime/CSharp/Antlr4.Runtime/Misc/RuleDependencyProcessor.cs b/runtime/CSharp/Antlr4.Runtime/Misc/RuleDependencyProcessor.cs
index 8b8cd8907..c9eb0e10c 100644
--- a/runtime/CSharp/Antlr4.Runtime/Misc/RuleDependencyProcessor.cs
+++ b/runtime/CSharp/Antlr4.Runtime/Misc/RuleDependencyProcessor.cs
@@ -36,6 +36,7 @@ using Antlr4.Runtime.Misc;
using Antlr4.Runtime.Sharpen;
using Antlr4.Runtime.Sharpen.Annotation;
using Javax.Annotation.Processing;
+using Javax.Lang.Model;
using Javax.Lang.Model.Element;
using Javax.Lang.Model.Type;
using Javax.Tools;
@@ -59,6 +60,27 @@ namespace Antlr4.Runtime.Misc
{
}
+ public override SourceVersion GetSupportedSourceVersion()
+ {
+ SourceVersion latestSupported = SourceVersion.LatestSupported();
+ if ((int)(latestSupported) <= 6)
+ {
+ return SourceVersion.Release6;
+ }
+ else
+ {
+ if ((int)(latestSupported) <= 8)
+ {
+ return latestSupported;
+ }
+ else
+ {
+ // this annotation processor is tested through Java 8
+ return SourceVersion.Values()[8];
+ }
+ }
+ }
+
public override bool Process<_T0>(HashSet<_T0> annotations, IRoundEnvironment roundEnv)
{
if (!CheckClassNameConstants())
diff --git a/runtime/CSharp/Antlr4.Runtime/Parser.cs b/runtime/CSharp/Antlr4.Runtime/Parser.cs
index 727f4ef26..53e4b6c65 100644
--- a/runtime/CSharp/Antlr4.Runtime/Parser.cs
+++ b/runtime/CSharp/Antlr4.Runtime/Parser.cs
@@ -1134,7 +1134,7 @@ namespace Antlr4.Runtime
for (int d = 0; d < _interp.atn.decisionToDFA.Length; d++)
{
DFA dfa = _interp.atn.decisionToDFA[d];
- s.Add(dfa.ToString(TokenNames, RuleNames));
+ s.Add(dfa.ToString(Vocabulary, RuleNames));
}
return s;
}
@@ -1154,7 +1154,7 @@ namespace Antlr4.Runtime
System.Console.Out.WriteLine();
}
System.Console.Out.WriteLine("Decision " + dfa.decision + ":");
- System.Console.Out.Write(dfa.ToString(TokenNames, RuleNames));
+ System.Console.Out.Write(dfa.ToString(Vocabulary, RuleNames));
seenOne = true;
}
}
diff --git a/runtime/CSharp/Antlr4.Runtime/ParserInterpreter.cs b/runtime/CSharp/Antlr4.Runtime/ParserInterpreter.cs
index d0bf0a245..c78d12af9 100644
--- a/runtime/CSharp/Antlr4.Runtime/ParserInterpreter.cs
+++ b/runtime/CSharp/Antlr4.Runtime/ParserInterpreter.cs
@@ -31,6 +31,7 @@ using System;
using System.Collections.Generic;
using Antlr4.Runtime;
using Antlr4.Runtime.Atn;
+using Antlr4.Runtime.Misc;
using Antlr4.Runtime.Sharpen;
namespace Antlr4.Runtime
@@ -58,19 +59,34 @@ namespace Antlr4.Runtime
protected internal readonly BitSet pushRecursionContextStates;
+ [Obsolete]
protected internal readonly string[] tokenNames;
protected internal readonly string[] ruleNames;
+ [NotNull]
+ private readonly IVocabulary vocabulary;
+
protected internal readonly Stack> _parentContextStack = new Stack>();
+ [System.ObsoleteAttribute(@"Use ParserInterpreter(string, IVocabulary, System.Collections.Generic.ICollection{E}, Antlr4.Runtime.Atn.ATN, ITokenStream) instead.")]
public ParserInterpreter(string grammarFileName, ICollection tokenNames, ICollection ruleNames, ATN atn, ITokenStream input)
+ : this(grammarFileName, Antlr4.Runtime.Vocabulary.FromTokenNames(Sharpen.Collections.ToArray(tokenNames, new string[tokenNames.Count])), ruleNames, atn, input)
+ {
+ }
+
+ public ParserInterpreter(string grammarFileName, IVocabulary vocabulary, ICollection ruleNames, ATN atn, ITokenStream input)
: base(input)
{
this.grammarFileName = grammarFileName;
this.atn = atn;
- this.tokenNames = Sharpen.Collections.ToArray(tokenNames, new string[tokenNames.Count]);
+ this.tokenNames = new string[atn.maxTokenType];
+ for (int i = 0; i < tokenNames.Length; i++)
+ {
+ tokenNames[i] = vocabulary.GetDisplayName(i);
+ }
this.ruleNames = Sharpen.Collections.ToArray(ruleNames, new string[ruleNames.Count]);
+ this.vocabulary = vocabulary;
// identify the ATN states where pushNewRecursionContext must be called
this.pushRecursionContextStates = new BitSet(atn.states.Count);
foreach (ATNState state in atn.states)
@@ -104,6 +120,14 @@ namespace Antlr4.Runtime
}
}
+ public override IVocabulary Vocabulary
+ {
+ get
+ {
+ return vocabulary;
+ }
+ }
+
public override string[] RuleNames
{
get
diff --git a/runtime/CSharp/Antlr4.Runtime/Recognizer`2.cs b/runtime/CSharp/Antlr4.Runtime/Recognizer`2.cs
index 2a2352816..018ab2068 100644
--- a/runtime/CSharp/Antlr4.Runtime/Recognizer`2.cs
+++ b/runtime/CSharp/Antlr4.Runtime/Recognizer`2.cs
@@ -41,13 +41,13 @@ namespace Antlr4.Runtime
{
public const int Eof = -1;
- private static readonly IDictionary> tokenTypeMapCache = new WeakHashMap>();
+ private static readonly IDictionary> tokenTypeMapCache = new WeakHashMap>();
private static readonly IDictionary> ruleIndexMapCache = new WeakHashMap>();
- private sealed class _CopyOnWriteArrayList_59 : CopyOnWriteArrayList>
+ private sealed class _CopyOnWriteArrayList_60 : CopyOnWriteArrayList>
{
- public _CopyOnWriteArrayList_59()
+ public _CopyOnWriteArrayList_60()
{
{
this.Add(ConsoleErrorListener.Instance);
@@ -56,7 +56,7 @@ namespace Antlr4.Runtime
}
[NotNull]
- private IList> _listeners = new _CopyOnWriteArrayList_59();
+ private IList> _listeners = new _CopyOnWriteArrayList_60();
protected internal ATNInterpreter _interp;
@@ -71,6 +71,7 @@ namespace Antlr4.Runtime
/// error reporting. The generated parsers implement a method
/// that overrides this to point to their String[] tokenNames.
///
+ [System.ObsoleteAttribute(@"Use Recognizer{Symbol, ATNInterpreter}.Vocabulary() instead.")]
public abstract string[] TokenNames
{
get;
@@ -81,6 +82,22 @@ namespace Antlr4.Runtime
get;
}
+ /// Get the vocabulary used by the recognizer.
+ /// Get the vocabulary used by the recognizer.
+ ///
+ /// A
+ ///
+ /// instance providing information about the
+ /// vocabulary used by the grammar.
+ ///
+ public virtual IVocabulary Vocabulary
+ {
+ get
+ {
+ return Antlr4.Runtime.Vocabulary.FromTokenNames(TokenNames);
+ }
+ }
+
/// Get a map from token names to token types.
///
/// Get a map from token names to token types.
@@ -90,20 +107,29 @@ namespace Antlr4.Runtime
{
get
{
- string[] tokenNames = TokenNames;
- if (tokenNames == null)
- {
- throw new NotSupportedException("The current recognizer does not provide a list of token names.");
- }
+ IVocabulary vocabulary = Vocabulary;
lock (tokenTypeMapCache)
{
- IDictionary result = tokenTypeMapCache.Get(tokenNames);
+ IDictionary result = tokenTypeMapCache.Get(vocabulary);
if (result == null)
{
- result = Utils.ToMap(tokenNames);
+ result = new Dictionary();
+ for (int i = 0; i < Atn.maxTokenType; i++)
+ {
+ string literalName = vocabulary.GetLiteralName(i);
+ if (literalName != null)
+ {
+ result.Put(literalName, i);
+ }
+ string symbolicName = vocabulary.GetSymbolicName(i);
+ if (symbolicName != null)
+ {
+ result.Put(symbolicName, i);
+ }
+ }
result.Put("EOF", TokenConstants.Eof);
result = Antlr4.Runtime.Sharpen.Collections.UnmodifiableMap(result);
- tokenTypeMapCache.Put(tokenNames, result);
+ tokenTypeMapCache.Put(vocabulary, result);
}
return result;
}
diff --git a/runtime/CSharp/Antlr4.Runtime/TokenStreamRewriter.cs b/runtime/CSharp/Antlr4.Runtime/TokenStreamRewriter.cs
index a7691b4f4..e1c675fc7 100644
--- a/runtime/CSharp/Antlr4.Runtime/TokenStreamRewriter.cs
+++ b/runtime/CSharp/Antlr4.Runtime/TokenStreamRewriter.cs
@@ -274,7 +274,7 @@ namespace Antlr4.Runtime
///
/// Rollback the instruction stream for a program so that
/// the indicated instruction (via instructionIndex) is no
- /// longer in the stream. UNTESTED!
+ /// longer in the stream. UNTESTED!
///
public virtual void Rollback(string programName, int instructionIndex)
{
@@ -549,8 +549,8 @@ namespace Antlr4.Runtime
///