aboutsummaryrefslogtreecommitdiff
path: root/v_windows/v/old/cmd/tools/vtest-parser.v
blob: e1e829ae6fd8a2b686cad60c91f1de0b5b3a5e25 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import os
import flag
import term
import time
import v.parser
import v.ast
import v.pref

const (
	vexe          = pref.vexe_path()
	vroot         = os.dir(vexe)
	support_color = term.can_show_color_on_stderr() && term.can_show_color_on_stdout()
	ecode_timeout = 101
	ecode_memout  = 102
	ecode_details = map{
		-1:  'worker executable not found'
		101: 'too slow'
		102: 'too memory hungry'
	}
)

struct Context {
mut:
	is_help    bool
	is_worker  bool
	is_verbose bool
	is_silent  bool // do not print any status/progress during processing, just failures.
	is_linear  bool // print linear progress log, without trying to do term cursor up + \r msg. Easier to use in a CI job
	timeout_ms int
	myself     string   // path to this executable, so the supervisor can launch worker processes
	all_paths  []string // all files given to the supervisor process
	path       string   // the current path, given to a worker process
	cut_index  int      // the cut position in the source from context.path
	max_index  int      // the maximum index (equivalent to the file content length)
	// parser context in the worker processes:
	table      ast.Table
	scope      ast.Scope
	pref       &pref.Preferences
	period_ms  int  // print periodic progress
	stop_print bool // stop printing the periodic progress
}

fn main() {
	mut context := process_cli_args()
	if context.is_worker {
		pid := os.getpid()
		context.log('> worker ${pid:5} starts parsing at cut_index: ${context.cut_index:5} | $context.path')
		// A worker's process job is to try to parse a single given file in context.path.
		// It can crash/panic freely.
		context.table = ast.new_table()
		context.scope = &ast.Scope{
			parent: 0
		}
		context.pref = &pref.Preferences{
			output_mode: .silent
		}
		mut source := os.read_file(context.path) ?
		source = source[..context.cut_index]

		go fn (ms int) {
			time.sleep(ms * time.millisecond)
			exit(ecode_timeout)
		}(context.timeout_ms)
		_ := parser.parse_text(source, context.path, context.table, .skip_comments, context.pref)
		context.log('> worker ${pid:5} finished parsing $context.path')
		exit(0)
	} else {
		// The process supervisor should NOT crash/panic, unlike the workers.
		// It's job, is to:
		// 1) start workers
		// 2) accumulate results
		// 3) produce a summary at the end
		context.expand_all_paths()
		mut fails := 0
		mut panics := 0
		sw := time.new_stopwatch()
		for path in context.all_paths {
			filesw := time.new_stopwatch()
			context.start_printing()
			new_fails, new_panics := context.process_whole_file_in_worker(path)
			fails += new_fails
			panics += new_panics
			context.stop_printing()
			context.info('File: ${path:-30} | new_fails: ${new_fails:5} | new_panics: ${new_panics:5} | Elapsed time: ${filesw.elapsed().milliseconds()}ms')
		}
		non_panics := fails - panics
		context.info('Total files processed: ${context.all_paths.len:5} | Errors found: ${fails:5} | Panics: ${panics:5} | Non panics: ${non_panics:5} | Elapsed time: ${sw.elapsed().milliseconds()}ms')
		if fails > 0 {
			exit(1)
		}
		exit(0)
	}
}

fn process_cli_args() &Context {
	mut context := &Context{
		pref: pref.new_preferences()
	}
	context.myself = os.executable()
	mut fp := flag.new_flag_parser(os.args_after('test-parser'))
	fp.application(os.file_name(context.myself))
	fp.version('0.0.1')
	fp.description('Test the V parser, by parsing each .v file in each PATH,\n' +
		'as if it was typed character by character by the user.\n' +
		'A PATH can be either a folder, or a specific .v file.\n' +
		'NB: you *have to quote* the PATH, if it contains spaces/punctuation.')
	fp.arguments_description('PATH1 PATH2 ...')
	fp.skip_executable()
	context.is_help = fp.bool('help', `h`, false, 'Show help/usage screen.')
	context.is_verbose = fp.bool('verbose', `v`, false, 'Be more verbose.')
	context.is_silent = fp.bool('silent', `S`, false, 'Do not print progress at all.')
	context.is_linear = fp.bool('linear', `L`, false, 'Print linear progress log. Suitable for CI.')
	context.period_ms = fp.int('progress_ms', `s`, 500, 'print a status report periodically, the period is given in milliseconds.')
	context.is_worker = fp.bool('worker', `w`, false, 'worker specific flag - is this a worker process, that can crash/panic.')
	context.cut_index = fp.int('cut_index', `c`, 1, 'worker specific flag - cut index in the source file, everything before that will be parsed, the rest - ignored.')
	context.timeout_ms = fp.int('timeout_ms', `t`, 250, 'worker specific flag - timeout in ms; a worker taking longer, will self terminate.')
	context.path = fp.string('path', `p`, '', 'worker specific flag - path to the current source file, which will be parsed.')
	//
	if context.is_help {
		println(fp.usage())
		exit(0)
	}
	context.all_paths = fp.finalize() or {
		context.error(err.msg)
		exit(1)
	}
	if !context.is_worker && context.all_paths.len == 0 {
		println(fp.usage())
		exit(0)
	}
	return context
}

// ////////////////
fn bold(msg string) string {
	if !support_color {
		return msg
	}
	return term.bold(msg)
}

fn red(msg string) string {
	if !support_color {
		return msg
	}
	return term.red(msg)
}

fn yellow(msg string) string {
	if !support_color {
		return msg
	}
	return term.yellow(msg)
}

fn (mut context Context) info(msg string) {
	println(msg)
}

fn (mut context Context) log(msg string) {
	if context.is_verbose {
		label := yellow('info')
		ts := time.now().format_ss_micro()
		eprintln('$label: $ts | $msg')
	}
}

fn (mut context Context) error(msg string) {
	label := red('error')
	eprintln('$label: $msg')
}

fn (mut context Context) expand_all_paths() {
	context.log('> context.all_paths before: $context.all_paths')
	mut files := []string{}
	for path in context.all_paths {
		if os.is_dir(path) {
			files << os.walk_ext(path, '.v')
			files << os.walk_ext(path, '.vsh')
			continue
		}
		if !path.ends_with('.v') && !path.ends_with('.vv') && !path.ends_with('.vsh') {
			context.error('`v test-parser` can only be used on .v/.vv/.vsh files.\nOffending file: "$path".')
			continue
		}
		if !os.exists(path) {
			context.error('"$path" does not exist.')
			continue
		}
		files << path
	}
	context.all_paths = files
	context.log('> context.all_paths after: $context.all_paths')
}

fn (mut context Context) process_whole_file_in_worker(path string) (int, int) {
	context.path = path // needed for the progress bar
	context.log('> context.process_whole_file_in_worker path: $path')
	if !(os.is_file(path) && os.is_readable(path)) {
		context.error('$path is not readable')
		return 1, 0
	}
	source := os.read_file(path) or { '' }
	if source == '' {
		// an empty file is a valid .v file
		return 0, 0
	}
	len := source.len - 1
	mut fails := 0
	mut panics := 0
	context.max_index = len
	for i in 0 .. len {
		verbosity := if context.is_verbose { '-v' } else { '' }
		context.cut_index = i // needed for the progress bar
		cmd := '"$context.myself" $verbosity --worker --timeout_ms ${context.timeout_ms:5} --cut_index ${i:5} --path "$path" '
		context.log(cmd)
		mut res := os.execute(cmd)
		context.log('worker exit_code: $res.exit_code | worker output:\n$res.output')
		if res.exit_code != 0 {
			fails++
			mut is_panic := false
			if res.output.contains('V panic:') {
				is_panic = true
				panics++
			}
			part := source[..i]
			line := part.count('\n') + 1
			last_line := part.all_after_last('\n')
			col := last_line.len
			err := if is_panic {
				red('parser failure: panic')
			} else {
				red('parser failure: crash, ${ecode_details[res.exit_code]}')
			}
			path_to_line := bold('$path:$line:$col:')
			err_line := last_line.trim_left('\t')
			println('$path_to_line $err')
			println('\t$line | $err_line')
			println('')
			eprintln(res.output)
		}
	}
	return fails, panics
}

fn (mut context Context) start_printing() {
	context.stop_print = false
	if !context.is_linear && !context.is_silent {
		println('\n')
	}
	go context.print_periodic_status()
}

fn (mut context Context) stop_printing() {
	context.stop_print = true
	time.sleep(time.millisecond * context.period_ms / 5)
}

fn (mut context Context) print_status() {
	if context.is_silent {
		return
	}
	if (context.cut_index == 1) && (context.max_index == 0) {
		return
	}
	msg := '>   ${context.path:-30} | index: ${context.cut_index:5}/${context.max_index - 1:5}'
	if context.is_linear {
		eprintln(msg)
		return
	}
	term.cursor_up(1)
	eprint('\r  $msg\n')
}

fn (mut context Context) print_periodic_status() {
	context.print_status()
	mut printed_at_least_once := false
	for !context.stop_print {
		context.print_status()
		for i := 0; i < 10 && !context.stop_print; i++ {
			time.sleep(time.millisecond * context.period_ms / 10)
			if context.cut_index > 50 && !printed_at_least_once {
				context.print_status()
				printed_at_least_once = true
			}
		}
	}
	context.print_status()
}