Skip to content
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

refactor(grammar): parameters, arguments and trait lists #422

Merged
merged 2 commits into from
Jun 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- The JSON schema for `tact.config.json` has been moved to the `json-schemas` project folder: PR [#404](https://github.com/tact-lang/tact/pull/404)
- Allow underscores as unused variable identifiers: PR [#338](https://github.com/tact-lang/tact/pull/338)
- The default compilation mode does decompile BoC files anymore, to additionally perform decompilation at the end of the pipeline, set the `fullWithDecompilation` mode in the `mode` project properties of `tact.config.json`: PR [#417](https://github.com/tact-lang/tact/pull/417)
- Trait lists, parameters and arguments in the Tact grammar were assigned their own names in the grammar for better readability and code deduplication: PR [#422](https://github.com/tact-lang/tact/pull/422)

### Fixed

Expand Down
4 changes: 2 additions & 2 deletions src/grammar/__snapshots__/grammar.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Line 1, col 19:
`;

exports[`grammar should fail expr-fun-call-trailing-comma-no-args 1`] = `
"<unknown>:6:14: Empty parameter list should not have a dangling comma.
"<unknown>:6:14: Empty argument list should not have a dangling comma.
Line 6, col 14:
5 | fun b(): Int {
> 6 | return a(,);
Expand All @@ -39,7 +39,7 @@ Line 6, col 14:
`;

exports[`grammar should fail expr-method-call-trailing-comma-no-args 1`] = `
"<unknown>:2:24: Empty parameter list should not have a dangling comma.
"<unknown>:2:24: Empty argument list should not have a dangling comma.
Line 2, col 24:
1 | fun another() {
> 2 | return 42.toString(,);
Expand Down
24 changes: 15 additions & 9 deletions src/grammar/grammar.ohm
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Tact {

ModuleConstant = ConstantDefinition

NativeFunctionDecl = "@name" "(" funcId ")" FunctionAttribute* native id "(" ListOf<Parameter,","> ","? ")" (":" Type)? ";"
NativeFunctionDecl = "@name" "(" funcId ")" FunctionAttribute* native id Parameters (":" Type)? ";"

Type = typeId "?" --optional
| typeId --regular
Expand All @@ -42,15 +42,17 @@ Tact {
StructField = FieldDecl
StructFields = ListOf<StructField, ";"> ";"?

Contract = ContractAttribute* contract id (with NonemptyListOf<id,","> ","?)? "{" ContractItemDecl* "}"
Contract = ContractAttribute* contract id (with InheritedTraits)? "{" ContractItemDecl* "}"

ContractItemDecl = ContractInit
| StorageVar
| Receiver
| FunctionDefinition
| ConstantDefinition

Trait = ContractAttribute* trait id (with NonemptyListOf<id,","> ","?)? "{" TraitItemDecl* "}"
Trait = ContractAttribute* trait id (with InheritedTraits)? "{" TraitItemDecl* "}"

InheritedTraits = NonemptyListOf<id,","> ","?

TraitItemDecl = StorageVar
| Receiver
Expand All @@ -61,7 +63,7 @@ Tact {

StorageVar = FieldDecl ";"

ContractInit = "init" "(" ListOf<Parameter,","> ","? ")" "{" Statement* "}"
ContractInit = "init" Parameters "{" Statement* "}"

ContractAttribute = "@interface" "(" stringLiteral ")" --interface

Expand All @@ -73,9 +75,11 @@ Tact {
| inline --inline
| abstract --abstract

FunctionDefinition = FunctionAttribute* fun id "(" ListOf<Parameter,","> ","? ")" (":" Type)? "{" Statement* "}"
FunctionDefinition = FunctionAttribute* fun id Parameters (":" Type)? "{" Statement* "}"

FunctionDeclaration = FunctionAttribute* fun id Parameters (":" Type)? ";"

FunctionDeclaration = FunctionAttribute* fun id "(" ListOf<Parameter,","> ","? ")" (":" Type)? ";"
Parameters = "(" ListOf<Parameter,","> ","? ")"

Parameter = id ":" Type

Expand Down Expand Up @@ -198,17 +202,19 @@ Tact {

ExpressionFieldAccess = ExpressionPrimary "." id ~"("

ExpressionMethodCall = ExpressionPrimary "." id "(" ListOf<Expression, ","> ","? ")"
ExpressionMethodCall = ExpressionPrimary "." id Arguments

ExpressionStructInstance = typeId "{" ListOf<StructFieldInitializer, ","> ","? "}"

ExpressionStaticCall = id "(" ListOf<Expression, ","> ","? ")"
ExpressionStaticCall = id Arguments

ExpressionInitOf = initOf id "(" ListOf<Expression, ","> ","? ")"
ExpressionInitOf = initOf id Arguments

StructFieldInitializer = id ":" Expression --full
| id --punned // shorthand for `id: id`

Arguments = "(" ListOf<Expression, ","> ","? ")"

// Type identifiers
typeId = letterAsciiUC typeIdPart*

Expand Down
Loading
Loading