parse

package
v0.5.2 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2022 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package parse implements a parser for corgi files.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNoFunc is the error returned during parsing, if a file that is being
	// parsed in ModeMain has no function definition.
	ErrNoFunc = errors.New("main files must define a func")

	// ErrMultipleProlog is the error multiple lex.Doctype items are
	// encountered, that define a prolog.
	ErrMultipleProlog = errors.New("you may only specify one XML prolog")
	// ErrMultipleDoctype is the error multiple lex.Doctype items are
	// encountered, that define a doctype (not a prolog).
	ErrMultipleDoctype = errors.New("you may only specify one doctype")
	// ErrMultipleExtend is the error returned if multiple lex.Extend items are
	// encountered.
	ErrMultipleExtend = errors.New("you may only specify one file to extend")
	// ErrMultipleFunc is the error returned if multiple lex.Func items are
	// encountered.
	ErrMultipleFunc = errors.New("you may only specify one func")

	// ErrExtendPlacement is the error returned if lex.Extend does not appear as
	// first item in a file.
	ErrExtendPlacement = errors.New("extend must be the first item in the file")
	// ErrImportPlacement is the error returned if imports are placed wrong.
	ErrImportPlacement = errors.New("imports must be declared directly after the extend statement")
	ErrUsePlacement    = errors.New("use statements must be declared directly after imports")

	ErrExtendDoctype = errors.New("files extending other files may not define a doctype or prolog")
	ErrExtendFunc    = errors.New("extended files may not define a func")

	// ErrUseExtends is the error returned if a file, that is being parsed in
	// ModeUse, has an extend statement.
	ErrUseExtends = errors.New("used files cannot extend other files")
	// ErrUseFunc is the error returned if a file, that is being parsed in
	// ModeUse, has a func statement.
	ErrUseFunc = errors.New("used files cannot define a func")
	// ErrUseDoctype is the error returned if a file, that is being parsed in
	// ModeUse, has a doctype statement.
	ErrUseDoctype = errors.New("used files cannot define a doctype")

	// ErrIncludeExtends is the error returned if a file, that is being parsed
	// in ModeInclude, has an extend statement.
	ErrIncludeExtends = errors.New("included files cannot extend other files")

	// ErrTernaryCondition is the error returned if a nil check is used in a
	// ternary expression.
	ErrTernaryCondition = errors.New("cannot use nil check or ternary expression as ternary condition")
	ErrNilCheckDefault  = errors.New("cannot use nil check or ternary expression as nil-check default")

	ErrIndexExpression    = errors.New("index expression has unclosed brackets")
	ErrFuncCallExpression = errors.New("func call expression has unclosed brackets")

	ErrCaseExpression         = errors.New("case expressions must resolve to regular Go expressions")
	ErrWhileExpression        = errors.New("while conditions must resolve to regular Go expressions")
	ErrMixinDefaultExpression = errors.New("mixin defaults must resolve to regular Go expressions")
)

Functions

This section is empty.

Types

type Context

type Context uint8

Context is the context in which the parser is parsing the current scope. It limits the expected items.

This is only relevant to outside callers, when parsing an included file.

const (
	// ContextRegular is the context used when no other context applies.
	ContextRegular Context = iota
	// ContextMixinDefinition is the Context used when in the body of a mixin
	// definition.
	//
	// In it, lex.Block and lex.IfBlock items are allowed.
	ContextMixinDefinition
	// ContextMixinCall is the Context used when in the body of a mixin call.
	//
	// In it only lex.If, lex.Switch, lex.And, lex.MixinCall, and lex.Block are
	// allowed.
	//
	// If inside a lex.Block inside a mixin call, ContextRegular is to be used.
	ContextMixinCall
	// ContextMixinCallConditional is the same as ContextMixinCall, but only
	// specifies that we're in a conditional (lex.If, or lex.Switch) where
	// lex.Block statements are not allowed.
	ContextMixinCallConditional
)

type Error

type Error struct {
	Line int
	Col  int
	Err  error
}

type Mode

type Mode uint8
const (
	// ModeMain represents the parsing of a main file.
	// A main file must define an output function and may extend other files.
	//
	// If the file extends another file, it may not define a doctype.
	ModeMain Mode = iota + 1
	// ModeExtend represents the parsing of an extended file.
	// Extended templates must not define an output function.
	// They may also extend other templates.
	//
	// If the file extends another file, it may not define a doctype.
	ModeExtend
	// ModeInclude represents the parsing of an included corgi file.
	// Included files may define an output function, which is ignored.
	// They must not extend other files.
	//
	// Included files also don't have global code, but only regular code.
	ModeInclude
	// ModeUse represents the parsing of a file that was imported through a use
	// directive.
	// Use files may only import packages, use other directories or files,
	// define global code, and define mixins.
	ModeUse
)

type Parser

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

func New

func New(mode Mode, context Context, source, name, in string) *Parser

New creates a new parser that parses the given input in the given Mode. Name is the name of the file that is being parsed.

func (*Parser) Parse

func (p *Parser) Parse() (f *file.File, err error)

Parse starts parsing the input. It returns the parsed file.File or an error.

type UnexpectedItemError

type UnexpectedItemError struct {
	Found    lex.ItemType
	Expected []lex.ItemType
}

UnexpectedItemError is the error returned when an unknown item is encountered.

func (*UnexpectedItemError) Error

func (e *UnexpectedItemError) Error() string

Jump to

Keyboard shortcuts

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