1+ #region BSD 3-Clause License
2+ // <copyright file="HighlightingTokenCache.cs" company="Edgerunner.org">
3+ // Copyright 2020
4+ // </copyright>
5+ //
6+ // BSD 3-Clause License
7+ //
8+ // Copyright (c) 2020,
9+ // All rights reserved.
10+ //
11+ // Redistribution and use in source and binary forms, with or without
12+ // modification, are permitted provided that the following conditions are met:
13+ //
14+ // 1. Redistributions of source code must retain the above copyright notice, this
15+ // list of conditions and the following disclaimer.
16+ //
17+ // 2. Redistributions in binary form must reproduce the above copyright notice,
18+ // this list of conditions and the following disclaimer in the documentation
19+ // and/or other materials provided with the distribution.
20+ //
21+ // 3. Neither the name of the copyright holder nor the names of its
22+ // contributors may be used to endorse or promote products derived from
23+ // this software without specific prior written permission.
24+ //
25+ // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26+ // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27+ // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28+ // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
29+ // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30+ // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31+ // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32+ // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
33+ // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34+ // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35+ #endregion
36+
37+ using System . Collections . Generic ;
38+
39+ using Org . Edgerunner . ANTLR4 . Tools . Common . Grammar ;
40+
41+ namespace Org . Edgerunner . ANTLR4 . Tools . Testing . GrunDotNet
42+ {
43+ public class HighlightingTokenCache
44+ {
45+ private readonly Dictionary < int , Dictionary < int , int > > _KnownTokens ;
46+
47+ public HighlightingTokenCache ( )
48+ {
49+ _KnownTokens = new Dictionary < int , Dictionary < int , int > > ( ) ;
50+ }
51+
52+ public bool IsKnown ( SyntaxToken token )
53+ {
54+ if ( _KnownTokens . TryGetValue ( token . LineNumber , out var lineCache ) )
55+ if ( lineCache . TryGetValue ( token . ColumnPosition , out var hash ) )
56+ if ( hash == token . GetHashCode ( ) )
57+ return true ;
58+
59+ return false ;
60+ }
61+
62+ public void RegisterToken ( SyntaxToken token )
63+ {
64+ if ( _KnownTokens . TryGetValue ( token . LineNumber , out var lineCache ) )
65+ lineCache [ token . ColumnPosition ] = token . GetHashCode ( ) ;
66+ else
67+ _KnownTokens [ token . LineNumber ] = new Dictionary < int , int > { { token . ColumnPosition , token . GetHashCode ( ) } } ;
68+ }
69+
70+ public void FlushTokensForLine ( int lineNumber )
71+ {
72+ if ( _KnownTokens . TryGetValue ( lineNumber , out var lineCache ) )
73+ lineCache . Clear ( ) ;
74+ }
75+ }
76+ }
0 commit comments