Skip to content

Commit fb478e8

Browse files
committed
1. Code refactoring
2. Minor bug fixes
1 parent e2fc2a3 commit fb478e8

File tree

4 files changed

+23
-12
lines changed

4 files changed

+23
-12
lines changed

src/exprs/literal.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ impl ExprLit {
155155
next({
156156
debug_assert_eq!(a.get(1..len-1).is_some(), true);
157157

158+
// It's safe, checks are done above (above `debug_assert`).
158159
let str = unsafe {
159160
a.get_unchecked(1..len-1)
160161
};

src/lib.rs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ use std::fmt::Write;
7474
*/
7575

7676
use std::slice::IterMut;
77-
use proc_macro2::TokenTree as TokenTree2;
77+
use proc_macro2::{TokenTree as TokenTree2, Group};
7878
use proc_macro::TokenStream;
7979
use proc_macro2::TokenStream as TokenStream2;
8080
use trees::sg_err;
@@ -121,19 +121,17 @@ fn search_include_and_replacegroup(
121121
if punct.as_char() == '#' {
122122
if let Some(m_ident) = iter.next() {
123123
if let TokenTree2::Ident(ident) = m_ident {
124-
let macro_fn = {
124+
let macro_fn: Option<fn(&Group) -> TreeResult<TokenTree2>> = {
125125
let str_ident = ident.to_string();
126126

127127
match str_ident.as_str() {
128-
"include" | "include_tt" => macro_rule_include::<IncludeTt>,
129-
"include_and_fix_unknown_start_token" | "include_tt_and_fix_unknown_start_token" => macro_rule_include::<IncludeTtAndFixUnkStartToken>,
128+
"include" | "include_tt" => Some(macro_rule_include::<IncludeTt>),
129+
"include_and_fix_unknown_start_token" | "include_tt_and_fix_unknown_start_token" => Some(macro_rule_include::<IncludeTtAndFixUnkStartToken>),
130130

131-
"include_str" => macro_rule_include::<IncludeStr>,
132-
"include_arr" => macro_rule_include::<IncludeArr>,
131+
"include_str" => Some(macro_rule_include::<IncludeStr>),
132+
"include_arr" => Some(macro_rule_include::<IncludeArr>),
133133

134-
_ => sg_err! {
135-
return [ident.span()]: "Unknown macro, expected `include`, `include_tt`, `include_and_fix_unknown_start_token`, `include_tt_and_fix_unknown_start_token`, `include_str`, `include_arr`"
136-
},
134+
_ => None,
137135
}
138136
};
139137

@@ -142,6 +140,13 @@ fn search_include_and_replacegroup(
142140
if punct2.as_char() == '!' {
143141
if let Some(m_group) = iter.next() {
144142
if let TokenTree2::Group(group) = m_group {
143+
let macro_fn = match macro_fn {
144+
Some(a) => a,
145+
None => sg_err! {
146+
return [ident.span()]: "Unknown macro, expected `include`, `include_tt`, `include_and_fix_unknown_start_token`, `include_tt_and_fix_unknown_start_token`, `include_str`, `include_arr`"
147+
}
148+
};
149+
145150
let result = ttry!( macro_fn(group) );
146151

147152
let nulltt = make_null_ttree();
@@ -159,8 +164,10 @@ fn search_include_and_replacegroup(
159164
}
160165
}
161166

162-
sg_err! {
163-
return [ident.span()]: "Unknown macro, expected `include(...)`, `include_tt(...)`, `include_and_fix_unknown_start_token(...)`, `include_tt_and_fix_unknown_start_token(...)`, `include_str(...)`, `include_arr(...)`"
167+
if macro_fn.is_some() { // The required macro was defined earlier, which means an error.
168+
sg_err! {
169+
return [ident.span()]: "Unknown macro, expected `include(...)`, `include_tt(...)`, `include_and_fix_unknown_start_token(...)`, `include_tt_and_fix_unknown_start_token(...)`, `include_str(...)`, `include_arr(...)`"
170+
}
164171
}
165172
}
166173
}

src/macros/include.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ pub fn macro_rule_include<A>(
335335
// a string and concatenated.
336336

337337
match ttry!( g_stringify(&g_stream) ) {
338-
Some(stringify) => A::make_tree(
338+
Some(stringify) => A::make_tree(
339339
// The value is already ready to be used as a path.
340340
BehMacroArg0::Stringify(stringify),
341341

src/trees/loader.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use proc_macro2::{TokenStream as TokenStream2, Span};
33
use std::io::Error as IOError;
44
use syn::Error as SynError;
55

6+
/// Variants of errors when loading a file and presenting it as a set of compiler trees.
67
#[derive(Debug)]
78
pub enum LoadFileAndAutoMakeTreeErr<'a> {
89
/// The error type for I/O operations of the
@@ -17,6 +18,8 @@ pub enum LoadFileAndAutoMakeTreeErr<'a> {
1718
}
1819

1920
impl<'a> LoadFileAndAutoMakeTreeErr<'a> {
21+
/// The error type for I/O operations of the
22+
/// [Read], [Write], [Seek], and associated traits.
2023
#[inline(always)]
2124
pub const fn read_to_string(err: IOError, path: &'a str) -> Self {
2225
Self::ReadToString {

0 commit comments

Comments
 (0)