Skip to content

v.token #

Constants #

const (
	assign_tokens   = [Kind.assign, .plus_assign, .minus_assign, .mult_assign, .div_assign,
		.xor_assign, .mod_assign, .or_assign, .and_assign, .right_shift_assign, .left_shift_assign,
		.unsigned_right_shift_assign]

	valid_at_tokens = ['@VROOT', '@VMODROOT', '@VEXEROOT', '@FN', '@METHOD', '@MOD', '@STRUCT',
		'@VEXE', '@FILE', '@LINE', '@COLUMN', '@VHASH', '@VMOD_FILE']

	token_str       = build_token_str()

	keywords        = build_keys()

	matcher         = new_keywords_matcher<Kind>(keywords)
)

fn build_precedences #

fn build_precedences() []Precedence

fn is_decl #

fn is_decl(t Kind) bool

fn is_key #

fn is_key(key string) bool

fn kind_from_string #

fn kind_from_string(s string) ?Kind

fn kind_to_string #

fn kind_to_string(k Kind) string

fn new_keywords_matcher #

fn new_keywords_matcher<T>(kw_map map[string]T) KeywordsMatcher

enum AtKind #

enum AtKind {
	unknown
	fn_name
	method_name
	mod_name
	struct_name
	vexe_path
	file_path
	line_nr
	column_nr
	vhash
	vmod_file
	vmodroot_path
	vroot_path // obsolete
	vexeroot_path
}

@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 @FILE => will be substituted with the path of the V source file @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).
@VMODROOT => will be substituted with the folder where the nearest v.mod file is (as a string).
@VEXE => will be substituted with the path to the V compiler @VEXEROOT => will be substituted with the folder where the V executable is (as a string).
@VROOT => the old name for @VMODROOT; sometimes it was used as @VEXEROOT; Note: @VROOT is now deprecated, use either @VMODROOT or @VEXEROOT instead.
Note: @VEXEROOT & @VMODROOT are used for compilation options like this: #include "@VMODROOT/include/abc.h" #flag -L@VEXEROOT/thirdparty/libgc

The @XYZ tokens allow for code like this: println( 'file: ' + @FILE + ' | line: ' + @LINE + ' | fn: ' + @MOD + '.' + @FN) ... which is useful while debugging/tracing.

@<type> is allowed for keyword variable names. E.g. 'type'

enum Kind #

enum Kind {
	unknown
	eof
	name // user
	number // 123
	string // 'foo'
	str_inter // 'name=$user.name'
	chartoken // `A` - rune
	plus // +
	minus // -
	mul // *
	div // /
	mod // %
	xor // ^
	pipe // |
	inc // ++
	dec // --
	and // &&
	logical_or // ||
	not // !
	bit_not // ~
	question // ?
	comma // ,
	semicolon // ;
	colon // :
	arrow // <-
	amp // &
	hash // #
	dollar // $
	at // @
	str_dollar
	left_shift // <<
	right_shift // >>
	unsigned_right_shift // >>>
	not_in // !in
	not_is // !is
	assign // =
	decl_assign // :=
	plus_assign // +=
	minus_assign // -=
	div_assign // /=
	mult_assign // *=
	xor_assign // ^=
	mod_assign // %=
	or_assign // |=
	and_assign // &=
	right_shift_assign // <<=
	left_shift_assign // >>=
	unsigned_right_shift_assign // >>>=
	lcbr // {
	rcbr // }
	lpar // (
	rpar // )
	lsbr // [
	nilsbr // #[
	rsbr // ]
	eq // ==
	ne // !=
	gt // >
	lt // <
	ge // >=
	le // <=
	comment
	nl
	dot // .
	dotdot // ..
	ellipsis // ...
	keyword_beg
	key_as
	key_asm
	key_assert
	key_atomic
	key_break
	key_const
	key_continue
	key_defer
	key_else
	key_enum
	key_false
	key_for
	key_fn
	key_global
	key_go
	key_goto
	key_if
	key_import
	key_in
	key_interface
	key_is
	key_match
	key_module
	key_mut
	key_shared
	key_lock
	key_rlock
	key_none
	key_return
	key_select
	key_sizeof
	key_isreftype
	key_likely
	key_unlikely
	key_offsetof
	key_struct
	key_true
	key_type
	key_typeof
	key_dump
	key_orelse
	key_union
	key_pub
	key_static
	key_volatile
	key_unsafe
	keyword_end
	_end_
}

fn (Kind) is_assign #

fn (t Kind) is_assign() bool

fn (Kind) str #

fn (t Kind) str() string

note: used for some code generation, so no quoting

fn (Kind) is_relational #

fn (tok Kind) is_relational() bool

fn (Kind) is_start_of_type #

fn (k Kind) is_start_of_type() bool

fn (Kind) is_prefix #

fn (kind Kind) is_prefix() bool

fn (Kind) is_infix #

fn (kind Kind) is_infix() bool

fn (Kind) is_postfix #

fn (kind Kind) is_postfix() bool

enum Precedence #

enum Precedence {
	lowest
	cond // OR or AND
	in_as
	assign // =
	eq // == or !=
// less_greater // > or <
	sum // + - | ^
	product // * / << >> >>> &
// mod // %
	prefix // -X or !X
	postfix // ++ or --
	call // func(X) or foo.method(X)
	index // array[index], map[key]
}

Representation of highest and lowest precedence

struct KeywordsMatcher #

struct KeywordsMatcher {
mut:
	len_min int = 9999
	len_max int = -1
	words   [max_keyword_len][]WIndex
}

KeywordsMatcher provides a faster way of determinining whether a given name is a reserved word, by doing a comparison with only the keywords that have exactly the same length as name.
Benchmarking shows that with -prod, it is 20-25% slower in the worst case compared to just using token.keywords[name], but can be 20x faster in the case, where there is a length mismatch, and 2x-3x faster in most cases, where there is a match.
Without -prod, with tcc, using KeywordsMatcher is always faster (2x to 14x times), compared to using a hash of all the keywords.

fn (KeywordsMatcher) find #

fn (km &KeywordsMatcher) find(word string) int

find returns the int index, given a word, by doing a binary search on the sorted list of words for each bin

struct Pos #

struct Pos {
pub:
	len     int // length of the literal in the source
	line_nr int // the line number in the source where the token occured
	pos     int // the position of the token in scanner text
	col     int // the column in the source where the token occured
pub mut:
	last_line int // the line number where the ast object ends (used by vfmt)
}

fn (Pos) free #

unsafe
fn (mut p Pos) free()

fn (Pos) line_str #

fn (p Pos) line_str() string

fn (Pos) extend #

fn (pos Pos) extend(end Pos) Pos

fn (Pos) extend_with_last_line #

fn (pos Pos) extend_with_last_line(end Pos, last_line int) Pos

fn (Pos) update_last_line #

fn (mut pos Pos) update_last_line(last_line int)

struct Token #

struct Token {
pub:
	kind    Kind   // the token number/enum; for quick comparisons
	lit     string // literal representation of the token
	line_nr int    // the line number in the source where the token occured
	col     int    // the column in the source where the token occured
// name_idx int // name table index for O(1) lookup
	pos  int // the position of the token in scanner text
	len  int // length of the literal
	tidx int // the index of the token
}

fn (Token) debug #

fn (t Token) debug() string

fn (Token) is_scalar #

fn (tok Token) is_scalar() bool

is_scalar returns true if the token is a scalar

fn (Token) is_unary #

fn (tok Token) is_unary() bool

is_unary returns true if the token can be in a unary expression

fn (Token) pos #

fn (tok &Token) pos() Pos

fn (Token) precedence #

fn (tok Token) precedence() int

precedence returns a tokens precedence if defined, otherwise lowest_prec

fn (Token) str #

fn (t Token) str() string