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

feat(grammar): semicolon can be omitted for the last statement in block #434

Merged
merged 2 commits into from
Jun 18, 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 @@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- 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)
- The semicolon (`;`) terminating a statement is optional if the statement is the last one in the statement block: PR [#434](https://github.com/tact-lang/tact/pull/434)

### Fixed

Expand Down
247 changes: 244 additions & 3 deletions src/grammar/__snapshots__/grammar.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ Line 2, col 14:
`;

exports[`grammar should fail literal-no-underscores-if-leading-zero 1`] = `
"<unknown>:2:15: Syntax error: expected ";"
"<unknown>:2:15: Syntax error: expected "}" or ";"
Line 2, col 15:
1 | fun test_fun(): Int {
> 2 | return 012_3;
Expand All @@ -229,7 +229,7 @@ Line 2, col 15:
`;

exports[`grammar should fail literal-non-binary-digits 1`] = `
"<unknown>:2:15: Syntax error: expected ";"
"<unknown>:2:15: Syntax error: expected "}" or ";"
Line 2, col 15:
1 | fun test_fun(): Int {
> 2 | return 0b123;
Expand All @@ -239,7 +239,7 @@ Line 2, col 15:
`;

exports[`grammar should fail literal-underscore-after-leading-zero 1`] = `
"<unknown>:2:13: Syntax error: expected ";"
"<unknown>:2:13: Syntax error: expected "}" or ";"
Line 2, col 13:
1 | fun test_fun(): Int {
> 2 | return 0_123;
Expand Down Expand Up @@ -4937,6 +4937,247 @@ exports[`grammar should parse stmt-if-else 1`] = `
}
`;

exports[`grammar should parse stmt-optional-semicolon-for-last-statement 1`] = `
{
"entries": [
{
"args": [],
"attributes": [],
"id": 17,
"kind": "def_function",
"name": "test1",
"origin": "user",
"ref": fun test1() {
let i: Int = 1;
while(i >= 10 || i <= 100) { i += 1 }
let i = 42
},
"return": null,
"statements": [
{
"expression": {
"id": 2,
"kind": "number",
"ref": 1,
"value": 1n,
},
"id": 3,
"kind": "statement_let",
"name": "i",
"ref": let i: Int = 1;,
"type": {
"id": 1,
"kind": "type_ref_simple",
"name": "Int",
"optional": false,
"ref": Int,
},
},
{
"condition": {
"id": 10,
"kind": "op_binary",
"left": {
"id": 6,
"kind": "op_binary",
"left": {
"id": 4,
"kind": "id",
"ref": i,
"value": "i",
},
"op": ">=",
"ref": i >= 10,
"right": {
"id": 5,
"kind": "number",
"ref": 10,
"value": 10n,
},
},
"op": "||",
"ref": i >= 10 || i <= 100,
"right": {
"id": 9,
"kind": "op_binary",
"left": {
"id": 7,
"kind": "id",
"ref": i,
"value": "i",
},
"op": "<=",
"ref": i <= 100,
"right": {
"id": 8,
"kind": "number",
"ref": 100,
"value": 100n,
},
},
},
"id": 14,
"kind": "statement_while",
"ref": while(i >= 10 || i <= 100) { i += 1 },
"statements": [
{
"expression": {
"id": 12,
"kind": "number",
"ref": 1,
"value": 1n,
},
"id": 13,
"kind": "statement_augmentedassign",
"op": "+",
"path": [
{
"id": 11,
"kind": "lvalue_ref",
"name": "i",
"ref": i,
},
],
"ref": i += 1,
},
],
},
{
"expression": {
"id": 15,
"kind": "number",
"ref": 42,
"value": 42n,
},
"id": 16,
"kind": "statement_let",
"name": "i",
"ref": let i = 42,
"type": null,
},
],
},
{
"args": [],
"attributes": [],
"id": 19,
"kind": "def_function",
"name": "test2",
"origin": "user",
"ref": fun test2() { return },
"return": null,
"statements": [
{
"expression": null,
"id": 18,
"kind": "statement_return",
"ref": return,
},
],
},
{
"args": [],
"attributes": [],
"id": 23,
"kind": "def_function",
"name": "test3",
"origin": "user",
"ref": fun test3(): Int { return 42 },
"return": {
"id": 20,
"kind": "type_ref_simple",
"name": "Int",
"optional": false,
"ref": Int,
},
"statements": [
{
"expression": {
"id": 21,
"kind": "number",
"ref": 42,
"value": 42n,
},
"id": 22,
"kind": "statement_return",
"ref": return 42,
},
],
},
{
"args": [],
"attributes": [],
"id": 33,
"kind": "def_function",
"name": "test4",
"origin": "user",
"ref": fun test4(): Int {
do { 21 + 21 } until (true && true)
},
"return": {
"id": 24,
"kind": "type_ref_simple",
"name": "Int",
"optional": false,
"ref": Int,
},
"statements": [
{
"condition": {
"id": 27,
"kind": "op_binary",
"left": {
"id": 25,
"kind": "boolean",
"ref": true,
"value": true,
},
"op": "&&",
"ref": true && true,
"right": {
"id": 26,
"kind": "boolean",
"ref": true,
"value": true,
},
},
"id": 32,
"kind": "statement_until",
"ref": do { 21 + 21 } until (true && true),
"statements": [
{
"expression": {
"id": 30,
"kind": "op_binary",
"left": {
"id": 28,
"kind": "number",
"ref": 21,
"value": 21n,
},
"op": "+",
"ref": 21 + 21,
"right": {
"id": 29,
"kind": "number",
"ref": 21,
"value": 21n,
},
},
"id": 31,
"kind": "statement_expression",
"ref": 21 + 21,
},
],
},
],
},
],
"id": 34,
"kind": "program",
}
`;

exports[`grammar should parse stmt-while-loop 1`] = `
{
"entries": [
Expand Down
13 changes: 7 additions & 6 deletions src/grammar/grammar.ohm
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ Tact {
| external "(" Parameter? ")" "{" Statement* "}" --externalRegular
| external "(" stringLiteral ")" "{" Statement* "}" --externalComment

// Statements
Statement = StatementLet
| StatementBlock
| StatementReturn
Expand All @@ -104,13 +103,15 @@ Tact {

StatementBlock = "{" Statement* "}"

StatementLet = let id (":" Type)? "=" Expression ";"
// Do not require the terminating semicolon
// if this is the last statement in a block
StatementLet = let id (":" Type)? "=" Expression (";" | &"}")

StatementReturn = return Expression? ";"
StatementReturn = return Expression? (";" | &"}")

StatementExpression = Expression ";"
StatementExpression = Expression (";" | &"}")

StatementAssign = LValue ("=" | "+=" | "-=" | "*=" | "/=" | "%=" | "|=" | "&=" | "^=") Expression ";"
StatementAssign = LValue ("=" | "+=" | "-=" | "*=" | "/=" | "%=" | "|=" | "&=" | "^=") Expression (";" | &"}")

StatementCondition = if Expression "{" Statement* "}" ~else --noElse
| if Expression "{" Statement* "}" else "{" Statement* "}" --withElse
Expand All @@ -120,7 +121,7 @@ Tact {

StatementRepeat = repeat "(" Expression ")" "{" Statement* "}"

StatementUntil = do "{" Statement* "}" until "(" Expression ")" ";"
StatementUntil = do "{" Statement* "}" until "(" Expression ")" (";" | &"}")

// making the catch clause optional using Ohm's `?` does not make sense
// because Ohm will create _independent_ optional grammar nodes which
Expand Down
15 changes: 10 additions & 5 deletions src/grammar/grammar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ semantics.addOperation<ASTNode>("astOfStatement", {
optType,
_equals,
expression,
_semicolon,
_optSemicolonIfLastStmtInBlock,
) {
checkVariableName(id.sourceString, createRef(id));

Expand All @@ -549,7 +549,7 @@ semantics.addOperation<ASTNode>("astOfStatement", {
ref: createRef(this),
});
},
StatementReturn(_returnKwd, optExpression, _semicolon) {
StatementReturn(_returnKwd, optExpression, _optSemicolonIfLastStmtInBlock) {
return createNode({
kind: "statement_return",
expression: unwrapOptNode(optExpression, (e) =>
Expand All @@ -558,14 +558,19 @@ semantics.addOperation<ASTNode>("astOfStatement", {
ref: createRef(this),
});
},
StatementExpression(expression, _semicolon) {
StatementExpression(expression, _optSemicolonIfLastStmtInBlock) {
return createNode({
kind: "statement_expression",
expression: expression.astOfExpression(),
ref: createRef(this),
});
},
StatementAssign(lvalue, operator, expression, _semicolon) {
StatementAssign(
lvalue,
operator,
expression,
_optSemicolonIfLastStmtInBlock,
) {
if (operator.sourceString === "=") {
return createNode({
kind: "statement_assign",
Expand Down Expand Up @@ -701,7 +706,7 @@ semantics.addOperation<ASTNode>("astOfStatement", {
_lparen,
condition,
_rparen,
_semicolon,
_optSemicolonIfLastStmtInBlock,
) {
return createNode({
kind: "statement_until",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// what is covered: assignment, let
fun test1() {
let i: Int = 1;
while(i >= 10 || i <= 100) { i += 1 }
let i = 42
}

// what is covered: return without expression
fun test2() { return }

// what is covered: return with expression
fun test3(): Int { return 42 }

// what is covered: do-until, expression statement
fun test4(): Int {
do { 21 + 21 } until (true && true)
}
Loading