Log errors

This commit is contained in:
Aleksey Kladov 2018-08-28 00:42:13 +03:00
parent 8f5330cb07
commit 13110f48e9
3 changed files with 21 additions and 3 deletions

View file

@ -15,6 +15,7 @@ mod module_map;
use std::{ use std::{
fmt, fmt,
path::{Path, PathBuf}, path::{Path, PathBuf},
panic,
sync::{ sync::{
Arc, Arc,
atomic::{AtomicBool, Ordering::SeqCst}, atomic::{AtomicBool, Ordering::SeqCst},
@ -307,8 +308,15 @@ impl FileData {
} }
fn syntax(&self) -> &File { fn syntax(&self) -> &File {
self.syntax let text = &self.text;
.get_or_init(|| File::parse(&self.text)) let syntax = &self.syntax;
match panic::catch_unwind(panic::AssertUnwindSafe(|| syntax.get_or_init(|| File::parse(text)))) {
Ok(file) => file,
Err(err) => {
error!("Parser paniced on:\n------\n{}\n------\n", &self.text);
panic::resume_unwind(err)
}
}
} }
fn syntax_transient(&self) -> File { fn syntax_transient(&self) -> File {

View file

@ -1,6 +1,8 @@
mod event; mod event;
mod input; mod input;
use std::cell::Cell;
use { use {
lexer::Token, lexer::Token,
parser_api::Parser, parser_api::Parser,
@ -51,6 +53,7 @@ pub(crate) struct ParserImpl<'t> {
pos: InputPosition, pos: InputPosition,
events: Vec<Event>, events: Vec<Event>,
steps: Cell<u32>,
} }
impl<'t> ParserImpl<'t> { impl<'t> ParserImpl<'t> {
@ -60,6 +63,7 @@ impl<'t> ParserImpl<'t> {
pos: InputPosition::new(), pos: InputPosition::new(),
events: Vec::new(), events: Vec::new(),
steps: Cell::new(0),
} }
} }
@ -91,6 +95,12 @@ impl<'t> ParserImpl<'t> {
} }
pub(super) fn nth(&self, n: u32) -> SyntaxKind { pub(super) fn nth(&self, n: u32) -> SyntaxKind {
let steps = self.steps.get();
if steps > 10_000_000 {
panic!("the parser seems stuck");
}
self.steps.set(steps + 1);
self.inp.kind(self.pos + n) self.inp.kind(self.pos + n)
} }

View file

@ -40,7 +40,7 @@ use ::{
pub type Result<T> = ::std::result::Result<T, ::failure::Error>; pub type Result<T> = ::std::result::Result<T, ::failure::Error>;
fn main() -> Result<()> { fn main() -> Result<()> {
Logger::with_env() Logger::with_env_or_str("m=error")
.duplicate_to_stderr(Duplicate::All) .duplicate_to_stderr(Duplicate::All)
.log_to_file() .log_to_file()
.directory("log") .directory("log")