Files
css/src/parser/lexer.zi
T
waozi bc247a75de Parse Kryon styles as a CSS subset
Work in progress: the CSS parser, sheet, compiled packs, formatter,
migrator and DOM export replace the KSS reader. Tests and downstream
conversion follow.
2026-11-07 13:15:02 -03:00

411 lines
12 KiB
Ziran

// Part of the css_parser module: source bytes, names, white space, comments,
// numbers and diagnostics. Every advance keeps line and column current so
// each diagnostic and rule origin points at its exact source position.
CssAtEnd :: (p: *CssParser) -> bool {
return p.cursor.pos >= cast(s32)p.cursor.source.count
}
CssPeek :: (p: *CssParser, ahead: s32) -> u8 {
index: s32 = p.cursor.pos + ahead
if index < 0 || index >= cast(s32)p.cursor.source.count { return 0 }
return p.cursor.source[index]
}
CssAdvance :: (p: *CssParser, count: s32) {
for step: cast(s32)0..count - 1 {
if p.cursor.pos >= cast(s32)p.cursor.source.count { return }
if p.cursor.source[p.cursor.pos] == 10 {
p.cursor.line += 1
p.cursor.column = 1
} else {
p.cursor.column += 1
}
p.cursor.pos += 1
}
}
CssIsSpace :: (byte: u8) -> bool {
return byte == #char " " || byte == 9 || byte == 10 || byte == 12 || byte == 13
}
CssIsDigit :: (byte: u8) -> bool {
return byte >= #char "0" && byte <= #char "9"
}
CssIsHexDigit :: (byte: u8) -> bool {
return CssIsDigit(byte) || (byte >= #char "a" && byte <= #char "f") ||
(byte >= #char "A" && byte <= #char "F")
}
CssIsAlpha :: (byte: u8) -> bool {
return (byte >= #char "a" && byte <= #char "z") || (byte >= #char "A" && byte <= #char "Z")
}
// CSS identifiers: letters, digits, '-', '_' and any non-ASCII byte. Escapes
// are outside the subset and reported where they appear.
CssNameStart :: (byte: u8) -> bool {
return CssIsAlpha(byte) || byte == #char "_" || byte >= 128
}
CssNameChar :: (byte: u8) -> bool {
return CssNameStart(byte) || CssIsDigit(byte) || byte == #char "-"
}
CssLower :: (byte: u8) -> u8 {
if byte >= #char "A" && byte <= #char "Z" { return byte + 32 }
return byte
}
CssNameEquals :: (name: CssName, text: string) -> bool {
if name.length != cast(s32)text.count { return false }
for index: cast(s32)0..name.length - 1 {
if CssLower(name.bytes[index]) != CssLower(text[index]) { return false }
}
return true
}
CssNameEqualsName :: (a: CssName, b: CssName) -> bool {
if a.length != b.length { return false }
for index: cast(s32)0..a.length - 1 {
if CssLower(a.bytes[index]) != CssLower(b.bytes[index]) { return false }
}
return true
}
// Custom property names are case-sensitive, as in CSS.
CssNameSame :: (a: CssName, b: CssName) -> bool {
if a.length != b.length { return false }
for index: cast(s32)0..a.length - 1 {
if a.bytes[index] != b.bytes[index] { return false }
}
return true
}
CssMakeName :: (text: string) -> CssName {
name: CssName
count: s32 = cast(s32)text.count
if count > 64 { count = 64 }
for index: cast(s32)0..count - 1 { name.bytes[index] = text[index] }
name.length = count
return name
}
CssCopyFileName :: (text: string) -> CssSourceFile {
file: CssSourceFile
count: s32 = cast(s32)text.count
if count > 96 { count = 96 }
for index: cast(s32)0..count - 1 { file.name[index] = text[index] }
file.length = count
return file
}
// Skip white space and /* */ comments. An unterminated comment is an error:
// it would otherwise hide the rest of the sheet.
CssSkipSpace :: (p: *CssParser) {
while !CssAtEnd(p) {
byte: u8 = p.cursor.source[p.cursor.pos]
if CssIsSpace(byte) {
CssAdvance(p, 1)
continue
}
if byte == #char "/" && CssPeek(p, 1) == #char "*" {
CssAdvance(p, 2)
closed: bool = false
while !CssAtEnd(p) {
if p.cursor.source[p.cursor.pos] == #char "*" && CssPeek(p, 1) == #char "/" {
CssAdvance(p, 2)
closed = true
break
}
CssAdvance(p, 1)
}
if !closed {
CssFail(p, "unterminated comment")
return
}
continue
}
if byte == #char "/" && CssPeek(p, 1) == #char "/" {
CssFail(p, "'//' comments are not CSS; use /* */")
return
}
return
}
}
CssFailed :: (p: *CssParser) -> bool {
return p.status == cast(s32)CssStatusError
}
// Read an identifier at the cursor without skipping space first. A custom
// property name keeps its leading '--'; a leading single '-' must start a
// name, as in '-kryon-material'.
CssReadName :: (p: *CssParser) -> CssName {
name: CssName
start: s32 = p.cursor.pos
if CssPeek(p, 0) == #char "-" && CssPeek(p, 1) == #char "-" {
CssAdvance(p, 2)
if !CssNameChar(CssPeek(p, 0)) {
CssFail(p, "expected a custom property name after '--'")
return name
}
} else if CssPeek(p, 0) == #char "-" {
if !CssNameStart(CssPeek(p, 1)) { return name }
CssAdvance(p, 1)
} else if !CssNameStart(CssPeek(p, 0)) {
return name
}
while !CssAtEnd(p) && CssNameChar(p.cursor.source[p.cursor.pos]) {
CssAdvance(p, 1)
}
if CssPeek(p, 0) == #char "\\" {
CssFail(p, "escapes in names are outside Kryon CSS")
return name
}
length: s32 = p.cursor.pos - start
if length > 64 {
CssFail(p, "name longer than 64 bytes")
return name
}
return CssMakeName(p.cursor.source[start:p.cursor.pos])
}
CssExpect :: (p: *CssParser, byte: u8, message: string) -> bool {
CssSkipSpace(p)
if CssFailed(p) { return false }
if CssAtEnd(p) || p.cursor.source[p.cursor.pos] != byte {
CssFail(p, message)
return false
}
CssAdvance(p, 1)
return true
}
// A quoted string's contents, as a view into the source. Strings end on
// their line, as in CSS; escapes are outside the subset.
CssStringSpan :: struct {
start: s32
length: s32
ok: bool
}
CssReadString :: (p: *CssParser) -> CssStringSpan {
span: CssStringSpan
quote: u8 = CssPeek(p, 0)
if quote != #char "\"" && quote != #char "'" { return span }
CssAdvance(p, 1)
span.start = p.cursor.pos
while !CssAtEnd(p) {
byte: u8 = p.cursor.source[p.cursor.pos]
if byte == quote {
span.length = p.cursor.pos - span.start
CssAdvance(p, 1)
span.ok = true
return span
}
if byte == 10 || byte == 13 { break }
if byte == #char "\\" {
CssFail(p, "escapes in strings are outside Kryon CSS")
return span
}
CssAdvance(p, 1)
}
CssFail(p, "unterminated string")
return span
}
// Diagnostics: the message, then the exact file, line and column.
CssDiagnosticAppend :: (p: *CssParser, text: string) {
for index: cast(s32)0..cast(s32)text.count - 1 {
if p.diagnostic_length >= 255 { return }
p.diagnostic[p.diagnostic_length] = text[index]
p.diagnostic_length += 1
}
}
CssDiagnosticAppendInt :: (p: *CssParser, value: s32) {
digits: [12]u8
count: s32 = 0
remaining: s32 = value
if remaining < 0 {
CssDiagnosticAppend(p, "-")
remaining = -remaining
}
if remaining == 0 {
CssDiagnosticAppend(p, "0")
return
}
while remaining > 0 && count < 12 {
digits[count] = cast(u8)(48 + remaining % 10)
remaining = remaining / 10
count += 1
}
while count > 0 {
count -= 1
if p.diagnostic_length < 255 {
p.diagnostic[p.diagnostic_length] = digits[count]
p.diagnostic_length += 1
}
}
}
CssDiagnosticAt :: (p: *CssParser) {
CssDiagnosticAppend(p, " at ")
file: s32 = p.cursor.file
if file >= 0 && file < p.file_count && p.files[file].length > 0 {
CssDiagnosticAppend(p, TextView(p.files[file].name[:p.files[file].length]))
CssDiagnosticAppend(p, ":")
}
CssDiagnosticAppendInt(p, p.cursor.line)
CssDiagnosticAppend(p, ":")
CssDiagnosticAppendInt(p, p.cursor.column)
}
CssFail :: (p: *CssParser, message: string) {
if CssFailed(p) { return }
p.diagnostic_length = 0
CssDiagnosticAppend(p, message)
CssDiagnosticAt(p)
p.status = cast(s32)CssStatusError
}
// "message 'name'suffix at file:line:column".
CssFailName :: (p: *CssParser, message: string, name: CssName, suffix: string) {
if CssFailed(p) { return }
p.diagnostic_length = 0
CssDiagnosticAppend(p, message)
CssDiagnosticAppend(p, " '")
CssDiagnosticAppend(p, TextView(name.bytes[:name.length]))
CssDiagnosticAppend(p, "'")
CssDiagnosticAppend(p, suffix)
CssDiagnosticAt(p)
p.status = cast(s32)CssStatusError
}
// The diagnostic, copied into `output`.
#program_export
CssDiagnostic :: (p: *CssParser, output: []u8) -> string {
count: s32 = p.diagnostic_length
if count > cast(s32)output.count { count = cast(s32)output.count }
for index: cast(s32)0..count - 1 { output[index] = p.diagnostic[index] }
return TextView(output[:count])
}
// A number with an optional unit: '12px', '0.5', '50%', '140ms'. The unit
// is an identifier directly after the digits, or '%'.
CssNumber :: struct {
value: float64
unit: CssName
percent: bool
ok: bool
}
CssReadNumber :: (p: *CssParser) -> CssNumber {
result: CssNumber
start: s32 = p.cursor.pos
negative: bool = false
if CssPeek(p, 0) == #char "-" || CssPeek(p, 0) == #char "+" {
negative = CssPeek(p, 0) == #char "-"
CssAdvance(p, 1)
}
whole: float64 = 0.0
digits: s32 = 0
while CssIsDigit(CssPeek(p, 0)) {
whole = whole * 10.0 + cast(float64)(CssPeek(p, 0) - 48)
digits += 1
CssAdvance(p, 1)
}
fraction: float64 = 0.0
scale: float64 = 1.0
if CssPeek(p, 0) == #char "." && CssIsDigit(CssPeek(p, 1)) {
CssAdvance(p, 1)
while CssIsDigit(CssPeek(p, 0)) {
if scale < 1.0e15 {
fraction = fraction * 10.0 + cast(float64)(CssPeek(p, 0) - 48)
scale = scale * 10.0
}
digits += 1
CssAdvance(p, 1)
}
}
if digits == 0 {
// Not a number: leave the cursor where it was.
p.cursor.column -= p.cursor.pos - start
p.cursor.pos = start
return result
}
if digits > 18 {
CssFail(p, "number has too many digits")
return result
}
if (CssPeek(p, 0) == #char "e" || CssPeek(p, 0) == #char "E") &&
(CssIsDigit(CssPeek(p, 1)) || ((CssPeek(p, 1) == #char "-" || CssPeek(p, 1) == #char "+") &&
CssIsDigit(CssPeek(p, 2)))) {
CssFail(p, "exponents are outside Kryon CSS")
return result
}
result.value = whole + fraction / scale
if negative { result.value = -result.value }
if CssPeek(p, 0) == #char "%" {
CssAdvance(p, 1)
result.percent = true
} else if CssNameStart(CssPeek(p, 0)) {
result.unit = CssReadName(p)
if CssFailed(p) { return result }
}
result.ok = true
return result
}
// Value text ends at ';' or '}' outside parentheses and strings. Returns the
// trimmed span; the cursor stops on the terminator.
CssValueSpan :: (p: *CssParser) -> CssStringSpan {
span: CssStringSpan
CssSkipSpace(p)
if CssFailed(p) { return span }
span.start = p.cursor.pos
depth: s32 = 0
while true {
if CssAtEnd(p) {
CssFail(p, "expected ';' or '}' after the value")
return span
}
byte: u8 = p.cursor.source[p.cursor.pos]
if byte == #char "\"" || byte == #char "'" {
unused CssReadString(p)
if CssFailed(p) { return span }
continue
}
if byte == #char "/" && CssPeek(p, 1) == #char "*" {
CssSkipSpace(p)
if CssFailed(p) { return span }
continue
}
if byte == #char "(" || byte == #char "[" {
depth += 1
} else if byte == #char ")" || byte == #char "]" {
if depth == 0 {
CssFail(p, "unbalanced ')' in the value")
return span
}
depth -= 1
} else if byte == #char "{" {
CssFail(p, "'{' inside a value; expected ';'")
return span
} else if depth == 0 && (byte == #char ";" || byte == #char "}") {
break
} else if depth > 0 && (byte == #char ";" || byte == #char "}") {
CssFail(p, "unclosed '(' in the value")
return span
}
CssAdvance(p, 1)
}
end: s32 = p.cursor.pos
while end > span.start && CssIsSpace(p.cursor.source[end - 1]) { end -= 1 }
span.length = end - span.start
span.ok = span.length > 0
return span
}