file

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Dec 11, 2023 License: MIT Imports: 0 Imported by: 0

Documentation

Overview

Package file provides structs that represent the structure of a corgi file.

Package file provides an AST for corgi files.

Index

Constants

This section is empty.

Variables

View Source
var InvalidPosition = Position{0, 0}

Functions

This section is empty.

Types

type And

type And struct {
	Attributes []AttributeCollection

	Position
}

And represents an '&' expression.

type AndPlaceholder

type AndPlaceholder struct {
	Position
}

AndPlaceholder is an attribute 'named' `&` that is used as a placeholder for the attributes attached to a mixin call.

mixin foo()
	div: span(&) foo

It may only be used inside a mixin definition.

type ArrowBlock

type ArrowBlock struct {
	Lines []TextLine
	Position
}

type Attribute

type Attribute interface {
	Poser
	// contains filtered or unexported methods
}

type AttributeCollection

type AttributeCollection interface {
	Poser
	// contains filtered or unexported methods
}

type AttributeList

type AttributeList struct {
	LParenPos  Position
	Attributes []Attribute
	RParenPos  Position
}

func (AttributeList) Pos

func (l AttributeList) Pos() Position

type BadItem

type BadItem struct {
	// Line contains the entire bad line, minus indentation.
	Line string
	// Body contains the body of the item, if it has any.
	Body Scope
	Position
}

type Block

type Block struct {
	// Type is the type of block.
	Type BlockType

	// Name is the name of the block.
	Name Ident

	Body Scope

	Position
}

Block represents a block with content. It is used for blocks from extendable templates as well as blocks in MixinCalls.

type BlockExpansion

type BlockExpansion struct {
	Item ScopeItem // Either Block, Element, ArrowBlock, or MixinCall
	Position
}

type BlockType

type BlockType uint8
const (
	BlockTypeBlock BlockType = iota + 1
	BlockTypePrepend
	BlockTypeAppend
)

type Case

type Case struct {
	// Expression is the expression written behind 'case'.
	//
	// Nil for the default case.
	Expression *Expression
	// Then is the scope of the code that is executed if the condition
	// evaluates to true.
	Then Scope

	Position
}

type ChainExpression

type ChainExpression struct {
	// Root is the root expression that is checked.
	Root GoExpression
	// CheckRoot specifies whether to check if the root is non-zero.
	CheckRoot bool
	// Chain is a list of GoExpression that yield the desired value.
	Chain []ChainExpressionItem

	// DerefCount is the number of leading *, used to dereference the chain
	// expression value
	DerefCount int

	// DefaultOpPos is the position of the '?!' operator.
	//
	// Only set, if Default is.
	DefaultOpPos *Position

	// Default represents the optional default value.
	Default *Expression // not a ChainExpression

	Position
}

ChainExpression is an expression that checks if elements in the chain are not zero, and if indexes exist.

type ChainExpressionItem

type ChainExpressionItem interface {
	Poser
	// contains filtered or unexported methods
}

ChainExpressionItem represents an expression that can be chained.

It is either a IndexExpression, or a DotIdentExpression.

type ClassShorthand

type ClassShorthand struct {
	Name string
	Position
}

type Code

type Code struct {
	Lines []CodeLine
	Position
}

Code represents a line or block of code.

type CodeLine

type CodeLine struct {
	// Code is the code in the line.
	Code string

	Position
}

type CommandFilter

type CommandFilter struct {
	Name string
	Args []CommandFilterArg

	Body []FilterLine

	Position
}

type CommandFilterArg

type CommandFilterArg interface {
	Poser
	// contains filtered or unexported methods
}

type CorgiComment

type CorgiComment struct {
	// Lines are the lines of the comment.
	//
	// Empty lines may be excluded.
	Lines []CorgiCommentLine
	Position
}

CorgiComment represents a corgi comment, i.e. a comment that is not printed.

type CorgiCommentLine

type CorgiCommentLine struct {
	Comment string
	Position
}

type CorgiInclude

type CorgiInclude struct {
	File *File
}

type DivShorthand

type DivShorthand struct {
	// Attributes are the attributes of the element.
	//
	// It contains at least one item.
	//
	// Attributes[0] will be either of type ClassShorthand or IDShorthand.
	Attributes []AttributeCollection

	Body Scope

	Position
}

type Doctype

type Doctype struct {
	Position
}

Doctype represents a doctype directive (`doctype html`).

type DotIdentExpression

type DotIdentExpression struct {
	Ident GoIdent
	Check bool

	Position // of the dot
}

DotIdentExpression represents a dot followed by a Go identifier.

type Element

type Element struct {
	Name       string
	Attributes []AttributeCollection
	Void       bool

	Body Scope

	Position
}

Element represents a single HTML element.

type ElementInterpolation

type ElementInterpolation struct {
	Element Element
	Value   InterpolationValue // nil for void elems

	Position
}

type Else

type Else struct {
	Then Scope
	Position
}

type ElseIf

type ElseIf struct {
	// Condition is the condition of the else if statement.
	Condition Expression

	// Then is scope of the code that is executed if the condition evaluates
	// to true.
	Then Scope

	Position
}

ElseIf represents an 'else if' statement.

type ElseIfBlock

type ElseIfBlock struct {
	// Name is the name of the block, whose existence is checked.
	Name Ident
	// Then is the scope of the code that is executed if the block exists.
	Then Scope

	Position
}

type Expression

type Expression struct {
	Expressions []ExpressionItem
}

Expression represents a chain of ExpressionItem elements.

func (Expression) Pos

func (e Expression) Pos() Position

type ExpressionInterpolationValue

type ExpressionInterpolationValue struct {
	LBracePos       Position
	FormatDirective string // a Sprintf formatting placeholder, w/o preceding '%'
	Expression      Expression
	RBracePos       Position
}

func (ExpressionInterpolationValue) Pos

type ExpressionItem

type ExpressionItem interface {
	Poser
	// contains filtered or unexported methods
}

type Extend

type Extend struct {
	// Path to the file.
	Path String
	File *File

	Position
}

type File

type File struct {
	Type Type

	// Name is the name of the file.
	//
	// If this is the main file, it will be just the file's name.
	//
	// If this is an extended, included, or used file, this will be the path
	// of the specified file in its source.
	Name string

	// Module is the path/name of the Go module providing this file.
	//
	// This won't be set for main and include files.
	Module string
	// PathInModule is the path to the file in the Go module, relative to the
	// module root.
	//
	// It is always specified as a forward slash separated path.
	//
	// This won't be set for main and include files.
	PathInModule string
	// AbsolutePath is the resolved absolute path to the file.
	//
	// It is specified using the system's separator.
	AbsolutePath string

	// Library is the library this file belongs to, if any.
	Library *Library
	// DirLibrary provides the library files located in the same directory as
	// this main, include, or template file.
	//
	// Not filled for library files.
	DirLibrary *Library

	// Raw contains the raw input file, as it was parsed.
	Raw string
	// Lines are the lines of Raw, stripped of their CRLF/LF line endings.
	Lines []string

	// TopLevelComments contains all comments made before the first scope item.
	TopLevelComments []CorgiComment

	// Extend is the file that this file extends, if any.
	Extend *Extend

	// Imports is a list of imports made by this file, in the order they
	// appear.
	Imports []Import

	// Uses is a list of used libraries, in the order they appear.
	Uses []Use

	// GlobalCode is the code that is written above the output function.
	GlobalCode []Code

	// Func is the function header.
	// It is always present for main files, i.e. those files that are given
	// to the corgi command.
	Func *Func

	// Scope is the global scope.
	Scope Scope
}

File represents a parsed corgi file.

type Filter

type Filter interface {
	ScopeItem
	// contains filtered or unexported methods
}

type FilterLine

type FilterLine struct {
	Line string
	Position
}

type For

type For struct {
	// Expression is the expression written in the head of the for, or nil if
	// this is an infinite loop.
	Expression *Expression
	Body       Scope

	Position
}

For represents a for loop.

type Func

type Func struct {
	// Name is the name of the function.
	Name GoIdent

	LParenPos Position
	Params    []FuncParam
	RParenPos Position

	Position
}

Func is the function header for the generated function.

type FuncParam

type FuncParam struct {
	Names    []GoIdent
	Variadic bool
	Type     GoType
}

func (FuncParam) Pos

func (p FuncParam) Pos() Position

type GoExpression

type GoExpression struct {
	Expression string
	Position
}

GoExpression is a raw Go expression.

type GoIdent

type GoIdent struct {
	Ident string
	Position
}

GoIdent represents a Go identifier.

type GoType

type GoType struct {
	Type string
	Position
}

GoType represents the name or definition of a Go type.

type HTMLComment

type HTMLComment struct {
	Lines []HTMLCommentLine
	Position
}

HTMLComment represents a comment.

type HTMLCommentLine

type HTMLCommentLine struct {
	Comment string
	Position
}

type IDShorthand

type IDShorthand struct {
	ID string
	Position
}

type Ident

type Ident struct {
	Ident string
	Position
}

Ident represents a corgi identifier.

type If

type If struct {
	// Condition is the condition of the if statement.
	Condition Expression

	// Then is scope of the code that is executed if the condition evaluates
	// to true.
	Then Scope

	// ElseIfs are the else if statements, if this If has any.
	ElseIfs []ElseIf
	// Else is the scope of the Else statement, if this If has one.
	Else *Else

	Position
}

If represents an 'if' statement.

type IfBlock

type IfBlock struct {
	// Name is the name of the block, whose existence is checked.
	Name Ident

	// Then is the scope of the code that is executed if the block exists.
	Then Scope
	// ElseIfs are the else if statements, if this IfBlock has any.
	ElseIfs []ElseIfBlock
	// Else is the scope of the code that is executed if the block does not
	// exist.
	Else *Else

	Position
}

IfBlock represents an 'if block' directive.

type Import

type Import struct {
	Imports []ImportSpec

	// Position is the position of the import keyword.
	// Hence, multiple imports may share the same position, if they are
	// grouped in a block.
	Position
}

Import represents a single import.

type ImportSpec

type ImportSpec struct {
	// Alias is the alias of the import, if any.
	Alias *GoIdent
	Path  String

	Position
}

type Include

type Include struct {
	// Path is the path to the file to include.
	Path String

	// Include is the included file.
	// It is populated by the linker.
	Include IncludeFile

	Position
}

type IncludeFile

type IncludeFile interface {
	// contains filtered or unexported methods
}

IncludeFile is the type used to represent an included file.

Its concrete type is either a CorgiInclude or a OtherInclude.

type IndexExpression

type IndexExpression struct {
	LBracePos Position
	Index     Expression
	RBracePos Position

	CheckIndex bool
	CheckValue bool
}

IndexExpression represents either a map or slice index expression.

func (IndexExpression) Pos

func (e IndexExpression) Pos() Position

type InlineText

type InlineText struct {
	Text TextLine
	Position
}

type Interpolation

type Interpolation interface {
	Poser
	// contains filtered or unexported methods
}

type InterpolationValue

type InterpolationValue interface {
	Poser
	// contains filtered or unexported methods
}

type LibDependency

type LibDependency struct {
	// Module is the path/name of the Go module providing this library.
	Module string
	// PathInModule is the path to the library in the Go module, relative to the
	// module root.
	//
	// It is always specified as a forward slash separated path.
	PathInModule string

	// Library is the linked library.
	Library *Library

	Mixins []MixinDependency
}

type Library

type Library struct {

	// Module is the path/name of the Go module providing this library.
	Module string
	// PathInModule is the path to the library in the Go module, relative to the
	// module root.
	//
	// It is always specified as a forward slash separated path.
	PathInModule string
	// AbsolutePath is the resolved absolute path to the library.
	//
	// It is specified using the system's separator.
	AbsolutePath string

	// Precompiled indicates whether this library was precompiled.
	//
	// If true, the files in this library will only have Type, Name, Module,
	// and PathInModule set.
	//
	// Additionally, Imports will be set,
	Precompiled bool

	// Files are the files this library consists of.
	//
	// If the library is precompiled, only Name, Module, PathInModule, and
	// Imports will be set.
	Files []*File

	Dependencies []LibDependency

	GlobalCode []PrecompiledCode

	Mixins []PrecompiledMixin
}

type LinkedMixin

type LinkedMixin struct {
	// File is the file the mixin was declared in.
	//
	// Note that the file's scope may be empty, if this mixin was precompiled.
	File *File
	// Mixin is the mixin itself.
	//
	// Note that the mixin's body may be empty, if this mixin was precompiled.
	Mixin *Mixin
}

type Mixin

type Mixin struct {
	// Name is the name of the mixin.
	Name Ident

	LParenPos *Position // nil if params were omitted

	// Params is a list of the parameters of the mixin.
	Params []MixinParam

	RParenPos *Position

	// Body is the scope of the mixin.
	Body Scope

	// Precompiled is the precompiled function literal.
	// Its args start with the mixins args, followed by func()s for each of
	// the Blocks, and lastly, if HasAndPlaceholders is true, a final func()
	// called each time that the mixin's &s are supposed to be placed.
	//
	// It is only present, if this mixin was precompiled.
	Precompiled []byte

	*MixinInfo

	Position
}

type MixinArg

type MixinArg struct {
	// Name is the name of the argument.
	Name Ident
	// Value is the expression that yields the value of the argument.
	Value Expression

	Position
}

MixinArg represents a single argument given to a mixin.

type MixinBlockInfo

type MixinBlockInfo struct {
	Name     string
	TopLevel bool // writes directly to the element it is called in
	// CanAttributes specifies whether &-directives can be used in this block.
	CanAttributes                   bool
	DefaultWritesBody               bool
	DefaultWritesElements           bool
	DefaultWritesTopLevelAttributes bool
	DefaultTopLevelAndPlaceholder   bool
}

type MixinCall

type MixinCall struct {
	// Namespace is the namespace of the mixin, if any.
	Namespace *Ident
	// Name is the name of the mixin.
	Name Ident

	// Mixin is a pointer to the called mixin.
	//
	// It is set by the linker.
	Mixin *LinkedMixin

	LParenPos *Position

	// Args is a list of the arguments of given to the mixin.
	Args []MixinArg

	RParenPos *Position

	// Body is the body of the mixin call.
	//
	// It will only consist of If, IfBlock, Switch, And, and Block items.
	Body Scope

	Position
}

MixinCall represents the call to a mixin.

type MixinCallAttribute

type MixinCallAttribute struct {
	Name string

	AssignPos Position

	MixinCall MixinCall
	Value     InterpolationValue

	Position
}

type MixinCallInterpolation

type MixinCallInterpolation struct {
	MixinCall MixinCall
	Value     InterpolationValue // may be nil

	Position
}

type MixinDependency

type MixinDependency struct {
	// Name is name of the mixin depended on.
	Name string
	// Var is the variable used by the depending mixins to call this mixin.
	Var string

	// Mixin is the linked mixin.
	Mixin *Mixin

	// RequiredBy are the names of the depending mixins.
	RequiredBy []string
}

type MixinInfo

type MixinInfo struct {
	// WritesBody indicates whether the mixin writes to the body of an element.
	// Blocks including block defaults are ignored.
	WritesBody bool
	// WritesElements indicates whether the mixin writes elements.
	//
	// Only true, if WritesBody is as well.
	WritesElements bool
	// WritesTopLevelAttributes indicates whether the mixin writes any top-level
	// attributes, except &-placeholders.
	WritesTopLevelAttributes bool
	// TopLevelAndPlaceholder indicates whether the mixin has any top-level
	// &-placeholders.
	TopLevelAndPlaceholder bool
	// Blocks is are the blocks used in the mixin in the order they appear in,
	// and in the order they appear in the functions' signature.
	Blocks             []MixinBlockInfo
	HasAndPlaceholders bool
}

type MixinMainBlockShorthand

type MixinMainBlockShorthand struct {
	Body Scope
	Position
}

type MixinParam

type MixinParam struct {
	// Name is the name of the parameter.
	Name Ident

	// Type is the name of the type of the parameter, or nil if the type is
	// inferred from the default.
	Type *GoType
	// InferredType is the type inferred from the Default, if Type is nil.
	//
	// It will be set by package typeinfer before linking.
	//
	// An empty string indicates the type could not be inferred.
	InferredType string

	AssignPos *Position
	// Default is the optional default value of the parameter.
	Default *Expression // never a chain expression

	Position
}

MixinParam represents a parameter of a mixin.

type OtherInclude

type OtherInclude struct {
	Contents string
}

OtherInclude represents an included file other than a Corgi file.

type ParenExpression

type ParenExpression struct {
	LParenPos Position
	Args      []Expression
	RParenPos Position

	// Check indicates whether to check the return value.
	Check bool
}

ParenExpression represents a function call or the paren part of a type cast.

func (ParenExpression) Pos

func (e ParenExpression) Pos() Position

type Poser

type Poser interface {
	Pos() Position
}

type Position

type Position struct {
	Line int
	Col  int
}

Position indicates the position where a token was encountered.

func (Position) Pos

func (p Position) Pos() Position

type PrecompiledCode

type PrecompiledCode struct {
	MachineComments []string
	Lines           []string
}

type PrecompiledMixin

type PrecompiledMixin struct {
	// File is the file in which the mixin appears.
	File *File

	MachineComments []string
	// Mixin is the mixin itself.
	//
	// Its body is empty.
	Mixin Mixin

	// Var is the variable used by the depending mixins to call this mixin.
	Var string
	// RequiredBy are the names of the depending mixins.
	RequiredBy []string
}

type RangeExpression

type RangeExpression struct {
	Var1, Var2 *GoIdent

	EqPos    Position // Position of the '=' or ':='
	Declares bool     // true if the range expression declares new variables (':=')
	Ordered  bool     // true if the range expression is ordered ('= ordered range')

	// RangeExpression is the expression that is being iterated over.
	RangeExpression Expression

	Position
}

RangeExpression represents a range expression as used for for-loops.

Hence, it is only present on for For items.

type RawCommandFilterArg

type RawCommandFilterArg struct {
	Value string
	Position
}

type RawFilter

type RawFilter struct {
	Type RawFilterType
	Body []FilterLine
	Position
}

type RawFilterType

type RawFilterType string
const (
	RawHTML RawFilterType = "html"
	RawSVG  RawFilterType = "svg"
	RawJS   RawFilterType = "js"
	RawCSS  RawFilterType = "css"
)

type Return

type Return struct {
	Err *Expression
	Position
}

type Scope

type Scope []ScopeItem

A Scope represents a level of indentation. Every mixin available inside a scope is also available in its child scopes.

type ScopeItem

type ScopeItem interface {
	Poser
	// contains filtered or unexported methods
}

ScopeItem represents an item in a scope.

type SimpleAttribute

type SimpleAttribute struct {
	Name string

	AssignPos *Position // pos of '=' or '!='; nil for boolean attributes

	Value *Expression // nil for boolean attributes

	Position
}

type SimpleInterpolation

type SimpleInterpolation struct {
	NoEscape bool
	Value    InterpolationValue

	Position
}

type String

type String struct {
	Quote    byte
	Contents string

	Position
}

type StringCommandFilterArg

type StringCommandFilterArg String

type StringExpression

type StringExpression struct {
	Quote    byte // either " or `
	Contents []StringExpressionItem

	// Position is the position of the quote.
	Position
}

StringExpression represents a sequence of characters enclosed in double quotes or backticks.

type StringExpressionInterpolation

type StringExpressionInterpolation struct {
	FormatDirective      string // a Sprintf formatting placeholder, w/o preceding '%'
	Expression           Expression
	LBracePos, RBracePos Position
	Position
}

type StringExpressionItem

type StringExpressionItem interface {
	// contains filtered or unexported methods
}

type StringExpressionText

type StringExpressionText struct {
	Text string
	Position
}

type Switch

type Switch struct {
	// Comparator is the expression that is compared against.
	//
	// It may be nil, in which case the cases will contain boolean
	// expressions.
	Comparator *Expression

	// Cases are the cases of the Switch.
	Cases []Case
	// Default is the default case, if there is one.
	Default *Case

	Position
}

Switch represents a 'switch' statement.

type TernaryExpression

type TernaryExpression struct {
	// Condition is the Expression yielding the condition that is being
	// evaluated.
	Condition Expression // not a ChainExpression

	// IfTrue is the Expression used if the condition is true.
	IfTrue Expression // not a ChainExpression
	// IfFalse is the Expression used if the condition is false.
	IfFalse Expression // not a ChainExpression

	RParenPos Position
	// LParenPos is Position.Col+1
	Position
}

TernaryExpression represents a ternary expression.

type Text

type Text struct {
	Text string
	Position
}

Text is a string of text written as content of an element. It is not HTML-escaped yet.

type TextInterpolationValue

type TextInterpolationValue struct {
	LBracketPos Position
	Text        string
	RBracketPos Position
}

func (TextInterpolationValue) Pos

func (interp TextInterpolationValue) Pos() Position

type TextItem

type TextItem interface {
	Poser
	// contains filtered or unexported methods
}

type TextLine

type TextLine []TextItem

func (TextLine) Pos

func (l TextLine) Pos() Position

type Type

type Type uint8
const (
	TypeMain Type = iota + 1
	TypeTemplate
	TypeLibraryFile
	TypeInclude
)

type TypeAssertionExpression

type TypeAssertionExpression struct {
	PointerCount int
	Package      *GoIdent
	Type         GoIdent
	RParenPos    Position

	// Check indicates whether to check if the assertion was successful.
	Check bool

	Position // of the dot
}

TypeAssertionExpression represents a type assertion.

type Use

type Use struct {
	Uses []UseSpec

	// Position is the position of the use keyword.
	// Hence, multiple uses may share the same position, if they are
	// grouped in a block.
	Position
}

type UseSpec

type UseSpec struct {
	// Alias is the alias of the used files, if any.
	Alias *Ident
	// Path is the path to the directory or file.
	Path String

	Position

	// Library is the used library.
	//
	// It is filled by the linker.
	Library *Library
}

Directories

Path Synopsis
Package fileutil provides utilities for interacting with a corgi AST and its contents.
Package fileutil provides utilities for interacting with a corgi AST and its contents.
Package precomp allows encoding and decoding of precompiled libraries.
Package precomp allows encoding and decoding of precompiled libraries.

Jump to

Keyboard shortcuts

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