1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
// TODO remove suppression for dead code warning
#![allow(dead_code)]
#![recursion_limit = "256"]
#![doc = include_str!("../README.md")]
//! # Examples
//!
//! This example generates the output as shown above.
//! The supporting function `parse` is required in order to avoid lifetime problems.
//!
//!```
//! # use rust_decimal::Decimal;
//! # use std::io::{self, Write};
//! # use std::path::PathBuf;
//!
//!use beancount_parser_lima::{
//! BeancountParser, BeancountSources, DirectiveVariant, ParseError, ParseSuccess,
//!};
//!
//!fn main() {
//! let sources = BeancountSources::from(PathBuf::from("examples/data/error-post-balancing.beancount"));
//! let parser = BeancountParser::new(&sources);
//!
//! parse(&sources, &parser, &io::stderr());
//!}
//!
//!fn parse<W>(sources: &BeancountSources, parser: &BeancountParser, error_w: W)
//!where
//! W: Write + Copy,
//!{
//! match parser.parse() {
//! Ok(ParseSuccess {
//! directives,
//! options: _,
//! plugins: _,
//! mut warnings,
//! }) => {
//! let mut errors = Vec::new();
//!
//! for directive in directives {
//! if let DirectiveVariant::Transaction(transaction) = directive.variant() {
//! let mut postings = transaction.postings().collect::<Vec<_>>();
//! let n_postings = postings.len();
//! let n_amounts = itertools::partition(&mut postings, |p| p.amount().is_some());
//!
//! if postings.is_empty() {
//! warnings.push(directive.warning("no postings"));
//! } else if n_amounts + 1 < n_postings {
//! errors.push(
//! directive
//! .error("multiple postings without amount specified")
//! .related_to_all(postings[n_amounts..].iter().copied()),
//! );
//! } else if n_amounts == n_postings {
//! let total: Decimal =
//! postings.iter().map(|p| p.amount().unwrap().value()).sum();
//!
//! if total != Decimal::ZERO {
//! let last_amount = postings.pop().unwrap().amount().unwrap();
//! let other_amounts = postings.iter().map(|p| p.amount().unwrap());
//!
//! errors.push(
//! last_amount
//! .error(format!("sum is {}, expected zero", total))
//! .related_to_all(other_amounts)
//! .in_context(&directive),
//! )
//! }
//! }
//! }
//! }
//!
//! sources.write(error_w, errors).unwrap();
//! sources.write(error_w, warnings).unwrap();
//! }
//!
//! Err(ParseError { errors, warnings }) => {
//! sources.write(error_w, errors).unwrap();
//! sources.write(error_w, warnings).unwrap();
//! }
//! }
//!}
//!```
use ariadne::{Color, Label, Report};
use chumsky::prelude::{Input, Parser};
use either::Either::{self, Left, Right};
use lazy_format::lazy_format;
use lexer::{lex, Token};
use parsers::{file, includes, ParserState};
use path_clean::PathClean;
use sort::SortIteratorAdaptor;
use std::{
collections::{HashMap, HashSet, VecDeque},
env::current_dir,
fmt::{self, Formatter},
fs::File,
io::{self, stderr, Read, Write},
path::{Path, PathBuf},
};
pub use types::*;
/// Contains the content of the Beancount source file, and the content of
/// the transitive closure of all the include'd source files.
///
/// Zero-copy parsing means that all string values are returned as references into these strings.
///
/// # Examples
/// ```
/// # use std::path::PathBuf;
/// use beancount_parser_lima::{BeancountParser, BeancountSources};
///
/// let sources = BeancountSources::from(PathBuf::from("examples/data/full.beancount"));
/// let beancount_parser = BeancountParser::new(&sources);
///
/// let result = beancount_parser.parse();
/// ```
pub struct BeancountSources {
// The source_id is the index in `content`, and the first of these is the root source.
// To support parsing from inline strings, we allow for the path to be optional, but
// there can only ever be one of these, the root.
all_content: Vec<(Option<PathBuf>, String, String)>,
all_errors: Vec<(PathBuf, anyhow::Error)>,
current_dir: Option<PathBuf>,
}
impl BeancountSources {
fn read_with_includes(root: Either<String, PathBuf>) -> Self {
let mut all_content = Vec::new();
let mut all_errors = Vec::new();
let mut pending = VecDeque::from([root]);
let mut all_paths = HashSet::new();
let current_dir = current_dir().ok();
while !pending.is_empty() {
let (path, content) = match pending.pop_front().unwrap() {
Left(content) => (None, Some(content)),
Right(path) => {
all_paths.insert(path.clone());
match read(&path) {
Ok(content) => (Some(path), Some(content)),
Err(e) => {
all_errors.push((path, e));
(None, None)
}
}
}
};
if let Some(content) = content {
let source_id = SourceId::from(all_content.len());
let source_id_string = path.as_ref().map_or("inline".to_owned(), |path| {
path.to_string_lossy().into_owned()
});
all_content.push((path, source_id_string, content));
let (path, source_id_string, content) = all_content.last().unwrap();
let tokens = Some(lex(source_id, content));
let spanned_tokens = tokens
.as_ref()
.unwrap()
.spanned(end_of_input(source_id, content))
.with_context(source_id);
// ignore any errors in parsing, we'll pick them up in the next pass
if let Some(includes) = includes().parse(spanned_tokens).into_output() {
for include in includes {
let included_path =
path.as_ref().map_or(PathBuf::from(&include), |parent| {
parent.join(include).clean()
});
if !all_paths.contains(included_path.as_path()) {
pending.push_back(Right(included_path));
} else {
// TODO include cycle error
writeln!(
&mut stderr(),
"warning: ignoring include cycle in {} for {}",
source_id_string,
&included_path.display()
)
.unwrap();
}
}
};
}
}
Self {
all_content,
all_errors,
current_dir,
}
}
pub fn write<W, K>(&self, w: W, errors_or_warnings: Vec<ErrorOrWarning<K>>) -> io::Result<()>
where
W: Write + Copy,
K: ErrorOrWarningKind,
{
for error_or_warning in errors_or_warnings.into_iter() {
use chumsky::span::Span;
let src_id = self.source_id_string(&error_or_warning.span);
let color = error_or_warning.color();
let report_kind = error_or_warning.report_kind();
Report::build(report_kind, src_id.to_string(), error_or_warning.span.start)
.with_message(error_or_warning.message)
.with_labels(Some(
Label::new((
src_id.to_string(),
error_or_warning.span.start()..error_or_warning.span.end(),
))
.with_message(error_or_warning.reason)
.with_color(color),
))
.with_labels(error_or_warning.contexts.into_iter().map(|(label, span)| {
Label::new((
self.source_id_string(&span).to_string(),
span.start()..span.end(),
))
.with_message(lazy_format!("in this {}", label))
.with_color(Color::Yellow)
}))
.with_labels(error_or_warning.related.into_iter().map(|(label, span)| {
Label::new((
self.source_id_string(&span).to_string(),
span.start()..span.end(),
))
.with_message(lazy_format!("{}", label))
.with_color(Color::Yellow)
}))
.finish()
.write(ariadne::sources(self.sources()), w)?;
}
Ok(())
}
fn source_id_string(&self, span: &Span) -> &str {
use chumsky::span::Span;
let source_index: usize = span.context().into();
&self.all_content[source_index].1
}
fn sources(&self) -> Vec<(String, &str)> {
self.all_content
.iter()
.map(|(_, source_id_string, content)| (source_id_string.to_string(), content.as_str()))
.collect()
}
fn content_iter(&self) -> impl Iterator<Item = (SourceId, Option<&Path>, &str)> {
self.all_content
.iter()
.enumerate()
.map(|(i, (pathbuf, _, content))| {
(
SourceId::from(i),
pathbuf.as_ref().map(|pathbuf| pathbuf.as_path()),
content.as_str(),
)
})
}
}
impl From<PathBuf> for BeancountSources {
fn from(source_path: PathBuf) -> Self {
Self::read_with_includes(Right(source_path))
}
}
impl From<&Path> for BeancountSources {
fn from(source_path: &Path) -> Self {
Self::read_with_includes(Right(source_path.to_owned()))
}
}
impl From<String> for BeancountSources {
fn from(source_string: String) -> Self {
Self::read_with_includes(Left(source_string))
}
}
impl From<&str> for BeancountSources {
fn from(source_string: &str) -> Self {
Self::read_with_includes(Left(source_string.to_owned()))
}
}
impl std::fmt::Debug for BeancountSources {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
writeln!(f, "BeancountSources(",)?;
for (_path, source_id_string, content) in &self.all_content {
writeln!(f, " {} ok len {},", source_id_string, content.len())?;
}
for (path, error) in &self.all_errors {
writeln!(f, " {} {},", path.display(), error)?;
}
writeln!(f, ")",)
}
}
fn read<P>(file_path: P) -> anyhow::Result<String>
where
P: AsRef<Path>,
{
let mut f = File::open(&file_path)?;
let mut file_content = String::new();
// read the whole file
f.read_to_string(&mut file_content)?;
Ok(file_content)
}
type SpannedToken<'t> = (Token<'t>, Span);
/// The Beancount parser itself, which tokenizes and parses the source files
/// contained in `BeancountSources`.
///
/// # Examples
/// ```
/// # use std::io::{self, Write};
/// # use std::path::PathBuf;
///
/// use beancount_parser_lima::{BeancountParser, BeancountSources, ParseError, ParseSuccess};
///
/// fn main() {
/// let sources = BeancountSources::from(PathBuf::from("examples/data/full.beancount"));
/// let parser = BeancountParser::new(&sources);
///
/// parse(&sources, &parser, &io::stderr());
/// }
///
/// fn parse<W>(sources: &BeancountSources, parser: &BeancountParser, error_w: W)
/// where
/// W: Write + Copy,
/// {
/// match parser.parse() {
/// Ok(ParseSuccess {
/// directives,
/// options: _,
/// plugins: _,
/// warnings,
/// }) => {
/// for directive in directives {
/// println!("{}\n", &directive);
/// }
///
/// sources.write(error_w, warnings).unwrap();
/// }
/// Err(ParseError { errors, warnings }) => {
/// sources.write(error_w, errors).unwrap();
/// sources.write(error_w, warnings).unwrap();
/// }
/// }
/// }
/// ````
pub struct BeancountParser<'s, 't> {
sources: &'s BeancountSources,
// indexed by source_id as per sources
tokenized_sources: Vec<Vec<SpannedToken<'t>>>,
}
// We seem to need to actual input type in places, ugh!
// It makes me sad that I had to do this. Perhaps when the
// dust has settled I'll find a way to get rid of it.
type ConcreteInput<'t> = chumsky::input::WithContext<
Span,
chumsky::input::SpannedInput<Token<'t>, Span, &'t [(Token<'t>, Span)]>,
>;
/// A successful parsing all the files, containing date-ordered `Directive`s, `Options`, `Plugin`s, and any `Warning`s.
#[derive(Debug)]
pub struct ParseSuccess<'t> {
pub directives: Vec<Spanned<Directive<'t>>>,
pub options: Options<'t>,
pub plugins: Vec<Plugin<'t>>,
pub warnings: Vec<Warning>,
}
/// The value returned when parsing fails.
#[derive(Debug)]
pub struct ParseError {
pub errors: Vec<Error>,
pub warnings: Vec<Warning>,
}
// result of parse_declarations
type ParseDeclarationsResult<'t> = (
Vec<Vec<Spanned<Declaration<'t>>>>,
Options<'t>,
Vec<Error>,
Vec<Warning>,
);
impl<'s, 't> BeancountParser<'s, 't>
where
's: 't,
{
/// Create a `BeancountParser` from `BeancountSources` read from all input files.
pub fn new(sources: &'s BeancountSources) -> Self {
let mut tokenized_sources = Vec::new();
for (source_id, _source_path, content) in sources.content_iter() {
tokenized_sources.push(lex(source_id, content));
}
BeancountParser {
sources,
tokenized_sources,
}
}
/// Parse the sources, returning date-sorted directives and options, or errors, along with warnings in both cases.
pub fn parse(&'t self) -> Result<ParseSuccess<'t>, ParseError>
where
's: 't,
{
let (all_declarations, options, mut errors, warnings) = self.parse_declarations();
let mut p = PragmaProcessor::new(all_declarations, options);
let directives = p
.by_ref()
.sort(|d| *d.item().date().item())
.collect::<Vec<_>>();
let (options, plugins, mut pragma_errors) = p.result();
errors.append(&mut pragma_errors);
if errors.is_empty() {
Ok(ParseSuccess {
directives,
options,
plugins,
warnings,
})
} else {
Err(ParseError { errors, warnings })
}
}
/// Parse the sources, returning declarations and any errors.
/// The declarations are indexed by SourceId
fn parse_declarations(&'t self) -> ParseDeclarationsResult<'t>
where
's: 't,
{
let mut all_outputs = Vec::new();
let mut all_errors = Vec::new();
let mut parser_state = ParserState::default();
for (source_id, source_path, content) in self.sources.content_iter() {
let i_source: usize = source_id.into();
let tokens = &self.tokenized_sources[i_source];
// type assertion here is to ensure we keep these in step
let spanned_tokens: ConcreteInput = tokens
.spanned(end_of_input(source_id, content))
.with_context(source_id);
let (output, errors) = file(source_path)
.parse_with_state(spanned_tokens, &mut parser_state)
.into_output_errors();
all_outputs.push(output.unwrap_or(Vec::new()));
all_errors.extend(errors);
}
let ParserState { options, warnings } = parser_state;
(
all_outputs,
Options::new(options),
all_errors.into_iter().map(Error::from).collect(),
warnings,
)
}
}
/// Iterator which applies pragmas to the sequence of `Directive`s.
///
/// When the iterator is exhausted, any errors should be collected by the caller.
#[derive(Debug)]
struct PragmaProcessor<'s, 't> {
included: HashMap<&'s Path, Span>,
current: VecDeque<Spanned<Declaration<'t>>>,
stacked: VecDeque<VecDeque<Spanned<Declaration<'t>>>>,
remaining: VecDeque<VecDeque<Spanned<Declaration<'t>>>>,
// tags and meta key/values for pragma push/pop
tags: HashSet<Spanned<&'t Tag<'t>>>,
meta_key_values: HashMap<Spanned<Key<'t>>, Spanned<MetaValue<'t>>>,
options: Options<'t>,
plugins: Vec<Plugin<'t>>,
// errors, for collection when the iterator is exhausted
errors: Vec<Error>,
}
impl<'s, 't> PragmaProcessor<'s, 't> {
fn new(all_declarations: Vec<Vec<Spanned<Declaration<'t>>>>, options: Options<'t>) -> Self {
let mut remaining = all_declarations
.into_iter()
.map(VecDeque::from)
.collect::<VecDeque<_>>();
let current = remaining.pop_front().unwrap_or(VecDeque::new());
PragmaProcessor {
included: HashMap::new(),
current,
stacked: VecDeque::new(),
remaining,
tags: HashSet::new(),
meta_key_values: HashMap::new(),
options,
plugins: Vec::new(),
errors: Vec::new(),
}
}
fn result(self) -> (Options<'t>, Vec<Plugin<'t>>, Vec<Error>) {
(self.options, self.plugins, self.errors)
}
}
impl<'s, 't> Iterator for PragmaProcessor<'s, 't> {
type Item = Spanned<Directive<'t>>;
fn next(&mut self) -> Option<Self::Item> {
match self.current.pop_front() {
Some(declaration) => {
match declaration.item {
Declaration::Directive(mut directive) => {
directive.metadata.merge_tags(&self.tags, &mut self.errors);
directive
.metadata
.merge_key_values(&self.meta_key_values, &mut self.errors);
Some(spanned(directive, declaration.span))
}
Declaration::Pragma(pragma) => {
use Pragma::*;
match pragma {
Pushtag(tag) => {
self.tags.insert(tag);
}
Poptag(tag) => {
self.tags.remove(&tag);
}
Pushmeta(meta) => {
self.meta_key_values.insert(meta.key, meta.value);
}
Popmeta(key) => {
self.meta_key_values.remove(&key);
}
Include(_path) => {
// TODO need to validate the path against what we have in remaining
match self.remaining.pop_front() {
Some(mut switcheroo) => {
std::mem::swap(&mut self.current, &mut switcheroo);
self.stacked.push_front(switcheroo);
}
None => {
eprintln!("warning, include found but nothing remaining");
}
}
}
Option(opt) => {
if let Err(e) = self.options.assimilate(opt) {
self.errors.push(e);
}
}
Plugin(plugin) => self.plugins.push(plugin),
}
// having silently consumed a pragma, go on to the next declaration
self.next()
}
}
}
None => match self.stacked.pop_front() {
Some(current) => {
self.current = current;
self.next()
}
None => None,
},
}
}
}
fn end_of_input(source_id: SourceId, s: &str) -> Span {
chumsky::span::Span::new(source_id, s.len()..s.len())
}
#[cfg(test)]
pub use lexer::bare_lex;
mod format;
mod lexer;
pub use options::Options;
mod options;
mod parsers;
mod sort;
pub mod types;