monarch 实验性
实验性
本包属于实验层:任何 0.x 版本都可能有不兼容变化(docs/release.md「API 稳定性」)。
import "github.com/tangoui/tango/ui/editor/monarch"本页由
site/tools/apigen从源码注释生成(按 darwin 构建标签选文件),请勿手改。
Package monarch interprets Monaco Monarch grammars (JSON, as exported by ui/editor/langs) and tokenizes text line by line.
A Registry maps language ids to grammars and compiles a Lexer on first use; Lexer.TokenizeLine takes the previous line's end State and returns the line's tokens and its end State. Semantics follow Monaco's monarchLexer, checked against Monaco's own tokenization tests.
Contracts:
Degraded lines. Grammars from plugins are untrusted. Pathological backtracking cannot stall a line: a single regexp2 match times out after regex.MatchTimeout, and a line degrades on the first timeout or once its tokenization exceeds LineBudget. A degraded line keeps the tokens produced before that point, covers the rest with one "" token and returns its input State, exactly like the long-line rule (MaxLineLength). TokenizeLineInfo reports this as LineInfo.Degraded (TokenizeLine drops it); Stats counts degraded lines. Because the returned State may differ from the true end state, a caller must not treat "end state unchanged" on a degraded line as a reason to stop re-tokenizing the lines below.
Runtime errors. A rule that would make Monaco's tokenizer throw, a no-progress loop, or a recovered panic yields one "" token for the whole line and the input State (counted in Stats).
Registry.Add. After Add(id, ...) replaces a grammar, every State previously produced for that language is invalid for the new Lexer — including States of other languages that embed it (they hold the old embedded Lexer). Callers must re-tokenize affected documents from Lexer.Initial(). The old Lexer itself keeps working on its own States.
Compilation. A grammar that cannot compile (undefined states or attributes, @attr expansion beyond 64 KiB, a panic in the compiler) makes Registry.Lexer return an error; the result is cached. Compilation happens outside the Registry lock.
Experimental: this package's API may change in any 0.x release (see docs/release.md, "API 稳定性").
常量
const LineBudget = 50 * time.MillisecondLineBudget 是一行分词的时间预算。超过预算(或一行内第一次回溯正则超时,见 regex.MatchTimeout) 时该行降级:已产出的 token 保留,其余部分作为一个 "" token,行尾状态等于输入状态(同长行规则), LineInfo.Degraded=true。一般匹配的墙钟超预算还须累计至少 minLineWork 的工作量,避免 调度/GC 停顿使普通短行降级;回溯匹配另累计经慢匹配重试确认的耗时估计,不受字节门槛 豁免。maxLineWork 是不依赖时钟的硬上限。每 budgetCheckEvery 次匹配迭代或每次回溯 引擎匹配后检查。第一次进入嵌入语言时编译其语法的耗时不计入。 -race 构建中预算放大 10 倍(race.go)。
const MaxLineLength = 20000MaxLineLength 是参与分词的最大行长(UTF-16 代码单元),与 Monaco 默认 editor.maxTokenizationLineLength 相同。更长的行只对前 MaxLineLength 个单元分词, 其余部分作为一个 "" 类型 token,行尾状态等于输入状态(长行不影响后续行)。
函数
func Stats
func Stats() (runtimeErrors, recoveredPanics, degradedLines int64)Stats 返回全局计数:TokenizeLine 因规则执行出错而输出安全结果的次数(含 recover 的 panic), 其中由 recover 兜住的 panic 次数,以及因超时/超预算而降级的行数(见 LineInfo)。 内置语法上三者都应为 0(降级只出现在病态输入上)。
类型
Lexer
type Lexer struct {
// contains filtered or unexported fields
}Lexer 是一种语言编译后的 Monarch 分词器。可被多个 goroutine 并发使用。
func (*Lexer) ID
func (l *Lexer) ID() stringID 返回语言 ID。
func (*Lexer) Initial
func (l *Lexer) Initial() StateInitial 返回行首的初始状态。
func (*Lexer) TokenizeLine
func (l *Lexer) TokenizeLine(line string, st State, dst []Token) ([]Token, State)TokenizeLine 对一行(不含换行符)分词,把 token 追加到 dst,返回追加后的切片与行尾状态。 st 为上一行的行尾状态(第一行用 Initial();0 或未知状态视同 Initial())。 规则执行出错时(Monaco 会抛异常的情形)与 Monaco 编辑器一样,整行输出一个 "" token,状态不变。 等同 TokenizeLineInfo 但丢弃 LineInfo;需要知道行是否降级的调用方用 TokenizeLineInfo。
func (*Lexer) TokenizeLineInfo
func (l *Lexer) TokenizeLineInfo(line string, st State, dst []Token) ([]Token, State, LineInfo)TokenizeLineInfo 同 TokenizeLine,另返回 LineInfo。病态正则(回溯超时,或本行分词超过 LineBudget)使该行降级:降级点之前的 token 保留,其后是一个 "" token,返回输入状态 st, Degraded=true。见 LineInfo。
func (*Lexer) Warnings
func (l *Lexer) Warnings() []stringWarnings 返回编译时无法编译、因而永不匹配的正则等提示(Monaco 中这些正则可用)。
LineInfo
type LineInfo struct {
// Degraded 表示该行因病态正则(回溯超时或超过 LineBudget)没有完整分词:降级点之后是一个
// "" token,返回的状态是输入状态,因而可能与真实的行尾状态不同。调用方不应把"行尾状态未变"
// 当作可以停止向下重新分词的依据(例如可在空闲时重试,或继续向下分词若干行)。
Degraded bool
}LineInfo 是 TokenizeLineInfo 关于一行的附加信息。
Registry
type Registry struct {
// contains filtered or unexported fields
}Registry 管理语言 ID 到 Monarch 语法的映射,按需编译 Lexer。可并发使用。
func NewRegistry
func NewRegistry() *RegistryNewRegistry 返回装入全部内置语言(langs.Builtin)的注册表;语法在第一次 Lexer 时编译。
func (*Registry) Add
func (r *Registry) Add(id string, grammar json.RawMessage) errorAdd 注册(或以同 ID 覆盖)一种语言的 Monarch 语法(JSON,正则写成 {"$regex","flags"})。 Add 只校验 JSON 语法;编译是惰性的,语法错误(未定义的状态、属性等)由之后的 Lexer(id) 返回。 覆盖后 Lexer(id) 返回新编译的 Lexer:之前为该语言产生的所有 State(包括嵌入了该语言的其他语言的 State,它们持有旧的嵌入 Lexer)对新 Lexer 都无效,调用方须从 Initial() 重新分词。 旧 Lexer 在它自己的 State 上仍可继续使用。
func (*Registry) Lexer
func (r *Registry) Lexer(id string) (*Lexer, error)Lexer 返回语言的分词器;语言未注册或没有 Monarch 语法(如 plaintext)时返回错误。 第一次调用时编译语法,结果(含错误)被缓存。编译在 r.mu 之外进行:慢编译只让请求同一语言的 调用等待,不阻塞其他语言。编译期的 panic(不可信插件语法)转为错误。
State
type State uint32State 是驻留后的行尾状态 ID;0 保留为"无"(TokenizeLine 视同 Initial)。 同一 Lexer 上,相同的(状态栈, 嵌入语言, 嵌入语言状态)总是得到同一个 State。 State 只在产生它的 Lexer 上有意义。
驻留表只增不减:状态名可由输入内容决定(如 `@heredoc.$1` 里的 heredoc 标记), 不同的状态名会让表持续增长。若这对长期运行的进程重要,可按文档重建 Registry。
Token
type Token struct {
Start int // UTF-16 列,对应 Monaco 的 startIndex
ByteStart int // 行内 UTF-8 字节偏移,供渲染使用
Type string // 带 tokenPostfix 的类型,如 "keyword.go";"" 表示无类型
}Token 是一行中一个 token 的起点与类型。