Convert span's start to line number + col

This requires to make line numbers a first-class citizen in the module
  hierarchy but it is fortunately not _too involved_.
This commit is contained in:
KtorZ
2024-01-18 12:44:20 +01:00
parent e67d5863a1
commit 59c784778e
13 changed files with 165 additions and 26 deletions

View File

@@ -27,10 +27,11 @@ use crate::{
gen_uplc::builder::{
check_replaceable_opaque_type, convert_opaque_type, erase_opaque_type_operations,
find_and_replace_generics, find_list_clause_or_default_first, get_arg_type_name,
get_generic_id_and_type, get_generic_variant_name, get_src_code_by_span, monomorphize,
pattern_has_conditions, wrap_as_multi_validator, wrap_validator_condition, CodeGenFunction,
SpecificClause,
get_generic_id_and_type, get_generic_variant_name, get_line_columns_by_span,
get_src_code_by_span, monomorphize, pattern_has_conditions, wrap_as_multi_validator,
wrap_validator_condition, CodeGenFunction, SpecificClause,
},
line_numbers::LineNumbers,
tipo::{
ModuleValueConstructor, PatternConstructor, Type, TypeInfo, ValueConstructor,
ValueConstructorVariant,
@@ -55,7 +56,7 @@ pub struct CodeGenerator<'a> {
functions: IndexMap<FunctionAccessKey, &'a TypedFunction>,
data_types: IndexMap<DataTypeKey, &'a TypedDataType>,
module_types: IndexMap<&'a String, &'a TypeInfo>,
module_src: IndexMap<String, String>,
module_src: IndexMap<String, (String, LineNumbers)>,
/// immutable option
tracing: TraceLevel,
/// mutable index maps that are reset
@@ -74,7 +75,7 @@ impl<'a> CodeGenerator<'a> {
functions: IndexMap<FunctionAccessKey, &'a TypedFunction>,
data_types: IndexMap<DataTypeKey, &'a TypedDataType>,
module_types: IndexMap<&'a String, &'a TypeInfo>,
module_src: IndexMap<String, String>,
module_src: IndexMap<String, (String, LineNumbers)>,
tracing: TraceLevel,
) -> Self {
CodeGenerator {
@@ -132,10 +133,10 @@ impl<'a> CodeGenerator<'a> {
air_tree_fun = wrap_validator_condition(air_tree_fun, self.tracing);
let src_code = self.module_src.get(module_name).unwrap().clone();
let (src_code, lines) = self.module_src.get(module_name).unwrap().clone();
let mut validator_args_tree =
self.check_validator_args(&fun.arguments, true, air_tree_fun, &src_code);
self.check_validator_args(&fun.arguments, true, air_tree_fun, &src_code, &lines);
validator_args_tree = AirTree::no_op().hoist_over(validator_args_tree);
@@ -154,8 +155,13 @@ impl<'a> CodeGenerator<'a> {
air_tree_fun_other = wrap_validator_condition(air_tree_fun_other, self.tracing);
let mut validator_args_tree_other =
self.check_validator_args(&other.arguments, true, air_tree_fun_other, &src_code);
let mut validator_args_tree_other = self.check_validator_args(
&other.arguments,
true,
air_tree_fun_other,
&src_code,
&lines,
);
validator_args_tree_other = AirTree::no_op().hoist_over(validator_args_tree_other);
@@ -474,8 +480,12 @@ impl<'a> CodeGenerator<'a> {
if kind.is_expect() {
let msg = match self.tracing {
TraceLevel::Silent => unreachable!("excluded from pattern guards"),
// TODO: line number & col
TraceLevel::Compact => format!("{}", location.start),
TraceLevel::Compact => get_line_columns_by_span(
module_name,
location,
&self.module_src,
)
.to_string(),
TraceLevel::Verbose => {
get_src_code_by_span(module_name, location, &self.module_src)
}
@@ -2746,6 +2756,7 @@ impl<'a> CodeGenerator<'a> {
has_context: bool,
body: AirTree,
src_code: &str,
lines: &LineNumbers,
) -> AirTree {
let checked_args = arguments
.iter()
@@ -2764,8 +2775,10 @@ impl<'a> CodeGenerator<'a> {
TraceLevel::Compact | TraceLevel::Verbose => {
let msg = match self.tracing {
TraceLevel::Silent => unreachable!("excluded from pattern guards"),
// TODO: Show line number + column
TraceLevel::Compact => format!("{}", arg_span.start),
TraceLevel::Compact => lines
.line_and_column_number(arg_span.start)
.expect("Out of bounds span")
.to_string(),
TraceLevel::Verbose => src_code
.get(arg_span.start..arg_span.end)
.expect("Out of bounds span")