all lex errors have a position now

main
Jordan Orelli 10 months ago
parent 1d504ff36d
commit 965cb45437

@ -207,7 +207,7 @@ impl Editor {
/// reads one command from the editor. This function blocks until the user has entered an
/// entire command.
pub fn read_command(&mut self, _history: &mut History) -> Result<Option<String>> {
pub fn read_command(&mut self, history: &mut History) -> Result<Option<String>> {
use input::Event::*;
loop {
@ -266,6 +266,7 @@ impl Editor {
match esc {
Left => self.back(1)?,
Right => self.forward(1)?,
Up => self.prev_history()?,
Home => self.seek_left()?,
End => self.seek_right()?,
esc => debug!(" Ignored escape: {esc:?}"),
@ -422,4 +423,8 @@ impl Editor {
self.display.reset()?;
Ok(())
}
fn prev_history(&mut self) -> Result<()> {
Ok(())
}
}

@ -1,4 +1,7 @@
use crate::{lex::Token, topo::Glyph};
use crate::{
lex::Token,
topo::{Glyph, Position},
};
use std::io;
use thiserror::Error;
use windows::Win32::Foundation::{GetLastError, BOOL};
@ -36,21 +39,32 @@ pub enum LexError {
UnexpectedCharacterInGlob(Glyph),
#[error("unexpected eof")]
UnexpectedEOF,
UnexpectedEOF(Position),
#[error("not yet supported: {0}")]
NotYetSupported(String),
#[error("not yet supported: {1}")]
NotYetSupported(Position, String),
}
impl LexError {
pub fn not_yet(msg: &str) -> Self {
LexError::NotYetSupported(msg.to_string())
pub fn not_yet(p: Position, msg: &str) -> Self {
LexError::NotYetSupported(p, msg.to_string())
}
fn position(&self) -> Position {
use LexError::*;
match self {
UnexpectedCharacter(g) => g.position,
UnexpectedCharacterInBareString(g) => g.position,
UnexpectedCharacterInGlob(g) => g.position,
UnexpectedEOF(p) => *p,
NotYetSupported(p, _) => *p,
}
}
}
#[derive(Debug, Error)]
pub enum ParseError {
#[error("lex error")]
#[error("lex error at line {line}, column {col}", line=.0.position().line, col=.0.position().column)]
LexError(#[from] LexError),
#[error("Unexpected Token: {0:?}")]

@ -1,7 +1,13 @@
pub struct History {}
pub struct History {
events: Vec<String>,
}
impl History {
pub fn new() -> Self {
Self {}
Self { events: Vec::new() }
}
pub fn add<S: Into<String>>(&mut self, event: S) {
self.events.push(event.into());
}
}

@ -154,7 +154,7 @@ impl<'text> Tokenizer<'text> {
}
if progress.is_empty() {
Err(LexError::UnexpectedEOF)
Err(LexError::UnexpectedEOF(self.source.next_position))
} else {
Ok(Token::Word(progress.into()))
}
@ -175,22 +175,31 @@ impl<'text> Tokenizer<'text> {
}
if progress.is_empty() {
Err(LexError::UnexpectedEOF)
Err(LexError::UnexpectedEOF(self.source.next_position))
} else {
Ok(Token::Glob(progress.into()))
}
}
fn lex_raw_string(&mut self, _progress: Vec<Glyph>) -> Result<Token, LexError> {
Err(LexError::not_yet("raw strings not done yet"))
Err(LexError::not_yet(
self.source.next_position,
"raw strings not done yet",
))
}
fn lex_interp_string(&mut self, _progress: Vec<Glyph>) -> Result<Token, LexError> {
Err(LexError::not_yet("interpreted strings not done yet"))
Err(LexError::not_yet(
self.source.next_position,
"interpreted strings not done yet",
))
}
fn lex_var(&mut self, _progress: Vec<Glyph>) -> Result<Token, LexError> {
Err(LexError::not_yet("variables are not done yet"))
Err(LexError::not_yet(
self.source.next_position,
"variables are not done yet",
))
}
}

@ -47,6 +47,12 @@ impl fmt::Debug for Position {
}
}
impl fmt::Display for Position {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
(self as &dyn fmt::Debug).fmt(f)
}
}
/// A Glyph represents a character occurring at some specific position in some input text.
#[derive(PartialEq, Clone)]
pub struct Glyph {
@ -97,7 +103,7 @@ pub struct Glyphs<'text> {
source: Chars<'text>,
/// the position of the next character in the source text
next_position: Position,
pub(crate) next_position: Position,
/// the number of bytes we've read from our input document so far
bytes_read: u64,
@ -153,7 +159,8 @@ impl<'text> Glyphs<'text> {
/// takes the next character from our input text
pub fn pop(&mut self) -> Result<Glyph, LexError> {
self.next().ok_or(LexError::UnexpectedEOF)
self.next()
.ok_or(LexError::UnexpectedEOF(self.next_position))
}
/// returns a reference to a character in our lookahead buffer at a given position. This allows

Loading…
Cancel
Save