lexer

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2026 License: Apache-2.0, Apache-2.0 Imports: 13 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.

README

@pierre/highlights

highlights.wasm is the WebAssembly module of @pierre/highlights, the syntax highlighter of the Pierre Computer Company (source). It is copied unchanged from the npm package's dist/highlights.wasm. The release is Version in tables.go, and its SHA-256 is wasmSHA256 there; a test checks that the two match.

@pierre/highlights is Copyright 2025 Pierre Computer Company and is licensed under the Apache License, Version 2.0. The license text, from the package, is in LICENSE-APACHE. Only the module is used: the package's themes and its JavaScript host are not part of jamb. The Go code of this package, which runs the module, is jamb's.

Updating

Change the version in the go:generate line of lexer.go, then, in this directory:

go generate .

The update command downloads the release's tarball from the npm registry, checks it against the registry's integrity hash, and writes highlights.wasm, LICENSE-APACHE and tables.go. tables.go holds the version, the module's SHA-256, the language names and aliases (from dist/languages.js) and the token names (from dist/token-types.js), which must match the module. Then run the tests of this package and of codeedit/highlight, and read the release's changes for the module's interface: the memory layout and exports that instance.go uses come from the package's src/memory.wat, src/live.wat and lib/live.ts.

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

View Source
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

func ByteOffset(b []byte, u int) int

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

func LanguageID(name string) (int, bool)

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

func Sanitize(text []byte) []byte

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.

func UTF16Len

func UTF16Len(b []byte) int

UTF16Len returns the number of UTF-16 code units of b, which must be valid UTF-8.

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

func New() (*Instance, error)

New compiles the module if this is its first use in the process, and returns a new instance of it.

func (*Instance) Close

func (in *Instance) Close() error

Close releases the instance and its memory.

func (*Instance) Edit

func (in *Instance) Edit(sl, sc, el, ec int, text []byte) error

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

func (in *Instance) Lex(lang int, text []byte, dst []Token) ([]Token, error)

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

func (in *Instance) LineCount() (int, error)

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

func (in *Instance) LineText(i int) ([]byte, error)

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

func (in *Instance) LineTokens(i int, dst []LineToken) ([]LineToken, error)

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

func (in *Instance) MemorySize() uint32

MemorySize returns the size of the instance's linear memory in bytes.

func (*Instance) Open

func (in *Instance) Open(lang int, text []byte) error

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

func (in *Instance) Pending() (bool, int, error)

Pending reports whether lines of the live document wait to be tokenized, and the first of them.

func (*Instance) Run

func (in *Instance) Run(end int) error

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.

func (*Instance) RunLines

func (in *Instance) RunLines(n int) error

RunLines tokenizes at most n of the waiting lines of the live document.

type LineToken

type LineToken struct {
	End int
	ID  int
}

LineToken ends a run of a line's text with one token ID. End is the run's end in UTF-16 code units from the start of the line; the run starts where the previous one ended, or at the start of the line.

type Token

type Token struct {
	Start, End int
	ID         int
}

Token is a run of text with one token ID. Start and End are byte offsets.

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:

Jump to

Keyboard shortcuts

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