textmate

package
v1.4.2 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2026 License: MIT Imports: 11 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package textmate loads TextMate grammars and tokenizes lines (SPEC §8.3).

Index

Constants

View Source
const DefaultMaxLineBytes = 20000

DefaultMaxLineBytes is the length from which a line is not tokenized (SPEC §8.3).

Variables

This section is empty.

Functions

This section is empty.

Types

type Grammar

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

Grammar is a grammar ready to tokenize lines. It is safe for concurrent use: the rules are compiled when the first line is tokenized and are not changed after that.

func NewGrammar

func NewGrammar(scopeName string, registry Registry, opts GrammarOptions) (*Grammar, error)

NewGrammar makes the grammar with the scope name from the registry. The error has the tag GrammarNotFound when the registry has no such grammar.

func (*Grammar) Compiling

func (g *Grammar) Compiling() bool

Compiling tells whether a goroutine is making the rules now: whoever tokenizes a line waits for it. A thread that must not wait (the one that paints) leaves the line to a goroutine that may.

func (*Grammar) Problems

func (g *Grammar) Problems() []error

Problems are the errors of the patterns and rules of the grammar that could not be used, found so far: a rule that has such a pattern never matches. They are found as the rules are compiled, the first time a line is tokenized, and as patterns are first used.

func (*Grammar) Ready

func (g *Grammar) Ready() bool

Ready tells whether the rules are made (by Warm or by the first line that was tokenized).

func (*Grammar) ScopeName

func (g *Grammar) ScopeName() string

ScopeName is the scope name of the grammar.

func (*Grammar) TokenizeLine

func (g *Grammar) TokenizeLine(line string, prev *State, timeLimit time.Duration) LineResult

TokenizeLine tokenizes a line, without its line break, from the state the previous line ended in (nil for the first line). timeLimit, when it is not zero, is how long the tokenizer may take.

func (*Grammar) Warm

func (g *Grammar) Warm()

Warm makes the rules of the grammar now, which the tokenizing of the first line would do: it lets a goroutine that has the time (the window is not shown yet, or a file is about to open) take the 2 to 60 ms the rules of a language take from the thread that paints. It is safe to call from several goroutines and again; a call while another makes the rules waits for it.

type GrammarOptions

type GrammarOptions struct {
	// MaxLineBytes is the length, in bytes, from which a line is one token and not tokenized; the
	// default is DefaultMaxLineBytes.
	MaxLineBytes int
	// ScanTimeout is the longest a search of one scanner may take; the default is
	// regex.ScanTimeout (SPEC §8.2). Tests that run slowly (under the race detector) lengthen it.
	ScanTimeout time.Duration
	// Cache compiles the patterns; a cache of the grammar is made when there is none.
	Cache *regex.Cache
	// InitialLanguage is the language id of the tokens that no embedded language scope covers.
	InitialLanguage int
	// EmbeddedLanguages maps scope names, such as source.css, to the id of the language of the
	// tokens in them (a scope matches its own name and the names that start with it and a dot).
	EmbeddedLanguages map[string]int
	// TokenTypes maps scope selectors to the standard type of the tokens they select, over what
	// the scope names say.
	TokenTypes map[string]TokenType
}

GrammarOptions are the options of NewGrammar.

type LineResult

type LineResult struct {
	// Tokens cover the line from its start to its end, in order. An empty line has one empty
	// token.
	Tokens []Token
	// State is where the tokenizer is at the end of the line; the next line starts from it.
	State *State
	// StoppedEarly is true when the line was not tokenized to its end, because the time limit was
	// reached or a pattern ran past its limit: the rest of the line is one token with the scopes
	// where the tokenizer stopped.
	StoppedEarly bool
}

LineResult is the result of tokenizing a line.

type RawGrammar

type RawGrammar struct {
	// ScopeName is the scope of the grammar, such as source.go.
	ScopeName string
	// Name is the display name.
	Name string
	// FileTypes are the extensions the grammar is for.
	FileTypes []string
	// FirstLineMatch is a regular expression for the first line of the files of the grammar.
	FirstLineMatch string
	// InjectionSelector, for a grammar that is an injection, says where it is injected.
	InjectionSelector string
	// Patterns are the rules of the top level.
	Patterns []*RawRule
	// Repository holds the named rules that patterns include with #name.
	Repository map[string]*RawRule
	// Injections are the rules the grammar injects into the scopes that match their selectors,
	// in the order of the file.
	Injections []RawInjection
}

RawGrammar is a TextMate grammar as it is written in a file (SPEC §8.3). It is read-only once it is loaded: grammars are shared by every document that uses them.

func ParseGrammar

func ParseGrammar(data []byte) (*RawGrammar, error)

ParseGrammar reads a grammar from JSON. The error has the tag GrammarInvalid.

type RawInjection

type RawInjection struct {
	// Selector says where the rule applies, such as "L:source.js -comment".
	Selector string
	// Rule is the rule.
	Rule *RawRule
}

RawInjection is a rule of the injections of a grammar and the selector for it.

type RawRule

type RawRule struct {
	// Include names another rule: $self, $base, #name, scope.name or scope.name#name.
	Include string
	// Name is the scope of what the rule matches.
	Name string
	// ContentName is the scope of what is between begin and end.
	ContentName string
	// Match is the regular expression of a rule that matches a single span.
	Match string
	// HasBegin tells whether the rule has a begin (even an empty one), which makes it a rule with
	// a beginning and an end.
	HasBegin bool
	// Begin starts a rule that spans text.
	Begin string
	// End finishes a rule that began with Begin; it may refer to the captures of Begin as \1.
	End string
	// While keeps a rule that began with Begin going for as long as it matches at the start of
	// each line.
	While string
	// Captures name the scopes of the groups of Match (or of Begin and End when the specific
	// ones are not there), by group number.
	Captures map[string]*RawRule
	// BeginCaptures are the captures of Begin.
	BeginCaptures map[string]*RawRule
	// EndCaptures are the captures of End.
	EndCaptures map[string]*RawRule
	// WhileCaptures are the captures of While.
	WhileCaptures map[string]*RawRule
	// Patterns are the rules that apply between Begin and End.
	Patterns []*RawRule
	// HasPatterns tells whether the file has patterns (an empty list as well).
	HasPatterns bool
	// Repository holds the rules the patterns of an include-only rule can include with #name.
	Repository map[string]*RawRule
	// ApplyEndPatternLast makes End the last pattern tried and not the first.
	ApplyEndPatternLast bool
}

RawRule is a rule of a grammar as it is written in a file.

type Registry

type Registry interface {
	// Grammar returns the grammar with the scope name, or nil when there is none.
	Grammar(scopeName string) *RawGrammar
	// Injections returns the scope names of the grammars that are injected into the grammar with
	// the scope name (the grammars of extensions that name it in injectTo), or nil.
	Injections(scopeName string) []string
}

Registry finds the grammars that other grammars include by their scope names.

type ScopeStack

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

ScopeStack is the scopes a token is in, outermost first, as a list that shares its tail with the scopes it was made from. It is immutable. The nil stack has no scopes.

func (*ScopeStack) Equal

func (s *ScopeStack) Equal(o *ScopeStack) bool

Equal tells whether two stacks have the same scopes.

func (*ScopeStack) Len

func (s *ScopeStack) Len() int

Len is the number of scopes.

func (*ScopeStack) Names

func (s *ScopeStack) Names() []string

Names are the names of the scopes, outermost first.

func (*ScopeStack) Parent

func (s *ScopeStack) Parent() *ScopeStack

Parent is the stack without its innermost scope, or nil for a stack of one scope or none.

func (*ScopeStack) Top

func (s *ScopeStack) Top() string

Top is the innermost scope, or "" when there is none.

type State

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

State is where the tokenizer is at the end of a line: the rules that are open, outermost first, as a list that shares its tail with the states it was made from. It is immutable and safe to keep for as long as needed; the state to start a document from is nil.

Two states are Equal when tokenizing the next line gives the same tokens from both, which is how re-tokenization knows it can stop.

func (*State) Depth

func (s *State) Depth() int

Depth is the number of rules that are open.

func (*State) Equal

func (s *State) Equal(o *State) bool

Equal tells whether two states are the same: the same rules are open, with the same end patterns, in the same scopes.

func (*State) Scopes

func (s *State) Scopes() *ScopeStack

Scopes are the scopes at the end of the line, which the text that follows is in.

type Token

type Token struct {
	// Start and End are the byte offsets of the token in the line.
	Start, End int
	// Scopes are the scopes of the token, outermost first.
	Scopes *ScopeStack
	// Type is the standard type of the token: comment, string, regular expression or other.
	Type TokenType
	// Language is the id of the language of the token, from the embedded languages of the
	// grammar; the initial language of the grammar for a token outside them.
	Language int
}

Token is a span of a line and the scopes it is in. Offsets are bytes of the line.

type TokenType

type TokenType int

TokenType is the standard type of a token, which brackets, auto-closing and comments use to tell code from comments and strings.

const (
	// TokenOther is a token that is none of the others.
	TokenOther TokenType = 0
	// TokenComment is a token in a comment.
	TokenComment TokenType = 1
	// TokenString is a token in a string.
	TokenString TokenType = 2
	// TokenRegEx is a token in a regular expression.
	TokenRegEx TokenType = 3
)

The standard token types, with the values of VS Code's StandardTokenType.

Jump to

Keyboard shortcuts

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