Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Internal] Engineering: Refactors code to reduce technical debt #2470

Merged
merged 6 commits into from
May 17, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Microsoft.Azure.Cosmos/src/OSS/Antlr/Atn/LL1Analyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -314,9 +314,8 @@ protected internal virtual void Look(ATNState s, ATNState stopState, PredictionC
for (int i_1 = 0; i_1 < n; i_1++)
{
Transition t = s.Transition(i_1);
if (t is RuleTransition)
if (t is RuleTransition ruleTransition)
{
RuleTransition ruleTransition = (RuleTransition)t;
if (calledRuleStack.Get(ruleTransition.ruleIndex))
{
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,9 @@ public virtual void Execute(Lexer lexer, ICharStream input, int startIndex)
foreach (ILexerAction lexerAction in lexerActions)
{
ILexerAction action = lexerAction;
if (action is LexerIndexedCustomAction)
if (action is LexerIndexedCustomAction lexerIndexedCustomAction)
{
int offset = ((LexerIndexedCustomAction)action).Offset;
int offset = lexerIndexedCustomAction.Offset;
input.Seek(startIndex + offset);
action = ((LexerIndexedCustomAction)action).Action;
requiresSeek = (startIndex + offset) != stopIndex;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2124,14 +2124,12 @@ public void DumpDeadEndConfigs(NoViableAltException nvae)
if (c.state.NumberOfTransitions > 0)
{
Transition t = c.state.Transition(0);
if (t is AtomTransition)
if (t is AtomTransition at)
{
AtomTransition at = (AtomTransition)t;
trans = "Atom " + GetTokenName(at.token);
}
else if (t is SetTransition)
else if (t is SetTransition st)
{
SetTransition st = (SetTransition)t;
bool not = st is NotSetTransition;
trans = (not ? "~" : "") + "Set " + st.set.ToString();
}
Expand Down
4 changes: 2 additions & 2 deletions Microsoft.Azure.Cosmos/src/OSS/Antlr/BufferedTokenStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,9 @@ protected internal virtual int Fetch(int n)
for (int i = 0; i < n; i++)
{
IToken t = _tokenSource.NextToken();
if (t is IWritableToken)
if (t is IWritableToken iWritableToken)
{
((IWritableToken)t).TokenIndex = tokens.Count;
iWritableToken.TokenIndex = tokens.Count;
}
tokens.Add(t);
if (t.Type == TokenConstants.EOF)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ public FailedPredicateException(Parser recognizer, string predicate, string mess
{
ATNState s = recognizer.Interpreter.atn.states[recognizer.State];
AbstractPredicateTransition trans = (AbstractPredicateTransition)s.Transition(0);
if (trans is PredicateTransition)
if (trans is PredicateTransition predicateTransition)
{
this.ruleIndex = ((PredicateTransition)trans).ruleIndex;
this.ruleIndex = predicateTransition.ruleIndex;
this.predicateIndex = ((PredicateTransition)trans).predIndex;
}
else
Expand Down
3 changes: 1 addition & 2 deletions Microsoft.Azure.Cosmos/src/OSS/Antlr/Misc/IntervalSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,8 @@ public virtual Antlr4.Runtime.Misc.IntervalSet AddAll(IIntSet set)
{
return this;
}
if (set is Antlr4.Runtime.Misc.IntervalSet)
if (set is Antlr4.Runtime.Misc.IntervalSet other)
{
Antlr4.Runtime.Misc.IntervalSet other = (Antlr4.Runtime.Misc.IntervalSet)set;
// walk set and add each interval
int n = other.intervals.Count;
for (int i = 0; i < n; i++)
Expand Down
3 changes: 1 addition & 2 deletions Microsoft.Azure.Cosmos/src/OSS/Antlr/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -604,9 +604,8 @@ public virtual ParseTreePattern CompileParseTreePattern(string pattern, int patt
if (((ITokenStream)InputStream) != null)
{
ITokenSource tokenSource = ((ITokenStream)InputStream).TokenSource;
if (tokenSource is Lexer)
if (tokenSource is Lexer lexer)
{
Lexer lexer = (Lexer)tokenSource;
return CompileParseTreePattern(pattern, patternRuleIndex, lexer);
}
}
Expand Down
6 changes: 2 additions & 4 deletions Microsoft.Azure.Cosmos/src/OSS/Antlr/ParserRuleContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,8 @@ public virtual ITerminalNode GetToken(int ttype, int i)
// what token with ttype have we found?
foreach (IParseTree o in children)
{
if (o is ITerminalNode)
if (o is ITerminalNode tnode)
{
ITerminalNode tnode = (ITerminalNode)o;
IToken symbol = tnode.Symbol;
if (symbol.Type == ttype)
{
Expand Down Expand Up @@ -295,9 +294,8 @@ public virtual ITerminalNode[] GetTokens(int ttype)
List<ITerminalNode> tokens = null;
foreach (IParseTree o in children)
{
if (o is ITerminalNode)
if (o is ITerminalNode tnode)
{
ITerminalNode tnode = (ITerminalNode)o;
IToken symbol = tnode.Symbol;
if (symbol.Type == ttype)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -503,9 +503,8 @@ protected internal virtual IParseTree MatchImpl(IParseTree tree, IParseTree patt
/// </summary>
protected internal virtual RuleTagToken GetRuleTagToken(IParseTree t)
{
if (t is IRuleNode)
if (t is IRuleNode r)
{
IRuleNode r = (IRuleNode)t;
if (r.ChildCount == 1 && r.GetChild(0) is ITerminalNode)
{
ITerminalNode c = (ITerminalNode)r.GetChild(0);
Expand All @@ -527,9 +526,8 @@ public virtual IList<IToken> Tokenize(string pattern)
IList<IToken> tokens = new List<IToken>();
foreach (Chunk chunk in chunks)
{
if (chunk is TagChunk)
if (chunk is TagChunk tagChunk)
{
TagChunk tagChunk = (TagChunk)chunk;
// add special rule token or conjure up new token from name
if (System.Char.IsUpper(tagChunk.Tag[0]))
{
Expand Down Expand Up @@ -690,9 +688,8 @@ internal virtual IList<Chunk> Split(string pattern)
for (int i_2 = 0; i_2 < chunks.Count; i_2++)
{
Chunk c = chunks[i_2];
if (c is TextChunk)
if (c is TextChunk tc)
{
TextChunk tc = (TextChunk)c;
string unescaped = tc.Text.Replace(escape, string.Empty);
if (unescaped.Length < tc.Text.Length)
{
Expand Down
12 changes: 6 additions & 6 deletions Microsoft.Azure.Cosmos/src/OSS/Antlr/Tree/Trees.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ public static string GetNodeText(ITree t, IList<string> ruleNames)
{
if (ruleNames != null)
{
if (t is RuleContext)
if (t is RuleContext ruleContext)
{
int ruleIndex = ((RuleContext)t).RuleIndex;
int ruleIndex = ruleContext.RuleIndex;
string ruleName = ruleNames[ruleIndex];
int altNumber = ((RuleContext)t).getAltNumber();
if ( altNumber!=Atn.ATN.INVALID_ALT_NUMBER ) {
Expand All @@ -105,9 +105,9 @@ public static string GetNodeText(ITree t, IList<string> ruleNames)
}
else
{
if (t is ITerminalNode)
if (t is ITerminalNode iTerminalNode)
{
IToken symbol = ((ITerminalNode)t).Symbol;
IToken symbol = iTerminalNode.Symbol;
if (symbol != null)
{
string s = symbol.Text;
Expand All @@ -119,9 +119,9 @@ public static string GetNodeText(ITree t, IList<string> ruleNames)
}
// no recog for rule names
object payload = t.Payload;
if (payload is IToken)
if (payload is IToken iToken)
{
return ((IToken)payload).Text;
return iToken.Text;
}
return t.Payload.ToString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ public override ICollection<IParseTree> Evaluate(IParseTree t)
IList<IParseTree> nodes = new List<IParseTree>();
foreach (ITree c in Trees.GetChildren(t))
{
if (c is ParserRuleContext)
if (c is ParserRuleContext ctx)
{
ParserRuleContext ctx = (ParserRuleContext)c;
if ((ctx.RuleIndex == ruleIndex && !invert) || (ctx.RuleIndex != ruleIndex && invert))
{
nodes.Add(ctx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ public override ICollection<IParseTree> Evaluate(IParseTree t)
IList<IParseTree> nodes = new List<IParseTree>();
foreach (ITree c in Trees.GetChildren(t))
{
if (c is ITerminalNode)
if (c is ITerminalNode tnode)
{
ITerminalNode tnode = (ITerminalNode)c;
if ((tnode.Symbol.Type == tokenType && !invert) || (tnode.Symbol.Type != tokenType && invert))
{
nodes.Add(tnode);
Expand Down
4 changes: 2 additions & 2 deletions Microsoft.Azure.Cosmos/src/OSS/Antlr/UnbufferedTokenStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,9 @@ protected internal virtual void Add(IToken t)
{
tokens = Arrays.CopyOf(tokens, tokens.Length * 2);
}
if (t is IWritableToken)
if (t is IWritableToken iWritableToken)
{
((IWritableToken)t).TokenIndex = GetBufferStartIndex() + n;
iWritableToken.TokenIndex = GetBufferStartIndex() + n;
}
tokens[n++] = t;
}
Expand Down
3 changes: 1 addition & 2 deletions Microsoft.Azure.Cosmos/src/ResourceThrottleRetryPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,8 @@ public Task<ShouldRetryResult> ShouldRetryAsync(
Exception exception,
CancellationToken cancellationToken)
{
if (exception is DocumentClientException)
if (exception is DocumentClientException dce)
ealsur marked this conversation as resolved.
Show resolved Hide resolved
{
DocumentClientException dce = (DocumentClientException)exception;
if (!this.IsValidThrottleStatusCode(dce.StatusCode))
{
DefaultTrace.TraceError(
Expand Down