Skip to content

Commit

Permalink
Parser cleanup
Browse files Browse the repository at this point in the history
- Separated out prefix operators in ParseExpr
- Replaced IdentifierExpr with Token for named expressions/statements
- Replaced ParameterSyntax with a tuple
- Continued making Syntax fields readonly
- Added approximate equality for ClientTests float variables
- Fixed bug when parsing/writing generic parameters
-
  • Loading branch information
jmjrawlings committed Aug 3, 2024
1 parent 4f0a35e commit ca825cf
Show file tree
Hide file tree
Showing 20 changed files with 279 additions and 283 deletions.
12 changes: 6 additions & 6 deletions src/MiniZinc.Compiler/BaseModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public abstract class BaseModel<T>
{
public string Name { get; set; } = "";

private Dictionary<string, IIdentifiedSyntax> _namespace;
private Dictionary<string, INamedSyntax> _namespace;

private Dictionary<string, List<DeclareStatement>>? _overloads;

Expand Down Expand Up @@ -50,7 +50,7 @@ public abstract class BaseModel<T>

public IEnumerable<OutputStatement> Outputs => _outputs ?? Enumerable.Empty<OutputStatement>();

public IReadOnlyDictionary<string, IIdentifiedSyntax> NameSpace => _namespace;
public IReadOnlyDictionary<string, INamedSyntax> NameSpace => _namespace;

public SolveStatement? SolveStatement => _solve;

Expand All @@ -69,7 +69,7 @@ protected BaseModel(bool allowFloats = true)
_warnings = null;
_outputs = null;
_constraints = null;
_namespace = new Dictionary<string, IIdentifiedSyntax>();
_namespace = new Dictionary<string, INamedSyntax>();
_searchDirectories = new List<DirectoryInfo>();
_allowFloats = allowFloats;
}
Expand Down Expand Up @@ -311,14 +311,14 @@ public void AddSyntax(SyntaxNode syntax)
break;

case TypeAliasSyntax alias:
var name = alias.Identifier.Name;
var name = alias.Name.StringValue;
if (_namespace.TryGetValue(name, out var old))
Error($"Type alias name \"{name}\" conflicts with {old})");
_namespace.Add(name, alias);
break;

case AssignStatement assign:
name = assign.Name;
name = assign.Name.StringValue;
var expr = assign.Expr;
_namespace.TryGetValue(name, out old);
switch (old)
Expand Down Expand Up @@ -353,7 +353,7 @@ public void AddSyntax(SyntaxNode syntax)
break;

case DeclareStatement declare:
name = declare.Name;
name = declare.Name.StringValue;
_namespace.TryGetValue(name, out old);
switch (old)
{
Expand Down
18 changes: 7 additions & 11 deletions src/MiniZinc.Parser/Expressions/CallSyntax.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
using System.Data.SqlTypes;
namespace MiniZinc.Parser.Syntax;

namespace MiniZinc.Parser.Syntax;

public sealed class CallSyntax : ExpressionSyntax, IIdentifiedSyntax
public sealed class CallSyntax : ExpressionSyntax, INamedSyntax
{
public IdentifierSyntax Identifier { get; }

public readonly IReadOnlyList<ExpressionSyntax>? Args;
public Token Name { get; }

public string Name => Identifier.Name;
public IReadOnlyList<ExpressionSyntax>? Args { get; }

public CallSyntax(IdentifierSyntax identifier, IReadOnlyList<ExpressionSyntax>? args = null)
: base(identifier.Start)
public CallSyntax(in Token name, IReadOnlyList<ExpressionSyntax>? args = null)
: base(name)
{
Args = args;
Identifier = identifier;
Name = name;
}
}
15 changes: 5 additions & 10 deletions src/MiniZinc.Parser/Expressions/GeneratorCallSyntax.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
namespace MiniZinc.Parser.Syntax;

public sealed class GeneratorCallSyntax : ExpressionSyntax, IIdentifiedSyntax
public sealed class GeneratorCallSyntax : ExpressionSyntax, INamedSyntax
{
public IdentifierSyntax Identifier { get; }
public ExpressionSyntax Expr { get; }
public List<GeneratorSyntax> Generators { get; }
public string Name => Identifier.Name;
public Token Name { get; }

public GeneratorCallSyntax(
IdentifierSyntax identifier,
ExpressionSyntax expr,
List<GeneratorSyntax> generators
)
: base(identifier.Start)
public GeneratorCallSyntax(Token name, ExpressionSyntax expr, List<GeneratorSyntax> generators)
: base(name)
{
Identifier = identifier;
Name = name;
Expr = expr;
Generators = generators;
}
Expand Down
9 changes: 6 additions & 3 deletions src/MiniZinc.Parser/Expressions/GeneratorSyntax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

public sealed class GeneratorSyntax : ExpressionSyntax
{
public GeneratorSyntax(in Token start)
: base(start) { }
public GeneratorSyntax(in Token start, List<Token> names)
: base(start)
{
Names = names;
}

public required List<IdentifierSyntax> Names { get; set; }
public List<Token> Names { get; }

public required ExpressionSyntax From { get; set; }

Expand Down
6 changes: 0 additions & 6 deletions src/MiniZinc.Parser/Expressions/IIdentifiedSyntax.cs

This file was deleted.

6 changes: 6 additions & 0 deletions src/MiniZinc.Parser/Expressions/INamedSyntax.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace MiniZinc.Parser.Syntax;

public interface INamedSyntax
{
public Token Name { get; }
}
4 changes: 1 addition & 3 deletions src/MiniZinc.Parser/Expressions/IdentifierSyntax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ public IdentifierSyntax(in Token token)

public TokenKind Kind => Start.Kind;

public string Name => Start.StringValue;
public Token Name => Start;

public override string ToString() => Start.ToString();

public static implicit operator string(IdentifierSyntax f) => f.Name;
}
14 changes: 0 additions & 14 deletions src/MiniZinc.Parser/Expressions/ParameterSyntax.cs

This file was deleted.

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

public List<(IdentifierSyntax, ExpressionSyntax)> Fields { get; } = new();
public List<(Token, ExpressionSyntax)> Fields { get; } = new();
}
4 changes: 2 additions & 2 deletions src/MiniZinc.Parser/Expressions/SyntaxNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ public bool Equals(SyntaxNode? other)
case (IncludeStatement a, IncludeStatement b):
return a.Path.Equals(b.Path);
case (AssignStatement a, AssignStatement b):
if (!a.Identifier.Equals(b.Identifier))
if (!a.Name.Equals(b.Name))
return false;
if (!a.Expr.Equals(b.Expr))
return false;
return true;
case (DeclareStatement a, DeclareStatement b):
if (!a.Identifier.Equals(b.Identifier))
if (!a.Name.Equals(b.Name))
return false;
if (!a.Type.Equals(b.Type))
return false;
Expand Down
61 changes: 24 additions & 37 deletions src/MiniZinc.Parser/Expressions/TypeInstSyntax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ public TypeSyntax(in Token start)

public TypeKind Kind { get; set; } = TypeKind.Any;

/// True if the type is a variable (ie not a parameter)
public Token Name { get; set; }

/// True if the type is a variable (ie not a parameter)
public bool Var { get; set; }

Expand All @@ -32,44 +35,48 @@ public TypeSyntax(in Token start)
/// <mzn>class(a:int) ++ class(b: bool)</mzn>
public sealed class CompositeTypeSyntax : TypeSyntax
{
/// <summary>
/// A concatenation of other types
/// </summary>
/// <mzn>class(a:int) ++ class(b: bool)</mzn>
public CompositeTypeSyntax(in Token Start)
: base(Start) { }
public IReadOnlyList<TypeSyntax> Types { get; }

public List<TypeSyntax> Types { get; set; } = new();
public CompositeTypeSyntax(in Token Start, List<TypeSyntax> types)
: base(Start)
{
Types = types;
Kind = TypeKind.Composite;
}
}

public sealed class RecordTypeSyntax : TypeSyntax
{
public RecordTypeSyntax(in Token start, IReadOnlyList<ParameterSyntax> fields)
public IReadOnlyList<(Token, TypeSyntax)> Fields { get; }

public RecordTypeSyntax(in Token start, List<(Token, TypeSyntax)> fields)
: base(start)
{
Fields = fields;
}

public IReadOnlyList<ParameterSyntax> Fields { get; }
}

public sealed class TupleTypeSyntax : TypeSyntax
{
public TupleTypeSyntax(in Token Start)
: base(Start) { }
public List<TypeSyntax> Items { get; }

public List<TypeSyntax> Items { get; set; } = new();
public TupleTypeSyntax(in Token Start, List<TypeSyntax> items)
: base(Start)
{
Items = items;
Kind = TypeKind.Tuple;
}
}

public sealed class ExprType : TypeSyntax
public sealed class ExprTypeSyntax : TypeSyntax
{
public ExprType(in Token Start, ExpressionSyntax Expr)
public ExpressionSyntax Expr { get; }

public ExprTypeSyntax(in Token Start, ExpressionSyntax Expr)
: base(Start)
{
this.Expr = Expr;
}

public ExpressionSyntax Expr { get; init; }
}

public sealed class ArrayTypeSyntax : TypeSyntax
Expand Down Expand Up @@ -109,23 +116,3 @@ public SetTypeSyntax(in Token Start)

public required TypeSyntax Items { get; init; }
}

/// <summary>
/// An identifier used as a type
/// </summary>
/// <mzn>type X = 1..3; var X: x;</mzn>
public sealed class IdentifierTypeSyntax : TypeSyntax
{
/// <summary>
/// An identifier used as a type
/// </summary>
/// <mzn>type X = 1..3; var X: x;</mzn>
public IdentifierTypeSyntax(in Token start, IdentifierSyntax identifier)
: base(start)
{
Identifier = identifier;
}

public readonly IdentifierSyntax Identifier;
public string Name => Identifier.Name;
}
Loading

0 comments on commit ca825cf

Please sign in to comment.