core.codemodel #
Code Model
A set of models that represent code, such as structs and functions. The motivation behind this module is to provide a more generic, and lighter alternative to v.ast code models, that can be used for code parsing and code generation across multiple languages.
Using Codemodel
While the models in this module can be used in any domain, the models here are used extensively in the modules codeparser and codegen (under development). Below are examples on how codemodel can be used for parsing and generating code.## Code parsing with codemodel
As shown in the example below, the codemodels returned by the parser can be used to infer information about the code written
code := codeparser.parse('somedir') // code is a list of code models
num_functions := code.filter(it is Function).len
structs := code.filter(it is Struct)
println('This directory has ${num_functions} functions')
println('The directory has the structs: ${structs.map(it.name)}')
or can be used as intermediate structures to serialize code into some other format:
code_md := ''
// describes the struct in markdown format
for struct in structs {
code_md +='# ${struct.name}'
code_md +='Type: ${struct.typ.symbol}'
code_md += '## Fields:'
for field in struct.fields {
code_md +='- ${field.name}'
}
}
The openrpc/docgen module demonstrates a good use case, where codemodels are serialized into JSON schema's, to generate an OpenRPC description document from a client in v.
fn get_struct #
fn get_struct(params GetStruct) ?Struct
fn inflate_struct_fields #
fn inflate_struct_fields(code []CodeItem, mut struct_ CodeItem)
fn inflate_types #
fn inflate_types(mut code []CodeItem)
fn new_file #
fn new_file(config CodeFile) CodeFile
fn parse_const #
fn parse_const(code_ string) !Const
fn parse_consts #
fn parse_consts(code_ string) ![]Const
fn parse_function #
fn parse_function(code_ string) !Function
fn parse_import #
fn parse_import(code_ string) Import
fn parse_param #
fn parse_param(code_ string) !Param
fn parse_result #
fn parse_result(code_ string) !Result
fn vgen #
fn vgen(code []CodeItem) string
fn vgen_generics #
fn vgen_generics(generics map[string]string) string
type CodeItem #
type CodeItem = Alias | Comment | CustomCode | Function | Import | Struct | Sumtype
type Value #
type Value = string
struct Alias #
struct Alias {
pub:
name string
description string
typ Type
}
struct Attribute #
struct Attribute {
pub:
name string // [name]
has_arg bool
arg string // [name: arg]
}
struct CodeFile #
struct CodeFile {
pub mut:
name string
mod string
imports []Import
consts []Const
items []CodeItem
content string
}
fn (CodeFile) add_import #
fn (mut file CodeFile) add_import(import_ Import) !
fn (CodeFile) write_v #
fn (code CodeFile) write_v(path string, options WriteOptions) !
fn (CodeFile) get_function #
fn (file CodeFile) get_function(name string) ?Function
fn (CodeFile) set_function #
fn (mut file CodeFile) set_function(function Function) !
struct Comment #
struct Comment {
pub:
text string
is_multi bool
}
struct Const #
struct Const {
name string
value string
}
struct CustomCode #
struct CustomCode {
pub:
text string
}
item for adding custom code in
fn (CustomCode) vgen #
fn (custom CustomCode) vgen() string
struct Example #
struct Example {
function Function
values map[string]Value
result Value
}
struct File #
struct File {
pub mut:
name string
extension string
content string
}
fn (File) write #
fn (f File) write(path string) !
struct Function #
struct Function {
pub:
name string
receiver Param
is_pub bool
mod string
pub mut:
description string
params []Param
body string
result Result
has_return bool
}
fn (Function) generate_call #
fn (func Function) generate_call(params GenerateCallParams) !string
fn (Function) vgen #
fn (function Function) vgen() string
vgen_function generates a function statement for a function
struct GenerateCallParams #
struct GenerateCallParams {
pub:
receiver string
}
struct GenerateValueParams #
struct GenerateValueParams {
}
struct GetStruct #
struct GetStruct {
pub:
code []CodeItem
mod string
name string
}
struct Import #
struct Import {
pub mut:
mod string
types []string
}
fn (Import) add_types #
fn (mut i Import) add_types(types []string)
fn (Import) vgen #
fn (import_ Import) vgen() string
vgen_import generates an import statement for a given type
struct Module #
struct Module {
pub mut:
name string
files []CodeFile
misc_files []File
// model CodeFile
// methods CodeFile
}
fn (Module) write_v #
fn (mod Module) write_v(path string, options WriteOptions) !
struct Param #
struct Param {
pub:
required bool
mutable bool
is_shared bool
is_optional bool
description string
name string
typ Type
struct_ Struct
}
fn (Param) generate_value #
fn (param Param) generate_value() !string
fn (Param) vgen #
fn (param Param) vgen() string
struct Result #
struct Result {
pub mut:
typ Type
description string
name string
result bool // whether is result type
optional bool // whether is result type
structure Struct
}
fn (Result) vgen #
fn (result Result) vgen() string
vgen_function generates a function statement for a function
struct Struct #
struct Struct {
pub mut:
name string
description string
mod string
is_pub bool
embeds []Struct @[str: skip]
generics map[string]string @[str: skip]
attrs []Attribute
fields []StructField
}
fn (Struct) get_type_symbol #
fn (structure Struct) get_type_symbol() string
fn (Struct) vgen #
fn (struct_ Struct) vgen() string
vgen_function generates a function statement for a function
struct StructField #
struct StructField {
pub mut:
comments []Comment
attrs []Attribute
name string
description string
default string
is_pub bool
is_mut bool
is_ref bool
anon_struct Struct @[str: skip] // sometimes fields may hold anonymous structs
typ Type
structure Struct @[str: skip]
}
fn (StructField) get_type_symbol #
fn (field StructField) get_type_symbol() string
fn (StructField) vgen #
fn (field StructField) vgen() string
struct Sumtype #
struct Sumtype {
pub:
name string
description string
types []Type
}
struct Type #
struct Type {
pub mut:
is_reference bool @[str: skip]
is_map bool @[str: skip]
is_array bool
is_mutable bool @[str: skip]
is_shared bool @[str: skip]
is_optional bool @[str: skip]
is_result bool @[str: skip]
symbol string
mod string @[str: skip]
}
Todo: maybe make 'is_' fields methods?
fn (Type) vgen #
fn (type_ Type) vgen() string
Todo: enfore that cant be both mutable and shared
struct VGenerator #
struct VGenerator {
format bool
}
fn (VGenerator) generate_struct #
fn (gen VGenerator) generate_struct(struct_ Struct) !string
fn (VGenerator) generate_struct_field #
fn (gen VGenerator) generate_struct_field(field StructField) string
struct WriteCode #
struct WriteCode {
destination string
}
struct WriteOptions #
struct WriteOptions {
pub:
format bool
overwrite bool
document bool
prefix string
}
- README
- fn get_struct
- fn inflate_struct_fields
- fn inflate_types
- fn new_file
- fn parse_const
- fn parse_consts
- fn parse_function
- fn parse_import
- fn parse_param
- fn parse_result
- fn vgen
- fn vgen_generics
- type CodeItem
- type Value
- struct Alias
- struct Attribute
- struct CodeFile
- struct Comment
- struct Const
- struct CustomCode
- struct Example
- struct File
- struct Function
- struct GenerateCallParams
- struct GenerateValueParams
- struct GetStruct
- struct Import
- struct Module
- struct Param
- struct Result
- struct Struct
- struct StructField
- struct Sumtype
- struct Type
- struct VGenerator
- struct WriteCode
- struct WriteOptions