ast

package
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2025 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultModeName = "$default"
View Source
const Print = 1000

Variables

View Source
var AmbiguousAlias = &TokenRule{}
View Source
var DiscardStatementSingleton = &DiscardStatement{}

DiscardStatementSingleton is a singleton for DiscardStatement. It works because DiscardStatement has no data and will be discarded by the parser.

Functions

func RunPass

func RunPass[T AST](ctx *Context, asts []T, pass Pass)

Types

type AST

type AST interface {
	RunPass(ctx *Context, pass Pass)
	SetBounds(b Bounds)
	Bounds() Bounds
}

type Action

type Action interface {
	AST

	// GetAction returns the mode.Action corresponding to the AST.
	GetAction() mode.Action
}

Action is the interface implemented by ASTs that define actions for tokens and fragments.

type ActionDiscard

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

ActionDiscard is the AST for the action @discard.

E.g.

// Discard whitespaces.
@frag [ \n\r\t]+  @discard;

func (*ActionDiscard) Bounds

func (a *ActionDiscard) Bounds() Bounds

func (*ActionDiscard) GetAction

func (a *ActionDiscard) GetAction() mode.Action

func (*ActionDiscard) RunPass

func (a *ActionDiscard) RunPass(ctx *Context, pass Pass)

func (*ActionDiscard) SetBounds

func (a *ActionDiscard) SetBounds(b Bounds)

type ActionEmit

type ActionEmit struct {
	Name string

	Terminal *lr1.Terminal
	// contains filtered or unexported fields
}

func (*ActionEmit) Bounds

func (a *ActionEmit) Bounds() Bounds

func (*ActionEmit) GetAction

func (a *ActionEmit) GetAction() mode.Action

func (*ActionEmit) RunPass

func (a *ActionEmit) RunPass(ctx *Context, pass Pass)

func (*ActionEmit) SetBounds

func (a *ActionEmit) SetBounds(b Bounds)

type ActionPopMode

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

ActionPopMode is the AST for the action @pop_mode.

Example:

@mode StringLiteral {
  STRING = '"' @pop_mode ;
  @frag [\u0020-\U0010FFFF]* ;
}

func (*ActionPopMode) Bounds

func (a *ActionPopMode) Bounds() Bounds

func (*ActionPopMode) GetAction

func (a *ActionPopMode) GetAction() mode.Action

func (*ActionPopMode) RunPass

func (a *ActionPopMode) RunPass(ctx *Context, pass Pass)

func (*ActionPopMode) SetBounds

func (a *ActionPopMode) SetBounds(b Bounds)

type ActionPushMode

type ActionPushMode struct {
	Mode string
	// contains filtered or unexported fields
}

ActionPushMode is the AST for the action @push_mode.

Example:

@frag '"'  @push_mode(StringLiteral) ;

func (*ActionPushMode) Bounds

func (a *ActionPushMode) Bounds() Bounds

func (*ActionPushMode) GetAction

func (a *ActionPushMode) GetAction() mode.Action

func (*ActionPushMode) RunPass

func (a *ActionPushMode) RunPass(ctx *Context, pass Pass)

func (*ActionPushMode) SetBounds

func (a *ActionPushMode) SetBounds(b Bounds)

type Associativity

type Associativity int
const (
	Left  Associativity = 0
	Right Associativity = 1
)

type Bounds

type Bounds struct {
	Begin gotoken.Pos
	End   gotoken.Pos
}

type Card

type Card int
const (
	One Card = iota
	ZeroOrOne
	ZeroOrMore
	ZeroOrMoreNG
	OneOrMore
	OneOrMoreNG
)

type CharClass

type CharClass struct {
	Neg            bool
	CharClassItems []*CharClassItem
	// contains filtered or unexported fields
}

func (*CharClass) Bounds

func (a *CharClass) Bounds() Bounds

func (*CharClass) GetRanges

func (t *CharClass) GetRanges() []rang3.Range

func (*CharClass) RunPass

func (t *CharClass) RunPass(ctx *Context, pass Pass)

func (*CharClass) SetBounds

func (a *CharClass) SetBounds(b Bounds)

type CharClassBinaryExpr

type CharClassBinaryExpr struct {
	Op    CharClassBinaryExprOp
	Left  CharClassExpr
	Right CharClassExpr
	// contains filtered or unexported fields
}

func (*CharClassBinaryExpr) Bounds

func (a *CharClassBinaryExpr) Bounds() Bounds

func (*CharClassBinaryExpr) GetRanges

func (e *CharClassBinaryExpr) GetRanges() []rang3.Range

func (*CharClassBinaryExpr) RunPass

func (e *CharClassBinaryExpr) RunPass(ctx *Context, pass Pass)

func (*CharClassBinaryExpr) SetBounds

func (a *CharClassBinaryExpr) SetBounds(b Bounds)

type CharClassBinaryExprOp

type CharClassBinaryExprOp int
const (
	CharClassBinaryExprAdd CharClassBinaryExprOp = 0
	CharClassBinaryExprSub CharClassBinaryExprOp = 1
)

type CharClassExpr

type CharClassExpr interface {
	AST
	GetRanges() []rang3.Range
}

type CharClassItem

type CharClassItem struct {
	From rune
	To   rune
	// contains filtered or unexported fields
}

func (*CharClassItem) Bounds

func (a *CharClassItem) Bounds() Bounds

func (*CharClassItem) RunPass

func (i *CharClassItem) RunPass(ctx *Context, pass Pass)

func (*CharClassItem) SetBounds

func (a *CharClassItem) SetBounds(b Bounds)

type Context

type Context struct {
	FSet *gotoken.FileSet
	Errs *errlogger.ErrLogger

	StartParserRule   *ParserRule
	HasParserRules    bool
	CurrentUnit       stack.Stack[*Unit]
	CurrentParserRule stack.Stack[*ParserRule]
	CurrentParserProd stack.Stack[*ParserProd]
	CurrentPrinter    stack.Stack[*Printer]
	CurrentLexerMode  stack.Stack[*mode.ModeBuilder]
	Grammar           *lr1.Grammar
	LexerModes        map[string]*mode.ModeBuilder
	LexerDFAs         map[string]*mode.Mode
	// contains filtered or unexported fields
}

func NewContext

func NewContext(fset *gotoken.FileSet, errs *errlogger.ErrLogger) *Context

func (*Context) Analyze

func (c *Context) Analyze(ast AST, untilPass Pass) bool

func (*Context) CreateAlias

func (c *Context) CreateAlias(name string, t *TokenRule)

func (*Context) CreateMode

func (c *Context) CreateMode(name string) *mode.ModeBuilder

func (*Context) Lookup

func (c *Context) Lookup(name string) AST

func (*Context) LookupAlias

func (c *Context) LookupAlias(name string) *TokenRule

func (*Context) Mode

func (c *Context) Mode() *mode.ModeBuilder

func (*Context) Position

func (c *Context) Position(ast AST) gotoken.Pos

func (*Context) Print

func (c *Context) Print(ast AST, out io.Writer)

func (*Context) RegisterName

func (c *Context) RegisterName(name string, ast AST) bool

type DiscardStatement

type DiscardStatement struct{}

DiscardStatement is a dummy Statement that will be discarded by the parser. It is used with the *! cardinality which makes the parser call the Discard() method.

func (*DiscardStatement) Bounds

func (s *DiscardStatement) Bounds() Bounds

func (*DiscardStatement) Discard

func (n *DiscardStatement) Discard() bool

func (*DiscardStatement) RunPass

func (n *DiscardStatement) RunPass(ctx *Context, pass Pass)

func (*DiscardStatement) SetBounds

func (s *DiscardStatement) SetBounds(b Bounds)

type ExternalName

type ExternalName struct {
	Name string
	// contains filtered or unexported fields
}

func (*ExternalName) Bounds

func (a *ExternalName) Bounds() Bounds

func (*ExternalName) RunPass

func (n *ExternalName) RunPass(ctx *Context, pass Pass)

func (*ExternalName) SetBounds

func (a *ExternalName) SetBounds(b Bounds)

type ExternalRule

type ExternalRule struct {
	Names []*ExternalName
	// contains filtered or unexported fields
}

func (*ExternalRule) Discard

func (s *ExternalRule) Discard() bool

func (*ExternalRule) RunPass

func (r *ExternalRule) RunPass(ctx *Context, pass Pass)

type FragRule

type FragRule struct {
	Expr    *LexerExpr
	Actions []Action
	// contains filtered or unexported fields
}

func (*FragRule) Discard

func (s *FragRule) Discard() bool

func (*FragRule) RunPass

func (r *FragRule) RunPass(ctx *Context, pass Pass)

type LexerExpr

type LexerExpr struct {
	Factors []*LexerFactor
	// contains filtered or unexported fields
}

func (*LexerExpr) Bounds

func (a *LexerExpr) Bounds() Bounds

func (*LexerExpr) NFACons

func (e *LexerExpr) NFACons(ctx *Context) *mode.NFAComposite

func (*LexerExpr) RunPass

func (e *LexerExpr) RunPass(ctx *Context, pass Pass)

func (*LexerExpr) SetBounds

func (a *LexerExpr) SetBounds(b Bounds)

type LexerFactor

type LexerFactor struct {
	Terms []*LexerTermCard
	// contains filtered or unexported fields
}

func (*LexerFactor) Bounds

func (a *LexerFactor) Bounds() Bounds

func (*LexerFactor) NFACons

func (f *LexerFactor) NFACons(ctx *Context) *mode.NFAComposite

func (*LexerFactor) RunPass

func (f *LexerFactor) RunPass(ctx *Context, pass Pass)

func (*LexerFactor) SetBounds

func (a *LexerFactor) SetBounds(b Bounds)

type LexerTerm

type LexerTerm interface {
	AST
	NFACons(ctx *Context) *mode.NFAComposite
}

type LexerTermCard

type LexerTermCard struct {
	Term LexerTerm
	Card Card
	// contains filtered or unexported fields
}

func (*LexerTermCard) Bounds

func (a *LexerTermCard) Bounds() Bounds

func (*LexerTermCard) NFACons

func (t *LexerTermCard) NFACons(ctx *Context) *mode.NFAComposite

func (*LexerTermCard) RunPass

func (t *LexerTermCard) RunPass(ctx *Context, pass Pass)

func (*LexerTermCard) SetBounds

func (a *LexerTermCard) SetBounds(b Bounds)

type LexerTermCharClass

type LexerTermCharClass struct {
	Expr CharClassExpr
	// contains filtered or unexported fields
}

func (*LexerTermCharClass) Bounds

func (a *LexerTermCharClass) Bounds() Bounds

func (*LexerTermCharClass) NFACons

func (t *LexerTermCharClass) NFACons(ctx *Context) *mode.NFAComposite

func (*LexerTermCharClass) RunPass

func (t *LexerTermCharClass) RunPass(ctx *Context, pass Pass)

func (*LexerTermCharClass) SetBounds

func (a *LexerTermCharClass) SetBounds(b Bounds)

type LexerTermLiteral

type LexerTermLiteral struct {
	Literal string
	// contains filtered or unexported fields
}

func (*LexerTermLiteral) Bounds

func (a *LexerTermLiteral) Bounds() Bounds

func (*LexerTermLiteral) NFACons

func (t *LexerTermLiteral) NFACons(ctx *Context) *mode.NFAComposite

func (*LexerTermLiteral) RunPass

func (t *LexerTermLiteral) RunPass(ctx *Context, pass Pass)

func (*LexerTermLiteral) SetBounds

func (a *LexerTermLiteral) SetBounds(b Bounds)

type LexerTermRef

type LexerTermRef struct {
	Ref string
	// contains filtered or unexported fields
}

func (*LexerTermRef) Bounds

func (a *LexerTermRef) Bounds() Bounds

func (*LexerTermRef) NFACons

func (t *LexerTermRef) NFACons(ctx *Context) *mode.NFAComposite

func (*LexerTermRef) RunPass

func (t *LexerTermRef) RunPass(ctx *Context, pass Pass)

func (*LexerTermRef) SetBounds

func (a *LexerTermRef) SetBounds(b Bounds)

type MacroRule

type MacroRule struct {
	Name string
	Expr *LexerExpr
	// contains filtered or unexported fields
}

func (*MacroRule) Discard

func (s *MacroRule) Discard() bool

func (*MacroRule) NFACons

func (r *MacroRule) NFACons(ctx *Context) *mode.NFAComposite

func (*MacroRule) RunPass

func (r *MacroRule) RunPass(ctx *Context, pass Pass)

type Mode

type Mode struct {
	Name  string
	Rules []Statement

	Mode *mode.ModeBuilder
	// contains filtered or unexported fields
}

func (*Mode) Discard

func (s *Mode) Discard() bool

func (*Mode) RunPass

func (m *Mode) RunPass(ctx *Context, pass Pass)

type ParserProd

type ParserProd struct {
	Terms     []*ParserTerm
	Qualifier *ProdQualifier

	Prod *lr1.Prod
	// contains filtered or unexported fields
}

func (*ParserProd) Bounds

func (a *ParserProd) Bounds() Bounds

func (*ParserProd) RunPass

func (p *ParserProd) RunPass(ctx *Context, pass Pass)

func (*ParserProd) SetBounds

func (a *ParserProd) SetBounds(b Bounds)

type ParserRule

type ParserRule struct {
	IsStart bool
	Name    string
	Prods   []*ParserProd

	Rule *lr1.Rule
	// contains filtered or unexported fields
}

func (*ParserRule) Discard

func (s *ParserRule) Discard() bool

func (*ParserRule) RunPass

func (r *ParserRule) RunPass(ctx *Context, pass Pass)

type ParserTerm

type ParserTerm struct {
	Type  ParserTermType
	Name  string
	Alias string
	Child *ParserTerm
	Sep   *ParserTerm

	Symbol lr1.Term
	// contains filtered or unexported fields
}

func (*ParserTerm) Bounds

func (a *ParserTerm) Bounds() Bounds

func (*ParserTerm) RunPass

func (t *ParserTerm) RunPass(ctx *Context, pass Pass)

func (*ParserTerm) SetBounds

func (a *ParserTerm) SetBounds(b Bounds)

type ParserTermType

type ParserTermType int
const (
	ParserTermSimple      ParserTermType = iota
	ParserTermZeroOrMore                 // *
	ParserTermZeroOrMoreF                // *!
	ParserTermOneOrMore                  // +
	ParserTermOneOrMoreF                 // +!
	ParserTermZeroOrOne                  // ?
	ParserTermList                       // @list
	ParserTermListOpt                    // @list?
	ParserTermError                      // @error
)

func (ParserTermType) String

func (t ParserTermType) String() string

type Pass

type Pass int
const (
	CreateNames Pass = iota
	Check
	Normalize
	GenerateGrammar
)
const AllPasses Pass = GenerateGrammar

type Printer

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

func NewPrinter

func NewPrinter(out io.Writer) *Printer

func (*Printer) Printf

func (p *Printer) Printf(s string, args ...any)

func (*Printer) WithIndent

func (p *Printer) WithIndent(n int) *Printer

type ProdQualifier

type ProdQualifier struct {
	Precedence    int
	Associativity Associativity
	// contains filtered or unexported fields
}

func (*ProdQualifier) Bounds

func (a *ProdQualifier) Bounds() Bounds

func (*ProdQualifier) SetBounds

func (a *ProdQualifier) SetBounds(b Bounds)

type Spec

type Spec struct {
	Units []*Unit

	DefaultLexerMode *mode.ModeBuilder
	// contains filtered or unexported fields
}

func (*Spec) Bounds

func (a *Spec) Bounds() Bounds

func (*Spec) RunPass

func (s *Spec) RunPass(ctx *Context, pass Pass)

func (*Spec) SetBounds

func (a *Spec) SetBounds(b Bounds)

type Statement

type Statement interface {
	AST

	Discard() bool
	// contains filtered or unexported methods
}

type TokenRule

type TokenRule struct {
	Name    string
	Expr    *LexerExpr
	Actions []Action

	Terminal *lr1.Terminal
	// contains filtered or unexported fields
}

func (*TokenRule) Discard

func (s *TokenRule) Discard() bool

func (*TokenRule) RunPass

func (r *TokenRule) RunPass(ctx *Context, pass Pass)

type Unit

type Unit struct {
	Statements []Statement
	// contains filtered or unexported fields
}

func (*Unit) Bounds

func (a *Unit) Bounds() Bounds

func (*Unit) RunPass

func (u *Unit) RunPass(ctx *Context, pass Pass)

func (*Unit) SetBounds

func (a *Unit) SetBounds(b Bounds)

Jump to

Keyboard shortcuts

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