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

parser: make most of parser methods private (#18249)

This commit is contained in:
yuyi 2023-05-25 08:51:59 +08:00 committed by GitHub
parent 190f5c69ea
commit f1c647cbbe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 50 additions and 50 deletions

View File

@ -15,7 +15,7 @@ const (
'sumtype', 'alias', 'function', 'option']
)
pub fn (mut p Parser) parse_comptime_type() ast.ComptimeType {
fn (mut p Parser) parse_comptime_type() ast.ComptimeType {
mut node := ast.ComptimeType{ast.ComptimeTypeKind.map_, p.tok.pos()}
p.check(.dollar)

View File

@ -7,7 +7,7 @@ import v.ast
import v.vet
import v.token
pub fn (mut p Parser) expr(precedence int) ast.Expr {
fn (mut p Parser) expr(precedence int) ast.Expr {
return p.check_expr(precedence) or {
if token.is_decl(p.tok.kind) && p.disallow_declarations_in_script_mode() {
return ast.empty_expr
@ -16,7 +16,7 @@ pub fn (mut p Parser) expr(precedence int) ast.Expr {
}
}
pub fn (mut p Parser) check_expr(precedence int) !ast.Expr {
fn (mut p Parser) check_expr(precedence int) !ast.Expr {
p.trace_parser('expr(${precedence})')
mut node := ast.empty_expr
is_stmt_ident := p.is_stmt_ident
@ -461,7 +461,7 @@ pub fn (mut p Parser) check_expr(precedence int) !ast.Expr {
return p.expr_with_left(node, precedence, is_stmt_ident)
}
pub fn (mut p Parser) expr_with_left(left ast.Expr, precedence int, is_stmt_ident bool) ast.Expr {
fn (mut p Parser) expr_with_left(left ast.Expr, precedence int, is_stmt_ident bool) ast.Expr {
mut node := left
if p.inside_asm && p.prev_tok.pos().line_nr < p.tok.pos().line_nr {
return node

View File

@ -8,7 +8,7 @@ import v.token
import v.util
import os
pub fn (mut p Parser) call_expr(language ast.Language, mod string) ast.CallExpr {
fn (mut p Parser) call_expr(language ast.Language, mod string) ast.CallExpr {
first_pos := p.tok.pos()
mut fn_name := if language == .c {
'C.${p.check_name()}'
@ -89,7 +89,7 @@ pub fn (mut p Parser) call_expr(language ast.Language, mod string) ast.CallExpr
}
}
pub fn (mut p Parser) call_args() []ast.CallArg {
fn (mut p Parser) call_args() []ast.CallArg {
mut args := []ast.CallArg{}
start_pos := p.tok.pos()
for p.tok.kind != .rpar {

View File

@ -6,7 +6,7 @@ module parser
import v.ast
// return true if file being parsed imports `mod`
pub fn (p &Parser) known_import(mod string) bool {
fn (p &Parser) known_import(mod string) bool {
return mod in p.imports
}

View File

@ -10,7 +10,7 @@ import v.token
const maximum_inline_sum_type_variants = 3
pub fn (mut p Parser) parse_array_type(expecting token.Kind, is_option bool) ast.Type {
fn (mut p Parser) parse_array_type(expecting token.Kind, is_option bool) ast.Type {
p.check(expecting)
// fixed array
if p.tok.kind in [.number, .name] {
@ -104,7 +104,7 @@ pub fn (mut p Parser) parse_array_type(expecting token.Kind, is_option bool) ast
return ast.new_type(idx)
}
pub fn (mut p Parser) parse_map_type() ast.Type {
fn (mut p Parser) parse_map_type() ast.Type {
is_option := p.tok.kind == .question && p.peek_tok.kind == .name // option map
if is_option {
p.next()
@ -155,7 +155,7 @@ pub fn (mut p Parser) parse_map_type() ast.Type {
}
}
pub fn (mut p Parser) parse_chan_type() ast.Type {
fn (mut p Parser) parse_chan_type() ast.Type {
if p.peek_tok.kind !in [.name, .key_mut, .amp, .lsbr] {
p.next()
return ast.chan_type
@ -171,7 +171,7 @@ pub fn (mut p Parser) parse_chan_type() ast.Type {
return ast.new_type(idx)
}
pub fn (mut p Parser) parse_thread_type() ast.Type {
fn (mut p Parser) parse_thread_type() ast.Type {
is_opt := p.peek_tok.kind == .question
if is_opt {
p.next()
@ -198,7 +198,7 @@ pub fn (mut p Parser) parse_thread_type() ast.Type {
return ast.new_type(idx)
}
pub fn (mut p Parser) parse_multi_return_type() ast.Type {
fn (mut p Parser) parse_multi_return_type() ast.Type {
p.check(.lpar)
mut mr_types := []ast.Type{}
mut has_generic := false
@ -230,7 +230,7 @@ pub fn (mut p Parser) parse_multi_return_type() ast.Type {
}
// given anon name based off signature when `name` is blank
pub fn (mut p Parser) parse_fn_type(name string, generic_types []ast.Type) ast.Type {
fn (mut p Parser) parse_fn_type(name string, generic_types []ast.Type) ast.Type {
fn_type_pos := p.peek_token(-2).pos()
p.check(.key_fn)
@ -293,7 +293,7 @@ pub fn (mut p Parser) parse_fn_type(name string, generic_types []ast.Type) ast.T
return ast.new_type(idx)
}
pub fn (mut p Parser) parse_type_with_mut(is_mut bool) ast.Type {
fn (mut p Parser) parse_type_with_mut(is_mut bool) ast.Type {
typ := p.parse_type()
if is_mut {
return typ.set_nr_muls(1)
@ -302,7 +302,7 @@ pub fn (mut p Parser) parse_type_with_mut(is_mut bool) ast.Type {
}
// Parses any language indicators on a type.
pub fn (mut p Parser) parse_language() ast.Language {
fn (mut p Parser) parse_language() ast.Language {
language := if p.tok.lit == 'C' {
ast.Language.c
} else if p.tok.lit == 'JS' {
@ -321,7 +321,7 @@ pub fn (mut p Parser) parse_language() ast.Language {
// parse_inline_sum_type parses the type and registers it in case the type is an anonymous sum type.
// It also takes care of inline sum types where parse_type only parses a standalone type.
pub fn (mut p Parser) parse_inline_sum_type() ast.Type {
fn (mut p Parser) parse_inline_sum_type() ast.Type {
if !p.pref.is_fmt {
p.warn(
'inline sum types have been deprecated and will be removed on January 1, 2023 due ' +
@ -363,7 +363,7 @@ pub fn (mut p Parser) parse_inline_sum_type() ast.Type {
// parse_sum_type_variants parses several types separated with a pipe and returns them as a list with at least one node.
// If there is less than one node, it will add an error to the error list.
pub fn (mut p Parser) parse_sum_type_variants() []ast.TypeNode {
fn (mut p Parser) parse_sum_type_variants() []ast.TypeNode {
p.inside_sum_type = true
defer {
p.inside_sum_type = false
@ -390,7 +390,7 @@ pub fn (mut p Parser) parse_sum_type_variants() []ast.TypeNode {
return types
}
pub fn (mut p Parser) parse_type() ast.Type {
fn (mut p Parser) parse_type() ast.Type {
// option or result
mut is_option := false
mut is_result := false
@ -507,7 +507,7 @@ If you need to modify an array in a function, use a mutable argument instead: `f
return typ
}
pub fn (mut p Parser) parse_any_type(language ast.Language, is_ptr bool, check_dot bool, is_option bool) ast.Type {
fn (mut p Parser) parse_any_type(language ast.Language, is_ptr bool, check_dot bool, is_option bool) ast.Type {
mut name := p.tok.lit
if language == .c {
name = 'C.${name}'
@ -670,7 +670,7 @@ pub fn (mut p Parser) parse_any_type(language ast.Language, is_ptr bool, check_d
}
}
pub fn (mut p Parser) find_type_or_add_placeholder(name string, language ast.Language) ast.Type {
fn (mut p Parser) find_type_or_add_placeholder(name string, language ast.Language) ast.Type {
// struct / enum / placeholder
mut idx := p.table.find_type_idx(name)
if idx > 0 {
@ -712,7 +712,7 @@ pub fn (mut p Parser) find_type_or_add_placeholder(name string, language ast.Lan
return ast.new_type(idx)
}
pub fn (mut p Parser) parse_generic_type(name string) ast.Type {
fn (mut p Parser) parse_generic_type(name string) ast.Type {
mut idx := p.table.find_type_idx(name)
if idx > 0 {
return ast.new_type(idx).set_flag(.generic)
@ -727,7 +727,7 @@ pub fn (mut p Parser) parse_generic_type(name string) ast.Type {
return ast.new_type(idx).set_flag(.generic)
}
pub fn (mut p Parser) parse_generic_inst_type(name string) ast.Type {
fn (mut p Parser) parse_generic_inst_type(name string) ast.Type {
mut bs_name := name
mut bs_cname := name
start_pos := p.tok.pos()

View File

@ -176,7 +176,7 @@ pub fn (mut p Parser) free() {
}
[unsafe]
pub fn (mut p Parser) free_scanner() {
fn (mut p Parser) free_scanner() {
unsafe {
if p.scanner != 0 {
p.scanner.free()
@ -463,19 +463,19 @@ pub fn (mut p Parser) codegen(code string) {
p.codegen_text += '\n' + code
}
pub fn (mut p Parser) init_parse_fns() {
fn (mut p Parser) init_parse_fns() {
// p.prefix_parse_fns = make(100, 100, sizeof(PrefixParseFn))
// p.prefix_parse_fns[token.Kind.name] = parse_name
}
pub fn (mut p Parser) read_first_token() {
fn (mut p Parser) read_first_token() {
// need to call next() 2 times to get peek token and current token
p.next()
p.next()
}
[inline]
pub fn (p &Parser) peek_token(n int) token.Token {
fn (p &Parser) peek_token(n int) token.Token {
return p.scanner.peek_token(n - 2)
}
@ -554,14 +554,14 @@ fn (p &Parser) is_array_type() bool {
return false
}
pub fn (mut p Parser) open_scope() {
fn (mut p Parser) open_scope() {
p.scope = &ast.Scope{
parent: p.scope
start_pos: p.tok.pos
}
}
pub fn (mut p Parser) close_scope() {
fn (mut p Parser) close_scope() {
// p.scope.end_pos = p.tok.pos
// NOTE: since this is usually called after `p.parse_block()`
// ie. when `prev_tok` is rcbr `}` we most likely want `prev_tok`
@ -572,14 +572,14 @@ pub fn (mut p Parser) close_scope() {
p.scope = p.scope.parent
}
pub fn (mut p Parser) parse_block() []ast.Stmt {
fn (mut p Parser) parse_block() []ast.Stmt {
p.open_scope()
stmts := p.parse_block_no_scope(false)
p.close_scope()
return stmts
}
pub fn (mut p Parser) parse_block_no_scope(is_top_level bool) []ast.Stmt {
fn (mut p Parser) parse_block_no_scope(is_top_level bool) []ast.Stmt {
p.check(.lcbr)
mut stmts := []ast.Stmt{cap: 20}
if p.tok.kind != .rcbr {
@ -691,7 +691,7 @@ fn (p &Parser) trace_parser(label string) {
eprintln('parsing: ${p.file_name:-30}|tok.pos: ${p.tok.pos().line_str():-39}|tok.kind: ${p.tok.kind:-10}|tok.lit: ${p.tok.lit:-10}|${label}')
}
pub fn (mut p Parser) top_stmt() ast.Stmt {
fn (mut p Parser) top_stmt() ast.Stmt {
p.trace_parser('top_stmt')
for {
match p.tok.kind {
@ -860,14 +860,14 @@ fn (mut p Parser) other_stmts(cur_stmt ast.Stmt) ast.Stmt {
}
// TODO [if vfmt]
pub fn (mut p Parser) check_comment() ast.Comment {
fn (mut p Parser) check_comment() ast.Comment {
if p.tok.kind == .comment {
return p.comment()
}
return ast.Comment{}
}
pub fn (mut p Parser) comment() ast.Comment {
fn (mut p Parser) comment() ast.Comment {
mut pos := p.tok.pos()
text := p.tok.lit
num_newlines := text.count('\n')
@ -888,7 +888,7 @@ pub fn (mut p Parser) comment() ast.Comment {
}
}
pub fn (mut p Parser) comment_stmt() ast.ExprStmt {
fn (mut p Parser) comment_stmt() ast.ExprStmt {
comment := p.comment()
return ast.ExprStmt{
expr: comment
@ -902,7 +902,7 @@ struct EatCommentsConfig {
follow_up bool // Comments directly below the previous token as long as there is no empty line
}
pub fn (mut p Parser) eat_comments(cfg EatCommentsConfig) []ast.Comment {
fn (mut p Parser) eat_comments(cfg EatCommentsConfig) []ast.Comment {
mut line := p.prev_tok.line_nr
mut comments := []ast.Comment{}
for {
@ -918,7 +918,7 @@ pub fn (mut p Parser) eat_comments(cfg EatCommentsConfig) []ast.Comment {
return comments
}
pub fn (mut p Parser) stmt(is_top_level bool) ast.Stmt {
fn (mut p Parser) stmt(is_top_level bool) ast.Stmt {
p.trace_parser('stmt(${is_top_level})')
p.is_stmt_ident = p.tok.kind == .name
match p.tok.kind {
@ -1874,19 +1874,19 @@ fn (mut p Parser) parse_attr() ast.Attr {
}
}
pub fn (mut p Parser) language_not_allowed_error(language ast.Language, pos token.Pos) {
fn (mut p Parser) language_not_allowed_error(language ast.Language, pos token.Pos) {
upcase_language := language.str().to_upper()
p.error_with_pos('${upcase_language} code is not allowed in .${p.file_backend_mode}.v files, please move it to a .${language}.v file',
pos)
}
pub fn (mut p Parser) language_not_allowed_warning(language ast.Language, pos token.Pos) {
fn (mut p Parser) language_not_allowed_warning(language ast.Language, pos token.Pos) {
upcase_language := language.str().to_upper()
p.warn_with_pos('${upcase_language} code will not be allowed in pure .v files, please move it to a .${language}.v file instead',
pos)
}
pub fn (mut p Parser) check_for_impure_v(language ast.Language, pos token.Pos) {
fn (mut p Parser) check_for_impure_v(language ast.Language, pos token.Pos) {
if language == .v {
// pure V code is always allowed everywhere
return
@ -1919,19 +1919,19 @@ pub fn (mut p Parser) check_for_impure_v(language ast.Language, pos token.Pos) {
}
}
pub fn (mut p Parser) error(s string) ast.NodeError {
fn (mut p Parser) error(s string) ast.NodeError {
return p.error_with_pos(s, p.tok.pos())
}
pub fn (mut p Parser) warn(s string) {
fn (mut p Parser) warn(s string) {
p.warn_with_pos(s, p.tok.pos())
}
pub fn (mut p Parser) note(s string) {
fn (mut p Parser) note(s string) {
p.note_with_pos(s, p.tok.pos())
}
pub fn (mut p Parser) error_with_pos(s string, pos token.Pos) ast.NodeError {
fn (mut p Parser) error_with_pos(s string, pos token.Pos) ast.NodeError {
mut kind := 'error:'
if p.pref.fatal_errors {
util.show_compiler_message(kind, pos: pos, file_path: p.file_name, message: s)
@ -1971,7 +1971,7 @@ pub fn (mut p Parser) error_with_pos(s string, pos token.Pos) ast.NodeError {
}
}
pub fn (mut p Parser) error_with_error(error errors.Error) {
fn (mut p Parser) error_with_error(error errors.Error) {
mut kind := 'error:'
if p.pref.fatal_errors {
util.show_compiler_message(kind, error.CompilerMessage)
@ -2000,7 +2000,7 @@ pub fn (mut p Parser) error_with_error(error errors.Error) {
}
}
pub fn (mut p Parser) warn_with_pos(s string, pos token.Pos) {
fn (mut p Parser) warn_with_pos(s string, pos token.Pos) {
if p.pref.warns_are_errors {
p.error_with_pos(s, pos)
return
@ -2024,7 +2024,7 @@ pub fn (mut p Parser) warn_with_pos(s string, pos token.Pos) {
}
}
pub fn (mut p Parser) note_with_pos(s string, pos token.Pos) {
fn (mut p Parser) note_with_pos(s string, pos token.Pos) {
if p.pref.skip_warnings {
return
}
@ -2043,7 +2043,7 @@ pub fn (mut p Parser) note_with_pos(s string, pos token.Pos) {
}
}
pub fn (mut p Parser) vet_error(msg string, line int, fix vet.FixKind, typ vet.ErrorType) {
fn (mut p Parser) vet_error(msg string, line int, fix vet.FixKind, typ vet.ErrorType) {
pos := token.Pos{
line_nr: line + 1
}
@ -2142,7 +2142,7 @@ fn (mut p Parser) is_following_concrete_types() bool {
return true
}
pub fn (mut p Parser) ident(language ast.Language) ast.Ident {
fn (mut p Parser) ident(language ast.Language) ast.Ident {
is_option := p.tok.kind == .question && p.peek_tok.kind == .lsbr
if is_option {
p.next()
@ -2407,7 +2407,7 @@ fn (mut p Parser) is_generic_cast() bool {
return false
}
pub fn (mut p Parser) name_expr() ast.Expr {
fn (mut p Parser) name_expr() ast.Expr {
prev_tok_kind := p.prev_tok.kind
mut node := ast.empty_expr
@ -4225,7 +4225,7 @@ fn (mut p Parser) rewind_scanner_to_current_token_in_new_mode() {
}
// returns true if `varname` is known
pub fn (mut p Parser) mark_var_as_used(varname string) bool {
fn (mut p Parser) mark_var_as_used(varname string) bool {
if mut obj := p.scope.find(varname) {
match mut obj {
ast.Var {