Documentation
¶
Index ¶
- func Highlight(t *Tree, text []uint16, sets *TagSets, span func(from, to int, set *TagSet))
- func StyleTags(specs []StyleSpec) (map[string][]Rule, error)
- type ChangedRange
- type Config
- type ContextTracker
- type DialectSpec
- type Externals
- type Input
- type InputStream
- type LocalGroupSpec
- type Mode
- type Nest
- type NestedParse
- type Node
- type NodeType
- type Parser
- func (p *Parser) Configure(c Config) (*Parser, error)
- func (p *Parser) Parse(text string) *Tree
- func (p *Parser) ParseUnits(input []uint16, ranges []Range, fragments []TreeFragment) *Tree
- func (p *Parser) Types() []NodeType
- func (p *Parser) WithNest(nest Nest) *Parser
- func (p *Parser) WithStyleTags(byName map[string][]Rule) *Parser
- type Range
- type Rule
- type RuleSpec
- type Spec
- type Specializer
- type SpecializerSpec
- type Stack
- type StyleSpec
- type TagSet
- type TagSets
- type Tokenizer
- type TokenizerSpec
- type TopRule
- type Tree
- type TreeFragment
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type ChangedRange ¶
type ChangedRange struct {
FromA, ToA, FromB, ToB int
}
ChangedRange is one edit between two versions of a document: [FromA, ToA) in the old one became [FromB, ToB) in the new one.
type Config ¶
Config changes what a Configure'd parser parses: Top names one of the grammar's top rules ("" keeps the current one), Dialect is a space-separated list of dialects to enable.
type ContextTracker ¶
type ContextTracker struct {
Start any
Shift func(context any, term int, s *Stack, in *InputStream) any
Reduce func(context any, term int, s *Stack, in *InputStream) any
Reuse func(context any, node *Tree, s *Stack, in *InputStream) any
Hash func(context any) int
NonStrict bool
}
ContextTracker keeps a value along a parse that external tokenizers can read, such as the indentation in Python's grammar, the way @lezer/lr's ContextTracker does. Values are compared with ==, so they must be comparable; like @lezer/lr's they should be immutable, replaced rather than changed. Shift, Reduce and Reuse (for a node an incremental parse takes from an earlier tree) default to keeping the value. Unless NonStrict, Hash tells values apart for the token cache and the tree.
type DialectSpec ¶
DialectSpec is a dialect and the offset in the state data of the terms it alone enables.
type Externals ¶
type Externals struct {
Tokenizers map[string]Tokenizer
Specializers map[string]Specializer
Context *ContextTracker
}
Externals are the Go ports of what a grammar takes from its own JavaScript module: external tokenizers and specializers by export name, and its @context tracker.
type Input ¶
type Input struct {
// contains filtered or unexported fields
}
Input is the text a parse reads, for a Nest to look at.
type InputStream ¶
type InputStream struct {
// Next is the code unit at Pos, -1 at the end of the input.
Next int
// Pos is the stream's position in the input.
Pos int
// contains filtered or unexported fields
}
InputStream is how a tokenizer reads the input: as UTF-16 code units, the way JavaScript strings index, across the parse's ranges as if they were one text. The whole text is in memory, so @lezer/lr's chunking has no counterpart here.
func (*InputStream) AcceptToken ¶
func (in *InputStream) AcceptToken(term, endOffset int)
AcceptToken accepts a token of type term ending endOffset units from Pos. An end before the token's start is a tokenizer bug, and panics.
func (*InputStream) AcceptTokenTo ¶
func (in *InputStream) AcceptTokenTo(term, endPos int)
AcceptTokenTo accepts a token of type term ending at endPos.
func (*InputStream) Advance ¶
func (in *InputStream) Advance(n int) int
Advance moves the stream n code units forward and returns the new Next.
func (*InputStream) Peek ¶
func (in *InputStream) Peek(offset int) int
Peek is the code unit offset units from Pos, -1 outside the input.
type LocalGroupSpec ¶
LocalGroupSpec is a LocalTokenGroup's constructor arguments.
type Mode ¶
type Mode int
Mode is how a styleTags rule applies below its node, with the values @lezer/highlight gives them.
const ( // ModeOpaque, a path ending in "!", styles the whole node and stops // matching inside it. ModeOpaque Mode = iota // ModeInherit, a path ending in "/...", adds the tags to every node // inside as well. ModeInherit // ModeNormal styles the node, its children matching their own rules. ModeNormal )
type Nest ¶
type Nest func(n *Node, in Input) *NestedParse
Nest is what @lezer/common's parseMixed takes: for a node of the outer parse, the parse of its content, or nil to leave it as it is.
type NestedParse ¶
type NestedParse struct {
Parser *Parser
// Overlay, unless nil, are the parts of the input the parser reads, as
// absolute positions inside the node, and the node keeps its own tree
// around them. A nil Overlay parses the whole node, and its tree
// replaces the node's.
Overlay []Range
}
NestedParse says how another parser reads a node's content.
type Node ¶
type Node struct {
// contains filtered or unexported fields
}
Node is a node of a tree in place, as @lezer/common's SyntaxNode: a Tree, or a node inside a TreeBuffer. Its navigation skips anonymous nodes and enters mounted trees that replace their node.
func (*Node) FirstChild ¶
FirstChild is the node's first child, nil for none.
func (*Node) NextSibling ¶
NextSibling is the node after this one in its parent, nil for none.
type NodeType ¶
type NodeType struct {
Name string
ID int
Props map[string]string
Top bool
Error bool
// Skipped marks nodes the grammar's @skip produces, like comments.
Skipped bool
// Rules are the styleTags rules for the type, deepest context first.
Rules []Rule
}
NodeType is a kind of node in a syntax tree.
type Parser ¶
type Parser struct {
// contains filtered or unexported fields
}
Parser holds a grammar's parse tables and parses text with them, as @lezer/lr's LRParser does. It is immutable and safe to share between goroutines.
func New ¶
New builds the parser for spec, taking the externals it names from ext. A missing external, a table of another format or a tag Parse rejects is an error.
func (*Parser) Configure ¶
Configure is a copy of p with c applied. An unknown top rule is an error.
func (*Parser) Parse ¶
Parse parses text whole and returns its tree. Positions in the tree are UTF-16 code units, as in JavaScript.
func (*Parser) ParseUnits ¶
func (p *Parser) ParseUnits(input []uint16, ranges []Range, fragments []TreeFragment) *Tree
ParseUnits parses the ranges of input, nil ranges meaning all of it, reusing what it can of fragments, the pieces of earlier trees ApplyChanges left: an incremental parse, as Parser.parse with fragments. A reused node is shared with the tree it came from, and a nested parse may store its tree on it, so no earlier tree may be read while this runs.
func (*Parser) WithNest ¶
WithNest is a copy of p whose parses run nest over every node of the tree they made and mount the nested parses it asks for, as LRParser.configure with a wrap of parseMixed(nest) does. A nest p had is replaced.
func (*Parser) WithStyleTags ¶ added in v0.5.0
WithStyleTags is p with byName's rules added to its node types' own, as configuring a parser with a styleTags prop does: a node type takes the rules of its name, or else of the first of its groups that has some. Trees p parsed are not reused by the new parser's incremental parses, since they hold p's node types.
type Rule ¶
Rule is one styleTags rule of a node type: its tags, the names of the direct parents the node must have for it to apply ("" for any), and the texts it is limited to (nil for any).
type RuleSpec ¶
RuleSpec is one styleTags rule as lezer-generator's caller wrote it: tags as Tag.toString prints them.
type Spec ¶
type Spec struct {
Version int
States string
StateData string
Goto string
NodeNames string
MaxTerm int
RepeatNodeCount int
NodeProps map[string]map[int]string
SkippedNodes []int
TokenData string
Tokenizers []TokenizerSpec
TopRules []TopRule
Dialects []DialectSpec
DynamicPrec map[int]int
Specialized []SpecializerSpec
TokenPrec int
// Context names the grammar's @context tracker, "" for none.
Context string
// Styles are each node type's styleTags rules, already combined and
// sorted the way @lezer/highlight's ruleNodeProp leaves them.
Styles map[int][]RuleSpec
}
Spec is a grammar's parse tables, as lezer-generator writes them for LRParser.deserialize, plus the styleTags rules its language package attaches. internal/lezer/gen writes one per grammar.
type Specializer ¶
Specializer maps a token's text to the term it specializes to, or -1 to leave the token as it is. Whether that term replaces the token or is tried alongside it is the grammar's @external specialize or extend.
type SpecializerSpec ¶
SpecializerSpec is a @specialize or @extend on Term: a table from token text to the encoded result (term<<1, |1 for an extension), or an external specializer by its export name.
type Stack ¶
type Stack struct {
// contains filtered or unexported fields
}
Stack is one parse stack: a parse runs several when the grammar is ambiguous or while it recovers from an error. Tokenizers and context trackers read the parse state through it.
func (*Stack) CanShift ¶
CanShift reports whether term could be shifted on the stack, after the reductions it would take: for an external tokenizer that should emit a token only where it applies.
func (*Stack) DialectEnabled ¶
DialectEnabled reports whether the dialect with the given index (its place in the grammar's dialect list) is enabled.
type StyleSpec ¶ added in v0.5.0
StyleSpec is one entry of a styleTags object: space-separated paths ("Block/...", "Annotation!", "*/ArgumentList/Identifier") and the tags the nodes they name take. Names, when set, limit the entry to nodes whose text is one of them, which styleTags has no way to say.
type TagSet ¶
TagSet is the tags that apply to a piece of text: a node's own and the ones inherited from its ancestors, in that order, as the class string highlightTree builds lists them. TagSets from one TagSets are interned, so two are equal exactly when they hold the same list.
type TagSets ¶
type TagSets struct {
// contains filtered or unexported fields
}
TagSets interns TagSets. It is not safe for concurrent use.
type Tokenizer ¶
type Tokenizer struct {
Token func(in *InputStream, s *Stack)
Contextual, Fallback, Extend bool
}
Tokenizer reads a token at the stream's position and accepts it with AcceptToken, as @lezer/lr's ExternalTokenizer does. Contextual tokens are re-read in every parse state instead of cached by position; a Fallback tokenizer runs even after an earlier one produced a token; an Extend tokenizer's token is tried alongside the next tokenizer's.
type TokenizerSpec ¶
type TokenizerSpec struct {
Group int
Local *LocalGroupSpec
External string
}
TokenizerSpec is one entry of a grammar's tokenizers: a token group of its TokenData, a local token group with its own data, or an external tokenizer by its export name.
type TopRule ¶
TopRule is one of a grammar's top rules, in the order the grammar lists them: the first is the default.
type Tree ¶
type Tree struct {
// contains filtered or unexported fields
}
Tree is a syntax tree node with its children, as @lezer/common builds it: small subtrees are packed into TreeBuffers, and long repetitions are balanced under anonymous nodes. The layout is kept exactly, because a tree cursor's view of a node's context depends on it (see cursor.go). Positions are UTF-16 code units, relative to the tree's start.
func (*Tree) Layout ¶
Layout describes how the tree is stored, which a node an incremental parse reuses keeps from the tree it came from, as gen/incremental.mjs writes it for @lezer/common's trees: every Tree as its type id and length, its contextHash (#) and lookAhead (~), a mounted tree in braces after its overlay ranges, and its children in brackets, each after its position, a TreeBuffer as B<length>/<buffer size>.
type TreeFragment ¶
TreeFragment is a piece of an earlier tree an incremental parse may reuse: the unchanged range [From, To) of the new document, and the Offset to add to go from a new position to the tree's. OpenStart and OpenEnd say whether the fragment's ends are the ends of a change rather than of the parse, where nodes may not be safe to reuse.
func AddTree ¶
func AddTree(tree *Tree, fragments []TreeFragment, partial bool) []TreeFragment
AddTree is the fragments of a fresh tree, with the fragments that reach past its end kept, as TreeFragment.addTree does. A partial tree's fragment is open at its end.
func ApplyChanges ¶
func ApplyChanges(fragments []TreeFragment, changes []ChangedRange) []TreeFragment
ApplyChanges drops or cuts the fragments the changes touch and moves the ones after them, as TreeFragment.applyChanges does with its default minGap. changes are in order and do not overlap.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package css is @lezer/css's CSS grammar for internal/lezer: its parse tables and styleTags rules, generated from the npm package, and the Go ports of its external tokenizers.
|
Package css is @lezer/css's CSS grammar for internal/lezer: its parse tables and styleTags rules, generated from the npm package, and the Go ports of its external tokenizers. |
|
Package golang is @lezer/go's Go grammar for internal/lezer: its parse tables and styleTags rules, generated from the npm package, and the ports of its external tokenizer and context tracker (tokens.go).
|
Package golang is @lezer/go's Go grammar for internal/lezer: its parse tables and styleTags rules, generated from the npm package, and the ports of its external tokenizer and context tracker (tokens.go). |
|
Package html is @lezer/html's HTML grammar for internal/lezer: its parse tables and styleTags rules, generated from the npm package, the Go ports of its external tokenizers and context tracker, and the nesting of JavaScript and CSS that @codemirror/lang-html sets up by default.
|
Package html is @lezer/html's HTML grammar for internal/lezer: its parse tables and styleTags rules, generated from the npm package, the Go ports of its external tokenizers and context tracker, and the nesting of JavaScript and CSS that @codemirror/lang-html sets up by default. |
|
Package java is @lezer/java's Java grammar for internal/lezer: its parse tables and styleTags rules, generated from the npm package.
|
Package java is @lezer/java's Java grammar for internal/lezer: its parse tables and styleTags rules, generated from the npm package. |
|
Package javascript is @lezer/javascript's JavaScript grammar for internal/lezer: its parse tables and styleTags rules, generated from the npm package, and ports of its external tokenizers and context tracker.
|
Package javascript is @lezer/javascript's JavaScript grammar for internal/lezer: its parse tables and styleTags rules, generated from the npm package, and ports of its external tokenizers and context tracker. |
|
Package lezertest checks a port against what the real Lezer gives: the goldens internal/lezer/gen/golden.mjs writes, one per fixture.
|
Package lezertest checks a port against what the real Lezer gives: the goldens internal/lezer/gen/golden.mjs writes, one per fixture. |
|
Package python is @lezer/python's Python grammar for internal/lezer: its parse tables and styleTags rules, generated from the npm package, and the ports of its external tokenizers and indentation tracker (tokens.go).
|
Package python is @lezer/python's Python grammar for internal/lezer: its parse tables and styleTags rules, generated from the npm package, and the ports of its external tokenizers and indentation tracker (tokens.go). |
|
Package rust is @lezer/rust's Rust grammar for internal/lezer: its parse tables and styleTags rules, generated from the npm package, and the ports of its external tokenizers (tokens.go).
|
Package rust is @lezer/rust's Rust grammar for internal/lezer: its parse tables and styleTags rules, generated from the npm package, and the ports of its external tokenizers (tokens.go). |