Documentation
¶
Overview ¶
Package lexer runs @pierre/highlights, the syntax highlighter of the Pierre Computer Company (https://github.com/pierrecomputer/pierre, Apache-2.0), in wazero. The highlighter is a WebAssembly module, written in WebAssembly Text, with a lexer for each of 76 languages. It reads UTF-8 text and gives each line a list of tokens, without a syntax tree, and it keeps the state of its lexers at the end of each line, so an edit relexes only the lines it changes, and the lines after them until the state matches what it was.
highlights.wasm is the module of the npm package's release named by Version, unchanged. The update command (go generate) downloads it and writes the tables of language and token names that match it; see README.md.
The package compiles the module once per process, and keeps the compiled code in a per-user cache directory. An Instance holds one module instance. It is not safe for concurrent use.
Index ¶
- Constants
- func ByteOffset(b []byte, u int) int
- func LanguageID(name string) (int, bool)
- func LanguageNames() []string
- func Sanitize(text []byte) []byte
- func TokenNames() []string
- func UTF16Len(b []byte) int
- type Instance
- func (in *Instance) Close() error
- func (in *Instance) Edit(sl, sc, el, ec int, text []byte) error
- func (in *Instance) Lex(lang int, text []byte, dst []Token) ([]Token, error)
- func (in *Instance) LineCount() (int, error)
- func (in *Instance) LineText(i int) ([]byte, error)
- func (in *Instance) LineTokens(i int, dst []LineToken) ([]LineToken, error)
- func (in *Instance) MemorySize() uint32
- func (in *Instance) Open(lang int, text []byte) error
- func (in *Instance) Pending() (bool, int, error)
- func (in *Instance) Run(end int) error
- func (in *Instance) RunLines(n int) error
- type LineToken
- type Token
Constants ¶
const Version = "0.1.3"
Version is the release of @pierre/highlights that highlights.wasm comes from.
Variables ¶
This section is empty.
Functions ¶
func ByteOffset ¶
ByteOffset returns the byte offset in b, which must be valid UTF-8, of the UTF-16 offset u, or len(b) when b is shorter.
func LanguageID ¶
LanguageID returns the module's ID for a language name or alias, such as "go", "golang" or "TS", in any case, and false for a name it does not know.
func LanguageNames ¶
func LanguageNames() []string
LanguageNames returns every name and alias that LanguageID knows, in lower case, in no particular order.
func Sanitize ¶
Sanitize returns a copy of text that the lexers can read: each byte that is not part of valid UTF-8 becomes '?', and each carriage return a space, so that only line feeds end lines. The copy has the same length, so offsets in it are offsets in text. A carriage return before a line feed, as in Windows text, becomes a space at the end of its line, which does not change the tokens.
func TokenNames ¶
func TokenNames() []string
TokenNames returns the names of the token IDs, indexed by ID, such as "keyword.control" or "string.escape". ID 0 is "none", which marks text that has no token. The caller must not change the slice.
Types ¶
type Instance ¶
type Instance struct {
// contains filtered or unexported fields
}
Instance is one instance of the module. It can lex whole texts with Lex, and it holds one live document, opened with Open, that it relexes as it is edited. It is not safe for concurrent use. After a call returns an error, the instance may be in an unknown state and should be closed.
func New ¶
New compiles the module if this is its first use in the process, and returns a new instance of it.
func (*Instance) Edit ¶
Edit replaces the text of the live document from column sc of line sl to column ec of line el with text. Columns count UTF-16 code units from the start of the line, and a column at the end of a line is before its line feed. The changed lines wait to be tokenized; see Run. text must come from Sanitize.
func (*Instance) Lex ¶
Lex lexes the whole of text, in the language with the given ID, and appends its tokens to dst. Text without a token (ID 0) and line ends are left out. text must come from Sanitize.
func (*Instance) LineCount ¶
LineCount returns the number of lines of the live document. Text that ends in a line feed has an empty last line.
func (*Instance) LineText ¶
LineText returns the bytes of line i of the live document, without its line feed. The slice is a view of the module's memory, valid until the next call.
func (*Instance) LineTokens ¶
LineTokens appends the tokens of line i of the live document to dst. A line that waits to be tokenized has the tokens it had before, and a new one none.
func (*Instance) MemorySize ¶
MemorySize returns the size of the instance's linear memory in bytes.
func (*Instance) Open ¶
Open makes text, in the language with the given ID, the instance's live document. Its lines wait to be tokenized; see Run. text must come from Sanitize.
func (*Instance) Pending ¶
Pending reports whether lines of the live document wait to be tokenized, and the first of them.
func (*Instance) Run ¶
Run tokenizes the waiting lines of the live document that come before line end, in order, or all of them when end is zero. Lines after a change are tokenized again until the lexer's state at the end of one matches the state it had before, so a change that opens a comment retokenizes up to where the comment closes.
Directories
¶
| Path | Synopsis |
|---|---|
|
Command update downloads a release of @pierre/highlights from the npm registry and writes its WebAssembly module, license and tables into the lexer package:
|
Command update downloads a release of @pierre/highlights from the npm registry and writes its WebAssembly module, license and tables into the lexer package: |