| 1 | // Copyright (c) 2026 Alexander Medvednikov. All rights reserved. |
| 2 | // Use of this source code is governed by an MIT license |
| 3 | // that can be found in the LICENSE file. |
| 4 | // vtest build: !windows |
| 5 | module parser |
| 6 | |
| 7 | import os |
| 8 | import v2.pref |
| 9 | import v2.token |
| 10 | |
| 11 | fn parse_code_for_test(code string) { |
| 12 | tmp_file := '/tmp/v2_parser_test_${os.getpid()}.v' |
| 13 | os.write_file(tmp_file, code) or { panic('failed to write temp file') } |
| 14 | defer { |
| 15 | os.rm(tmp_file) or {} |
| 16 | } |
| 17 | prefs := &pref.Preferences{} |
| 18 | mut file_set := token.FileSet.new() |
| 19 | mut par := Parser.new(prefs) |
| 20 | files := par.parse_files([tmp_file], mut file_set) |
| 21 | assert files.len == 1 |
| 22 | } |
| 23 | |
| 24 | fn test_if_condition_parses_selector_struct_init_before_infix() { |
| 25 | parse_code_for_test(' |
| 26 | module main |
| 27 | |
| 28 | fn main() { |
| 29 | mut pos := token.Pos{} |
| 30 | if pos == token.Pos{} && true { |
| 31 | x := 1 |
| 32 | } |
| 33 | } |
| 34 | ') |
| 35 | } |
| 36 | |
| 37 | fn test_if_condition_keeps_uppercase_selector_followed_by_block() { |
| 38 | parse_code_for_test(' |
| 39 | module main |
| 40 | |
| 41 | fn main() int { |
| 42 | ch := 0 |
| 43 | if ch == C.EOF { |
| 44 | return -1 |
| 45 | } |
| 46 | return ch |
| 47 | } |
| 48 | ') |
| 49 | } |
| 50 | |
| 51 | fn test_match_branch_keeps_empty_uppercase_branch_block() { |
| 52 | parse_code_for_test(' |
| 53 | module main |
| 54 | |
| 55 | type Obj = EmptyScopeObject | AsmRegister |
| 56 | |
| 57 | struct AsmRegister {} |
| 58 | struct EmptyScopeObject {} |
| 59 | |
| 60 | fn main() { |
| 61 | node := Obj(EmptyScopeObject{}) |
| 62 | match node { |
| 63 | AsmRegister, EmptyScopeObject {} |
| 64 | } |
| 65 | } |
| 66 | ') |
| 67 | } |
| 68 | |
| 69 | fn test_global_unsafe_block_parses_leading_array_literal_stmt() { |
| 70 | parse_code_for_test(' |
| 71 | module main |
| 72 | |
| 73 | struct File {} |
| 74 | |
| 75 | __global files = unsafe { []&File{} } |
| 76 | |
| 77 | fn main() {} |
| 78 | ') |
| 79 | } |
| 80 | |
| 81 | fn test_array_literal_parses_backslash_char_literal() { |
| 82 | parse_code_for_test(' |
| 83 | module main |
| 84 | |
| 85 | fn main() { |
| 86 | x := [u8(`\\\\`)] |
| 87 | _ = x |
| 88 | } |
| 89 | ') |
| 90 | } |
| 91 | |
| 92 | fn test_call_argument_parses_untyped_backslash_char_array_literal() { |
| 93 | parse_code_for_test(' |
| 94 | module main |
| 95 | |
| 96 | fn b(bytes []u8) []u8 { |
| 97 | return bytes |
| 98 | } |
| 99 | |
| 100 | fn main() { |
| 101 | x := b([`\\\\`]) |
| 102 | _ = x |
| 103 | } |
| 104 | ') |
| 105 | } |
| 106 | |