1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

scanner: add support for @VMOD_FILE

This commit is contained in:
Delyan Angelov
2020-05-26 23:39:15 +03:00
parent bb48851092
commit 3cfdd2a4cd
3 changed files with 49 additions and 0 deletions

View File

@@ -7,6 +7,7 @@ import os
import v.token
import v.pref
import v.util
import v.vmod
const (
single_quote = `\'`
@@ -32,6 +33,7 @@ pub mut:
fn_name string // needed for @FN
mod_name string // needed for @MOD
struct_name string // needed for @STRUCT
vmod_file_content string // needed for @VMOD_FILE, contents of the file, *NOT its path*
is_print_line_on_error bool
is_print_colored_error bool
is_print_rel_paths_on_error bool
@@ -745,6 +747,7 @@ pub fn (mut s Scanner) scan() token.Token {
// @LINE => will be substituted with the V line number where it appears (as a string).
// @COLUMN => will be substituted with the column where it appears (as a string).
// @VHASH => will be substituted with the shortened commit hash of the V compiler (as a string).
// @VMOD_FILE => will be substituted with the contents of the nearest v.mod file (as a string).
// This allows things like this:
// println( 'file: ' + @FILE + ' | line: ' + @LINE + ' | fn: ' + @MOD + '.' + @FN)
// ... which is useful while debugging/tracing
@@ -773,6 +776,17 @@ pub fn (mut s Scanner) scan() token.Token {
if name == 'VHASH' {
return s.new_token(.string, util.vhash(), 6)
}
if name == 'VMOD_FILE' {
if s.vmod_file_content.len == 0 {
vmod_file_location := vmod.mod_file_cacher.get( os.dir( os.real_path(s.file_path) ) )
if vmod_file_location.vmod_file.len == 0 {
s.error('@VMOD_FILE can be used only in projects, that have v.mod file')
}
vmod_content := os.read_file(vmod_file_location.vmod_file) or {''}
s.vmod_file_content = vmod_content
}
return s.new_token(.string, s.vmod_file_content, 10)
}
if !token.is_key(name) {
s.error('@ must be used before keywords (e.g. `@type string`)')
}

View File

@@ -82,3 +82,12 @@ fn test_scan() {
}
fn test_vmod_file() {
content := @VMOD_FILE
assert content.len > 0
assert content.contains('Module {')
assert content.contains('name:')
assert content.contains('version:')
assert content.contains('description:')
}