compiler

package
v0.15.0 Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2020 License: BSD-3-Clause Imports: 21 Imported by: 0

Documentation

Overview

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

A program can be compiled using

CompileProgram

while a template is compiled through

CompileTemplate

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidPath is returned from the ParseTemplate function when the path
	// argument is not valid.
	ErrInvalidPath = errors.New("scriggo: invalid path")

	// ErrNotExist is returned from the ParseTemplate function when when the path
	// argument does not exist.
	ErrNotExist = errors.New("scriggo: path does not exist")
)

Functions

func Disassemble

func Disassemble(main *runtime.Function, globals []Global) (assembler map[string]string, err error)

func DisassembleFunction

func DisassembleFunction(w io.Writer, fn *runtime.Function, globals []Global) (int64, error)

func ParsePackageLessProgram

func ParsePackageLessProgram(src io.Reader, loader PackageLoader, shebang bool) (*ast.Tree, error)

ParsePackageLessProgram parses a package-less program reading its source from src and the imported packages form the loader. shebang reports whether the package-less program can have the shebang as first line.

func ParseProgram

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

ParseProgram parses a program reading its sources from loaders.

func ParseSource

func ParseSource(src []byte, isPackageLessProgram, shebang bool) (tree *ast.Tree, err error)

ParseSource parses a program or a package-less program and returns its tree. isPackageLessProgram reports whether it is a package-less program and shebang reports whether a package-less program can have the shebang as first line.

func ParseTemplate

func ParseTemplate(path string, reader FileReader, lang ast.Language, relaxedBoolean bool, loader PackageLoader) (*ast.Tree, error)

ParseTemplate parses the template file with the given path and written in language lang, reading the template files from the reader. path, if not absolute, is relative to the root of the template. lang can be Text, HTML, CSS or JavaScript.

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

relaxedBoolean reports whether the operators 'and', 'or' and 'not' as well as non-boolean conditions in the if statement are allowed.

The parsed trees are cached so only one call per combination of path and context is made to the reader.

func ParseTemplateSource

func ParseTemplateSource(src []byte, lang ast.Language, relaxedBoolean bool) (tree *ast.Tree, err error)

ParseTemplateSource parses a template with source src written in the language lang and returns its tree. language can be Text, HTML, CSS or JavaScript.

ParseTemplateSource does not expand the nodes Extends, Include and Import.

relaxedBoolean reports whether the operators 'and', 'or' and 'not' as well as non-boolean conditions in the if statement are allowed.

func ValidTemplatePath

func ValidTemplatePath(path string) bool

ValidTemplatePath indicates whether path is a valid template path.

Types

type CSSEnvStringer

type CSSEnvStringer interface{ CSS(runtime.Env) string }

These interfaces are like HTMLStringer, CSSStringer and JavaScriptStringer, but their method accepts a runtime.Env parameter that can be used inside the method's body to access some environment information.

type CSSStringer

type CSSStringer interface{ CSS() string }

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
}

Code is the result of a package emitting process.

func CompileProgram

func CompileProgram(r io.Reader, importer PackageLoader, opts Options) (*Code, error)

CompileProgram compiles a program. Any error related to the compilation itself is returned as a CompilerError.

func CompileTemplate

func CompileTemplate(path string, r FileReader, lang ast.Language, opts Options) (*Code, error)

CompileTemplate compiles the template file with the given path and written in language lang. It reads the template files from the reader. path, if not absolute, is relative to the root of the template. lang can be Text, HTML, CSS or JavaScript. Any error related to the compilation itself is returned as a CompilerError.

type Declarations

type Declarations map[string]interface{}

Declarations.

type EnvStringer

type EnvStringer interface{ String(runtime.Env) string }

EnvStringer is like fmt.Stringer, but its method accepts a runtime.Env as parameter.

type FileReader

type FileReader interface {
	ReadFile(name string) ([]byte, error)
}

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 HTMLEnvStringer

type HTMLEnvStringer interface{ HTML(runtime.Env) string }

These interfaces are like HTMLStringer, CSSStringer and JavaScriptStringer, but their method accepts a runtime.Env parameter that can be used inside the method's body to access some environment information.

type HTMLStringer

type HTMLStringer interface{ HTML() string }

type JavaScriptEnvStringer

type JavaScriptEnvStringer interface{ JavaScript(runtime.Env) string }

These interfaces are like HTMLStringer, CSSStringer and JavaScriptStringer, but their method accepts a runtime.Env parameter that can be used inside the method's body to access some environment information.

type JavaScriptStringer

type JavaScriptStringer interface{ JavaScript() string }

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
	PackageLess      bool
	Builtins         Declarations

	// Loader loads Scriggo packages and precompiled packages.
	//
	// For template files, Loader only loads precompiled packages; the template
	// files are read from the FileReader.
	Loader PackageLoader

	TemplateFailOnTODO bool
	RelaxedBoolean     bool
	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.
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