Skip to content

Commit

Permalink
feat: add KCLSourceMap struct and integrate into Evaluator for source…
Browse files Browse the repository at this point in the history
… mapping support

Signed-off-by: Akash <akashsingh2210670@gmail.com>
  • Loading branch information
SkySingh04 committed Jan 16, 2025
1 parent 4f76c38 commit 9b68de9
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 55 deletions.
8 changes: 6 additions & 2 deletions kclvm/evaluator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use generational_arena::{Arena, Index};
use indexmap::IndexMap;
use kclvm_runtime::val_plan::KCL_PRIVATE_VAR_PREFIX;
use lazy::{BacktrackMeta, LazyEvalScope};
use node::KCLSourceMap;
use proxy::{Frame, Proxy};
use rule::RuleEvalContextRef;
use schema::SchemaEvalContextRef;
Expand Down Expand Up @@ -88,7 +89,9 @@ pub struct Evaluator<'ctx> {
/// Schema attr backtrack meta.
pub backtrack_meta: RefCell<Vec<BacktrackMeta>>,
/// Current AST id for the evaluator walker.
pub ast_id: RefCell<AstIndex>,
pub ast_id: RefCell<AstIndex>,
// Source map for code generated
pub source_map: std::option::Option<KCLSourceMap>,
}

#[derive(Clone)]
Expand Down Expand Up @@ -147,11 +150,12 @@ impl<'ctx> Evaluator<'ctx> {
local_vars: RefCell::new(Default::default()),
backtrack_meta: RefCell::new(Default::default()),
ast_id: RefCell::new(AstIndex::default()),
source_map: Some(KCLSourceMap::new()),
}
}

/// Evaluate the program and return the JSON and YAML result.
pub fn run(self: &Evaluator<'ctx>) -> Result<(String, String)> {
pub fn run(self: &Evaluator<'ctx> ) -> Result<(String, String)> {
let modules = self.program.get_modules_for_pkg(kclvm_ast::MAIN_PKG);
self.init_scope(kclvm_ast::MAIN_PKG);
self.compile_ast_modules(&modules);
Expand Down
32 changes: 32 additions & 0 deletions kclvm/evaluator/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use std::sync::{Arc, RwLock};

use anyhow::Ok;
use generational_arena::Index;
// use serde::{Serialize, Deserialize};
use crate::HashMap;
use kclvm_ast::ast::{self, CallExpr, ConfigEntry, Module, NodeRef};
use kclvm_ast::walker::TypedResultWalker;
use kclvm_runtime::{
Expand All @@ -28,6 +30,36 @@ use crate::{backtrack_break_here, backtrack_update_break};
use crate::{error as kcl_error, GLOBAL_LEVEL, INNER_LEVEL};
use crate::{EvalResult, Evaluator};


pub struct KCLSourceMap {
version: u8,
sources: Vec<String>,
mappings: HashMap<String, Vec<Mapping>>,
}

pub struct Mapping {
generated_line: u32,
generated_column: u32,
original_line: u32,
original_column: u32,
source_index: usize,
}

impl KCLSourceMap {
pub fn new() -> Self {
Self {
version: 1,
sources: Vec::new(),
mappings: HashMap::new(),
}
}

pub fn add_mapping(&mut self, source: String, mapping: Mapping) {
self.mappings.entry(source).or_default().push(mapping);
}
}


/// Impl TypedResultWalker for Evaluator to visit AST nodes to evaluate the result.
impl<'ctx> TypedResultWalker<'ctx> for Evaluator<'ctx> {
type Result = EvalResult;
Expand Down
70 changes: 17 additions & 53 deletions kclvm/runner/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,36 +33,6 @@ pub type kclvm_context_t = std::ffi::c_void;
#[allow(non_camel_case_types)]
pub type kclvm_value_ref_t = std::ffi::c_void;

#[derive(Debug, Serialize, Deserialize)]
pub struct KCLSourceMap {
version: u8,
sources: Vec<String>,
mappings: HashMap<String, Vec<Mapping>>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Mapping {
generated_line: u32,
generated_column: u32,
original_line: u32,
original_column: u32,
source_index: usize,
}

impl KCLSourceMap {
pub fn new() -> Self {
Self {
version: 1,
sources: Vec::new(),
mappings: HashMap::new(),
}
}

pub fn add_mapping(&mut self, source: String, mapping: Mapping) {
self.mappings.entry(source).or_default().push(mapping);
}
}


/// ExecProgramArgs denotes the configuration required to execute the KCL program.
#[derive(Serialize, Deserialize, Debug, Default, Clone)]
Expand Down Expand Up @@ -528,15 +498,13 @@ static ONCE_PANIC_HOOK: Lazy<()> = Lazy::new(|| {

pub struct FastRunner {
opts: RunnerOptions,
sourcemap: Option<KCLSourceMap>,
}

impl FastRunner {
/// New a runner using the lib path and options.
pub fn new(opts: Option<RunnerOptions>) -> Self {
Self {
opts: opts.unwrap_or_default(),
sourcemap: None,
}
}

Expand Down Expand Up @@ -571,33 +539,29 @@ impl FastRunner {
}
})
}));

// Before evaluation, initialize sourcemap if enabled
if args.sourcemap == "true" {
self.sourcemap = Some(KCLSourceMap::new());
}

// During evaluation, track locations
let evaluator_result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
//boolean value to check if the sourcemap is enabled
let result = evaluator.run();

// If sourcemap enabled, collect mappings
if let Some(sourcemap) = &mut self.sourcemap {
// Get source location from result
let source_loc = result.get_source_location();
// // If sourcemap enabled, collect mappings
// if let Some(sourcemap) = &mut self.sourcemap {
// // Get source location from result
// // let source_loc = result.get_source_location();

// Create mapping
let mapping = Mapping {
generated_line: result.yaml_line,
generated_column: result.yaml_column,
original_line: source_loc.line,
original_column: source_loc.column,
source_index: source_loc.file_index,
};

// Add to sourcemap
sourcemap.add_mapping(source_loc.filename, mapping);
}
// // // Create mapping
// // let mapping = Mapping {
// // generated_line: result.yaml_line,
// // generated_column: result.yaml_column,
// // original_line: source_loc.line,
// // original_column: source_loc.column,
// // source_index: source_loc.file_index,
// // };

// // Add to sourcemap
// sourcemap.add_mapping(source_loc.filename, mapping);
// }

result
}));
Expand Down

0 comments on commit 9b68de9

Please sign in to comment.