Skip to content

Commit ee00d3e

Browse files
committed
added Top-Level Comment to Modules
1 parent 0717f50 commit ee00d3e

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

src/ast/module.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ type Module struct {
1616
FileNameToken *token.Token
1717
// all the imported modules
1818
Imports []*ImportStmt
19+
// First token in the file if present, or nil otherwise
20+
Comment *token.Token
1921
// a set which contains all files needed
2022
// to link the final executable
2123
// contains .c, .lib, .a and .o files

src/parser/declarations.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ func (p *parser) varDeclaration(startDepth int, isField bool) ast.Declaration {
6060
if comment != nil && comment.Range.End.Line < begin.Range.Start.Line-1 {
6161
comment = nil
6262
}
63+
// prefer to attach the comment to a declaration, rather than to the module
64+
if comment == p.module.Comment {
65+
p.module.Comment = nil
66+
}
6367

6468
isPublic := p.peekN(startDepth+1).Type == token.OEFFENTLICHE || p.peekN(startDepth+1).Type == token.OEFFENTLICHEN
6569

@@ -383,6 +387,10 @@ func (p *parser) funcDeclaration(startDepth int) ast.Declaration {
383387
if comment != nil && comment.Range.End.Line < begin.Range.Start.Line-1 {
384388
comment = nil
385389
}
390+
// prefer to attach the comment to a declaration, rather than to the module
391+
if comment == p.module.Comment {
392+
p.module.Comment = nil
393+
}
386394

387395
isPublic := p.peekN(startDepth+1).Type == token.OEFFENTLICHE
388396

@@ -695,6 +703,10 @@ func (p *parser) structDeclaration() ast.Declaration {
695703
if comment != nil && comment.Range.End.Line < begin.Range.Start.Line-1 {
696704
comment = nil
697705
}
706+
// prefer to attach the comment to a declaration, rather than to the module
707+
if comment == p.module.Comment {
708+
p.module.Comment = nil
709+
}
698710

699711
p.consume(token.NENNEN, token.DIE)
700712
isPublic := p.match(token.OEFFENTLICHE)

src/parser/parser.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ func newParser(name string, tokens []token.Token, modules map[string]*ast.Module
5454
tokens = append(tokens, token.Token{Type: token.EOF})
5555
}
5656

57+
is_first_tok_comment := tokens[0].Type == token.COMMENT
58+
5759
comments := make([]token.Token, 0)
5860
// filter the comments out
5961
i := 0
@@ -67,6 +69,11 @@ func newParser(name string, tokens []token.Token, modules map[string]*ast.Module
6769
}
6870
tokens = tokens[:i]
6971

72+
var module_comment *token.Token
73+
if is_first_tok_comment {
74+
module_comment = &comments[0]
75+
}
76+
7077
parser := &parser{
7178
tokens: tokens,
7279
comments: comments,
@@ -75,6 +82,7 @@ func newParser(name string, tokens []token.Token, modules map[string]*ast.Module
7582
module: &ast.Module{
7683
FileName: name,
7784
Imports: make([]*ast.ImportStmt, 0),
85+
Comment: module_comment,
7886
ExternalDependencies: make(map[string]struct{}, 5),
7987
Ast: &ast.Ast{
8088
Statements: make([]ast.Statement, 0),

0 commit comments

Comments
 (0)