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

Feat: Dafny format file.dfy and IDE extension #2399

Merged
merged 368 commits into from
Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
368 commits
Select commit Hold shift + click to select a range
d16d4da
Fields working
MikaelMayer Jul 28, 2022
affbc10
Applied the formatter itself!
MikaelMayer Jul 28, 2022
0fb6675
WIP adding owned tokens to datatypes
MikaelMayer Jul 29, 2022
1adc50a
Initial work on datatypes
MikaelMayer Jul 29, 2022
3b0c4c5
Datatypes correctly indented
MikaelMayer Jul 29, 2022
1417745
Final constructor correctly indented
MikaelMayer Jul 29, 2022
84fccd1
No space after last declaration?
MikaelMayer Jul 29, 2022
d33ea6f
Merge branch 'master' into feat-add-trivia-next-token-formatter
MikaelMayer Aug 2, 2022
916dab3
Added example
MikaelMayer Aug 2, 2022
bd0451b
Fixed trivia so that the last newline is for the leadingTrivia
MikaelMayer Aug 3, 2022
3403297
Revert "Fixed trivia so that the last newline is for the leadingTrivia"
MikaelMayer Aug 3, 2022
61db34c
trailing trivia indented correctly
MikaelMayer Aug 4, 2022
a4df959
datatype indentation
MikaelMayer Aug 4, 2022
5d35232
Fixed var indentation.
MikaelMayer Aug 4, 2022
b660509
WIP on the formatter
MikaelMayer Aug 4, 2022
a8cea01
Support for nested generic types
MikaelMayer Aug 4, 2022
228eba9
SynonymTypeDecl 1
MikaelMayer Aug 4, 2022
0b6351a
Fixed a regression test
MikaelMayer Aug 5, 2022
c5716a6
Finished modules, going to do imports and exports
MikaelMayer Aug 5, 2022
92e9029
inline module correctly indented
MikaelMayer Aug 5, 2022
c54ea76
ownedTokens in modules, includes and export sets
MikaelMayer Aug 5, 2022
c2c0ca2
Support for abstract modules, export declarations, and import declara…
MikaelMayer Aug 8, 2022
6492b76
ComprehensionExprFormatting
MikaelMayer Aug 9, 2022
5a45cb2
Fixed a few indentations
MikaelMayer Aug 10, 2022
e2c40ff
Support for iterators
MikaelMayer Aug 11, 2022
0c2684d
More tests
MikaelMayer Aug 12, 2022
62db98f
Support for parenthesized expressions
MikaelMayer Aug 15, 2022
e557e09
Correctly normalize newline type
MikaelMayer Aug 15, 2022
d33ab23
Fixed indents of comments
MikaelMayer Aug 15, 2022
c976656
Fixed loops formatting.
MikaelMayer Aug 15, 2022
052a07d
Support for bit shift, universal pattern and datatype's first parenth…
MikaelMayer Aug 15, 2022
64a2eda
If case and while case statements
MikaelMayer Aug 15, 2022
1726661
Fixed a subtle bug.
MikaelMayer Aug 15, 2022
176fa3f
Support for CALC statements
MikaelMayer Aug 18, 2022
9b42af0
==> and <==
MikaelMayer Aug 18, 2022
d79ceb2
Calc statement working
MikaelMayer Aug 18, 2022
42a6053
Support for labelled statements + refactoring
MikaelMayer Aug 22, 2022
0a4384c
Fixed modules with prefixes
MikaelMayer Aug 23, 2022
8ef7192
Fixed comments after subset type
MikaelMayer Aug 23, 2022
6569d33
Fixed the print statement
MikaelMayer Aug 23, 2022
bdcc9a2
Fixed the tterator
MikaelMayer Aug 23, 2022
5c30819
Fixed the indentation of multi-line comments and refined statements
MikaelMayer Aug 24, 2022
3d85b10
Comment before a case indented with the case
MikaelMayer Aug 24, 2022
b671096
Merge branch 'master' into feat-add-trivia-next-token-formatter
MikaelMayer Aug 25, 2022
0cbc911
Fixed the include tokens.
MikaelMayer Aug 25, 2022
33f0774
Fixed calc alignment + match
MikaelMayer Aug 25, 2022
13b26f0
Fixed the last token
MikaelMayer Aug 25, 2022
07cc6d2
Let or fail expression support and var aligned.
MikaelMayer Aug 25, 2022
0955e4a
Fixed extreme predicates
MikaelMayer Aug 25, 2022
76df147
Support for abstract module decl. Filter out include tokens.
MikaelMayer Aug 25, 2022
edd87e8
Fixed newtype with members
MikaelMayer Aug 25, 2022
5fcfba7
Refactor to enable keeping trailing comment alignment
MikaelMayer Aug 26, 2022
58d90d0
Support for modify statements
MikaelMayer Aug 26, 2022
ae2af6f
Support for comment of cases, and set, map and seq display
MikaelMayer Aug 26, 2022
1937c81
Comments of cases or disjunctions are aligned no matter what.
MikaelMayer Aug 26, 2022
d8312f4
Fixed a null pointer exception
MikaelMayer Aug 29, 2022
41c3744
Always take the first token
MikaelMayer Sep 1, 2022
2759d42
Fixed the root token choice
MikaelMayer Sep 1, 2022
4cd5c19
Fixed arguments and alignement after "print"
MikaelMayer Sep 2, 2022
ff9c042
Support for array creation
MikaelMayer Sep 2, 2022
5315ffa
Support for flat blocks after arrow
MikaelMayer Sep 2, 2022
87942c9
Support for :- as the beginning of a statement
MikaelMayer Sep 2, 2022
61212cf
Support for opaque types
MikaelMayer Sep 2, 2022
df29fab
Better support for closures
MikaelMayer Sep 2, 2022
2fb6762
Support for decreases and invariant in alternative while statements
MikaelMayer Sep 2, 2022
ff4e80a
Support for commas in decreases and reads
MikaelMayer Sep 2, 2022
49bfee2
Solved four more cases
MikaelMayer Sep 7, 2022
efaf165
Modify statement's indentation afterwards
MikaelMayer Sep 7, 2022
ab5f62d
Support for labels for each statement
MikaelMayer Sep 7, 2022
48d0271
Better support for skeleton statements
MikaelMayer Sep 7, 2022
f92cdb7
Fixed skeleton for while statement
MikaelMayer Sep 7, 2022
99a5364
Fixed datatype indentation when | at the end of the line
MikaelMayer Sep 7, 2022
0f27aa5
Fixed two bugs
MikaelMayer Sep 8, 2022
cdbf7e9
Fix the UTF-8 encoding of tokens
MikaelMayer Sep 8, 2022
ecefc07
Added test to ensure include printed correctly
MikaelMayer Sep 8, 2022
116f030
Support for nested includes + added test
MikaelMayer Sep 8, 2022
5b2e1fb
Reverted UTF8, using the more generic version
MikaelMayer Sep 8, 2022
a975d08
Merge branch 'master' into feat-add-trivia-next-token-formatter
MikaelMayer Sep 8, 2022
23a1ae9
Fix master merge
MikaelMayer Sep 8, 2022
2d52aec
Support for formatting :- without LHS in expressions
MikaelMayer Sep 8, 2022
bfcbc07
Fixed the last test
MikaelMayer Sep 8, 2022
14bee8b
datatype indent with inline vertical bars
MikaelMayer Sep 8, 2022
78e2f0a
Fixed first token
MikaelMayer Sep 8, 2022
75266c1
Merge branch 'master' into feat-add-trivia-next-token-formatter
MikaelMayer Sep 13, 2022
1170e16
Binary comparison aligned
MikaelMayer Sep 13, 2022
6f9f6f3
Fixed the merge
MikaelMayer Sep 13, 2022
ee8cc8e
Support for ///
MikaelMayer Sep 13, 2022
3be8f79
Refactoring
MikaelMayer Sep 13, 2022
62078f5
Removed useless method
MikaelMayer Sep 13, 2022
35d07ed
TO REVERT IN OTHER PR: Removed string edit code
MikaelMayer Sep 13, 2022
d1962f2
Finished to prove the dafny part of the formatter
MikaelMayer Sep 13, 2022
68a5a4d
Made the proof 2x faster
MikaelMayer Sep 14, 2022
c5ec51c
Refactoring
MikaelMayer Sep 14, 2022
8ed8a8b
Fixed the driver and Dafny formatter
MikaelMayer Sep 14, 2022
422f94e
Fixed refactoring, simplified proof
MikaelMayer Sep 14, 2022
d478cc7
Fixed a formatting rule
MikaelMayer Sep 14, 2022
b052f5e
Added forall test
MikaelMayer Sep 15, 2022
c316fa3
More refactorings
MikaelMayer Sep 15, 2022
0e9c3d8
Added language server tests
MikaelMayer Sep 16, 2022
70c46aa
Support for multiple files to format
MikaelMayer Sep 19, 2022
f8a1982
Merge branch 'master' into feat-add-trivia-next-token-formatter
MikaelMayer Sep 19, 2022
4b930ee
Updated documentation
MikaelMayer Sep 20, 2022
ad43802
Argument list for functions and method on the same indent as the func…
MikaelMayer Sep 21, 2022
04bdafc
Merge branch 'master' into feat-add-trivia-next-token-formatter
MikaelMayer Sep 21, 2022
e934fab
Huge refactoring to scope the indentation formatter in regions
MikaelMayer Sep 21, 2022
769b841
Fixed parsing.
MikaelMayer Sep 21, 2022
19761f9
Support for statement expr
MikaelMayer Sep 21, 2022
f626961
Fixed a case with the if statement
MikaelMayer Sep 21, 2022
ee65cd7
Alignment of the right of an operator if both operands are on the sam…
MikaelMayer Sep 21, 2022
218771f
Merge branch 'master' into feat-add-trivia-next-token-formatter
MikaelMayer Sep 21, 2022
6ed732b
Further fixes on the formatter.
MikaelMayer Sep 22, 2022
0a9d008
Aligning hints
MikaelMayer Sep 22, 2022
3e83655
Fixed a bug in the Dafny formatter for VSCode
MikaelMayer Sep 22, 2022
b6aa69b
&& var
MikaelMayer Sep 23, 2022
b45b2d8
Support for regex for recursive comments
MikaelMayer Sep 26, 2022
8ca299d
Support for not aligning operators if it conflicts for precedence.
MikaelMayer Sep 26, 2022
9680661
Comments before case are aligned with the case
MikaelMayer Sep 27, 2022
7216a04
Fixed one corner case of && var
MikaelMayer Sep 27, 2022
48ba225
Removed a useless file + review comments
MikaelMayer Sep 27, 2022
74975e2
Renamed files and added tests
MikaelMayer Sep 27, 2022
fbd4c21
Renamed II
MikaelMayer Sep 27, 2022
6d2e228
Update docs/DafnyRef/UserGuide.md
MikaelMayer Sep 27, 2022
4ae8db3
Better support for new expressions.
MikaelMayer Sep 28, 2022
1048226
Added a way so that there is no extra indentation added after var f :…
MikaelMayer Sep 28, 2022
12ad952
Merge branch 'feat-add-trivia-next-token-formatter' of https://github…
MikaelMayer Sep 28, 2022
6b0cdb7
Handled one special case where LHS is empty
MikaelMayer Sep 28, 2022
471b385
Support for no extra indent if datatype has single constructor
MikaelMayer Sep 28, 2022
3bd90ba
Reduce blockiness for match statements and expressions as well
MikaelMayer Sep 28, 2022
b4b2c0c
Support for formatting parent traits
MikaelMayer Sep 28, 2022
6b33529
Support for class extends with multiple parent traits
MikaelMayer Sep 28, 2022
71d8474
Review comments
MikaelMayer Oct 4, 2022
f7ca328
Support for -check and doc
MikaelMayer Oct 4, 2022
8ec6c85
Merge branch 'master' into feat-add-trivia-next-token-formatter
MikaelMayer Oct 4, 2022
361b4b3
Fixed the merge commit
MikaelMayer Oct 5, 2022
0c2b7d8
Review + set comprehension formatting
MikaelMayer Oct 5, 2022
1d9e416
Updated CONTRIBUTING.md
MikaelMayer Oct 12, 2022
aaf64ad
Better dummy lit command
MikaelMayer Oct 13, 2022
13ec228
WIP to ensure every token is taken into account
MikaelMayer Oct 13, 2022
9c368b3
WIP on ensuring every token is owned.
MikaelMayer Oct 14, 2022
f3508b3
WIP fixed compilation issues
MikaelMayer Oct 24, 2022
b976585
Made some progress about owned tokens
MikaelMayer Oct 27, 2022
7277eca
Progress
MikaelMayer Oct 28, 2022
a665f05
Making progress on having every token owned
MikaelMayer Oct 28, 2022
42ba60d
Prepare the PR so that it's ready for merge
MikaelMayer Nov 9, 2022
5cb757b
ReduceBlockiness no longer static
MikaelMayer Nov 9, 2022
103b7a0
Cleaned up initialization of fields
MikaelMayer Nov 9, 2022
5398021
StringBuilder
MikaelMayer Nov 9, 2022
b0ad862
Merge branch 'master' into feat-add-trivia-next-token-formatter
MikaelMayer Nov 9, 2022
28fdb6b
Ensures everything works with the latest master
MikaelMayer Nov 9, 2022
2422cc5
Review comments: IEnumerable + !x instead of x == false
MikaelMayer Nov 10, 2022
b122333
Review comment
MikaelMayer Nov 10, 2022
36a4e56
Review comments
MikaelMayer Nov 10, 2022
79db3ec
Applied the formatter to Formatting.dfy
MikaelMayer Nov 10, 2022
d8fd6a1
Added the PR's description to IndentationFormatter.cs
MikaelMayer Nov 10, 2022
824b8db
Merge branch 'master' into feat-add-trivia-next-token-formatter
MikaelMayer Nov 10, 2022
1d1dd05
Code review
MikaelMayer Nov 17, 2022
e5e658c
Added documentation for PreprocessorHelper
MikaelMayer Nov 17, 2022
cb85dbe
Merge branch 'master' into feat-add-trivia-next-token-formatter
MikaelMayer Nov 17, 2022
0b74c16
Fixed the merge
MikaelMayer Nov 17, 2022
f409101
Added failing test
MikaelMayer Nov 28, 2022
a6cbcaf
Fixing tests
MikaelMayer Dec 28, 2022
4c8a787
Merge branch 'merged-owned-tokens' into feat-add-trivia-next-token-fo…
MikaelMayer Dec 30, 2022
c1cc399
Merge branch 'master' into feat-add-trivia-next-token-formatter
MikaelMayer Jan 10, 2023
9895fb0
Fixed merge
MikaelMayer Jan 10, 2023
e547f0c
WIP figuring out what's wrong
MikaelMayer Jan 10, 2023
727377f
WIP fixing the merge
MikaelMayer Jan 13, 2023
e25b508
Fixed the "requires"
MikaelMayer Jan 17, 2023
ef7221d
Fixed 5 tests
MikaelMayer Jan 17, 2023
c7c2111
Only 9 remaining failing tests
MikaelMayer Jan 18, 2023
acc3ecc
Fixed ClassDecl's formatting
MikaelMayer Jan 19, 2023
2cf6a08
Only 7 tests failed
MikaelMayer Jan 19, 2023
4db837f
Fixed map display problem
MikaelMayer Jan 20, 2023
0e563a4
Making things work better locally
MikaelMayer Jan 20, 2023
9f8e58d
Fixed << problem + converging to other indentation mechanisms
MikaelMayer Jan 23, 2023
f8f90f8
Only set and map comprehensions to go!
MikaelMayer Jan 25, 2023
25bc882
All tests passing again
MikaelMayer Jan 25, 2023
696b76d
Fixed warnings
MikaelMayer Jan 25, 2023
8bd23a5
Optimizations
MikaelMayer Jan 25, 2023
7ddcefe
Optimizations
MikaelMayer Jan 25, 2023
434b1f3
Merge branch 'master' into feat-add-trivia-next-token-formatter
MikaelMayer Jan 25, 2023
20e0fa3
Fixing the merge's compilation
MikaelMayer Jan 25, 2023
63d4040
Last two fixes
MikaelMayer Jan 26, 2023
16b8310
Review comments
MikaelMayer Jan 26, 2023
fb0e555
Review comment
MikaelMayer Jan 26, 2023
358dae1
Review comments
MikaelMayer Jan 27, 2023
a163561
Started to refactor SetIndent in other places
MikaelMayer Jan 27, 2023
a3c804b
Refactored BinExpr
MikaelMayer Jan 27, 2023
eea16d5
More refactoring
MikaelMayer Jan 27, 2023
84c4030
Refactored all expression-related formatting code into expressions
MikaelMayer Jan 27, 2023
d994a6c
Refactored BlockStmt
MikaelMayer Jan 27, 2023
9aebcd0
Refactored BlockStmt
MikaelMayer Jan 27, 2023
3e109e0
Finished the refactoring
MikaelMayer Jan 27, 2023
7f27457
Update docs/DafnyRef/UserGuide.md
MikaelMayer Jan 27, 2023
a17a442
last changes
MikaelMayer Jan 27, 2023
8c87936
Update CONTRIBUTING.md
MikaelMayer Jan 27, 2023
c9ab3d8
Merge branch 'feat-add-trivia-next-token-formatter' of https://github…
MikaelMayer Jan 27, 2023
94182aa
Update RELEASE_NOTES.md
MikaelMayer Jan 30, 2023
1f44b32
Update RELEASE_NOTES.md
MikaelMayer Jan 30, 2023
4f54857
Fixed the integration tests
MikaelMayer Jan 30, 2023
4bad284
Review comments
MikaelMayer Jan 30, 2023
e2c0186
Refactorings
MikaelMayer Jan 30, 2023
6d8c5b8
Indented regex to make it clearer
MikaelMayer Jan 30, 2023
fa77a2b
Moved options to outer scope
MikaelMayer Jan 30, 2023
db57129
Merge branch 'feat-add-trivia-next-token-formatter' of https://github…
MikaelMayer Jan 30, 2023
233fdb8
Split a class in two
MikaelMayer Jan 30, 2023
967bec9
Merge branch 'master' into feat-add-trivia-next-token-formatter
MikaelMayer Jan 30, 2023
e5eceaa
Support for formatting folders explicitly
MikaelMayer Jan 31, 2023
a5d243a
Update CONTRIBUTING.md
MikaelMayer Jan 31, 2023
22161f1
Support for formatting stdin
MikaelMayer Jan 31, 2023
9fae867
Merge branch 'feat-add-trivia-next-token-formatter' of https://github…
MikaelMayer Jan 31, 2023
51daed1
Merge branch 'master' into feat-add-trivia-next-token-formatter
MikaelMayer Jan 31, 2023
07371a1
Update docs/DafnyRef/UserGuide.md
MikaelMayer Jan 31, 2023
f16d10c
Review comments and refactorings
MikaelMayer Jan 31, 2023
60ca207
Merge branch 'feat-add-trivia-next-token-formatter' of https://github…
MikaelMayer Jan 31, 2023
275cba5
Removed dependency on Dafny temporary
MikaelMayer Feb 1, 2023
386d956
Fixed the Dafny conversion script
MikaelMayer Feb 1, 2023
dba9a0b
Support for --verbose, fixed compilation.
MikaelMayer Feb 1, 2023
f339fad
Simplified the 3 dictionaries into one.
MikaelMayer Feb 1, 2023
21700d4
Update docs/DafnyRef/UserGuide.md
MikaelMayer Feb 1, 2023
7db7c89
Update docs/DafnyRef/UserGuide.md
MikaelMayer Feb 1, 2023
9cb0a60
Update docs/DafnyRef/UserGuide.md
MikaelMayer Feb 1, 2023
6aa5dfe
Update docs/DafnyRef/UserGuide.md
MikaelMayer Feb 1, 2023
3796627
Merge branch 'master' into feat-add-trivia-next-token-formatter
MikaelMayer Feb 1, 2023
44d21c2
Updated dafny-to-C# script so that it does not include the runtime an…
MikaelMayer Feb 2, 2023
4bc14c4
Exits command reprint
MikaelMayer Feb 2, 2023
3de2525
Merge branch 'feat-add-trivia-next-token-formatter' of https://github…
MikaelMayer Feb 2, 2023
19fa1de
Made the code idiomatic for now
MikaelMayer Feb 2, 2023
ee4948a
Removed unused references
MikaelMayer Feb 2, 2023
ea2b6ff
More idiomatic + fixed error
MikaelMayer Feb 2, 2023
31e7409
Fixed a few CI tests
MikaelMayer Feb 2, 2023
517948e
dotnet format: Removed unused variables
MikaelMayer Feb 2, 2023
456e137
Merge branch 'master' into feat-add-trivia-next-token-formatter
MikaelMayer Feb 2, 2023
ca05a7c
Fixed verification issue
MikaelMayer Feb 2, 2023
647ff7d
Fixed CI issues
MikaelMayer Feb 2, 2023
67a9fe8
Fixed the single CI warning
MikaelMayer Feb 2, 2023
4434213
Fixed more CI
MikaelMayer Feb 2, 2023
3e3b5ae
Fixed two crashes in CI
MikaelMayer Feb 2, 2023
d8065e9
Fixed LSP test
MikaelMayer Feb 2, 2023
0d8946c
Fixed reading files on disk for the language server
MikaelMayer Feb 2, 2023
96a878c
Change TypeRHS to use previous children. Make ConcreteChildren different
MikaelMayer Feb 3, 2023
54fb307
Merge branch 'master' into feat-add-trivia-next-token-formatter
MikaelMayer Feb 3, 2023
af7bfdf
Recovery for files on the language server
MikaelMayer Feb 3, 2023
a4d1103
Merge branch 'master' into feat-add-trivia-next-token-formatter
MikaelMayer Feb 3, 2023
bd32a64
Fixed a resolution problem
MikaelMayer Feb 3, 2023
aceb1cb
Fixed the four issues seen on CI
MikaelMayer Feb 6, 2023
b9d7e97
Merge branch 'master' into feat-add-trivia-next-token-formatter
MikaelMayer Feb 6, 2023
febccad
Fixed merge
MikaelMayer Feb 6, 2023
1222c06
Removed trailing space
MikaelMayer Feb 6, 2023
d3b0f8a
Merge branch 'master' into feat-add-trivia-next-token-formatter
MikaelMayer Feb 6, 2023
9fe876e
ConcreteChildren => PreResolveChildren
MikaelMayer Feb 6, 2023
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,4 @@ Source/.vs

node_modules/
package.json
package-lock.json
package-lock.json
15 changes: 15 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,18 @@ Once it succeeds, one has to go back to (view 1) and re-run the failed jobs, so

After doing these steps once, for other PRs, one only needs to re-run deep checks in (view 1)

### How can I write portions of Dafny in Dafny itself?

Since https://github.com/dafny-lang/dafny/pull/2399, it is possible to add \*.dfy files next to other source files.
The plugin `dafny-msbuild` takes all the dafny files and compiles them into a single file `Source/DafnyCore/obj/Debug/net6.0/GeneratedFromDafny.cs`
that is automatically included in the build process. This file contains also the Dafny run-time in C#.
One example of such file is `Source/DafnyCore/AST/Formatting.dfy`, and you can use it as a starting point.

Since Dafny cannot read C# files directly, you have to declare the C# functions it is calling using the `{:extern}` attribute to
interoperate with Dafny.
For example, `Formatting.dfy`

- Defines `System.CsString` as an alias for c# strings and concatenation, so that we can interoperate with existing strings rather than using sequences of characters
- Defines `CsStringEmpty` as an alias for `System.String.Empty`
- Defines `Microsoft.Dafny.HelperString.FinishesByNewline` by also using externs. That helper is defined in `IndentationFormatter.cs`
- Defines a trait `IIndentationFormatter` that Dafny can extend and provide to `ReindentProgramFromFirstToken`
5 changes: 3 additions & 2 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,12 +256,13 @@ See [docs/dev/news/](docs/dev/news/).

# 3.7.2

- fix: Hovering over variables and methods inside cases of nested match statements work again. (https://github.com/dafny-lang/dafny/pull/2334)
- fix: Hovering over variables and methods inside cases of nested match statements works again. (https://github.com/dafny-lang/dafny/pull/2334)
- fix: Fix bug in translation of two-state predicates with heap labels. (https://github.com/dafny-lang/dafny/pull/2300)
- fix: Check that arguments of 'unchanged' satisfy enclosing reads clause. (https://github.com/dafny-lang/dafny/pull/2302)
- feat: Whitespaces and comments are kept in relevant parts of the AST (https://github.com/dafny-lang/dafny/pull/1801)
- fix: Underconstrained closures don't crash Dafny anymore. (https://github.com/dafny-lang/dafny/pull/2382)
- fix: Caching in the language server does not prevent gutter icons from being updated correctly. (https://github.com/dafny-lang/dafny/pull/2312)
- fix: Last edited verified first & corrected display of verification status. (https://github.com/dafny-lang/dafny/pull/2352)
- fix: Last edited file verified first & corrected display of verification status. (https://github.com/dafny-lang/dafny/pull/2352)
- fix: Correctly infer type of numeric arguments, where the type is a subset type of a newtype. (https://github.com/dafny-lang/dafny/pull/2314)
- fix: Fix concurrency bug that sometimes led to an exception during the production of verification logs. (https://github.com/dafny-lang/dafny/pull/2398)

Expand Down
51 changes: 43 additions & 8 deletions Source/DafnyCore/AST/DafnyAst.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,29 @@ public string Name {
}
}

/// Get the first token that is in the same file as the DefaultModule.RootToken.FileName
/// (skips included tokens)
public IToken GetFirstTopLevelToken() {
return DefaultModuleDef.GetFirstTopLevelToken();
if (DefaultModule.RootToken.Next == null) {
return null;
}

var firstToken = DefaultModule.RootToken.Next;
// We skip all included files
while (firstToken is { Next: { } } && firstToken.Next.Filename != DefaultModule.RootToken.Filename) {
firstToken = firstToken.Next;
}

if (firstToken == null || firstToken.kind == 0) {
return null;
}

return firstToken;
}

public override IEnumerable<Node> Children => new[] { DefaultModule };

public IEnumerable<Node> ConcreteChildren => Children;
public override IEnumerable<Node> PreResolveChildren => Children;
}

public class Include : TokenNode, IComparable {
Expand Down Expand Up @@ -125,6 +141,7 @@ public int CompareTo(object obj) {
}

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

/// <summary>
Expand Down Expand Up @@ -316,6 +333,8 @@ public static bool ContainsMatchingValue(Attributes attrs, string nm, ref object
Prev == null
? Enumerable.Empty<Node>()
: new List<Node> { Prev });

public override IEnumerable<Node> PreResolveChildren => Children;
}

public static class AttributesExtensions {
Expand Down Expand Up @@ -575,7 +594,8 @@ public NonglobalVariable(IToken tok, string name, Type type, bool isGhost) {
}

public IToken NameToken => tok;
public override IEnumerable<Node> Children => IsTypeExplicit ? Type.Nodes : Enumerable.Empty<Node>();
public override IEnumerable<Node> Children => IsTypeExplicit ? new List<Node>() { Type } : Enumerable.Empty<Node>();
public override IEnumerable<Node> PreResolveChildren => IsTypeExplicit ? new List<Node>() { Type } : Enumerable.Empty<Node>();
}

public class Formal : NonglobalVariable {
Expand Down Expand Up @@ -722,7 +742,8 @@ public class ActualBinding : TokenNode {
public readonly bool IsGhost;

public override IEnumerable<Node> Children => new List<Node> { Actual }.Where(x => x != null);
// Names are owned by the method call

public override IEnumerable<Node> PreResolveChildren => Children;

public ActualBinding(IToken /*?*/ formalParameterName, Expression actual, bool isGhost = false) {
Contract.Requires(actual != null);
Expand Down Expand Up @@ -768,6 +789,7 @@ public void AcceptArgumentExpressionsAsExactParameterList(List<Expression> args
}

public override IEnumerable<Node> Children => arguments == null ? ArgumentBindings : arguments;
public override IEnumerable<Node> PreResolveChildren => Children;
}

class QuantifiedVariableDomainCloner : Cloner {
Expand Down Expand Up @@ -808,6 +830,7 @@ public bool HasAttributes() {
}

public override IEnumerable<Node> Children => Expressions;
public override IEnumerable<Node> PreResolveChildren => Children;
}

public class BottomUpVisitor {
Expand Down Expand Up @@ -874,6 +897,7 @@ public void Visit(Function function) {
Visit(function.Ens);
Visit(function.Decreases.Expressions);
if (function.Body != null) { Visit(function.Body); }
if (function.ByMethodBody != null) { Visit(function.ByMethodBody); }
//TODO More?
}
public void Visit(Expression expr) {
Expand Down Expand Up @@ -904,11 +928,17 @@ protected virtual void VisitOneStmt(Statement stmt) {
}
}
public class TopDownVisitor<State> {
protected bool preResolve = false;

public void Visit(Expression expr, State st) {
Contract.Requires(expr != null);
if (VisitOneExpr(expr, ref st)) {
// recursively visit all subexpressions and all substatements
expr.SubExpressions.Iter(e => Visit(e, st));
if (preResolve && expr is ConcreteSyntaxExpression concreteSyntaxExpression) {
concreteSyntaxExpression.PreResolveSubExpressions.Iter(e => Visit(e, st));
Copy link
Member

Choose a reason for hiding this comment

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

I feel the "two state" nature of our current AST, with its pre and post resolve state, is more complex than it needs to be. I'm hoping that at some point we can remove the ResolvedExpression and ResolvedStatement concepts from our AST.

How would you feel about cloning the AST after parsing, so you can use that instead of having to define and implement PreResolveSubExpressions ?

Making a clone has a small performance penalty, but I expect us to make post-parsing clones anyways once we start adding caching to our pipeline, so that files which have not been changed but need to be re-resolved because their imports changed, don't have to be parsed again.

Copy link
Member Author

@MikaelMayer MikaelMayer Nov 9, 2022

Choose a reason for hiding this comment

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

I feel the "two state" nature of our current AST, with its pre and post resolve state, is more complex than it needs to be. I'm hoping that at some point we can remove the ResolvedExpression and ResolvedStatement concepts from our AST.

+ 1

How would you feel about cloning the AST after parsing, so you can use that instead of having to define and implement PreResolveSubExpressions ?
Making a clone has a small performance penalty, but I expect us to make post-parsing clones anyways once we start adding caching to our pipeline, so that files which have not been changed but need to be re-resolved because their imports changed, don't have to be parsed again.

I would be all in so that SubExpressions is a single field instead of two. The problem is, as @cpitclaudel mentioned it, that .SubExpressions's specification is only specified in the comments to list sub expressions after resolution, not just after parsing. So there would be an inconsistency.
Maybe for the scope of another PR?

Copy link
Member

Choose a reason for hiding this comment

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

As discussed elsewhere, we will remove ConcreteChildren and PreResolver by cloning the AST before resolution, but we'll do that in another PR.

} else {
// recursively visit all subexpressions and all substatements
expr.SubExpressions.Iter(e => Visit(e, st));
}
if (expr is StmtExpr) {
// a StmtExpr also has a substatement
var e = (StmtExpr)expr;
Expand All @@ -920,8 +950,13 @@ public void Visit(Statement stmt, State st) {
Contract.Requires(stmt != null);
if (VisitOneStmt(stmt, ref st)) {
// recursively visit all subexpressions and all substatements
stmt.SubExpressions.Iter(e => Visit(e, st));
stmt.SubStatements.Iter(s => Visit(s, st));
if (preResolve) {
stmt.PreResolveSubExpressions.Iter(e => Visit(e, st));
stmt.PreResolveSubStatements.Iter(s => Visit(s, st));
} else {
stmt.SubExpressions.Iter(e => Visit(e, st));
stmt.SubStatements.Iter(s => Visit(s, st));
}
}
}
public void Visit(IEnumerable<Expression> exprs, State st) {
Expand Down
41 changes: 40 additions & 1 deletion Source/DafnyCore/AST/Expressions/ComprehensionExpr.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace Microsoft.Dafny;
/// LambdaExpr also inherits from this base class but isn't really a comprehension,
/// and should be considered implementation inheritance.
/// </summary>
public abstract class ComprehensionExpr : Expression, IAttributeBearingDeclaration, IBoundVarsBearingExpression {
public abstract class ComprehensionExpr : Expression, IAttributeBearingDeclaration, IBoundVarsBearingExpression, ICanFormat {
public virtual string WhatKind => "comprehension";
public readonly List<BoundVar> BoundVars;
public readonly Expression Range;
Expand Down Expand Up @@ -444,6 +444,7 @@ protected ComprehensionExpr(Cloner cloner, ComprehensionExpr original) : base(cl
}
}
public override IEnumerable<Node> Children => (Attributes != null ? new List<Node> { Attributes } : Enumerable.Empty<Node>()).Concat(SubExpressions);
public override IEnumerable<Node> PreResolveChildren => Children;

public override IEnumerable<Expression> SubExpressions {
get {
Expand All @@ -456,4 +457,42 @@ public override IEnumerable<Expression> SubExpressions {
}

public override IEnumerable<Type> ComponentTypes => BoundVars.Select(bv => bv.Type);
public virtual bool SetIndent(int indentBefore, TokenNewIndentCollector formatter) {
var alreadyAligned = false;
var assignOpIndent = indentBefore;

foreach (var token in OwnedTokens) {
switch (token.val) {
case "forall":
case "exists":
case "map":
case "set":
case "imap":
case "iset": {
indentBefore = formatter.ReduceBlockiness ? indentBefore : formatter.GetNewTokenVisualIndent(token, indentBefore);
assignOpIndent = formatter.ReduceBlockiness ? indentBefore + formatter.SpaceTab : indentBefore;
formatter.SetOpeningIndentedRegion(token, indentBefore);
break;
}
case ":=":
case "::": {
var afterAssignIndent = (formatter.ReduceBlockiness && token.Prev.line == token.line) || token.line == token.Next.line ? assignOpIndent : assignOpIndent + formatter.SpaceTab;
if (alreadyAligned) {
formatter.SetIndentations(token, afterAssignIndent, assignOpIndent, afterAssignIndent);
} else {
if (TokenNewIndentCollector.IsFollowedByNewline(token)) {
formatter.SetIndentations(token, afterAssignIndent, assignOpIndent, afterAssignIndent);
} else {
alreadyAligned = true;
formatter.SetAlign(assignOpIndent, token, out afterAssignIndent, out assignOpIndent);
assignOpIndent -= 1; // because "::" or ":=" has one more char than a comma
}
}
break;
}
}
}

return true;
}
}
1 change: 1 addition & 0 deletions Source/DafnyCore/AST/Expressions/DisjunctivePattern.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public DisjunctivePattern(IToken tok, List<ExtendedPattern> alternatives, bool i
}

public override IEnumerable<Node> Children => Alternatives;
public override IEnumerable<Node> PreResolveChildren => Children;
public override void Resolve(Resolver resolver, ResolutionContext resolutionContext,
Type sourceType, bool isGhost, bool mutable,
bool inPattern, bool inDisjunctivePattern) {
Expand Down
Loading