Documentation
¶
Index ¶
- Constants
- Variables
- func Compile(platform Platform, builtin FileSystem, args []string)
- func FileNode(f *File) *ast.File
- func Generate(c *Compiler) error
- func IsTemplateNotFoundError(err error) bool
- func LenExpr(a *ArrayType) ast.Expr
- func UsedTypeNode(u *UsedType) ast.Type
- type Annotation
- type Annotations
- type ArrayType
- type CallStmt
- type Comment
- type Compiler
- func (c *Compiler) AddFile(f *ast.File) (*File, error)
- func (c *Compiler) Debug(msg string, args ...any)
- func (c *Compiler) Error(args ...any)
- func (c *Compiler) FileSet() *token.FileSet
- func (c *Compiler) Files() map[string]*File
- func (c *Compiler) GetFile(path string) *File
- func (c *Compiler) Getenv(key string) (string, bool)
- func (c *Compiler) Output() io.Writer
- func (c *Compiler) Position() token.Position
- func (c *Compiler) Resolve() error
- func (c *Compiler) SetupCommandFlags(flagSet *flag.FlagSet, u flags.UsageFunc)
- func (c *Compiler) Trace(msg string, args ...any)
- func (c *Compiler) ValidateGrammar() error
- type Const
- func (d Const) Annotations() Annotations
- func (d Const) Doc() *Doc
- func (x Const) File() *File
- func (x Const) Name() string
- func (x Const) NamePos() Position
- func (x Const) Package() *Package
- func (x Const) Pos() Position
- func (x *Const) Type() *PrimitiveType
- func (*Const) Typeof() string
- func (x *Const) UsedKinds() Kinds
- func (x *Const) Value() *Value
- type Consts
- type Decl
- type DeclType
- func (d *DeclType[T]) Actual() reflect.Value
- func (x *DeclType[T]) Decl() Decl
- func (d *DeclType[T]) File() *File
- func (x *DeclType[T]) Kind() Kind
- func (x *DeclType[T]) Package() *Package
- func (x *DeclType[T]) Pos() Position
- func (d *DeclType[T]) String() string
- func (x *DeclType[T]) Typeof() string
- func (x *DeclType[T]) UsedKinds() Kinds
- type Decls
- type Doc
- type Enum
- func (d Enum) Annotations() Annotations
- func (d Enum) Doc() *Doc
- func (x Enum) File() *File
- func (e *Enum) LookupLocalSymbol(name string) Symbol
- func (x Enum) Name() string
- func (x Enum) NamePos() Position
- func (x Enum) Package() *Package
- func (x Enum) Pos() Position
- func (*Enum) Typeof() string
- func (x *Enum) UsedKinds() Kinds
- type EnumMember
- func (d EnumMember) Annotations() Annotations
- func (d EnumMember) Doc() *Doc
- func (x EnumMember) File() *File
- func (m *EnumMember) Index() int
- func (m *EnumMember) IsFirst() bool
- func (m *EnumMember) IsLast() bool
- func (x EnumMember) Name() string
- func (x EnumMember) NamePos() Position
- func (x EnumMember) Package() *Package
- func (x EnumMember) Pos() Position
- func (*EnumMember) Typeof() string
- func (m *EnumMember) Value() *Value
- type EnumMembers
- type EnumType
- type Enums
- type Fields
- type File
- func (f *File) Annotations() Annotations
- func (f *File) Decls() *Decls[*File]
- func (f *File) Doc() *Doc
- func (x *File) File() *File
- func (f *File) Imports() *Imports[*File]
- func (f *File) LookupLocalSymbol(name string) Symbol
- func (f *File) LookupLocalType(name string) (Type, error)
- func (f *File) LookupLocalValue(name string) (*Value, error)
- func (x *File) Name() string
- func (x *File) NamePos() Position
- func (x *File) Package() *Package
- func (x *File) Pos() Position
- func (*File) Typeof() string
- func (x *File) UsedKinds() Kinds
- type FileSystem
- type Formatter
- type Import
- type Imports
- type Interface
- func (d Interface) Annotations() Annotations
- func (d Interface) Doc() *Doc
- func (x Interface) File() *File
- func (i *Interface) Methods() *InterfaceMethods
- func (x Interface) Name() string
- func (x Interface) NamePos() Position
- func (x Interface) Package() *Package
- func (x Interface) Pos() Position
- func (*Interface) Typeof() string
- func (x *Interface) UsedKinds() Kinds
- type InterfaceMethod
- func (d InterfaceMethod) Annotations() Annotations
- func (d InterfaceMethod) Doc() *Doc
- func (x InterfaceMethod) File() *File
- func (m *InterfaceMethod) Index() int
- func (m *InterfaceMethod) IsFirst() bool
- func (m *InterfaceMethod) IsLast() bool
- func (x InterfaceMethod) Name() string
- func (x InterfaceMethod) NamePos() Position
- func (x InterfaceMethod) Package() *Package
- func (x InterfaceMethod) Pos() Position
- func (*InterfaceMethod) Typeof() string
- type InterfaceMethodParameter
- func (d InterfaceMethodParameter) Annotations() Annotations
- func (d InterfaceMethodParameter) Doc() *Doc
- func (x InterfaceMethodParameter) File() *File
- func (p *InterfaceMethodParameter) Index() int
- func (p *InterfaceMethodParameter) IsFirst() bool
- func (p *InterfaceMethodParameter) IsLast() bool
- func (x InterfaceMethodParameter) Name() string
- func (x InterfaceMethodParameter) NamePos() Position
- func (x InterfaceMethodParameter) Package() *Package
- func (x InterfaceMethodParameter) Pos() Position
- func (*InterfaceMethodParameter) Typeof() string
- type InterfaceMethodParams
- type InterfaceMethodResult
- type InterfaceMethods
- type InterfaceType
- type Interfaces
- type Kind
- func (k Kind) Bits() int
- func (k Kind) Compatible(other Kind) Kind
- func (k Kind) IsAny() bool
- func (k Kind) IsArray() bool
- func (k Kind) IsBool() bool
- func (k Kind) IsByte() bool
- func (k Kind) IsBytes() bool
- func (k Kind) IsDuration() bool
- func (k Kind) IsEnum() bool
- func (k Kind) IsFloat() bool
- func (k Kind) IsInteger() bool
- func (k Kind) IsInterface() bool
- func (k Kind) IsMap() bool
- func (k Kind) IsNumeric() bool
- func (k Kind) IsPrimitive() bool
- func (k Kind) IsString() bool
- func (k Kind) IsStruct() bool
- func (k Kind) IsTime() bool
- func (k Kind) IsVector() bool
- func (k *Kind) Set(s string) error
- func (i Kind) String() string
- func (k Kind) Valid() bool
- type Kinds
- type List
- type LocatedObject
- type MapType
- type Meta
- type Node
- type Object
- type Options
- type Package
- func (p *Package) Annotations() Annotations
- func (p *Package) Decls() *Decls[*Package]
- func (p *Package) Doc() *Doc
- func (p *Package) File() *File
- func (p *Package) Files() []*File
- func (p *Package) Has(obj Object) (bool, error)
- func (p *Package) Imports() *Imports[*Package]
- func (p *Package) LookupLocalType(name string) (Type, error)
- func (p *Package) LookupLocalValue(name string) (*Value, error)
- func (p *Package) Name() string
- func (x *Package) NamePos() Position
- func (p *Package) Package() *Package
- func (p *Package) Pos() Position
- func (*Package) Typeof() string
- func (p *Package) Types() []Type
- func (x *Package) UsedKinds() Kinds
- type Packages
- type Platform
- type Position
- type PrimitiveType
- type Scope
- type Stmt
- type Struct
- func (d Struct) Annotations() Annotations
- func (d Struct) Doc() *Doc
- func (s *Struct) Fields() *StructFields
- func (x Struct) File() *File
- func (x Struct) Name() string
- func (x Struct) NamePos() Position
- func (x Struct) Package() *Package
- func (x Struct) Pos() Position
- func (*Struct) Typeof() string
- func (x *Struct) UsedKinds() Kinds
- type StructField
- func (d StructField) Annotations() Annotations
- func (d StructField) Doc() *Doc
- func (x StructField) File() *File
- func (f *StructField) Index() int
- func (f *StructField) IsFirst() bool
- func (f *StructField) IsLast() bool
- func (x StructField) Name() string
- func (x StructField) NamePos() Position
- func (x StructField) Package() *Package
- func (x StructField) Pos() Position
- func (*StructField) Typeof() string
- type StructFields
- type StructType
- type Structs
- type Symbol
- type SymbolNotFoundError
- type SymbolRedefinedError
- type TemplateNotFoundError
- type Type
- type UnexpectedSymbolTypeError
- type UsedType
- type Value
- type VectorType
Constants ¶
const NEXT_MAXSTACK = "NEXT_MAXSTACK"
@api(Environment/NEXT_MAXSTACK) represents the environment variable to set the maximum stack depth. The value is a positive integer that represents the maximum stack depth. The default value is 100.
const NEXT_NOCOPYBUILTIN = "NEXT_NOCOPYBUILTIN"
@api(Environment/NEXT_NOCOPYBUILTIN) represents the environment variable to disable copying builtin files. If the value is `1`, `true`, `on`, or `yes` (case-insensitive), builtin files will not be copied to the user home directory.
const NEXT_PATH = "NEXT_PATH"
@api(Environment/NEXT_PATH) represents the environment variable to set the search path for the next files. Search path returns an ordered list of directories to search for language support map and template files. The order is from the most specific to the least specific. The most specific directory is the NEXT_PATH, then user's home directory. The least specific directories are the system directories. The system directories are:
- /etc/next.d
- /usr/local/etc/next.d
- /Library/Application Support/next.d
- ~/Library/Application Support/next.d
- %APPDATA%/next.d
const StubPrefix = "next_stub_0041b8dcd21c3ad6ea2eb3a9f033a9861bc2873e6ab05106e8684ce1b961d4a7_"
StubPrefix is the prefix for stub templates.
Variables ¶
Functions ¶
func Compile ¶
func Compile(platform Platform, builtin FileSystem, args []string)
Compile compiles the next files.
func IsTemplateNotFoundError ¶
func UsedTypeNode ¶
UsedTypeNode returns the AST node of the used type.
Types ¶
type Annotation ¶
@api(Object/Common/Annotation) represents an annotation by `name` => value.
Annotation is a map that stores the parameters of a single annotation. It allows for flexible parameter types, including strings, numbers, booleans and Type(#Object/Common/Type)s.
Example:
Next code:
```next
@json(omitempty)
@event(name="Login")
@message(name="Login", type=100)
struct Login {}
@next(type=int8)
enum Color {
Red = 1;
Green = 2;
Blue = 3;
}
```
Will be represented as:
```npl
{{- define "go/struct" -}}
{{.Annotations.json.omitempty}}
{{.Annotations.event.name}}
{{.Annotations.message.name}}
{{.Annotations.message.type}}
{{- end}}
{{- define "go/enum" -}}
{{.Annotations.next.type}}
{{- end}}
```
Output:
``` true Login Login 100 int8 ```
The `next` annotation is used to pass information to the next compiler. It's a reserved annotation and should not be used for other purposes. The `next` annotation can be annotated to `package` statements, `const` declarations, `enum` declarations, `struct` declarations, `field` declarations, `interface` declarations, `method` declarations, and `parameter` declarations.
:::note
Annotation name **MUST NOT** be "Has", it's a reserved method for checking whether the annotation contains the given parameter. Parameter name **MUST NOT** be start with "_" and uppercase letter (A-Z).
```next @message(type=100) // OK // This will error @message(_type=100) // invalid parameter name "_type": must not start with an underscore (_) // This will error @next(Pos=100) // invalid parameter name "Pos": must not start with an uppercase letter (A-Z) ```
:::
func (Annotation) Has ¶
func (a Annotation) Has(name string) bool
@api(Object/Common/Annotation.Has) reports whether the annotation contains the given parameter.
Example:
```next
@json(omitempty)
struct User {/*...*/}
```
```npl
{{if .Annotations.json.Has "omitempty"}}
{{/* do something */}}
{{end}}
```
:::note
If you want to check whether the annotation has a non-empty value, you can use the parameter name directly.
```npl
{{if .Annotations.json.omitempty}}
{{/* do something */}}
{{end}}
```
:::
func (Annotation) Len ¶ added in v0.2.6
func (a Annotation) Len() int
@api(Object/Common/Annotation.Len) returns the number of parameters in the annotation.
func (Annotation) NamePos ¶
func (a Annotation) NamePos(name string) Position
@api(Object/Common/Annotation.NamePos) returns the position of the annotation name in the source code. It's useful to provide a better error message when needed.
Example:
```next title="example.next" showLineNumbers
package demo;
@message(type=100)
struct Login {/*...*/}
```
```npl
{{error "%s: Something went wrong" (.Annotations.message.NamePos "type")}}
```
Output:
``` example.next:3:10: Something went wrong ```
func (Annotation) Node ¶
func (a Annotation) Node() Node
@api(Object/Common/Annotation.Node) returns the node that the annotation is linked to.
func (Annotation) Pos ¶
func (a Annotation) Pos() Position
@api(Object/Common/Annotation.Pos) returns the position of the annotation in the source code. It's useful to provide a better error message when needed.
Example:
```next title="example.next" showLineNumbers
package demo;
@message(type=100)
struct Login {/*...*/}
```
```npl
{{error "%s: Something went wrong" .Annotations.message.Pos}}
```
Output:
``` example.next:3:1: Something went wrong ```
func (Annotation) ValuePos ¶
func (a Annotation) ValuePos(name string) Position
@api(Object/Common/Annotation.ValuePos) returns the position of the annotation value in the source code. It's useful to provide a better error message when needed.
Example:
```next title="example.next" showLineNumbers
package demo;
@message(type=100)
struct Login {/*...*/}
```
```npl
{{error "%s: Something went wrong" (.Annotations.message.ValuePos "type")}}
```
Output:
``` example.next:3:15: Something went wrong ```
type Annotations ¶
type Annotations map[string]Annotation
@api(Object/Common/Annotations) represents a group of annotations by `name` => Annotation(#Object/Common/Annotation).
Annotations is a map that stores multiple annotations for a given entity. The key is the annotation name (string), and the value is the corresponding Annotation(#Object/Common/Annotation) object.
func (Annotations) Has ¶
func (a Annotations) Has(name string) bool
@api(Object/Common/Annotations.Has) reports whether the annotations contain the given annotation.
Example:
```next
@json(omitempty)
struct User {/*...*/}
```
```npl
{{if .Annotations.Has "json"}}
{{/* do something */}}
{{end}}
```
type ArrayType ¶
type ArrayType struct {
// @api(Object/ArrayType.ElemType) represents the element [Type](#Object/Common/Type) of the array.
ElemType Type
// @api(Object/ArrayType.N) represents the number of elements in the array.
N int64
// contains filtered or unexported fields
}
@api(Object/ArrayType) represents an fixed-size array Type(#Object/Common/Type).
type Comment ¶
type Comment struct {
// contains filtered or unexported fields
}
@api(Object/Comment) represents a line comment or a comment group in Next source code. Use this in templates to access and format comments.
func (*Comment) String ¶
@api(Object/Comment.String) returns the full original comment text, including delimiters.
Example:
```next
const x = 1; // This is a comment.
```
```npl
{{.Comment.String}}
```
Output:
``` // This is a comment. ```
type Compiler ¶
type Compiler struct {
// contains filtered or unexported fields
}
Compiler represents a compiler of a Next program
func NewCompiler ¶
func NewCompiler(platform Platform, builtin FileSystem) *Compiler
NewCompiler creates a new compiler with builtin language supports
func (*Compiler) SetupCommandFlags ¶
func (*Compiler) ValidateGrammar ¶
ValidateGrammar validates the grammar of the context
type Const ¶
type Const struct {
// @api(Object/Const.Comment) is the line [Comment](#Object/Comment) of the constant declaration.
//
// Example:
//
// “`next
// const x = 1; // This is a line comment for the constant.
// “`
//
// “`npl
// {{.Comment.Text}}
// “`
//
// Output:
//
// “`
// This is a line comment for the constant.
// “`
Comment *Comment
// contains filtered or unexported fields
}
@api(Object/Const) (extends Decl(#Object/Common/Decl)) represents a const declaration.
func (Const) Annotations ¶
func (d Const) Annotations() Annotations
func (*Const) Type ¶
func (x *Const) Type() *PrimitiveType
@api(Object/Const.Type) represents the type of the constant.
type Consts ¶
@api(Object/Consts) represents a List(#Object/Common/List) of Const(#Object/Const) declarations.
type Decl ¶
type Decl interface {
Node
// @api(Object/Common/Decl.UsedKinds) returns the used kinds in the declaration.
// Returns 0 if the declaration does not use any kinds.
// Otherwise, returns the OR of all used kinds.
//
// Example:
//
// “`next
// struct User {
// int64 id;
// string name;
// vector<string> emails;
// map<int, bool> flags;
// }
// “`
// The used kinds in the `User` struct are: `(1<<KindInt64) | (1<<KindString) | (1<<KindVector) | (1<<KindMap) | (1<<KindInt) | (1<<KindBool)`.
UsedKinds() Kinds
// contains filtered or unexported methods
}
@api(Object/Common/Decl) represents a top-level declaration Node(#Object/Common/Node) in a file.
Currently, the following declarations are supported:
- Package(#Object/Package) - File(#Object/File) - Const(#Object/Const) - Enum(#Object/Enum) - Struct(#Object/Struct) - Interface(#Object/Interface)
type DeclType ¶
type DeclType[T Decl] struct { // contains filtered or unexported fields }
DeclType represents a declaration type which is a type of a declaration: enum, struct, interface.
type Decls ¶
type Decls[T Decl] struct { Decl T // contains filtered or unexported fields }
@api(Object/Decls) holds all top-level declarations in a file.
func (*Decls[T]) Consts ¶
@api(Object/Decls.Consts) represents the List(#Object/Common/List) of Const(#Object/Const) declarations.
func (*Decls[T]) Enums ¶
@api(Object/Decls.Enums) represents the List(#Object/Common/List) of Enum(#Object/Enum) declarations.
func (*Decls[T]) Interfaces ¶
func (d *Decls[T]) Interfaces() Interfaces
@api(Object/Decls.Interfaces) represents the List(#Object/Common/List) of Interface(#Object/Interface) declarations.
type Doc ¶
type Doc struct {
// contains filtered or unexported fields
}
@api(Object/Doc) represents a documentation comment for a declaration in Next source code. Use this in templates to access and format documentation comments.
func (*Doc) Format ¶
@api(Object/Doc.Format) formats the documentation comment for various output styles.
Parameters: (_indent_ string[, _begin_ string[, _end_ string]])
Example:
```next
// This is a documentation comment.
// It can be multiple lines.
const x = 1;
```
```npl
{{.Doc.Format "/// "}}
{{.Doc.Format " * " "/**\n" " */"}}
```
Output:
``` /// This is a documentation comment. /// It can be multiple lines. /** * This is a documentation comment. * It can be multiple lines. */ ```
func (*Doc) String ¶
@api(Object/Doc.String) returns the full original documentation comment, including delimiters.
Example:
```next
// This is a documentation comment.
// It can be multiple lines.
const x = 1;
```
```npl
{{.Doc.String}}
```
Output:
``` // This is a documentation comment. // It can be multiple lines. ```
func (*Doc) Text ¶
@api(Object/Doc.Text) returns the content of the documentation comment without comment delimiters.
Example:
```next
// This is a documentation comment.
// It can be multiple lines.
const x = 1;
```
```npl
{{.Doc.Text}}
```
Output:
``` This is a documentation comment. It can be multiple lines. ```
type Enum ¶
type Enum struct {
// @api(Object/Enum.MemberType) represents the [PrimitiveType](#Object/PrimitiveType) of the enum members.
MemberType *PrimitiveType
// @api(Object/Enum.Type) is the [EnumType](#Object/EnumType) of the enum.
Type *EnumType
// @api(Object/Enum.Members) is the [Fields](#Object/Common/Fields) of [EnumMember](#Object/EnumMember).
Members *EnumMembers
// contains filtered or unexported fields
}
@api(Object/Enum) (extends Decl(#Object/Common/Decl)) represents an enum declaration.
func (Enum) Annotations ¶
func (d Enum) Annotations() Annotations
func (*Enum) LookupLocalSymbol ¶
type EnumMember ¶
type EnumMember struct {
// @api(Object/EnumMember.Decl) represents the [Enum](#Object/Enum) that contains the member.
Decl *Enum
// @api(Object/EnumMember.Comment) represents the line [Comment](#Object/Comment) of the enum member declaration.
Comment *Comment
// contains filtered or unexported fields
}
@api(Object/EnumMember) (extends Decl(#Object/Common/Decl)) represents an enum member object in an Enum(#Object/Enum) declaration.
func (EnumMember) Annotations ¶
func (d EnumMember) Annotations() Annotations
func (*EnumMember) Index ¶
func (m *EnumMember) Index() int
@api(Object/EnumMember.Index) represents the index of the enum member in the enum type.
func (*EnumMember) IsFirst ¶
func (m *EnumMember) IsFirst() bool
@api(Object/Value.IsFirst) reports whether the value is the first member of the enum type.
Example:
```next
enum Color {
Red = 1; // IsFirst is true for Red
Green = 2;
Blue = 3;
}
```
func (*EnumMember) IsLast ¶
func (m *EnumMember) IsLast() bool
@api(Object/Value.IsLast) reports whether the value is the last member of the enum type.
Example:
```next
enum Color {
Red = 1;
Green = 2;
Blue = 3; // IsLast is true for Blue
}
```
func (*EnumMember) Typeof ¶
func (*EnumMember) Typeof() string
func (*EnumMember) Value ¶
func (m *EnumMember) Value() *Value
@api(Object/EnumMember.Value) represents the Value(#Object/Value) object of the enum member.
type EnumMembers ¶
type EnumMembers = Fields[*Enum, *EnumMember]
@api(Object/EnumMembers) represents the Fields(#Object/Common/Fields) of [EnumEember](#Object/EnumMember) in an Enum(#Object/Enum) declaration.
type EnumType ¶
@api(Object/EnumType) represents the Type(#Object/Common/Type) of an Enum(#Object/Enum) declaration.
type Enums ¶
@api(Object/Enums) represents a List(#Object/Common/List) of Enum(#Object/Enum) declarations.
type Fields ¶
type Fields[D, F Node] struct { // @api(Object/Common/Fields.Decl) is the declaration [Node](#Object/Common/Node) that contains the fields. // // Currently, it is one of following types: // // - [Enum](#Object/Enum) // - [Struct](#Object/Struct) // - [Interface](#Object/Interface) // - [InterfaceMethod](#Object/InterfaceMethod). Decl D // @api(Object/Common/Fields.List) is the slice of fields: **\[[Object](#Object)\]**. // // Currently, the field object is one of following types: // // - [EnumMember](#Object/EnumMember) // - [StructField](#Object/StructField) // - [InterfaceMethod](#Object/InterfaceMethod). // - [InterfaceMethodParameter](#Object/InterfaceMethodParameter). List []F }
@api(Object/Common/Fields) `Fields<D, F>` represents a list of fields of a declaration where `D` is the declaration Node(#Object/Common/Node) and `F` is the field object Node(#Object/Common/Node).
type File ¶
type File struct {
// @api(Object/File.Path) represents the file full path.
Path string
// contains filtered or unexported fields
}
@api(Object/File) (extends Decl(#Object/Common/Decl)) represents a Next source file.
func (*File) Annotations ¶
func (f *File) Annotations() Annotations
func (*File) LookupLocalSymbol ¶
func (*File) LookupLocalType ¶
@api(Object/File.LookupLocalType) looks up a type by name in the file's symbol table. If the type is not found, it returns an error. If the symbol is found but it is not a type, it returns an error.
func (*File) LookupLocalValue ¶
@api(Object/File.LookupLocalValue) looks up a value by name in the file's symbol table. If the value is not found, it returns an error. If the symbol is found but it is not a value, it returns an error.
type FileSystem ¶
FileSystem is an interface that abstracts the file system functions.
type Import ¶
type Import struct {
// @api(Object/Import.Doc) represents the [Doc](#Object/Doc) comment of the import.
//
// Example:
//
// “`next
// // This is a documenation comment for the import.
// // It can be multiline.
// import "path/to/file.next"; // This is a line comment for the import declaration.
// “`
//
// “`npl
// {{.Doc.Text}}
// “`
//
// Output:
//
// “`
// This is a documenation comment for the import.
// It can be multiline.
// “`
Doc *Doc
// @api(Object/Import.Comment) represents the line [Comment](#Object/Comment) of the import declaration.
//
// Example:
//
// “`next
// // This is a documenation comment for the import.
// // It can be multiline.
// import "path/to/file.next"; // This is a line comment for the import.
// “`
//
// “`npl
// {{.Comment.Text}}
// “`
//
// Output:
//
// “`
// This is a line comment for the import.
// “`
Comment *Comment
// @api(Object/Import.Path) represents the import path.
//
// Example:
//
// “`next
// import "path/to/file.next";
// “`
//
// “`npl
// {{.Path}}
// “`
//
// Output:
//
// “`
// path/to/file.next
// “`
Path string
// @api(Object/Import.FullPath) represents the full path of the import.
//
// Example:
//
// “`next
// import "path/to/file.next";
// “`
//
// “`npl
// {{.FullPath}}
// “`
//
// Output:
//
// “`
// /full/path/to/file.next
// “`
FullPath string
// contains filtered or unexported fields
}
@api(Object/Import) represents a import declaration in a File(#Object/File).
func (*Import) File ¶
@api(Object/Import.File) represents the File(#Object/File) object that contains the import declaration.
Example:
```next title="file1.next"
package a;
import "file2.next";
import "file3.next";
```
```npl
{{range .Imports.List}}
{{.File.Path}}
{{end}}
```
Output:
```
file1.next
file1.next
```
type Imports ¶
type Imports[T Decl] struct { // @api(Object/Imports.Decl) represents the declaration [Node](#Object/Common/Node) that contains the imports. // Currently, it is one of following types: // // - [File](#Object/File) // - [Package](#Object/Package) // // Example: // // “`next title="file1.next" // package a; // // import "path/to/file2.next"; // “` // // “`next title="file2.next" // package a; // “` // // <Tabs // defaultValue="file" // values={[ // { label: 'file.npl', value: 'file' }, // { label: 'package.npl', value: 'package' }, // ]}> // // <TabItem value="file"> // “`npl // {{- define "meta/this" -}}file{{- end -}} // // {{range this.Imports.List}} // {{.Decl.Name}} // {{end}} // “` // // Output (for file1.next): // // “` // file1 // “` // </TabItem> // // <TabItem value="package"> // “`npl // {{- define "meta/this" -}}package{{- end -}} // // {{range this.Imports.List}} // {{.Decl.Name}} // {{end}} // “` // // Output: // // “` // a // “` // </TabItem> // // </Tabs> Decl Node // @api(Object/Imports.List) represents a slice of [Import](#Object/Import) declarations: **\[[Import](#Object/Import)\]**. // // Example: see [Object/Imports.Decl](#Object/Imports.Decl). List []*Import }
@api(Object/Imports) holds a slice of Import(#Object/Import) declarations and the declaration that contains the imports.
func (*Imports[T]) TrimmedList ¶
@api(Object/Imports.TrimmedList) represents a slice of unique imports sorted by package name.
Example:
```next
package c;
import "path/to/file1.next"; // package: a
import "path/to/file2.next"; // package: a
import "path/to/file3.next"; // package: b
```
```npl
{{range .Imports.TrimmedList}}
{{.Target.Package.Name}}
{{end}}
```
Output:
``` a b ```
type Interface ¶
type Interface struct {
// @api(Object/Interface.Type) represents [InterfaceType](#Object/InterfaceType) of the interface.
Type *InterfaceType
// contains filtered or unexported fields
}
@api(Object/Interface) (extends Decl(#Object/Common/Decl)) represents an interface declaration.
func (Interface) Annotations ¶
func (d Interface) Annotations() Annotations
func (*Interface) Methods ¶
func (i *Interface) Methods() *InterfaceMethods
@api(Object/Interface.Methods) represents the list of interface methods.
type InterfaceMethod ¶
type InterfaceMethod struct {
// @api(Object/InterfaceMethod.Decl) represents the interface that contains the method.
Decl *Interface
// @api(Object/InterfaceMethod.Params) represents the [Fields](#Object/Common/Fields) of [InterfaceMethodParameter](#Object/InterfaceMethodParameter).
Params *InterfaceMethodParams
// @api(Object/InterfaceMethod.Result) represents the [InterfaceMethodResult](#Object/InterfaceMethodResult) of the method.
Result *InterfaceMethodResult
// @api(Object/InterfaceMethod.Comment) represents the line [Comment](#Object/Comment) of the interface method declaration.
Comment *Comment
// contains filtered or unexported fields
}
@api(Object/InterfaceMethod) (extends Node(#Object/Common/Node)) represents an interface method declaration.
func (InterfaceMethod) Annotations ¶
func (d InterfaceMethod) Annotations() Annotations
func (*InterfaceMethod) Index ¶
func (m *InterfaceMethod) Index() int
@api(Object/InterfaceMethod.Index) represents the index of the interface method in the interface.
Example:
```next
interface Shape {
draw(); // Index is 0 for draw
area() float64; // Index is 1 for area
}
```
func (*InterfaceMethod) IsFirst ¶
func (m *InterfaceMethod) IsFirst() bool
@api(Object/InterfaceMethod.IsFirst) reports whether the method is the first method in the interface.
Example:
```next
interface Shape {
draw(); // IsFirst is true for draw
area() float64;
}
```
func (*InterfaceMethod) IsLast ¶
func (m *InterfaceMethod) IsLast() bool
@api(Object/InterfaceMethod.IsLast) reports whether the method is the last method in the interface type.
Example:
```next
interface Shape {
draw();
area() float64; // IsLast is true for area
}
```
func (*InterfaceMethod) Typeof ¶
func (*InterfaceMethod) Typeof() string
type InterfaceMethodParameter ¶
type InterfaceMethodParameter struct {
// @api(Object/InterfaceMethodParameter.Method) represents the [InterfaceMethod](#Object/InterfaceMethod) that contains the parameter.
Method *InterfaceMethod
// @api(Object/InterfaceMethodParameter.Type) represents the [Type](#Object/Common/Type) of the parameter.
Type Type
// contains filtered or unexported fields
}
@api(Object/InterfaceMethodParameter) (extends Node(#Object/Common/Node)) represents an interface method parameter declaration.
func (InterfaceMethodParameter) Annotations ¶
func (d InterfaceMethodParameter) Annotations() Annotations
func (*InterfaceMethodParameter) Index ¶
func (p *InterfaceMethodParameter) Index() int
@api(Object/InterfaceMethodParameter.Index) represents the index of the interface method parameter in the method.
Example:
```next
interface Shape {
draw(int x, int y); // Index is 0 for x, 1 for y
}
```
func (*InterfaceMethodParameter) IsFirst ¶
func (p *InterfaceMethodParameter) IsFirst() bool
@api(Object/InterfaceMethodParameter.IsFirst) reports whether the parameter is the first parameter in the method.
Example:
```next
interface Shape {
draw(int x, int y); // IsFirst is true for x
}
```
func (*InterfaceMethodParameter) IsLast ¶
func (p *InterfaceMethodParameter) IsLast() bool
@api(Object/InterfaceMethodParameter.IsLast) reports whether the parameter is the last parameter in the method. Example:
```next
interface Shape {
draw(int x, int y); // IsLast is true for y
}
```
func (*InterfaceMethodParameter) Typeof ¶
func (*InterfaceMethodParameter) Typeof() string
type InterfaceMethodParams ¶
type InterfaceMethodParams = Fields[*InterfaceMethod, *InterfaceMethodParameter]
@api(Object/InterfaceMethodParams) represents the Fields(#Object/Common/Fields) of InterfaceMethodParameter(#Object/InterfaceMethodParameter) in an InterfaceMethod(#Object/InterfaceMethod) declaration.
type InterfaceMethodResult ¶
type InterfaceMethodResult struct {
// @api(Object/InterfaceMethodResult.Method) represents the [InterfaceMethod](#Object/InterfaceMethod) that contains the result.
Method *InterfaceMethod
// @api(Object/InterfaceMethodResult.Type) represents the underlying [Type](#Object/Common/Type) of the result.
Type Type
// contains filtered or unexported fields
}
@api(Object/InterfaceMethodResult) represents an interface method result.
func (*InterfaceMethodResult) Typeof ¶
func (*InterfaceMethodResult) Typeof() string
type InterfaceMethods ¶
type InterfaceMethods = Fields[*Interface, *InterfaceMethod]
@api(Object/InterfaceMethods) represents the Fields(#Object/Common/Fields) of InterfaceMethod(#Object/InterfaceMethod) in an Interface(#Object/Interface) declaration.
type InterfaceType ¶
@api(Object/InterfaceType) represents the Type(#Object/Common/Type) of an Interface(#Object/Interface) declaration.
type Interfaces ¶
@api(Object/Interfaces) represents a List(#Object/Common/List) of Interface(#Object/Interface) declarations.
type Kind ¶
type Kind int
const ( KindInvalid Kind = iota // invalid KindBool // bool KindInt // int KindInt8 // int8 KindInt16 // int16 KindInt32 // int32 KindInt64 // int64 KindFloat32 // float32 KindFloat64 // float64 KindByte // byte KindBytes // bytes KindString // string KindTime // time KindDuration // duration KindAny // any KindMap // map KindVector // vector KindArray // array KindEnum // enum KindStruct // struct KindInterface // interface )
@api(Object/Common/Type/Kind) represents the type kind. Currently, the following kinds are supported:
- **bool**: true or false - **int**: integer - **int8**: 8-bit integer - **int16**: 16-bit integer - **int32**: 32-bit integer - **int64**: 64-bit integer - **float32**: 32-bit floating point - **float64**: 64-bit floating point - **byte**: byte - **bytes**: byte slice - **string**: string - **time**: time - **duration**: duration - **any**: any object - **map**: dictionary - **vector**: vector of elements - **array**: array of elements - **enum**: enumeration - **struct**: structure - **interface**: interface
func (Kind) Bits ¶
@api(Object/Common/Type/Kind.Bits) returns the number of bits for the type. If the type has unknown bits, it returns 0 (for example, `any`, `string`, `bytes`).
func (Kind) Compatible ¶
@api(Object/Common/Type/Kind.Compatible) returns the compatible type kind between two kinds. If the kinds are not compatible, it returns `KindInvalid`. If the kinds are the same, it returns the kind. If the kinds are both numeric, it returns the kind with the most bits.
func (Kind) IsBytes ¶
@api(Object/Common/Type/Kind.IsBytes) reports whether the type is a byte slice.
func (Kind) IsDuration ¶ added in v0.2.8
@api(Object/Common/Type/Kind.IsDuration) reports whether the type is a duration.
func (Kind) IsEnum ¶
@api(Object/Common/Type/Kind.IsEnum) reports whether the type is an enumeration.
func (Kind) IsFloat ¶
@api(Object/Common/Type/Kind.IsFloat) reports whether the type is a floating point. It includes `float32` and `float64`.
func (Kind) IsInteger ¶
@api(Object/Common/Type/Kind.IsInteger) reports whether the type is an integer. It includes `int`, `int8`, `int16`, `int32`, `int64`, and `byte`.
func (Kind) IsInterface ¶
@api(Object/Common/Type/Kind.IsInterface) reports whether the type is an interface.
func (Kind) IsNumeric ¶
@api(Object/Common/Type/Kind.IsNumeric) reports whether the type is a numeric type. It includes integer and floating point types.
func (Kind) IsPrimitive ¶
@api(Object/Common/Type/Kind.IsPrimitive) reports whether the type is a PrimitiveType(#Object/PrimitiveType).
func (Kind) IsStruct ¶
@api(Object/Common/Type/Kind.IsStruct) reports whether the type is a structure.
func (Kind) IsTime ¶ added in v0.2.8
@api(Object/Common/Type/Kind.IsTime) reports whether the type is a time.
type List ¶
type List[T Object] []T
@api(Object/Common/List) `List<T>` represents a slice of objects: **\[T: Object(#Object)\]**.
type LocatedObject ¶
type LocatedObject interface {
Object
// @api(Object/Common/LocatedObject.Pos) represents the [Position](#Object/Common/Position) of the object.
//
// Example:
//
// “`next title="demo.next"
// package demo;
// const Name = "hei hei";
// “`
//
// “`npl
// {{- define "meta/this" -}}const{{- end -}}
// {{this.Pos}}
// {{this.Value.Pos}}
// “`
//
// Output:
//
// “`
// demo.next:2:1
// demo.next:2:14
// “`
Pos() Position
// @api(Object/Common/LocatedObject.File) represents the file containing the object.
File() *File
// @api(Object/Common/LocatedObject.Package) represents the package containing the object.
Package() *Package
}
@api(Object/Common/LocatedObject) represents an Object(#Object) with a location in a file.
type MapType ¶
type MapType struct {
// @api(Object/MapType.KeyType) represents the key [Type](#Object/Common/Type) of the map.
KeyType Type
// @api(Object/MapType.ElemType) represents the element [Type](#Object/Common/Type) of the map.
ElemType Type
// contains filtered or unexported fields
}
@api(Object/MapType) represents a map Type(#Object/Common/Type).
type Node ¶
type Node interface {
LocatedObject
// @api(Object/Common/Node.Name) represents the name of the node.
//
// Example:
//
// “`next
// const x = 1;
// “`
//
// “`npl
// {{.Name}}
// “`
//
// Output:
//
// “`
// x
// “`
Name() string
// @api(Object/Common/Node.NamePos) represents the position of the node name.
//
// Example:
//
// “`next title="demo.next"
// package demo;
// const x = 1;
// “`
//
// “`npl
// {{.NamePos}}
// “`
//
// Output:
//
// “`
// demo.next:2:7
// “`
NamePos() Position
// @api(Object/Common/Node.Doc) represents the documentation comment for the node.
// The documentation comment is a comment that appears before the node declaration.
//
// Example:
//
// “`next
// // This is a documentation comment for the node.
// // It can be multiple lines.
// const x = 1;
// “`
//
// “`npl
// {{.Doc.Text}}
// “`
//
// Output:
//
// “`
// This is a documentation comment for the node.
// It can be multiple lines.
// “`
Doc() *Doc
// @api(Object/Common/Node.Annotations) represents the [Annotations](#Object/Common/Annotations) for the node.
//
// Example:
//
// “`next title="demo.next"
// package demo;
// @next(type=int8)
// @custom
// enum Color {
// Red = 1;
// Green = 2;
// Blue = 3;
// }
// “`
//
// “`npl
// {{.Annotations.next.type}}
// {{.Annotations.next.Pos}}
// {{.Annotations.Has "custom"}}
// “`
//
// Output:
//
// “`
// int8
// demo.next:2:1
// true
// “`
Annotations() Annotations
}
@api(Object/Common/Node) represents a LocatedObject(#Object/Common/LocatedObject) that is a node in a file.
Currently, the following nodes are supported:
- File(#Object/File) - Const(#Object/Const) - Enum(#Object/Enum) - Struct(#Object/Struct) - Interface(#Object/Interface) - EnumMember(#Object/EnumMember) - StructField(#Object/StructField) - InterfaceMethod(#Object/InterfaceMethod) - InterfaceMethodParameter(#Object/InterfaceMethodParameter)
type Object ¶
type Object interface {
// @api(Object.Typeof) returns the type name of the object.
// The type name is a string that represents the type of the object.
// Except for objects under [Common](#Object/Common), the type names of other objects are lowercase names
// separated by dots. For example, the type name of a `EnumMember` object is `enum.member`, and the type name
// of a `Enum` object is `enum`. These objects can be customized for code generation by defining templates.
//
// Example:
//
// “`next
// package demo;
//
// enum Color {
// Red = 1;
// Green = 2;
// Blue = 3;
// }
// “`
//
// “`npl
// {{- define "go/enum.member" -}}
// const {{render "enum.member:name" .Name}} = {{next .Value}}
// {{- end}}
//
// {{- define "go/enum.member:name" -}}
// {{.Decl.Name}}_{{.}}
// {{- end}}
// “`
//
// Output:
//
// “`go
// package demo
//
// type Color int
//
// const Color_Red = 1
// const Color_Green = 2
// const Color_Blue = 3
// “`
//
// These two definitions will override the built-in template functions `next/go/enum.member` and `next/go/enum.member:name`.
Typeof() string
}
@api(Object)
import Tabs from "@theme/Tabs"; import TabItem from "@theme/TabItem";
`Object` is a generic object type. These objects can be used as parameters for the `Context/next` function, like `{{next .}}`.
type Options ¶
type Options struct {
// @api(CommandLine/Options.verbose) represents the verbosity level of the compiler.
//
// Example:
//
// “`yaml
// verbose: 1
// “`
//
// See the [-v](#CommandLine/Flag/-v) flag for more information.
Verbose int `yaml:"verbose" json:"verbose"`
// @api(CommandLine/Options.head) represents the header comment for generated code.
//
// Example:
//
// “`yaml
// head: "Code generated by Next; DO NOT EDIT."
// “`
//
// See the [-head](#CommandLine/Flag/-head) flag for more information.
Head string `yaml:"head" json:"head"`
// @api(CommandLine/Options.perm) represents the file permission for generated code if `{{head}}` is called.
Perm int `yaml:"perm" json:"perm"`
// @api(CommandLine/Options.grammar) represents the custom grammar for the next source code.
//
// Example:
//
// “`yaml
// grammar: grammar.yaml
// “`
//
// See the [-grammar](#CommandLine/Flag/-grammar) flag for more information.
Grammar string `yaml:"grammar" json:"grammar"`
// @api(CommandLine/Options.strict) represents the strict mode of the compiler.
//
// Example:
//
// “`yaml
// strict: true
// “`
//
// See the [-s](#CommandLine/Flag/-s) flag for more information.
Strict bool `yaml:"strict" json:"strict"`
// @api(CommandLine/Options.env) represents the custom environment variables for code generation.
//
// Example:
//
// “`yaml
// env:
// VERSION: "2.1"
// DEBUG: ""
// NAME: myapp
// “`
//
// See the [-D](#CommandLine/Flag/-D) flag for more information.
Env flags.Map `yaml:"env" json:"env"`
// @api(CommandLine/Options.output) represents the output directories for generated code of each target language.
//
// Example:
//
// “`yaml
// output:
// go: ./output/go
// ts: ./output/ts
// “`
//
// See the [-O](#CommandLine/Flag/-O) flag for more information.
Output flags.Map `yaml:"output" json:"output"`
// @api(CommandLine/Options.formatter) represents the custom formatter for generated code.
//
// Example:
//
// “`yaml
// formatter:
// go: gofmt -s -w
// ts: prettier --write
// java: java -jar /path/to/google-java-format-1.24.0-all-deps.jar -i
// cpp: clang-format -i
// “`
Formatter flags.Map `yaml:"formatter" json:"formatter"`
// @api(CommandLine/Options.mapping) represents the language-specific type mappings and features.
//
// Example:
//
// “`yaml
// mapping:
// cpp.vector: "std::vector<%T%>"
// java.array: "ArrayList<%T%>"
// go.map: "map[%K%]%V%"
// python.ext: ".py"
// protobuf.vector: "repeated %T.E%"
// ruby.comment: "# %T%"
// “`
//
// See the [-M](#CommandLine/Flag/-M) flag for more information.
Mapping flags.Map `yaml:"mapping" json:"mapping"`
// @api(CommandLine/Options.templates) represents the custom template directories or files for each target language.
//
// Example:
//
// “`yaml
// templates:
// go:
// - ./templates/go
// - ./templates/go_extra.npl
// python:
// - ./templates/python.npl
// “`
//
// See the [-T](#CommandLine/Flag/-T) flag for more information.
Templates flags.MapSlice `yaml:"templates" json:"templates"`
// @api(CommandLine/Options.solvers) represents the custom annotation solver commands for code generation.
//
// Example:
//
// “`yaml
// solvers:
// message: "message-type-allocator message-types.json"
// “`
//
// See the [-X](#CommandLine/Flag/-X) flag for more information.
Solvers flags.MapSlice `yaml:"solvers" json:"solvers"`
// contains filtered or unexported fields
}
@api(CommandLine/Options)
import CodeBlock from "@theme/CodeBlock"; import ExampleNextProjSource from "!!raw-loader!@site/example/example.nextproj";
Options represents the options of the Next project. The options is used to mamange the compiler options, such as verbosity, output directories, and custom templates. If the options is provided, you can generate code like this:
```sh next build example.nextproj ```
The options file is a YAML or JSON (for .json extension) file that contains the compiler options. Here is an example of the options file:
<CodeBlock language="yaml" title="example.nextproj">
{ExampleNextProjSource}
</CodeBlock>
type Package ¶
type Package struct {
// contains filtered or unexported fields
}
@api(Object/Package) (extends Decl(#Object/Common/Decl)) represents a Next package.
func (*Package) Annotations ¶
func (p *Package) Annotations() Annotations
func (*Package) Decls ¶
@api(Object/Package.Decls) represents the top-level declarations in the package.
func (*Package) Files ¶
@api(Object/Package.Files) represents the all declared file objects in the package.
func (*Package) Has ¶
@api(Object/Package.Has) reports whether the package contains the given Type(#Object/Common/Type) or Symbol(#Object/Common/Symbol). If the current package is nil, it always returns true.
Example:
```npl
{{- define "next/go/used.type" -}}
{{if not (.File.Package.Has .Type) -}}
{{.Type.Decl.File.Package.Name -}}.
{{- end -}}
{{next .Type}}
{{- end}}
```
func (*Package) Imports ¶
@api(Object/Package.Imports) represents the package's import declarations.
func (*Package) LookupLocalType ¶
@api(Object/Package.LookupLocalType) looks up a type by name in the package's symbol table.
func (*Package) LookupLocalValue ¶
@api(Object/Package.LookupLocalValue) looks up a value by name in the package's symbol table.
type Packages ¶ added in v0.2.6
type Packages []*Package
@api(Object/Packages) represents a list of Next packages.
func (Packages) Annotations ¶ added in v0.2.6
func (Packages) Annotations() Annotations
type Platform ¶
type Platform interface {
// Getenv retrieves the value of the environment variable named by the key.
Getenv(string) string
// UserHomeDir returns the current user's home directory.
UserHomeDir() (string, error)
// Stdin returns the standard input.
Stdin() io.Reader
// Stderr returns the standard error.
Stderr() io.Writer
// ReadFile reads the file named by filename and returns the contents.
ReadFile(string) ([]byte, error)
// WriteFile writes data to the file named by filename.
WriteFile(string, []byte, os.FileMode, Formatter) error
// IsExist reports whether the named file or directory exists.
IsExist(string) bool
// IsNotExist reports whether the named file or directory does not exist.
IsNotExist(string) bool
}
Platform is an interface that abstracts the platform-specific functions.
func StandardPlatform ¶
func StandardPlatform() Platform
StandardPlatform returns a Platform that uses the standard library functions.
type Position ¶
type Position struct {
// @api(Object/Common/Position.Filename) represents the filename of the position.
Filename string
// @api(Object/Common/Position.Line) represents the line number of the position starting from 1.
Line int
// @api(Object/Common/Position.Column) represents the column number of the position starting from 1.
Column int
// contains filtered or unexported fields
}
@api(Object/Common/Position) represents the position of an LocatedObject(#Object/Common/LocatedObject) in a file.
type PrimitiveType ¶
type PrimitiveType struct {
// contains filtered or unexported fields
}
@api(Object/PrimitiveType) represents a primitive type.
Currently, the following primitive types are supported:
- **int** - **int8** - **int16** - **int32** - **int64** - **float32** - **float64** - **bool** - **string** - **byte** - **bytes** - **any**
func (*PrimitiveType) Actual ¶
func (b *PrimitiveType) Actual() reflect.Value
func (*PrimitiveType) Decl ¶
func (x *PrimitiveType) Decl() Decl
func (*PrimitiveType) Kind ¶
func (x *PrimitiveType) Kind() Kind
func (*PrimitiveType) String ¶
func (b *PrimitiveType) String() string
func (*PrimitiveType) Typeof ¶
func (x *PrimitiveType) Typeof() string
func (*PrimitiveType) UsedKinds ¶
func (x *PrimitiveType) UsedKinds() Kinds
type Scope ¶
type Scope interface {
// LookupLocalSymbol looks up a symbol by name in the scope.
LookupLocalSymbol(name string) Symbol
// contains filtered or unexported methods
}
Scope represents a symbol scope.
type Stmt ¶
type Stmt interface {
// contains filtered or unexported methods
}
Stmt represents a Next statement.
type Struct ¶
type Struct struct {
// @api(Object/Struct.Type) represents [StructType](#Object/StructType) of the struct.
Type *StructType
// contains filtered or unexported fields
}
@api(Object/Struct) (extends Decl(#Object/Common/Decl)) represents a struct declaration.
func (Struct) Annotations ¶
func (d Struct) Annotations() Annotations
func (*Struct) Fields ¶
func (s *Struct) Fields() *StructFields
@api(Object/Struct.Fields) represents the Fields(#Object/Common/Fields) of StructField(#Object/StructField).
Example:
```next
struct Point {
int x;
int y;
}
```
```npl
{{range .Fields.List}}
{{.Name}} {{.Type}}
{{end}}
```
Output:
``` x int y int ```
type StructField ¶
type StructField struct {
// @api(Object/StructField.Decl) represents the [Struct](#Object/Struct) that contains the field.
Decl *Struct
// @api(Object/StructField.Type) represents the [Type](#Object/Common/Type) of the struct field.
Type Type
// @api(Object/StructField.Comment) represents the line [Comment](#Object/Comment) of the struct field declaration.
Comment *Comment
// contains filtered or unexported fields
}
@api(Object/StructField) (extends Node(#Object/Common/Node)) represents a struct field declaration.
func (StructField) Annotations ¶
func (d StructField) Annotations() Annotations
func (*StructField) Index ¶
func (f *StructField) Index() int
@api(Object/StructField.Index) represents the index of the struct field in the struct type.
Example:
```next
struct Point {
int x; // Index is 0 for x
int y; // Index is 1 for y
}
```
func (*StructField) IsFirst ¶
func (f *StructField) IsFirst() bool
@api(Object/StructField.IsFirst) reports whether the field is the first field in the struct type.
Example:
```next
struct Point {
int x; // IsFirst is true for x
int y;
}
```
func (*StructField) IsLast ¶
func (f *StructField) IsLast() bool
@api(Object/StructField.IsLast) reports whether the field is the last field in the struct type.
Example:
```next
struct Point {
int x;
int y; // IsLast is true for y
}
```
func (*StructField) Typeof ¶
func (*StructField) Typeof() string
type StructFields ¶
type StructFields = Fields[*Struct, *StructField]
@api(Object/StructFields) represents the Fields(#Object/Common/Fields) of StructField(#Object/StructField) in a Struct(#Object/Struct) declaration.
type StructType ¶
@api(Object/StructType) represents the Type(#Object/Common/Type) of a Struct(#Object/Struct) declaration.
type Structs ¶
@api(Object/Structs) represents a List(#Object/Common/List) of Struct(#Object/Struct) declarations.
type Symbol ¶
type Symbol interface {
LocatedObject
// contains filtered or unexported methods
}
@api(Object/Common/Symbol) represents a Next symbol. There are two types of symbols:
- Value(#Object/Value) symbol: such as a constant or an enum member. - Type(#Object/Common/Type) symbol: such as an enum, a struct, or an interface.
func LookupSymbol ¶
LookupSymbol looks up a symbol by name in the given scope and its parent scopes.
type SymbolNotFoundError ¶
type SymbolNotFoundError struct {
Name string
}
func (*SymbolNotFoundError) Error ¶
func (e *SymbolNotFoundError) Error() string
type SymbolRedefinedError ¶
func (*SymbolRedefinedError) Error ¶
func (e *SymbolRedefinedError) Error() string
type TemplateNotFoundError ¶
type TemplateNotFoundError struct {
Name string
}
func (*TemplateNotFoundError) Error ¶
func (e *TemplateNotFoundError) Error() string
type Type ¶
type Type interface {
Object
// @api(Object/Common/Type.Kind) returns the [Kind](#Object/Common/Type/Kind) of the type.
//
// Example:
//
// “`next
// package demo;
//
// enum Color {
// Red = 1;
// Green = 2;
// Blue = 3;
// }
// “`
//
// “`npl
// {{- define "cpp/enum" -}}
// {{.Type.Kind}}
// {{.MemberType.Kind}}
// {{end}}
// “`
//
// Output:
//
// “`
// Enum
// Int32
// “`
Kind() Kind
// @api(Object/Common/Type.UsedKinds) returns the used kinds in the type.
//
// Example:
//
// “`next
// package demo;
//
// struct User {
// int64 id;
// string name;
// vector<string> emails;
// map<int, bool> flags;
// }
// “`
// The used kinds in the `User` struct are: `(1<<KindStruct) | (1<<KindInt64) | (1<<KindString) | (1<<KindVector) | (1<<KindMap) | (1<<KindInt) | (1<<KindBool)`.
//
// “`npl
// {{.UsedKinds.Has "struct"}}
// {{.UsedKinds.Has "int64"}}
// {{.UsedKinds.Has "string"}}
// {{.UsedKinds.Has "vector"}}
// {{.UsedKinds.Has "map"}}
// {{.UsedKinds.Has "int"}}
// {{.UsedKinds.Has "bool"}}
// {{.UsedKinds.Has "float32"}}
// “`
//
// Output:
//
// “`
// true
// true
// true
// true
// true
// true
// true
// false
// “`
UsedKinds() Kinds
// @api(Object/Common/Type.String) represents the string representation of the type.
String() string
// @api(Object/Common/Type.Decl) represents the [Decl](#Object/Common/Decl) of the type.
// If the type is a built-in type, it returns a special declaration. Otherwise, it returns
// the declaration of the type: [Enum](#Object/Enum), [Struct](#Object/Struct), or [Interface](#Object/Interface).
Decl() Decl
// @api(Object/Common/Type.Actual) represents the actual type.
// You need to use the `Actual` method to get the actual type to access the specific fields of the type.
//
// For example, if you have a type `ArrayType`:
//
// “`npl
// {{.Type.Actual.ElemType}} {{/* Good */}}
// {{.Type.Actual.N}} {{/* Good */}}
//
// // This will error
// {{.Type.ElemType}} {{/* Error */}}
// // This will error
// {{.Type.N}} {{/* Error */}}
// “`
//
// Example:
//
// “`next
// package demo;
//
// struct User {
// int64 id;
// string name;
// array<int, 3> codes;
// }
// “`
//
// “`npl
// {{- define "c/struct.field" -}}
// {{- if .Type.Kind.IsArray -}}
// {{next .Type.Actual.ElemType}} {{.Name}}[{{.Type.Actual.N}}];
// {{- else -}}
// {{next .Type}} {{.Name}};
// {{- end}}
// {{- end}}
// “`
//
// Output:
//
// “`c
// typedef struct User {
// int64_t id;
// char* name;
// int codes[3];
// } User;
// “`
//
// :::tip
//
// In the example above, the `codes` field is an single-dimensional array of integers with a length of 3.
// If we want to process the multi-dimensional array, we need to fix the template to recursively process the array type.
//
// Example:
//
// “`next
// package demo;
//
// struct User {
// int64 id;
// string name;
// array<array<int, 3>, 2> codes;
// }
// “`
//
// “`npl
// {{- define "c/struct.field" -}}
// {{next .Doc}}{{render "dict:struct.field.decl" (dict "type" .Type "name" (render "struct.field:name" .))}};{{next .Comment}}
// {{- end}}
//
// {{- define "c/dict:struct.field.decl" -}}
// {{- $type := .type -}}
// {{- $name := .name -}}
// {{- if $type.Kind.IsArray -}}
// {{render "dict:struct.field.decl" (dict "type" $type.Actual.ElemType "name" (printf "%s[%d]" $name $type.Actual.N))}}
// {{- else -}}
// {{next $type}} {{$name}}
// {{- end}}
// {{- end}}
// “`
//
// Output:
//
// “`c
// typedef struct User {
// int64_t id;
// char* name;
// int codes[2][3];
// } User;
// “`
//
// :::
Actual() reflect.Value
}
@api(Object/Common/Type) represents a Next type Object(#Object).
Currently, the following types are supported:
- UsedType(#Object/UsedType) - PrimitiveType(#Object/PrimitiveType) - ArrayType(#Object/ArrayType) - VectorType(#Object/VectorType) - MapType(#Object/MapType) - EnumType(#Object/EnumType) - StructType(#Object/StructType) - InterfaceType(#Object/InterfaceType)
type UnexpectedSymbolTypeError ¶
func (*UnexpectedSymbolTypeError) Error ¶
func (e *UnexpectedSymbolTypeError) Error() string
type UsedType ¶
type UsedType struct {
// @api(Object/UsedType.Type) represents the underlying [Type](#Object/Common/Type).
//
// Example:
//
// “`next
// package demo;
//
// struct User {/*...*/}
//
// struct Group {
// // highlight-next-line
// User user;
// }
// “`
//
// The type of the `user` field is a `UsedType` with the underlying type `StructType` of the `User` struct.
Type Type
// contains filtered or unexported fields
}
@api(Object/UsedType) represents a used Type(#Object/Common/Type) in a file.
func (*UsedType) File ¶
@api(Object/UsedType.File) represents the File(#Object/File) where the type is used.
func (*UsedType) Node ¶
@api(Object/UsedType.Node) represents the node where the type is used.
The node may be:
- StructField(#Object/StructField): for a struct field type - InterfaceMethod(#Object/InterfaceMethod): for a method result type - InterfaceMethodParameter(#Object/InterfaceMethodParameter): for a method parameter type
type Value ¶
type Value struct {
// contains filtered or unexported fields
}
@api(Object/Value) represents a value for a const declaration or an enum member.
func LookupValue ¶
LookupValue looks up a value by name in the given scope and its parent scopes.
func (*Value) Actual ¶
@api(Object/Value.Actual) represents the underlying value of the constant. It returns one of the following types:
- **int32** - **int64** - **float32** - **float64** - **bool** - **string** - **nil**
func (*Value) Enum ¶
@api(Object/Value.Enum) represents the enum object that contains the value if it is an enum member. Otherwise, it returns nil.
func (*Value) Type ¶
func (v *Value) Type() *PrimitiveType
@api(Object/Value.Type) represents the PrimitiveType(#Object/PrimitiveType) of the value.
type VectorType ¶
type VectorType struct {
// @api(Object/VectorType.ElemType) represents the element [Type](#Object/Common/Type) of the vector.
ElemType Type
// contains filtered or unexported fields
}
@api(Object/VectorType) represents a vector Type(#Object/Common/Type).
func (*VectorType) Actual ¶
func (v *VectorType) Actual() reflect.Value
func (*VectorType) Decl ¶
func (x *VectorType) Decl() Decl
func (*VectorType) Kind ¶
func (*VectorType) Kind() Kind
func (*VectorType) String ¶
func (v *VectorType) String() string
func (*VectorType) Typeof ¶
func (*VectorType) Typeof() string
func (*VectorType) UsedKinds ¶
func (x *VectorType) UsedKinds() Kinds