Skip to content

Commit

Permalink
CHG: Move ScanError to its own module.
Browse files Browse the repository at this point in the history
  • Loading branch information
krisvanrens committed Aug 10, 2023
1 parent 431d7fc commit b9ab22d
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 63 deletions.
3 changes: 3 additions & 0 deletions nexus-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ pub mod nxs_scanner {
/// Scanner/lexer for Nexus.
pub mod scanner;

/// Scanner error representation
pub mod scan_error;

/// Line of source code.
pub mod source_line;

Expand Down
66 changes: 66 additions & 0 deletions nexus-rs/src/nxs_scanner/scan_error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use super::cursor::Cursor;
use super::source_line::SourceLine;
use std::fmt::Display;
use thiserror::Error;

/// Scanning/lexing error representation.
#[derive(Error, Debug)]
pub enum ScanErrorKind {
#[error("malformed string literal")]
MalformedString,

#[error("failed to parse number '{0}'")]
NumberParseError(String),

#[error("failed to parse word")]
WordParseError,

#[error("unexpected character")]
UnexpectedCharacter,

#[error("unterminated string")]
UnterminatedString,
}

#[derive(Error, Debug)]
pub struct ScanError {
line: SourceLine,
kind: ScanErrorKind,
char_index: usize,
}

impl Display for ScanError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
// TODO: Simplify?
let prefix_fill = " ".repeat(
self.line
.number
.map_or("".to_owned(), |n| n.to_string())
.len()
+ 2,
); // +2 for spaces.
let char_fill = " ".repeat(self.char_index);
f.write_fmt(format_args!(
"{}|\n {} | {}\n{}| {}{}\n{}| error: {}\n{}|",
prefix_fill,
self.line.number.map_or("".to_owned(), |n| n.to_string()),
self.line.line,
prefix_fill,
char_fill,
"^",
prefix_fill,
self.kind,
prefix_fill,
))
}
}

impl ScanError {
pub fn new(line: SourceLine, kind: ScanErrorKind, cursor: &Cursor) -> Self {
ScanError {
line,
kind,
char_index: cursor.index(),
}
}
}
64 changes: 1 addition & 63 deletions nexus-rs/src/nxs_scanner/scanner.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use super::cursor::Cursor;
use super::scan_error::{ScanError, ScanErrorKind};
use super::source_line::SourceLine;
use crate::token::{Token, Tokens};
use lazy_static::lazy_static;
use std::collections::HashMap;
use std::fmt::Display;
use thiserror::Error;

#[cfg(test)]
use pretty_assertions::assert_eq;
Expand All @@ -20,67 +19,6 @@ pub struct Scanner {
comment_: bool, //<! Indicates multiline comment state.
}

/// Scanning/lexing error representation.
#[derive(Error, Debug)]
pub enum ScanErrorKind {
#[error("malformed string literal")]
MalformedString,

#[error("failed to parse number '{0}'")]
NumberParseError(String),

#[error("failed to parse word")]
WordParseError,

#[error("unexpected character")]
UnexpectedCharacter,

#[error("unterminated string")]
UnterminatedString,
}

#[derive(Error, Debug)]
pub struct ScanError {
line: SourceLine,
kind: ScanErrorKind,
char_index: usize,
}

impl Display for ScanError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let prefix_fill = " ".repeat(
self.line
.number
.map_or("".to_owned(), |n| n.to_string())
.len()
+ 2,
); // +2 for spaces.
let char_fill = " ".repeat(self.char_index);
f.write_fmt(format_args!(
"{}|\n {} | {}\n{}| {}{}\n{}| error: {}\n{}|",
prefix_fill,
self.line.number.map_or("".to_owned(), |n| n.to_string()),
self.line.line,
prefix_fill,
char_fill,
"^",
prefix_fill,
self.kind,
prefix_fill,
))
}
}

impl ScanError {
fn new(line: SourceLine, kind: ScanErrorKind, cursor: &Cursor) -> Self {
ScanError {
line,
kind,
char_index: cursor.index(),
}
}
}

impl Scanner {
/// Construct a new scanner.
pub fn new() -> Self {
Expand Down

0 comments on commit b9ab22d

Please sign in to comment.