parser

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package parser builds an *ast.Program from a token stream produced by the lexer. The grammar implemented here is Lua 5.4 (reference manual §9).

The parser is a hand-written, single-pass recursive-descent parser with a Pratt-style operator-precedence layer for expressions. There is no state machine and no lookahead beyond a single peek token — Lua 5.4 is LL(2) at most (see the Name vs. funcname disambiguation in `parseStatement`).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Expression

type Expression struct {
	Value string
	Range Range
}

Expression containing Go code.

func NewExpression

func NewExpression(value string, from, to Position) Expression

NewExpression creates a Go expression.

type Mode

type Mode int

Mode controls REPL-friendly error reporting (e.g. distinguishing "still typing — give me more input" from a real syntax error).

const (
	NormalMode Mode = iota + 1
	REPLMode
)

Recognised parser modes.

type Parser

type Parser struct {
	Lexer *lexer.Lexer
	Mode  Mode
	// contains filtered or unexported fields
}

Parser owns the token cursor and parser state.

func New

func New(l *lexer.Lexer) *Parser

New constructs a parser ready to consume the supplied lexer's tokens. Call ParseProgram to drive it.

func (*Parser) ParseProgram

func (p *Parser) ParseProgram() (program *ast.Program, err *errors.Error)

ParseProgram drives the top-level block until EOF and returns the AST. Returns the partial AST and an error if one occurred. The parser is single-shot: do not call ParseProgram more than once on the same Parser.

type Position

type Position struct {
	Index int64
	Line  uint32
	Col   uint32
}

Source mapping to map from the source code of the template to the in-memory representation.

func NewPosition

func NewPosition(index int64, line, col uint32) Position

NewPosition initialises a position.

func (*Position) String

func (p *Position) String() string

type Range

type Range struct {
	From Position
	To   Position
}

Range of text within a file.

func NewRange

func NewRange(from, to Position) Range

NewRange creates a Range expression.

type SourceMap

type SourceMap struct {
	Expressions               []string
	SourceLinesToTarget       map[uint32]map[uint32]Position
	TargetLinesToSource       map[uint32]map[uint32]Position
	SourceSymbolRangeToTarget map[uint32]map[uint32]Range
	TargetSymbolRangeToSource map[uint32]map[uint32]Range
}

func NewSourceMap

func NewSourceMap() *SourceMap

func (*SourceMap) Add

func (sm *SourceMap) Add(src Expression, tgt Range) (updatedFrom Position)

Add an item to the lookup.

func (*SourceMap) AddSymbolRange

func (sm *SourceMap) AddSymbolRange(src Range, tgt Range)

func (*SourceMap) SourcePositionFromTarget

func (sm *SourceMap) SourcePositionFromTarget(line, col uint32) (src Position, ok bool)

SourcePositionFromTarget looks the source position using the target position. If a source exists on the line but not the col, the function will search backwards.

func (*SourceMap) SymbolSourceRangeFromTarget

func (sm *SourceMap) SymbolSourceRangeFromTarget(line, col uint32) (src Range, ok bool)

func (*SourceMap) SymbolTargetRangeFromSource

func (sm *SourceMap) SymbolTargetRangeFromSource(line, col uint32) (tgt Range, ok bool)

func (*SourceMap) TargetPositionFromSource

func (sm *SourceMap) TargetPositionFromSource(line, col uint32) (tgt Position, ok bool)

TargetPositionFromSource looks up the target position using the source position.

Directories

Path Synopsis
Package errors defines the typed parser error used throughout the luascript front-end.
Package errors defines the typed parser error used throughout the luascript front-end.
Package precedence encodes the Lua 5.4 binary-operator precedence ladder (see Lua 5.4 reference manual §3.4.8).
Package precedence encodes the Lua 5.4 binary-operator precedence ladder (see Lua 5.4 reference manual §3.4.8).

Jump to

Keyboard shortcuts

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