From a084bd0d30fcf0c94694a65b871c46be22795b65 Mon Sep 17 00:00:00 2001 From: A4-Tacks Date: Sun, 16 Jun 2024 17:54:43 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=99=90=E5=88=B6,=20?= =?UTF-8?q?=E9=98=B2=E6=AD=A2=E7=BB=99=E5=AE=9A=E6=95=B0=E5=AD=97=E8=BF=87?= =?UTF-8?q?=E5=A4=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/lib.rs | 21 ++++++++++++--------- src/main.rs | 5 +++-- src/parser.rs | 18 ++++++++++-------- 5 files changed, 27 insertions(+), 21 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ebea052..53195ce 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,7 +4,7 @@ version = 3 [[package]] name = "mtsyntax-plus" -version = "0.2.2" +version = "0.2.3" dependencies = [ "peg", ] diff --git a/Cargo.toml b/Cargo.toml index 2093c0d..22f4634 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mtsyntax-plus" -version = "0.2.2" +version = "0.2.3" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/src/lib.rs b/src/lib.rs index 8ce9df3..7192511 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -71,7 +71,7 @@ pub enum Expr<'a> { impl<'a> fmt::Display for Expr<'a> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { - Expr::Literal(lit, _) => f.write_str(&*lit), + Expr::Literal(lit, _) => f.write_str(lit), Expr::KwdsToRegex(kwds) => { write!(f, "keywordsToRegex(")?; if let Some(first) = kwds.first() { @@ -110,7 +110,7 @@ impl Expr<'_> { where F: FnMut(std::fmt::Arguments<'_>) -> io::Result<()>, C: Iterator>, { - Ok(match self { + match self { | &Expr::KwdsToRegex(_) => (), | &Expr::Ref(name) => { let rule = ctx.rule_map.get(name) @@ -135,7 +135,8 @@ impl Expr<'_> { ctx.current_color.set(cur_color + count); }, - }) + } + Ok(()) } } @@ -246,12 +247,13 @@ impl Pattern<'_> { ) -> Result<()> where F: FnMut(std::fmt::Arguments<'_>) -> io::Result<()>, { - Ok(match self { + match self { Pattern::Normal(data) => data.build(ctx, octx)?, Pattern::IncludePattern(name) => { octx.output(fa!("{{include: {name}}}"))?; }, - }) + } + Ok(()) } } @@ -320,7 +322,7 @@ pub struct OutputContext<'a, F = fn(fmt::Arguments<'_>) -> io::Result<()>> { output: F, } -impl<'a, F> OutputContext<'_, F> +impl OutputContext<'_, F> where F: FnMut(fmt::Arguments<'_>) -> io::Result<()>, { pub fn new(output: F) -> Self { @@ -407,10 +409,11 @@ where I: IntoIterator>, if let Some(Pattern::Normal(data)) = rule.pats.into_iter().next() { - if let Some(_) = ctx.rule_map - .insert((*rule.name).into(), data) + if ctx.rule_map + .insert(rule.name.into(), data) + .is_some() { - return Err(Error::RepeatDefineName((*rule.name).into())); + return Err(Error::RepeatDefineName(rule.name.into())); } } } diff --git a/src/main.rs b/src/main.rs index fa6dc9e..3d0719c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,8 +6,9 @@ use std::{ use mtsyntax_plus::{build, parser, BuildContext, Error, OutputContext}; fn main() -> io::Result<()> { - if args().count() != 1 { - eprintln!("Error: Invalid args, expected args count = 0\n"); + let count = args().count(); + if count != 1 { + eprintln!("Error: Invalid args count {count}, expected 1 args\n"); eprintln!("将MT管理器语法进行强化, 使其正则定义可以携带颜色"); eprintln!("input from stdin, output to stdout"); eprintln!("version: {}", env!("CARGO_PKG_VERSION")); diff --git a/src/parser.rs b/src/parser.rs index 6d681e7..18dcb5f 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -10,8 +10,7 @@ fn new_rule_data<'a>( mut colors: Vec>, ) -> RuleData<'a> { if let Some(first) = colors.first_mut() - .map(|color| color.take()) - .flatten() + .and_then(|color| color.take()) { colors.insert(1, first.into()); exprs.insert(0, Expr::Literal("/(/".into(), 1)); @@ -92,13 +91,16 @@ peg::parser!(grammar parser() for str { / expected!("regex") pub rule unum() -> u32 - = s:quiet!{$("0" / !"0" ['0'..='9']+)} - {? - s.parse().map_err(|_| { - "invalid number" - }) + = quiet!{ + s:$("0" / !"0" ['0'..='9']+) + {? + s.parse() + .ok() + .filter(|&n| n < 100000) + .ok_or("") + } } - / expected!("number") + / expected!("number(0..100000)") rule color() -> (u32, &'input str) = "$" n:unum() _ ":" _ name:string()