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/// This trait provides a shortcut for two commonly used iterator adapters.
24///
25/// * [join(...)](CollectJoin::join): joins [ToString] items into a `Vec::<String>` by inserting a separator between them.
26/// * [to_vec(...)](CollectJoin::to_vec): equivalent to `.collect::<Vec<_>>()`
27pub trait CollectJoin {
28 /// Iterator adapter that joins [ToString] items into a `Vec::<String>` by inserting `separator` between them.
29 ///
30 /// ## Example
31 ///
32 /// ```
33 /// # use lexigram_core::CollectJoin;
34 /// let numbers = (0..10).filter(|&x| x < 5).join(", ");
35 /// assert_eq!(numbers, "0, 1, 2, 3, 4");
36 /// ```
37 fn join(&mut self, separator: &str) -> String
38 where Self: Iterator,
39 <Self as Iterator>::Item: ToString
40 {
41 self.map(|x| x.to_string()).collect::<Vec<_>>().join(separator)
42 }
43
44 /// Iterator adapter that joins items into a `Vec<_>`; equivalent to `.collect::<Vec<_>>()`
45 ///
46 /// ## Example
47 ///
48 /// ```
49 /// # use lexigram_core::CollectJoin;
50 /// let values = [1, 5, 10, 25, 50].into_iter().map(|x| x * x).to_vec();
51 /// assert_eq!(values, vec![1, 25, 100, 625, 2500]);
52 /// ```
53 fn to_vec(self) -> Vec<<Self as Iterator>::Item>
54 where Self: Iterator + Sized
55 {
56 self.collect::<Vec<_>>()
57 }
58}
59
60impl<I: Iterator> CollectJoin for I {}
61
62// ---------------------------------------------------------------------------------------------
63// Macros
64
65pub mod macros {
66 /// Generates an `OpCode` instance.
67 ///
68 /// # Examples
69 /// ```
70 /// # use lexigram_core::TokenId;
71 /// # use lexigram_core::opcode;
72 /// # use lexigram_core::VarId;
73 /// # use lexigram_core::parser::OpCode;
74 /// assert_eq!(opcode!(e), OpCode::Empty);
75 /// assert_eq!(opcode!(t 2), OpCode::T(2 as TokenId));
76 /// assert_eq!(opcode!(nt 3), OpCode::NT(3));
77 /// assert_eq!(opcode!(loop 2), OpCode::Loop(2));
78 /// assert_eq!(opcode!(exit 1), OpCode::Exit(1));
79 /// assert_eq!(opcode!(nt 3), OpCode::NT(3));
80 /// assert_eq!(opcode!(loop 2), OpCode::Loop(2));
81 /// assert_eq!(opcode!(exit 1), OpCode::Exit(1));
82 /// assert_eq!(opcode!(hook), OpCode::Hook);
83 /// assert_eq!(opcode!(end), OpCode::End);
84 #[macro_export]
85 macro_rules! opcode {
86 (e) => { $crate::parser::OpCode::Empty };
87 (t $id:expr) => { $crate::parser::OpCode::T($id as $crate::TokenId) };
88 (nt $id:expr) => { $crate::parser::OpCode::NT($id as $crate::VarId) };
89 (loop $id:expr) => { $crate::parser::OpCode::Loop($id as $crate::VarId) };
90 (exit $id:expr) => { $crate::parser::OpCode::Exit($id as $crate::VarId) };
91 (nt $id:expr) => { $crate::parser::OpCode::NT($id as $crate::VarId, 0) };
92 (loop $id:expr) => { $crate::parser::OpCode::Loop($id as $crate::VarId, 0) };
93 (exit $id:expr) => { $crate::parser::OpCode::Exit($id as $crate::VarId, 0) };
94 (hook) => { $crate::parser::OpCode::Hook };
95 (end) => { $crate::parser::OpCode::End };
96 }
97
98 /// Generates an opcode strip. A strip is made up of `OpCode` items separated by a comma.
99 ///
100 /// # Example
101 /// ```
102 /// # use lexigram_core::{TokenId, VarId, strip, opcode};
103 /// # use lexigram_core::alt::Alternative;
104 /// # use lexigram_core::parser::{OpCode, Symbol};
105 /// assert_eq!(strip!(nt 1, loop 5, t 3, e), vec![opcode!(nt 1), opcode!(loop 5), opcode!(t 3), opcode!(e)]);
106 /// ```
107 #[macro_export]
108 macro_rules! strip {
109 () => { std::vec![] };
110 ($($a:ident $($b:expr)?,)+) => { strip![$($a $($b)?),+] };
111 ($($a:ident $($b:expr)?),*) => { std::vec![$($crate::opcode!($a $($b)?)),*] };
112 }
113}