Skip to content

Commit

Permalink
fix: add function attributes to AST nodes traversal (#932)
Browse files Browse the repository at this point in the history
  • Loading branch information
anton-trunov authored Oct 9, 2024
1 parent 0666a8c commit 2024557
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 14 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- New CSpell dictionaries: TVM instructions and adjusted list of Fift words: PR [#881](https://github.com/tact-lang/tact/pull/881)
- Docs: the `description` property to the frontmatter of the each page for better SEO: PR [#916](https://github.com/tact-lang/tact/pull/916)
- Docs: Google Analytics tags per every page: PR [#921](https://github.com/tact-lang/tact/pull/921)
- Ability to specify a compile-time method ID expression for getters: PR [#922](https://github.com/tact-lang/tact/pull/922)
- Ability to specify a compile-time method ID expression for getters: PR [#922](https://github.com/tact-lang/tact/pull/922) and PR [#932](https://github.com/tact-lang/tact/pull/932)

### Changed

Expand Down
10 changes: 10 additions & 0 deletions src/grammar/__snapshots__/grammar.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,7 @@ exports[`grammar should parse contract-getter-with-method-id 1`] = `
{
"attributes": [
{
"kind": "function_attribute",
"loc": get(crc32("crc32") + 42 & 0x3ffff | 0x4000),
"methodId": {
"id": 10,
Expand Down Expand Up @@ -1493,6 +1494,7 @@ exports[`grammar should parse contract-with-trait 1`] = `
{
"attributes": [
{
"kind": "function_attribute",
"loc": virtual,
"type": "virtual",
},
Expand Down Expand Up @@ -1572,6 +1574,7 @@ exports[`grammar should parse contract-with-trait 1`] = `
{
"attributes": [
{
"kind": "function_attribute",
"loc": override,
"type": "override",
},
Expand Down Expand Up @@ -1679,6 +1682,7 @@ exports[`grammar should parse contract-with-trait-string-literal 1`] = `
{
"attributes": [
{
"kind": "function_attribute",
"loc": virtual,
"type": "virtual",
},
Expand Down Expand Up @@ -1758,6 +1762,7 @@ exports[`grammar should parse contract-with-trait-string-literal 1`] = `
{
"attributes": [
{
"kind": "function_attribute",
"loc": override,
"type": "override",
},
Expand Down Expand Up @@ -3573,6 +3578,7 @@ exports[`grammar should parse items-asm-funs 1`] = `
{
"attributes": [
{
"kind": "function_attribute",
"loc": extends,
"type": "extends",
},
Expand Down Expand Up @@ -3639,6 +3645,7 @@ exports[`grammar should parse items-asm-funs 1`] = `
{
"attributes": [
{
"kind": "function_attribute",
"loc": extends,
"type": "extends",
},
Expand Down Expand Up @@ -3715,6 +3722,7 @@ exports[`grammar should parse items-asm-funs 1`] = `
{
"attributes": [
{
"kind": "function_attribute",
"loc": extends,
"type": "extends",
},
Expand Down Expand Up @@ -4370,6 +4378,7 @@ exports[`grammar should parse items-method-def-initof-trailing-comma-shifts 1`]
{
"attributes": [
{
"kind": "function_attribute",
"loc": extends,
"type": "extends",
},
Expand Down Expand Up @@ -9735,6 +9744,7 @@ exports[`grammar should parse trait-optional-semicolon-for-last-fun-decl 1`] = `
{
"attributes": [
{
"kind": "function_attribute",
"loc": abstract,
"type": "abstract",
},
Expand Down
20 changes: 13 additions & 7 deletions src/grammar/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -633,13 +633,18 @@ export type AstContractAttribute = {
};

export type AstFunctionAttribute =
| { type: "get"; methodId: AstExpression | null; loc: SrcInfo }
| { type: "mutates"; loc: SrcInfo }
| { type: "extends"; loc: SrcInfo }
| { type: "virtual"; loc: SrcInfo }
| { type: "abstract"; loc: SrcInfo }
| { type: "override"; loc: SrcInfo }
| { type: "inline"; loc: SrcInfo };
| {
kind: "function_attribute";
type: "get";
methodId: AstExpression | null;
loc: SrcInfo;
}
| { kind: "function_attribute"; type: "mutates"; loc: SrcInfo }
| { kind: "function_attribute"; type: "extends"; loc: SrcInfo }
| { kind: "function_attribute"; type: "virtual"; loc: SrcInfo }
| { kind: "function_attribute"; type: "abstract"; loc: SrcInfo }
| { kind: "function_attribute"; type: "override"; loc: SrcInfo }
| { kind: "function_attribute"; type: "inline"; loc: SrcInfo };

export type AstTypedParameter = {
kind: "typed_parameter";
Expand Down Expand Up @@ -685,6 +690,7 @@ export type AstNode =
| AstFieldDecl
| AstTypedParameter
| AstFunctionDef
| AstFunctionAttribute
| AstAsmFunctionDef
| AstFunctionDecl
| AstModule
Expand Down
37 changes: 31 additions & 6 deletions src/grammar/grammar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -585,28 +585,53 @@ semantics.addOperation<string>("astOfAsmInstruction", {
semantics.addOperation<AstFunctionAttribute>("astOfFunctionAttributes", {
FunctionAttribute_getter(_getKwd, _optLparen, optMethodId, _optRparen) {
return {
kind: "function_attribute",
type: "get",
methodId: unwrapOptNode(optMethodId, (e) => e.astOfExpression()),
loc: createRef(this),
};
},
FunctionAttribute_extends(_) {
return { type: "extends", loc: createRef(this) };
return {
kind: "function_attribute",
type: "extends",
loc: createRef(this),
};
},
FunctionAttribute_mutates(_) {
return { type: "mutates", loc: createRef(this) };
return {
kind: "function_attribute",
type: "mutates",
loc: createRef(this),
};
},
FunctionAttribute_override(_) {
return { type: "override", loc: createRef(this) };
return {
kind: "function_attribute",
type: "override",
loc: createRef(this),
};
},
FunctionAttribute_inline(_) {
return { type: "inline", loc: createRef(this) };
return {
kind: "function_attribute",
type: "inline",
loc: createRef(this),
};
},
FunctionAttribute_virtual(_) {
return { type: "virtual", loc: createRef(this) };
return {
kind: "function_attribute",
type: "virtual",
loc: createRef(this),
};
},
FunctionAttribute_abstract(_) {
return { type: "abstract", loc: createRef(this) };
return {
kind: "function_attribute",
type: "abstract",
loc: createRef(this),
};
},
});

Expand Down
22 changes: 22 additions & 0 deletions src/grammar/iterators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ export function traverse(node: AstNode, callback: (node: AstNode) => void) {
traverse(node.name, callback);
break;
case "function_def":
node.attributes.forEach((attr) => {
traverse(attr, callback);
});
traverse(node.name, callback);
if (node.return) traverse(node.return, callback);
node.params.forEach((e) => {
Expand All @@ -48,6 +51,9 @@ export function traverse(node: AstNode, callback: (node: AstNode) => void) {
});
break;
case "function_decl":
node.attributes.forEach((attr) => {
traverse(attr, callback);
});
traverse(node.name, callback);
if (node.return) traverse(node.return, callback);
node.params.forEach((e) => {
Expand All @@ -62,6 +68,22 @@ export function traverse(node: AstNode, callback: (node: AstNode) => void) {
});
if (node.return) traverse(node.return, callback);
break;
case "function_attribute":
switch (node.type) {
case "get":
{
if (node.methodId) traverse(node.methodId, callback);
}
break;
case "mutates":
case "extends":
case "virtual":
case "abstract":
case "override":
case "inline":
break;
}
break;
case "constant_def":
traverse(node.name, callback);
traverse(node.type, callback);
Expand Down
Loading

0 comments on commit 2024557

Please sign in to comment.