Skip to content

Commit

Permalink
feat: optional trailing semicolons for contract/trait items
Browse files Browse the repository at this point in the history
  • Loading branch information
anton-trunov committed Aug 22, 2024
1 parent 2d67ad5 commit f0eaef3
Show file tree
Hide file tree
Showing 7 changed files with 207 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Constant evaluator now uses an interpreter: PR [#664](https://github.com/tact-lang/tact/pull/664). This increases the expression simplification capabilities of the constant evaluator to expressions that include:
- Calls to user-defined functions.
- References to declared global constants.
- Allow omitting semicolons in contract/trait declarations and definitions: PR [#718](https://github.com/tact-lang/tact/pull/718)

### Fixed

Expand Down
198 changes: 198 additions & 0 deletions src/grammar/__snapshots__/grammar.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,112 @@ exports[`grammar should parse case-35 1`] = `
}
`;
exports[`grammar should parse contract-optional-semicolon-for-last-const-def 1`] = `
{
"id": 7,
"imports": [],
"items": [
{
"attributes": [],
"declarations": [
{
"attributes": [],
"id": 5,
"initializer": {
"id": 4,
"kind": "number",
"loc": 42,
"value": 42n,
},
"kind": "constant_def",
"loc": const foo: Int = 42,
"name": {
"id": 2,
"kind": "id",
"loc": foo,
"text": "foo",
},
"type": {
"id": 3,
"kind": "type_id",
"loc": Int,
"text": "Int",
},
},
],
"id": 6,
"kind": "contract",
"loc": contract Test { const foo: Int = 42 },
"name": {
"id": 1,
"kind": "id",
"loc": Test,
"text": "Test",
},
"traits": [],
},
],
"kind": "module",
}
`;
exports[`grammar should parse contract-optional-semicolon-for-last-storage-var 1`] = `
{
"id": 8,
"imports": [],
"items": [
{
"attributes": [],
"declarations": [
{
"as": null,
"id": 6,
"initializer": null,
"kind": "field_decl",
"loc": m: map<Int, Int>,
"name": {
"id": 2,
"kind": "id",
"loc": m,
"text": "m",
},
"type": {
"id": 5,
"keyStorageType": null,
"keyType": {
"id": 3,
"kind": "type_id",
"loc": Int,
"text": "Int",
},
"kind": "map_type",
"loc": map<Int, Int>,
"valueStorageType": null,
"valueType": {
"id": 4,
"kind": "type_id",
"loc": Int,
"text": "Int",
},
},
},
],
"id": 7,
"kind": "contract",
"loc": contract Test { m: map<Int, Int> },
"name": {
"id": 1,
"kind": "id",
"loc": Test,
"text": "Test",
},
"traits": [],
},
],
"kind": "module",
}
`;
exports[`grammar should parse contract-with-const-override 1`] = `
{
"id": 7,
Expand Down Expand Up @@ -8243,6 +8349,98 @@ exports[`grammar should parse struct-msg-trailing-semicolon 1`] = `
}
`;
exports[`grammar should parse trait-optional-semicolon-for-last-const-decl 1`] = `
{
"id": 6,
"imports": [],
"items": [
{
"attributes": [],
"declarations": [
{
"attributes": [
{
"loc": abstract,
"type": "abstract",
},
],
"id": 4,
"kind": "constant_decl",
"loc": abstract const foo: Int,
"name": {
"id": 2,
"kind": "id",
"loc": foo,
"text": "foo",
},
"type": {
"id": 3,
"kind": "type_id",
"loc": Int,
"text": "Int",
},
},
],
"id": 5,
"kind": "trait",
"loc": trait Test { abstract const foo: Int },
"name": {
"id": 1,
"kind": "id",
"loc": Test,
"text": "Test",
},
"traits": [],
},
],
"kind": "module",
}
`;
exports[`grammar should parse trait-optional-semicolon-for-last-fun-decl 1`] = `
{
"id": 5,
"imports": [],
"items": [
{
"attributes": [],
"declarations": [
{
"attributes": [
{
"loc": abstract,
"type": "abstract",
},
],
"id": 3,
"kind": "function_decl",
"loc": abstract fun foo(),
"name": {
"id": 2,
"kind": "id",
"loc": foo,
"text": "foo",
},
"params": [],
"return": null,
},
],
"id": 4,
"kind": "trait",
"loc": trait Test { abstract fun foo() },
"name": {
"id": 1,
"kind": "id",
"loc": Test,
"text": "Test",
},
"traits": [],
},
],
"kind": "module",
}
`;
exports[`grammar should parse traits-inheritance-trailing-comma 1`] = `
{
"id": 20,
Expand Down
8 changes: 4 additions & 4 deletions src/grammar/grammar.ohm
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ Tact {
| override --override
| abstract --abstract

ConstantDefinition = ConstantAttribute* const id ":" Type "=" Expression ";"
ConstantDefinition = ConstantAttribute* const id ":" Type "=" Expression (";" | &"}")

ConstantDeclaration = ConstantAttribute* const id ":" Type ";"
ConstantDeclaration = ConstantAttribute* const id ":" Type (";" | &"}")

StructDecl = "struct" typeId "{" StructFields "}" --regular
| "message" ("(" integerLiteral ")")? typeId "{" StructFields "}" --message
Expand All @@ -65,7 +65,7 @@ Tact {
| ConstantDefinition
| ConstantDeclaration

StorageVar = FieldDecl ";"
StorageVar = FieldDecl (";" | &"}")

ContractInit = "init" Parameters "{" Statement* "}"

Expand All @@ -81,7 +81,7 @@ Tact {

FunctionDefinition = FunctionAttribute* fun id Parameters (":" Type)? "{" Statement* "}"

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

Parameters = "(" ListOf<Parameter,","> ","? ")"

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
contract Test { const foo: Int = 42 }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
contract Test { m: map<Int, Int> }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
trait Test { abstract const foo: Int }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
trait Test { abstract fun foo() }

0 comments on commit f0eaef3

Please sign in to comment.