3个版本 (破坏性)
0.3.0 | 2024年1月3日 |
---|---|
0.2.0 | 2023年12月24日 |
0.1.0 | 2023年8月22日 |
#982 在 文本处理
6KB
98 行
read_chars
这是我为我的编程语言项目Rouge中的新词法分析器所创建的一个简单的库。您可以在MIT许可证下自由使用它。
use read_chars::ReadChars;
use std::convert::From;
use std::io;
use std::fs::File;
fn main() -> io::Result<()> {
let chars = ReadChars::from(File::open("test.txt")?);
let max_nesting = chars.filter(|r| match r { Ok(c) if c == '(' || c == ')' => Some(c), _ => None })
.scan(0, |acc, chr| if chr == '(' { acc + 1 } else { acc - 1 })
.max()
.ok_or_else(|| io::Error::from(io::ErrorKind::InvalidData))?;
println!("Max parentheses nesting in file 'text,txt' is {max_nesting}.");
Ok(())
}
lib.rs
:
[ReadChars]是一个简单的迭代器crate,用于将I/O读者的字节转换为字符,以供像手写词法分析器这样的工具使用。