From d86aeab03534d8ccd2407d2391d79bd204980adf Mon Sep 17 00:00:00 2001 From: Anton Trunov Date: Wed, 26 Jun 2024 01:42:50 +0400 Subject: [PATCH] fix: parsing of non-decimal message opcodes (#481) --- CHANGELOG.md | 1 + cspell.json | 2 ++ src/grammar/grammar.ts | 15 +++++++++++---- src/test/codegen/all-contracts.tact | 3 ++- src/test/codegen/message-opcode-parsing.tact | 11 +++++++++++ 5 files changed, 27 insertions(+), 5 deletions(-) create mode 100644 src/test/codegen/message-opcode-parsing.tact diff --git a/CHANGELOG.md b/CHANGELOG.md index 78f1e532e..9205a5682 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Name clashes with FunC keywords in struct constructor function parameters: PR [#467](https://github.com/tact-lang/tact/issues/467) - Error messages for traversing non-path-expressions in `foreach`-loops : PR [#479](https://github.com/tact-lang/tact/pull/479) - Shadowing of trait constants by contract storage variables: PR [#480](https://github.com/tact-lang/tact/pull/480) +- Parsing of non-decimal message opcodes: PR [#481](https://github.com/tact-lang/tact/pull/481) ## [1.4.0] - 2024-06-21 diff --git a/cspell.json b/cspell.json index 58ce24ad0..cc049eb5f 100644 --- a/cspell.json +++ b/cspell.json @@ -42,6 +42,8 @@ "Korshakov", "nocheck", "noexcept", + "Nonterminal", + "nonterminal", "Neovim", "Offchain", "Parens", diff --git a/src/grammar/grammar.ts b/src/grammar/grammar.ts index 237680883..cb848ce0f 100644 --- a/src/grammar/grammar.ts +++ b/src/grammar/grammar.ts @@ -16,7 +16,7 @@ import { } from "./ast"; import { throwParseError, throwCompilationError } from "../errors"; import { checkVariableName } from "./checkVariableName"; -import { Node, IterationNode } from "ohm-js"; +import { Node, IterationNode, NonterminalNode } from "ohm-js"; import { TypeOrigin } from "../types/types"; import { checkFunctionAttributes } from "./checkFunctionAttributes"; import { checkConstAttributes } from "./checkConstAttributes"; @@ -112,7 +112,7 @@ semantics.addOperation("astOfModuleItem", { StructDecl_message( _messageKwd, _optLparen, - optId, + optIntMsgId, _optRparen, typeId, _lbrace, @@ -125,7 +125,9 @@ semantics.addOperation("astOfModuleItem", { origin: ctx!.origin, name: typeId.sourceString, fields: fields.astsOfList(), - prefix: unwrapOptNode(optId, (id) => parseInt(id.sourceString)), + prefix: unwrapOptNode(optIntMsgId, (number) => + Number(bigintOfIntLiteral(number)), + ), message: true, ref: createRef(this), }); @@ -819,13 +821,18 @@ semantics.addOperation("astOfType", { }, }); +// handles binary, octal, decimal and hexadecimal integer literals +function bigintOfIntLiteral(litString: NonterminalNode): bigint { + return BigInt(litString.sourceString.replaceAll("_", "")); +} + // Expressions semantics.addOperation("astOfExpression", { // Literals integerLiteral(number) { return createNode({ kind: "number", - value: BigInt(number.sourceString.replaceAll("_", "")), + value: bigintOfIntLiteral(number), ref: createRef(this), }); // Parses dec, hex, and bin numbers }, diff --git a/src/test/codegen/all-contracts.tact b/src/test/codegen/all-contracts.tact index 0f82e7b9a..f63e2ad54 100644 --- a/src/test/codegen/all-contracts.tact +++ b/src/test/codegen/all-contracts.tact @@ -1,4 +1,5 @@ import "./empty-message.tact"; import "./large-contract.tact"; import "./struct-field-storage-annotation.tact"; -import "./struct-field-func-keywords-name-clash"; \ No newline at end of file +import "./struct-field-func-keywords-name-clash"; +import "./message-opcode-parsing.tact"; diff --git a/src/test/codegen/message-opcode-parsing.tact b/src/test/codegen/message-opcode-parsing.tact new file mode 100644 index 000000000..7860502a2 --- /dev/null +++ b/src/test/codegen/message-opcode-parsing.tact @@ -0,0 +1,11 @@ +message(0b101010) Binary {} +message(0o53) Octal {} +message(44) Decimal {} +message(0x2D) Hexadecimal {} + +contract Example { + receive(msg: Binary) { } + receive(msg: Octal) { } + receive(msg: Decimal) { } + receive(msg: Hexadecimal) { } +}