crml_core/lib.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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
pub mod generator;
pub mod selector;
use selector::{Selector, SelectorState};
/// The type of a given [`Token`].
#[derive(Debug)]
pub enum TokenType {
/// A direct string of Rust code:
///
/// ```text
/// # let a = 1
/// ```
///
/// Begins with `#`.
RustString,
/// A direct string of Rust code which is pushed to the output HTML:
///
/// ```text
/// $ (a + b).to_string()
///
/// # fn get_new_string() {
/// # String::new()
/// # }
///
/// $ get_new_string()
/// ```
///
/// Begins with `+`.
PushedRustString,
/// A CSS selector which will be transformed into an HTML element:
///
/// ```text
/// %element.class#id[attr=val]
/// ```
///
/// Begins with `%`. If a single quote (`'`) comes after the selector,
/// everything else on the line will be treated as the `innerHTML`, and the
/// element will be closed as well.
Selector,
/// Raw text:
///
/// ```text
/// anything not matched into the previous types
/// ```
Raw,
}
/// A *token* is a representation of fully parsed data.
#[derive(Debug)]
pub struct Token {
/// The type of the token.
pub r#type: TokenType,
/// The raw CRML string of the token.
pub raw: String,
/// The HTML string of the token.
pub html: String,
/// The indent level of the token.
pub indent: usize,
/// The line number the token is found on.
pub line: i32,
/// The selector of the token. Only applies to [`TokenType::Selector`].
pub selector: Option<SelectorState>,
}
impl Token {
/// Create a [`Token`] given its `indent` and `line` value.
pub fn from_indent_ln(indent: usize, line: i32) -> Self {
Self {
r#type: TokenType::Raw,
raw: "\n".to_string(),
html: "\n".to_string(),
indent,
line,
selector: None,
}
}
/// Create a [`Token`] from a given [`String`] value,
pub fn from_string(value: String, indent: usize, line: i32) -> Option<Self> {
let mut chars = value.chars();
match match chars.next() {
Some(c) => c,
None => {
return Some(Self::from_indent_ln(indent, line));
}
} {
'#' => {
// starting with an opening sign; rust data
// not much real parsing to do here
let mut raw = String::new();
while let Some(char) = chars.next() {
raw.push(char);
}
return Some(Self {
r#type: TokenType::RustString,
raw,
html: String::new(),
indent,
line,
selector: None,
});
}
'$' => {
// starting with an opening sign; rust data
// not much real parsing to do here
let mut raw = String::new();
while let Some(char) = chars.next() {
raw.push(char);
}
return Some(Self {
r#type: TokenType::PushedRustString,
raw,
html: String::new(),
indent,
line,
selector: None,
});
}
'%' => {
// starting with a beginning sign; selector
let mut raw = String::new();
let mut data = String::new();
let mut inline: bool = false;
while let Some(char) = chars.next() {
// check for inline char
if char == '\'' {
inline = true;
break;
}
// push char
raw.push(char);
}
if inline {
while let Some(char) = chars.next() {
data.push(char);
}
}
let selector = Selector::new(raw.clone()).parse();
return Some(Self {
r#type: TokenType::Selector,
raw: format!("{raw}{data}"),
html: if inline {
// inline element
format!("{}{data}</{}>", selector.clone().render(), selector.tag)
} else {
selector.clone().render()
},
indent,
line,
selector: Some(selector),
});
}
_ => {
// no recognizable starting character; raw data
return Some(Self {
r#type: TokenType::Raw,
raw: value.clone(),
html: value,
indent,
line,
selector: None,
});
}
}
}
}
/// Iterable version of [`Parser`]. Created through [`Parser::parse`].
pub struct TokenStream(Parser);
impl Iterator for TokenStream {
type Item = Token;
fn next(&mut self) -> Option<Self::Item> {
self.0.next()
}
}
/// The current state of the given [`Parser`].
pub struct ParserState {
/// The current line the parser is on.
///
/// We parse line by line to enforce whitespace. This means we just need to
/// track what line we are currently on.
pub line_number: i32,
}
impl Default for ParserState {
fn default() -> Self {
Self { line_number: -1 }
}
}
/// General character-by-character parser for CRML.
pub struct Parser(Vec<String>, ParserState);
impl Parser {
/// Create a new [`Parser`]
pub fn new(input: String) -> Self {
let mut lines = Vec::new();
for line in input.split("\n") {
lines.push(line.to_owned())
}
// ...
Self(lines, ParserState::default())
}
/// Begin parsing the `input`
pub fn parse(self) -> TokenStream {
TokenStream(self)
}
/// Parse the next line in the given `input`
pub fn next(&mut self) -> Option<Token> {
// get line
self.1.line_number += 1;
let line = match self.0.get(self.1.line_number as usize) {
Some(l) => l,
None => return None,
};
if line.is_empty() {
return Some(Token::from_indent_ln(0, self.1.line_number));
}
// get indent
let mut indent: usize = 0;
let mut chars = line.chars();
while let Some(char) = chars.next() {
if (char != ' ') & (char != '\t') {
break;
}
indent += 1;
}
// parse token
Token::from_string(line.trim().to_owned(), indent, self.1.line_number)
}
}