ast

package
v0.0.0-...-017d103 Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2023 License: BSD-3-Clause Imports: 4 Imported by: 2

Documentation

Overview

Package ast declares the types used to represent syntax trees for bibtex files.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Walk

func Walk(n Node, w Walker) error

Walk walks the AST using depth-first-search.

Specifically, walker is first called on a node with isEntering set to true. Then each child is visited recursively. Finally, walker is called with isEntering set to false.

The traversal stops whenever the walker returns WalkStop or an error.

Types

type AbbrevDecl

type AbbrevDecl struct {
	Doc    *TexCommentGroup // associated documentation; or nil
	Entry  gotok.Pos        // position of the "@STRING" token
	Tag    *TagStmt
	RBrace gotok.Pos // position of the closing right brace token: "}".
}

An AbbrevDecl node represents a bibtex abbreviation, like:

@STRING { foo = "bar" }

func (*AbbrevDecl) End

func (e *AbbrevDecl) End() gotok.Pos

func (*AbbrevDecl) Kind

func (e *AbbrevDecl) Kind() NodeKind

func (*AbbrevDecl) Pos

func (e *AbbrevDecl) Pos() gotok.Pos

type Author

type Author struct {
	From, To gotok.Pos
	First    Expr // given name
	Prefix   Expr // often called the 'von' part
	Last     Expr // family name
	Suffix   Expr // often called the 'jr' part
}

An Author node represents a single bibtex author.

func (*Author) End

func (x *Author) End() gotok.Pos

func (*Author) IsEmpty

func (x *Author) IsEmpty() bool

func (*Author) IsOthers

func (x *Author) IsOthers() bool

IsOthers returns true if this author was created from the "and others" suffix in from authors field.

func (*Author) Kind

func (x *Author) Kind() NodeKind

func (*Author) Pos

func (x *Author) Pos() gotok.Pos

type Authors

type Authors []*Author

An Authors node represents a list of authors, typically in the author or editor fields of a bibtex declaration.

func (Authors) End

func (x Authors) End() gotok.Pos

func (Authors) Kind

func (x Authors) Kind() NodeKind

func (Authors) Pos

func (x Authors) Pos() gotok.Pos

type BadDecl

type BadDecl struct {
	From, To gotok.Pos // position range of bad declaration
}

A BadDecl node is a placeholder for declarations containing syntax errors for which no correct declaration nodes can be created.

func (*BadDecl) End

func (e *BadDecl) End() gotok.Pos

func (*BadDecl) Kind

func (e *BadDecl) Kind() NodeKind

func (*BadDecl) Pos

func (e *BadDecl) Pos() gotok.Pos

type BadExpr

type BadExpr struct {
	From, To gotok.Pos
}

A BadExpr node is a placeholder for expressions containing syntax errors for which no correct expression nodes can be created.

func (*BadExpr) End

func (x *BadExpr) End() gotok.Pos

func (*BadExpr) Kind

func (x *BadExpr) Kind() NodeKind

func (*BadExpr) Pos

func (x *BadExpr) Pos() gotok.Pos

type BadStmt

type BadStmt struct {
	From, To gotok.Pos // position range of bad statement
}

A BadStmt node is a placeholder for statements containing syntax errors for which no correct statement nodes can be created.

func (*BadStmt) End

func (x *BadStmt) End() gotok.Pos

func (*BadStmt) Kind

func (x *BadStmt) Kind() NodeKind

func (*BadStmt) Pos

func (x *BadStmt) Pos() gotok.Pos

type BibDecl

type BibDecl struct {
	Type      string           // type of entry, e.g. "article"
	Doc       *TexCommentGroup // associated documentation; or nil
	Entry     gotok.Pos        // position of the start token, e.g. "@article"
	Key       *Ident           // the first key in the declaration
	ExtraKeys []*Ident         // any other keys in the declaration, usually nil
	Tags      []*TagStmt       // all tags in the declaration
	RBrace    gotok.Pos        // position of the closing right brace token: "}".
}

An BibDecl node represents a bibtex entry, like:

@article { author = "bar" }

func (*BibDecl) End

func (e *BibDecl) End() gotok.Pos

func (*BibDecl) Kind

func (e *BibDecl) Kind() NodeKind

func (*BibDecl) Pos

func (e *BibDecl) Pos() gotok.Pos

type ConcatExpr

type ConcatExpr struct {
	X     Expr
	OpPos gotok.Pos
	Y     Expr
}

A ConcatExpr node represents a bibtex concatenation like:

"foo" # "bar"

func (*ConcatExpr) End

func (x *ConcatExpr) End() gotok.Pos

func (*ConcatExpr) Kind

func (x *ConcatExpr) Kind() NodeKind

func (*ConcatExpr) Pos

func (x *ConcatExpr) Pos() gotok.Pos

type Decl

type Decl interface {
	Node
	// contains filtered or unexported methods
}

All declaration nodes implement the Decl interface, like the @article, @STRING, @COMMENT, and @PREAMBLE entries.

type Expr

type Expr interface {
	Node
	// contains filtered or unexported methods
}

All expression nodes implement the Expr interface.

type File

type File struct {
	Name       string
	Doc        *TexCommentGroup   // associated documentation; or nil
	Entries    []Decl             // top-level entries; or nil
	Scope      *Scope             // package scope (this file only)
	Unresolved []*Ident           // unresolved identifiers in this file
	Comments   []*TexCommentGroup // list of all comments in the source file
}

A File node represents a bibtex source file.

The Comments list contains all comments in the source file in order of appearance, including the comments that are pointed to from other nodes via Doc and Comment fields.

For correct printing of source code containing comments (using packages go/format and go/printer), special care must be taken to update comments when a File's syntax tree is modified: For printing, comments are interspersed between tokens based on their position. If syntax tree nodes are removed or moved, relevant comments in their vicinity must also be removed (from the File.Comments list) or moved accordingly (by updating their positions). A CommentMap may be used to facilitate some of these operations.

Whether and how a comment is associated with a node depends on the interpretation of the syntax tree by the manipulating program: Except for Doc and Comment comments directly associated with nodes, the remaining comments are "free-floating".

func (*File) End

func (f *File) End() gotok.Pos

func (*File) Kind

func (f *File) Kind() NodeKind

func (*File) Pos

func (f *File) Pos() gotok.Pos

type Ident

type Ident struct {
	NamePos gotok.Pos // identifier position
	Name    string    // identifier name
	Obj     *Object   // denoted object; or nil
}

An Ident node represents an identifier like a bibtex citation key or tag key.

func (*Ident) End

func (x *Ident) End() gotok.Pos

func (*Ident) Kind

func (x *Ident) Kind() NodeKind

func (*Ident) Pos

func (x *Ident) Pos() gotok.Pos

type Node

type Node interface {
	Pos() gotok.Pos
	End() gotok.Pos
	Kind() NodeKind
}

type NodeKind

type NodeKind int
const (
	KindTexComment NodeKind = iota
	KindTexCommentGroup
	KindBadExpr
	KindIdent
	KindNumber
	KindAuthors
	KindAuthor
	KindUnparsedText
	KindParsedText
	KindText
	KindTextComma
	KindTextEscaped
	KindTextHyphen
	KindTextMath
	KindTextNBSP
	KindTextSpace
	KindTextMacro
	KindConcatExpr
	KindBadStmt
	KindTagStmt
	KindBadDecl
	KindAbbrevDecl
	KindBibDecl
	KindPreambleDecl
	KindFile
	KindPackage
)

func (NodeKind) String

func (k NodeKind) String() string

type Number

type Number struct {
	ValuePos gotok.Pos
	Value    string
}

A Number node represents an unquoted number, like:

year = 2004

func (*Number) End

func (x *Number) End() gotok.Pos

func (*Number) Kind

func (x *Number) Kind() NodeKind

func (*Number) Pos

func (x *Number) Pos() gotok.Pos

type ObjKind

type ObjKind int

ObjKind describes what an object represents.

const (
	Bad    ObjKind = iota // for error handling
	Entry                 // bibtex entry, usually from a crossref
	Abbrev                // abbreviation
)

The list of possible Object kinds.

func (ObjKind) String

func (kind ObjKind) String() string

type Object

type Object struct {
	Kind ObjKind
	Name string      // declared name
	Decl interface{} // corresponding BibDecl, AbbrevDecl, or nil
}

An Object describes a named language entity such as a bibtex entry, or string abbreviation.

The Data fields contains object-specific data:

Kind    Data type         Data value
Pkg     *Scope            package scope
Con     int               iota for the respective declaration

func NewObj

func NewObj(kind ObjKind, name string) *Object

NewObj creates a new object of a given kind and name.

func (*Object) Pos

func (obj *Object) Pos() token.Pos

Pos computes the source position of the declaration of an object name. The result may be an invalid position if it cannot be computed (obj.Decl may be nil or not correct).

type Package

type Package struct {
	Scope   *Scope             // package scope across all files
	Objects map[string]*Object // map of package id -> package object
	Files   map[string]*File   // Bibtex source files by filename
}

A Package node represents a set of source files collectively representing a single, unified bibliography.

func (*Package) End

func (p *Package) End() gotok.Pos

func (*Package) Kind

func (p *Package) Kind() NodeKind

func (*Package) Pos

func (p *Package) Pos() gotok.Pos

type ParsedText

type ParsedText struct {
	Opener gotok.Pos // opening delimiter
	Depth  int       // the brace depth
	Delim  TextDelimiter
	Values []Expr    // Text, ParsedText, or any of the Text* types
	Closer gotok.Pos // closing delimiter
}

A ParsedText node represents a parsed bibtex string.

func (*ParsedText) End

func (x *ParsedText) End() gotok.Pos

func (*ParsedText) Kind

func (x *ParsedText) Kind() NodeKind

func (*ParsedText) Pos

func (x *ParsedText) Pos() gotok.Pos

type PreambleDecl

type PreambleDecl struct {
	Doc    *TexCommentGroup // associated documentation; or nil
	Entry  gotok.Pos        // position of the "@PREAMBLE" token
	Text   Expr             // The content of the preamble node
	RBrace gotok.Pos        // position of the closing right brace token: "}"
}

An PreambleDecl node represents a bibtex preamble, like:

@PREAMBLE { "foo" }

func (*PreambleDecl) End

func (e *PreambleDecl) End() gotok.Pos

func (*PreambleDecl) Kind

func (e *PreambleDecl) Kind() NodeKind

func (*PreambleDecl) Pos

func (e *PreambleDecl) Pos() gotok.Pos

type Scope

type Scope struct {
	Outer   *Scope
	Objects map[string]*Object
}

A Scope maintains the set of named language entities declared in the scope and a link to the immediately surrounding (outer) scope.

func NewScope

func NewScope(outer *Scope) *Scope

NewScope creates a new scope nested in the outer scope.

func (*Scope) Insert

func (s *Scope) Insert(obj *Object) (alt *Object)

Insert attempts to insert a named object obj into the scope s. If the scope already contains an object alt with the same name, Insert leaves the scope unchanged and returns alt. Otherwise it inserts obj and returns nil.

func (*Scope) Lookup

func (s *Scope) Lookup(name string) *Object

Lookup returns the object with the given name if it is found in scope s, otherwise it returns nil. Outer scopes are ignored.

func (*Scope) String

func (s *Scope) String() string

Debugging support

type SimplifyTagTransformer

type SimplifyTagTransformer struct{}

func (SimplifyTagTransformer) Transform

func (s SimplifyTagTransformer) Transform(node Node) error

type Stmt

type Stmt interface {
	Node
	// contains filtered or unexported methods
}

All statement nodes implement the Stmt interface, like bibtex entry tags.

type TagStmt

type TagStmt struct {
	Doc     *TexCommentGroup // associated documentation; or nil
	NamePos gotok.Pos        // identifier position
	Name    string           // identifier name, normalized with lowercase
	RawName string           // identifier name as it appeared in source
	Value   Expr             // denoted expression
}

An TagStmt node represents a tag in an BibDecl or AbbrevDecl, i.e. author = "foo".

func (*TagStmt) End

func (x *TagStmt) End() gotok.Pos

func (*TagStmt) Kind

func (x *TagStmt) Kind() NodeKind

func (*TagStmt) Pos

func (x *TagStmt) Pos() gotok.Pos

type TexComment

type TexComment struct {
	Start gotok.Pos // position of the '%' starting the comment
	Text  string    // comment text excluding '\n'
}

A TexComment node represents a single %-style comment.

func (*TexComment) End

func (c *TexComment) End() gotok.Pos

func (*TexComment) Kind

func (c *TexComment) Kind() NodeKind

func (*TexComment) Pos

func (c *TexComment) Pos() gotok.Pos

type TexCommentGroup

type TexCommentGroup struct {
	List []*TexComment // len(List) > 0
}

A TexCommentGroup represents a sequence of comments with no other tokens and no empty lines between.

func (*TexCommentGroup) End

func (g *TexCommentGroup) End() gotok.Pos

func (*TexCommentGroup) Kind

func (g *TexCommentGroup) Kind() NodeKind

func (*TexCommentGroup) Pos

func (g *TexCommentGroup) Pos() gotok.Pos

type Text

type Text struct {
	ValuePos gotok.Pos // literal position
	Value    string
}

A Text node is a string of simple text.

func SimplifyParsedText

func SimplifyParsedText(txt *ParsedText) *Text

func (*Text) End

func (x *Text) End() gotok.Pos

func (*Text) Kind

func (x *Text) Kind() NodeKind

func (*Text) Pos

func (x *Text) Pos() gotok.Pos

type TextComma

type TextComma struct {
	ValuePos gotok.Pos // literal position
}

A TextComma node is a string of exactly 1 comma. Useful because a comma has semantic meaning for parsing authors as a separator for names.

func (*TextComma) End

func (x *TextComma) End() gotok.Pos

func (*TextComma) Kind

func (x *TextComma) Kind() NodeKind

func (*TextComma) Pos

func (x *TextComma) Pos() gotok.Pos

type TextDelimiter

type TextDelimiter int
const (
	QuoteDelimiter TextDelimiter = iota
	BraceDelimiter
)

func (TextDelimiter) String

func (t TextDelimiter) String() string

type TextEscaped

type TextEscaped struct {
	ValuePos gotok.Pos // literal position
	Value    string    // the escaped char without the backslash
}

A TextEscaped node is a string of exactly 1 escaped character. The only escapable characters are:

'\\', '$', '&', '%', '{', '}'

In all other cases, a backslash is interpreted as the start of a TeX macro.

func (*TextEscaped) End

func (x *TextEscaped) End() gotok.Pos

func (*TextEscaped) Kind

func (x *TextEscaped) Kind() NodeKind

func (*TextEscaped) Pos

func (x *TextEscaped) Pos() gotok.Pos

type TextHyphen

type TextHyphen struct {
	ValuePos gotok.Pos // literal position
}

A TextHyphen node is a string of exactly 1 hyphen (-). Hyphens are important in some cases of parsing author names so keep it as a separate node.

func (*TextHyphen) End

func (x *TextHyphen) End() gotok.Pos

func (*TextHyphen) Kind

func (x *TextHyphen) Kind() NodeKind

func (*TextHyphen) Pos

func (x *TextHyphen) Pos() gotok.Pos

type TextMacro

type TextMacro struct {
	Cmd    gotok.Pos // command position
	Name   string    // command name without backslash, i.e. 'url'
	Values []Expr    // parameters: Text, ParsedText, or TextMacro
	RBrace gotok.Pos // position of the closing }, if any
}

A TextMacro node represents a piece of ParsedText that's a latex macro invocation.

func (*TextMacro) End

func (x *TextMacro) End() gotok.Pos

func (*TextMacro) Kind

func (x *TextMacro) Kind() NodeKind

func (*TextMacro) Pos

func (x *TextMacro) Pos() gotok.Pos

type TextMath

type TextMath struct {
	ValuePos gotok.Pos // literal position
	Value    string    // the text in-between the $...$, not including the $'s.
}

A TextMath node is the string delimited by dollar signs, representing Math in TeX.

func (*TextMath) End

func (x *TextMath) End() gotok.Pos

func (*TextMath) Kind

func (x *TextMath) Kind() NodeKind

func (*TextMath) Pos

func (x *TextMath) Pos() gotok.Pos

type TextNBSP

type TextNBSP struct {
	ValuePos gotok.Pos // literal position
}

A TextNBSP node is a single non-breaking space, represented in TeX as '~'.

func (*TextNBSP) End

func (x *TextNBSP) End() gotok.Pos

func (*TextNBSP) Kind

func (x *TextNBSP) Kind() NodeKind

func (*TextNBSP) Pos

func (x *TextNBSP) Pos() gotok.Pos

type TextSpace

type TextSpace struct {
	ValuePos gotok.Pos // literal position
	Value    string
}

A TextSpace node is any consecutive whitespace '\n', '\r', '\t', ' ' in a bibtex string.

func (*TextSpace) End

func (x *TextSpace) End() gotok.Pos

func (*TextSpace) Kind

func (x *TextSpace) Kind() NodeKind

func (*TextSpace) Pos

func (x *TextSpace) Pos() gotok.Pos

type Transformer

type Transformer interface {
	Transform(Node) error
}

type UnparsedText

type UnparsedText struct {
	ValuePos gotok.Pos   // literal position
	Type     token.Token // token.String or token.BraceString
	Value    string      // excluding delimiters
}

An UnparsedText is a bibtex string as it appears in source. Only appears when Mode.ParseStrings == 0 is passed to ParseFile.

func (*UnparsedText) End

func (x *UnparsedText) End() gotok.Pos

func (*UnparsedText) Kind

func (x *UnparsedText) Kind() NodeKind

func (*UnparsedText) Pos

func (x *UnparsedText) Pos() gotok.Pos

type WalkStatus

type WalkStatus int
const (
	WalkStop         WalkStatus = iota // stop walking immediately
	WalkContinue                       // continue walking
	WalkSkipChildren                   // continue walking
)

type Walker

type Walker = func(n Node, isEntering bool) (WalkStatus, error)

Walker is a function that's called on every node recursively as part of the traversal for Walk.

Jump to

Keyboard shortcuts

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