fmt: format long sum-types with a line for each type (#11461)
parent
5c4385a472
commit
c8471528ce
|
@ -37,8 +37,20 @@ pub const (
|
||||||
string_max_len = 2048
|
string_max_len = 2048
|
||||||
)
|
)
|
||||||
|
|
||||||
pub type Primitive = InfixType | bool | byte | f32 | f64 | i16 | i64 | i8 | int | string |
|
pub type Primitive = InfixType
|
||||||
time.Time | u16 | u32 | u64
|
| bool
|
||||||
|
| byte
|
||||||
|
| f32
|
||||||
|
| f64
|
||||||
|
| i16
|
||||||
|
| i64
|
||||||
|
| i8
|
||||||
|
| int
|
||||||
|
| string
|
||||||
|
| time.Time
|
||||||
|
| u16
|
||||||
|
| u32
|
||||||
|
| u64
|
||||||
|
|
||||||
pub enum OperationKind {
|
pub enum OperationKind {
|
||||||
neq // !=
|
neq // !=
|
||||||
|
|
118
vlib/v/ast/ast.v
118
vlib/v/ast/ast.v
|
@ -9,25 +9,104 @@ import v.pref
|
||||||
|
|
||||||
pub type TypeDecl = AliasTypeDecl | FnTypeDecl | SumTypeDecl
|
pub type TypeDecl = AliasTypeDecl | FnTypeDecl | SumTypeDecl
|
||||||
|
|
||||||
pub type Expr = AnonFn | ArrayDecompose | ArrayInit | AsCast | Assoc | AtExpr | BoolLiteral |
|
pub type Expr = AnonFn
|
||||||
CTempVar | CallExpr | CastExpr | ChanInit | CharLiteral | Comment | ComptimeCall |
|
| ArrayDecompose
|
||||||
ComptimeSelector | ConcatExpr | DumpExpr | EmptyExpr | EnumVal | FloatLiteral | GoExpr |
|
| ArrayInit
|
||||||
Ident | IfExpr | IfGuardExpr | IndexExpr | InfixExpr | IntegerLiteral | IsRefType |
|
| AsCast
|
||||||
Likely | LockExpr | MapInit | MatchExpr | NodeError | None | OffsetOf | OrExpr | ParExpr |
|
| Assoc
|
||||||
PostfixExpr | PrefixExpr | RangeExpr | SelectExpr | SelectorExpr | SizeOf | SqlExpr |
|
| AtExpr
|
||||||
StringInterLiteral | StringLiteral | StructInit | TypeNode | TypeOf | UnsafeExpr
|
| BoolLiteral
|
||||||
|
| CTempVar
|
||||||
|
| CallExpr
|
||||||
|
| CastExpr
|
||||||
|
| ChanInit
|
||||||
|
| CharLiteral
|
||||||
|
| Comment
|
||||||
|
| ComptimeCall
|
||||||
|
| ComptimeSelector
|
||||||
|
| ConcatExpr
|
||||||
|
| DumpExpr
|
||||||
|
| EmptyExpr
|
||||||
|
| EnumVal
|
||||||
|
| FloatLiteral
|
||||||
|
| GoExpr
|
||||||
|
| Ident
|
||||||
|
| IfExpr
|
||||||
|
| IfGuardExpr
|
||||||
|
| IndexExpr
|
||||||
|
| InfixExpr
|
||||||
|
| IntegerLiteral
|
||||||
|
| IsRefType
|
||||||
|
| Likely
|
||||||
|
| LockExpr
|
||||||
|
| MapInit
|
||||||
|
| MatchExpr
|
||||||
|
| NodeError
|
||||||
|
| None
|
||||||
|
| OffsetOf
|
||||||
|
| OrExpr
|
||||||
|
| ParExpr
|
||||||
|
| PostfixExpr
|
||||||
|
| PrefixExpr
|
||||||
|
| RangeExpr
|
||||||
|
| SelectExpr
|
||||||
|
| SelectorExpr
|
||||||
|
| SizeOf
|
||||||
|
| SqlExpr
|
||||||
|
| StringInterLiteral
|
||||||
|
| StringLiteral
|
||||||
|
| StructInit
|
||||||
|
| TypeNode
|
||||||
|
| TypeOf
|
||||||
|
| UnsafeExpr
|
||||||
|
|
||||||
pub type Stmt = AsmStmt | AssertStmt | AssignStmt | Block | BranchStmt | CompFor | ConstDecl |
|
pub type Stmt = AsmStmt
|
||||||
DeferStmt | EmptyStmt | EnumDecl | ExprStmt | FnDecl | ForCStmt | ForInStmt | ForStmt |
|
| AssertStmt
|
||||||
GlobalDecl | GotoLabel | GotoStmt | HashStmt | Import | InterfaceDecl | Module | NodeError |
|
| AssignStmt
|
||||||
Return | SqlStmt | StructDecl | TypeDecl
|
| Block
|
||||||
|
| BranchStmt
|
||||||
|
| CompFor
|
||||||
|
| ConstDecl
|
||||||
|
| DeferStmt
|
||||||
|
| EmptyStmt
|
||||||
|
| EnumDecl
|
||||||
|
| ExprStmt
|
||||||
|
| FnDecl
|
||||||
|
| ForCStmt
|
||||||
|
| ForInStmt
|
||||||
|
| ForStmt
|
||||||
|
| GlobalDecl
|
||||||
|
| GotoLabel
|
||||||
|
| GotoStmt
|
||||||
|
| HashStmt
|
||||||
|
| Import
|
||||||
|
| InterfaceDecl
|
||||||
|
| Module
|
||||||
|
| NodeError
|
||||||
|
| Return
|
||||||
|
| SqlStmt
|
||||||
|
| StructDecl
|
||||||
|
| TypeDecl
|
||||||
|
|
||||||
pub type ScopeObject = AsmRegister | ConstField | GlobalField | Var
|
pub type ScopeObject = AsmRegister | ConstField | GlobalField | Var
|
||||||
|
|
||||||
// TODO: replace Param
|
// TODO: replace Param
|
||||||
pub type Node = CallArg | ConstField | EmptyNode | EnumField | Expr | File | GlobalField |
|
pub type Node = CallArg
|
||||||
IfBranch | MatchBranch | NodeError | Param | ScopeObject | SelectBranch | Stmt | StructField |
|
| ConstField
|
||||||
StructInitField
|
| EmptyNode
|
||||||
|
| EnumField
|
||||||
|
| Expr
|
||||||
|
| File
|
||||||
|
| GlobalField
|
||||||
|
| IfBranch
|
||||||
|
| MatchBranch
|
||||||
|
| NodeError
|
||||||
|
| Param
|
||||||
|
| ScopeObject
|
||||||
|
| SelectBranch
|
||||||
|
| Stmt
|
||||||
|
| StructField
|
||||||
|
| StructInitField
|
||||||
|
|
||||||
pub struct TypeNode {
|
pub struct TypeNode {
|
||||||
pub:
|
pub:
|
||||||
|
@ -1155,8 +1234,15 @@ pub mut:
|
||||||
}
|
}
|
||||||
|
|
||||||
// [eax+5] | j | displacement literal (e.g. 123 in [rax + 123] ) | eax | true | `a` | 0.594 | 123 | label_name
|
// [eax+5] | j | displacement literal (e.g. 123 in [rax + 123] ) | eax | true | `a` | 0.594 | 123 | label_name
|
||||||
pub type AsmArg = AsmAddressing | AsmAlias | AsmDisp | AsmRegister | BoolLiteral | CharLiteral |
|
pub type AsmArg = AsmAddressing
|
||||||
FloatLiteral | IntegerLiteral | string
|
| AsmAlias
|
||||||
|
| AsmDisp
|
||||||
|
| AsmRegister
|
||||||
|
| BoolLiteral
|
||||||
|
| CharLiteral
|
||||||
|
| FloatLiteral
|
||||||
|
| IntegerLiteral
|
||||||
|
| string
|
||||||
|
|
||||||
pub struct AsmRegister {
|
pub struct AsmRegister {
|
||||||
pub mut:
|
pub mut:
|
||||||
|
|
|
@ -1,7 +1,18 @@
|
||||||
module ast
|
module ast
|
||||||
|
|
||||||
pub type ComptTimeConstValue = EmptyExpr | byte | f32 | f64 | i16 | i64 | i8 | int | rune |
|
pub type ComptTimeConstValue = EmptyExpr
|
||||||
string | u16 | u32 | u64
|
| byte
|
||||||
|
| f32
|
||||||
|
| f64
|
||||||
|
| i16
|
||||||
|
| i64
|
||||||
|
| i8
|
||||||
|
| int
|
||||||
|
| rune
|
||||||
|
| string
|
||||||
|
| u16
|
||||||
|
| u32
|
||||||
|
| u64
|
||||||
|
|
||||||
pub fn empty_comptime_const_expr() ComptTimeConstValue {
|
pub fn empty_comptime_const_expr() ComptTimeConstValue {
|
||||||
return EmptyExpr{}
|
return EmptyExpr{}
|
||||||
|
|
|
@ -16,8 +16,20 @@ import v.pref
|
||||||
|
|
||||||
pub type Type = int
|
pub type Type = int
|
||||||
|
|
||||||
pub type TypeInfo = Aggregate | Alias | Array | ArrayFixed | Chan | Enum | FnType | GenericInst |
|
pub type TypeInfo = Aggregate
|
||||||
Interface | Map | MultiReturn | Struct | SumType | Thread
|
| Alias
|
||||||
|
| Array
|
||||||
|
| ArrayFixed
|
||||||
|
| Chan
|
||||||
|
| Enum
|
||||||
|
| FnType
|
||||||
|
| GenericInst
|
||||||
|
| Interface
|
||||||
|
| Map
|
||||||
|
| MultiReturn
|
||||||
|
| Struct
|
||||||
|
| SumType
|
||||||
|
| Thread
|
||||||
|
|
||||||
pub enum Language {
|
pub enum Language {
|
||||||
v
|
v
|
||||||
|
|
|
@ -1262,6 +1262,7 @@ pub fn (mut f Fmt) fn_type_decl(node ast.FnTypeDecl) {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (mut f Fmt) sum_type_decl(node ast.SumTypeDecl) {
|
pub fn (mut f Fmt) sum_type_decl(node ast.SumTypeDecl) {
|
||||||
|
start_pos := f.out.len
|
||||||
if node.is_pub {
|
if node.is_pub {
|
||||||
f.write('pub ')
|
f.write('pub ')
|
||||||
}
|
}
|
||||||
|
@ -1269,19 +1270,27 @@ pub fn (mut f Fmt) sum_type_decl(node ast.SumTypeDecl) {
|
||||||
f.write_generic_types(node.generic_types)
|
f.write_generic_types(node.generic_types)
|
||||||
f.write(' = ')
|
f.write(' = ')
|
||||||
|
|
||||||
mut sum_type_names := []string{}
|
mut sum_type_names := node.variants.map(f.table.type_to_str_using_aliases(it.typ,
|
||||||
for t in node.variants {
|
f.mod2alias))
|
||||||
sum_type_names << f.table.type_to_str_using_aliases(t.typ, f.mod2alias)
|
|
||||||
}
|
|
||||||
sum_type_names.sort()
|
sum_type_names.sort()
|
||||||
|
|
||||||
|
mut separator := ' | '
|
||||||
|
// if line length is too long, put each type on its own line
|
||||||
|
mut line_length := f.out.len - start_pos
|
||||||
|
for sum_type_name in sum_type_names {
|
||||||
|
// 3 = length of ' = ' or ' | '
|
||||||
|
line_length += 3 + sum_type_name.len
|
||||||
|
if line_length > fmt.max_len.last() {
|
||||||
|
separator = '\n\t| '
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for i, name in sum_type_names {
|
for i, name in sum_type_names {
|
||||||
|
if i > 0 {
|
||||||
|
f.write(separator)
|
||||||
|
}
|
||||||
f.write(name)
|
f.write(name)
|
||||||
if i < sum_type_names.len - 1 {
|
|
||||||
f.write(' | ')
|
|
||||||
}
|
|
||||||
if i < sum_type_names.len - 1 {
|
|
||||||
f.wrap_long_line(3, true)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
f.comments(node.comments, has_nl: false)
|
f.comments(node.comments, has_nl: false)
|
||||||
|
|
|
@ -0,0 +1,13 @@
|
||||||
|
import v.ast
|
||||||
|
|
||||||
|
type ManyExpr = ast.AnonFn
|
||||||
|
| ast.ArrayDecompose
|
||||||
|
| ast.ArrayInit
|
||||||
|
| ast.AsCast
|
||||||
|
| ast.AtExpr
|
||||||
|
| ast.BoolLiteral
|
||||||
|
| ast.CTempVar
|
||||||
|
| ast.CallExpr
|
||||||
|
| ast.CastExpr
|
||||||
|
|
||||||
|
type SomeStmt = ast.AsmStmt | ast.AssertStmt
|
|
@ -0,0 +1,6 @@
|
||||||
|
import v.ast
|
||||||
|
|
||||||
|
type ManyExpr = ast.AnonFn | ast.ArrayDecompose | ast.ArrayInit | ast.AsCast | ast.AtExpr | ast.BoolLiteral | ast.CTempVar | ast.CallExpr | ast.CastExpr
|
||||||
|
|
||||||
|
type SomeStmt = ast.AsmStmt
|
||||||
|
| ast.AssertStmt
|
|
@ -4,8 +4,7 @@
|
||||||
module json2
|
module json2
|
||||||
|
|
||||||
// `Any` is a sum type that lists the possible types to be decoded and used.
|
// `Any` is a sum type that lists the possible types to be decoded and used.
|
||||||
pub type Any = Null | []Any | bool | f32 | f64 | i64 | int | map[string]Any | string |
|
pub type Any = Null | []Any | bool | f32 | f64 | i64 | int | map[string]Any | string | u64
|
||||||
u64
|
|
||||||
|
|
||||||
// `Null` struct is a simple representation of the `null` value in JSON.
|
// `Null` struct is a simple representation of the `null` value in JSON.
|
||||||
pub struct Null {
|
pub struct Null {
|
||||||
|
|
Loading…
Reference in New Issue