Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pure parse function #4083

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Source/DafnyCore.Test/DooFileTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ public void UnknownManifestEntries() {
private static Program ParseProgram(string dafnyProgramText, DafnyOptions options) {
const string fullFilePath = "untitled:foo";
var rootUri = new Uri(fullFilePath);
var outerModule = new DefaultModuleDefinition(new List<Uri> { rootUri }, false);
Microsoft.Dafny.Type.ResetScopes();
var errorReporter = new ConsoleErrorReporter(options, outerModule);
var errorReporter = new ConsoleErrorReporter(options);
var program = ParseUtils.Parse(dafnyProgramText, rootUri, errorReporter);
Assert.Equal(0, errorReporter.ErrorCount);
return program;
Expand Down
32 changes: 21 additions & 11 deletions Source/DafnyCore/AST/BuiltIns.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,32 +67,42 @@ public static Attributes AxiomAttribute() {
public UserDefinedType ArrayType(int dims, Type arg, bool allowCreationOfNewClass) {
Contract.Requires(1 <= dims);
Contract.Requires(arg != null);
return ArrayType(Token.NoToken, dims, new List<Type>() { arg }, allowCreationOfNewClass);
var (result, mod) = ArrayType(Token.NoToken, dims, new List<Type> { arg }, allowCreationOfNewClass);
mod(this);
return result;
}
public UserDefinedType ArrayType(IToken tok, int dims, List<Type> optTypeArgs, bool allowCreationOfNewClass, bool useClassNameType = false) {

public static (UserDefinedType type, Action<BuiltIns> ModifyBuiltins) ArrayType(IToken tok, int dims, List<Type> optTypeArgs, bool allowCreationOfNewClass, bool useClassNameType = false) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we document the difference between this ArrayType and the previous one?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is some implicit documentation in the fact that this one is static. I'll add documentation to the non-static one saying it updates the instance.

Contract.Requires(tok != null);
Contract.Requires(1 <= dims);
Contract.Requires(optTypeArgs == null || optTypeArgs.Count > 0); // ideally, it is 1, but more will generate an error later, and null means it will be filled in automatically
Contract.Ensures(Contract.Result<UserDefinedType>() != null);

var arrayName = ArrayClassName(dims);
if (useClassNameType) {
arrayName = arrayName + "?";
arrayName += "?";
}
if (allowCreationOfNewClass && !arrayTypeDecls.ContainsKey(dims)) {
ArrayClassDecl arrayClass = new ArrayClassDecl(dims, SystemModule, DontCompile());

void ModifyBuiltins(BuiltIns builtIns) {
if (!allowCreationOfNewClass || builtIns.arrayTypeDecls.ContainsKey(dims)) {
return;
}

ArrayClassDecl arrayClass = new ArrayClassDecl(dims, builtIns.SystemModule, builtIns.DontCompile());
for (int d = 0; d < dims; d++) {
string name = dims == 1 ? "Length" : "Length" + d;
Field len = new SpecialField(RangeToken.NoToken, name, SpecialField.ID.ArrayLength, dims == 1 ? null : (object)d, false, false, false, Type.Int, null);
len.EnclosingClass = arrayClass; // resolve here
len.EnclosingClass = arrayClass; // resolve here
arrayClass.Members.Add(len);
}
arrayTypeDecls.Add(dims, arrayClass);
SystemModule.SourceDecls.Add(arrayClass);
CreateArrowTypeDecl(dims); // also create an arrow type with this arity, since it may be used in an initializing expression for the array

builtIns.arrayTypeDecls.Add(dims, arrayClass);
builtIns.SystemModule.SourceDecls.Add(arrayClass);
builtIns.CreateArrowTypeDecl(dims); // also create an arrow type with this arity, since it may be used in an initializing expression for the array
}
UserDefinedType udt = new UserDefinedType(tok, arrayName, optTypeArgs);
return udt;

var udt = new UserDefinedType(tok, arrayName, optTypeArgs);
return (udt, ModifyBuiltins);
}

public static string ArrayClassName(int dims) {
Expand Down
28 changes: 1 addition & 27 deletions Source/DafnyCore/AST/DafnyAst.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public string Name {
/// Get the first token that is in the same file as the DefaultModule.RootToken.FileName
/// (skips included tokens)
public IToken GetFirstTopLevelToken() {
var rootToken = DefaultModule.RangeToken.StartToken;
var rootToken = DefaultModuleDef.RangeToken.StartToken;
if (rootToken.Next == null) {
return null;
}
Expand All @@ -137,32 +137,6 @@ public override IEnumerable<Assumption> Assumptions(Declaration decl) {
}
}

public class Include : TokenNode, IComparable {
public Uri IncluderFilename { get; }
public Uri IncludedFilename { get; }
public string CanonicalPath { get; }
public bool ErrorReported;

public Include(IToken tok, Uri includer, Uri theFilename) {
this.tok = tok;
this.IncluderFilename = includer;
this.IncludedFilename = theFilename;
this.CanonicalPath = DafnyFile.Canonicalize(theFilename.LocalPath).LocalPath;
this.ErrorReported = false;
}

public int CompareTo(object obj) {
if (obj is Include include) {
return CanonicalPath.CompareTo(include.CanonicalPath);
} else {
throw new NotImplementedException();
}
}

public override IEnumerable<Node> Children => Enumerable.Empty<Node>();
public override IEnumerable<Node> PreResolveChildren => Enumerable.Empty<Node>();
}

/// <summary>
/// An expression introducting bound variables
/// </summary>
Expand Down
Loading