| 1 | module main |
| 2 | |
| 3 | import os |
| 4 | import v.scanner |
| 5 | import v.token |
| 6 | import flag |
| 7 | |
| 8 | fn main() { |
| 9 | mut fp := flag.new_flag_parser(os.args#[2..]) |
| 10 | fp.application('v scan') |
| 11 | fp.version('0.0.1') |
| 12 | fp.description('\nScan .v source files, and print the V tokens contained in them.') |
| 13 | fp.arguments_description('PATH [PATH]...') |
| 14 | fp.limit_free_args_to_at_least(1)! |
| 15 | mut all_paths := fp.remaining_parameters() |
| 16 | for path in all_paths { |
| 17 | content := os.read_file(path) or { |
| 18 | eprintln('> could not read: ${path}, skipping; err: ${err}') |
| 19 | continue |
| 20 | } |
| 21 | mut scanner_ := scanner.new_silent_scanner() |
| 22 | scanner_.prepare_for_new_text(content) |
| 23 | scanner_.is_fmt = false |
| 24 | scanner_.pref.output_mode = .stdout |
| 25 | scanner_.comments_mode = .skip_comments |
| 26 | mut tok := token.Token{} |
| 27 | for tok.kind != .eof { |
| 28 | tok = scanner_.text_scan() |
| 29 | pos := tok.pos() |
| 30 | location := '${path}:${pos.line_nr + 1}:${pos.col + 1}:' |
| 31 | println('${location:-32} | pos: ${pos.pos:-5} | ${tok.debug()}') |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | |