-
Notifications
You must be signed in to change notification settings - Fork 149
[WIP] Retain comments in AST #3509
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
bartolomej
wants to merge
92
commits into
onflow:master
Choose a base branch
from
bartolomej:preserve-comments
base: master
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 79 commits
Commits
Show all changes
92 commits
Select commit
Hold shift + click to select a range
d8066a5
stop consuming trivia as tokens
bartolomej 76f7107
compute leading and trailing trivia
bartolomej 95438a8
partially fix trivia sourcing
bartolomej a6619eb
add draft test case
bartolomej 5cffdbf
test doc comments in AST
bartolomej 44c666b
refactor trivia parsing
bartolomej 0539fa3
start tracking comments in ast, add sample tests
bartolomej 1fd5a6f
update comments
bartolomej 96e7347
parse block trailing comments
bartolomej ff4f9d8
update old parser, remove DocField field on FunctionDeclaration
bartolomej 86bd62b
Merge branch 'refs/heads/master' into preserve-comments
bartolomej e4a1073
fix existing lexer tests
bartolomej 4158ce8
introduce `isFollowedByTrivia`
bartolomej e0d8714
remove custom meta left denotations
bartolomej 8fe1b5f
keep emiting space/newline tokens to make the parsing logic work
bartolomej 213ae48
Revert "remove custom meta left denotations"
bartolomej 2d7093d
Revert "introduce `isFollowedByTrivia`"
bartolomej 4c106aa
remove comment parsing logic and token types
bartolomej ae6525c
combine space and newline trivia types
bartolomej d1592df
do not track trivia on space tokens
bartolomej 8d2ce86
track leading comment for special function declarations
bartolomej de4f950
track only comments as trivia in lexer
bartolomej d98ab21
update tests
bartolomej 05ed569
test eof comment
bartolomej dcff58b
fix parser tests
bartolomej 4c7047e
Revert "fix existing lexer tests"
bartolomej 2be52ed
remove trivia structs
bartolomej 1c28707
include comments in event declaration
bartolomej 1f1c78f
update variable declaration parsing
bartolomej 79ecacf
update parameter parsing
bartolomej 28604a6
Merge branch 'refs/heads/master' into preserve-comments
bartolomej 63155ed
update variable declaration parsing
bartolomej b9bd76a
update event parsing tests
bartolomej f65f9d5
fix old parser declaration tests
bartolomej a145552
update parsing for composite declaration, declaration field, declarat…
bartolomej 39457a8
update attachment declaration parsing
bartolomej 1c66016
update interface declaration, transaction declaration parsing
bartolomej 3575509
update entitlement parsing
bartolomej 716a3f4
update return statement parsing
bartolomej da65123
Merge branch 'refs/heads/master' into preserve-comments
bartolomej de4bb91
fix merge issues
bartolomej 8f05ada
fix type errors
bartolomej 0412649
clarify legacy field usage
bartolomej 1cf6bfd
fix parser test issues
bartolomej a9d9924
add todo
bartolomej 1471e54
update if statement parsing
bartolomej 27b320a
update if comments test case
bartolomej bb4952f
update return, continue, break parsing
bartolomej 1c7e1a7
update switch/case parsing
bartolomej ef4fab5
update while parsing
bartolomej 6a956b0
update remove from parsing
bartolomej 9486f3c
update emit statement parsing
bartolomej d8c32df
update for statement parsing, start attaching comments to identifiers
bartolomej b7feec6
update function declaration or expression stmt parsing
bartolomej d7ea0a3
Merge branch 'master' into preserve-comments
bartolomej db9f216
fix lexer tests
bartolomej 3707776
fix pooled lexer reset for comments & other smaller stuff
bartolomej ef73932
fix leading inline comments for parameters
bartolomej 60eddf3
fix access token comments for variable declaration
bartolomej 50ba770
attach comments to identifier expressions
bartolomej 522daba
remove legacy DocString fields
bartolomej 19ebdab
fix tests
bartolomej 7d19248
Merge branch 'master' into preserve-comments
bartolomej 14834e4
fixed all lint issues
bartolomej 8260761
replaces all usages of `ast.Comments{}` with `ast.EmptyComments`
bartolomej 01ba32b
fix invalid arguments
bartolomej 88a6550
fix json format assertions to include comments field
bartolomej 62cb4a8
revert all comment tracking changes in the old parser, tests passing
bartolomej 99de326
remove NewNominalTypeWithComments
bartolomej f0eb847
resolve small todos
bartolomej dcd9b8b
cleanup comments parsing in lexer
bartolomej 64663e3
fix missing comment end error
turbolent 96eb1d2
remove UnexpectedTokenInBlockCommentError
turbolent 3e177c5
make comment tracking configurable
bartolomej 43b4adf
Merge branch 'preserve-comments' of https://github.com/bartolomej/cad…
bartolomej 1992948
rename parser config field
bartolomej a9352af
fix lexer init in parser
bartolomej 39571f2
avoid unecessary slice allocation
bartolomej fb5324a
add back IsValidIdentifier
bartolomej e0f7aa4
break the arguments into multiple lines
bartolomej 8f71c39
add back empty comments test case, small allocation fix, add comment
bartolomej 094ec78
reset trackComments in clear func
bartolomej 73fd987
reuse existing comments slice allocation
bartolomej 6880089
remove unecessary if condition
bartolomej 9cb7e62
Merge branch 'preserve-comments' of https://github.com/bartolomej/cad…
bartolomej a91c792
fix tests
bartolomej 41b9972
ignore edge case func param comments for simplicity
bartolomej 9941b2e
resolve all unit tests
bartolomej dd67e6e
Merge branch 'master' into preserve-comments
bartolomej 9457798
move some test cases around
bartolomej 43544d0
refactored to named `Comments Comments` struct field
bartolomej d1ded08
fix comments json serialization
bartolomej 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
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 |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| package ast | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "strings" | ||
|
|
||
| "github.com/onflow/cadence/common" | ||
| ) | ||
|
|
||
| type Comments struct { | ||
| Leading []*Comment `json:"-"` | ||
| Trailing []*Comment `json:"-"` | ||
| } | ||
|
|
||
| var EmptyComments = Comments{} | ||
|
|
||
| // All combines Leading and Trailing comments in a single array. | ||
| func (c Comments) All() []*Comment { | ||
| var comments []*Comment | ||
| comments = append(comments, c.Leading...) | ||
| comments = append(comments, c.Trailing...) | ||
| return comments | ||
| } | ||
|
|
||
| // LeadingDocString prints the leading doc comments to string | ||
| func (c Comments) LeadingDocString() string { | ||
| var s strings.Builder | ||
| for _, comment := range c.Leading { | ||
| if comment.IsDoc() { | ||
| if s.Len() > 0 { | ||
| s.WriteRune('\n') | ||
| } | ||
| s.Write(comment.Text()) | ||
| } | ||
| } | ||
| return s.String() | ||
| } | ||
|
|
||
| type Comment struct { | ||
| source []byte | ||
| } | ||
|
|
||
| func NewComment(memoryGauge common.MemoryGauge, source []byte) *Comment { | ||
| common.UseMemory(memoryGauge, common.NewRawStringMemoryUsage(len(source))) | ||
| return &Comment{ | ||
| source: source, | ||
| } | ||
| } | ||
|
|
||
| var blockCommentDocStringPrefix = []byte("/**") | ||
| var blockCommentStringPrefix = []byte("/*") | ||
| var lineCommentDocStringPrefix = []byte("///") | ||
| var lineCommentStringPrefix = []byte("//") | ||
| var blockCommentStringSuffix = []byte("*/") | ||
|
|
||
| func (c Comment) Multiline() bool { | ||
| return bytes.HasPrefix(c.source, blockCommentStringPrefix) | ||
| } | ||
|
|
||
| func (c Comment) IsDoc() bool { | ||
| if c.Multiline() { | ||
| return bytes.HasPrefix(c.source, blockCommentDocStringPrefix) | ||
| } else { | ||
| return bytes.HasPrefix(c.source, lineCommentDocStringPrefix) | ||
| } | ||
| } | ||
|
|
||
| var commentPrefixes = [][]byte{ | ||
| blockCommentDocStringPrefix, // must be before blockCommentStringPrefix | ||
| blockCommentStringPrefix, | ||
| lineCommentDocStringPrefix, // must be before lineCommentStringPrefix | ||
| lineCommentStringPrefix, | ||
| } | ||
|
|
||
| var commentSuffixes = [][]byte{ | ||
| blockCommentStringSuffix, | ||
| } | ||
|
|
||
| // Text without opening/closing comment characters /*, /**, */, // | ||
| func (c Comment) Text() []byte { | ||
| withoutPrefixes := cutOptionalPrefixes(c.source, commentPrefixes) | ||
| return cutOptionalSuffixes(withoutPrefixes, commentSuffixes) | ||
| } | ||
|
|
||
| func cutOptionalPrefixes(input []byte, prefixes [][]byte) (output []byte) { | ||
| output = input | ||
| for _, prefix := range prefixes { | ||
| cut, _ := bytes.CutPrefix(output, prefix) | ||
| output = cut | ||
| } | ||
| return | ||
| } | ||
|
|
||
| func cutOptionalSuffixes(input []byte, suffixes [][]byte) (output []byte) { | ||
| output = input | ||
| for _, suffix := range suffixes { | ||
| cut, _ := bytes.CutSuffix(output, suffix) | ||
| output = cut | ||
| } | ||
| return | ||
| } | ||
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.