commit cb1fe01ec77c81565b285d0ceb1625ab0799723f Author: Waozi Date: Tue Sep 29 19:25:14 2026 -0300 Tokenize Ziran, C, and Make source as a standalone package The tokenizer moves out of Kryon so any Ziran program can use it without a UI dependency. It reports token kinds and byte ranges and colors them into caller-owned SyntaxColorSpan values, which have the same fields as Kryon's TextAreaColorSpan. diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..da9bd07 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,27 @@ +name: CI + +on: + push: + branches: [master] + pull_request: + workflow_dispatch: + +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v7 + with: + path: syntax + + - name: Build the ziran launcher + run: | + git clone --depth 1 https://github.com/ziranlang/ziran.git ziran + make -C ziran + echo "$PWD/ziran/build/bin" >> "$GITHUB_PATH" + + - name: Test + working-directory: syntax + run: | + ziran fetch --locked + make check diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d086e16 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +build/ +ziran.local.toml diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..9eeb030 --- /dev/null +++ b/LICENSE @@ -0,0 +1,28 @@ +BSD 3-Clause License + +Copyright (c) 2026, Waozi + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its contributors + may be used to endorse or promote products derived from this software without + specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..55fad18 --- /dev/null +++ b/Makefile @@ -0,0 +1,24 @@ +.DEFAULT_GOAL := check + +ZIRAN ?= ziran +CC ?= cc +BUILD := build + +.PHONY: check test clean + +# Checks the package, then runs the tests as C and as a portable bundle. +check: test + +test: + $(ZIRAN) check --project + $(ZIRAN) build --project --target=c --entry syntax_test:main \ + -o $(BUILD)/test-c tests/syntax_test.zi + $(CC) -std=c99 -pedantic-errors -I$$($(ZIRAN) pkg path ziran)/include \ + -I$(BUILD)/test-c $(BUILD)/test-c/*.c -o $(BUILD)/syntax-test + $(BUILD)/syntax-test + $(ZIRAN) bundle --project --entry syntax_test:main \ + -o $(BUILD)/syntax-test.zib tests/syntax_test.zi + test "$$($(ZIRAN) run $(BUILD)/syntax-test.zib)" = 0 + +clean: + rm -rf $(BUILD) diff --git a/README.md b/README.md new file mode 100644 index 0000000..f2b7fff --- /dev/null +++ b/README.md @@ -0,0 +1,42 @@ +# Syntax + +Syntax is a small [Ziran](https://github.com/ziranlang/ziran) package that +tokenizes Ziran, C, and Make source. It reports each token's byte range and +kind (keyword, string, number, path, comment, whitespace, or plain text) and +can color the tokens for a light or dark background. It allocates nothing: +callers pass the text and own the output spans. + +## Use it + +```sh +ziran add https://github.com/ziranlang/syntax.git +``` + +```zi +#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`. +Put a Ziran checkout next to this one, or copy `ziran.local.toml` from the +example below, to test against a local compiler: + +```toml +[overrides] +ziran = "../ziran" +``` diff --git a/src/syntax.zi b/src/syntax.zi new file mode 100644 index 0000000..5a2e650 --- /dev/null +++ b/src/syntax.zi @@ -0,0 +1,342 @@ +// 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. +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 +} +using SyntaxMode; +using SyntaxTokenKind; + + +SyntaxToken :: struct { + length: s32 + kind: SyntaxTokenKind + block_comment: bool +} + +#program_export +SyntaxIdentStart :: (byte: u8) -> bool { + return (byte >= cast(u8)65 && byte <= cast(u8)90) || + (byte >= cast(u8)97 && byte <= cast(u8)122) || + byte == cast(u8)95 +} + +#program_export +SyntaxIdent :: (mode: SyntaxMode, byte: u8) -> bool { + return SyntaxIdentStart(byte) || + (byte >= cast(u8)48 && byte <= cast(u8)57) || + (mode == cast(SyntaxMode)SyntaxMake && byte == cast(u8)45) +} + +#program_export +SyntaxWordInList :: (value: string, start: s32, + length: s32, words: string) -> bool { + if start < 0 || length <= 0 || + start + length > (cast(s32)value.count) { return false } + offset: s32 = 0 + while offset < (cast(s32)words.count) { + if words[offset] != cast(u8)124 { + offset += 1 + continue + } + begin: s32 = offset + 1 + end: s32 = begin + while end < (cast(s32)words.count) && words[end] != cast(u8)124 { + end += 1 + } + if end - begin == length { + equal: bool = true + index: s32 = 0 + while index < length { + if value[start + index] != words[begin + index] { + equal = false + break + } + index += 1 + } + if equal { return true } + } + offset = end + } + return false +} + +#program_export +SyntaxKeywordFor :: (mode: SyntaxMode, + value: string, start: s32, + length: s32) -> bool { + if mode == cast(SyntaxMode)SyntaxZiran { + return SyntaxWordInList(value, start, length, + "|import|program_export|foreign|system_library|scope_file|scope_module|scope_export|char|type|struct|enum|enum_flags|specified|case|complete|if|ifx|then|else|while|return|cast|size_of|type_of|true|false|null|bool|string|int|s8|s16|s32|s64|u8|u16|u32|u64|float32|float64|") + } + if mode == cast(SyntaxMode)SyntaxC { + return SyntaxWordInList(value, start, length, + "|auto|break|case|char|const|continue|default|do|double|else|enum|extern|float|for|goto|if|inline|int|long|register|restrict|return|short|signed|sizeof|static|struct|switch|typedef|union|unsigned|void|volatile|while|_Bool|_Complex|_Atomic|_Thread_local|") + } + if mode == cast(SyntaxMode)SyntaxMake { + return SyntaxWordInList(value, start, length, + "|define|else|endef|endif|export|ifdef|ifeq|ifndef|ifneq|include|override|private|undefine|unexport|vpath|") + } + return false +} + +#program_export +SyntaxDarkBackground :: (background: u32) -> bool { + red: u32 = (background >> cast(u32)24) & cast(u32)255 + green: u32 = (background >> cast(u32)16) & cast(u32)255 + blue: u32 = (background >> cast(u32)8) & cast(u32)255 + return red * cast(u32)3 + green * cast(u32)6 + blue < + cast(u32)1280 +} + +#program_export +SyntaxColorFor :: (kind: SyntaxTokenKind, + dark: bool, base: u32) -> u32 { + if kind == cast(SyntaxTokenKind)SyntaxKeyword { + if dark { return cast(u32)0x8ea0f0ff } + return cast(u32)0x2448acff + } + if kind == cast(SyntaxTokenKind)SyntaxString { + if dark { return cast(u32)0x6ec8b0ff } + return cast(u32)0x227e52ff + } + if kind == cast(SyntaxTokenKind)SyntaxNumber { + if dark { return cast(u32)0xd59152ff } + return cast(u32)0x96487eff + } + if kind == cast(SyntaxTokenKind)SyntaxPath { + if dark { return cast(u32)0xcdaa6eff } + return cast(u32)0x8a6020ff + } + if kind == cast(SyntaxTokenKind)SyntaxComment { + if dark { return cast(u32)0x8a92a0ff } + return cast(u32)0x68707cff + } + return base +} + +#program_export +SyntaxTokenAt :: (value: string, start: s32, + end: s32, mode: SyntaxMode, first_token: bool, + block_comment: bool) -> SyntaxToken { + token: SyntaxToken + token.kind = cast(SyntaxTokenKind)SyntaxPlain + token.block_comment = block_comment + if start < 0 || end > (cast(s32)value.count) || + start >= end { return token } + byte: u8 = value[start] + index: s32 = start + if block_comment { + token.kind = cast(SyntaxTokenKind)SyntaxComment + while index < end { + if value[index] == cast(u8)42 && index + 1 < end && + value[index + 1] == cast(u8)47 { + index += 2 + token.block_comment = false + break + } + index += 1 + } + token.length = index - start + return token + } + if byte == cast(u8)32 || byte == cast(u8)9 { + token.kind = cast(SyntaxTokenKind)SyntaxWhitespace + while index < end && + (value[index] == cast(u8)32 || + value[index] == cast(u8)9) { index += 1 } + token.length = index - start + return token + } + if mode == cast(SyntaxMode)SyntaxC && byte == cast(u8)47 && + start + 1 < end && value[start + 1] == cast(u8)42 { + token.kind = cast(SyntaxTokenKind)SyntaxComment + token.block_comment = true + index = start + 2 + while index < end { + if value[index] == cast(u8)42 && index + 1 < end && + value[index + 1] == cast(u8)47 { + index += 2 + token.block_comment = false + break + } + index += 1 + } + token.length = index - start + return token + } + if mode == cast(SyntaxMode)SyntaxC && byte == cast(u8)47 && + start + 1 < end && value[start + 1] == cast(u8)47 { + token.length = end - start + token.kind = cast(SyntaxTokenKind)SyntaxComment + return token + } + if (mode == cast(SyntaxMode)SyntaxZiran || + mode == cast(SyntaxMode)SyntaxMake) && byte == cast(u8)35 { + if mode == cast(SyntaxMode)SyntaxZiran && first_token && + start + 1 < end && + SyntaxIdentStart(value[start + 1]) { + index = start + 2 + while index < end && + SyntaxIdent(mode, value[index]) { index += 1 } + if SyntaxKeywordFor(mode, value, + start + 1, index - start - 1) { + token.length = index - start + token.kind = cast(SyntaxTokenKind)SyntaxKeyword + return token + } + } + token.length = end - start + token.kind = cast(SyntaxTokenKind)SyntaxComment + return token + } + if byte == cast(u8)34 || + (mode == cast(SyntaxMode)SyntaxC && byte == cast(u8)39) { + index = start + 1 + while index < end { + if value[index] == cast(u8)92 && index + 1 < end { + index += 2 + continue + } + if value[index] == byte { + index += 1 + break + } + index += 1 + } + token.length = index - start + token.kind = cast(SyntaxTokenKind)SyntaxString + return token + } + if mode == cast(SyntaxMode)SyntaxMake && byte == cast(u8)36 { + index = start + 1 + if index < end && + (value[index] == cast(u8)40 || + value[index] == cast(u8)123) { + close: u8 = cast(u8)41 + if value[index] == cast(u8)123 { close = cast(u8)125 } + index += 1 + while index < end && value[index] != close { + index += 1 + } + if index < end { index += 1 } + } else { + if index < end { index += 1 } + } + token.length = index - start + token.kind = cast(SyntaxTokenKind)SyntaxPath + return token + } + digit: bool = byte >= cast(u8)48 && byte <= cast(u8)57 + negative: bool = byte == cast(u8)45 && + start + 1 < end && value[start + 1] >= cast(u8)48 && + value[start + 1] <= cast(u8)57 + if digit || negative { + index = start + 1 + while index < end { + next: u8 = value[index] + if (next >= cast(u8)48 && next <= cast(u8)57) || + next == cast(u8)46 || next == cast(u8)95 || + next == cast(u8)120 || next == cast(u8)88 || + (next >= cast(u8)65 && next <= cast(u8)70) || + (next >= cast(u8)97 && next <= cast(u8)102) { + index += 1 + continue + } + break + } + token.length = index - start + token.kind = cast(SyntaxTokenKind)SyntaxNumber + return token + } + if SyntaxIdentStart(byte) { + index = start + 1 + while index < end && SyntaxIdent(mode, value[index]) { + index += 1 + } + token.length = index - start + if SyntaxKeywordFor(mode, value, + start, token.length) { + token.kind = cast(SyntaxTokenKind)SyntaxKeyword + } + return token + } + if byte >= cast(u8)128 { + index = start + 1 + while index < end && + (value[index] & cast(u8)192) == cast(u8)128 { + index += 1 + } + token.length = index - start + return token + } + token.length = 1 + return token +} + +SyntaxSpansResult :: struct { + count: s32 + complete: bool +} + +// A colored byte range, [start, end), of the scanned text. Its layout +// matches the color spans of text widgets such as Kryon's TextArea. +SyntaxColorSpan :: struct { + start: s32 + end: s32 + color: u32 +} + +// 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. +#program_export +SyntaxColorSpans :: (value: string, mode: SyntaxMode, + background: u32, foreground: u32, + spans: []SyntaxColorSpan) -> SyntaxSpansResult { + result: SyntaxSpansResult + result.complete = true + if mode == cast(SyntaxMode)SyntaxNone { return result } + dark: bool = SyntaxDarkBackground(background) + line_start: s32 = 0 + block_comment: bool = false + while line_start < cast(s32)value.count { + line_end: s32 = line_start + while line_end < cast(s32)value.count && + value[line_end] != cast(u8)10 { line_end += 1 } + cursor: s32 = line_start + first_token: bool = true + while cursor < line_end { + token: SyntaxToken = SyntaxTokenAt(value, cursor, + line_end, mode, first_token, block_comment) + if token.length <= 0 || + result.count >= cast(s32)spans.count { + result.complete = false + return result + } + spans[result.count] = SyntaxColorSpan.{ + cursor, cursor + token.length, + SyntaxColorFor(token.kind, dark, foreground)} + result.count += 1 + if token.kind != cast(SyntaxTokenKind)SyntaxWhitespace { + first_token = false + } + block_comment = token.block_comment + cursor += token.length + } + line_start = line_end + 1 + } + return result +} diff --git a/tests/syntax_test.zi b/tests/syntax_test.zi new file mode 100644 index 0000000..509ee03 --- /dev/null +++ b/tests/syntax_test.zi @@ -0,0 +1,71 @@ +#import "syntax" + +using SyntaxMode; +using SyntaxTokenKind; + +// Token kinds for each language, block comments across lines, and caller +// owned span output that reports when it runs out of room. +#program_export +main :: () -> s32 { + directive: SyntaxToken = SyntaxTokenAt("#import \"app\"", 0, 13, + cast(SyntaxMode)SyntaxZiran, true, false) + comment: SyntaxToken = SyntaxTokenAt("# note", 0, 6, + cast(SyntaxMode)SyntaxZiran, true, false) + keyword: SyntaxToken = SyntaxTokenAt("return 42", 0, 9, + cast(SyntaxMode)SyntaxZiran, true, false) + number: SyntaxToken = SyntaxTokenAt("return 42", 7, 9, + cast(SyntaxMode)SyntaxZiran, false, false) + open: SyntaxToken = SyntaxTokenAt("/* open", 0, 7, + cast(SyntaxMode)SyntaxC, true, false) + close: SyntaxToken = SyntaxTokenAt("close */x", 0, 9, + cast(SyntaxMode)SyntaxC, false, true) + make: SyntaxToken = SyntaxTokenAt("$(CC) file", 0, 10, + cast(SyntaxMode)SyntaxMake, true, false) + if directive.length != 7 || + directive.kind != cast(SyntaxTokenKind)SyntaxKeyword || + comment.kind != cast(SyntaxTokenKind)SyntaxComment || + keyword.kind != cast(SyntaxTokenKind)SyntaxKeyword || + number.kind != cast(SyntaxTokenKind)SyntaxNumber || + !open.block_comment || close.block_comment || + close.length != 8 || + make.kind != cast(SyntaxTokenKind)SyntaxPath || + make.length != 5 { return 1 } + if !SyntaxDarkBackground(cast(u32)0x101820ff) || + SyntaxDarkBackground(cast(u32)0xffffffff) || + SyntaxColorFor(cast(SyntaxTokenKind)SyntaxKeyword, + false, cast(u32)0) != cast(u32)0x2448acff { return 2 } + short: [1]SyntaxColorSpan + limited: SyntaxSpansResult = SyntaxColorSpans("return 42", + cast(SyntaxMode)SyntaxZiran, cast(u32)0xffffffff, + cast(u32)0x172b4dff, short[:]) + if limited.complete || limited.count != 1 || + short[0].start != 0 || short[0].end != 6 || + short[0].color != cast(u32)0x2448acff { return 3 } + full: [8]SyntaxColorSpan + painted: SyntaxSpansResult = SyntaxColorSpans("return 42", + cast(SyntaxMode)SyntaxZiran, cast(u32)0xffffffff, + cast(u32)0x172b4dff, full[:]) + if !painted.complete || painted.count != 3 || + full[2].start != 7 || full[2].end != 9 || + full[2].color != cast(u32)0x96487eff { return 4 } + // A directive and a statement on two lines; the newline gets no span. + lines: [8]SyntaxColorSpan + two: SyntaxSpansResult = SyntaxColorSpans("#import \"app\"\nreturn 42", + cast(SyntaxMode)SyntaxZiran, cast(u32)0xffffffff, + cast(u32)0x172b4dff, lines[:]) + if !two.complete || two.count != 6 || + lines[0].end != 7 || lines[0].color != cast(u32)0x2448acff || + lines[1].start != 7 || lines[1].end != 8 || + lines[1].color != cast(u32)0x172b4dff || + lines[2].start != 8 || lines[2].end != 13 || + lines[2].color != cast(u32)0x227e52ff || + lines[3].start != 14 || lines[3].end != 20 || + lines[4].start != 20 || lines[4].end != 21 || + lines[5].start != 21 || lines[5].end != 23 || + lines[5].color != cast(u32)0x96487eff { return 6 } + none: SyntaxSpansResult = SyntaxColorSpans("return 42", + cast(SyntaxMode)SyntaxNone, cast(u32)0xffffffff, + cast(u32)0x172b4dff, full[:]) + if !none.complete || none.count != 0 { return 5 } + return 0 +} diff --git a/ziran.lock b/ziran.lock new file mode 100644 index 0000000..51d99e4 --- /dev/null +++ b/ziran.lock @@ -0,0 +1,15 @@ +{ + "format": 1, + "packages": [ + + ], + "root": { + "dependencies": {}, + "name": "Syntax" + }, + "toolchain": { + "commit": "363b2ec5461aa1ec457176b2acf77fc9b92f2175", + "ref": "master", + "url": "https://github.com/ziranlang/ziran.git" + } +} diff --git a/ziran.toml b/ziran.toml new file mode 100644 index 0000000..20f5f77 --- /dev/null +++ b/ziran.toml @@ -0,0 +1,10 @@ +[package] +name = "Syntax" +module_roots = ["src", "tests"] + +[toolchain] +git = "https://github.com/ziranlang/ziran.git" +ref = "master" + +[exports] +Syntax = "src/syntax.zi"