Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 6 additions & 6 deletions src/TextMateSharp/Grammar/Injection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@

namespace TextMateSharp.Grammars
{
public class Injection
internal sealed class Injection
{
public int Priority { get; private set; } // -1 | 0 | 1; // 0 is the default. -1 for 'L' and 1 for 'R'
public RuleId RuleId { get; private set; }
public IRawGrammar Grammar { get; private set; }
internal int Priority { get; private set; } // -1 | 0 | 1; // 0 is the default. -1 for 'L' and 1 for 'R'
internal RuleId RuleId { get; private set; }
internal IRawGrammar Grammar { get; private set; }

private Predicate<List<string>> _matcher;
private readonly Predicate<List<string>> _matcher;

public Injection(Predicate<List<string>> matcher, RuleId ruleId, IRawGrammar grammar, int priority)
internal Injection(Predicate<List<string>> matcher, RuleId ruleId, IRawGrammar grammar, int priority)
{
RuleId = ruleId;
Grammar = grammar;
Expand Down
16 changes: 8 additions & 8 deletions src/TextMateSharp/Grammar/StateStack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ public interface IStateStack
string EndRule { get; }
}

public class StateStack : IStateStack, IEquatable<StateStack>
public sealed class StateStack : IStateStack, IEquatable<StateStack>
{
public static StateStack NULL = new StateStack(
public static readonly StateStack NULL = new StateStack(
null,
RuleId.NO_RULE,
0,
Expand All @@ -28,9 +28,9 @@ public class StateStack : IStateStack, IEquatable<StateStack>
public int Depth { get; private set; }
public RuleId RuleId { get; private set; }
public string EndRule { get; private set; }
public AttributedScopeStack NameScopesList { get; private set; }
public AttributedScopeStack ContentNameScopesList { get; private set; }
public bool BeginRuleCapturedEOL { get; private set; }
internal AttributedScopeStack NameScopesList { get; private set; }
internal AttributedScopeStack ContentNameScopesList { get; private set; }
internal bool BeginRuleCapturedEOL { get; private set; }

private int _enterPos;
private int _anchorPos;
Expand All @@ -40,7 +40,7 @@ public class StateStack : IStateStack, IEquatable<StateStack>
// ContentNameScopesList) are not mutated after construction
private readonly int _hashCode;

public StateStack(
internal StateStack(
StateStack parent,
RuleId ruleId,
int enterPos,
Expand Down Expand Up @@ -249,7 +249,7 @@ public StateStack SafePop()
return this;
}

public StateStack Push(
internal StateStack Push(
RuleId ruleId,
int enterPos,
int anchorPos,
Expand Down Expand Up @@ -316,7 +316,7 @@ public override string ToString()
return builder.ToString();
}

public StateStack WithContentNameScopesList(AttributedScopeStack contentNameScopesList)
internal StateStack WithContentNameScopesList(AttributedScopeStack contentNameScopesList)
{
// Null-safe comparison matching Java upstream's Objects.equals() pattern
if (AttributedScopeStack.Equals(this.ContentNameScopesList, contentNameScopesList))
Expand Down
37 changes: 18 additions & 19 deletions src/TextMateSharp/Internal/Grammars/Grammar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@

namespace TextMateSharp.Internal.Grammars
{
public class Grammar : IGrammar, IRuleFactoryHelper
public sealed class Grammar : IGrammar, IRuleFactoryHelper
{
private string _rootScopeName;
private readonly string _rootScopeName;
private RuleId _rootId;
private int _lastRuleId;
private volatile bool _isCompiling;
private Dictionary<RuleId, Rule> _ruleId2desc;
private Dictionary<string, IRawGrammar> _includedGrammars;
private IGrammarRepository _grammarRepository;
private IRawGrammar _rawGrammar;
private readonly Dictionary<RuleId, Rule> _ruleId2desc;
private readonly Dictionary<string, IRawGrammar> _includedGrammars;
private readonly IGrammarRepository _grammarRepository;
private readonly IRawGrammar _rawGrammar;
private List<Injection> _injections;
private BasicScopeAttributesProvider _basicScopeAttributesProvider;
private List<TokenTypeMatcher> _tokenTypeMatchers;
private BalancedBracketSelectors _balancedBracketSelectors;
private readonly BasicScopeAttributesProvider _basicScopeAttributesProvider;
private readonly List<TokenTypeMatcher> _tokenTypeMatchers;
private readonly BalancedBracketSelectors _balancedBracketSelectors;

public Grammar(
string scopeName,
Expand Down Expand Up @@ -53,14 +53,14 @@ public void OnDidChangeTheme()
this._basicScopeAttributesProvider.OnDidChangeTheme();
}

public BasicScopeAttributes GetMetadataForScope(string scope)
internal BasicScopeAttributes GetMetadataForScope(string scope)
{
return this._basicScopeAttributesProvider.GetBasicScopeAttributes(scope);
}

public bool IsCompiling => _isCompiling;

public List<Injection> GetInjections()
internal List<Injection> GetInjections()
{
if (this._injections == null)
{
Expand Down Expand Up @@ -118,7 +118,7 @@ public List<Injection> GetInjections()
return this._injections;
}

private void CollectInjections(List<Injection> result, string selector, IRawRule rule,
private static void CollectInjections(List<Injection> result, string selector, IRawRule rule,
IRuleFactoryHelper ruleFactoryHelper, IRawGrammar grammar)
{
var matchers = Matcher.Matcher.CreateMatchers(selector);
Expand Down Expand Up @@ -173,7 +173,7 @@ public IRawGrammar GetExternalGrammar(string scopeName, IRawRepository repositor
return null;
}

private IRawGrammar InitGrammar(IRawGrammar grammar, IRawRule ruleBase)
private static IRawGrammar InitGrammar(IRawGrammar grammar, IRawRule ruleBase)
{
grammar = grammar.Clone();
if (grammar.GetRepository() == null)
Expand Down Expand Up @@ -271,7 +271,7 @@ private object Tokenize(ReadOnlyMemory<char> lineText, StateStack prevState, boo
}

int lineLength = effectiveLineText.Length;
LineTokens lineTokens = new LineTokens(emitBinaryTokens, effectiveLineText, _tokenTypeMatchers,
LineTokens lineTokens = new LineTokens(emitBinaryTokens, _tokenTypeMatchers,
_balancedBracketSelectors);
TokenizeStringResult tokenizeResult = LineTokenizer.TokenizeString(this, effectiveLineText, isFirstLine, 0,
prevState,
Expand Down Expand Up @@ -301,13 +301,12 @@ private void GenerateRootId()
}
}

private List<TokenTypeMatcher> GenerateTokenTypeMatchers(Dictionary<string, int> tokenTypes)
private static List<TokenTypeMatcher> GenerateTokenTypeMatchers(Dictionary<string, int> tokenTypes)
{
var result = new List<TokenTypeMatcher>();

if (tokenTypes == null)
return result;
return new List<TokenTypeMatcher>();

var result = new List<TokenTypeMatcher>(tokenTypes.Keys.Count);
foreach (var selector in tokenTypes.Keys)
{
foreach (var matcher in Matcher.Matcher.CreateMatchers(selector))
Expand Down Expand Up @@ -336,7 +335,7 @@ public ICollection<string> GetFileTypes()

class GrammarRepository : IGrammarRepository
{
private Grammar _grammar;
private readonly Grammar _grammar;
internal GrammarRepository(Grammar grammar)
{
_grammar = grammar;
Expand Down
57 changes: 27 additions & 30 deletions src/TextMateSharp/Internal/Grammars/LineTokenizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace TextMateSharp.Internal.Grammars
{
class LineTokenizer
internal sealed class LineTokenizer
{
private readonly Grammar _grammar;
private readonly ReadOnlyMemory<char> _lineText;
Expand All @@ -24,7 +24,7 @@ class LineTokenizer
private readonly List<LocalStackElement> _localStackBuffer = new List<LocalStackElement>();
private readonly List<WhileStack> _whileRulesBuffer = new List<WhileStack>();

public LineTokenizer(Grammar grammar, ReadOnlyMemory<char> lineText, bool isFirstLine, int linePos, StateStack stack,
private LineTokenizer(Grammar grammar, ReadOnlyMemory<char> lineText, bool isFirstLine, int linePos, StateStack stack,
LineTokens lineTokens)
{
this._grammar = grammar;
Expand All @@ -36,7 +36,7 @@ public LineTokenizer(Grammar grammar, ReadOnlyMemory<char> lineText, bool isFirs
this._lineTokens = lineTokens;
}

public TokenizeStringResult Scan(bool checkWhileConditions, TimeSpan timeLimit)
private TokenizeStringResult Scan(bool checkWhileConditions, TimeSpan timeLimit)
{
_stop = false;

Expand Down Expand Up @@ -138,22 +138,20 @@ private void ScanNext()
nameScopesList,
nameScopesList);

if (rule is BeginEndRule)
if (rule is BeginEndRule beginEndRule)
{
BeginEndRule pushedRule = (BeginEndRule)rule;

HandleCaptures(
_grammar,
_lineText,
_isFirstLine,
_stack,
_lineTokens,
pushedRule.BeginCaptures,
beginEndRule.BeginCaptures,
captureIndices);
_lineTokens.Produce(_stack, captureIndices[0].End);
_anchorPosition = captureIndices[0].End;

string contentName = pushedRule.GetContentName(
string contentName = beginEndRule.GetContentName(
_lineText,
captureIndices);

Expand All @@ -163,10 +161,10 @@ private void ScanNext()

_stack = _stack.WithContentNameScopesList(contentNameScopesList);

if (pushedRule.EndHasBackReferences)
if (beginEndRule.EndHasBackReferences)
{
_stack = _stack.WithEndRule(
pushedRule.GetEndWithResolvedBackReferences(
beginEndRule.GetEndWithResolvedBackReferences(
_lineText,
captureIndices));
}
Expand All @@ -181,9 +179,8 @@ private void ScanNext()
return;
}
}
else if (rule is BeginWhileRule)
else if (rule is BeginWhileRule beginWhileRule)
{
BeginWhileRule pushedRule = (BeginWhileRule)rule;
// if (IN_DEBUG_MODE) {
// console.log(' pushing ' + pushedRule.debugName);
// }
Expand All @@ -194,19 +191,19 @@ private void ScanNext()
_isFirstLine,
_stack,
_lineTokens,
pushedRule.BeginCaptures,
beginWhileRule.BeginCaptures,
captureIndices);
_lineTokens.Produce(_stack, captureIndices[0].End);
_anchorPosition = captureIndices[0].End;

string contentName = pushedRule.GetContentName(_lineText, captureIndices);
string contentName = beginWhileRule.GetContentName(_lineText, captureIndices);
AttributedScopeStack contentNameScopesList = nameScopesList.PushAttributed(contentName, _grammar);
_stack = _stack.WithContentNameScopesList(contentNameScopesList);

if (pushedRule.WhileHasBackReferences)
if (beginWhileRule.WhileHasBackReferences)
{
_stack = _stack.WithEndRule(
pushedRule.getWhileWithResolvedBackReferences(_lineText, captureIndices));
beginWhileRule.getWhileWithResolvedBackReferences(_lineText, captureIndices));
}

if (!hasAdvanced && beforePush.HasSameRuleAs(_stack))
Expand Down Expand Up @@ -254,7 +251,7 @@ private void ScanNext()
}
}

private MatchResult MatchRule(Grammar grammar, ReadOnlyMemory<char> lineText, in bool isFirstLine, in int linePos,
private static MatchResult MatchRule(Grammar grammar, ReadOnlyMemory<char> lineText, in bool isFirstLine, in int linePos,
StateStack stack, in int anchorPosition)
{
Rule rule = stack.GetRule(grammar);
Expand Down Expand Up @@ -499,9 +496,9 @@ private WhileCheckResult CheckWhileConditions(Grammar grammar, ReadOnlyMemory<ch
for (StateStack node = stack; node != null; node = node.Pop())
{
Rule nodeRule = node.GetRule(grammar);
if (nodeRule is BeginWhileRule)
if (nodeRule is BeginWhileRule beginWhileRule)
{
whileRules.Add(new WhileStack(node, (BeginWhileRule)nodeRule));
whileRules.Add(new WhileStack(node, beginWhileRule));
}
}
for (int i = whileRules.Count - 1; i >= 0; i--)
Expand Down Expand Up @@ -544,32 +541,32 @@ private WhileCheckResult CheckWhileConditions(Grammar grammar, ReadOnlyMemory<ch
return new WhileCheckResult(stack, linePos, anchorPosition, isFirstLine);
}

public static TokenizeStringResult TokenizeString(Grammar grammar, ReadOnlyMemory<char> lineText, bool isFirstLine, int linePos,
internal static TokenizeStringResult TokenizeString(Grammar grammar, ReadOnlyMemory<char> lineText, bool isFirstLine, int linePos,
StateStack stack, LineTokens lineTokens, bool checkWhileConditions, TimeSpan timeLimit)
{
return new LineTokenizer(grammar, lineText, isFirstLine, linePos, stack, lineTokens).Scan(checkWhileConditions, timeLimit);
}

class WhileStack
sealed class WhileStack
{
public StateStack Stack { get; private set; }
public BeginWhileRule Rule { get; private set; }
internal StateStack Stack { get; private set; }
internal BeginWhileRule Rule { get; private set; }

public WhileStack(StateStack stack, BeginWhileRule rule)
internal WhileStack(StateStack stack, BeginWhileRule rule)
{
Stack = stack;
Rule = rule;
}
}

class WhileCheckResult
sealed class WhileCheckResult
{
public StateStack Stack { get; private set; }
public int LinePos { get; private set; }
public int AnchorPosition { get; private set; }
public bool IsFirstLine { get; private set; }
internal StateStack Stack { get; private set; }
internal int LinePos { get; private set; }
internal int AnchorPosition { get; private set; }
internal bool IsFirstLine { get; private set; }

public WhileCheckResult(StateStack stack, int linePos, int anchorPosition, bool isFirstLine)
internal WhileCheckResult(StateStack stack, int linePos, int anchorPosition, bool isFirstLine)
{
Stack = stack;
LinePos = linePos;
Expand Down
Loading
Loading