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

v.token: correct some comments, and add some missing comments (#8542)

This commit is contained in:
yuyi 2021-02-04 15:18:38 +08:00 committed by GitHub
parent 162c42dbe9
commit a976876211
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -5,13 +5,13 @@ module token
pub struct Token { pub struct Token {
pub: pub:
kind Kind // the token number/enum; for quick comparisons kind Kind // the token number/enum; for quick comparisons
lit string // literal representation of the token lit string // literal representation of the token
line_nr int // the line number in the source where the token occured line_nr int // the line number in the source where the token occured
// name_idx int // name table index for O(1) lookup // name_idx int // name table index for O(1) lookup
pos int // the position of the token in scanner text pos int // the position of the token in scanner text
len int // length of the literal len int // length of the literal
tidx int // the index of the token tidx int // the index of the token
} }
pub enum Kind { pub enum Kind {
@ -22,31 +22,31 @@ pub enum Kind {
string // 'foo' string // 'foo'
str_inter // 'name=$user.name' str_inter // 'name=$user.name'
chartoken // `A` - rune chartoken // `A` - rune
plus plus // +
minus minus // -
mul mul // *
div div // /
mod mod // %
xor // ^ xor // ^
pipe // | pipe // |
inc // ++ inc // ++
dec // -- dec // --
and // && and // &&
logical_or logical_or // ||
not not // !
bit_not bit_not // ~
question question // ?
comma comma // ,
semicolon semicolon // ;
colon colon // :
arrow // <- arrow // <-
amp amp // &
hash hash // #
dollar dollar // $
at // @ at // @
str_dollar str_dollar
left_shift left_shift // <<
right_shift right_shift // >>
not_in // !in not_in // !in
not_is // !is not_is // !is
assign // = assign // =
@ -58,26 +58,26 @@ pub enum Kind {
xor_assign // ^= xor_assign // ^=
mod_assign // %= mod_assign // %=
or_assign // |= or_assign // |=
and_assign and_assign // &=
right_shift_assign right_shift_assign // <<=
left_shift_assign // {} () [] left_shift_assign // >>=
lcbr lcbr // {
rcbr rcbr // }
lpar lpar // (
rpar rpar // )
lsbr lsbr // [
rsbr // == != <= < >= > rsbr // ]
eq eq // ==
ne ne // !=
gt gt // >
lt lt // <
ge ge // >=
le le // <=
comment comment
nl nl
dot dot // .
dotdot dotdot // ..
ellipsis // keywords ellipsis // ...
keyword_beg keyword_beg
key_as key_as
key_asm key_asm
@ -99,7 +99,7 @@ pub enum Kind {
key_import key_import
key_in key_in
key_interface key_interface
key_is // key_it key_is
key_match key_match
key_module key_module
key_mut key_mut
@ -132,7 +132,7 @@ pub const (
) )
const ( const (
nr_tokens = int(Kind._end_) nr_tokens = int(Kind._end_)
) )
// @FN => will be substituted with the name of the current V function // @FN => will be substituted with the name of the current V function
@ -174,7 +174,7 @@ pub const (
fn build_keys() map[string]Kind { fn build_keys() map[string]Kind {
mut res := map[string]Kind{} mut res := map[string]Kind{}
for t in int(Kind.keyword_beg) + 1 .. int(Kind.keyword_end) { for t in int(Kind.keyword_beg) + 1 .. int(Kind.keyword_end) {
key := token_str[t] key := token.token_str[t]
res[key] = Kind(t) res[key] = Kind(t)
} }
return res return res
@ -182,7 +182,7 @@ fn build_keys() map[string]Kind {
// TODO remove once we have `enum Kind { name('name') if('if') ... }` // TODO remove once we have `enum Kind { name('name') if('if') ... }`
fn build_token_str() []string { fn build_token_str() []string {
mut s := []string{len: nr_tokens} mut s := []string{len: token.nr_tokens}
s[Kind.unknown] = 'unknown' s[Kind.unknown] = 'unknown'
s[Kind.eof] = 'eof' s[Kind.eof] = 'eof'
s[Kind.name] = 'name' s[Kind.name] = 'name'
@ -297,11 +297,11 @@ const (
) )
pub const ( pub const (
keywords = build_keys() keywords = build_keys()
) )
pub fn key_to_token(key string) Kind { pub fn key_to_token(key string) Kind {
return Kind(keywords[key]) return Kind(token.keywords[key])
} }
pub fn is_key(key string) bool { pub fn is_key(key string) bool {
@ -309,16 +309,17 @@ pub fn is_key(key string) bool {
} }
pub fn is_decl(t Kind) bool { pub fn is_decl(t Kind) bool {
return t in return t in [.key_enum, .key_interface, .key_fn, .key_struct, .key_type, .key_const, .key_pub,
[.key_enum, .key_interface, .key_fn, .key_struct, .key_type, .key_const, .key_pub, .eof] .eof,
]
} }
pub fn (t Kind) is_assign() bool { pub fn (t Kind) is_assign() bool {
return t in assign_tokens return t in token.assign_tokens
} }
pub fn (t Kind) str() string { pub fn (t Kind) str() string {
return token_str[int(t)] return token.token_str[int(t)]
} }
pub fn (t Token) str() string { pub fn (t Token) str() string {
@ -405,7 +406,7 @@ const (
// precedence returns a tokens precedence if defined, otherwise lowest_prec // precedence returns a tokens precedence if defined, otherwise lowest_prec
pub fn (tok Token) precedence() int { pub fn (tok Token) precedence() int {
return int(precedences[tok.kind]) return int(token.precedences[tok.kind])
} }
// is_scalar returns true if the token is a scalar // is_scalar returns true if the token is a scalar
@ -416,7 +417,7 @@ pub fn (tok Token) is_scalar() bool {
// is_unary returns true if the token can be in a unary expression // is_unary returns true if the token can be in a unary expression
pub fn (tok Token) is_unary() bool { pub fn (tok Token) is_unary() bool {
// `+` | `-` | `!` | `~` | `*` | `&` | `<-` // `+` | `-` | `!` | `~` | `*` | `&` | `<-`
return tok.kind in [ .plus, .minus, .not, .bit_not, .mul, .amp, .arrow] return tok.kind in [.plus, .minus, .not, .bit_not, .mul, .amp, .arrow]
} }
pub fn (tok Kind) is_relational() bool { pub fn (tok Kind) is_relational() bool {
@ -433,8 +434,9 @@ pub fn (kind Kind) is_prefix() bool {
} }
pub fn (kind Kind) is_infix() bool { pub fn (kind Kind) is_infix() bool {
return kind in return kind in [.plus, .minus, .mod, .mul, .div, .eq, .ne, .gt, .lt, .key_in, .key_as, .ge,
[.plus, .minus, .mod, .mul, .div, .eq, .ne, .gt, .lt, .key_in, .key_as, .ge, .le, .logical_or, .xor, .not_in, .key_is, .not_is, .and, .dot, .pipe, .amp, .left_shift, .right_shift, .arrow] .le, .logical_or, .xor, .not_in, .key_is, .not_is, .and, .dot, .pipe, .amp, .left_shift,
.right_shift, .arrow]
} }
// Pass table.builtin_type_names // Pass table.builtin_type_names