Skip to content

Commit

Permalink
添加IncludeRef功能
Browse files Browse the repository at this point in the history
  • Loading branch information
A4-Tacks committed Jun 21, 2024
1 parent 314384a commit 744a78f
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mtsyntax-plus"
version = "0.2.3"
version = "0.2.4"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ MT管理器语法文件强化语法, 主要作用就是可以自动展开regexp
使用`@name`来引用定义的正则, 使用`&name`来引用外部定义的正则,
并且可以使用`&name(count)`标注外部的正则拥有多少个高亮组, 以避免组匹配歪掉

可以使用`&name(@)`来include引用,
有时我们不需要具体的颜色但是又不想算定义的正则拥有多少个组时很有用

每个匹配通过可选的加号连接, 比如`@foo + @bar``@foo @bar`都可以

在定义了匹配后, 可以跟上属性和颜色组,
Expand Down
20 changes: 18 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ pub enum Expr<'a> {
/// Include extern regexp, and group count
/// e.g `&foo` `&foo(2)`
Include(Cow<'a, str>, u32),
/// Include ref, but not build colors
/// e.g `&foo(@)`
IncludeRef(&'a str),
}
impl<'a> fmt::Display for Expr<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand All @@ -85,7 +88,8 @@ impl<'a> fmt::Display for Expr<'a> {
}
write!(f, ")")
},
Expr::Ref(name) => write!(f, "include(\"{name}\")"),
Expr::Ref(name) | Expr::IncludeRef(name)
=> write!(f, "include(\"{name}\")"),
Expr::Include(name, _) => write!(f, "include({name})"),
}
}
Expand All @@ -100,7 +104,8 @@ impl Expr<'_> {
| &Expr::Include(_, c)
| &Expr::Literal(_, c) => c,
| &Expr::KwdsToRegex(_) => 0,
| &Expr::Ref(name) => f(name)
| &Expr::Ref(name)
| &Expr::IncludeRef(name) => f(name)
.ok_or_else(|| Error::UndefineRef(name.into()))?,
})
}
Expand All @@ -125,6 +130,17 @@ impl Expr<'_> {
})?;
rule.build_colors(octx, ctx)?
},
| &Expr::IncludeRef(name) => {
let rule = ctx.rule_map.get(name)
.ok_or_else(|| Error::UndefineRef(name.into()))
.and_then(|data| {
data.regexp.then_some(data)
.ok_or_else(|| Error::RefNotARegexp(name.into()))
})?;

let cur_color = ctx.current_color.get();
ctx.current_color.set(cur_color + rule.group_count.unwrap());
},
| &Expr::ColorGroup(ref color) => {
let id = ctx.current_color.get();
octx.newline()?;
Expand Down
1 change: 1 addition & 0 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ peg::parser!(grammar parser() for str {
Expr::KwdsToRegex(s)
}
/ "@" i:ident() { Expr::Ref(i) }
/ "&" i:ident() "(@)" { Expr::IncludeRef(i) }
/ "&" n:eident()
c:("(" n:unum() ")" { n })?
{ Expr::Include(n, c.unwrap_or(0)) }
Expand Down

0 comments on commit 744a78f

Please sign in to comment.