Skip to content

Commit

Permalink
Improved some error spans
Browse files Browse the repository at this point in the history
  • Loading branch information
NicEastvillage committed Oct 21, 2023
1 parent 63e7568 commit 705533b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 31 deletions.
14 changes: 5 additions & 9 deletions cgaal-engine/src/game_structure/lcgs/intermediate.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -51,7 +47,7 @@ impl IntermediateLcgs {
players,
labels,
vars,
} = symbol_check(root, &errors);
} = symbol_check(root, &errors)?;

Check warning on line 50 in cgaal-engine/src/game_structure/lcgs/intermediate.rs

View workflow job for this annotation

GitHub Actions / clippy

this expression creates a reference which is immediately dereferenced by the compiler

warning: this expression creates a reference which is immediately dereferenced by the compiler --> cgaal-engine/src/game_structure/lcgs/intermediate.rs:50:32 | 50 | } = symbol_check(root, &errors)?; | ^^^^^^^ help: change this to: `errors` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow

let ilcgs = IntermediateLcgs {
decls: symbols.solidify(),
Expand Down
28 changes: 8 additions & 20 deletions cgaal-engine/src/game_structure/lcgs/symbol_checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@ fn register_decls(root: LcgsRoot) -> Result<SymbolRegistry, SpannedError> {
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
Expand All @@ -66,31 +64,23 @@ fn register_decls(root: LcgsRoot) -> Result<SymbolRegistry, SpannedError> {
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."),
Expand Down Expand Up @@ -130,18 +120,16 @@ fn register_decls(root: LcgsRoot) -> Result<SymbolRegistry, SpannedError> {
.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::<Result<Vec<_>, _>>()?;

drop(pdecl);
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, _) => {
Expand Down
8 changes: 6 additions & 2 deletions cgaal-engine/src/game_structure/lcgs/symbol_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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<SymbIdx, String> {
pub fn insert(&mut self, decl: Decl) -> Result<SymbIdx, SpannedError> {
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))
Expand Down

0 comments on commit 705533b

Please sign in to comment.