compiler

package
v0.34.0 Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2021 License: BSD-3-Clause Imports: 22 Imported by: 0

Documentation

Overview

Package compiler implements parsing, type checking and emitting of sources.

A program can be compiled using

BuildProgram

while a template is compiled through

BuildTemplate

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Disassemble

func Disassemble(main *runtime.Function, globals []Global, n int) map[string][]byte

Disassemble disassembles the main function fn with the given globals, returning the assembly code for each package. The returned map has a package path as key and its assembly as value.

n determines the maximum length, in runes, of the disassembled text in a Text instruction:

n > 0: at most n runes; leading and trailing white space are removed
n == 0: no text
n < 0: all text

func DisassembleFunction

func DisassembleFunction(fn *runtime.Function, globals []Global, n int) []byte

DisassembleFunction disassembles the function fn with the given globals.

n determines the maximum length, in runes, of the disassembled text in a Text instruction:

n > 0: at most n runes; leading and trailing white space are removed
n == 0: no text
n < 0: all text

func ParseProgram

func ParseProgram(packages PackageLoader) (*ast.Tree, error)

ParseProgram parses a program reading its sources from loaders.

func ParseScript added in v0.29.0

func ParseScript(src io.Reader, packages PackageLoader, shebang bool) (*ast.Tree, error)

ParseScript parses a script reading its source from src and the imported packages form the loader. shebang reports whether the script can have the shebang as first line.

func ParseTemplate

func ParseTemplate(fsys fs.FS, name string, packages PackageLoader) (*ast.Tree, error)

ParseTemplate parses the named template file rooted at the given file system. If fsys implements FormatFS, the file format is read from its Format method, otherwise it depends on the extension of the file name. Any error related to the compilation itself is returned as a CompilerError.

ParseTemplate expands the nodes Extends, Import and Render parsing the relative trees.

func ParseTemplateSource

func ParseTemplateSource(src []byte, format ast.Format, imported bool) (tree *ast.Tree, unexpanded []ast.Node, err error)

ParseTemplateSource parses a template with content src in the given format and returns its tree and the unexpanded Extends, Import, Render and Assignment nodes.

format can be Text, HTML, CSS, JavaScript, JSON and Markdown. imported indicates whether it is imported.

func ValidTemplatePath

func ValidTemplatePath(path string) bool

ValidTemplatePath indicates whether path is a valid template path. A valid template path is a valid file system path but "." is not valid and it can start with '/' or alternatively can start with one o more ".." elements.

Types

type CheckingError

type CheckingError struct {
	// contains filtered or unexported fields
}

CheckingError records a type checking error with the path and the position where the error occurred.

func (*CheckingError) Error

func (e *CheckingError) Error() string

Error returns a string representation of the type checking error.

func (*CheckingError) Message

func (e *CheckingError) Message() string

Message returns the message of the type checking error, without position and path.

func (*CheckingError) Path

func (e *CheckingError) Path() string

Path returns the path of the type checking error.

func (*CheckingError) Position

func (e *CheckingError) Position() ast.Position

Position returns the position of the checking error.

type Code

type Code struct {
	// Globals is a slice of all globals used in Code.
	Globals []Global
	// Functions is a map of exported functions indexed by name.
	Functions map[string]*runtime.Function
	// Main is the Code entry point.
	Main *runtime.Function
	// Types allows to manipulate types, including new types defined in code.
	Types runtime.Types
}

Code is the result of a package emitting process.

func BuildProgram added in v0.31.0

func BuildProgram(r io.Reader, packages PackageLoader, opts Options) (*Code, error)

BuildProgram builds a program. Any error related to the compilation itself is returned as a CompilerError.

func BuildScript added in v0.31.0

func BuildScript(r io.Reader, packages PackageLoader, opts Options) (*Code, error)

BuildScript builds a script. Any error related to the compilation itself is returned as a CompilerError.

func BuildTemplate added in v0.31.0

func BuildTemplate(fsys fs.FS, name string, opts Options) (*Code, error)

BuildTemplate builds the named template file rooted at the given file system. If fsys implements FormatFS, the file format is read from its Format method, otherwise it depends on the extension of the file name. Any error related to the compilation itself is returned as a CompilerError.

type CycleError added in v0.18.0

type CycleError struct {
	// contains filtered or unexported fields
}

CycleError implements an error indicating the presence of a cycle.

func (*CycleError) Error added in v0.18.0

func (e *CycleError) Error() string

func (*CycleError) Message added in v0.18.0

func (e *CycleError) Message() string

Message returns the message of cycle error, without position and path.

func (*CycleError) Path added in v0.18.0

func (e *CycleError) Path() string

Path returns the path of the first referenced file in the cycle.

func (*CycleError) Position added in v0.18.0

func (e *CycleError) Position() ast.Position

Position returns the position of the statements extends and import or the expression render referring the second file in the cycle.

type Declarations

type Declarations map[string]interface{}

Declarations.

type FormatFS added in v0.33.0

type FormatFS interface {
	fs.FS
	Format(name string) (ast.Format, error)
}

FormatFS is the interface implemented by a file system that can determine the file format from a path name.

type Global

type Global struct {
	Pkg   string
	Name  string
	Type  reflect.Type
	Value interface{}
}

Global represents a global variable with a package, name, type (only for not predefined globals) and value (only for predefined globals). Value, if present, must be a pointer to the variable value.

type LimitExceededError

type LimitExceededError struct {
	// contains filtered or unexported fields
}

A LimitExceededError is an error returned by the compiler reporting that the compilation has exceeded a limit imposed by the implementation.

func (*LimitExceededError) Error

func (e *LimitExceededError) Error() string

Error implements the interface error for the LimitError.

func (*LimitExceededError) Message

func (e *LimitExceededError) Message() string

Message returns the error message of the LimitError.

func (*LimitExceededError) Path

func (e *LimitExceededError) Path() string

Path returns the path of the source code that caused the LimitError.

func (*LimitExceededError) Position

func (e *LimitExceededError) Position() ast.Position

Position returns the position of the function that caused the LimitError.

type Options

type Options struct {
	AllowShebangLine bool
	DisallowGoStmt   bool
	FormatTypes      map[ast.Format]reflect.Type
	Globals          Declarations
	Renderer         runtime.Renderer

	// Packages loads Scriggo packages and precompiled packages.
	//
	// For template files, Packages only loads precompiled packages; the template
	// files are read from a file system.
	Packages PackageLoader

	TreeTransformer func(*ast.Tree) error
}

Options represents a set of options used during the compilation.

type PackageLoader

type PackageLoader interface {
	Load(pkgPath string) (interface{}, error)
}

PackageLoader is implemented by package loaders. Load returns a predefined package as *Package or the source of a non predefined package as an io.Reader.

If the package does not exist it returns nil and nil. If the package exists but there was an error while loading the package, it returns nil and the error.

type SyntaxError

type SyntaxError struct {
	// contains filtered or unexported fields
}

SyntaxError records a parsing error with the path and the position where the error occurred.

func (*SyntaxError) Error

func (e *SyntaxError) Error() string

Error returns a string representing the syntax error.

func (*SyntaxError) Message

func (e *SyntaxError) Message() string

Message returns the message of the syntax error, without position and path.

func (*SyntaxError) Path

func (e *SyntaxError) Path() string

Path returns the path of the syntax error.

func (*SyntaxError) Position

func (e *SyntaxError) Position() ast.Position

Position returns the position of the syntax error.

type UntypedBooleanConst

type UntypedBooleanConst bool

UntypedBooleanConst represents an untyped boolean constant.

type UntypedNumericConst

type UntypedNumericConst string

UntypedNumericConst represents an untyped numeric constant.

type UntypedStringConst

type UntypedStringConst string

UntypedStringConst represents an untyped string constant.

Directories

Path Synopsis
ast
Package ast declares the types used to define the template trees.
Package ast declares the types used to define the template trees.
astutil
Package astutil implements methods to walk and dump a tree.
Package astutil implements methods to walk and dump a tree.
internal
package types implements functions and types to represent and work with Scriggo types.
package types implements functions and types to represent and work with Scriggo types.

Jump to

Keyboard shortcuts

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