parser

package
v0.0.0-...-dd1d5c9 Latest Latest
Warning

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

Go to latest
Published: Jul 1, 2023 License: BSD-3-Clause Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TokenKeyword = "%token"
	TypeKeyword  = "%type"

	// intermediate tokens used by the lexer, not directly consumed by the
	// parser.
	Arrow = -2
)
View Source
const (
	LRTokenToken          = LRSymbolId(256)
	LRTypeToken           = LRSymbolId(257)
	LRStartToken          = LRSymbolId(258)
	LRRuleDefToken        = LRSymbolId(259)
	LRLabelToken          = LRSymbolId(260)
	LRSectionMarkerToken  = LRSymbolId(261)
	LRCharacterToken      = LRSymbolId(262)
	LRIdentifierToken     = LRSymbolId(263)
	LRSectionContentToken = LRSymbolId(264)
)
View Source
const (
	LRGrammarType              = LRSymbolId(269)
	LRAdditionalSectionsType   = LRSymbolId(270)
	LRAdditionalSectionType    = LRSymbolId(271)
	LRDefsType                 = LRSymbolId(272)
	LRDefType                  = LRSymbolId(273)
	LRRwordType                = LRSymbolId(274)
	LRNonemptyIdentListType    = LRSymbolId(275)
	LRNonemptyIdOrCharListType = LRSymbolId(276)
	LRIdOrCharListType         = LRSymbolId(277)
	LRRuleType                 = LRSymbolId(278)
	LRLabeledClausesType       = LRSymbolId(279)
	LRLabeledClauseType        = LRSymbolId(280)
)

Variables

This section is empty.

Functions

This section is empty.

Types

type AdditionalSection

type AdditionalSection struct {
	Name    *Token
	Content *Token
}

func NewAdditionalSection

func NewAdditionalSection(name *Token, content *Token) *AdditionalSection

type Clause

type Clause struct {
	Label *Token // optional
	Body  []*Token

	// set by NewRule
	LRLocation
	Parent *Rule

	SortId int
}

func NewClause

func NewClause(label *Token, body []*Token) *Clause

func (*Clause) String

func (clause *Clause) String() string

type Definition

type Definition interface {
	Loc() LRLocation
	String() string
}

type Grammar

type Grammar struct {
	Definitions []Definition

	AdditionalSections []*AdditionalSection
}

func LRParse

func LRParse(lexer LRLexer, reducer LRReducer) (*Grammar, error)

func LRParseWithCustomErrorHandler

func LRParseWithCustomErrorHandler(
	lexer LRLexer,
	reducer LRReducer,
	errHandler LRParseErrorHandler) (
	*Grammar,
	error)

func NewGrammar

func NewGrammar(
	defs []Definition,
	additionalSections []*AdditionalSection) *Grammar

func Parse

func Parse(filename string, reader io.Reader) (*Grammar, error)

type LRDefaultParseErrorHandler

type LRDefaultParseErrorHandler struct{}

func (LRDefaultParseErrorHandler) Error

func (LRDefaultParseErrorHandler) Error(nextToken LRToken, stack _LRStack) error

type LRGenericSymbol

type LRGenericSymbol struct {
	LRSymbolId
	LRLocation
}

func (*LRGenericSymbol) Id

func (t *LRGenericSymbol) Id() LRSymbolId

func (*LRGenericSymbol) Loc

func (t *LRGenericSymbol) Loc() LRLocation

type LRLexer

type LRLexer interface {
	// Note: Return io.EOF to indicate end of stream
	// Token with unspecified value type should return *LRGenericSymbol
	Next() (LRToken, error)

	CurrentLocation() LRLocation
}

func NewLexer

func NewLexer(filename string, reader io.Reader) LRLexer

type LRLocation

type LRLocation struct {
	FileName string
	Line     int
	Column   int
}

func (LRLocation) ShortString

func (l LRLocation) ShortString() string

func (LRLocation) String

func (l LRLocation) String() string

type LRParseErrorHandler

type LRParseErrorHandler interface {
	Error(nextToken LRToken, parseStack _LRStack) error
}

type LRReducer

type LRReducer interface {
	// 42:11: grammar -> ...
	ToGrammar(Defs_ []Definition, AdditionalSections_ []*AdditionalSection) (*Grammar, error)

	// 45:4: additional_sections -> add: ...
	AddToAdditionalSections(AdditionalSections_ []*AdditionalSection, AdditionalSection_ *AdditionalSection) ([]*AdditionalSection, error)

	// 46:4: additional_sections -> nil: ...
	NilToAdditionalSections() ([]*AdditionalSection, error)

	// 48:22: additional_section -> ...
	ToAdditionalSection(SectionMarker_ *LRGenericSymbol, Identifier_ *Token, SectionContent_ *Token) (*AdditionalSection, error)

	// 51:4: defs -> add: ...
	AddToDefs(Defs_ []Definition, Def_ Definition) ([]Definition, error)

	// 52:4: defs -> add_explicit: ...
	AddExplicitToDefs(Defs_ []Definition, Def_ Definition, char *LRGenericSymbol) ([]Definition, error)

	// 53:4: defs -> def: ...
	DefToDefs(Def_ Definition) ([]Definition, error)

	// 54:4: defs -> explicit_def: ...
	ExplicitDefToDefs(Def_ Definition, char *LRGenericSymbol) ([]Definition, error)

	// 59:4: def -> term_decl: ...
	TermDeclToDef(Rword_ *LRGenericSymbol, char *LRGenericSymbol, Identifier_ *Token, char2 *LRGenericSymbol, NonemptyIdOrCharList_ []*Token) (Definition, error)

	// 60:4: def -> untyped_term_decl: ...
	UntypedTermDeclToDef(Rword_ *LRGenericSymbol, NonemptyIdOrCharList_ []*Token) (Definition, error)

	// 62:4: def -> start_decl: ...
	StartDeclToDef(Start_ *LRGenericSymbol, NonemptyIdentList_ []*Token) (Definition, error)

	// 63:4: def -> rule: ...
	RuleToDef(Rule_ *Rule) (Definition, error)

	// 66:4: rword -> token: ...
	TokenToRword(Token_ *LRGenericSymbol) (*LRGenericSymbol, error)

	// 67:4: rword -> type: ...
	TypeToRword(Type_ *LRGenericSymbol) (*LRGenericSymbol, error)

	// 70:4: nonempty_ident_list -> add: ...
	AddToNonemptyIdentList(NonemptyIdentList_ []*Token, Identifier_ *Token) ([]*Token, error)

	// 71:4: nonempty_ident_list -> ident: ...
	IdentToNonemptyIdentList(Identifier_ *Token) ([]*Token, error)

	// 74:4: nonempty_id_or_char_list -> add_id: ...
	AddIdToNonemptyIdOrCharList(NonemptyIdOrCharList_ []*Token, Identifier_ *Token) ([]*Token, error)

	// 75:4: nonempty_id_or_char_list -> add_char: ...
	AddCharToNonemptyIdOrCharList(NonemptyIdOrCharList_ []*Token, Character_ *Token) ([]*Token, error)

	// 76:4: nonempty_id_or_char_list -> id: ...
	IdToNonemptyIdOrCharList(Identifier_ *Token) ([]*Token, error)

	// 77:4: nonempty_id_or_char_list -> char: ...
	CharToNonemptyIdOrCharList(Character_ *Token) ([]*Token, error)

	// 80:4: id_or_char_list -> list: ...
	ListToIdOrCharList(NonemptyIdOrCharList_ []*Token) ([]*Token, error)

	// 81:4: id_or_char_list -> nil: ...
	NilToIdOrCharList() ([]*Token, error)

	// 84:4: rule -> unlabeled_clause: ...
	UnlabeledClauseToRule(RuleDef_ *Token, IdOrCharList_ []*Token) (*Rule, error)

	// 85:4: rule -> clauses: ...
	ClausesToRule(RuleDef_ *Token, LabeledClauses_ []*Clause) (*Rule, error)

	// 88:4: labeled_clauses -> add: ...
	AddToLabeledClauses(LabeledClauses_ []*Clause, char *LRGenericSymbol, LabeledClause_ *Clause) ([]*Clause, error)

	// 89:4: labeled_clauses -> clause: ...
	ClauseToLabeledClauses(LabeledClause_ *Clause) ([]*Clause, error)

	// 91:18: labeled_clause -> ...
	ToLabeledClause(Label_ *Token, IdOrCharList_ []*Token) (*Clause, error)
}

type LRSymbol

type LRSymbol struct {
	SymbolId_ LRSymbolId

	Generic_ *LRGenericSymbol

	AdditionalSection  *AdditionalSection
	AdditionalSections []*AdditionalSection
	Clause             *Clause
	Clauses            []*Clause
	Definition         Definition
	Definitions        []Definition
	Grammar            *Grammar
	Rule               *Rule
	Token              *Token
	Tokens             []*Token
}

func NewSymbol

func NewSymbol(token LRToken) (*LRSymbol, error)

func (*LRSymbol) Id

func (s *LRSymbol) Id() LRSymbolId

func (*LRSymbol) Loc

func (s *LRSymbol) Loc() LRLocation

type LRSymbolId

type LRSymbolId int

func LRExpectedTerminals

func LRExpectedTerminals(id _LRStateId) []LRSymbolId

func (LRSymbolId) String

func (i LRSymbolId) String() string

type LRToken

type LRToken interface {
	Id() LRSymbolId
	Loc() LRLocation
}

type Reducer

type Reducer struct {
}

func (Reducer) AddCharToNonemptyIdOrCharList

func (Reducer) AddCharToNonemptyIdOrCharList(
	list []*Token,
	char *Token) (
	[]*Token,
	error)

func (Reducer) AddExplicitToDefs

func (Reducer) AddExplicitToDefs(
	defs []Definition,
	def Definition,
	terminator *LRGenericSymbol) (
	[]Definition, error)

func (Reducer) AddIdToNonemptyIdOrCharList

func (Reducer) AddIdToNonemptyIdOrCharList(
	list []*Token,
	id *Token) (
	[]*Token,
	error)

func (Reducer) AddToAdditionalSections

func (Reducer) AddToAdditionalSections(
	sections []*AdditionalSection,
	section *AdditionalSection) (
	[]*AdditionalSection,
	error)

func (Reducer) AddToDefs

func (Reducer) AddToDefs(
	defs []Definition,
	def Definition) (
	[]Definition,
	error)

func (Reducer) AddToLabeledClauses

func (Reducer) AddToLabeledClauses(
	clauses []*Clause,
	or *LRGenericSymbol,
	clause *Clause) (
	[]*Clause,
	error)

func (Reducer) AddToNonemptyIdentList

func (Reducer) AddToNonemptyIdentList(
	identList []*Token,
	ident *Token) (
	[]*Token,
	error)

func (Reducer) CharToNonemptyIdOrCharList

func (Reducer) CharToNonemptyIdOrCharList(char *Token) ([]*Token, error)

func (Reducer) ClauseToLabeledClauses

func (Reducer) ClauseToLabeledClauses(clause *Clause) ([]*Clause, error)

func (Reducer) ClausesToRule

func (Reducer) ClausesToRule(
	ruleName *Token,
	clauses []*Clause) (
	*Rule,
	error)

func (Reducer) DefToDefs

func (Reducer) DefToDefs(def Definition) ([]Definition, error)

func (Reducer) ExplicitDefToDefs

func (Reducer) ExplicitDefToDefs(
	def Definition,
	terminator *LRGenericSymbol) (
	[]Definition,
	error)

func (Reducer) IdToNonemptyIdOrCharList

func (Reducer) IdToNonemptyIdOrCharList(id *Token) ([]*Token, error)

func (Reducer) IdentToNonemptyIdentList

func (Reducer) IdentToNonemptyIdentList(ident *Token) ([]*Token, error)

func (Reducer) ListToIdOrCharList

func (Reducer) ListToIdOrCharList(list []*Token) ([]*Token, error)

func (Reducer) NilToAdditionalSections

func (Reducer) NilToAdditionalSections() ([]*AdditionalSection, error)

func (Reducer) NilToIdOrCharList

func (Reducer) NilToIdOrCharList() ([]*Token, error)

func (Reducer) RuleToDef

func (Reducer) RuleToDef(rule *Rule) (Definition, error)

func (Reducer) StartDeclToDef

func (Reducer) StartDeclToDef(
	startKw *LRGenericSymbol,
	ruleNames []*Token) (
	Definition,
	error)

func (Reducer) TermDeclToDef

func (Reducer) TermDeclToDef(
	rword *LRGenericSymbol,
	lt *LRGenericSymbol,
	value *Token,
	gt *LRGenericSymbol,
	terms []*Token) (
	Definition,
	error)

func (Reducer) ToAdditionalSection

func (Reducer) ToAdditionalSection(
	marker *LRGenericSymbol,
	name *Token,
	content *Token) (
	*AdditionalSection,
	error)

func (Reducer) ToGrammar

func (Reducer) ToGrammar(
	defs []Definition,
	additionalSections []*AdditionalSection) (
	*Grammar,
	error)

func (Reducer) ToLabeledClause

func (Reducer) ToLabeledClause(
	label *Token,
	clauseBody []*Token) (
	*Clause,
	error)

func (Reducer) TokenToRword

func (Reducer) TokenToRword(tokenKw *LRGenericSymbol) (*LRGenericSymbol, error)

func (Reducer) TypeToRword

func (Reducer) TypeToRword(typeKw *LRGenericSymbol) (*LRGenericSymbol, error)

func (Reducer) UnlabeledClauseToRule

func (Reducer) UnlabeledClauseToRule(
	ruleName *Token,
	clauseBody []*Token) (
	*Rule,
	error)

func (Reducer) UntypedTermDeclToDef

func (Reducer) UntypedTermDeclToDef(
	rword *LRGenericSymbol,
	terms []*Token) (
	Definition,
	error)

type Rule

type Rule struct {
	Name    *Token
	Clauses []*Clause
}

func NewRule

func NewRule(name *Token, clauses []*Clause) *Rule

func (*Rule) Loc

func (r *Rule) Loc() LRLocation

func (*Rule) String

func (r *Rule) String() string

type StartDeclaration

type StartDeclaration struct {
	LRLocation

	Ids []*Token
}

func NewStartDeclaration

func NewStartDeclaration(
	start *LRGenericSymbol,
	ids []*Token) *StartDeclaration

func (*StartDeclaration) Loc

func (sd *StartDeclaration) Loc() LRLocation

func (*StartDeclaration) String

func (sd *StartDeclaration) String() string

type TermDeclaration

type TermDeclaration struct {
	TermType *LRGenericSymbol

	IsTerminal bool

	ValueType *Token

	Terms []*Token
}

func NewTermDeclaration

func NewTermDeclaration(
	termType *LRGenericSymbol,
	valueType *Token,
	terms []*Token) *TermDeclaration

func (*TermDeclaration) Loc

func (td *TermDeclaration) Loc() LRLocation

func (*TermDeclaration) String

func (td *TermDeclaration) String() string

type Token

type Token struct {
	LRLocation

	LRSymbolId
	Value string
}

func (*Token) Id

func (t *Token) Id() LRSymbolId

func (*Token) Loc

func (t *Token) Loc() LRLocation

func (*Token) String

func (t *Token) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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