Skip to content

Commit

Permalink
Fixed portability problems in C# target. Also cleaned up some XML doc…
Browse files Browse the repository at this point in the history
… comments
  • Loading branch information
RobertvanderHulst committed Mar 15, 2017
1 parent 1705498 commit ef49021
Show file tree
Hide file tree
Showing 16 changed files with 125 additions and 119 deletions.
3 changes: 2 additions & 1 deletion contributors.txt
Expand Up @@ -140,4 +140,5 @@ YYYY/MM/DD, github id, Full name, email
2017/02/26, jvasileff, John Vasileff, john@vasileff.com
2017/03/08, harry-tallbelt, Igor Vysokopoyasny, harry.tallbelt@gmail.com
2017/03/09, teverett, Tom Everett, tom@khubla.com
2017/03/03, chund, Christian Hund, christian.hund@gmail.com
2017/03/03, chund, Christian Hund, christian.hund@gmail.com
2017/03/15, robertvanderhulst, Robert van der Hulst, robert@xsharp.eu
Expand Up @@ -287,7 +287,7 @@ protected override string ConvertDataToString(int start, int count)

/// <summary>
/// Alternative to
/// <see cref="ANTLRInputStream"/>
/// <see cref="AntlrInputStream"/>
/// which treats the input as a series of Unicode code points,
/// instead of a series of UTF-16 code units.
///
Expand Down
2 changes: 1 addition & 1 deletion runtime/CSharp/runtime/CSharp/Antlr4.Runtime/Atn/ATN.cs
Expand Up @@ -104,7 +104,7 @@ public virtual PredictionContext GetCachedContext(PredictionContext context)
/// If
/// <paramref name="ctx"/>
/// is
/// <see cref="PredictionContext.EmptyLocal"/>
/// <see cref="PredictionContext.EMPTY"/>
/// , the set of tokens will not include what can follow
/// the rule surrounding
/// <paramref name="s"/>
Expand Down
Expand Up @@ -583,7 +583,7 @@ protected internal virtual void Reset(char[] data)
/// <see cref="StarLoopEntryState"/>
/// states in the specified ATN to set
/// the
/// <see cref="StarLoopEntryState.precedenceRuleDecision"/>
/// <see cref="StarLoopEntryState.isPrecedenceDecision"/>
/// field to the
/// correct value.
/// </summary>
Expand Down
Expand Up @@ -75,7 +75,14 @@ public virtual void ClearDFA()
throw new Exception("This ATN simulator does not support clearing the DFA.");
}

public PredictionContextCache getSharedContextCache()
protected void ConsoleWriteLine(string format, params object[] arg)
{
#if !PORTABLE
System.Console.WriteLine(format, arg);
#endif
}

public PredictionContextCache getSharedContextCache()
{
return sharedContextCache;
}
Expand Down
Expand Up @@ -20,7 +20,7 @@ namespace Antlr4.Runtime.Atn
/// determine that the SLL conflict is truly an ambiguity. For example, if none
/// of the ATN configurations in the conflicting SLL configuration set have
/// traversed a global follow transition (i.e.
/// <see cref="ATNConfig.ReachesIntoOuterContext()"/>
/// <see cref="ATNConfig.reachesIntoOuterContext"/>
/// is
/// <see langword="false"/>
/// for all
Expand Down
Expand Up @@ -23,7 +23,7 @@ namespace Antlr4.Runtime.Atn
/// the two-stage parsing strategy to improve parsing performance for that
/// input.</p>
/// </remarks>
/// <seealso cref="ParserATNSimulator.ReportContextSensitivity(Antlr4.Runtime.Dfa.DFA, int, SimulatorState, int, int)"/>
/// <seealso cref="ParserATNSimulator.ReportContextSensitivity(Dfa.DFA, int, ATNConfigSet, int, int)"/>
/// <seealso cref="Antlr4.Runtime.IParserErrorListener.ReportContextSensitivity(Antlr4.Runtime.Parser, Antlr4.Runtime.Dfa.DFA, int, int, int, SimulatorState)"/>
/// <since>4.3</since>
public class ContextSensitivityInfo : DecisionEventInfo
Expand Down
Expand Up @@ -27,8 +27,8 @@ public EpsilonTransition(ATNState target, int outermostPrecedenceReturn)
/// the rule index of a precedence rule for which this transition is
/// returning from, where the precedence value is 0; otherwise, -1.
/// </returns>
/// <seealso cref="ATNConfig.PrecedenceFilterSuppressed()"/>
/// <seealso cref="ParserATNSimulator.ApplyPrecedenceFilter(ATNConfigSet, Antlr4.Runtime.ParserRuleContext, PredictionContextCache)"/>
/// <seealso cref="ATNConfig.IsPrecedenceFilterSuppressed"/>
/// <seealso cref="ParserATNSimulator.ApplyPrecedenceFilter(ATNConfigSet)"/>
/// <since>4.4.1</since>
public int OutermostPrecedenceReturn
{
Expand Down
Expand Up @@ -33,7 +33,7 @@ public class ErrorInfo : DecisionEventInfo
/// <param name="state">
/// The final simulator state reached during prediction
/// prior to reaching the
/// <see cref="ATNSimulator.Error"/>
/// <see cref="ATNSimulator.ERROR"/>
/// state
/// </param>
/// <param name="input">The input token stream</param>
Expand Down
Expand Up @@ -46,7 +46,7 @@ LexerActionType ActionType
/// , do not check the input index during their execution.
/// Actions like this are position-independent, and may be stored more
/// efficiently as part of the
/// <see cref="ATNConfig.ActionExecutor()"/>
/// <see cref="LexerATNConfig.lexerActionExecutor"/>
/// .</p>
/// </remarks>
/// <returns>
Expand Down
Expand Up @@ -184,7 +184,7 @@ public virtual IntervalSet Look(ATNState s, ATNState stopState, RuleContext ctx)
/// If
/// <paramref name="ctx"/>
/// is
/// <see cref="PredictionContext.EmptyLocal"/>
/// <see cref="PredictionContext.EMPTY"/>
/// and
/// <paramref name="stopState"/>
/// or the end of the rule containing
Expand All @@ -194,7 +194,7 @@ public virtual IntervalSet Look(ATNState s, ATNState stopState, RuleContext ctx)
/// is added to the result set. If
/// <paramref name="ctx"/>
/// is not
/// <see cref="PredictionContext.EmptyLocal"/>
/// <see cref="PredictionContext.EMPTY"/>
/// and
/// <paramref name="addEOF"/>
/// is
Expand All @@ -213,7 +213,7 @@ public virtual IntervalSet Look(ATNState s, ATNState stopState, RuleContext ctx)
/// </param>
/// <param name="ctx">
/// The outer context, or
/// <see cref="PredictionContext.EmptyLocal"/>
/// <see cref="PredictionContext.EMPTY"/>
/// if
/// the outer context should not be used.
/// </param>
Expand Down Expand Up @@ -250,7 +250,7 @@ public virtual IntervalSet Look(ATNState s, ATNState stopState, RuleContext ctx)
/// outermost context is reached. This parameter has no effect if
/// <paramref name="ctx"/>
/// is
/// <see cref="PredictionContext.EmptyLocal"/>
/// <see cref="PredictionContext.EMPTY"/>
/// .
/// </param>
protected internal virtual void Look(ATNState s, ATNState stopState, PredictionContext ctx, IntervalSet look, HashSet<ATNConfig> lookBusy, BitSet calledRuleStack, bool seeThruPreds, bool addEOF)
Expand Down
Expand Up @@ -12,11 +12,9 @@ namespace Antlr4.Runtime.Atn
/// <summary>"dup" of ParserInterpreter</summary>
public class LexerATNSimulator : ATNSimulator
{
#if !PORTABLE
public readonly bool debug = false;

public readonly bool dfa_debug = false;
#endif


public static readonly int MIN_DFA_EDGE = 0;
Expand Down Expand Up @@ -64,7 +62,9 @@ public class LexerATNSimulator : ATNSimulator
this.recog = recog;
}

public void CopyState(LexerATNSimulator simulator)


public void CopyState(LexerATNSimulator simulator)
{
this.charPositionInLine = simulator.charPositionInLine;
this.thisLine = simulator.thisLine;
Expand Down Expand Up @@ -117,13 +117,11 @@ public override void ClearDFA()
protected int MatchATN(ICharStream input)
{
ATNState startState = atn.modeToStartState[mode];

if (debug)
if (debug)
{
Console.WriteLine("matchATN mode " + mode + " start: " + startState);
ConsoleWriteLine("matchATN mode " + mode + " start: " + startState);
}

int old_mode = mode;
int old_mode = mode;

ATNConfigSet s0_closure = ComputeStartState(input, startState);
bool suppressEdge = s0_closure.hasSemanticContext;
Expand All @@ -136,24 +134,21 @@ protected int MatchATN(ICharStream input)
}

int predict = ExecATN(input, next);

if (debug)
if (debug)
{
Console.WriteLine("DFA after matchATN: " + decisionToDFA[old_mode].ToString());
ConsoleWriteLine("DFA after matchATN: " + decisionToDFA[old_mode].ToString());
}

return predict;
return predict;
}

protected int ExecATN(ICharStream input, DFAState ds0)
{
//System.out.println("enter exec index "+input.index()+" from "+ds0.configs);
if (debug)
{
Console.WriteLine("start state closure=" + ds0.configSet);
//System.out.println("enter exec index "+input.index()+" from "+ds0.configs);
if (debug)
{
ConsoleWriteLine("start state closure=" + ds0.configSet);
}

if (ds0.isAcceptState)
if (ds0.isAcceptState)
{
// allow zero-length tokens
CaptureSimState(prevAccept, input, ds0);
Expand All @@ -165,29 +160,28 @@ protected int ExecATN(ICharStream input, DFAState ds0)

while (true)
{ // while more work
if (debug)
{
Console.WriteLine("execATN loop starting closure: " + s.configSet);
if (debug)
{
ConsoleWriteLine("execATN loop starting closure: " + s.configSet);
}

// As we move src->trg, src->trg, we keep track of the previous trg to
// avoid looking up the DFA state again, which is expensive.
// If the previous target was already part of the DFA, we might
// be able to avoid doing a reach operation upon t. If s!=null,
// it means that semantic predicates didn't prevent us from
// creating a DFA state. Once we know s!=null, we check to see if
// the DFA state has an edge already for t. If so, we can just reuse
// it's configuration set; there's no point in re-computing it.
// This is kind of like doing DFA simulation within the ATN
// simulation because DFA simulation is really just a way to avoid
// computing reach/closure sets. Technically, once we know that
// we have a previously added DFA state, we could jump over to
// the DFA simulator. But, that would mean popping back and forth
// a lot and making things more complicated algorithmically.
// This optimization makes a lot of sense for loops within DFA.
// A character will take us back to an existing DFA state
// that already has lots of edges out of it. e.g., .* in comments.
DFAState target = GetExistingTargetState(s, t);
// As we move src->trg, src->trg, we keep track of the previous trg to
// avoid looking up the DFA state again, which is expensive.
// If the previous target was already part of the DFA, we might
// be able to avoid doing a reach operation upon t. If s!=null,
// it means that semantic predicates didn't prevent us from
// creating a DFA state. Once we know s!=null, we check to see if
// the DFA state has an edge already for t. If so, we can just reuse
// it's configuration set; there's no point in re-computing it.
// This is kind of like doing DFA simulation within the ATN
// simulation because DFA simulation is really just a way to avoid
// computing reach/closure sets. Technically, once we know that
// we have a previously added DFA state, we could jump over to
// the DFA simulator. But, that would mean popping back and forth
// a lot and making things more complicated algorithmically.
// This optimization makes a lot of sense for loops within DFA.
// A character will take us back to an existing DFA state
// that already has lots of edges out of it. e.g., .* in comments.
DFAState target = GetExistingTargetState(s, t);
if (target == null)
{
target = ComputeTargetState(input, s, t);
Expand Down Expand Up @@ -245,7 +239,7 @@ protected DFAState GetExistingTargetState(DFAState s, int t)
DFAState target = s.edges[t - MIN_DFA_EDGE];
if (debug && target != null)
{
Console.WriteLine("reuse state " + s.stateNumber + " edge to " + target.stateNumber);
ConsoleWriteLine("reuse state " + s.stateNumber + " edge to " + target.stateNumber);
}

return target;
Expand Down Expand Up @@ -329,7 +323,7 @@ protected void GetReachableConfigSet(ICharStream input, ATNConfigSet closure, AT

if (debug)
{
Console.WriteLine("testing " + GetTokenName(t) + " at " + c.ToString(recog, true));
ConsoleWriteLine("testing " + GetTokenName(t) + " at " + c.ToString(recog, true));
}

int n = c.state.NumberOfTransitions;
Expand Down Expand Up @@ -363,7 +357,7 @@ protected void GetReachableConfigSet(ICharStream input, ATNConfigSet closure, AT
{
if (debug)
{
Console.WriteLine("ACTION " + lexerActionExecutor);
ConsoleWriteLine("ACTION " + lexerActionExecutor);
}

// seek to after last char in token
Expand Down Expand Up @@ -417,7 +411,7 @@ protected bool Closure(ICharStream input, LexerATNConfig config, ATNConfigSet co
{
if (debug)
{
Console.WriteLine("closure(" + config.ToString(recog, true) + ")");
ConsoleWriteLine("closure(" + config.ToString(recog, true) + ")");
}

if (config.state is RuleStopState)
Expand All @@ -426,10 +420,10 @@ protected bool Closure(ICharStream input, LexerATNConfig config, ATNConfigSet co
{
if (recog != null)
{
Console.WriteLine("closure at " + recog.RuleNames[config.state.ruleIndex] + " rule stop " + config);
ConsoleWriteLine("closure at " + recog.RuleNames[config.state.ruleIndex] + " rule stop " + config);
}
else {
Console.WriteLine("closure at rule stop " + config);
ConsoleWriteLine("closure at rule stop " + config);
}
}

Expand Down Expand Up @@ -529,7 +523,7 @@ protected bool Closure(ICharStream input, LexerATNConfig config, ATNConfigSet co
PredicateTransition pt = (PredicateTransition)t;
if (debug)
{
Console.WriteLine("EVAL rule " + pt.ruleIndex + ":" + pt.predIndex);
ConsoleWriteLine("EVAL rule " + pt.ruleIndex + ":" + pt.predIndex);
}
configs.hasSemanticContext = true;
if (EvaluatePredicate(input, pt.ruleIndex, pt.predIndex, speculative))
Expand Down Expand Up @@ -688,7 +682,7 @@ protected void AddDFAEdge(DFAState p, int t, DFAState q)

if (debug)
{
Console.WriteLine("EDGE " + p + " -> " + q + " upon " + ((char)t));
ConsoleWriteLine("EDGE " + p + " -> " + q + " upon " + ((char)t));
}

lock (p)
Expand Down

0 comments on commit ef49021

Please sign in to comment.