heraclitus_compiler/compiling_rules/rules.rs
1use super::region::Region;
2
3#[cfg(feature = "serde")]
4use serde::{Serialize, Deserialize};
5
6/// Determine lexing rules for the parser
7///
8/// Rules struct that contains list of symbols as well as region tree
9///
10/// This struct requires two things:
11/// 1. List of symbols
12/// 2. Region Tree
13///
14/// More on those below in the **Fields** section
15///
16/// # Example
17/// ```
18/// # use heraclitus_compiler::prelude::*;
19/// let symbols = vec!['+', '-', '*', '/', '(', ')', '&', '|', '!'];
20/// let compounds = vec![('&', '&'), ('|', '|')];
21/// let region = reg![
22/// reg!(str as "string literal" => {
23/// begin: "'",
24/// end: "'"
25/// })
26/// ];
27/// Rules::new(symbols, compounds, region);
28/// ```
29
30#[derive(Debug, Clone, PartialEq)]
31#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
32pub struct Rules {
33 /// Symbols that should be separated (most commonly: (, ), +, -, ...)
34 /// This handles situations like for instance if we want to parse `1+1` as
35 /// three tokens `1` `+` `1` and not single `1+1` token.
36 pub symbols: Vec<char>,
37 /// Region tree that determines what code regions should remain untokenized.
38 /// Most common case is a string where we want to preserve all the spaces and words inside.
39 pub region_tree: Region,
40 /// Escape symbol
41 pub escape_symbol: char,
42 /// Vector of pairs of symbols that should be merged together
43 pub compounds: Vec<(char, char)>
44}
45
46impl Rules {
47 /// Creates new rules that can be supplied to the compiler
48 pub fn new(symbols: Vec<char>, compounds: Vec<(char, char)>, region_tree: Region) -> Rules {
49 Rules {
50 symbols,
51 compounds,
52 region_tree,
53 escape_symbol: '\\'
54 }
55 }
56
57 /// Set custom escape symbol for your language
58 pub fn set_escape(mut self, symbol: char) -> Self {
59 self.escape_symbol = symbol;
60 self
61 }
62}