-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #143 from marcin-golebiowski/master
Include column information in tokens and netlist model, improve exceptions
- Loading branch information
Showing
19 changed files
with
354 additions
and
157 deletions.
There are no files selected for viewing
4 changes: 1 addition & 3 deletions
4
...pParser/Lexers/LexerLineNumberProvider.cs → ...ser.CodeAnalysis/LexerLineInfoProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace SpiceSharpParser.Common | ||
{ | ||
public interface ILocationProvider | ||
{ | ||
/// <summary> | ||
/// Gets or sets token line number. | ||
/// </summary> | ||
int LineNumber { get; } | ||
|
||
/// <summary> | ||
/// Gets or sets start column index. | ||
/// </summary> | ||
int StartColumnIndex { get; } | ||
|
||
/// <summary> | ||
/// Gets or sets end column index. | ||
/// </summary> | ||
int EndColumnIndex { get; } | ||
|
||
/// <summary> | ||
/// Gets token file name. | ||
/// </summary> | ||
string FileName { get; } | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace SpiceSharpParser.Lexers | ||
{ | ||
public class LexerLineInfoProvider | ||
{ | ||
private readonly string _text; | ||
private readonly List<LexerLineRange> _ranges = new List<LexerLineRange>(); | ||
|
||
public LexerLineInfoProvider(string text) | ||
{ | ||
_text = text; | ||
|
||
Init(); | ||
} | ||
|
||
public int GetLineForIndex(int textIndex) | ||
{ | ||
// binary search over line ranges | ||
int start = 0; | ||
int end = _ranges.Count - 1; | ||
|
||
while (start <= end) | ||
{ | ||
int middle = (end + start) / 2; | ||
|
||
if (_ranges[middle].From <= textIndex && textIndex <= _ranges[middle].To) | ||
{ | ||
return _ranges[middle].LineNumber; | ||
} | ||
else if (_ranges[middle].From > textIndex) | ||
{ | ||
end = middle - 1; | ||
} | ||
else if (_ranges[middle].To < textIndex) | ||
{ | ||
start = middle + 1; | ||
} | ||
else | ||
{ | ||
return -1; | ||
} | ||
} | ||
|
||
return -1; | ||
} | ||
|
||
public int GetColumnForIndex(int textIndex) | ||
{ | ||
// binary search over line ranges | ||
int start = 0; | ||
int end = _ranges.Count - 1; | ||
|
||
while (start <= end) | ||
{ | ||
int middle = (end + start) / 2; | ||
|
||
if (_ranges[middle].From <= textIndex && textIndex <= _ranges[middle].To) | ||
{ | ||
return textIndex - _ranges[middle].From; | ||
} | ||
else if (_ranges[middle].From > textIndex) | ||
{ | ||
end = middle - 1; | ||
} | ||
else if (_ranges[middle].To < textIndex) | ||
{ | ||
start = middle + 1; | ||
} | ||
else | ||
{ | ||
return -1; | ||
} | ||
} | ||
|
||
return -1; | ||
} | ||
|
||
private void Init() | ||
{ | ||
int currentIndex = 0; | ||
|
||
var newRange = new LexerLineRange { From = 0, LineNumber = 1 }; | ||
int currentLineNumber = 1; | ||
|
||
while (currentIndex < _text.Length) | ||
{ | ||
if (_text[currentIndex] == '\r') | ||
{ | ||
if ((_text.Length > currentIndex + 1) && _text[currentIndex + 1] == '\n') | ||
{ | ||
newRange.To = currentIndex + 1; | ||
newRange.LineNumber = currentLineNumber; | ||
_ranges.Add(newRange); | ||
|
||
currentLineNumber++; | ||
currentIndex += 2; | ||
|
||
newRange = new LexerLineRange { From = currentIndex, LineNumber = currentLineNumber }; | ||
} | ||
else | ||
{ | ||
newRange.To = currentIndex; | ||
newRange.LineNumber = currentLineNumber; | ||
_ranges.Add(newRange); | ||
|
||
currentLineNumber++; | ||
currentIndex++; | ||
|
||
newRange = new LexerLineRange { From = currentIndex, LineNumber = currentLineNumber }; | ||
} | ||
} | ||
else if (_text[currentIndex] == '\n') | ||
{ | ||
newRange.To = currentIndex; | ||
newRange.LineNumber = currentLineNumber; | ||
_ranges.Add(newRange); | ||
|
||
currentLineNumber++; | ||
currentIndex++; | ||
|
||
newRange = new LexerLineRange { From = currentIndex, LineNumber = currentLineNumber }; | ||
} | ||
else | ||
{ | ||
currentIndex++; | ||
} | ||
} | ||
|
||
newRange.To = _text.Length - 1; | ||
_ranges.Add(newRange); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.