main: ty parser test mode, visually distinct prompt
This commit is contained in:
48
src/main.rs
48
src/main.rs
@@ -1,6 +1,6 @@
|
|||||||
//! Tests the lexer
|
//! Tests the lexer
|
||||||
use doughlang::{
|
use doughlang::{
|
||||||
ast::{Anno, Pat},
|
ast::{Anno, Pat, Ty},
|
||||||
parser::PPrec,
|
parser::PPrec,
|
||||||
};
|
};
|
||||||
#[allow(unused_imports)]
|
#[allow(unused_imports)]
|
||||||
@@ -22,7 +22,7 @@ use std::{
|
|||||||
|
|
||||||
fn main() -> Result<(), Box<dyn Error>> {
|
fn main() -> Result<(), Box<dyn Error>> {
|
||||||
if stdin().is_terminal() {
|
if stdin().is_terminal() {
|
||||||
read_and("\x1b[32m", " >", "?>", |line| match line.trim_end() {
|
read_and("\x1b[32m", ".>", " >", |line| match line.trim_end() {
|
||||||
"" => Ok(Response::Continue),
|
"" => Ok(Response::Continue),
|
||||||
"exit" => Ok(Response::Break),
|
"exit" => Ok(Response::Break),
|
||||||
"clear" => {
|
"clear" => {
|
||||||
@@ -41,16 +41,21 @@ fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
pats()?;
|
pats()?;
|
||||||
Ok(Response::Deny)
|
Ok(Response::Deny)
|
||||||
}
|
}
|
||||||
|
"ty" => {
|
||||||
|
tys()?;
|
||||||
|
Ok(Response::Deny)
|
||||||
|
}
|
||||||
"macro" => {
|
"macro" => {
|
||||||
if let Err(e) = subst() {
|
if let Err(e) = subst() {
|
||||||
println!("\x1b[31m{e}\x1b[0m");
|
println!("\x1b[31m{e}\x1b[0m");
|
||||||
}
|
}
|
||||||
Ok(Response::Deny)
|
Ok(Response::Deny)
|
||||||
}
|
}
|
||||||
_ => {
|
_ if line.ends_with("\n\n") => {
|
||||||
parse(line);
|
parse(line);
|
||||||
Ok(Response::Accept)
|
Ok(Response::Accept)
|
||||||
}
|
}
|
||||||
|
_ => Ok(Response::Continue),
|
||||||
})?;
|
})?;
|
||||||
} else {
|
} else {
|
||||||
let doc = std::io::read_to_string(stdin())?;
|
let doc = std::io::read_to_string(stdin())?;
|
||||||
@@ -85,23 +90,24 @@ fn lex() -> Result<(), Box<dyn Error>> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn exprs() -> Result<(), Box<dyn Error>> {
|
fn exprs() -> Result<(), Box<dyn Error>> {
|
||||||
read_and("\x1b[93m", " >", "?>", |line| {
|
read_and("\x1b[93m", ".>", " >", |line| {
|
||||||
let mut parser = Parser::new(Lexer::new(line));
|
let mut parser = Parser::new(Lexer::new(line));
|
||||||
if line.trim().is_empty() {
|
if line.trim().is_empty() {
|
||||||
return Ok(Response::Break);
|
return Ok(Response::Break);
|
||||||
}
|
}
|
||||||
loop {
|
for idx in 0.. {
|
||||||
match parser.parse::<Anno<Expr>>(0) {
|
match parser.parse::<Anno<Expr>>(0) {
|
||||||
Err(ParseError::FromLexer(LexError { res: "EOF", .. })) => {
|
Err(ParseError::FromLexer(LexError { res: "EOF", .. })) => {
|
||||||
break Ok(Response::Accept);
|
return Ok(Response::Accept);
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
println!("\x1b[31m{e}\x1b[0m");
|
println!("\x1b[31m{e}\x1b[0m");
|
||||||
break Ok(Response::Deny);
|
return Ok(Response::Deny);
|
||||||
}
|
}
|
||||||
Ok(v) => println!("{v}\n{v:?}"),
|
Ok(v) => println!("{idx}: {v}\n{v:#?}"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Ok(Response::Accept)
|
||||||
})?;
|
})?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@@ -128,6 +134,28 @@ fn pats() -> Result<(), Box<dyn Error>> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn tys() -> Result<(), Box<dyn Error>> {
|
||||||
|
read_and("\x1b[94m", ".>", " >", |line| {
|
||||||
|
let mut parser = Parser::new(Lexer::new(line));
|
||||||
|
if line.trim().is_empty() {
|
||||||
|
return Ok(Response::Break);
|
||||||
|
}
|
||||||
|
loop {
|
||||||
|
match parser.parse::<Ty>(()) {
|
||||||
|
Err(ParseError::FromLexer(LexError { res: "EOF", .. })) => {
|
||||||
|
break Ok(Response::Accept);
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
println!("\x1b[31m{e}\x1b[0m");
|
||||||
|
break Ok(Response::Deny);
|
||||||
|
}
|
||||||
|
Ok(v) => println!("{v}\n{v:#?}"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
fn subst() -> Result<(), Box<dyn Error>> {
|
fn subst() -> Result<(), Box<dyn Error>> {
|
||||||
let mut rl = repline::Repline::new("\x1b[35mexp", " >", "?>");
|
let mut rl = repline::Repline::new("\x1b[35mexp", " >", "?>");
|
||||||
let exp = rl.read()?;
|
let exp = rl.read()?;
|
||||||
@@ -177,7 +205,7 @@ fn subst() -> Result<(), Box<dyn Error>> {
|
|||||||
|
|
||||||
fn parse(document: &str) {
|
fn parse(document: &str) {
|
||||||
let mut parser = Parser::new(Lexer::new(document));
|
let mut parser = Parser::new(Lexer::new(document));
|
||||||
loop {
|
for idx in 0.. {
|
||||||
match parser.parse::<Expr>(0) {
|
match parser.parse::<Expr>(0) {
|
||||||
Err(ParseError::FromLexer(LexError { res: "EOF", .. })) => break,
|
Err(ParseError::FromLexer(LexError { res: "EOF", .. })) => break,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
@@ -185,7 +213,7 @@ fn parse(document: &str) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
Ok(v) => {
|
Ok(v) => {
|
||||||
println!("{v}");
|
println!("\x1b[{}m{v}", (idx + 5) % 6 + 31);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user