pub mod ast;
pub mod bms;
pub mod copy;
pub mod csd;
mod debugging;
pub mod feedback;
pub mod jni;
pub mod lexer;
pub mod parser;
pub mod report;
pub mod source;
pub mod sql;
pub mod system;
use std::fmt;
pub use rt::vocab::Pos;
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Severity {
Informational,
Warning,
Error,
Severe,
Unrecoverable,
}
impl Severity {
pub const fn return_code(self) -> u8 {
match self {
Self::Informational => 0,
Self::Warning => 4,
Self::Error => 8,
Self::Severe => 12,
Self::Unrecoverable => 16,
}
}
pub const fn label(self) -> Option<&'static str> {
match self {
Self::Informational => Some("informational"),
Self::Warning => Some("warning"),
Self::Error | Self::Severe | Self::Unrecoverable => None,
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Error {
pub pos: Pos,
pub message: String,
pub file: Option<String>,
pub severity: Severity,
}
impl Error {
pub fn at(pos: Pos, message: impl Into<String>) -> Self {
Self { pos, message: message.into(), file: None, severity: Severity::Severe }
}
pub fn warning(pos: Pos, message: impl Into<String>) -> Self {
Self::at(pos, message).graded(Severity::Warning)
}
pub fn graded(mut self, severity: Severity) -> Self {
self.severity = severity;
self
}
pub fn in_files(mut self, files: &[String]) -> Self {
self.file = files.get(self.pos.file as usize).filter(|f| !f.is_empty()).cloned();
self
}
pub fn place(&self, main: &str) -> String {
match (&self.file, self.pos.line) {
(Some(f), _) => format!("{f}:{self}"),
(None, 0) => format!("{main}: {}", self.labelled()),
(None, _) => format!("{main}:{self}"),
}
}
pub fn labelled(&self) -> String {
match self.severity.label() {
Some(label) => format!("{label}: {}", self.message),
None => self.message.clone(),
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}: {}", self.pos, self.labelled())
}
}
pub fn return_code(messages: &[Error]) -> u8 {
messages.iter().map(|m| m.severity.return_code()).max().unwrap_or(0)
}
pub fn most_severe(messages: &[Error]) -> Option<&Error> {
messages.iter().rev().max_by_key(|m| m.severity)
}
impl std::error::Error for Error {}
pub fn parse(text: &str) -> Result<ast::Program, Error> {
parse_with(text, ©::Libraries::default())
}
pub fn parse_with(text: &str, libraries: ©::Libraries) -> Result<ast::Program, Error> {
Ok(parse_all_with(text, libraries)?.remove(0))
}
pub fn parse_all_with(text: &str, libraries: ©::Libraries) -> Result<Vec<ast::Program>, Error> {
let mut files = vec![String::new()];
let mut source = source::read(text).and_then(|s| copy::expand(s, libraries, &mut files)).map_err(|e| e.in_files(&files))?;
let mut tokens = lexer::lex(&source).map_err(|e| e.in_files(&files))?;
if debugging::requested(&tokens) {
files.truncate(1);
source = source::read_file_debugging(text, 0).and_then(|s| copy::expand(s, libraries, &mut files)).map_err(|e| e.in_files(&files))?;
let lexed = lexer::lex(&source).map_err(|e| e.in_files(&files))?;
tokens = debugging::keep(lexed, source.debugging.as_deref().unwrap_or_default());
}
let mut programs = parser::parse(&tokens, source.options).map_err(|e| e.in_files(&files))?;
for p in &mut programs {
p.sources = files.clone();
}
Ok(programs)
}
#[cfg(test)]
mod tests {
use super::*;
const ALL: [Severity; 5] = [Severity::Informational, Severity::Warning, Severity::Error, Severity::Severe, Severity::Unrecoverable];
fn message(severity: Severity, line: u32) -> Error {
Error::at(Pos { file: 0, line, col: 8 }, format!("m{line}")).graded(severity)
}
#[test]
fn severities_rise_with_ibms_return_codes() {
assert_eq!(ALL.map(Severity::return_code), [0, 4, 8, 12, 16]);
assert!(ALL.windows(2).all(|w| w[0] < w[1]));
assert_eq!(Error::at(Pos::default(), "m").severity, Severity::Severe);
assert_eq!(Error::warning(Pos::default(), "m").severity, Severity::Warning);
}
#[test]
fn a_compilation_returns_the_highest_code_of_its_messages() {
let codes = |severities: &[Severity]| return_code(&severities.iter().enumerate().map(|(i, &s)| message(s, i as u32 + 1)).collect::<Vec<_>>());
use Severity::*;
assert_eq!(codes(&[]), 0);
assert_eq!(codes(&[Informational, Informational]), 0);
assert_eq!(codes(&[Informational, Warning]), 4);
assert_eq!(codes(&[Warning, Error, Informational]), 8);
assert_eq!(codes(&[Error, Severe, Warning]), 12);
assert_eq!(codes(&[Informational, Warning, Error, Severe]), 12);
assert_eq!(codes(&[Severe, Unrecoverable]), 16);
}
#[test]
fn the_message_a_refusal_names_is_the_first_of_the_most_severe() {
let messages = [message(Severity::Warning, 1), message(Severity::Severe, 2), message(Severity::Error, 3), message(Severity::Severe, 4)];
assert_eq!(most_severe(&messages).map(|m| m.pos.line), Some(2));
assert!(most_severe(&[]).is_none());
}
#[test]
fn an_error_line_carries_no_label_and_a_warning_or_informational_one_says_which_it_is() {
let lines: Vec<String> = ALL.map(|s| message(s, 7).place("p.cbl")).to_vec();
assert_eq!(lines, ["p.cbl:7:8: informational: m7", "p.cbl:7:8: warning: m7", "p.cbl:7:8: m7", "p.cbl:7:8: m7", "p.cbl:7:8: m7"]);
let unplaced = |s: Severity| Error::at(Pos::default(), "m").graded(s).place("p.cbl");
assert_eq!((unplaced(Severity::Severe), unplaced(Severity::Warning)), ("p.cbl: m".to_string(), "p.cbl: warning: m".to_string()));
let in_member = Error::warning(Pos { file: 1, line: 3, col: 8 }, "m3").in_files(&[String::new(), "COPYA".into()]);
assert_eq!(in_member.place("p.cbl"), "COPYA:3:8: warning: m3");
assert_eq!(message(Severity::Severe, 3).to_string(), "3:8: m3");
}
}