From 25a3873019ea17f2af36d67e77dc8b206b46479a Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Fri, 5 Feb 2021 16:30:58 +0200 Subject: [PATCH] parser,checker: support @METHOD, replaced by 'ReceiverType.MethodName' --- doc/docs.md | 1 + vlib/v/checker/checker.v | 9 +++++++++ vlib/v/parser/comptime.v | 1 + vlib/v/tests/comptime_at_test.v | 2 ++ vlib/v/token/token.v | 7 ++++--- 5 files changed, 17 insertions(+), 3 deletions(-) diff --git a/doc/docs.md b/doc/docs.md index c8cadcbced..05aa69c27b 100644 --- a/doc/docs.md +++ b/doc/docs.md @@ -3516,6 +3516,7 @@ V also gives your code access to a set of pseudo string variables, that are substituted at compile time: - `@FN` => replaced with the name of the current V function +- `@METHOD` => replaced with ReceiverType.MethodName - `@MOD` => replaced with the name of the current V module - `@STRUCT` => replaced with the name of the current V struct - `@FILE` => replaced with the path of the V source file diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 878696add8..f471a86869 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -3911,6 +3911,15 @@ fn (mut c Checker) at_expr(mut node ast.AtExpr) table.Type { .fn_name { node.val = c.cur_fn.name.all_after_last('.') } + .method_name { + fname := c.cur_fn.name.all_after_last('.') + if c.cur_fn.is_method { + node.val = c.table.type_to_str(c.cur_fn.receiver.typ).all_after_last('.') + '.' + + fname + } else { + node.val = fname + } + } .mod_name { node.val = c.cur_fn.mod } diff --git a/vlib/v/parser/comptime.v b/vlib/v/parser/comptime.v index ed0aa5e8e3..7cb8318394 100644 --- a/vlib/v/parser/comptime.v +++ b/vlib/v/parser/comptime.v @@ -255,6 +255,7 @@ fn (mut p Parser) at() ast.AtExpr { name := p.tok.lit kind := match name { '@FN' { token.AtKind.fn_name } + '@METHOD' { token.AtKind.method_name } '@MOD' { token.AtKind.mod_name } '@STRUCT' { token.AtKind.struct_name } '@VEXE' { token.AtKind.vexe_path } diff --git a/vlib/v/tests/comptime_at_test.v b/vlib/v/tests/comptime_at_test.v index 7590348810..bd9d15ab39 100644 --- a/vlib/v/tests/comptime_at_test.v +++ b/vlib/v/tests/comptime_at_test.v @@ -51,11 +51,13 @@ fn (mut t TestFn) tst_1() { fn (mut t TestFn) tst_2(cb fn (int)) { assert @FN == 'tst_2' + assert @METHOD == 'TestFn.tst_2' cb(1) } fn fn_name_mod_level() { assert @FN == 'fn_name_mod_level' + assert @METHOD == 'fn_name_mod_level' } fn fn_name_mod_level_high_order(cb fn (int)) { diff --git a/vlib/v/token/token.v b/vlib/v/token/token.v index 8ea77f14af..3e10def019 100644 --- a/vlib/v/token/token.v +++ b/vlib/v/token/token.v @@ -136,6 +136,7 @@ const ( ) // @FN => will be substituted with the name of the current V function +// @METHOD => will be substituted with ReceiverType.MethodName // @MOD => will be substituted with the name of the current V module // @STRUCT => will be substituted with the name of the current V struct // @VEXE => will be substituted with the path to the V compiler @@ -153,6 +154,7 @@ const ( pub enum AtKind { unknown fn_name + method_name mod_name struct_name vexe_path @@ -164,9 +166,8 @@ pub enum AtKind { } pub const ( - valid_at_tokens = ['@FN', '@MOD', '@STRUCT', '@VEXE', '@FILE', '@LINE', '@COLUMN', '@VHASH', - '@VMOD_FILE', - ] + valid_at_tokens = ['@FN', '@METHOD', '@MOD', '@STRUCT', '@VEXE', '@FILE', '@LINE', '@COLUMN', + '@VHASH', '@VMOD_FILE'] ) // build_keys genereates a map with keywords' string values: