Skip to content

Commit

Permalink
1. added and_break macros which combine primary and post-parse operat…
Browse files Browse the repository at this point in the history
…ion.

2. added shorter form for break_search_macro ("break")
3. small fixes
  • Loading branch information
denisandroid committed May 16, 2024
1 parent c69efeb commit 35b37ee
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 30 deletions.
14 changes: 14 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# EditorConfig helps developers define and maintain consistent
# coding styles between different editors and IDEs
# http://editorconfig.org

root = true

[*.rs]
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = false
insert_final_newline = false
indent_style = tab
indent_size = 5
max_line_length = 80
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "include_tt"
version = "1.0.4"
version = "1.0.5"
edition = "2021"
authors = ["Denis Kotlyarov (Денис Котляров) <denis2005991@gmail.com>"]
repository = "https://github.com/clucompany/include_tt.git"
Expand Down
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ include_tt = "1.0.4"
```

and this to your source code:

```rust
use include_tt::include_tt;
```

## Example:
## Example

```rust
use include_tt::include_tt;
Expand Down Expand Up @@ -97,7 +98,8 @@ use std::fmt::Write;
See all
</a>

## License:
## License

This project has a dual license according to (LICENSE-MIT) and (LICENSE-APACHE-2-0).

<div align="left">
Expand All @@ -110,7 +112,7 @@ This project has a dual license according to (LICENSE-MIT) and (LICENSE-APACHE-2
</br></br></br>
</div>

### Apache License:
### Apache License
<div align="left">
<a href="./LICENSE_APACHE">
<img align="left" src="https://github.com/UlinProject/img/blob/main/block_220_100/apache2.png?raw=true" alt="apache2"/>
Expand All @@ -120,7 +122,7 @@ This project has a dual license according to (LICENSE-MIT) and (LICENSE-APACHE-2
</br></br></br></br>
</div>

### MIT License:
### MIT License
<div align="left">
<a href="./LICENSE_MIT">
<img align="left" src="https://github.com/UlinProject/img/blob/main/block_220_100/mit.png?raw=true" alt="mit"/>
Expand Down
4 changes: 2 additions & 2 deletions src/exprs/literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ impl Deref for ExprLit {
impl Debug for ExprLit {
#[inline(always)]
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
Debug::fmt(&self as &str, f)
Debug::fmt(self as &str, f)
}
}

impl Display for ExprLit {
#[inline(always)]
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
Display::fmt(&self as &str, f)
Display::fmt(self as &str, f)
}
}

Expand Down
39 changes: 18 additions & 21 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,17 +144,22 @@ fn search_include_and_replacegroup(
if punct.as_char() == '#' {
if let Some(m_ident) = iter.next() {
if let TokenTree2::Ident(ident) = m_ident {
let macro_fn: Option<fn(&Group) -> TreeResult<TokenTree2>> = {
let (add_auto_break, macro_fn): (bool, fn(&Group) -> TreeResult<TokenTree2>) = {
let str_ident = ident.to_string();

match str_ident.as_str() {
"include" | "include_tt" => Some(macro_rule_include::<IncludeTT>),
"include_and_fix_unknown_start_token" | "include_tt_and_fix_unknown_start_token" => Some(macro_rule_include::<IncludeTTAndFixUnkStartToken>),
"include" | "include_tt" => (false, macro_rule_include::<IncludeTT>),
"include_and_break" | "include_tt+break" => (true, macro_rule_include::<IncludeTT>),

"include_str" => Some(macro_rule_include::<IncludeStr>),
"include_arr" => Some(macro_rule_include::<IncludeArr>),
"include_and_fix_unknown_start_token" | "include_tt_and_fix_unknown_start_token" => (false, macro_rule_include::<IncludeTTAndFixUnkStartToken>),
"include_and_fix_unknown_start_token_and_break" | "include_tt_and_fix_unknown_start_token_and_break" => (true, macro_rule_include::<IncludeTTAndFixUnkStartToken>),

"break_search_macro" => {
"include_str" => (false, macro_rule_include::<IncludeStr>),
"include_str_and_break" => (true, macro_rule_include::<IncludeStr>),
"include_arr" => (false, macro_rule_include::<IncludeArr>),
"include_arr_and_break" => (true, macro_rule_include::<IncludeArr>),

"break" | "break_search_macro" => {
/*
Stop indexing after the given keyword. This saves resources.
*/
Expand All @@ -177,7 +182,9 @@ fn search_include_and_replacegroup(
}
},

_ => None,
_ => sg_err! {
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`, `include_and_break`, `include_tt_and_break`, `include_and_fix_unknown_start_token_and_break`, `include_tt_and_fix_unknown_start_token_and_break`, `include_str_and_break`, `include_arr_and_break`."
},
}
};

Expand All @@ -186,13 +193,6 @@ fn search_include_and_replacegroup(
if punct2.as_char() == '!' {
if let Some(m_group) = iter.next() {
if let TokenTree2::Group(group) = m_group {
let macro_fn = match macro_fn {
Some(a) => a,
None => sg_err! {
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`."
}
};

let result = ttry!( macro_fn(group) );

let nulltt = make_null_ttree();
Expand All @@ -202,19 +202,16 @@ fn search_include_and_replacegroup(
*m_punct2 = nulltt.clone();
*m_group = result;

continue 'sbegin;
match add_auto_break {
false => continue 'sbegin,
true => return SearchGroup::Break,
}
}
}
}
// autoskip
}
}

if macro_fn.is_some() { // The required macro was defined earlier, which means an error.
sg_err! {
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(...)`."
}
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/break.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fn test_empty_tt() {
The use of this keyword leaves the search for occurrences
needed for replacement.
*/
#break_search_macro;
#break;

stringify!(#include!("./tests/empty.tt"))
};
Expand Down
2 changes: 1 addition & 1 deletion tests/expr_lit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use include_tt::include_tt;
fn test_expr_lit() {
let str = include_tt! {
// File contains: `"123\"test"`
#include!("./tests/expr_lit.tt")
#include_and_break!("./tests/expr_lit.tt")
};

assert_eq!(str, "123\"test");
Expand Down

0 comments on commit 35b37ee

Please sign in to comment.