Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add logging to driver and parser crates. #402

Merged
merged 2 commits into from
Mar 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ edition = "2018"
[lib]

[dependencies]
jsparagus-structured = { path = "crates/structured" }
jsparagus-ast = { path = "crates/ast" }
jsparagus-driver = { path = "crates/driver", optional = true }
jsparagus-emitter = { path = "crates/emitter" }
Expand Down
2 changes: 2 additions & 0 deletions crates/driver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ jsparagus-interpreter = { path = "../interpreter" }
jsparagus-parser = { path = "../parser" }
rustyline = "6.0.0"
rustyline-derive = "0.3.0"
log = { version = "0.4.0", features = ["max_level_debug", "release_max_level_warn"] }
env_logger = "0.7.1"

# jemalloc is temporarily disabled due to a known upstream bug (macOS crashes
# in release builds): <https://github.com/gnzlbg/jemallocator/issues/136>
Expand Down
2 changes: 2 additions & 0 deletions crates/driver/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod demo;

use env_logger;
use std::env;

// jemalloc is temporarily disabled due to a known upstream bug (macOS crashes
Expand All @@ -11,6 +12,7 @@ use std::env;
// static ALLOC: Jemalloc = Jemalloc;

fn main() {
env_logger::init();
let args: Vec<String> = env::args().collect();
match args.len() {
1 => demo::read_print_loop(),
Expand Down
2 changes: 2 additions & 0 deletions crates/parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ license = "MIT/Apache-2.0"

[dependencies]
bumpalo = "2.6.0"
jsparagus-structured = { path = "../structured" }
jsparagus-ast = { path = "../ast" }
jsparagus-generated-parser = { path = "../generated_parser" }
unic-ucd-ident = { version = "0.9.0", features = ["id"] }
log = { version = "0.4.0", features = ["max_level_debug", "release_max_level_warn"] }

[dev-dependencies]
criterion = "0.3"
Expand Down
6 changes: 6 additions & 0 deletions crates/parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ mod tests;

extern crate jsparagus_ast as ast;
extern crate jsparagus_generated_parser as generated_parser;
extern crate jsparagus_structured as structured;
extern crate log;

use crate::parser::Parser;
use ast::{
Expand All @@ -22,8 +24,10 @@ use generated_parser::{
};
pub use generated_parser::{ParseError, Result};
use lexer::Lexer;
use log::debug;
use std::cell::RefCell;
use std::rc::Rc;
use structured::structured;

pub struct ParseOptions {}
impl ParseOptions {
Expand All @@ -38,6 +42,7 @@ pub fn parse_script<'alloc>(
_options: &ParseOptions,
atoms: Rc<RefCell<SourceAtomSet<'alloc>>>,
) -> Result<'alloc, arena::Box<'alloc, Script<'alloc>>> {
structured!(debug! { parse : "{:?}" = "script" });
Ok(parse(allocator, source, START_STATE_SCRIPT, atoms)?.to_ast()?)
}

Expand All @@ -47,6 +52,7 @@ pub fn parse_module<'alloc>(
_options: &ParseOptions,
atoms: Rc<RefCell<SourceAtomSet<'alloc>>>,
) -> Result<'alloc, arena::Box<'alloc, Module<'alloc>>> {
structured!(debug! { parse : "{:?}" = "module" });
Ok(parse(allocator, source, START_STATE_MODULE, atoms)?.to_ast()?)
}

Expand Down
35 changes: 33 additions & 2 deletions crates/parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use generated_parser::{
full_actions, AstBuilder, AstBuilderDelegate, ErrorCode, ParseError, ParserTrait, Result,
StackValue, Term, TermValue, TerminalId, Token, TABLES,
};
use log::trace;
use structured::structured;

pub struct Parser<'alloc> {
/// Vector of states visited in the LR parse table.
Expand All @@ -26,6 +28,7 @@ impl<'alloc> AstBuilderDelegate<'alloc> for Parser<'alloc> {
impl<'alloc> ParserTrait<'alloc, StackValue<'alloc>> for Parser<'alloc> {
fn shift(&mut self, tv: TermValue<StackValue<'alloc>>) -> Result<'alloc, bool> {
// Shift the new terminal/nonterminal and its associated value.
structured!(trace! { enter : "{:?}" = "shift" });
let mut state = self.state();
assert!(state < TABLES.shift_count);
let mut tv = tv;
Expand All @@ -34,11 +37,17 @@ impl<'alloc> ParserTrait<'alloc, StackValue<'alloc>> for Parser<'alloc> {
assert!(term_index < TABLES.shift_width);
let index = state * TABLES.shift_width + term_index;
let goto = TABLES.shift_table[index];
structured!(trace! {
from : "{}" = state,
to : "{}" = goto,
term : "{:?}" = { let s : &'static str = tv.term.into(); s },
});
if goto < 0 {
// Error handling is in charge of shifting an ErrorSymbol from the
// current state.
self.try_error_handling(tv)?;
tv = self.replay_stack.pop().unwrap();
structured!(trace! { replay_term : "{}" = true });
continue;
}
state = goto as usize;
Expand All @@ -47,13 +56,15 @@ impl<'alloc> ParserTrait<'alloc, StackValue<'alloc>> for Parser<'alloc> {
// Execute any actions, such as reduce actions ast builder actions.
while state >= TABLES.shift_count {
assert!(state < TABLES.action_count + TABLES.shift_count);
structured!(trace! { action : "{}"= state });
if full_actions(self, state)? {
return Ok(true);
}
state = self.state();
}
assert!(state < TABLES.shift_count);
if let Some(tv_temp) = self.replay_stack.pop() {
structured!(trace! { replay_term : "{}" = true });
tv = tv_temp;
} else {
break;
Expand Down Expand Up @@ -104,6 +115,12 @@ impl<'alloc> Parser<'alloc> {
}

pub fn write_token(&mut self, token: &Token) -> Result<'alloc, ()> {
structured!(trace! {
method : "{:?}" = "write_token",
is_on_new_line : "{:?}" = token.is_on_new_line,
start : "{:?}" = token.loc.start,
end : "{:?}" = token.loc.end,
});
// Shift the token with the associated StackValue.
let accept = self.shift(TermValue {
term: Term::Terminal(token.terminal_id),
Expand All @@ -117,6 +134,10 @@ impl<'alloc> Parser<'alloc> {

pub fn close(&mut self, position: usize) -> Result<'alloc, StackValue<'alloc>> {
// Shift the End terminal with the associated StackValue.
structured!(trace! {
method : "{:?}" = "close",
position : "{:?}" = position,
});
let loc = SourceLocation::new(position, position);
let token = Token::basic_token(TerminalId::End, loc);
let accept = self.shift(TermValue {
Expand Down Expand Up @@ -147,6 +168,10 @@ impl<'alloc> Parser<'alloc> {
}

fn try_error_handling(&mut self, t: TermValue<StackValue<'alloc>>) -> Result<'alloc, bool> {
structured!(trace! { try_error_handling_term : "{:?}" = {
let s : &'static str = t.term.into();
s
} });
if let StackValue::Token(ref token) = t.value {
// Error tokens might them-self cause more errors to be reported.
// This happens due to the fact that the ErrorToken can be replayed,
Expand Down Expand Up @@ -202,9 +227,15 @@ impl<'alloc> Parser<'alloc> {

pub fn can_accept_terminal(&self, t: TerminalId) -> bool {
let bogus_loc = SourceLocation::new(0, 0);
self.simulator()
let result = self
.simulator()
.write_token(&Token::basic_token(t, bogus_loc))
.is_ok()
.is_ok();
structured!(trace! {
can_accept : "{:?}" = result,
terminal : "{:?}" = t,
});
result
}

/// Return true if self.close() would succeed.
Expand Down
6 changes: 6 additions & 0 deletions crates/structured/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "jsparagus-structured"
version = "0.1.0"
authors = ["Nicolas B. Pierron <[email protected]>"]
edition = "2018"
license = "MIT/Apache-2.0"
Loading