parser

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2025 License: GPL-3.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// EOT represents "end of file" token name. Its value corresponds to "End Of Text"
	// control character of ASCII standard.
	EOT = "\x03"

	// Literal is pseudo--name for a token indicating that its name should be the actual value of token.
	Literal = "__literal__"

	// Skip is pseudo--name for token that lexer should ignore.
	Skip = "Skip"
)
View Source
const MaxTokenSize = 128

MaxTokenSize is the maximum number of character of a token

Variables

This section is empty.

Functions

func SortPatternOptionsByLength

func SortPatternOptionsByLength(pattern string) string

SortPatternOptionsByLength sort pattern alternatives like `<a1>|<a2>|<a3>` in such a way that ensures longest--matching by regex package.

Types

type Grammar

type Grammar struct {
	Term    utils.Set[string]
	NonTerm utils.Set[string]
	Start   string
	Prod    map[string][][]string
}

func NewGrammarFromProd

func NewGrammarFromProd(start string, prod map[string][][]string) *Grammar

func (*Grammar) FirstTable

func (g *Grammar) FirstTable() table

func (*Grammar) FollowTable

func (g *Grammar) FollowTable(firstTable table, start string) table

func (*Grammar) ParseTable

func (g *Grammar) ParseTable(start string) (ParseTable, error)

type Lexer

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

Lexer represent an object that can provide a sequence of Token

func NewLexer

func NewLexer(tokenDefs TokenDefs, reader io.Reader) *Lexer

NewLexer returns a Lexer that match token of tokenDefs and read from a reader

func (*Lexer) Consume

func (l *Lexer) Consume() error

Consume read next token. If an error occur, then Consume returns an error.

func (*Lexer) Current

func (l *Lexer) Current() Token

Current returns last token consumed. If lexer reached EOF, Current returns a token with name that equals EOT.

func (*Lexer) Previous

func (l *Lexer) Previous() Token

Previous returns token that precede current token.

func (*Lexer) Reset

func (l *Lexer) Reset(reader io.Reader)

Reset resets this lexer to read from another reader

type Node

type Node interface {
	fmt.Stringer
	Name() string
	utils.Ranger
}

type ParseTable

type ParseTable map[string]map[string]ParseTableCell

ParseTable maps a (non-term, term) to a sentential form

type ParseTableCell

type ParseTableCell struct {
	SententialForm []string
	Alternative    int
}

type ParseTree

type ParseTree struct {
	Value    Node
	Children []ParseTree
}

func (ParseTree) LeveledChildren

func (pt ParseTree) LeveledChildren(target string) []ParseTree

LeveledChildren returns a flat view of annidated (under named target nodes) children

Example with target:

a
├ b
│ ├ c
│ └ b
│  ├ d
│  └ e
└ f
  └ h
= [c, d, e, f]

func (ParseTree) RootString

func (pt ParseTree) RootString() string

func (ParseTree) VisitChildren

func (pt ParseTree) VisitChildren(v utils.TreeVisitor)

type Parser

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

Parser is an object capable of constructing a parse tree from a grammar and a reader. After initialization is secure for multiple go ruotine to use same object.

func New

func New(parseTable ParseTable, terminal utils.Set[string], start string, tokenDefs TokenDefs) *Parser

parseTable and tokenDefs should not be modified after

func NewFromGrammar

func NewFromGrammar(grammar *Grammar, tokenDefs TokenDefs) (*Parser, error)

parseTable and tokenDefs should not be modified after

func (*Parser) Parse

func (p *Parser) Parse(reader io.Reader) (ParseTree, error)

Parse returns parse tree for input reader or a error if occured.

type ProductionNode

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

func (ProductionNode) Alternative

func (p ProductionNode) Alternative() int

func (ProductionNode) End

func (n ProductionNode) End() utils.Position

func (ProductionNode) Name

func (n ProductionNode) Name() string

func (ProductionNode) Pos

func (n ProductionNode) Pos() utils.Position

func (ProductionNode) String

func (p ProductionNode) String() string

type Token

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

Token represents partial output of a Lexer.

func (Token) End

func (n Token) End() utils.Position

func (Token) Name

func (n Token) Name() string

func (Token) Pos

func (n Token) Pos() utils.Position

func (Token) String

func (t Token) String() string

func (Token) Value

func (p Token) Value() string

type TokenDefs

type TokenDefs []struct {
	Name    string
	Pattern string
}

TokenDefs represent a ordered list of token. Token that came first as precedence over last.

Jump to

Keyboard shortcuts

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