Skip to content

Commit

Permalink
Lint and formatting fix, more docs
Browse files Browse the repository at this point in the history
  • Loading branch information
SaphireLattice committed Nov 17, 2024
1 parent cec85d2 commit 07472cf
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
11 changes: 5 additions & 6 deletions DMCompiler/Compiler/DM/AST/DMAST.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@ public override string ToString() {
return $"{ToString(null)}";
}

public string ToString(Location? loc, bool hideLoc = false) {
if (hideLoc || (loc is not null && Location.SourceFile == loc.Value.SourceFile && Location.Line == loc.Value.Line))
return $"{ToStringNoLocation()}";
public string ToString(Location? loc) {
if (loc is not null && Location.SourceFile == loc.Value.SourceFile && Location.Line == loc.Value.Line)
return ToStringNoLocation();
return $"{ToStringNoLocation()} [{Location}]";
}

public virtual string ToStringNoLocation()
{
return $"{GetType().Name}";
public virtual string ToStringNoLocation() {
return GetType().Name;
}
}

Expand Down
32 changes: 26 additions & 6 deletions DMCompiler/Compiler/Lexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,31 @@
namespace DMCompiler.Compiler;

internal class Lexer<TSourceType> {
/// <summary>
/// Location of token that'll be output by <see cref="GetCurrent"/>. If you skip through more
/// </summary>
public Location CurrentLocation { get; protected set; }
public Location PreviousLocation { get; protected set; }
public string SourceName { get; protected set; }
public IEnumerable<TSourceType> Source { get; protected set; }
public bool AtEndOfSource { get; protected set; } = false;
/// <summary>

Check warning

Code scanning / InspectCode

Incorrect blank lines: Blank lines are missing elsewhere Warning

Blank lines are missing, expected minimum 1 instead of 0
/// Location of a previous token.
/// </summary>
public Location PreviousLocation { get; private set; }
public IEnumerable<TSourceType> Source { get; private set; }

Check warning

Code scanning / InspectCode

Incorrect blank lines: Blank lines are missing elsewhere Warning

Blank lines are missing, expected minimum 1 instead of 0

Check notice

Code scanning / InspectCode

Auto-property can be made get-only: Private accessibility Note

Auto-property can be made get-only
public bool AtEndOfSource { get; private set; }

protected Queue<Token> _pendingTokenQueue = new();

private readonly IEnumerator<TSourceType> _sourceEnumerator;
private TSourceType _current;

/// <summary>
/// Given a stream of some type, allows to advance through it and create <see cref="Token"/> tokens
/// </summary>
/// <param name="sourceName">Used to build the initial Location, access through <see cref="CurrentLocation"/></param>
/// <param name="source">Source of <see cref="TSourceType"/> input</param>
/// <exception cref="FileNotFoundException">Thrown if <paramref name="source"/> is null</exception>
protected Lexer(string sourceName, IEnumerable<TSourceType> source) {

Check warning on line 28 in DMCompiler/Compiler/Lexer.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Non-nullable field '_current' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
CurrentLocation = new Location(sourceName, 1, 0);
PreviousLocation = CurrentLocation;
SourceName = sourceName;
Source = source;
if (source == null)
throw new FileNotFoundException("Source file could not be read: " + sourceName);
Expand All @@ -28,7 +38,7 @@ public Token GetNextToken() {
if (_pendingTokenQueue.Count > 0)
return _pendingTokenQueue.Dequeue();

Token nextToken = ParseNextToken();
var nextToken = ParseNextToken();
while (nextToken.Type == TokenType.Skip) nextToken = ParseNextToken();

if (_pendingTokenQueue.Count > 0) {
Expand All @@ -48,10 +58,19 @@ protected Token CreateToken(TokenType type, string text, Location location, obje
return token;
}

/// <summary>
/// Creates a new <see cref="Token"/> located at <see cref="PreviousLocation"/>
/// </summary>
/// <remarks>
/// If you have used <see cref="Advance"/> more than once, the <see cref="Location"/> will be incorrect,
/// and you'll need to use <see cref="CreateToken(TokenType, string, Location, object?)"/>
/// with a previously recorded <see cref="CurrentLocation"/>
/// </remarks>
protected Token CreateToken(TokenType type, string text, object? value = null) {
return CreateToken(type, text, PreviousLocation, value);
}

/// <inheritdoc cref="CreateToken(TokenType, string, object?)"/>
protected Token CreateToken(TokenType type, char text, object? value = null) {
return CreateToken(type, char.ToString(text), value);
}
Expand All @@ -75,6 +94,7 @@ protected virtual TSourceType Advance() {
}

internal class TokenLexer : Lexer<Token> {
/// <inheritdoc/>
protected TokenLexer(string sourceName, IEnumerable<Token> source) : base(sourceName, source) {
Advance();
}
Expand Down

0 comments on commit 07472cf

Please sign in to comment.