-
Notifications
You must be signed in to change notification settings - Fork 81
experiment: rascal formatter based on Box and layoutDiff #2346
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
Draft
jurgenvinju
wants to merge
123
commits into
main
Choose a base branch
from
rascal-formatter
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 10 commits
Commits
Show all changes
123 commits
Select commit
Hold shift + click to select a range
c46d22d
a Rascal formatter setup
jurgenvinju d6d886e
make sure around empty boxes no additional spacing is added
jurgenvinju 6f038c0
removed dead var
jurgenvinju 2fcf830
progress on a rascal formatter
jurgenvinju 3c20142
Merge branch 'hifi-tree-diff' into rascal-formatter
jurgenvinju b36df06
Merge branch 'main' into rascal-formatter
jurgenvinju f196ba9
Merge branch 'main' into rascal-formatter
jurgenvinju 2c616ef
removed dead doc comment
jurgenvinju 6adec9f
minor progress on rascal formatter
jurgenvinju afdc030
convenience for separated lists
jurgenvinju 7085bfa
improving the formatter
jurgenvinju 73356c3
Merge branch 'minor-fixes-for-formatters' into rascal-formatter
jurgenvinju d43ecf5
more additions to the formatter
jurgenvinju 962b521
renamed test to a unique name
jurgenvinju bc33cb6
added lots of new formatting rules for Rascal
jurgenvinju bd5d40e
some deforestation makes the algorithm much faster
jurgenvinju a0cae59
added missing group function (ported from Set)
jurgenvinju 5610e68
factored expensive constant to private global
jurgenvinju b5ca046
more constructs are formatted now and we group statements if they wer…
jurgenvinju be576a1
added lots of formatting rules
jurgenvinju 2b7f7c3
prevent generation of empty literals, replace by NULL where neceessary
jurgenvinju 7709572
fixed an issue and added some debug code
jurgenvinju 9279533
added assert for empty literals
jurgenvinju 2958ef0
Merge branch 'main' into rascal-formatter
jurgenvinju 0993c59
minor fixes
jurgenvinju 0a1d7ea
fixed unused warning
jurgenvinju 850acdd
fixed some unused imports and removed unused function
jurgenvinju 16dc9b7
replaced the complex and broken GG feature of Box2Text by an eager re…
jurgenvinju ff20d58
continued with adding more formatting rules
jurgenvinju bd75140
added rs for rowSeparator feature to Arrays, for when we want to disp…
jurgenvinju 84213b4
tables fixed for the occurence of nested splices. This involves infer…
jurgenvinju a23e703
separated lists do not have space by default between the previous ele…
jurgenvinju 5d08cf4
removed unused pattern variable
jurgenvinju dd6fc23
added more formatting rules. needed fixes in Box2Text
jurgenvinju 43e23fe
streamlining
jurgenvinju e6c0b45
some progress with string termplate formatting
jurgenvinju a88b978
added todo
jurgenvinju 95dc0f4
turned prefix of \' continuation into a layout node to make sure layo…
jurgenvinju c4badbe
re-introduced striprec which has disappeared from Type during a cleanup
jurgenvinju eb94796
gave constructor names to StringLiteral alternatives for easy API
jurgenvinju ace1460
added plausably correct formatting of string templates with left-alig…
jurgenvinju 5ccca1d
slices
jurgenvinju d10dc4a
Merge branch 'main' into rascal-formatter
jurgenvinju d68abb3
better callOrTree indentation
jurgenvinju e0d7d25
Merge branch 'main' into rascal-formatter
jurgenvinju 9786b06
inlined G semantics again to be able to deal with nested U boxes prop…
jurgenvinju 75e0191
started formatting syntax definitions
jurgenvinju 2679cea
fixes in G boxes with H contexts
jurgenvinju ba3128c
added todo
jurgenvinju a2da436
added and used toClusterBox function for retaining vertical clusters …
jurgenvinju e95a888
add clustering to function statements
jurgenvinju fc257f3
AG is now also inlined and lazily evaluated to allow for U and G grou…
jurgenvinju 98dc007
improved relation tables
jurgenvinju 791d6b9
added debUG function for debugging G U and AG groups
jurgenvinju eca370e
fixed Main again
jurgenvinju f2af14d
added more constructs to format
jurgenvinju 7b6dd34
all binary expressions are now lists by tree2box
jurgenvinju 52823cd
added backward grouping
jurgenvinju 3cfe450
added backward option to G groups
jurgenvinju 46736f0
added backwards grouping to debUG
jurgenvinju 6f5658f
single line comments that have to end with a newline now end with a n…
jurgenvinju bf88448
expression elements wrapped in HV because they can become lists now d…
jurgenvinju f2527b3
single line comments fixed
jurgenvinju 2070d2c
incremental additions to the Rascal formatter'
jurgenvinju e3e44aa
incremental additions to the Rascal formatter'
jurgenvinju a56918e
better empty sets
jurgenvinju a45661d
fixed tricky issues with single line comments in layoutDiff
jurgenvinju 8cfe4c4
much better solution for single line comments that end up formatted i…
jurgenvinju f7e2f18
finetuning
jurgenvinju dcb6934
radical optimization due to caching symbol for newline character class
jurgenvinju 280edda
conditional debug prints get special treatment
jurgenvinju d3565ca
added asserts to formatter
jurgenvinju c5b68b9
less leading space for indented data declaration variants
jurgenvinju 082ab36
much better indentation for assignments and declarations with initial…
jurgenvinju d2ea2c5
fix for non-single-line comments that do not need forced newlines, bu…
jurgenvinju 21531a7
removed test comments
jurgenvinju 97393ec
inlined the hot width function (alias for string size)
jurgenvinju f153c9c
factored constant to a global for efficiency sake
jurgenvinju e5d3399
introduced varargs versions of all boxes and renamed the respective b…
jurgenvinju f72ba58
lot of finetuning and removing little issues
jurgenvinju 13a3474
removed all superfluous brackets using the cleanMeUp function
jurgenvinju f8f6c47
fixed issues caused by rough cleanup
jurgenvinju 1d70edf
more fixes
jurgenvinju 2303b89
fixed multi-variable declarations
jurgenvinju ef55725
fixed additional statements in for loops inside string templates
jurgenvinju e5d03b5
fixed return of binary expressions
jurgenvinju 883a798
added some more sanity checks, also for documentation purposes
jurgenvinju 6427de1
removed the superfluous brackets from the Box2Text test cases, for do…
jurgenvinju 54641d8
test renamings
jurgenvinju 34e3cc2
removed more superfluous brackets
jurgenvinju 5214fd3
Merge branch 'main' into rascal-formatter
jurgenvinju 5e2b8f0
moving ahead with testing all files in the library automatically
jurgenvinju a7ad1a3
some bugs
jurgenvinju 849a60a
fixed an 11-year-old search/replace bug in the C grammar
jurgenvinju bf82621
still fixing fall out of changin the default formatter for binary exp…
jurgenvinju f2b50f1
checker found another undeclared constructor
jurgenvinju a88df2c
restored relations-as-tables
jurgenvinju 892e0de
single expressions end up in the first cell
jurgenvinju c648a6a
incremental improvements
jurgenvinju 6d1060f
while and do-while in templates forgotten
jurgenvinju a3af4f0
formatted two files from the standard library
jurgenvinju e8893e7
Merge remote-tracking branch 'origin/main' into rascal-formatter
jurgenvinju ca2995f
spacing in signatures fixed
jurgenvinju 2c215b1
optimized executeTextEdits from quadratic to linear algorithm
jurgenvinju 727a604
fixed issues with reals that start with a dot
jurgenvinju c9f5175
Merge branch 'main' into rascal-formatter
jurgenvinju 6ee64b6
fixed if-then-else formatter
jurgenvinju 4f979f0
fixed rewrite-rule semi-colon attachment
jurgenvinju ead9233
fixed common keyword param spacing
jurgenvinju 0af35cd
fixed default bracket syntax
jurgenvinju d37693f
not flatten the non-associative comparison operators
jurgenvinju b5cf64e
fixing details
jurgenvinju bf0bb8d
improved lists and sets of tuples
jurgenvinju e474a60
improved lists and sets of tuples also for patterns
jurgenvinju 2dc71de
added pattern-replacement-with-when case
jurgenvinju cca1c89
rewrote core box constructors from _X to X_ to avoid issue with type …
jurgenvinju f320d0e
made intercalate and intersperse faster
jurgenvinju e0c8a3e
fixed typo in docs
jurgenvinju dcc531d
fixed function parameter layout
jurgenvinju 1510755
radical optimization for large table layouts
jurgenvinju 0fc3f9c
fixed #2512
jurgenvinju 64e93b9
debugging
jurgenvinju a8399cb
variable becomes formatted
jurgenvinju File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,10 @@ | |
| @contributor{Arnold Lankamp - [email protected]} | ||
| @synopsis{Convert the Rascal internal grammar representation format (Grammar) to | ||
| a syntax definition in Rascal source code.} | ||
| @pitfalls{ | ||
| This function does not use advanced formatting feature because it is a part of | ||
| components early in Rascal's bootstrapping and standard library construction cycle. | ||
| } | ||
| module lang::rascal::format::Grammar | ||
|
|
||
| import ParseTree; | ||
|
|
||
170 changes: 170 additions & 0 deletions
170
src/org/rascalmpl/library/lang/rascal/format/Rascal.rsc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| @license{ | ||
| Copyright (c) 2022, NWO-I Centrum Wiskunde & Informatica (CWI) | ||
| All rights reserved. | ||
|
|
||
| Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: | ||
|
|
||
| 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. | ||
|
|
||
| 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. | ||
|
|
||
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| } | ||
| @synopsis{Composes a default formatter for Rascal modules} | ||
| @description{ | ||
| This module composes and describes a "standard" formatting style for Rascal. | ||
| There could be other styles of course. Other styles can be build by | ||
| writing different `toBox` rules. | ||
| } | ||
| @bootstrapParser | ||
| module lang::rascal::format::Rascal | ||
|
|
||
| // by extending these modules we compose a `toBox` function | ||
| // which handles all relevant constructs of Rascal | ||
| extend lang::box::util::Tree2Box; | ||
| extend lang::rascal::\syntax::Rascal; | ||
|
|
||
| import ParseTree; | ||
| import analysis::diff::edits::ExecuteTextEdits; | ||
| import analysis::diff::edits::HiFiLayoutDiff; | ||
| import analysis::diff::edits::TextEdits; | ||
| import lang::box::\syntax::Box; | ||
| import lang::box::util::Box2Text; | ||
| import String; | ||
|
|
||
| @synopsis{Format an entire Rascal file, in-place.} | ||
| void formatRascalFile(loc \module) { | ||
| start[Module] tree = parse(#start[Module], \module); | ||
| edits = formatRascalModule(tree); | ||
| executeFileSystemChanges(changed(\module, edits)); | ||
| } | ||
|
|
||
| @synopsis{Format a Rascal module string} | ||
| str formatRascalString(str \module) | ||
| = executeTextEdits(\module, formatRascalModule(parse(#start[Module], \module, |unknown:///|))); | ||
|
|
||
| @synopsis{Top-level work-horse for formatting Rascal modules} | ||
| @benefits{ | ||
| * retains source code comments | ||
| * uses Box for adaptive nested formatting | ||
| } | ||
| list[TextEdit] formatRascalModule(start[Module] \module) | ||
| = layoutDiff(\module, parse(#start[Module], format(toBox(\module)), \module@\loc.top)); | ||
|
|
||
|
|
||
| /* Modules */ | ||
|
|
||
| Box toBox(Toplevel* toplevels) = V([toBox(t) | t <- toplevels], vs=2); | ||
|
|
||
| Box toBox((Module) `<Tags tags> module <QualifiedName name> <Import* imports> <Body body>`) | ||
| = V([ | ||
| toBox(tags), | ||
| H([L("module"), toBox(name)]), | ||
| toBox(imports), | ||
| toBox(body) | ||
| ], vs=1); | ||
|
|
||
| Box toBox(Import* imports) = V([toBox(i) | i <- imports]); | ||
|
|
||
| Box toBox((Import) `import <ImportedModule m> ;`) | ||
| = H([L("import"), H([toBox(m), L(";")], hs=0)]); | ||
|
|
||
| Box toBox((Visibility) ``) = NULL(); | ||
|
|
||
| /* Declarations */ | ||
|
|
||
| Box toBox(FunctionModifier* modifiers) = H([toBox(b) | b <- modifiers]); | ||
|
|
||
| Box toBox((Signature) `<FunctionModifiers modifiers> <Type typ> <Name name> <Parameters parameters> throws <{Type ","}+ exs>`) | ||
| = HOV([ | ||
| H([toBox(modifiers), toBox(typ), H([toBox(name), toBox(parameters)], hs=0)]), | ||
| H([L("throws"), HV([toBox(e) | e <- exs])])], hs=1); | ||
|
|
||
| Box toBox((Signature) `<FunctionModifiers modifiers> <Type typ> <Name name> <Parameters parameters>`) | ||
| = H([toBox(modifiers), toBox(typ), H([toBox(name), toBox(parameters)], hs=0)]); | ||
|
|
||
| Box toBox((FunctionDeclaration) `<Tags tags> <Visibility vis> <Signature sig> ;`) | ||
| = V([ | ||
| toBox(tags), | ||
| H([toBox(vis), toBox(sig), L(";")]) | ||
| ]); | ||
|
|
||
| Box toBox((FunctionDeclaration) `<Tags tags> <Visibility vis> <Signature sig> = <Expression exp>;`) | ||
| = V([ | ||
| toBox(tags), | ||
| H([toBox(vis), toBox(sig)]), | ||
| I([H([L("="), H([toBox(exp), L(";")], hs=0)])]) | ||
| ]); | ||
|
|
||
| Box toBox((FunctionDeclaration) `<Tags tags> <Visibility vis> <Signature sig> = <Expression exp> when <{Expression ","}+ conds>;`) | ||
| = V([ | ||
| toBox(tags), | ||
| H([toBox(vis), toBox(sig)]), | ||
| I([H([L("="), toBox(exp)])]), | ||
| I([L("when"), V([toBox(c) | c <- conds])]) | ||
| ]); | ||
|
|
||
| Box toBox((FunctionDeclaration) `<Tags tags> <Visibility vis> <Signature sig> { <Statement* stats> }`) | ||
| = V([ | ||
| toBox(tags), | ||
| H([toBox(vis), toBox(sig), L("{")]), | ||
| I([toBox(stats)]), | ||
| L("}") | ||
| ]); | ||
|
|
||
| Box toBox(Tag* tags) = V([toBox(t) | Tag t <- tags]); | ||
|
|
||
| Box toBox((Tag) `@synopsis <TagString c>`) | ||
| = H([ | ||
| L("@"), L("synopsis"), L("{"), | ||
| H([L("<l>") | l <- split("\n", "<c>"[1..-1])]), | ||
| L("}")] | ||
| , hs=0); | ||
|
|
||
| Box toBox((Tag) `@<Name n> <TagString c>`) | ||
| = HOV([ | ||
| H([L("@"), L("<n>")], hs=0), | ||
| toBox(c)] | ||
| , hs=0) | ||
| when "<n>" != "synopsis"; | ||
|
|
||
| Box toBox((Parameters) `( <Formals formals> <KeywordFormals keywordFormals>)`) | ||
| = H([L("("), H([toBox(formals), toBox(keywordFormals)]), L(")")], hs=0); | ||
|
|
||
| Box toBox((Parameters) `( <Formals formals> ... <KeywordFormals keywordFormals>)`) | ||
| = H([L("("), H([H([toBox(formals), L("...")], hs=0), toBox(keywordFormals)]), L(")")], hs=0); | ||
|
|
||
| /* Statements */ | ||
|
|
||
| Box toBox(Statement* stmts) | ||
| = V([toBox(s) | s <- stmts]); | ||
|
|
||
| Box toBox((Statement) `return <Expression e>;`) | ||
| = HV([L("return"), I([H([toBox(e), L(";")], hs=0)])]); | ||
|
|
||
| /* Expressions */ | ||
|
|
||
| Box toBox((Expression) `<Expression condition> ? <Expression thenExp> : <Expression elseExp>`) | ||
| = HOV([ | ||
| toBox(condition), | ||
| I([H([L("?"), toBox(thenExp)])]), | ||
| I([H([L(":"), toBox(elseExp)])]) | ||
| ]); | ||
|
|
||
| // call without kwargs | ||
| Box toBox((Expression) `<Expression caller>(<{Expression ","}* arguments>)`) | ||
| = H([toBox(caller), L("("), toBox(arguments), L(")")], hs=0); | ||
|
|
||
| // call with kwargs | ||
| Box toBox((Expression) `<Expression caller>(<{Expression ","}* arguments>, <{KeywordArgument[Expression] ","}+ kwargs>)`) | ||
| = H([toBox(caller), L("("), toBox(arguments), H([L(","), toBox(kwargs)], hs=1), L(")")], hs=0); | ||
|
|
||
| // call with kwargs no-comma | ||
| Box toBox((Expression) `<Expression caller>(<{Expression ","}* arguments> <{KeywordArgument[Expression] ","}+ kwargs>)`) | ||
| = H([toBox(caller), L("("), V([toBox(arguments),toBox(kwargs)]), L(")")], hs=0); | ||
|
|
||
| Box toBox({KeywordArgument[Expression] ","}+ args) | ||
| = SL([toBox(a) | a <- args], L(","), hs=0); | ||
|
|
||
| // this should not be necessary | ||
jurgenvinju marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // Box HV([H([])]) = U([]); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.