add tracing crate for logging
This commit is contained in:
parent
5336ffb91e
commit
dd79541ae9
|
@ -30,7 +30,8 @@ getset = "0.1.2"
|
||||||
mlua = { version = "0.9.7", features = ["lua54", "vendored"], optional = true }
|
mlua = { version = "0.9.7", features = ["lua54", "vendored"], optional = true }
|
||||||
path-absolutize = "3.1.1"
|
path-absolutize = "3.1.1"
|
||||||
serde = { version = "1.0.197", features = ["derive", "rc"], optional = true }
|
serde = { version = "1.0.197", features = ["derive", "rc"], optional = true }
|
||||||
shulkerbox = { git = "https://github.com/moritz-hoelting/shulkerbox", default-features = false, optional = true, rev = "b79c9ecd6d45f9319c9083a8103ef0186839b0c0" }
|
shulkerbox = { git = "https://github.com/moritz-hoelting/shulkerbox", default-features = false, optional = true, rev = "e31f9f904a5f5905e912907d988c189ffc8cf313" }
|
||||||
strum = { version = "0.26.2", features = ["derive"] }
|
strum = { version = "0.26.2", features = ["derive"] }
|
||||||
strum_macros = "0.26.2"
|
strum_macros = "0.26.2"
|
||||||
thiserror = "1.0.58"
|
thiserror = "1.0.58"
|
||||||
|
tracing = "0.1.40"
|
||||||
|
|
|
@ -45,6 +45,7 @@ impl TokenStream {
|
||||||
/// A tuple containing the stream of successfully tokenized tokens and a list of lexical errors
|
/// A tuple containing the stream of successfully tokenized tokens and a list of lexical errors
|
||||||
/// encountered during tokenization.
|
/// encountered during tokenization.
|
||||||
#[must_use]
|
#[must_use]
|
||||||
|
#[tracing::instrument(level = "debug", skip_all, fields(source_file = %source_file.path().display()))]
|
||||||
pub fn tokenize(source_file: &Arc<SourceFile>, handler: &impl Handler<error::Error>) -> Self {
|
pub fn tokenize(source_file: &Arc<SourceFile>, handler: &impl Handler<error::Error>) -> Self {
|
||||||
// The list of token trees that will be returned.
|
// The list of token trees that will be returned.
|
||||||
let mut tokens = Vec::new();
|
let mut tokens = Vec::new();
|
||||||
|
@ -57,13 +58,17 @@ impl TokenStream {
|
||||||
Err(TokenizeError::EndOfSourceCodeIteratorArgument) => {
|
Err(TokenizeError::EndOfSourceCodeIteratorArgument) => {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
Err(TokenizeError::FatalLexicalError) => (),
|
Err(TokenizeError::FatalLexicalError) => {
|
||||||
|
tracing::error!("Fatal lexical error encountered while tokenizing source code");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// reverse to use pop() instead of remove(0)
|
// reverse to use pop() instead of remove(0)
|
||||||
tokens.reverse();
|
tokens.reverse();
|
||||||
|
|
||||||
|
tracing::debug!("Structuring tokens into trees");
|
||||||
|
|
||||||
// stucture the tokens into a token stream
|
// stucture the tokens into a token stream
|
||||||
let mut token_trees = Vec::new();
|
let mut token_trees = Vec::new();
|
||||||
while let Some(token_tree) = Self::handle_token(&mut tokens, handler) {
|
while let Some(token_tree) = Self::handle_token(&mut tokens, handler) {
|
||||||
|
|
|
@ -15,6 +15,8 @@ use shulkerbox::{datapack::Datapack, util::compile::CompileOptions, virtual_fs::
|
||||||
|
|
||||||
/// Tokenizes the source code at the given path.
|
/// Tokenizes the source code at the given path.
|
||||||
pub fn tokenize(printer: &Printer, path: &Path) -> Result<TokenStream> {
|
pub fn tokenize(printer: &Printer, path: &Path) -> Result<TokenStream> {
|
||||||
|
tracing::info!("Tokenizing the source code at path: {}", path.display());
|
||||||
|
|
||||||
let source_file = SourceFile::load(path)?;
|
let source_file = SourceFile::load(path)?;
|
||||||
|
|
||||||
Ok(TokenStream::tokenize(&source_file, printer))
|
Ok(TokenStream::tokenize(&source_file, printer))
|
||||||
|
@ -30,6 +32,8 @@ pub fn parse(printer: &Printer, path: &Path) -> Result<ProgramFile> {
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tracing::info!("Parsing the source code at path: {}", path.display());
|
||||||
|
|
||||||
let mut parser = Parser::new(&tokens);
|
let mut parser = Parser::new(&tokens);
|
||||||
let program = parser.parse_program(printer).ok_or(Error::Other(
|
let program = parser.parse_program(printer).ok_or(Error::Other(
|
||||||
"An error occured while parsing the source code.",
|
"An error occured while parsing the source code.",
|
||||||
|
@ -67,6 +71,8 @@ where
|
||||||
.filter_map(Result::ok)
|
.filter_map(Result::ok)
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
|
tracing::info!("Transpiling the source code.");
|
||||||
|
|
||||||
let mut transpiler = Transpiler::new(27);
|
let mut transpiler = Transpiler::new(27);
|
||||||
transpiler.transpile(&programs, printer)?;
|
transpiler.transpile(&programs, printer)?;
|
||||||
let datapack = transpiler.into_datapack();
|
let datapack = transpiler.into_datapack();
|
||||||
|
@ -88,5 +94,7 @@ where
|
||||||
{
|
{
|
||||||
let datapack = transpile(printer, script_paths)?;
|
let datapack = transpile(printer, script_paths)?;
|
||||||
|
|
||||||
|
tracing::info!("Compiling the source code.");
|
||||||
|
|
||||||
Ok(datapack.compile(&CompileOptions::default()))
|
Ok(datapack.compile(&CompileOptions::default()))
|
||||||
}
|
}
|
||||||
|
|
|
@ -278,6 +278,7 @@ impl<'a> Parser<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tracing::instrument(level = "trace", skip_all)]
|
||||||
pub fn parse_declaration(&mut self, handler: &impl Handler<Error>) -> Option<Declaration> {
|
pub fn parse_declaration(&mut self, handler: &impl Handler<Error>) -> Option<Declaration> {
|
||||||
match self.stop_at_significant() {
|
match self.stop_at_significant() {
|
||||||
Reading::Atomic(Token::Keyword(function_keyword))
|
Reading::Atomic(Token::Keyword(function_keyword))
|
||||||
|
@ -285,6 +286,8 @@ impl<'a> Parser<'a> {
|
||||||
{
|
{
|
||||||
let function = self.parse_function(handler)?;
|
let function = self.parse_function(handler)?;
|
||||||
|
|
||||||
|
tracing::trace!("Parsed function '{:?}'", function.identifier.span.str());
|
||||||
|
|
||||||
Some(Declaration::Function(function))
|
Some(Declaration::Function(function))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -361,6 +364,8 @@ impl<'a> Parser<'a> {
|
||||||
if let Some(items) = items {
|
if let Some(items) = items {
|
||||||
let semicolon = self.parse_punctuation(';', true, handler)?;
|
let semicolon = self.parse_punctuation(';', true, handler)?;
|
||||||
|
|
||||||
|
tracing::trace!("Parsed import from '{:?}'", module.str_content());
|
||||||
|
|
||||||
Some(Declaration::Import(Import {
|
Some(Declaration::Import(Import {
|
||||||
from_keyword,
|
from_keyword,
|
||||||
module,
|
module,
|
||||||
|
|
|
@ -78,7 +78,10 @@ impl Namespace {
|
||||||
|
|
||||||
impl<'a> Parser<'a> {
|
impl<'a> Parser<'a> {
|
||||||
/// Parses a [`ProgramFile`].
|
/// Parses a [`ProgramFile`].
|
||||||
|
#[tracing::instrument(level = "debug", skip_all)]
|
||||||
pub fn parse_program(&mut self, handler: &impl Handler<Error>) -> Option<ProgramFile> {
|
pub fn parse_program(&mut self, handler: &impl Handler<Error>) -> Option<ProgramFile> {
|
||||||
|
tracing::debug!("Parsing program");
|
||||||
|
|
||||||
let namespace = match self.stop_at_significant() {
|
let namespace = match self.stop_at_significant() {
|
||||||
Reading::Atomic(Token::Keyword(namespace_keyword))
|
Reading::Atomic(Token::Keyword(namespace_keyword))
|
||||||
if namespace_keyword.keyword == KeywordKind::Namespace =>
|
if namespace_keyword.keyword == KeywordKind::Namespace =>
|
||||||
|
@ -110,6 +113,11 @@ impl<'a> Parser<'a> {
|
||||||
}
|
}
|
||||||
}?;
|
}?;
|
||||||
|
|
||||||
|
tracing::debug!(
|
||||||
|
"Found namespace '{}', parsing declarations",
|
||||||
|
namespace.namespace_name.str_content()
|
||||||
|
);
|
||||||
|
|
||||||
let mut declarations = Vec::new();
|
let mut declarations = Vec::new();
|
||||||
|
|
||||||
while !self.is_exhausted() {
|
while !self.is_exhausted() {
|
||||||
|
|
|
@ -249,11 +249,13 @@ impl<'a> Parser<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parses a [`Statement`].
|
/// Parses a [`Statement`].
|
||||||
|
#[tracing::instrument(level = "trace", skip_all)]
|
||||||
pub fn parse_statement(&mut self, handler: &impl Handler<Error>) -> Option<Statement> {
|
pub fn parse_statement(&mut self, handler: &impl Handler<Error>) -> Option<Statement> {
|
||||||
match self.stop_at_significant() {
|
match self.stop_at_significant() {
|
||||||
// variable declaration
|
// variable declaration
|
||||||
Reading::Atomic(Token::CommandLiteral(command)) => {
|
Reading::Atomic(Token::CommandLiteral(command)) => {
|
||||||
self.forward();
|
self.forward();
|
||||||
|
tracing::trace!("Parsed literal command '{}'", command.clean_command());
|
||||||
Some(Statement::LiteralCommand(command))
|
Some(Statement::LiteralCommand(command))
|
||||||
}
|
}
|
||||||
// block statement
|
// block statement
|
||||||
|
@ -286,6 +288,8 @@ impl<'a> Parser<'a> {
|
||||||
|
|
||||||
let block = self.parse_block(handler)?;
|
let block = self.parse_block(handler)?;
|
||||||
|
|
||||||
|
tracing::trace!("Parsed group command");
|
||||||
|
|
||||||
Some(Statement::Grouping(Grouping {
|
Some(Statement::Grouping(Grouping {
|
||||||
group_keyword,
|
group_keyword,
|
||||||
block,
|
block,
|
||||||
|
@ -302,6 +306,8 @@ impl<'a> Parser<'a> {
|
||||||
let expression = self.parse_expression(handler)?;
|
let expression = self.parse_expression(handler)?;
|
||||||
let semicolon = self.parse_punctuation(';', true, handler)?;
|
let semicolon = self.parse_punctuation(';', true, handler)?;
|
||||||
|
|
||||||
|
tracing::trace!("Parsed run statement: {:?}", expression);
|
||||||
|
|
||||||
Some(Statement::Run(Run {
|
Some(Statement::Run(Run {
|
||||||
run_keyword,
|
run_keyword,
|
||||||
expression,
|
expression,
|
||||||
|
@ -314,6 +320,8 @@ impl<'a> Parser<'a> {
|
||||||
let expression = self.parse_expression(handler)?;
|
let expression = self.parse_expression(handler)?;
|
||||||
let semicolon = self.parse_punctuation(';', true, handler)?;
|
let semicolon = self.parse_punctuation(';', true, handler)?;
|
||||||
|
|
||||||
|
tracing::trace!("Parsed semicolon statement: {:?}", expression);
|
||||||
|
|
||||||
Some(Statement::Semicolon(Semicolon {
|
Some(Statement::Semicolon(Semicolon {
|
||||||
expression,
|
expression,
|
||||||
semicolon,
|
semicolon,
|
||||||
|
|
|
@ -15,10 +15,13 @@ mod enabled {
|
||||||
///
|
///
|
||||||
/// # Errors
|
/// # Errors
|
||||||
/// - If Lua code evaluation is disabled.
|
/// - If Lua code evaluation is disabled.
|
||||||
|
#[tracing::instrument(level = "debug", name = "eval_lua", skip_all, ret)]
|
||||||
pub fn eval_string(
|
pub fn eval_string(
|
||||||
&self,
|
&self,
|
||||||
handler: &impl Handler<TranspileError>,
|
handler: &impl Handler<TranspileError>,
|
||||||
) -> TranspileResult<String> {
|
) -> TranspileResult<String> {
|
||||||
|
tracing::debug!("Evaluating Lua code");
|
||||||
|
|
||||||
let lua = Lua::new();
|
let lua = Lua::new();
|
||||||
|
|
||||||
let name = {
|
let name = {
|
||||||
|
@ -89,6 +92,7 @@ mod disabled {
|
||||||
handler: &impl Handler<TranspileError>,
|
handler: &impl Handler<TranspileError>,
|
||||||
) -> TranspileResult<String> {
|
) -> TranspileResult<String> {
|
||||||
handler.receive(TranspileError::LuaDisabled);
|
handler.receive(TranspileError::LuaDisabled);
|
||||||
|
tracing::error!("Lua code evaluation is disabled");
|
||||||
Err(TranspileError::LuaDisabled)
|
Err(TranspileError::LuaDisabled)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -60,6 +60,7 @@ impl Transpiler {
|
||||||
///
|
///
|
||||||
/// # Errors
|
/// # Errors
|
||||||
/// - [`TranspileError::MissingFunctionDeclaration`] If a called function is missing
|
/// - [`TranspileError::MissingFunctionDeclaration`] If a called function is missing
|
||||||
|
#[tracing::instrument(level = "trace", skip_all)]
|
||||||
pub fn transpile<Ident>(
|
pub fn transpile<Ident>(
|
||||||
&mut self,
|
&mut self,
|
||||||
programs: &[(Ident, ProgramFile)],
|
programs: &[(Ident, ProgramFile)],
|
||||||
|
@ -68,6 +69,8 @@ impl Transpiler {
|
||||||
where
|
where
|
||||||
Ident: AsRef<str>,
|
Ident: AsRef<str>,
|
||||||
{
|
{
|
||||||
|
tracing::trace!("Transpiling program declarations");
|
||||||
|
|
||||||
for (identifier, program) in programs {
|
for (identifier, program) in programs {
|
||||||
self.transpile_program_declarations(program, identifier.as_ref(), handler);
|
self.transpile_program_declarations(program, identifier.as_ref(), handler);
|
||||||
}
|
}
|
||||||
|
@ -87,6 +90,11 @@ impl Transpiler {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tracing::trace!(
|
||||||
|
"Transpiling functions requested by user: {:?}",
|
||||||
|
always_transpile_functions
|
||||||
|
);
|
||||||
|
|
||||||
for (program_identifier, name) in always_transpile_functions {
|
for (program_identifier, name) in always_transpile_functions {
|
||||||
self.get_or_transpile_function(&name, &program_identifier, handler)?;
|
self.get_or_transpile_function(&name, &program_identifier, handler)?;
|
||||||
}
|
}
|
||||||
|
@ -171,6 +179,7 @@ impl Transpiler {
|
||||||
/// Gets the function at the given path, or transpiles it if it hasn't been transpiled yet.
|
/// Gets the function at the given path, or transpiles it if it hasn't been transpiled yet.
|
||||||
/// Returns the location of the function or None if the function does not exist.
|
/// Returns the location of the function or None if the function does not exist.
|
||||||
#[allow(clippy::significant_drop_tightening)]
|
#[allow(clippy::significant_drop_tightening)]
|
||||||
|
#[tracing::instrument(level = "trace", skip(self, handler))]
|
||||||
fn get_or_transpile_function(
|
fn get_or_transpile_function(
|
||||||
&mut self,
|
&mut self,
|
||||||
name: &str,
|
name: &str,
|
||||||
|
@ -194,6 +203,8 @@ impl Transpiler {
|
||||||
.is_some()
|
.is_some()
|
||||||
};
|
};
|
||||||
if !already_transpiled {
|
if !already_transpiled {
|
||||||
|
tracing::trace!("Function not transpiled yet, transpiling.");
|
||||||
|
|
||||||
let statements = {
|
let statements = {
|
||||||
let functions = self.functions.read().unwrap();
|
let functions = self.functions.read().unwrap();
|
||||||
let function_data = functions
|
let function_data = functions
|
||||||
|
|
Loading…
Reference in New Issue