Skip to main content

lexigram_core/
lib.rs

1// Copyright (c) 2025 Redglyph (@gmail.com). All Rights Reserved.
2
3pub mod alt;
4pub mod segmap;
5pub mod char_reader;
6pub mod fixed_sym_table;
7pub mod log;
8pub mod lexer;
9pub mod parser;
10pub mod text_span;
11
12// package name & version
13pub const CORE_PKG_NAME: &str = env!("CARGO_PKG_NAME");
14pub const CORE_PKG_VERSION: &str = env!("CARGO_PKG_VERSION");
15
16/// ID of a lexer token
17pub type TokenId = u16;
18/// ID of a nonterminal
19pub type VarId = u16;
20/// ID of a rule alternative. We use the same type as [VarId] because they're very similar quantities.
21pub type AltId = VarId;
22
23#[derive(Clone, Debug)]
24pub struct LL1;
25
26#[derive(Clone, Debug)]
27pub struct LALR;
28
29// ---------------------------------------------------------------------------------------------
30// General helper traits
31
32pub trait CharLen {
33    /// Returns the length in characters (not bytes).
34    fn charlen(&self) -> usize;
35}
36
37impl<T: AsRef<str>> CharLen for T {
38    fn charlen(&self) -> usize {
39        self.as_ref().chars().count()
40    }
41}
42
43/// This trait provides a shortcut for two commonly used iterator adapters.
44///
45/// * [join(...)](CollectJoin::join): joins [ToString] items into a `Vec::<String>` by inserting a separator between them.
46/// * [to_vec(...)](CollectJoin::to_vec): equivalent to `.collect::<Vec<_>>()`
47pub trait CollectJoin {
48    /// Iterator adapter that joins [ToString] items into a `Vec::<String>` by inserting `separator` between them.
49    ///
50    /// ## Example
51    ///
52    /// ```
53    /// # use lexigram_core::CollectJoin;
54    /// let numbers = (0..10).filter(|&x| x < 5).join(", ");
55    /// assert_eq!(numbers, "0, 1, 2, 3, 4");
56    /// ```
57    fn join(&mut self, separator: &str) -> String
58        where Self: Iterator,
59              <Self as Iterator>::Item: ToString
60    {
61        self.map(|x| x.to_string()).collect::<Vec<_>>().join(separator)
62    }
63
64    /// Iterator adapter that joins items into a `Vec<_>`; equivalent to `.collect::<Vec<_>>()`
65    ///
66    /// ## Example
67    ///
68    /// ```
69    /// # use lexigram_core::CollectJoin;
70    /// let values = [1, 5, 10, 25, 50].into_iter().map(|x| x * x).to_vec();
71    /// assert_eq!(values, vec![1, 25, 100, 625, 2500]);
72    /// ```
73    fn to_vec(self) -> Vec<<Self as Iterator>::Item>
74        where Self: Iterator + Sized
75    {
76        self.collect::<Vec<_>>()
77    }
78}
79
80impl<I: Iterator> CollectJoin for I {}
81
82// ---------------------------------------------------------------------------------------------
83// Macros
84
85pub mod macros {
86    /// Generates an `OpCode` instance.
87    ///
88    /// # Examples
89    /// ```
90    /// # use lexigram_core::TokenId;
91    /// # use lexigram_core::opcode;
92    /// # use lexigram_core::VarId;
93    /// # use lexigram_core::parser::OpCode;
94    /// assert_eq!(opcode!(e), OpCode::Empty);
95    /// assert_eq!(opcode!(t 2), OpCode::T(2 as TokenId));
96    /// assert_eq!(opcode!(nt 3), OpCode::NT(3));
97    /// assert_eq!(opcode!(loop 2), OpCode::Loop(2));
98    /// assert_eq!(opcode!(exit 1), OpCode::Exit(1));
99    /// assert_eq!(opcode!(nt 3), OpCode::NT(3));
100    /// assert_eq!(opcode!(loop 2), OpCode::Loop(2));
101    /// assert_eq!(opcode!(exit 1), OpCode::Exit(1));
102    /// assert_eq!(opcode!(hook), OpCode::Hook);
103    /// assert_eq!(opcode!(end), OpCode::End);
104    #[macro_export]
105    macro_rules! opcode {
106        (e) => { $crate::parser::OpCode::Empty };
107        (t $id:expr) => { $crate::parser::OpCode::T($id as $crate::TokenId) };
108        (nt $id:expr) => { $crate::parser::OpCode::NT($id as $crate::VarId) };
109        (loop $id:expr) => { $crate::parser::OpCode::Loop($id as $crate::VarId) };
110        (exit $id:expr) => { $crate::parser::OpCode::Exit($id as $crate::VarId) };
111        (nt $id:expr) => { $crate::parser::OpCode::NT($id as $crate::VarId, 0) };
112        (loop $id:expr) => { $crate::parser::OpCode::Loop($id as $crate::VarId, 0) };
113        (exit $id:expr) => { $crate::parser::OpCode::Exit($id as $crate::VarId, 0) };
114        (hook) => { $crate::parser::OpCode::Hook };
115        (end) => { $crate::parser::OpCode::End };
116    }
117
118    /// Generates an opcode strip. A strip is made up of `OpCode` items separated by a comma.
119    ///
120    /// # Example
121    /// ```
122    /// # use lexigram_core::{TokenId, VarId, strip, opcode};
123    /// # use lexigram_core::alt::Alternative;
124    /// # use lexigram_core::parser::{OpCode, Symbol};
125    /// assert_eq!(strip!(nt 1, loop 5, t 3, e), vec![opcode!(nt 1), opcode!(loop 5), opcode!(t 3), opcode!(e)]);
126    /// ```
127    #[macro_export]
128    macro_rules! strip {
129        () => { std::vec![] };
130        ($($a:ident $($b:expr)?,)+) => { strip![$($a $($b)?),+] };
131        ($($a:ident $($b:expr)?),*) => { std::vec![$($crate::opcode!($a $($b)?)),*] };
132    }
133}