mirror of
https://github.com/vlang/v.git
synced 2025-09-11 08:25:42 -04:00
parser: cleanup and simplify language parsing (#20185)
This commit is contained in:
parent
0a029d4532
commit
226e075e7a
@ -249,17 +249,8 @@ fn (mut p Parser) fn_decl() ast.FnDecl {
|
|||||||
p.check(.key_fn)
|
p.check(.key_fn)
|
||||||
comments << p.eat_comments()
|
comments << p.eat_comments()
|
||||||
p.open_scope()
|
p.open_scope()
|
||||||
// C. || JS.
|
|
||||||
mut language := ast.Language.v
|
|
||||||
language_tok_pos := p.tok.pos()
|
language_tok_pos := p.tok.pos()
|
||||||
if p.tok.kind == .name && p.tok.lit == 'C' {
|
mut language := p.parse_language()
|
||||||
is_unsafe = !is_trusted
|
|
||||||
language = .c
|
|
||||||
} else if p.tok.kind == .name && p.tok.lit == 'JS' {
|
|
||||||
language = .js
|
|
||||||
} else if p.tok.kind == .name && p.tok.lit == 'WASM' {
|
|
||||||
language = .wasm
|
|
||||||
}
|
|
||||||
p.fn_language = language
|
p.fn_language = language
|
||||||
if language != .v {
|
if language != .v {
|
||||||
for fna in p.attrs {
|
for fna in p.attrs {
|
||||||
@ -274,9 +265,10 @@ fn (mut p Parser) fn_decl() ast.FnDecl {
|
|||||||
language_tok_pos)
|
language_tok_pos)
|
||||||
}
|
}
|
||||||
if language != .v {
|
if language != .v {
|
||||||
p.next()
|
|
||||||
p.check(.dot)
|
|
||||||
p.check_for_impure_v(language, language_tok_pos)
|
p.check_for_impure_v(language, language_tok_pos)
|
||||||
|
if language == .c {
|
||||||
|
is_unsafe = !is_trusted
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// Receiver?
|
// Receiver?
|
||||||
mut rec := ReceiverParsingInfo{
|
mut rec := ReceiverParsingInfo{
|
||||||
@ -521,14 +513,11 @@ run them via `v file.v` instead',
|
|||||||
language: language
|
language: language
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
if language == .c {
|
name = match language {
|
||||||
name = 'C.${name}'
|
.c { 'C.${name}' }
|
||||||
} else if language == .js {
|
.js { 'JS.${name}' }
|
||||||
name = 'JS.${name}'
|
.wasm { 'WASM.${name}' }
|
||||||
} else if language == .wasm {
|
else { p.prepend_mod(name) }
|
||||||
name = 'WASM.${name}'
|
|
||||||
} else {
|
|
||||||
name = p.prepend_mod(name)
|
|
||||||
}
|
}
|
||||||
if !p.pref.translated && language == .v {
|
if !p.pref.translated && language == .v {
|
||||||
if existing := p.table.fns[name] {
|
if existing := p.table.fns[name] {
|
||||||
|
@ -327,14 +327,11 @@ fn (mut p Parser) parse_type_with_mut(is_mut bool) ast.Type {
|
|||||||
|
|
||||||
// Parses any language indicators on a type.
|
// Parses any language indicators on a type.
|
||||||
fn (mut p Parser) parse_language() ast.Language {
|
fn (mut p Parser) parse_language() ast.Language {
|
||||||
language := if p.tok.lit == 'C' {
|
language := match p.tok.lit {
|
||||||
ast.Language.c
|
'C' { ast.Language.c }
|
||||||
} else if p.tok.lit == 'JS' {
|
'JS' { ast.Language.js }
|
||||||
ast.Language.js
|
'WASM' { ast.Language.wasm }
|
||||||
} else if p.tok.lit == 'WASM' {
|
else { ast.Language.v }
|
||||||
ast.Language.wasm
|
|
||||||
} else {
|
|
||||||
ast.Language.v
|
|
||||||
}
|
}
|
||||||
if language != .v {
|
if language != .v {
|
||||||
p.next()
|
p.next()
|
||||||
|
@ -2534,15 +2534,13 @@ fn (mut p Parser) name_expr() ast.Expr {
|
|||||||
pos: type_pos
|
pos: type_pos
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
mut language := ast.Language.v
|
language := match p.tok.lit {
|
||||||
if p.tok.lit == 'C' {
|
'C' { ast.Language.c }
|
||||||
language = ast.Language.c
|
'JS' { ast.Language.js }
|
||||||
p.check_for_impure_v(language, p.tok.pos())
|
'WASM' { ast.Language.wasm }
|
||||||
} else if p.tok.lit == 'JS' {
|
else { ast.Language.v }
|
||||||
language = ast.Language.js
|
}
|
||||||
p.check_for_impure_v(language, p.tok.pos())
|
if language != .v {
|
||||||
} else if p.tok.lit == 'WASM' {
|
|
||||||
language = ast.Language.wasm
|
|
||||||
p.check_for_impure_v(language, p.tok.pos())
|
p.check_for_impure_v(language, p.tok.pos())
|
||||||
}
|
}
|
||||||
is_option := p.tok.kind == .question
|
is_option := p.tok.kind == .question
|
||||||
|
@ -25,19 +25,7 @@ fn (mut p Parser) struct_decl(is_anon bool) ast.StructDecl {
|
|||||||
} else {
|
} else {
|
||||||
p.check(.key_union)
|
p.check(.key_union)
|
||||||
}
|
}
|
||||||
language := if p.tok.lit == 'C' && p.peek_tok.kind == .dot {
|
language := p.parse_language()
|
||||||
ast.Language.c
|
|
||||||
} else if p.tok.lit == 'JS' && p.peek_tok.kind == .dot {
|
|
||||||
ast.Language.js
|
|
||||||
} else if p.tok.lit == 'WASM' && p.peek_tok.kind == .dot {
|
|
||||||
ast.Language.wasm
|
|
||||||
} else {
|
|
||||||
ast.Language.v
|
|
||||||
}
|
|
||||||
if language != .v {
|
|
||||||
p.next() // C || JS
|
|
||||||
p.next() // .
|
|
||||||
}
|
|
||||||
name_pos := p.tok.pos()
|
name_pos := p.tok.pos()
|
||||||
p.check_for_impure_v(language, name_pos)
|
p.check_for_impure_v(language, name_pos)
|
||||||
if p.disallow_declarations_in_script_mode() {
|
if p.disallow_declarations_in_script_mode() {
|
||||||
@ -522,17 +510,7 @@ fn (mut p Parser) interface_decl() ast.InterfaceDecl {
|
|||||||
p.next()
|
p.next()
|
||||||
}
|
}
|
||||||
p.next() // `interface`
|
p.next() // `interface`
|
||||||
language := if p.tok.lit == 'C' && p.peek_tok.kind == .dot {
|
language := p.parse_language()
|
||||||
ast.Language.c
|
|
||||||
} else if p.tok.lit == 'JS' && p.peek_tok.kind == .dot {
|
|
||||||
ast.Language.js
|
|
||||||
} else {
|
|
||||||
ast.Language.v
|
|
||||||
}
|
|
||||||
if language != .v {
|
|
||||||
p.next() // C || JS | WASM
|
|
||||||
p.next() // .
|
|
||||||
}
|
|
||||||
name_pos := p.tok.pos()
|
name_pos := p.tok.pos()
|
||||||
p.check_for_impure_v(language, name_pos)
|
p.check_for_impure_v(language, name_pos)
|
||||||
if p.disallow_declarations_in_script_mode() {
|
if p.disallow_declarations_in_script_mode() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user