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

Do not let Parser.Parse edit the default module #4059

Merged
merged 44 commits into from
May 25, 2023

Conversation

keyboardDrummer
Copy link
Member

@keyboardDrummer keyboardDrummer commented May 22, 2023

  • Parser.Parse no longer takes the default module as an argument which it then appends to, but instead returns a new type FileModuleDefinition, which acts as a container for file contents.
  • SourceProcessor.ProcessDirectives now makes sure not to remove or add newlines at the end of the file.
  • Parsing code is shared between CLI and IDE
  • Module cloning code has been moved out of Cloner and into the module classes.

By submitting this pull request, I confirm that my contribution is made under the terms of the MIT license.

@keyboardDrummer keyboardDrummer marked this pull request as ready for review May 25, 2023 10:05
@keyboardDrummer keyboardDrummer changed the title Pure parse function Do not let Parser.Parse edit the default module May 25, 2023
@keyboardDrummer keyboardDrummer changed the title Do not let Parser.Parse edit the default module Do not let Parser.Parse edit the default module May 25, 2023
@keyboardDrummer keyboardDrummer enabled auto-merge (squash) May 25, 2023 13:07
Copy link
Contributor

@alex-chew alex-chew left a comment

Choose a reason for hiding this comment

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

Please split up big PRs like this, especially since much of the diff here is refactoring and moving code around, as opposed to implementing the changes described in the PR description.

Comment on lines +101 to +116
// private static string ParseFile(DafnyFile dafnyFile, Include include, BuiltIns builtIns, ErrorReporter errorReporter) {
// var fn = builtIns.Options.UseBaseNameForFileName ? Path.GetFileName(dafnyFile.FilePath) : dafnyFile.FilePath;
// try {
// var parseResult = ParseUtils.Parse(dafnyFile.Content, dafnyFile.Uri, builtIns, errorReporter);
// var errorCount = parseResult.ErrorCount;
// if (errorCount != 0) {
// return $"{errorCount} parse errors detected in {fn}";
// }
// } catch (IOException e) {
// IToken tok = include == null ? Token.NoToken : include.tok;
// errorReporter.Error(MessageSource.Parser, tok, "Unable to open included file");
// return $"Error opening file \"{fn}\": {e.Message}";
// }
//
// return null; // Success
// }
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this still needed?

Copy link
Member Author

Choose a reason for hiding this comment

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

Ugh, woops. Will remove in the follow-up PR (#4083)

@@ -147,7 +147,6 @@ ensures true
}

AreAllTokensOwned(program);
Assert.Equal(9, count); // Sanity check
Copy link
Contributor

Choose a reason for hiding this comment

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

Why does this need to be removed?

Copy link
Member Author

Choose a reason for hiding this comment

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

Because the DefaultModule now has a filename in its StartToken, the traversal doesn't go deeper than that Node and count only gets up to 1. This is fine though. I'm not sure why Assert.Equal(9, count) was in there because it wasn't asserting any required behavior.

} else {
Reporter.Error(MessageSource.RefinementTransformer, nw, commonMsg, nw.WhatKind, nw.Name);
}
} else if (nw is DefaultClassDecl) {
if (d is DefaultClassDecl) {
m.TopLevelDecls[index] = MergeClass((DefaultClassDecl)nw, (DefaultClassDecl)d);
m.DefaultClass = (DefaultClassDecl)MergeClass((DefaultClassDecl)nw, (DefaultClassDecl)d);
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not very familiar with this part of the code, but is it not necessary to nwPointer.Set(m.DefaultClass) here?

Copy link
Member Author

Choose a reason for hiding this comment

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

This will work as well, since there's only one DefaultClass field so assigning to it will do the same as using the Set of that field's pointer, but what you suggest looks better. I will change that in the follow-up PR.

@keyboardDrummer
Copy link
Member Author

keyboardDrummer commented May 25, 2023

Please split up big PRs like this, especially since much of the diff here is refactoring and moving code around, as opposed to implementing the changes described in the PR description.

The pure moves are just ScopeCloner and AbstractSignatureCloner, that were placed in their own file, and indeed those didn't need to be in this PR. Sorry for that. The core of the PR change is the new parsing logic in ParseUtils, which is a combination of what was in DafnyLangParser and in DafnyMain, and the remaining changes were just to get the tests to pass again.

@keyboardDrummer keyboardDrummer merged commit 3e555a0 into dafny-lang:master May 25, 2023
keyboardDrummer added a commit that referenced this pull request May 26, 2023
Follow-up of #4059

### Changes
- No longer let the error message "Error: the included file <file>
contains error(s)" appear when using the CLI. This error is not useful
on the CLI because the actual errors of the included file are shown as
well.
- No longer let `ParseFile.ParseFile` take and modify an ErrorReporter,
but instead let it return one.
- No longer let Parser.Parse take and modify a `Builtins`, but instead
let it return a list of actions that can modify a `Builtin`.

### Testing
- Updated existing test expect files for the change in included file
error reporting

<small>By submitting this pull request, I confirm that my contribution
is made under the terms of the [MIT
license](https://github.com/dafny-lang/dafny/blob/master/LICENSE.txt).</small>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants