compile

package
v0.2.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 4, 2024 License: MIT Imports: 47 Imported by: 0

Documentation

Index

Constants

View Source
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.

View Source
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.

View Source
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
View Source
const StubPrefix = "next_stub_0041b8dcd21c3ad6ea2eb3a9f033a9861bc2873e6ab05106e8684ce1b961d4a7_"

StubPrefix is the prefix for stub templates.

Variables

View Source
var (
	ErrParamNotFound          = errors.New("param not found")
	ErrUnpexpectedParamType   = errors.New("unexpected param type")
	ErrUnexpectedConstantType = errors.New("unexpected constant type")
)

Functions

func Compile

func Compile(platform Platform, builtin FileSystem, args []string)

Compile compiles the next files.

func FileNode

func FileNode(f *File) *ast.File

FileNode represents the original AST node of the file.

func Generate

func Generate(c *Compiler) error

Genertate generates files for each language specified in the flags.outputs.

func IsTemplateNotFoundError

func IsTemplateNotFoundError(err error) bool

func LenExpr added in v0.2.5

func LenExpr(a *ArrayType) ast.Expr

func UsedTypeNode

func UsedTypeNode(u *UsedType) ast.Type

UsedTypeNode returns the AST node of the used type.

Types

type Annotation

type Annotation map[string]any

@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).

func (*ArrayType) Actual

func (a *ArrayType) Actual() reflect.Value

func (*ArrayType) Decl

func (x *ArrayType) Decl() Decl

func (*ArrayType) Kind

func (*ArrayType) Kind() Kind

func (*ArrayType) String

func (a *ArrayType) String() string

func (*ArrayType) Typeof

func (*ArrayType) Typeof() string

func (*ArrayType) UsedKinds

func (x *ArrayType) UsedKinds() Kinds

type CallStmt

type CallStmt struct {
	CallExpr *ast.CallExpr
	// contains filtered or unexported fields
}

func (*CallStmt) Typeof

func (*CallStmt) Typeof() string

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

func (c *Comment) String() 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.
```

func (*Comment) Text

func (c *Comment) Text() string

@api(Object/Comment.Text) returns the content of the comment without comment delimiters.

Example:

```next
const x = 1; // This is a comment.
```

```npl
{{.Comment.Text}}
```

Output:

```
This is a comment.
```

func (*Comment) Typeof

func (*Comment) Typeof() string

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) AddFile

func (c *Compiler) AddFile(f *ast.File) (*File, error)

AddFile adds a file to the context

func (*Compiler) Debug

func (c *Compiler) Debug(msg string, args ...any)

Debug logs a message if debug logging is enabled

func (*Compiler) Error

func (c *Compiler) Error(args ...any)

Error logs an error message

func (*Compiler) FileSet

func (c *Compiler) FileSet() *token.FileSet

FileSet returns the file set used to track file positions

func (*Compiler) Files

func (c *Compiler) Files() map[string]*File

Files returns all files in the context

func (*Compiler) GetFile

func (c *Compiler) GetFile(path string) *File

GetFile returns the file by path

func (*Compiler) Getenv

func (c *Compiler) Getenv(key string) (string, bool)

Getenv returns the value of an environment variable

func (*Compiler) Output

func (c *Compiler) Output() io.Writer

Output returns the output writer for logging

func (*Compiler) Position

func (c *Compiler) Position() token.Position

Position returns the current position for call expression

func (*Compiler) Resolve

func (c *Compiler) Resolve() error

Resolve resolves all files in the context.

func (*Compiler) SetupCommandFlags

func (c *Compiler) SetupCommandFlags(flagSet *flag.FlagSet, u flags.UsageFunc)

func (*Compiler) Trace

func (c *Compiler) Trace(msg string, args ...any)

Trace logs a message if trace logging is enabled

func (*Compiler) ValidateGrammar

func (c *Compiler) ValidateGrammar() error

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) Doc

func (d Const) Doc() *Doc

func (Const) File

func (x Const) File() *File

func (Const) Name

func (x Const) Name() string

func (Const) NamePos

func (x Const) NamePos() Position

func (Const) Package

func (x Const) Package() *Package

func (Const) Pos

func (x Const) Pos() Position

func (*Const) Type

func (x *Const) Type() *PrimitiveType

@api(Object/Const.Type) represents the type of the constant.

func (*Const) Typeof

func (*Const) Typeof() string

func (*Const) UsedKinds

func (x *Const) UsedKinds() Kinds

func (*Const) Value

func (x *Const) Value() *Value

@api(Object/Const.Value) represents the Value(#Object/Value) object of the constant.

type Consts

type Consts = List[*Const]

@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.

func (*DeclType[T]) Actual

func (d *DeclType[T]) Actual() reflect.Value

func (*DeclType[T]) Decl

func (x *DeclType[T]) Decl() Decl

func (*DeclType[T]) File

func (d *DeclType[T]) File() *File

func (*DeclType[T]) Kind

func (x *DeclType[T]) Kind() Kind

func (*DeclType[T]) Package

func (x *DeclType[T]) Package() *Package

func (*DeclType[T]) Pos

func (x *DeclType[T]) Pos() Position

func (*DeclType[T]) String

func (d *DeclType[T]) String() string

func (*DeclType[T]) Typeof

func (x *DeclType[T]) Typeof() string

func (*DeclType[T]) UsedKinds

func (x *DeclType[T]) UsedKinds() Kinds

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

func (d *Decls[T]) Consts() Consts

@api(Object/Decls.Consts) represents the List(#Object/Common/List) of Const(#Object/Const) declarations.

func (*Decls[T]) Enums

func (d *Decls[T]) Enums() 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.

func (*Decls[T]) List

func (d *Decls[T]) List() []Decl

@api(Object/Decls.List) returns the all top-level declarations.

func (*Decls[T]) Structs

func (d *Decls[T]) Structs() Structs

@api(Object/Decls.Structs) represents the List(#Object/Common/List) of Struct(#Object/Struct) declarations.

func (*Decls[T]) Typeof

func (*Decls[T]) Typeof() string

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

func (d *Doc) Format(indent string, beginAndEnd ...string) string

@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

func (d *Doc) String() 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

func (d *Doc) Text() string

@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.
```

func (*Doc) Typeof

func (*Doc) Typeof() string

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) Doc

func (d Enum) Doc() *Doc

func (Enum) File

func (x Enum) File() *File

func (*Enum) LookupLocalSymbol

func (e *Enum) LookupLocalSymbol(name string) Symbol

func (Enum) Name

func (x Enum) Name() string

func (Enum) NamePos

func (x Enum) NamePos() Position

func (Enum) Package

func (x Enum) Package() *Package

func (Enum) Pos

func (x Enum) Pos() Position

func (*Enum) Typeof

func (*Enum) Typeof() string

func (*Enum) UsedKinds

func (x *Enum) UsedKinds() Kinds

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) Doc

func (d EnumMember) Doc() *Doc

func (EnumMember) File

func (x EnumMember) File() *File

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) Name

func (x EnumMember) Name() string

func (EnumMember) NamePos

func (x EnumMember) NamePos() Position

func (EnumMember) Package

func (x EnumMember) Package() *Package

func (EnumMember) Pos

func (x EnumMember) Pos() Position

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

type EnumType = DeclType[*Enum]

@api(Object/EnumType) represents the Type(#Object/Common/Type) of an Enum(#Object/Enum) declaration.

type Enums

type Enums = List[*Enum]

@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).

func (*Fields[D, F]) Lookup

func (f *Fields[D, F]) Lookup(name string) F

@api(Object/Common/Fields.Lookup) looks up a field by name and returns the field object.

func (*Fields[D, F]) Typeof

func (x *Fields[D, F]) Typeof() string

Fields objects: enum.members, struct.fields, interface.methods

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) Decls

func (f *File) Decls() *Decls[*File]

@api(Object/File.Decls) returns the file's all top-level declarations.

func (*File) Doc

func (f *File) Doc() *Doc

func (*File) File

func (x *File) File() *File

func (*File) Imports

func (f *File) Imports() *Imports[*File]

@api(Object/File.Imports) represents the file's import declarations.

func (*File) LookupLocalSymbol

func (f *File) LookupLocalSymbol(name string) Symbol

func (*File) LookupLocalType

func (f *File) LookupLocalType(name string) (Type, error)

@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

func (f *File) LookupLocalValue(name string) (*Value, error)

@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.

func (*File) Name

func (x *File) Name() string

@api(Object/File.Name) represents the file name without the ".next" extension.

func (*File) NamePos

func (x *File) NamePos() Position

func (*File) Package

func (x *File) Package() *Package

func (*File) Pos

func (x *File) Pos() Position

func (*File) Typeof

func (*File) Typeof() string

func (*File) UsedKinds

func (x *File) UsedKinds() Kinds

type FileSystem

type FileSystem interface {
	fs.FS
	Abs(name string) (string, error)
}

FileSystem is an interface that abstracts the file system functions.

type Formatter

type Formatter func(filename string) error

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

func (i *Import) File() *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
```

func (*Import) Target

func (i *Import) Target() *File

@api(Object/Import.Target) represents the imported File(#Object/File) object.

Example:

```next title="file1.next"
package a;

import "file2.next";
import "file3.next";
```

```npl
{{range .Imports.List}}
{{.Target.Path}}
{{end}}
```

Output:

```
file2.next
file3.next
```

func (*Import) Typeof

func (*Import) Typeof() string

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

func (i *Imports[T]) TrimmedList() []*Import

@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
```

func (*Imports[T]) Typeof

func (*Imports[T]) Typeof() string

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) Doc

func (d Interface) Doc() *Doc

func (Interface) File

func (x Interface) File() *File

func (*Interface) Methods

func (i *Interface) Methods() *InterfaceMethods

@api(Object/Interface.Methods) represents the list of interface methods.

func (Interface) Name

func (x Interface) Name() string

func (Interface) NamePos

func (x Interface) NamePos() Position

func (Interface) Package

func (x Interface) Package() *Package

func (Interface) Pos

func (x Interface) Pos() Position

func (*Interface) Typeof

func (*Interface) Typeof() string

func (*Interface) UsedKinds

func (x *Interface) UsedKinds() Kinds

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) Doc

func (d InterfaceMethod) Doc() *Doc

func (InterfaceMethod) File

func (x InterfaceMethod) File() *File

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) Name

func (x InterfaceMethod) Name() string

func (InterfaceMethod) NamePos

func (x InterfaceMethod) NamePos() Position

func (InterfaceMethod) Package

func (x InterfaceMethod) Package() *Package

func (InterfaceMethod) Pos

func (x InterfaceMethod) Pos() Position

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) Doc

func (d InterfaceMethodParameter) Doc() *Doc

func (InterfaceMethodParameter) File

func (x InterfaceMethodParameter) File() *File

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) Name

func (x InterfaceMethodParameter) Name() string

func (InterfaceMethodParameter) NamePos

func (x InterfaceMethodParameter) NamePos() Position

func (InterfaceMethodParameter) Package

func (x InterfaceMethodParameter) Package() *Package

func (InterfaceMethodParameter) Pos

func (x InterfaceMethodParameter) Pos() Position

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

type InterfaceType = DeclType[*Interface]

@api(Object/InterfaceType) represents the Type(#Object/Common/Type) of an Interface(#Object/Interface) declaration.

type Interfaces

type Interfaces = List[*Interface]

@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

func (k Kind) Bits() int

@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

func (k Kind) Compatible(other Kind) Kind

@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) IsAny

func (k Kind) IsAny() bool

@api(Object/Common/Type/Kind.IsAny) reports whether the type is any.

func (Kind) IsArray

func (k Kind) IsArray() bool

@api(Object/Common/Type/Kind.IsArray) reports whether the type is an array.

func (Kind) IsBool

func (k Kind) IsBool() bool

@api(Object/Common/Type/Kind.IsBool) reports whether the type is a boolean.

func (Kind) IsByte

func (k Kind) IsByte() bool

@api(Object/Common/Type/Kind.IsByte) reports whether the type is a byte.

func (Kind) IsBytes

func (k Kind) IsBytes() bool

@api(Object/Common/Type/Kind.IsBytes) reports whether the type is a byte slice.

func (Kind) IsDuration added in v0.2.8

func (k Kind) IsDuration() bool

@api(Object/Common/Type/Kind.IsDuration) reports whether the type is a duration.

func (Kind) IsEnum

func (k Kind) IsEnum() bool

@api(Object/Common/Type/Kind.IsEnum) reports whether the type is an enumeration.

func (Kind) IsFloat

func (k Kind) IsFloat() bool

@api(Object/Common/Type/Kind.IsFloat) reports whether the type is a floating point. It includes `float32` and `float64`.

func (Kind) IsInteger

func (k Kind) IsInteger() bool

@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

func (k Kind) IsInterface() bool

@api(Object/Common/Type/Kind.IsInterface) reports whether the type is an interface.

func (Kind) IsMap

func (k Kind) IsMap() bool

@api(Object/Common/Type/Kind.IsMap) reports whether the type is a map.

func (Kind) IsNumeric

func (k Kind) IsNumeric() bool

@api(Object/Common/Type/Kind.IsNumeric) reports whether the type is a numeric type. It includes integer and floating point types.

func (Kind) IsPrimitive

func (k Kind) IsPrimitive() bool

@api(Object/Common/Type/Kind.IsPrimitive) reports whether the type is a PrimitiveType(#Object/PrimitiveType).

func (Kind) IsString

func (k Kind) IsString() bool

@api(Object/Common/Type/Kind.IsString) reports whether the type is a string.

func (Kind) IsStruct

func (k Kind) IsStruct() bool

@api(Object/Common/Type/Kind.IsStruct) reports whether the type is a structure.

func (Kind) IsTime added in v0.2.8

func (k Kind) IsTime() bool

@api(Object/Common/Type/Kind.IsTime) reports whether the type is a time.

func (Kind) IsVector

func (k Kind) IsVector() bool

@api(Object/Common/Type/Kind.IsVector) reports whether the type is a vector.

func (*Kind) Set

func (k *Kind) Set(s string) error

func (Kind) String

func (i Kind) String() string

func (Kind) Valid

func (k Kind) Valid() bool

@api(Object/Common/Type/Kind.Valid) reports whether the type is valid.

type Kinds

type Kinds uint64

@api(Object/Common/Type/Kinds) represents the type kind set.

func (Kinds) Has

func (ks Kinds) Has(k any) (bool, error)

@api(Object/Common/Type/Kinds.Has) reports whether the type contains specific kind. The kind can be a `Kind` (or any integer) or a string representation of the Kind(#Object/Common/Type/Kind). If the kind is invalid, it returns an error.

type List

type List[T Object] []T

@api(Object/Common/List) `List<T>` represents a slice of objects: **\[T: Object(#Object)\]**.

func (List[T]) List

func (l List[T]) List() []T

@api(Object/Common/List.List) represents the slice of Object(#Object)s. It is used to provide a uniform way to access.

func (List[T]) Typeof

func (x List[T]) Typeof() string

list objects: consts, enums, structs, interfaces

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).

func (*MapType) Actual

func (m *MapType) Actual() reflect.Value

func (*MapType) Decl

func (x *MapType) Decl() Decl

func (*MapType) Kind

func (*MapType) Kind() Kind

func (*MapType) String

func (m *MapType) String() string

func (*MapType) Typeof

func (*MapType) Typeof() string

func (*MapType) UsedKinds

func (x *MapType) UsedKinds() Kinds

type Meta

type Meta map[string]string

Meta represents the metadata of a entrypoint template file.

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 .}}`.

func LookupFileObject

func LookupFileObject(f *File, n ast.Node) Object

LookupFileObject looks up an object by its AST node in the file's symbol table.

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>

func (*Options) SetupCommandFlags

func (o *Options) SetupCommandFlags(flagSet *flag.FlagSet, u flags.UsageFunc)

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

func (p *Package) Decls() *Decls[*Package]

@api(Object/Package.Decls) represents the top-level declarations in the package.

func (*Package) Doc

func (p *Package) Doc() *Doc

func (*Package) File

func (p *Package) File() *File

func (*Package) Files

func (p *Package) Files() []*File

@api(Object/Package.Files) represents the all declared file objects in the package.

func (*Package) Has

func (p *Package) Has(obj Object) (bool, error)

@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

func (p *Package) Imports() *Imports[*Package]

@api(Object/Package.Imports) represents the package's import declarations.

func (*Package) LookupLocalType

func (p *Package) LookupLocalType(name string) (Type, error)

@api(Object/Package.LookupLocalType) looks up a type by name in the package's symbol table.

func (*Package) LookupLocalValue

func (p *Package) LookupLocalValue(name string) (*Value, error)

@api(Object/Package.LookupLocalValue) looks up a value by name in the package's symbol table.

func (*Package) Name

func (p *Package) Name() string

@api(Object/Package.Name) represents the package name string.

func (*Package) NamePos

func (x *Package) NamePos() Position

func (*Package) Package

func (p *Package) Package() *Package

func (*Package) Pos

func (p *Package) Pos() Position

func (*Package) Typeof

func (*Package) Typeof() string

func (*Package) Types

func (p *Package) Types() []Type

@api(Object/Package.Types) represents the all declared types in the package.

func (*Package) UsedKinds

func (x *Package) UsedKinds() Kinds

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

func (Packages) Doc added in v0.2.6

func (Packages) Doc() *Doc

func (Packages) File added in v0.2.6

func (Packages) File() *File

func (Packages) Name added in v0.2.6

func (Packages) Name() string

func (Packages) NamePos added in v0.2.6

func (Packages) NamePos() Position

func (Packages) Package added in v0.2.6

func (Packages) Package() *Package

func (Packages) Pos added in v0.2.6

func (Packages) Pos() Position

func (Packages) Typeof added in v0.2.6

func (Packages) Typeof() string

func (Packages) UsedKinds added in v0.2.6

func (x Packages) UsedKinds() Kinds

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.

func (Position) IsValid

func (p Position) IsValid() bool

@api(Object/Common/Position.IsValid) reports whether the position is valid.

func (Position) String

func (p Position) String() string

@api(Object/Common/Position) returns the string representation of the position, e.g., `demo.next:10:2`.

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) Doc

func (d Struct) Doc() *Doc

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
```

func (Struct) File

func (x Struct) File() *File

func (Struct) Name

func (x Struct) Name() string

func (Struct) NamePos

func (x Struct) NamePos() Position

func (Struct) Package

func (x Struct) Package() *Package

func (Struct) Pos

func (x Struct) Pos() Position

func (*Struct) Typeof

func (*Struct) Typeof() string

func (*Struct) UsedKinds

func (x *Struct) UsedKinds() Kinds

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) Doc

func (d StructField) Doc() *Doc

func (StructField) File

func (x StructField) File() *File

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) Name

func (x StructField) Name() string

func (StructField) NamePos

func (x StructField) NamePos() Position

func (StructField) Package

func (x StructField) Package() *Package

func (StructField) Pos

func (x StructField) Pos() Position

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

type StructType = DeclType[*Struct]

@api(Object/StructType) represents the Type(#Object/Common/Type) of a Struct(#Object/Struct) declaration.

type Structs

type Structs = List[*Struct]

@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

func LookupSymbol(scope Scope, name string) Symbol

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

type SymbolRedefinedError struct {
	Name string
	Prev Symbol
}

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)

func LookupType

func LookupType(scope Scope, name string) (Type, error)

LookupType looks up a type by name in the given scope and its parent scopes.

type UnexpectedSymbolTypeError

type UnexpectedSymbolTypeError struct {
	Name string
	Want string
	Got  string
}

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 Use

func Use(depth int, src ast.Type, node Node, t Type) *UsedType

Use uses a type in a file.

func (*UsedType) Actual

func (u *UsedType) Actual() reflect.Value

func (*UsedType) Decl

func (x *UsedType) Decl() Decl

func (*UsedType) File

func (u *UsedType) File() *File

@api(Object/UsedType.File) represents the File(#Object/File) where the type is used.

func (*UsedType) Kind

func (x *UsedType) Kind() Kind

func (*UsedType) Node

func (u *UsedType) Node() reflect.Value

@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

func (*UsedType) String

func (u *UsedType) String() string

func (*UsedType) Typeof

func (*UsedType) Typeof() string

func (*UsedType) UsedKinds

func (x *UsedType) UsedKinds() Kinds

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

func LookupValue(scope Scope, name string) (*Value, error)

LookupValue looks up a value by name in the given scope and its parent scopes.

func (*Value) Actual

func (v *Value) Actual() any

@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

func (v *Value) Enum() *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) File

func (v *Value) File() *File

func (*Value) Package

func (x *Value) Package() *Package

func (*Value) Pos

func (x *Value) Pos() Position

func (*Value) String

func (v *Value) String() string

@api(Object/Value.String) represents the string representation of the value.

func (*Value) Type

func (v *Value) Type() *PrimitiveType

@api(Object/Value.Type) represents the PrimitiveType(#Object/PrimitiveType) of the value.

func (*Value) Typeof

func (x *Value) Typeof() string

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL