2023-10-17 03:51:59 +00:00
|
|
|
//! This example grabs input from stdin, lexes it, parses it, and prints the AST
|
|
|
|
#![allow(unused_imports)]
|
|
|
|
use conlang::{lexer::Lexer, parser::Parser, pretty_printer::PrettyPrintable, token::Token};
|
|
|
|
use std::{
|
|
|
|
error::Error,
|
2023-10-21 17:24:52 +00:00
|
|
|
io::{stdin, stdout, IsTerminal, Read, Write},
|
2023-10-17 03:51:59 +00:00
|
|
|
path::{Path, PathBuf},
|
|
|
|
};
|
|
|
|
|
|
|
|
fn main() -> Result<(), Box<dyn Error>> {
|
|
|
|
let conf = Config::new();
|
|
|
|
if conf.paths.is_empty() {
|
|
|
|
take_stdin()?;
|
|
|
|
} else {
|
|
|
|
for path in conf.paths.iter().map(PathBuf::as_path) {
|
|
|
|
parse(&std::fs::read_to_string(path)?, Some(path));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Config {
|
|
|
|
paths: Vec<PathBuf>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Config {
|
|
|
|
fn new() -> Self {
|
|
|
|
Config { paths: std::env::args().skip(1).map(PathBuf::from).collect() }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn take_stdin() -> Result<(), Box<dyn Error>> {
|
2023-10-21 17:24:52 +00:00
|
|
|
const PROMPT: &str = "> ";
|
2023-10-17 03:51:59 +00:00
|
|
|
if stdin().is_terminal() {
|
2023-10-21 17:24:52 +00:00
|
|
|
print!("{PROMPT}");
|
|
|
|
stdout().flush()?;
|
2023-10-17 03:51:59 +00:00
|
|
|
for line in stdin().lines() {
|
2023-10-21 17:24:52 +00:00
|
|
|
let line = line?;
|
|
|
|
if !line.is_empty() {
|
|
|
|
parse(&line, None);
|
|
|
|
println!();
|
|
|
|
}
|
|
|
|
print!("{PROMPT}");
|
|
|
|
stdout().flush()?;
|
2023-10-17 03:51:59 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
parse(&std::io::read_to_string(stdin())?, None)
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse(file: &str, path: Option<&Path>) {
|
2023-10-20 20:33:02 +00:00
|
|
|
use conlang::parser::error::Error;
|
2023-10-17 03:51:59 +00:00
|
|
|
match Parser::from(Lexer::new(file)).parse() {
|
|
|
|
Ok(ast) => ast.print(),
|
2023-10-21 17:24:52 +00:00
|
|
|
Err(e) if e.start().is_some() => print!("{:?}:{}", path.unwrap_or(Path::new("-")), e),
|
|
|
|
Err(e) => print!("{e}"),
|
2023-10-17 03:51:59 +00:00
|
|
|
}
|
|
|
|
println!();
|
|
|
|
}
|