xonecas.io
8447
Co

conveyor / files

conveyor / templates / library / widgets / code_scan.rs

10.7 KB
1//! A table-driven code scanner for the languages rune_kit's own scanners don't know: each
2//! language is a list of keywords, types, constants, comment markers and quotes.
3use rune_kit::syntax::{Highlight, Paint, Syntax};
4use std::ops::Range;
5
6pub struct Spec {
7    /// Space-separated word lists.
8    keywords: &'static [&'static str],
9    types: &'static str,
10    consts: &'static str,
11    line: &'static [&'static str],
12    block: Option<(&'static str, &'static str)>,
13    quotes: &'static [u8],
14    /// Bytes that may start a word as well as letters: a word they start is a property
15    /// (`$var`, `@field`, `@Annotation`) unless it is a keyword (`#include`, `.PHONY`).
16    starts: &'static [u8],
17    /// Whether a word before `=` is a key, as in config files.
18    keys: bool,
19}
20
21const NONE: Spec = Spec { keywords: &[], types: "", consts: "true false null", line: &["//"], block: Some(("/*", "*/")), quotes: b"\"'", starts: b"", keys: false };
22
23const C: &str = "auto break case const continue default do else enum extern for goto if inline register restrict return sizeof static struct switch typedef union volatile while \
24    #include #define #undef #if #ifdef #ifndef #elif #else #endif #pragma #error";
25const C_TYPES: &str = "void char short int long float double signed unsigned bool size_t ssize_t int8_t int16_t int32_t int64_t uint8_t uint16_t uint32_t uint64_t";
26const CPP: &str = "class namespace template typename public private protected virtual override final new delete this using operator friend try catch throw \
27    noexcept constexpr consteval explicit mutable static_cast dynamic_cast reinterpret_cast const_cast decltype concept requires co_await co_return co_yield";
28
29/// The scanner for `lang` (a canonical or fence name), if this module has one.
30pub fn spec(lang: &str) -> Option<&'static Spec> {
31    Some(match lang {
32        "c" | "h" => &Spec { keywords: &[C], types: C_TYPES, consts: "NULL true false", starts: b"#", ..NONE },
33        "cpp" | "c++" | "cc" | "hpp" => &Spec { keywords: &[C, CPP], types: C_TYPES, consts: "NULL nullptr true false", starts: b"#", ..NONE },
34        "cs" | "csharp" | "c#" => &Spec {
35            keywords: &["abstract as base break case catch checked class const continue default delegate do else enum event explicit extern finally fixed \
36                for foreach goto if implicit in interface internal is lock namespace new operator out override params private protected public readonly ref \
37                return sealed sizeof stackalloc static struct switch this throw try typeof unchecked unsafe using virtual volatile while async await var get \
38                set init record where yield"],
39            types: "bool byte char decimal double float int long object sbyte short string uint ulong ushort void dynamic", starts: b"#", ..NONE },
40        "java" => &Spec {
41            keywords: &["abstract assert break case catch class const continue default do else enum extends final finally for goto if implements import \
42                instanceof interface native new package private protected public return static strictfp super switch synchronized this throw throws \
43                transient try volatile while var record sealed permits yield"],
44            types: "boolean byte char double float int long short void", starts: b"@", ..NONE },
45        "kotlin" | "kt" | "kts" => &Spec {
46            keywords: &["as break class continue do else for fun if in interface is object package return super this throw try typealias typeof val var \
47                when while by catch constructor finally get import init set where abstract annotation companion const data enum external final infix \
48                inline inner internal lateinit open operator out override private protected public reified sealed suspend tailrec vararg"],
49            starts: b"@", ..NONE },
50        "scala" => &Spec {
51            keywords: &["abstract case catch class def do else enum export extends final finally for given if implicit import lazy match new object \
52                override package private protected return sealed super then this throw trait try type using val var while with yield"],
53            ..NONE },
54        "dart" => &Spec {
55            keywords: &["abstract as assert async await break case catch class const continue covariant default deferred do else enum export extends \
56                extension external factory final finally for get hide if implements import in interface is late library mixin new on operator part \
57                required rethrow return sealed set show static super switch sync this throw try typedef var when while with yield"],
58            types: "void int double num bool dynamic", starts: b"@", ..NONE },
59        "php" => &Spec {
60            keywords: &["abstract and array as break callable case catch class clone const continue declare default do echo else elseif empty \
61                enddeclare endfor endforeach endif endswitch endwhile enum extends final finally fn for foreach function global goto if implements \
62                include include_once instanceof insteadof interface isset list match namespace new or print private protected public readonly require \
63                require_once return static switch throw trait try unset use var while xor yield"],
64            consts: "true false null TRUE FALSE NULL", line: &["//", "#"], quotes: b"\"'`", starts: b"$", ..NONE },
65        "ruby" | "rb" => &Spec {
66            keywords: &["BEGIN END alias and begin break case class def do else elsif end ensure for if in module next not or redo rescue retry return \
67                self super then undef unless until when while yield require require_relative include extend attr_accessor attr_reader attr_writer \
68                private protected public raise lambda proc puts"],
69            consts: "true false nil", line: &["#"], block: Some(("=begin", "=end")), quotes: b"\"'`", starts: b"@$", ..NONE },
70        "lua" => &Spec {
71            keywords: &["and break do else elseif end for function goto if in local not or repeat return then until while"],
72            consts: "true false nil", line: &["--"], block: Some(("--[[", "]]")), ..NONE },
73        "zig" => &Spec {
74            keywords: &["addrspace align allowzero and anyframe anytype asm async await break callconv catch comptime const continue defer else enum \
75                errdefer error export extern fn for if inline linksection noalias noinline nosuspend opaque or orelse packed pub resume return struct \
76                suspend switch test threadlocal try union unreachable usingnamespace var volatile while"],
77            types: "i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 isize usize f16 f32 f64 f128 bool void type anyerror noreturn comptime_int comptime_float",
78            consts: "true false null undefined", block: None, starts: b"@", ..NONE },
79        "make" | "makefile" | "mk" => &Spec {
80            keywords: &["ifeq ifneq ifdef ifndef else endif include define endef export unexport override vpath .PHONY .DEFAULT .SUFFIXES"],
81            consts: "", line: &["#"], block: None, starts: b"$.", ..NONE },
82        "dockerfile" | "docker" | "containerfile" => &Spec {
83            keywords: &["FROM RUN CMD LABEL MAINTAINER EXPOSE ENV ADD COPY ENTRYPOINT VOLUME USER WORKDIR ARG ONBUILD STOPSIGNAL HEALTHCHECK SHELL AS"],
84            consts: "", line: &["#"], block: None, starts: b"$", ..NONE },
85        "toml" => &Spec { consts: "true false inf nan", line: &["#"], block: None, keys: true, ..NONE },
86        "ini" | "conf" | "cfg" => &Spec { consts: "true false yes no on off", line: &["#", ";"], block: None, keys: true, ..NONE },
87        _ => return None,
88    })
89}
90
91/// Adds `r` as `k`, joined to the last range when they touch and match.
92pub fn push(out: &mut Vec<Highlight>, r: Range<usize>, k: Syntax) {
93    if let Some((last, Paint::Kind(lk))) = out.last_mut() && last.end == r.start && *lk == k { last.end = r.end; return }
94    out.push((r, Paint::Kind(k)));
95}
96
97/// `s`'s highlights of `code[from..to]`. Config files colour a line starting with `[` as a section.
98pub fn scan(s: &Spec, code: &str, from: usize, to: usize, out: &mut Vec<Highlight>) {
99    let b = code.as_bytes();
100    let has = |list: &str, w: &str| list.split_ascii_whitespace().any(|k| k == w);
101    let word = |c: u8| c.is_ascii_alphanumeric() || c == b'_';
102    let mut i = from;
103    while i < to {
104        let (start, c, rest) = (i, b[i], &b[i..to]);
105        let kind = if let Some((open, close)) = s.block.filter(|(open, _)| rest.starts_with(open.as_bytes())) {
106            i = find(b, i + open.len(), to, close.as_bytes()).map_or(to, |e| e + close.len());
107            Syntax::Comment
108        } else if s.line.iter().any(|p| rest.starts_with(p.as_bytes())) {
109            i = find(b, i, to, b"\n").unwrap_or(to);
110            Syntax::Comment
111        } else if s.keys && c == b'[' && (i == 0 || b[i - 1] == b'\n') {
112            i = find(b, i, to, b"\n").unwrap_or(to);
113            Syntax::Type
114        } else if s.quotes.contains(&c) {
115            i = string_end(b, i, to, c);
116            Syntax::String
117        } else if c.is_ascii_digit() {
118            while i < to && (word(b[i]) || b[i] == b'.') { i += 1 }
119            Syntax::Number
120        } else if word(c) || s.starts.contains(&c) {
121            i += 1;
122            while i < to && (word(b[i]) || b[i] == b'-' && s.keys) { i += 1 }
123            let w = &code[start..i];
124            let after = b[i..to].iter().position(|&c| c != b' ' && c != b'\t').map(|p| (b[i + p], b.get(i + p + 1).copied()));
125            if s.keywords.iter().any(|l| has(l, w)) { Syntax::Keyword }
126            else if has(s.consts, w) { Syntax::Number }
127            else if !word(c) { Syntax::Property }
128            else if s.keys && matches!(after, Some((b'=', n)) if n != Some(b'=')) { Syntax::Property }
129            else if has(s.types, w) { Syntax::Type }
130            else if w.len() > 1 && w.bytes().all(|c| c.is_ascii_uppercase() || c.is_ascii_digit() || c == b'_') { Syntax::Number }
131            else if c.is_ascii_uppercase() { Syntax::Type }
132            else if matches!(after, Some((b'(', _))) { Syntax::Function }
133            else { continue }
134        } else {
135            i += 1;
136            if b"{}[]()<>=+-*/%!&|^~?:;,.".contains(&c) { Syntax::Punctuation } else { continue }
137        };
138        push(out, start..i, kind);
139    }
140}
141
142/// Where the string opening at `i` ends: after its closing quote, or at its line's end. A
143/// tripled quote (`"""`) runs to its tripled close across lines.
144fn string_end(b: &[u8], i: usize, to: usize, q: u8) -> usize {
145    if b[i..to].starts_with(&[q; 3]) { return find(b, i + 3, to, &[q; 3]).map_or(to, |e| e + 3) }
146    let mut j = i + 1;
147    while j < to && b[j] != q && b[j] != b'\n' { j += if b[j] == b'\\' { 2 } else { 1 } }
148    if j < to && b[j] == q { j + 1 } else { j.min(to) }
149}
150
151fn find(b: &[u8], from: usize, to: usize, pat: &[u8]) -> Option<usize> {
152    b.get(from..to)?.windows(pat.len()).position(|w| w == pat).map(|p| from + p)
153}