lex

package
v0.5.6 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2022 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package lex provides the lexer for corgi.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrMixedIndentation is the error returned if both tabs and spaces are
	// used in a single indent.
	ErrMixedIndentation = errors.New("you cannot mix tabs and spaces when indenting")

	ErrIndentationIncrease = errors.New("you can only increase one indentation level at a time")
)

Functions

This section is empty.

Types

type EOFError

type EOFError struct {
	After string
	In    string
}

EOFError is the error returned when an end of file was encountered unexpectedly.

func (*EOFError) Error

func (e *EOFError) Error() string

type EOLError

type EOLError struct {
	After string
	In    string
}

EOLError is the error returned when a newline was encountered unexpectedly.

func (*EOLError) Error

func (e *EOLError) Error() string

type IllegalIndentationError

type IllegalIndentationError struct {
	In string
}

IllegalIndentationError is the error returned when using indentation in a place where it can't be used, such as an import statement.

func (*IllegalIndentationError) Error

func (e *IllegalIndentationError) Error() string

type IndentationError

type IndentationError struct {
	Expect    rune
	ExpectLen int
	RefLine   int

	Actual    rune
	ActualLen int
}

IndentationError is the error returned when the indentation of a line is inconsistent with the first indented line, which is used as reference. This occurs either when using both tabs and spaces for indentation or when the amount of indentation is not consistent.

func (*IndentationError) Error

func (e *IndentationError) Error() string

type Item

type Item struct {
	// Type is the type of the item.
	Type ItemType
	// Expression is the value of the item, if any.
	Val string
	// Err is the error.
	// It is only set when Type is Error.
	Err error
	// Line is the line where the item starts.
	Line int
	// Col is the column after which the item starts.
	Col int
}

Item represents a lexical item as emitted by the lexer.

type ItemType

type ItemType uint8

ItemType is an enum representing the type of item the lexer encounters.

The value assigned to an item may change, e.g. if the list ever gets expanded. Only use ItemTypes in conjunction with their constants.

const (
	Error ItemType = iota
	EOF

	Indent // indention level increased
	Dedent // indention level decreased

	Element    // element name
	Ident      // identifier
	Literal    // after a '.', '#' etc.
	Expression // a Go expression
	Text       // the text after an element, pipe etc. that needs HTML escaping

	CodeStart // '-'
	Code      // after CodeStart

	Ternary     // '?' at the start of code
	TernaryElse // ':' after the ifTrue

	NilCheck // '?' for nil and out-of-bounds checks

	LParen   // '('
	RParen   // ')'
	LBrace   // '{'
	RBrace   // '}'
	LBracket // '['
	RBracket // ']'

	Assign         // '='
	AssignNoEscape // '!='
	Comma          // ',' used for mixin args and ternary expressions

	Comment // '//'

	Import // 'import'
	Func   // 'func'

	Extend  // 'extend'
	Include // 'include'
	Use     // 'use'

	Doctype // 'doctype'

	Block        // 'block'
	BlockAppend  // 'append' or 'block append'
	BlockPrepend // 'prepend' or 'block prepend'

	If      // 'if'
	IfBlock // 'if block'
	ElseIf  // 'else if'
	Else    // 'else'

	Switch      // 'switch'
	Case        // 'case'
	DefaultCase // 'default

	For   // 'for'
	Range // 'range' keyword used in a for

	While // 'while'

	Mixin              // 'mixin'
	MixinCall          // '+'
	MixinBlockShortcut // '>' after a mixin call with a single block

	And // '&'

	Div // sent before a '.' or '#' to indicate a div is being created

	Class // '.'
	ID    // '#'

	BlockExpansion // ':'

	Filter // ':'

	DotBlock     // '.' e.g., after an element, such as 'p.'
	DotBlockLine // used at the start of each line in a DotBlock

	Pipe // '|'

	Hash     // '#'
	NoEscape // '!' after a hash

	TagVoid // '/'
)

func (ItemType) String

func (i ItemType) String() string

type Lexer

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

func New

func New(in string) *Lexer

New creates a new lexer.

func (*Lexer) Lex

func (l *Lexer) Lex()

Lex starts a new goroutine that lexes the input. The lexical items can be retrieved by calling Next.

func (*Lexer) Next

func (l *Lexer) Next() Item

Next returns the next lexical item.

func (*Lexer) Stop

func (l *Lexer) Stop()

Stop stops the lexer's goroutine by draining all input elements.

type UnknownItemError

type UnknownItemError struct {
	Expected string
}

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

func (*UnknownItemError) Error

func (e *UnknownItemError) Error() string

Jump to

Keyboard shortcuts

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