Skip to content

Commit

Permalink
feat: namer add symbol_db
Browse files Browse the repository at this point in the history
  • Loading branch information
NeverRaR committed Oct 18, 2023
1 parent 4495df4 commit ed75646
Show file tree
Hide file tree
Showing 9 changed files with 385 additions and 246 deletions.
10 changes: 10 additions & 0 deletions kclvm/Cargo.lock

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

9 changes: 5 additions & 4 deletions kclvm/ast/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
compiler_base_span = {path = "../../compiler_base/span", version = "0.0.2"}
uuid = { version = "1.4.1", features = ["v4"] }
compiler_base_span = { path = "../../compiler_base/span", version = "0.0.2" }
serde = { version = "1", features = ["derive"] }
serde_json = "1.0"

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

[dev-dependencies]
kclvm-parser = {path = "../parser"}
kclvm-parser = { path = "../parser" }
34 changes: 33 additions & 1 deletion kclvm/ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ use serde::{Deserialize, Serialize};
use std::collections::HashMap;

use compiler_base_span::{Loc, Span};
use std::fmt::Debug;
use uuid;

use super::token;
use crate::{node_ref, pos::ContainsPos};
Expand Down Expand Up @@ -79,12 +81,22 @@ impl Into<Range> for Pos {
}
}

#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub struct AstIndex(uuid::Uuid);

impl Default for AstIndex {
fn default() -> Self {
Self(uuid::Uuid::new_v4())
}
}
/// Node is the file, line and column number information
/// that all AST nodes need to contain.
/// In fact, column and end_column are the counts of character,
/// For example, `\t` is counted as 1 character, so it is recorded as 1 here, but generally col is 4.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[derive(Serialize, Deserialize, Clone, PartialEq)]
pub struct Node<T> {
#[serde(skip_serializing, skip_deserializing, default)]
pub id: AstIndex,
pub node: T,
pub filename: String,
pub line: u64,
Expand All @@ -93,6 +105,19 @@ pub struct Node<T> {
pub end_column: u64,
}

impl<T: Debug> Debug for Node<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Node")
.field("node", &self.node)
.field("filename", &self.filename)
.field("line", &self.line)
.field("column", &self.column)
.field("end_line", &self.end_line)
.field("end_column", &self.end_column)
.finish()
}
}

impl<T> Node<T> {
pub fn new(
node: T,
Expand All @@ -103,6 +128,7 @@ impl<T> Node<T> {
end_column: u64,
) -> Self {
Self {
id: AstIndex::default(),
node,
filename,
line,
Expand All @@ -114,6 +140,7 @@ impl<T> Node<T> {

pub fn dummy_node(node: T) -> Self {
Self {
id: AstIndex::default(),
node,
filename: "".to_string(),
line: 1,
Expand All @@ -125,6 +152,7 @@ impl<T> Node<T> {

pub fn node(node: T, (lo, hi): (Loc, Loc)) -> Self {
Self {
id: AstIndex::default(),
node,
filename: format!("{}", lo.file.name.prefer_remapped()),
line: lo.line as u64,
Expand All @@ -136,6 +164,7 @@ impl<T> Node<T> {

pub fn node_with_pos(node: T, pos: PosTuple) -> Self {
Self {
id: AstIndex::default(),
node,
filename: pos.0.clone(),
line: pos.1,
Expand Down Expand Up @@ -178,6 +207,7 @@ impl TryInto<Node<Identifier>> for Node<Expr> {
fn try_into(self) -> Result<Node<Identifier>, Self::Error> {
match self.node {
Expr::Identifier(ident) => Ok(Node {
id: self.id,
node: ident,
filename: self.filename,
line: self.line,
Expand All @@ -194,6 +224,7 @@ impl Node<Expr> {
/// Into a missing identifier.
pub fn into_missing_identifier(&self) -> Node<Identifier> {
Node {
id: self.id.clone(),
node: Identifier {
names: vec![],
pkgpath: String::new(),
Expand All @@ -214,6 +245,7 @@ impl TryInto<Node<SchemaExpr>> for Node<Expr> {
fn try_into(self) -> Result<Node<SchemaExpr>, Self::Error> {
match self.node {
Expr::Schema(schema_expr) => Ok(Node {
id: self.id,
node: schema_expr,
filename: self.filename,
line: self.line,
Expand Down
3 changes: 3 additions & 0 deletions kclvm/parser/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1171,6 +1171,7 @@ impl<'a> Parser<'a> {
x.node.orelse = if_item.orelse;

let t = Node {
id: AstIndex::default(),
node: Expr::ListIfItem(x.node),
filename: x.filename,
line: x.line,
Expand Down Expand Up @@ -1607,6 +1608,7 @@ impl<'a> Parser<'a> {
x.node.orelse = if_entry.node.orelse;

let t = Node {
id: AstIndex::default(),
node: Expr::ConfigIfEntry(x.node),
filename: x.filename,
line: x.line,
Expand Down Expand Up @@ -1646,6 +1648,7 @@ impl<'a> Parser<'a> {

let mut body = {
let node = Node {
id: AstIndex::default(),
node: Expr::NameConstantLit(NameConstantLit {
value: NameConstant::None, // ignore
}),
Expand Down
1 change: 1 addition & 0 deletions kclvm/parser/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ impl<'a> Parser<'a> {
let filename: String = format!("{}", lo.file.name.prefer_remapped());

let node = kclvm_ast::ast::Node {
id: kclvm_ast::ast::AstIndex::default(),
node: Comment {
text: x.as_str().to_string(),
},
Expand Down
1 change: 1 addition & 0 deletions kclvm/parser/src/parser/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,7 @@ impl<'a> Parser<'a> {
index_signature: body_index_signature,

name: Box::new(Node {
id: AstIndex::default(),
node: "".to_string(),
filename: "".to_string(),
line: 0,
Expand Down
Loading

0 comments on commit ed75646

Please sign in to comment.