Skip to content

v.parser #

Parallel file parsing. The input file list is split into contiguous, size-balanced chunks; each worker parses its chunk into a private Parser + FlatAst on its own thread, and the master folds the results back in chunk order, shifting node ids and children offsets by the merge position. A file's nodes only ever reference nodes of the same file, and chunks are contiguous slices of the input list merged in input order, so the merged nodes/children arrays are laid out exactly as a serial parse of the same list — the phases downstream see an identical AST either way.

fn Parser.new #

fn Parser.new(prefs &pref.Preferences) &Parser

new creates a Parser value for parser.

struct Diagnostic #

struct Diagnostic {
pub:
	file           string
	pos            token.Pos
	line           int
	column         int
	severity       string
	message        string
	details        []string
	detail_pos     token.Pos
	detail_message string
}

Diagnostic is a structured source-ingestion, lexical, or parse diagnostic.

struct Parser #

struct Parser {
	prefs &pref.Preferences
mut:
	s                            scanner.Scanner
	tok                          token.Token
	lit                          string
	tok_pos                      int
	tok_end                      int
	prev_tok_end                 int
	peek_tok                     token.Token = .eof
	peek_lit                     string
	peek_pos                     int
	peek_end                     int
	has_peek                     bool
	cur_file                     string
	cur_file_id                  int
	next_file_id                 int = 1
	cur_module                   string
	is_translated                bool
	cur_fn                       string
	cur_fn_offset                int = -1
	cur_fn_generic_params        []string
	cur_veb_ctx_name             string // source-level name of the active veb request context
	veb_tmpl_counter             int    // monotonic id for unique `$veb.html`/`$tmpl` builder var names
	has_veb_template             bool
	pending_template_diagnostics []TemplateDiagnostic
	may_have_local_types         bool
	cur_struct                   string   // receiver type name of the current method, for `@STRUCT`
	cur_method_is_static         bool     // distinguishes `Type.method()` from `(x Type) method()` for `@LOCATION`
	defer_depth                  int      // >0 while parsing a `defer` block body; gates `$res()` to defer contexts only
	defer_result_allowed         bool     // true when the active defer is guaranteed to run during function return
	unsafe_depth                 int      // >0 while parsing an `unsafe` block body
	nested_block_depth           int      // lexical block depth below the current function body's outer scope
	comptime_for_vars            []string // active `$for` loop variables; a `$if` that reads one is deferred to unroll time
	comptime_method_var          string   // innermost active `$for method in Type.methods` loop variable
	comptime_const_values        map[string]string
	comptime_local_values        map[string]string
	imported_module_names        map[string]bool // import aliases in the current file; not captured by inlined template closures
	file_method_names            map[string]bool // qualified method names declared so far in the current file, for duplicate diagnostics
	check_imports                bool            // enabled by the compiler driver, but not by syntax-only parser clients
	// local_binding_* track the variable/parameter names currently in scope, so an inlined
	// template closure captures a bare callee only when it is an actual local binding (a
	// function-valued parameter/local) rather than a module/top-level function. Scoped like
	// comptime_value_*: a name is in scope when local_binding_counts[name] > 0.
	local_binding_counts              map[string]int
	global_names                      map[string]bool
	parse_batch_paths                 []string
	translated_sizeof_type_names      map[string]bool
	translated_sizeof_const_names     map[string]bool
	translated_sizeof_global_names    map[string]bool
	translated_sizeof_scanned_modules map[string]bool
	local_binding_undos               []string
	local_binding_scopes              []int
	active_lambda_param_counts        map[string]int
	comptime_value_undos              []ComptimeValueUndo
	comptime_value_scopes             []int
	pending_flag                      bool
	pending_json_as_number            bool // `@[json_as_number]` seen before the next enum decl
	pending_params                    bool
	pending_typedef                   bool
	pending_export                    string
	pending_noreturn                  bool
	pending_soa                       bool
	pending_has_aligned               bool
	pending_aligned                   string
	skip_next_decl                    bool
	disable_fn_body                   bool
	pending_decl_pub                  bool
	pending_decl_attrs                []string
	pending_decl_attr_kinds           []int
	pending_decl_attr_sources         []string
	in_for_container                  bool
	in_select_branch_condition        int
	in_match_branch_condition         int
	in_array_literal                  int
	inside_array_init_type_expr       bool
	in_map_value                      int
	in_struct_init_value              int
	unsupported_inline_asm_guards     map[int]bool
	parsing_inferred_fixed_array_type bool
	parsing_struct_field_type         bool
	parsing_c_struct_fields           bool
	diagnostic_limit_reached          bool
	local_type_names                  map[string]string
	local_type_decls_by_block         map[int][]string
	local_type_decls_indexed          bool
	local_type_scopes                 []string
	anonymous_struct_types            map[string][]string
	anonymous_struct_count            int
	sql_query_data_aliases            map[string]bool
	export_records                    []ExportRecord
pub mut:
	a                          &flat.FlatAst = unsafe { nil }
	parsed_v_files             int
	parsed_v_file_paths        []string
	parsed_v_header_files      int
	parsed_v_header_file_paths []string
	diagnostics                []Diagnostic
}

Parser represents parser data used by parser.

fn (Parser) reserve_selfhost_ast #

fn (mut p Parser) reserve_selfhost_ast()

reserve_selfhost_ast prepares the shared AST for a compiler-sized input without retaining successively doubled backing arrays during transform.

fn (Parser) enable_import_diagnostics #

fn (mut p Parser) enable_import_diagnostics()

enable_import_diagnostics enables parser diagnostics that require a complete source file rather than syntax-only parsing.

fn (Parser) parse_file #

fn (mut p Parser) parse_file(path string) &flat.FlatAst

parse_file reads parse file input for parser.

fn (Parser) parse_files #

fn (mut p Parser) parse_files(paths []string) &flat.FlatAst

parse_files reads parse files input for parser.

fn (Parser) release_source_storage #

fn (mut p Parser) release_source_storage()

release_source_storage canonicalizes retained metadata and drops parser/scanner references to raw file buffers. Parsed node text is canonicalized as each file is completed and when parallel worker ASTs are merged.

fn (Parser) parse_files_with_starts #

fn (mut p Parser) parse_files_with_starts(paths []string) []int

parse_files_with_starts parses paths in order like parse_files, recording the first node id of each file's region. Import resolution uses the starts to bound per-file post-processing (module-name canonicalization) when files are parsed in batches instead of one at a time.

fn (Parser) parse_into #

fn (mut p Parser) parse_into(path string)

parse_into reads parse into input for parser.

fn (Parser) parse_files_dispatch #

@[direct_array_access]
fn (mut p Parser) parse_files_dispatch(paths []string, allow_parallel bool) ([]int, bool)

parse_files_dispatch parses paths in order, appending to p.a exactly like a serial parse_into loop, across worker threads when there is enough work. Returns each file's first node id in p.a and whether threads were used.