Skip to content

Commit

Permalink
增加限制, 防止给定数字过大
Browse files Browse the repository at this point in the history
  • Loading branch information
A4-Tacks committed Jun 16, 2024
1 parent 2015005 commit a084bd0
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 21 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.2"
version = "0.2.3"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
21 changes: 12 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -110,7 +110,7 @@ impl Expr<'_> {
where F: FnMut(std::fmt::Arguments<'_>) -> io::Result<()>,
C: Iterator<Item = Option<&'a str>>,
{
Ok(match self {
match self {
| &Expr::KwdsToRegex(_) => (),
| &Expr::Ref(name) => {
let rule = ctx.rule_map.get(name)
Expand All @@ -135,7 +135,8 @@ impl Expr<'_> {

ctx.current_color.set(cur_color + count);
},
})
}
Ok(())
}
}

Expand Down Expand Up @@ -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(())
}
}

Expand Down Expand Up @@ -320,7 +322,7 @@ pub struct OutputContext<'a, F = fn(fmt::Arguments<'_>) -> io::Result<()>> {
output: F,
}

impl<'a, F> OutputContext<'_, F>
impl<F> OutputContext<'_, F>
where F: FnMut(fmt::Arguments<'_>) -> io::Result<()>,
{
pub fn new(output: F) -> Self {
Expand Down Expand Up @@ -407,10 +409,11 @@ where I: IntoIterator<Item = Rule<'a>>,
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()));
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down
18 changes: 10 additions & 8 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ fn new_rule_data<'a>(
mut colors: Vec<Option<&'a str>>,
) -> 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));
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit a084bd0

Please sign in to comment.