conlang: add toy program to interact with the tokenizer

This commit is contained in:
John 2023-09-25 18:06:03 -05:00
parent 09f22d6bf3
commit 46e72e4889

View File

@ -0,0 +1,18 @@
//! This example grabs input from stdin, lexes it, and prints which lexer rules matched
#![allow(unused_imports)]
use conlang::lexer::Lexer;
use std::{io::stdin, error::Error};
fn main() -> Result<(), Box<dyn Error>>{
// get input from stdin
for line in stdin().lines() {
let line = line?;
// lex the line
for func in [Lexer::line_comment, Lexer::block_comment, Lexer::shebang_comment, Lexer::identifier, Lexer::integer, Lexer::float, Lexer::string] {
if let Some(token) = func(&mut Lexer::new(&line)) {
println!("{:?}: {}", token, &line[token.range()])
}
}
}
Ok(())
}