Skip to content

Commit

Permalink
general // ModelSyntax and DataSyntax
Browse files Browse the repository at this point in the history
- Make ModelSyntax its own thing and not SyntaxNode
- Make DataSyntax
- Parse{Model,Data}String and File functions
- Statements and Expressions from SyntaxNode
  • Loading branch information
jmjrawlings committed Jun 23, 2024
1 parent 1db8362 commit afb76b2
Show file tree
Hide file tree
Showing 36 changed files with 516 additions and 292 deletions.
3 changes: 2 additions & 1 deletion src/MiniZinc.Client/SolveResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ public abstract record SolveResult<T>
SolveStatus.EvaluationError => true,
SolveStatus.SyntaxError => true,
SolveStatus.TypeError => true,
SolveStatus.Timeout => true,
_ => false
};

Expand Down Expand Up @@ -181,7 +182,7 @@ public IEnumerable<U> GetArray1D<U>(string id)
var array = Get<Array1DSyntax>(id);
foreach (var node in array.Elements)
{
if (node is not SyntaxNode<U> literal)
if (node is not ExpressionSyntax<U> literal)
throw new Exception();
yield return literal.Value;
}
Expand Down
26 changes: 17 additions & 9 deletions src/MiniZinc.Client/SolverProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,13 +242,11 @@ private void OnSolutionOutput(SolutionOutput o)

if (dzn is not null)
{
var parsed = Parser.ParseString(dzn!);
var parsed = Parser.ParseDataString(dzn);
_data = new Dictionary<string, SyntaxNode>();
parsed.EnsureOk();
foreach (var node in parsed.SyntaxNode.Nodes)
foreach (var assign in parsed.Data.Assignments)
{
if (node is not AssignmentSyntax assign)
throw new Exception();
var name = assign.Identifier.ToString();
var value = assign.Expr;
_data[name] = value;
Expand Down Expand Up @@ -292,7 +290,7 @@ private void OnErrorOutput(ErrorOutput o)

private void OnStatusOutput(StatusOutput o)
{
_solveStatus = o.Status switch
SolveStatus? solveStatus = o.Status switch
{
"ALL_SOLUTIONS" => SolveStatus.AllSolutions,

Expand All @@ -304,13 +302,23 @@ private void OnStatusOutput(StatusOutput o)

"UNSAT_OR_UNBOUNDED" => SolveStatus.UnsatOrUnbounded,

"UNKNOWN" => SolveStatus.Error,

"ERROR" => SolveStatus.Error,

var s => throw new Exception($"Unexpected status {s}")
_ => null
};
_current = CreateResult();

if (solveStatus is { } status)
{
_solveStatus = status;
_current = CreateResult();
}
else
{
// TODO - confirm
_solveStatus = SolveStatus.Timeout;
_current = CreateResult(error: "time limit reached");
}

_channel.Writer.TryWrite(_current);
}

Expand Down
25 changes: 13 additions & 12 deletions src/MiniZinc.Compiler/Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
/// </summary>
/// <remarks>
/// This class extracts useful semantic information
/// from <see cref="SyntaxTree"/>
/// from <see cref="ModelSyntax"/>
/// </remarks>
public class Model
{
Expand Down Expand Up @@ -321,17 +321,12 @@ it out */
else
{
_parsedFiles.Add(file.FullName);
var result = Parser.ParseFile(file);
var result = Parser.ParseModelFile(file);
result.EnsureOk();
AddNode(result.SyntaxNode);
AddModel(result.Model);
}
}
break;

case SyntaxTree tree:
foreach (var node in tree.Nodes)
AddNode(node);
break;
}
}

Expand Down Expand Up @@ -413,13 +408,19 @@ public void AddFile(string path)
var message = CreateFileNotFoundMessage(path);
throw new FileNotFoundException(message, path);
}
var result = Parser.ParseFile(file);
var result = Parser.ParseModelFile(file);
result.EnsureOk();

// Added models become a search directory
if (file.Directory is { } dir)
AddSearchPath(dir);
AddNode(result.SyntaxNode);
AddModel(result.Model);
}

private void AddModel(ModelSyntax model)
{
foreach (var statement in model.Statements)
AddNode(statement);
}

/// <summary>
Expand Down Expand Up @@ -452,9 +453,9 @@ public Model AddFiles(params string[] paths)

public void AddString(string mzn)
{
var result = Parser.ParseString(mzn);
var result = Parser.ParseModelString(mzn);
result.EnsureOk();
AddNode(result.SyntaxNode);
AddModel(result.Model);
}

public Model AddStrings(params string[] strings)
Expand Down
18 changes: 13 additions & 5 deletions src/MiniZinc.Parser/ParseResult.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
namespace MiniZinc.Parser;
using MiniZinc.Parser.Syntax;

using Syntax;
namespace MiniZinc.Parser;

public sealed record ParseResult<T>
where T : SyntaxNode
public record ParseResult
{
public required T SyntaxNode { get; init; }
public required string? SourceFile { get; init; }
public required string SourceText { get; init; }
public required bool Ok { get; init; }
Expand All @@ -24,3 +22,13 @@ public void EnsureOk()
throw new ParseException("Could not parse");
}
}

public sealed record ModelParseResult : ParseResult
{
public required ModelSyntax Model { get; init; }
}

public sealed record DataParseResult : ParseResult
{
public required DataSyntax Data { get; init; }
}
Loading

0 comments on commit afb76b2

Please sign in to comment.