Skip to content

Commit

Permalink
feat: impl AST index serializer
Browse files Browse the repository at this point in the history
Signed-off-by: peefy <[email protected]>
  • Loading branch information
Peefy committed Jan 23, 2024
1 parent b1b7585 commit af251e0
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 6 deletions.
5 changes: 3 additions & 2 deletions kclvm/Cargo.lock

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

4 changes: 4 additions & 0 deletions kclvm/api/src/service/service_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ impl KclvmServiceImpl {
resolve_ast: args.resolve_ast,
load_builtin: args.load_builtin,
})?;
if args.with_ast_index {
// Thread local options
kclvm_ast::ast::set_should_serialize_id(true);
}
let program_json = serde_json::to_string(&packages.program)?;
let mut node_symbol_map = HashMap::new();
let mut pkg_scope_map = HashMap::new();
Expand Down
1 change: 1 addition & 0 deletions kclvm/ast/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ serde_json = "1.0"

kclvm-span = { path = "../span" }
kclvm-error = { path = "../error" }
thread_local = "1.1.7"

[dev-dependencies]
kclvm-parser = { path = "../parser" }
29 changes: 26 additions & 3 deletions kclvm/ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
//! in the compiler and regenerate the walker code.
//! :copyright: Copyright The KCL Authors. All rights reserved.
use serde::{Deserialize, Serialize};
use serde::{Deserialize, Serialize, Serializer};
use std::collections::HashMap;

use compiler_base_span::{Loc, Span};
Expand All @@ -43,6 +43,11 @@ use uuid;
use super::token;
use crate::{node_ref, pos::ContainsPos};
use kclvm_error::{diagnostic::Range, Position};
use std::cell::RefCell;

thread_local! {
static SHOULD_SERIALIZE_ID: RefCell<bool> = RefCell::new(false);
}

/// PosTuple denotes the tuple `(filename, line, column, end_line, end_column)`.
pub type PosTuple = (String, u64, u64, u64, u64);
Expand Down Expand Up @@ -87,7 +92,7 @@ impl Serialize for AstIndex {
where
S: serde::Serializer,
{
serializer.serialize_bytes(self.0.as_bytes())
serializer.serialize_str(&self.to_string())
}
}

Expand All @@ -109,7 +114,7 @@ impl ToString for AstIndex {
/// For example, `\t` is counted as 1 character, so it is recorded as 1 here, but generally col is 4.
#[derive(Serialize, Deserialize, Clone, PartialEq)]
pub struct Node<T> {
#[serde(skip_serializing, skip_deserializing, default)]
#[serde(serialize_with = "serialize_id", skip_deserializing, default)]
pub id: AstIndex,
pub node: T,
pub filename: String,
Expand All @@ -119,6 +124,24 @@ pub struct Node<T> {
pub end_column: u64,
}

pub fn set_should_serialize_id(value: bool) {
SHOULD_SERIALIZE_ID.with(|f| {
*f.borrow_mut() = value;
});
}

fn serialize_id<S>(id: &AstIndex, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let should_serialize_id = SHOULD_SERIALIZE_ID.with(|f| *f.borrow());
if should_serialize_id {
id.serialize(serializer)
} else {
serializer.serialize_none()
}
}

impl<T: Debug> Debug for Node<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Node")
Expand Down
3 changes: 2 additions & 1 deletion kclvm/spec/gpyrpc/gpyrpc.proto
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 The KCL Authors. All rights reserved.
// Copyright The KCL Authors. All rights reserved.
//
// This file defines the request parameters and return structure of the KCL RPC server.

Expand Down Expand Up @@ -122,6 +122,7 @@ message LoadPackage_Args {
ParseProgram_Args parse_args = 1;
bool resolve_ast = 2;
bool load_builtin = 3;
bool with_ast_index = 4;
}

message LoadPackage_Result {
Expand Down

0 comments on commit af251e0

Please sign in to comment.