Skip to content

Commit

Permalink
update variable declaration parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
bartolomej committed Sep 21, 2024
1 parent 1c28707 commit 1f1c78f
Show file tree
Hide file tree
Showing 13 changed files with 108 additions and 34 deletions.
15 changes: 15 additions & 0 deletions runtime/ast/comments.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ast
import (
"bytes"
"github.com/onflow/cadence/runtime/common"
"strings"
)

type Comments struct {
Expand All @@ -17,6 +18,20 @@ func (c Comments) PackToList() []*Comment {
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.Doc() {
if s.Len() > 0 {
s.WriteRune('\n')
}
s.Write(comment.Text())
}
}
return s.String()
}

type Comment struct {
source []byte
}
Expand Down
3 changes: 3 additions & 0 deletions runtime/ast/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ type IntegerExpression struct {
PositiveLiteral []byte
Range
Base int
Comments
}

var _ Element = &IntegerExpression{}
Expand All @@ -238,6 +239,7 @@ func NewIntegerExpression(
value *big.Int,
base int,
tokenRange Range,
comments Comments,
) *IntegerExpression {
common.UseMemory(gauge, common.IntegerExpressionMemoryUsage)

Expand All @@ -246,6 +248,7 @@ func NewIntegerExpression(
Value: value,
Base: base,
Range: tokenRange,
Comments: comments,
}
}

Expand Down
13 changes: 1 addition & 12 deletions runtime/ast/function_declaration.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ package ast

import (
"encoding/json"
"strings"

"github.com/turbolent/prettier"

"github.com/onflow/cadence/runtime/common"
Expand Down Expand Up @@ -209,16 +207,7 @@ func (d *FunctionDeclaration) DeclarationMembers() *Members {
}

func (d *FunctionDeclaration) DeclarationDocString() string {
var s strings.Builder
for _, comment := range d.Comments.Leading {
if comment.Doc() {
if s.Len() > 0 {
s.WriteRune('\n')
}
s.Write(comment.Text())
}
}
return s.String()
return d.Comments.LeadingDocString()
}

func (d *FunctionDeclaration) Doc() prettier.Doc {
Expand Down
8 changes: 4 additions & 4 deletions runtime/ast/variable_declaration.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ type VariableDeclaration struct {
Transfer *Transfer
SecondTransfer *Transfer
ParentIfStatement *IfStatement `json:"-"`
DocString string
Identifier Identifier
StartPos Position `json:"-"`
Access Access
IsConstant bool
Comments
}

var _ Element = &VariableDeclaration{}
Expand All @@ -55,7 +55,7 @@ func NewVariableDeclaration(
startPos Position,
secondTransfer *Transfer,
secondValue Expression,
docString string,
comments Comments,
) *VariableDeclaration {
common.UseMemory(gauge, common.VariableDeclarationMemoryUsage)

Expand All @@ -69,7 +69,7 @@ func NewVariableDeclaration(
StartPos: startPos,
SecondTransfer: secondTransfer,
SecondValue: secondValue,
DocString: docString,
Comments: comments,
}
}

Expand Down Expand Up @@ -127,7 +127,7 @@ func (d *VariableDeclaration) DeclarationMembers() *Members {
}

func (d *VariableDeclaration) DeclarationDocString() string {
return d.DocString
return d.Comments.LeadingDocString()
}

var varKeywordDoc prettier.Doc = prettier.Text("var")
Expand Down
2 changes: 1 addition & 1 deletion runtime/old_parser/declaration.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ func parseVariableDeclaration(
startPos,
secondTransfer,
secondValue,
docString,
ast.Comments{},
)

castingExpression, leftIsCasting := value.(*ast.CastingExpression)
Expand Down
2 changes: 1 addition & 1 deletion runtime/old_parser/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -1771,7 +1771,7 @@ func parseIntegerLiteral(p *parser, literal, text []byte, kind common.IntegerLit
value = new(big.Int)
}

return ast.NewIntegerExpression(p.memoryGauge, literal, value, base, tokenRange)
return ast.NewIntegerExpression(p.memoryGauge, literal, value, base, tokenRange, ast.Comments{})
}

func parseFixedPointPart(gauge common.MemoryGauge, part string) (integer *big.Int, scale uint) {
Expand Down
17 changes: 13 additions & 4 deletions runtime/parser/declaration.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func parseDeclaration(p *parser) (ast.Declaration, error) {
if purity != ast.FunctionPurityUnspecified {
return nil, NewSyntaxError(*purityPos, "invalid view modifier for variable")
}
return parseVariableDeclaration(p, access, accessPos, docString)
return parseVariableDeclaration(p, access, accessPos)

case KeywordFun:
return parseFunctionDeclaration(
Expand Down Expand Up @@ -512,10 +512,10 @@ func parseVariableDeclaration(
p *parser,
access ast.Access,
accessPos *ast.Position,
docString string,
) (*ast.VariableDeclaration, error) {

startPos := p.current.StartPos
startToken := p.current
startPos := startToken.StartPos
if accessPos != nil {
startPos = *accessPos
}
Expand All @@ -525,6 +525,8 @@ func parseVariableDeclaration(
// Skip the `let` or `var` keyword
p.nextSemanticToken()

identifierToken := p.current

identifier, err := p.nonReservedIdentifier("after start of variable declaration")
if err != nil {
return nil, err
Expand All @@ -546,6 +548,8 @@ func parseVariableDeclaration(
}

p.skipSpace()

transferToken := p.current
transfer := parseTransfer(p)
if transfer == nil {
return nil, p.syntaxError("expected transfer")
Expand Down Expand Up @@ -578,7 +582,12 @@ func parseVariableDeclaration(
startPos,
secondTransfer,
secondValue,
docString,
ast.Comments{
Leading: append(
append(startToken.PackToList(), identifierToken.PackToList()...),
transferToken.PackToList()...,
),
},
)

castingExpression, leftIsCasting := value.(*ast.CastingExpression)
Expand Down
55 changes: 54 additions & 1 deletion runtime/parser/declaration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,59 @@ func TestParseVariableDeclaration(t *testing.T) {
)
})

t.Run("var, no type annotation, copy, one value, comments", func(t *testing.T) {

t.Parallel()

result, errs := testParseDeclarations(`
// Before x
var x = /* Before 1 */ 1 // After 1
// Ignored
`)
require.Empty(t, errs)

utils.AssertEqualWithDiff(t,
[]ast.Declaration{
&ast.VariableDeclaration{
Access: ast.AccessNotSpecified,
IsConstant: false,
Identifier: ast.Identifier{
Identifier: "x",
Pos: ast.Position{Line: 3, Column: 4, Offset: 17},
},
Comments: ast.Comments{
Leading: []*ast.Comment{
ast.NewComment(nil, []byte("// Before x")),
ast.NewComment(nil, []byte("/* Before 1 */")),
},
Trailing: []*ast.Comment{},
},
Value: &ast.IntegerExpression{
PositiveLiteral: []byte("1"),
Value: big.NewInt(1),
Base: 10,
Range: ast.Range{
StartPos: ast.Position{Line: 3, Column: 23, Offset: 36},
EndPos: ast.Position{Line: 3, Column: 23, Offset: 36},
},
Comments: ast.Comments{
Leading: []*ast.Comment{},
Trailing: []*ast.Comment{
ast.NewComment(nil, []byte("// After 1")),
},
},
},
Transfer: &ast.Transfer{
Operation: ast.TransferOperationCopy,
Pos: ast.Position{Line: 3, Column: 6, Offset: 19},
},
StartPos: ast.Position{Line: 3, Column: 0, Offset: 13},
},
},
result,
)
})

t.Run("var, no type annotation, copy, one value, access(all)", func(t *testing.T) {

t.Parallel()
Expand Down Expand Up @@ -2493,7 +2546,7 @@ func TestParseEvent(t *testing.T) {
result, errs := testParseDeclarations(`
// Before E
event E() // After E
// Should be ignored
// Ignored
`)
require.Empty(t, errs)

Expand Down
16 changes: 8 additions & 8 deletions runtime/parser/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ func init() {
literal,
literal[2:],
common.IntegerLiteralKindBinary,
token.Range,
token,
), nil
},
})
Expand All @@ -374,7 +374,7 @@ func init() {
literal,
literal[2:],
common.IntegerLiteralKindOctal,
token.Range,
token,
), nil
},
})
Expand All @@ -388,7 +388,7 @@ func init() {
literal,
literal,
common.IntegerLiteralKindDecimal,
token.Range,
token,
), nil
},
})
Expand All @@ -402,7 +402,7 @@ func init() {
literal,
literal[2:],
common.IntegerLiteralKindHexadecimal,
token.Range,
token,
), nil
},
})
Expand All @@ -416,7 +416,7 @@ func init() {
literal,
literal[2:],
common.IntegerLiteralKindUnknown,
token.Range,
token,
), nil
},
})
Expand Down Expand Up @@ -1730,7 +1730,7 @@ func parseHex(r rune) rune {
return -1
}

func parseIntegerLiteral(p *parser, literal, text []byte, kind common.IntegerLiteralKind, tokenRange ast.Range) *ast.IntegerExpression {
func parseIntegerLiteral(p *parser, literal, text []byte, kind common.IntegerLiteralKind, token lexer.Token) *ast.IntegerExpression {

report := func(invalidKind InvalidNumberLiteralKind) {
p.report(
Expand All @@ -1739,7 +1739,7 @@ func parseIntegerLiteral(p *parser, literal, text []byte, kind common.IntegerLit
InvalidIntegerLiteralKind: invalidKind,
// NOTE: not using text, because it has the base-prefix stripped
Literal: string(literal),
Range: tokenRange,
Range: token.Range,
},
)
}
Expand Down Expand Up @@ -1789,7 +1789,7 @@ func parseIntegerLiteral(p *parser, literal, text []byte, kind common.IntegerLit
value = new(big.Int)
}

return ast.NewIntegerExpression(p.memoryGauge, literal, value, base, tokenRange)
return ast.NewIntegerExpression(p.memoryGauge, literal, value, base, token.Range, token.Comments)
}

func parseFixedPointPart(gauge common.MemoryGauge, part string) (integer *big.Int, scale uint) {
Expand Down
2 changes: 1 addition & 1 deletion runtime/parser/statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ func parseIfStatement(p *parser) (*ast.IfStatement, error) {
switch string(p.currentTokenSource()) {
case KeywordLet, KeywordVar:
variableDeclaration, err =
parseVariableDeclaration(p, ast.AccessNotSpecified, nil, "")
parseVariableDeclaration(p, ast.AccessNotSpecified, nil)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion runtime/sema/check_variable_declaration.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ func (checker *Checker) declareVariableDeclaration(declaration *ast.VariableDecl
variable, err := checker.valueActivations.declare(variableDeclaration{
identifier: identifier,
ty: declarationType,
docString: declaration.DocString,
docString: declaration.DeclarationDocString(),
access: checker.accessFromAstAccess(declaration.Access),
kind: declaration.DeclarationKind(),
pos: declaration.Identifier.Pos,
Expand Down
2 changes: 1 addition & 1 deletion runtime/sema/positioninfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ func (i *PositionInfo) recordVariableDeclarationRange(
Identifier: identifier,
DeclarationKind: declaration.DeclarationKind(),
Type: declarationType,
DocString: declaration.DocString,
DocString: declaration.DeclarationDocString(),
},
)
}
5 changes: 5 additions & 0 deletions tools/maprange/go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA=
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/telemetry v0.0.0-20240228155512-f48c80bd79b2/go.mod h1:TeRTkGYfJXctD9OcfyVLyj2J3IxLnKwHJR8f4D8a3YE=
golang.org/x/tools v0.21.0 h1:qc0xYgIbsSDt9EyWz05J5wfa7LOVW0YTLOXrqdLAWIw=
golang.org/x/tools v0.21.0/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk=

0 comments on commit 1f1c78f

Please sign in to comment.