parser,checker: support @METHOD, replaced by 'ReceiverType.MethodName'

pull/8591/head
Delyan Angelov 2021-02-05 16:30:58 +02:00
parent d30f94507c
commit 25a3873019
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
5 changed files with 17 additions and 3 deletions

View File

@ -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

View File

@ -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
}

View File

@ -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 }

View File

@ -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)) {

View File

@ -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: