Skip to content

Commit

Permalink
parser // tightened up Syntax/Expr/Type syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
jmjrawlings committed Jul 14, 2024
1 parent 3f7a861 commit c06c7b9
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 101 deletions.
2 changes: 1 addition & 1 deletion src/MiniZinc.Parser/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1925,7 +1925,7 @@ ident.Kind is TokenKind.GENERIC_SEQUENCE
internal bool ParseArrayType([NotNullWhen(true)] out ArrayTypeSyntax? arr)
{
arr = default;
var dims = new List<SyntaxNode>();
var dims = new List<TypeSyntax>();

if (!Expect(TokenKind.ARRAY, out var start))
return false;
Expand Down
6 changes: 3 additions & 3 deletions src/MiniZinc.Parser/Syntax/IndexAndNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

public sealed class IndexAndNode : ExpressionSyntax
{
public readonly SyntaxNode Index;
public readonly SyntaxNode Value;
public readonly ExpressionSyntax Index;
public readonly ExpressionSyntax Value;

public IndexAndNode(SyntaxNode index, SyntaxNode value)
public IndexAndNode(ExpressionSyntax index, ExpressionSyntax value)
: base(index.Start)
{
Index = index;
Expand Down
4 changes: 2 additions & 2 deletions src/MiniZinc.Parser/Syntax/LetSyntax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public sealed class LetSyntax : ExpressionSyntax
{
public LetSyntax(in Token start, List<ILetLocalSyntax>? locals, SyntaxNode body)
public LetSyntax(in Token start, List<ILetLocalSyntax>? locals, ExpressionSyntax body)
: base(start)
{
Locals = locals;
Expand All @@ -11,5 +11,5 @@ public LetSyntax(in Token start, List<ILetLocalSyntax>? locals, SyntaxNode body)

public List<ILetLocalSyntax>? Locals { get; }

public SyntaxNode Body { get; }
public ExpressionSyntax Body { get; }
}
2 changes: 1 addition & 1 deletion src/MiniZinc.Parser/Syntax/SetLiteralSyntax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ public sealed class SetLiteralSyntax : ExpressionSyntax
public SetLiteralSyntax(in Token start)
: base(start) { }

public List<SyntaxNode> Elements { get; } = new();
public List<ExpressionSyntax> Elements { get; } = new();
}
11 changes: 11 additions & 0 deletions src/MiniZinc.Parser/Syntax/SyntaxExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@

public static class SyntaxExtensions
{
/// <summary>
/// Write this node as a minizinc string
/// </summary>
public static string Write(this SyntaxNode node, WriteOptions? options = null)
{
var writer = new Writer(options);
writer.WriteSyntax(node);
var mzn = writer.ToString();
return mzn;
}

/// <summary>
/// Create a deep clone of this syntax node
/// </summary>
Expand Down
13 changes: 1 addition & 12 deletions src/MiniZinc.Parser/Syntax/SyntaxNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,7 @@ protected SyntaxNode(in Token start)
Start = start;
}

public string SourceText => Write(WriteOptions.Minimal);

/// <summary>
/// Write this node as a minizinc string
/// </summary>
public string Write(WriteOptions? options = null)
{
var writer = new Writer(options);
writer.WriteSyntax(this);
var mzn = writer.ToString();
return mzn;
}
public string SourceText => this.Write(WriteOptions.Minimal);

public override bool Equals(object? obj)
{
Expand Down
2 changes: 1 addition & 1 deletion src/MiniZinc.Parser/Syntax/TupleAccessSyntax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public sealed class TupleAccessSyntax : ExpressionSyntax
{
public readonly SyntaxNode Expr;
public readonly ExpressionSyntax Expr;

public readonly Token Field;

Expand Down
6 changes: 3 additions & 3 deletions src/MiniZinc.Parser/Syntax/TypeInstSyntax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ public TupleTypeSyntax(in Token Start)

public sealed class ExprType : TypeSyntax
{
public ExprType(in Token Start, SyntaxNode Expr)
public ExprType(in Token Start, ExpressionSyntax Expr)
: base(Start)
{
this.Expr = Expr;
}

public SyntaxNode Expr { get; init; }
public ExpressionSyntax Expr { get; init; }
}

public sealed class ArrayTypeSyntax : TypeSyntax
Expand All @@ -79,7 +79,7 @@ public ArrayTypeSyntax(in Token Start)

public required TypeSyntax Items { get; set; }

public required List<SyntaxNode> Dimensions { get; set; }
public required List<TypeSyntax> Dimensions { get; set; }

public int N => Dimensions.Count;
}
Expand Down
4 changes: 2 additions & 2 deletions src/MiniZinc.Parser/Syntax/UnaryOperatorSyntax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ public sealed class UnaryOperatorSyntax : ExpressionSyntax
{
public Operator Operator { get; }

public SyntaxNode Expr { get; }
public ExpressionSyntax Expr { get; }

public UnaryOperatorSyntax(in Token start, Operator op, SyntaxNode expr)
public UnaryOperatorSyntax(in Token start, Operator op, ExpressionSyntax expr)
: base(start)
{
Operator = op;
Expand Down
Loading

0 comments on commit c06c7b9

Please sign in to comment.