From 90c2c5b8a4adc4d7de1dba952da9581f6d76f1ae Mon Sep 17 00:00:00 2001 From: Seven Du Date: Mon, 5 Sep 2022 22:00:35 +0800 Subject: [PATCH] token: add @FILE_LEN (#15661) --- doc/docs.md | 3 ++- vlib/v/checker/checker.v | 3 +++ vlib/v/parser/comptime.v | 1 + vlib/v/tests/comptime_at_test.v | 6 ++++++ vlib/v/token/token.v | 3 ++- 5 files changed, 14 insertions(+), 2 deletions(-) diff --git a/doc/docs.md b/doc/docs.md index b6aee4da1..bef743f6f 100644 --- a/doc/docs.md +++ b/doc/docs.md @@ -5580,8 +5580,9 @@ that are substituted at compile time: - `@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 +- `@FILE` => replaced with the absolute path of the V source file - `@LINE` => replaced with the V line number where it appears (as a string). +- `@FILE_LINE` => like `@FILE:@LINE`, but the file part is a relative path - `@COLUMN` => replaced with the column where it appears (as a string). - `@VEXE` => replaced with the path to the V compiler - `@VEXEROOT` => will be substituted with the *folder*, diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 27baec9a7..41a81f0af 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -2643,6 +2643,9 @@ fn (mut c Checker) at_expr(mut node ast.AtExpr) ast.Type { .line_nr { node.val = (node.pos.line_nr + 1).str() } + .file_path_line_nr { + node.val = os.file_name(c.file.path) + ':' + (node.pos.line_nr + 1).str() + } .column_nr { node.val = (node.pos.col + 1).str() } diff --git a/vlib/v/parser/comptime.v b/vlib/v/parser/comptime.v index 335c1c360..0da9e4651 100644 --- a/vlib/v/parser/comptime.v +++ b/vlib/v/parser/comptime.v @@ -319,6 +319,7 @@ fn (mut p Parser) at() ast.AtExpr { '@STRUCT' { token.AtKind.struct_name } '@FILE' { token.AtKind.file_path } '@LINE' { token.AtKind.line_nr } + '@FILE_LINE' { token.AtKind.file_path_line_nr } '@COLUMN' { token.AtKind.column_nr } '@VHASH' { token.AtKind.vhash } '@VMOD_FILE' { token.AtKind.vmod_file } diff --git a/vlib/v/tests/comptime_at_test.v b/vlib/v/tests/comptime_at_test.v index bd9d15ab3..c8f463f38 100644 --- a/vlib/v/tests/comptime_at_test.v +++ b/vlib/v/tests/comptime_at_test.v @@ -71,6 +71,12 @@ fn test_at_file() { assert f == 'comptime_at_test.v' } +fn test_at_file_len() { + // Test @FILE_LINE + line1, line2 := '${@LINE}', '${@FILE_LINE}' + assert os.file_name(@FILE) + ':' + line1.str() == line2 +} + fn test_at_fn() { // Test @FN assert @FN == 'test_at_fn' diff --git a/vlib/v/token/token.v b/vlib/v/token/token.v index bde02d6e7..4265d67d6 100644 --- a/vlib/v/token/token.v +++ b/vlib/v/token/token.v @@ -173,6 +173,7 @@ pub enum AtKind { vmodroot_path vroot_path // obsolete vexeroot_path + file_path_line_nr } pub const ( @@ -181,7 +182,7 @@ pub const ( .unsigned_right_shift_assign] valid_at_tokens = ['@VROOT', '@VMODROOT', '@VEXEROOT', '@FN', '@METHOD', '@MOD', '@STRUCT', - '@VEXE', '@FILE', '@LINE', '@COLUMN', '@VHASH', '@VMOD_FILE'] + '@VEXE', '@FILE', '@LINE', '@COLUMN', '@VHASH', '@VMOD_FILE', '@FILE_LINE'] token_str = build_token_str() -- 2.30.2