From 705533b561cb7272e4e03616c813788057676c0c Mon Sep 17 00:00:00 2001 From: NicEastvillage Date: Sat, 21 Oct 2023 10:33:46 +0200 Subject: [PATCH] Improved some error spans --- .../src/game_structure/lcgs/intermediate.rs | 14 ++++------ .../src/game_structure/lcgs/symbol_checker.rs | 28 ++++++------------- .../src/game_structure/lcgs/symbol_table.rs | 8 ++++-- 3 files changed, 19 insertions(+), 31 deletions(-) diff --git a/cgaal-engine/src/game_structure/lcgs/intermediate.rs b/cgaal-engine/src/game_structure/lcgs/intermediate.rs index a6b7b6ad..b5f57871 100644 --- a/cgaal-engine/src/game_structure/lcgs/intermediate.rs +++ b/cgaal-engine/src/game_structure/lcgs/intermediate.rs @@ -1,18 +1,14 @@ use crate::atl::Phi; use std::collections::{HashMap, HashSet}; use std::fmt::{Display, Formatter}; -use std::ops::DerefMut; use crate::game_structure::lcgs::eval::Evaluator; use crate::game_structure::lcgs::query::convert_expr_to_phi; -use crate::game_structure::lcgs::relabeling::Relabeler; -use crate::game_structure::lcgs::symbol_checker::{ - symbol_check, CheckMode, SymbolChecker, SymbolRegistry, -}; -use crate::game_structure::lcgs::symbol_table::{SymbIdx, SymbolTable}; +use crate::game_structure::lcgs::symbol_checker::{symbol_check, SymbolRegistry}; +use crate::game_structure::lcgs::symbol_table::SymbIdx; use crate::game_structure::{ActionIdx, GameStructure, PlayerIdx, PropIdx, StateIdx}; -use crate::parsing::ast::{Decl, DeclKind, Expr, ExprKind, LcgsRoot}; -use crate::parsing::errors::{ErrorLog, SpannedError}; +use crate::parsing::ast::{Decl, DeclKind, Expr, LcgsRoot}; +use crate::parsing::errors::ErrorLog; /// A struct that holds information about players for the intermediate representation #[derive(Clone, Debug, Eq, PartialEq, Hash)] @@ -51,7 +47,7 @@ impl IntermediateLcgs { players, labels, vars, - } = symbol_check(root, &errors); + } = symbol_check(root, &errors)?; let ilcgs = IntermediateLcgs { decls: symbols.solidify(), diff --git a/cgaal-engine/src/game_structure/lcgs/symbol_checker.rs b/cgaal-engine/src/game_structure/lcgs/symbol_checker.rs index 6c076e62..cf04a980 100644 --- a/cgaal-engine/src/game_structure/lcgs/symbol_checker.rs +++ b/cgaal-engine/src/game_structure/lcgs/symbol_checker.rs @@ -55,9 +55,7 @@ fn register_decls(root: LcgsRoot) -> Result { ident.name, DeclKind::Const(Expr::new(expr.span, ExprKind::Num(reduced))), ); - let _ = symbols - .insert(decl) - .map_err(|msg| SpannedError::new(span, msg))?; + let _ = symbols.insert(decl)?; } DeclKind::StateLabel(_, expr) => { // Insert in symbol table and add to labels list @@ -66,31 +64,23 @@ fn register_decls(root: LcgsRoot) -> Result { ident.name, DeclKind::StateLabel(PropIdx(labels.len()), expr), ); - let index = symbols - .insert(decl) - .map_err(|msg| SpannedError::new(span, msg))?; + let index = symbols.insert(decl)?; labels.push(index); } DeclKind::StateVar(state_var) => { // Insert in symbol table and add to vars list let decl = Decl::new(span, ident.name, DeclKind::StateVar(state_var)); - let index = symbols - .insert(decl) - .map_err(|msg| SpannedError::new(span, msg))?; + let index = symbols.insert(decl)?; vars.push(index); } DeclKind::Template(inner_decls) => { let decl = Decl::new(span, ident.name, DeclKind::Template(inner_decls)); - let _ = symbols - .insert(decl) - .map_err(|msg| SpannedError::new(span, msg))?; + let _ = symbols.insert(decl)?; } DeclKind::Player(mut player) => { player.index = PlayerIdx(players_indices.len()); let decl = Decl::new(span, ident.name, DeclKind::Player(player)); - let index = symbols - .insert(decl) - .map_err(|msg| SpannedError::new(span, msg))?; + let index = symbols.insert(decl)?; players_indices.push(index); } _ => unreachable!("Not a global declaration. Parser must have failed."), @@ -130,7 +120,7 @@ fn register_decls(root: LcgsRoot) -> Result { .map(|idecl| { let mut new_decl = relabeler.relabel_decl(idecl.clone())?; new_decl.ident.owner = Some(pdecl.ident.name.clone()); - Ok((new_decl, idecl.span)) + Ok(new_decl) }) .collect::, _>>()?; @@ -138,10 +128,8 @@ fn register_decls(root: LcgsRoot) -> Result { drop(tdecl); // Register the new declarations - for (new_decl, span) in new_decls { - let index = symbols - .insert(new_decl) - .map_err(|msg| SpannedError::new(span, msg))?; + for new_decl in new_decls { + let index = symbols.insert(new_decl)?; let mut ndecl = symbols.get(index).borrow_mut(); match &mut ndecl.kind { DeclKind::StateLabel(id, _) => { diff --git a/cgaal-engine/src/game_structure/lcgs/symbol_table.rs b/cgaal-engine/src/game_structure/lcgs/symbol_table.rs index f570226f..2d3cf15f 100644 --- a/cgaal-engine/src/game_structure/lcgs/symbol_table.rs +++ b/cgaal-engine/src/game_structure/lcgs/symbol_table.rs @@ -3,6 +3,7 @@ use std::fmt::Display; use std::ops::Deref; use crate::parsing::ast::Decl; +use crate::parsing::errors::SpannedError; /// An index of a [Symbol] in a [SymbolTable]. #[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)] @@ -63,9 +64,12 @@ impl SymbolTable { /// Creates and inserts a symbol for the given declaration under the given owned name. /// Returns the index of the inserted symbol. /// If the name is already associated with a different symbol, an error is returned instead. - pub fn insert(&mut self, decl: Decl) -> Result { + pub fn insert(&mut self, decl: Decl) -> Result { if self.exists(&decl.ident.to_string()) { - return Err(format!("The name '{}' is already declared", decl.ident)); + return Err(SpannedError::new( + decl.ident.name.span, + format!("The name '{}' is already declared", decl.ident), + )); } self.symbols.push(Symbol::new(decl)); Ok(SymbIdx(self.symbols.len() - 1))