-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathhighlight.rs
62 lines (50 loc) · 1.88 KB
/
highlight.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// RCL -- A reasonable configuration language.
// Copyright 2023 Ruud van Asseldonk
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// A copy of the License has been included in the root of the repository.
//! A syntax highlighter that acts on the token stream.
use crate::lexer::{Lexeme, Token};
use crate::markup::{Markup, MarkupString};
fn get_markup(token: &Token) -> Markup {
match token {
Token::LineComment => Markup::Comment,
Token::NumBinary | Token::NumHexadecimal | Token::NumDecimal => Markup::Number,
Token::QuoteOpen(..) | Token::QuoteClose | Token::StringInner => Markup::String,
Token::HoleOpen | Token::HoleClose | Token::Escape(..) => Markup::Escape,
Token::Ident => Markup::Field,
Token::KwAnd
| Token::KwAssert
| Token::KwElse
| Token::KwFalse
| Token::KwFor
| Token::KwIf
| Token::KwImport
| Token::KwIn
| Token::KwLet
| Token::KwNot
| Token::KwNull
| Token::KwOr
| Token::KwTrace
| Token::KwTrue => Markup::Keyword,
_ => Markup::None,
}
}
pub fn highlight<'a>(tokens: &[Lexeme], input: &'a str) -> MarkupString<'a> {
let mut out = MarkupString::new();
let mut end = 0;
for (token, span) in tokens {
// Insignificant space is not represented explicitly as a token, we can
// infer it from a gap in the byte range.
if span.start() > end {
out.push(&input[end..span.start()], Markup::None);
}
let markup = get_markup(token);
let string = &input[span.start()..span.end()];
out.push(string, markup);
end = span.end();
}
// Add a trailing newline, which is lost during the parsing if there is any.
out.push("\n", Markup::None);
out
}