Skip to content

v.astjson #

Module astjson renders a parsed V file as JSON.

It exists so v ast (cmd/tools/vast) and the v_ast MCP tool (cmd/tools/vmcp) produce byte-identical output from one implementation: an agent that learned to read v ast -p output reads the MCP tool the same way.

The layout is the one cJSON's formatted printer produces, which is what v ast has always emitted: object members one per line, indented with tabs, as "key":<tab>value, and array elements on one line separated by , .

fn dump #

fn dump(a &flat.FlatAst, opts Options) string

dump renders the AST of the already parsed a as JSON.

Every .file node with at least one child becomes an entry of the top level files array; the collected comments follow under comments. That mirrors what v ast writes, so its output stays stable.

fn parse #

fn parse(path string) &flat.FlatAst

parse reads one V file into a fresh AST with the preferences v ast uses.

struct Options #

struct Options {
pub:
	// terse keeps only the node kinds and the tree shape, dropping every detail.
	terse bool
	// skip_defaults drops properties holding a zero value, such as `[]`, `{}`,
	// `false`, `0` and `""`.
	skip_defaults bool
	// hidden names properties to leave out entirely.
	hidden []string
}

Options selects which properties appear in the output and how deep it goes.

fn (Options) show #

fn (opts Options) show(key string, is_default bool) bool

show reports whether the key property is part of the output. is_default says whether the property holds a zero value, which skip_defaults drops.

struct Writer #

struct Writer {
mut:
	sb     strings.Builder
	depth  int
	counts []int // number of members/elements written, per open object/array
}

Writer writes indented JSON in the layout v ast uses: object members one per line, indented with tabs, as "key":<tab>value, and array elements on one line, separated by , .

fn (Writer) begin_object #

fn (mut w Writer) begin_object()

begin_object opens a JSON object.

fn (Writer) end_object #

fn (mut w Writer) end_object()

end_object closes the open JSON object.

fn (Writer) begin_array #

fn (mut w Writer) begin_array()

begin_array opens a JSON array.

fn (Writer) end_array #

fn (mut w Writer) end_array()

end_array closes the open JSON array.

fn (Writer) key #

fn (mut w Writer) key(name string)

key starts the next member of the current object.

fn (Writer) array_item #

fn (mut w Writer) array_item()

array_item starts the next element of the current array.

fn (Writer) boolean #

fn (mut w Writer) boolean(value bool)

boolean writes a JSON boolean.

fn (Writer) number #

fn (mut w Writer) number(value int)

number writes a JSON integer.

fn (Writer) key_raw #

fn (mut w Writer) key_raw(name string, value_json string)

key_raw starts the next member of the current object, writing a pre-rendered JSON fragment as its value. It is how a nested object or array is inserted without re-parsing what a helper already rendered.

fn (Writer) array_raw #

fn (mut w Writer) array_raw(value_json string)

array_raw starts the next element of the current array, writing a pre-rendered JSON fragment as its value.

fn (Writer) position #

fn (mut w Writer) position(pos token.Pos)

position writes a source position as a file id with its byte offsets.

fn (Writer) string #

fn (mut w Writer) string(value string)

string writes a quoted, escaped JSON string.

fn (Writer) str #

fn (w &Writer) str() string

str returns the accumulated JSON.