Skip to content

Commit

Permalink
fix template text starting with '\' + code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Pizzaandy committed Jun 3, 2024
1 parent 138f36b commit 23d7b67
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 28 deletions.
12 changes: 5 additions & 7 deletions Gobo/GmlFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@

namespace Gobo;

public struct FormatResult
public class FormatResult
{
public static readonly FormatResult Empty = new();

public string Output;

public string Ast = string.Empty;
Expand All @@ -23,7 +21,7 @@ public FormatResult(string output)
Output = output;
}

public override readonly string ToString()
public override string ToString()
{
var result = "";

Expand Down Expand Up @@ -64,7 +62,7 @@ public static FormatResult Format(string code, FormatOptions options)
parseStart = Stopwatch.GetTimestamp();
}

var parseResult = new GmlParser(code, options.TabWidth).Parse();
var parseResult = new GmlParser(code).Parse();

new CommentMapper(code, parseResult.TriviaGroups).AttachComments(parseResult.Ast);

Expand Down Expand Up @@ -136,7 +134,7 @@ public static FormatResult Format(string code, FormatOptions options)

try
{
updatedParseResult = new GmlParser(output, options.TabWidth).Parse();
updatedParseResult = new GmlParser(output).Parse();
}
catch (GmlSyntaxErrorException ex)
{
Expand All @@ -145,7 +143,7 @@ public static FormatResult Format(string code, FormatOptions options)
);
}

var resultHash = updatedParseResult.Ast.GetHashCode();
int resultHash = updatedParseResult.Ast.GetHashCode();

if (initialHash != resultHash)
{
Expand Down
13 changes: 8 additions & 5 deletions Gobo/Parser/GmlLexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,14 @@ internal enum LexerMode
private int columnNumber;
private int startIndex;
private int index;
private readonly int tabWidth;
private int character;
private string CurrentToken => text[startIndex..index];

private static readonly char[] whitespaces = { '\u000B', '\u000C', '\u0020', '\u00A0', '\t' };

public GmlLexer(string text, int tabWidth = 4)
public GmlLexer(string text)
{
this.text = text;
this.tabWidth = tabWidth;
index = 0;
lineNumber = 1;
}
Expand Down Expand Up @@ -548,7 +546,12 @@ static bool IsTemplateStringCharacter(int c)
return c != '"' && c != '{' && c != '\r' && c != '\n';
}

while (IsTemplateStringCharacter(Peek()) && !HitEof)
if (character == '\\')
{
Advance();
}

while (!HitEof && IsTemplateStringCharacter(Peek()))
{
if (Peek() == '\\')
{
Expand Down Expand Up @@ -634,7 +637,7 @@ private void Advance()
columnNumber = 0;
break;
case '\t':
columnNumber += tabWidth;
columnNumber += 1;
break;
default:
columnNumber += 1;
Expand Down
6 changes: 3 additions & 3 deletions Gobo/Parser/GmlParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Gobo.Parser;

internal struct GmlParseResult
internal class GmlParseResult
{
public GmlSyntaxNode Ast;

Check warning on line 9 in Gobo/Parser/GmlParser.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-20.04)

Non-nullable field 'Ast' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 9 in Gobo/Parser/GmlParser.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Non-nullable field 'Ast' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 9 in Gobo/Parser/GmlParser.cs

View workflow job for this annotation

GitHub Actions / deploy-to-github-pages

Non-nullable field 'Ast' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 9 in Gobo/Parser/GmlParser.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Non-nullable field 'Ast' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
public List<Token[]> TriviaGroups;

Check warning on line 10 in Gobo/Parser/GmlParser.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-20.04)

Non-nullable field 'TriviaGroups' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 10 in Gobo/Parser/GmlParser.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Non-nullable field 'TriviaGroups' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 10 in Gobo/Parser/GmlParser.cs

View workflow job for this annotation

GitHub Actions / deploy-to-github-pages

Non-nullable field 'TriviaGroups' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 10 in Gobo/Parser/GmlParser.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Non-nullable field 'TriviaGroups' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
Expand Down Expand Up @@ -112,9 +112,9 @@ internal class GmlParser

private delegate bool BinaryExpressionRule(out GmlSyntaxNode node);

public GmlParser(string code, int tabWidth)
public GmlParser(string code)
{
lexer = new GmlLexer(code, tabWidth);
lexer = new GmlLexer(code);
token = lexer.NextToken();
ProcessToken(token);
}
Expand Down
9 changes: 1 addition & 8 deletions Gobo/PrintContext.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
using Gobo.SyntaxNodes;

namespace Gobo;
namespace Gobo;

internal class PrintContext
{
public FormatOptions Options { get; init; }
public string SourceText { get; init; }

// TODO: consider removing stack from context (it's currently unused)
public Stack<GmlSyntaxNode> Stack = new();
public int PrintDepth => Stack.Count;
public string PrintedStack => $"[{string.Join(", ", Stack.Select(node => node.Kind))}]";

public PrintContext(FormatOptions options, string sourceText)
{
Options = options;
Expand Down
5 changes: 0 additions & 5 deletions Gobo/SyntaxNodes/GmlSyntaxNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ public GmlSyntaxNode(TextSpan textSpan)

public Doc Print(PrintContext ctx)
{
ctx.Stack.Push(this);

Doc printed;

var hasComments = Comments.Count > 0;
Expand Down Expand Up @@ -88,8 +86,6 @@ public Doc Print(PrintContext ctx)
printed = PrintWithOwnComments(ctx, printed);
}

ctx.Stack.Pop();

return printed;
}

Expand All @@ -104,7 +100,6 @@ public List<Doc> PrintChildren(PrintContext ctx)
return Children.Select(child => child.Print(ctx)).ToList();
}

// TODO: Move comment logic?
public virtual Doc PrintLeadingComments(
PrintContext ctx,
CommentType asType = CommentType.Leading
Expand Down

0 comments on commit 23d7b67

Please sign in to comment.