From 744a78f63b525651e90bcc90fdce5827dff781e1 Mon Sep 17 00:00:00 2001 From: A4-Tacks Date: Fri, 21 Jun 2024 22:35:02 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0IncludeRef=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.lock | 2 +- Cargo.toml | 2 +- README.md | 3 +++ src/lib.rs | 20 ++++++++++++++++++-- src/parser.rs | 1 + 5 files changed, 24 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 53195ce..4f7a460 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,7 +4,7 @@ version = 3 [[package]] name = "mtsyntax-plus" -version = "0.2.3" +version = "0.2.4" dependencies = [ "peg", ] diff --git a/Cargo.toml b/Cargo.toml index 22f4634..dea5eb6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 diff --git a/README.md b/README.md index eee0835..dba47b9 100644 --- a/README.md +++ b/README.md @@ -90,6 +90,9 @@ MT管理器语法文件强化语法, 主要作用就是可以自动展开regexp 使用`@name`来引用定义的正则, 使用`&name`来引用外部定义的正则, 并且可以使用`&name(count)`标注外部的正则拥有多少个高亮组, 以避免组匹配歪掉 +可以使用`&name(@)`来include引用, +有时我们不需要具体的颜色但是又不想算定义的正则拥有多少个组时很有用 + 每个匹配通过可选的加号连接, 比如`@foo + @bar`和`@foo @bar`都可以 在定义了匹配后, 可以跟上属性和颜色组, diff --git a/src/lib.rs b/src/lib.rs index 215f9b5..aaef279 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 { @@ -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})"), } } @@ -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()))?, }) } @@ -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()?; diff --git a/src/parser.rs b/src/parser.rs index 396bcaa..75ef7c4 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -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)) }