From c847afbcfb3300fc091379db0cbae32aea5085b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20M=C3=BCller?= Date: Wed, 19 Jun 2024 08:56:36 -0700 Subject: [PATCH] add command to dump all hard keywords --- runtime/cmd/info/main.go | 11 +++++++++++ runtime/parser/declaration_test.go | 2 +- runtime/parser/expression_test.go | 4 ++-- runtime/parser/keyword.go | 12 ++++++------ runtime/parser/statement_test.go | 2 +- 5 files changed, 21 insertions(+), 10 deletions(-) diff --git a/runtime/cmd/info/main.go b/runtime/cmd/info/main.go index 0c8cb150cd..fb8a56b277 100644 --- a/runtime/cmd/info/main.go +++ b/runtime/cmd/info/main.go @@ -28,6 +28,7 @@ import ( "github.com/onflow/cadence/runtime/ast" "github.com/onflow/cadence/runtime/common" "github.com/onflow/cadence/runtime/errors" + "github.com/onflow/cadence/runtime/parser" "github.com/onflow/cadence/runtime/sema" "github.com/onflow/cadence/runtime/stdlib" "github.com/onflow/cadence/runtime/tests/checker" @@ -71,6 +72,10 @@ var commands = map[string]command{ help: "Dumps all built-in values", handler: dumpBuiltinValues, }, + "dump-hard-keywords": { + help: "Dumps all hard keywords", + handler: dumpHardKeywords, + }, } func dumpBuiltinTypes() { @@ -291,6 +296,12 @@ func dumpBuiltinValues() { } } +func dumpHardKeywords() { + for _, keyword := range parser.HardKeywords { + fmt.Printf("- %s\n", keyword) + } +} + func printAvailableCommands() { type commandHelp struct { name string diff --git a/runtime/parser/declaration_test.go b/runtime/parser/declaration_test.go index 9c34532c43..9f3544050f 100644 --- a/runtime/parser/declaration_test.go +++ b/runtime/parser/declaration_test.go @@ -9642,7 +9642,7 @@ func TestSoftKeywordsInFunctionDeclaration(t *testing.T) { }) } - for _, keyword := range softKeywords { + for _, keyword := range SoftKeywords { testSoftKeyword(keyword) } } diff --git a/runtime/parser/expression_test.go b/runtime/parser/expression_test.go index d82cc21ec8..f93e0769d4 100644 --- a/runtime/parser/expression_test.go +++ b/runtime/parser/expression_test.go @@ -6496,7 +6496,7 @@ func TestParseIdentifiers(t *testing.T) { func TestParseHardKeywords(t *testing.T) { t.Parallel() - testParseIdentifiersWith(t, hardKeywords, func(t *testing.T, keyword string, err error) { + testParseIdentifiersWith(t, HardKeywords, func(t *testing.T, keyword string, err error) { utils.AssertEqualWithDiff(t, []error{ &SyntaxError{ @@ -6512,7 +6512,7 @@ func TestParseHardKeywords(t *testing.T) { func TestParseSoftKeywords(t *testing.T) { t.Parallel() - testParseIdentifiersWith(t, softKeywords, func(t *testing.T, _ string, err error) { + testParseIdentifiersWith(t, SoftKeywords, func(t *testing.T, _ string, err error) { require.Empty(t, err) }) } diff --git a/runtime/parser/keyword.go b/runtime/parser/keyword.go index 401c351c70..e7b4f4f3de 100644 --- a/runtime/parser/keyword.go +++ b/runtime/parser/keyword.go @@ -168,9 +168,9 @@ var allKeywords = []string{ KeywordIs, } -// softKeywords are keywords that can be used as identifiers anywhere, +// SoftKeywords are keywords that can be used as identifiers anywhere, // without any restriction or ambiguity. -var softKeywords = []string{ +var SoftKeywords = []string{ KeywordFrom, KeywordAccount, KeywordAll, @@ -181,12 +181,12 @@ var softKeywords = []string{ KeywordType, } -var softKeywordsTable = mph.Build(softKeywords) +var softKeywordsTable = mph.Build(SoftKeywords) -// hardKeywords are restricted from being used as identifiers in certain places. +// HardKeywords are restricted from being used as identifiers in certain places. // i.e: places where ambiguity can exist, such as composite declaration names, function names, etc. // However, they are not restricted to be used as fields names, and many other places. -var hardKeywords = filter( +var HardKeywords = filter( allKeywords, func(keyword string) bool { _, ok := softKeywordsTable.Lookup(keyword) @@ -194,7 +194,7 @@ var hardKeywords = filter( }, ) -var hardKeywordsTable = mph.Build(hardKeywords) +var hardKeywordsTable = mph.Build(HardKeywords) func IsHardKeyword(identifier string) bool { _, ok := hardKeywordsTable.Lookup(identifier) diff --git a/runtime/parser/statement_test.go b/runtime/parser/statement_test.go index 996cec1c3b..02596a8980 100644 --- a/runtime/parser/statement_test.go +++ b/runtime/parser/statement_test.go @@ -2690,7 +2690,7 @@ func TestSoftKeywordsInStatement(t *testing.T) { }) } - for _, keyword := range softKeywords { + for _, keyword := range SoftKeywords { // it's not worth the additional complexity to support assigning to `remove` or `attach`-named // variables, so we just accept this as a parsing error if keyword == KeywordAttach || keyword == KeywordRemove {