Add the dependency

In your project’s ziran.toml; pin ref to a commit for reproducible builds.

[dependencies.syntax]
git = "https://github.com/ziranlang/syntax.git"
ref = "master"

Overview

Use it

ziran add https://github.com/ziranlang/syntax.git
#import "syntax/Syntax"

using SyntaxMode;

spans: [256]SyntaxColorSpan
result: SyntaxSpansResult = SyntaxColorSpans(source, cast(SyntaxMode)SyntaxZiran,
    background, foreground, spans[:])
// spans[0:result.count] cover the text; result.complete is false when the
// output was too short.

SyntaxTokenAt scans one token at a time for editors that keep their own line state. A SyntaxColorSpan has the same start, end, and color fields as a Kryon TextAreaColorSpan, so a Kryon app copies them into its TextArea props.

Develop

make check checks the package and runs its tests as C and as a portable bundle. It uses the ziran launcher and the compiler pinned in ziran.lock. To test against the local compiler at ziranlang/ziran, put this in an ignored ziran.local.toml:

[overrides]
ziran = "../../ziran"

syntax/src/syntax.zi

Syntax

Tokenizes Ziran, C, and Make source into byte spans with a token kind. Everything is allocation-free: callers pass the text and own the output.

Import with #import "syntax/Syntax".

SyntaxMode :: enum { SyntaxNone = 0; SyntaxZiran = 1; SyntaxC = 2; SyntaxMake = 3; }
SyntaxTokenKind :: enum { SyntaxPlain = 0; SyntaxWhitespace = 1; SyntaxKeyword = 2; SyntaxString = 3; SyntaxNumber = 4; SyntaxPath = 5; SyntaxComment = 6; }
SyntaxToken :: struct { length: s32; kind: SyntaxTokenKind; block_comment: bool; }
SyntaxIdentStart :: (byte: u8) -> bool
SyntaxIdent :: (mode: SyntaxMode, byte: u8) -> bool
SyntaxWordInList :: (value: string, start: s32, length: s32, words: string) -> bool
SyntaxKeywordFor :: (mode: SyntaxMode, value: string, start: s32, length: s32) -> bool
SyntaxDarkBackground :: (background: u32) -> bool
SyntaxColorFor :: (kind: SyntaxTokenKind, dark: bool, base: u32) -> u32
SyntaxTokenAt :: (value: string, start: s32, end: s32, mode: SyntaxMode, first_token: bool, block_comment: bool) -> SyntaxToken
SyntaxSpansResult :: struct { count: s32; complete: bool; }
SyntaxColorSpan :: struct { start: s32; end: s32; color: u32; }

A colored byte range, [start, end), of the scanned text. Its layout matches the color spans of text widgets such as Kryon's TextArea.

SyntaxColorSpans :: (value: string, mode: SyntaxMode, background: u32, foreground: u32, spans: []SyntaxColorSpan) -> SyntaxSpansResult

Colors every token of value for a background and foreground in RGBA. The caller owns the output; when it is too short, complete is false and count spans are filled.

Source ↗