Skip to content

Commit

Permalink
errors: LineReport -> TokenReport
Browse files Browse the repository at this point in the history
  • Loading branch information
rpitasky committed Sep 8, 2024
1 parent fca75d4 commit 49d75a4
Show file tree
Hide file tree
Showing 28 changed files with 93 additions and 91 deletions.
24 changes: 12 additions & 12 deletions ti-basic-optimizer/src/error_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use titokens::tokenizer::TokenBoundaries;
macro_rules! next_or_err {
($tokens: ident) => {
$tokens.next().ok_or_else(|| {
(crate::error_reporting::LineReport::new(
(crate::error_reporting::TokenReport::new(
$tokens.current_position() - 2,
"Unexpected end of input.",
None,
Expand All @@ -15,7 +15,7 @@ macro_rules! next_or_err {

($tokens: ident, $message: literal) => {
$tokens.next().ok_or_else(|| {
(crate::error_reporting::LineReport::new(
(crate::error_reporting::TokenReport::new(
$tokens.current_position() - 2,
$message,
None,
Expand All @@ -26,10 +26,10 @@ macro_rules! next_or_err {
}

macro_rules! expect_tok {
($tokens: ident, $token: expr, $token_name: literal) => {
($tokens: ident, $token: pat, $token_name: literal) => {
crate::error_reporting::next_or_err!($tokens).and_then(|tok| {
if tok != ($token) {
Err(crate::error_reporting::LineReport::new(
if !matches!(tok, $token) {
Err(crate::error_reporting::TokenReport::new(
$tokens.current_position() - 1,
concat!("Expected token \"", $token_name, "\"."),
Some("Add the token."),
Expand All @@ -43,7 +43,7 @@ macro_rules! expect_tok {
($tokens: ident, $token: expr, $error: literal, $help: literal) => {
crate::error_reporting::next_or_err!($tokens).and_then(|tok| {
if tok != ($token) {
Err(crate::error_reporting::LineReport::new(
Err(crate::error_reporting::TokenReport::new(
$tokens.current_position() - 1,
$error,
Some($help),
Expand All @@ -58,7 +58,7 @@ macro_rules! expect_tok {
macro_rules! expect_some {
($option: expr, $tokens: ident, $expected_kind: literal) => {
$option.ok_or_else(|| {
crate::error_reporting::LineReport::new(
crate::error_reporting::TokenReport::new(
$tokens.current_position() - 1,
concat!("Expected to find ", $expected_kind, "."),
None,
Expand All @@ -72,7 +72,7 @@ macro_rules! expect_some {

($option: expr, $tokens: ident, $expected_kind: literal, $help: literal) => {
$option.ok_or_else(|| {
crate::error_reporting::LineReport::new(
crate::error_reporting::TokenReport::new(
$tokens.current_position() - 1,
concat!("Expected to find ", $expected_kind, "."),
None,
Expand All @@ -83,7 +83,7 @@ macro_rules! expect_some {

($option: expr, $tokens: ident, $ofs: expr, $expected_kind: literal, $help: literal) => {
$option.ok_or_else(|| {
crate::error_reporting::LineReport::new(
crate::error_reporting::TokenReport::new(
$tokens.current_position() - $ofs,
concat!("Expected to find ", $expected_kind, "."),
None,
Expand Down Expand Up @@ -113,7 +113,7 @@ impl LabelKind {
}

#[derive(Debug, Clone)]
pub struct LineReport {
pub struct TokenReport {
location: usize,
message: String,
suggestion: Option<String>,
Expand All @@ -122,10 +122,10 @@ pub struct LineReport {
labels: Vec<(LabelKind, String)>,
}

impl LineReport {
impl TokenReport {
#[must_use]
pub fn new(location: usize, message: &str, suggestion: Option<&str>) -> Self {
LineReport {
TokenReport {
location,
message: message.to_string(),
suggestion: suggestion.map(|x| x.to_string()),
Expand Down
6 changes: 3 additions & 3 deletions ti-basic-optimizer/src/parse/commands/control_flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mod menu;

pub use {for_loop::ForLoop, isds::IsDs, menu::Menu};

use crate::error_reporting::{expect_some, next_or_err, LineReport};
use crate::error_reporting::{expect_some, next_or_err, TokenReport};
use crate::parse::{expression::Expression, Parse, Reconstruct};
use crate::Config;
use std::iter::once;
Expand Down Expand Up @@ -66,7 +66,7 @@ impl LabelName {
}

impl Parse for LabelName {
fn parse(token: Token, more: &mut Tokens) -> Result<Option<Self>, LineReport> {
fn parse(token: Token, more: &mut Tokens) -> Result<Option<Self>, TokenReport> {
if !token.is_alphanumeric() {
return Ok(None);
}
Expand Down Expand Up @@ -115,7 +115,7 @@ pub enum ControlFlow {

impl Parse for ControlFlow {
#[rustfmt::skip]
fn parse(token: Token, more: &mut Tokens) -> Result<Option<Self>, LineReport> {
fn parse(token: Token, more: &mut Tokens) -> Result<Option<Self>, TokenReport> {
use ControlFlow as CF;
use Expression as Expr;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::error_reporting::{expect_some, expect_tok, next_or_err, LineReport};
use crate::error_reporting::{expect_some, expect_tok, next_or_err, TokenReport};
use crate::parse::expression::Expression;
use crate::parse::{Parse, Reconstruct};
use crate::Config;
Expand All @@ -16,7 +16,7 @@ pub struct ForLoop {
}

impl Parse for ForLoop {
fn parse(token: Token, more: &mut Tokens) -> Result<Option<Self>, LineReport> {
fn parse(token: Token, more: &mut Tokens) -> Result<Option<Self>, TokenReport> {
if token != Token::OneByte(0xD3) {
return Ok(None);
}
Expand Down
4 changes: 2 additions & 2 deletions ti-basic-optimizer/src/parse/commands/control_flow/isds.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::error_reporting::{expect_some, expect_tok, next_or_err, LineReport};
use crate::error_reporting::{expect_some, expect_tok, next_or_err, TokenReport};
use crate::parse::{components::NumericVarName, expression::Expression, Parse, Reconstruct};

use crate::Config;
Expand All @@ -12,7 +12,7 @@ pub struct IsDs {
}

impl Parse for IsDs {
fn parse(token: Token, more: &mut Tokens) -> Result<Option<Self>, LineReport> {
fn parse(token: Token, more: &mut Tokens) -> Result<Option<Self>, TokenReport> {
if token == Token::OneByte(0xDA) || token == Token::OneByte(0xDB) {
let variable = expect_some!(
NumericVarName::parse(next_or_err!(more)?, more)?,
Expand Down
6 changes: 3 additions & 3 deletions ti-basic-optimizer/src/parse/commands/control_flow/menu.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::error_reporting::{expect_some, expect_tok, next_or_err, LineReport};
use crate::error_reporting::{expect_some, expect_tok, next_or_err, TokenReport};
use crate::parse::{commands::control_flow::LabelName, expression::Expression, Parse, Reconstruct};
use crate::Config;
use std::iter::once;
Expand All @@ -12,7 +12,7 @@ pub struct Menu {
}

impl Parse for Menu {
fn parse(token: Token, more: &mut Tokens) -> Result<Option<Self>, LineReport> {
fn parse(token: Token, more: &mut Tokens) -> Result<Option<Self>, TokenReport> {
if token != Token::OneByte(0xE6) {
return Ok(None);
}
Expand Down Expand Up @@ -60,7 +60,7 @@ impl Parse for Menu {

Some(Token::OneByte(0x3E | 0x3F)) | None => break, // :, \n, EOF

Some(_) => Err(LineReport::new(
Some(_) => Err(TokenReport::new(
more.current_position() - 1,
"Unexpected character in Menu(",
Some("perhaps it's unimplemented?"),
Expand Down
6 changes: 4 additions & 2 deletions ti-basic-optimizer/src/parse/commands/delvar_chain.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
use crate::error_reporting::{next_or_err, LineReport};
use crate::error_reporting::{next_or_err, TokenReport};
use crate::parse::{commands::Command, components::DelVarTarget, Parse, Reconstruct};
use crate::Config;
use std::iter::once;
use titokens::{Token, Tokens};

/// `DelVar` statements do not require a trailing newline, and so a series of `deletions` can be
/// chained back-to-back.
#[derive(Clone, Debug)]
pub struct DelVarChain {
pub deletions: Vec<DelVarTarget>,
pub valence: Option<Box<Command>>,
}

impl Parse for DelVarChain {
fn parse(token: Token, more: &mut Tokens) -> Result<Option<Self>, LineReport> {
fn parse(token: Token, more: &mut Tokens) -> Result<Option<Self>, TokenReport> {
if token != Token::TwoByte(0xBB, 0x54) {
return Ok(None);
}
Expand Down
6 changes: 3 additions & 3 deletions ti-basic-optimizer/src/parse/commands/generic.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use itertools::Itertools;

use crate::error_reporting::LineReport;
use crate::error_reporting::TokenReport;
use crate::parse::expression::Expression;
use crate::parse::{Parse, Reconstruct};
use crate::Config;
Expand All @@ -13,7 +13,7 @@ pub struct Generic {
}

impl Parse for Generic {
fn parse(token: Token, more: &mut Tokens) -> Result<Option<Self>, LineReport> {
fn parse(token: Token, more: &mut Tokens) -> Result<Option<Self>, TokenReport> {
if !Generic::recognize(token) {
return Ok(None);
}
Expand Down Expand Up @@ -44,7 +44,7 @@ impl Parse for Generic {
}
Some(Token::OneByte(0x3E | 0x3F)) | None => break, // :, \n, EOF

Some(_) => Err(LineReport::new(
Some(_) => Err(TokenReport::new(
more.current_position() - 1,
"Unexpected character in command invocation",
Some("perhaps it's unimplemented?"),
Expand Down
4 changes: 2 additions & 2 deletions ti-basic-optimizer/src/parse/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub use setupeditor::SetUpEditor;
pub use generic::Generic;
use std::iter::once;

use crate::error_reporting::{expect_some, next_or_err, LineReport};
use crate::error_reporting::{expect_some, next_or_err, TokenReport};
use crate::parse::components::StoreTarget;
use crate::parse::{expression::Expression, Parse, Reconstruct};
use crate::Config;
Expand All @@ -31,7 +31,7 @@ pub enum Command {

impl Parse for Command {
#[allow(unused_parens)]
fn parse(token: Token, more: &mut Tokens) -> Result<Option<Self>, LineReport> {
fn parse(token: Token, more: &mut Tokens) -> Result<Option<Self>, TokenReport> {
if let Some(cmd) = Generic::parse(token, more)?.map(Command::Generic) {
Ok(Some(cmd))
} else if let Some(cmd) = ControlFlow::parse(token, more)?.map(Command::ControlFlow) {
Expand Down
8 changes: 4 additions & 4 deletions ti-basic-optimizer/src/parse/commands/prgm.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::error_reporting::LineReport;
use crate::error_reporting::TokenReport;
use crate::parse::{Parse, Reconstruct};
use crate::Config;
use std::iter::once;
Expand All @@ -10,7 +10,7 @@ pub struct ProgramName {
}

impl Parse for ProgramName {
fn parse(token: Token, more: &mut Tokens) -> Result<Option<Self>, LineReport> {
fn parse(token: Token, more: &mut Tokens) -> Result<Option<Self>, TokenReport> {
if token != Token::OneByte(0x5F) {
return Ok(None);
}
Expand All @@ -23,7 +23,7 @@ impl Parse for ProgramName {
|| (name.is_empty() && token.is_alphanumeric())
{
if name.len() > 8 {
Err(LineReport::new(
Err(TokenReport::new(
start_position,
"Program name has too many characters (max 8)",
None,
Expand All @@ -43,7 +43,7 @@ impl Parse for ProgramName {
}

if name.is_empty() {
Err(LineReport::new(
Err(TokenReport::new(
start_position,
"Expected a program name.",
Some("Program names start with a letter A-θ."),
Expand Down
8 changes: 4 additions & 4 deletions ti-basic-optimizer/src/parse/commands/setupeditor.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::error_reporting::LineReport;
use crate::error_reporting::TokenReport;
use crate::parse::components::{ListName, DEFAULT_LISTS};
use crate::parse::{Parse, Reconstruct};
use titokens::{Token, Tokens};
Expand All @@ -12,7 +12,7 @@ pub struct SetUpEditor {
}

impl Parse for SetUpEditor {
fn parse(token: Token, more: &mut Tokens) -> Result<Option<Self>, LineReport> {
fn parse(token: Token, more: &mut Tokens) -> Result<Option<Self>, TokenReport> {
if token != Token::TwoByte(0xBB, 0x4A) {
return Ok(None);
}
Expand All @@ -29,7 +29,7 @@ impl Parse for SetUpEditor {
if let Some(name) = ListName::parse_custom_name(more)? {
lists.push(name);
} else {
Err(LineReport::new(
Err(TokenReport::new(
more.current_position(),
"Expected a list name",
None,
Expand All @@ -44,7 +44,7 @@ impl Parse for SetUpEditor {
}
Some(Token::OneByte(0x3E | 0x3F)) | None => break, // :, \n, EOF

Some(_) => Err(LineReport::new(
Some(_) => Err(TokenReport::new(
more.current_position() - 1,
"Unexpected character in SetUpEditor",
None,
Expand Down
6 changes: 3 additions & 3 deletions ti-basic-optimizer/src/parse/components/data_access.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::error_reporting::{expect_some, expect_tok, next_or_err, LineReport};
use crate::error_reporting::{expect_some, expect_tok, next_or_err, TokenReport};
use crate::parse::{
components::{ListName, MatrixName, Operand},
expression::Expression,
Expand Down Expand Up @@ -87,7 +87,7 @@ impl MatrixIndex {
subject: MatrixIndexable,
token: Token,
more: &mut Tokens,
) -> Result<Option<Self>, LineReport> {
) -> Result<Option<Self>, TokenReport> {
if token != Token::OneByte(0x10) {
return Ok(None);
}
Expand Down Expand Up @@ -146,7 +146,7 @@ impl ListIndex {
subject: ListIndexable,
token: Token,
more: &mut Tokens,
) -> Result<Option<Self>, LineReport> {
) -> Result<Option<Self>, TokenReport> {
if token != Token::OneByte(0x10) {
return Ok(None);
}
Expand Down
4 changes: 2 additions & 2 deletions ti-basic-optimizer/src/parse/components/delvar_target.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::error_reporting::LineReport;
use crate::error_reporting::TokenReport;
use crate::parse::{
components::{
EquationName, ImageName, ListIndex, ListName, MatrixIndex, MatrixName, NumericVarName,
Expand All @@ -24,7 +24,7 @@ pub enum DelVarTarget {
}

impl Parse for DelVarTarget {
fn parse(token: Token, more: &mut Tokens) -> Result<Option<Self>, LineReport> {
fn parse(token: Token, more: &mut Tokens) -> Result<Option<Self>, TokenReport> {
match token {
Token::OneByte(0x41..=0x5B) | Token::TwoByte(0x62, 0x21) => {
Ok(NumericVarName::parse(token, more)?.map(Self::NumericVar))
Expand Down
4 changes: 2 additions & 2 deletions ti-basic-optimizer/src/parse/components/equation_name.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::error_reporting::LineReport;
use crate::error_reporting::TokenReport;
use crate::parse::{Parse, Reconstruct};
use crate::Config;
use titokens::{Token, Tokens};
Expand All @@ -7,7 +7,7 @@ use titokens::{Token, Tokens};
pub struct EquationName(Token);

impl Parse for EquationName {
fn parse(token: Token, _more: &mut Tokens) -> Result<Option<Self>, LineReport> {
fn parse(token: Token, _more: &mut Tokens) -> Result<Option<Self>, TokenReport> {
Ok(match token {
Token::TwoByte(0x5E, 0x10..=0x2B | 0x40..=0x45 | 0x80..=0x82) => {
Some(EquationName(token))
Expand Down
6 changes: 3 additions & 3 deletions ti-basic-optimizer/src/parse/components/function_call.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::error_reporting::LineReport;
use crate::error_reporting::TokenReport;
use crate::parse::expression::Expression;
use crate::parse::{Parse, Reconstruct};
use crate::Config;
Expand All @@ -12,7 +12,7 @@ pub struct FunctionCall {
}

impl Parse for FunctionCall {
fn parse(token: Token, more: &mut Tokens) -> Result<Option<Self>, LineReport> {
fn parse(token: Token, more: &mut Tokens) -> Result<Option<Self>, TokenReport> {
if !FunctionCall::recognize(token) {
return Ok(None);
}
Expand All @@ -39,7 +39,7 @@ impl Parse for FunctionCall {

Some(Token::OneByte(0x3E | 0x3F)) | None => break, // :, \n, EOF

Some(_) => Err(LineReport::new(
Some(_) => Err(TokenReport::new(
more.current_position() - 1,
"Unexpected character in function call",
Some("perhaps it's unimplemented?"),
Expand Down
4 changes: 2 additions & 2 deletions ti-basic-optimizer/src/parse/components/list.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::error_reporting::{next_or_err, LineReport};
use crate::error_reporting::{next_or_err, TokenReport};
use crate::parse::expression::Expression;
use crate::parse::{Parse, Reconstruct};
use crate::Config;
Expand All @@ -11,7 +11,7 @@ pub struct TIList {
}

impl Parse for TIList {
fn parse(token: Token, more: &mut Tokens) -> Result<Option<Self>, LineReport> {
fn parse(token: Token, more: &mut Tokens) -> Result<Option<Self>, TokenReport> {
// {
if token != Token::OneByte(0x08) {
return Ok(None);
Expand Down
Loading

0 comments on commit 49d75a4

Please sign in to comment.