AshtonSnapp / read_chars

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

read_chars

This is just a simple library I made for use in a new lexer for my programming language project Rouge. You are free to use it how you wish under the MIT license.

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(())
}

About


Languages

Language:Rust 100.0%