Skip to content

Commit 124aef7

Browse files
committed
Merge branch 'master' into fxamacker/use-atree-map-to-replace-domain-regsiters
2 parents 94e8cc2 + a56e521 commit 124aef7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+2956
-857
lines changed

.github/workflows/compatibility-check-template.yml

+1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ jobs:
108108
path: |
109109
./tmp/output-old.txt
110110
./tmp/output-new.txt
111+
./tmp/contracts.csv
111112
112113
# Check Diff
113114

.github/workflows/get-contracts.yml

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
name: Get contracts
2+
3+
on:
4+
workflow_call:
5+
inputs:
6+
chain:
7+
required: true
8+
type: string
9+
secrets:
10+
FLOWDIVER_API_KEY:
11+
required: true
12+
13+
env:
14+
GO_VERSION: '1.22'
15+
16+
concurrency:
17+
group: ${{ github.workflow }}-${{ github.run_id }}-${{ inputs.chain }}
18+
cancel-in-progress: true
19+
20+
jobs:
21+
check:
22+
runs-on: ubuntu-latest
23+
steps:
24+
- name: Checkout
25+
uses: actions/checkout@v4
26+
27+
- uses: actions/setup-go@v4
28+
with:
29+
go-version: ${{ env.GO_VERSION }}
30+
cache: true
31+
32+
- name: Make output dirs
33+
run: |
34+
mkdir tmp
35+
36+
# Get contracts
37+
38+
- name: Download contracts
39+
env:
40+
FLOWDIVER_API_KEY: ${{ secrets.FLOWDIVER_API_KEY }}
41+
working-directory: ./tools/get-contracts
42+
run: |
43+
go run . --chain=${{ inputs.chain }} --apiKey="$FLOWDIVER_API_KEY" > ../../tmp/contracts.csv
44+
45+
# Upload
46+
47+
- name: Upload
48+
uses: actions/upload-artifact@v3
49+
with:
50+
name: ${{ inputs.chain }}-contracts
51+
path: |
52+
./tmp/contracts.csv

ast/elementtype.go

+1
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,5 @@ const (
8585
ElementTypeForceExpression
8686
ElementTypePathExpression
8787
ElementTypeAttachExpression
88+
ElementTypeStringTemplateExpression
8889
)

ast/elementtype_string.go

+3-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ast/expression.go

+76-1
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,81 @@ func (*StringExpression) precedence() precedence {
220220
return precedenceLiteral
221221
}
222222

223+
// StringTemplateExpression
224+
225+
type StringTemplateExpression struct {
226+
// Values and Expressions are assumed to be interleaved, V[0] + E[0] + V[1] + ... + E[n-1] + V[n]
227+
// this is enforced in the parser e.g. "a\(b)c" will be parsed as follows
228+
// Values: []string{"a","c"}
229+
// Expressions: []Expression{b}
230+
Values []string
231+
Expressions []Expression
232+
Range
233+
}
234+
235+
var _ Expression = &StringTemplateExpression{}
236+
237+
func NewStringTemplateExpression(
238+
gauge common.MemoryGauge,
239+
values []string,
240+
exprs []Expression,
241+
exprRange Range,
242+
) *StringTemplateExpression {
243+
common.UseMemory(gauge, common.NewStringTemplateExpressionMemoryUsage(len(values)+len(exprs)))
244+
if len(values) != len(exprs)+1 {
245+
// assert string template alternating structure
246+
panic(errors.NewUnreachableError())
247+
}
248+
return &StringTemplateExpression{
249+
Values: values,
250+
Expressions: exprs,
251+
Range: exprRange,
252+
}
253+
}
254+
255+
var _ Element = &StringExpression{}
256+
var _ Expression = &StringExpression{}
257+
258+
func (*StringTemplateExpression) ElementType() ElementType {
259+
return ElementTypeStringTemplateExpression
260+
}
261+
262+
func (*StringTemplateExpression) isExpression() {}
263+
264+
func (*StringTemplateExpression) isIfStatementTest() {}
265+
266+
func (e *StringTemplateExpression) Walk(walkChild func(Element)) {
267+
walkExpressions(walkChild, e.Expressions)
268+
}
269+
270+
func (e *StringTemplateExpression) String() string {
271+
return Prettier(e)
272+
}
273+
274+
func (e *StringTemplateExpression) Doc() prettier.Doc {
275+
if len(e.Expressions) == 0 {
276+
return prettier.Text(QuoteString(e.Values[0]))
277+
}
278+
279+
// TODO: must reproduce expressions as literals
280+
panic("not implemented")
281+
}
282+
283+
func (e *StringTemplateExpression) MarshalJSON() ([]byte, error) {
284+
type Alias StringTemplateExpression
285+
return json.Marshal(&struct {
286+
*Alias
287+
Type string
288+
}{
289+
Type: "StringTemplateExpression",
290+
Alias: (*Alias)(e),
291+
})
292+
}
293+
294+
func (*StringTemplateExpression) precedence() precedence {
295+
return precedenceLiteral
296+
}
297+
223298
// IntegerExpression
224299

225300
type IntegerExpression struct {
@@ -1416,7 +1491,7 @@ func FunctionDocument(
14161491
}
14171492

14181493
// NOTE: not all functions have a parameter list,
1419-
// e.g. the `destroy` special function
1494+
// e.g. the `init` (initializer, special function)
14201495
if parameterList != nil {
14211496

14221497
signatureDoc = append(

ast/expression_extractor.go

+59-25
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ type StringExtractor interface {
4848
ExtractString(extractor *ExpressionExtractor, expression *StringExpression) ExpressionExtraction
4949
}
5050

51+
type StringTemplateExtractor interface {
52+
ExtractStringTemplate(extractor *ExpressionExtractor, expression *StringTemplateExpression) ExpressionExtraction
53+
}
54+
5155
type ArrayExtractor interface {
5256
ExtractArray(extractor *ExpressionExtractor, expression *ArrayExpression) ExpressionExtraction
5357
}
@@ -117,31 +121,32 @@ type AttachExtractor interface {
117121
}
118122

119123
type ExpressionExtractor struct {
120-
IndexExtractor IndexExtractor
121-
ForceExtractor ForceExtractor
122-
BoolExtractor BoolExtractor
123-
NilExtractor NilExtractor
124-
IntExtractor IntExtractor
125-
FixedPointExtractor FixedPointExtractor
126-
StringExtractor StringExtractor
127-
ArrayExtractor ArrayExtractor
128-
DictionaryExtractor DictionaryExtractor
129-
IdentifierExtractor IdentifierExtractor
130-
AttachExtractor AttachExtractor
131-
MemoryGauge common.MemoryGauge
132-
VoidExtractor VoidExtractor
133-
UnaryExtractor UnaryExtractor
134-
ConditionalExtractor ConditionalExtractor
135-
InvocationExtractor InvocationExtractor
136-
BinaryExtractor BinaryExtractor
137-
FunctionExtractor FunctionExtractor
138-
CastingExtractor CastingExtractor
139-
CreateExtractor CreateExtractor
140-
DestroyExtractor DestroyExtractor
141-
ReferenceExtractor ReferenceExtractor
142-
MemberExtractor MemberExtractor
143-
PathExtractor PathExtractor
144-
nextIdentifier int
124+
IndexExtractor IndexExtractor
125+
ForceExtractor ForceExtractor
126+
BoolExtractor BoolExtractor
127+
NilExtractor NilExtractor
128+
IntExtractor IntExtractor
129+
FixedPointExtractor FixedPointExtractor
130+
StringExtractor StringExtractor
131+
StringTemplateExtractor StringTemplateExtractor
132+
ArrayExtractor ArrayExtractor
133+
DictionaryExtractor DictionaryExtractor
134+
IdentifierExtractor IdentifierExtractor
135+
AttachExtractor AttachExtractor
136+
MemoryGauge common.MemoryGauge
137+
VoidExtractor VoidExtractor
138+
UnaryExtractor UnaryExtractor
139+
ConditionalExtractor ConditionalExtractor
140+
InvocationExtractor InvocationExtractor
141+
BinaryExtractor BinaryExtractor
142+
FunctionExtractor FunctionExtractor
143+
CastingExtractor CastingExtractor
144+
CreateExtractor CreateExtractor
145+
DestroyExtractor DestroyExtractor
146+
ReferenceExtractor ReferenceExtractor
147+
MemberExtractor MemberExtractor
148+
PathExtractor PathExtractor
149+
nextIdentifier int
145150
}
146151

147152
var _ ExpressionVisitor[ExpressionExtraction] = &ExpressionExtractor{}
@@ -271,6 +276,35 @@ func (extractor *ExpressionExtractor) ExtractString(expression *StringExpression
271276
return rewriteExpressionAsIs(expression)
272277
}
273278

279+
func (extractor *ExpressionExtractor) VisitStringTemplateExpression(expression *StringTemplateExpression) ExpressionExtraction {
280+
281+
// delegate to child extractor, if any,
282+
// or call default implementation
283+
284+
if extractor.StringTemplateExtractor != nil {
285+
return extractor.StringTemplateExtractor.ExtractStringTemplate(extractor, expression)
286+
}
287+
return extractor.ExtractStringTemplate(expression)
288+
}
289+
290+
func (extractor *ExpressionExtractor) ExtractStringTemplate(expression *StringTemplateExpression) ExpressionExtraction {
291+
292+
// copy the expression
293+
newExpression := *expression
294+
295+
// rewrite all value expressions
296+
297+
rewrittenExpressions, extractedExpressions :=
298+
extractor.VisitExpressions(expression.Expressions)
299+
300+
newExpression.Expressions = rewrittenExpressions
301+
302+
return ExpressionExtraction{
303+
RewrittenExpression: &newExpression,
304+
ExtractedExpressions: extractedExpressions,
305+
}
306+
}
307+
274308
func (extractor *ExpressionExtractor) VisitArrayExpression(expression *ArrayExpression) ExpressionExtraction {
275309

276310
// delegate to child extractor, if any,

ast/precedence.go

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ const (
8383
// - BoolExpression
8484
// - NilExpression
8585
// - StringExpression
86+
// - StringTemplateExpression
8687
// - IntegerExpression
8788
// - FixedPointExpression
8889
// - ArrayExpression

ast/string_template_test.go

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Cadence - The resource-oriented smart contract programming language
3+
*
4+
* Copyright Flow Foundation
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package ast
20+
21+
import (
22+
"testing"
23+
24+
"github.com/stretchr/testify/assert"
25+
26+
"github.com/turbolent/prettier"
27+
)
28+
29+
func TestStringTemplate_Doc(t *testing.T) {
30+
31+
t.Parallel()
32+
33+
stmt := &StringTemplateExpression{
34+
Values: []string{
35+
"abc",
36+
},
37+
Expressions: []Expression{},
38+
Range: Range{
39+
StartPos: Position{Offset: 4, Line: 2, Column: 3},
40+
EndPos: Position{Offset: 11, Line: 2, Column: 10},
41+
},
42+
}
43+
44+
assert.Equal(t,
45+
prettier.Text("\"abc\""),
46+
stmt.Doc(),
47+
)
48+
}

ast/visitor.go

+4
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ type ExpressionVisitor[T any] interface {
183183
VisitNilExpression(*NilExpression) T
184184
VisitBoolExpression(*BoolExpression) T
185185
VisitStringExpression(*StringExpression) T
186+
VisitStringTemplateExpression(*StringTemplateExpression) T
186187
VisitIntegerExpression(*IntegerExpression) T
187188
VisitFixedPointExpression(*FixedPointExpression) T
188189
VisitDictionaryExpression(*DictionaryExpression) T
@@ -219,6 +220,9 @@ func AcceptExpression[T any](expression Expression, visitor ExpressionVisitor[T]
219220
case ElementTypeStringExpression:
220221
return visitor.VisitStringExpression(expression.(*StringExpression))
221222

223+
case ElementTypeStringTemplateExpression:
224+
return visitor.VisitStringTemplateExpression(expression.(*StringTemplateExpression))
225+
222226
case ElementTypeIntegerExpression:
223227
return visitor.VisitIntegerExpression(expression.(*IntegerExpression))
224228

common/memorykind.go

+1
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ const (
204204
MemoryKindIntegerExpression
205205
MemoryKindFixedPointExpression
206206
MemoryKindArrayExpression
207+
MemoryKindStringTemplateExpression
207208
MemoryKindDictionaryExpression
208209
MemoryKindIdentifierExpression
209210
MemoryKindInvocationExpression

0 commit comments

Comments
 (0)