Add trivia to AST Node (#5330)

Signed-off-by: Nick Cameron <nrc@ncameron.org>
This commit is contained in:
Nick Cameron
2025-02-11 18:30:14 +13:00
committed by GitHub
parent 0e9d37c0a0
commit d4d9bf6c7f
56 changed files with 119 additions and 65 deletions

View File

@ -58,6 +58,7 @@ export class KclManager {
nonCodeNodes: {},
startNodes: [],
},
trivia: [],
}
private _execState: ExecState = emptyExecState()
private _programMemory: ProgramMemory = ProgramMemory.empty()
@ -239,6 +240,7 @@ export class KclManager {
nonCodeNodes: {},
startNodes: [],
},
trivia: [],
}
}

View File

@ -128,15 +128,78 @@ describe('Testing findUniqueName', () => {
it('should find a unique name', () => {
const result = findUniqueName(
JSON.stringify([
{ type: 'Identifier', name: 'yo01', start: 0, end: 0, moduleId: 0 },
{ type: 'Identifier', name: 'yo02', start: 0, end: 0, moduleId: 0 },
{ type: 'Identifier', name: 'yo03', start: 0, end: 0, moduleId: 0 },
{ type: 'Identifier', name: 'yo04', start: 0, end: 0, moduleId: 0 },
{ type: 'Identifier', name: 'yo05', start: 0, end: 0, moduleId: 0 },
{ type: 'Identifier', name: 'yo06', start: 0, end: 0, moduleId: 0 },
{ type: 'Identifier', name: 'yo07', start: 0, end: 0, moduleId: 0 },
{ type: 'Identifier', name: 'yo08', start: 0, end: 0, moduleId: 0 },
{ type: 'Identifier', name: 'yo09', start: 0, end: 0, moduleId: 0 },
{
type: 'Identifier',
name: 'yo01',
start: 0,
end: 0,
moduleId: 0,
trivia: [],
},
{
type: 'Identifier',
name: 'yo02',
start: 0,
end: 0,
moduleId: 0,
trivia: [],
},
{
type: 'Identifier',
name: 'yo03',
start: 0,
end: 0,
moduleId: 0,
trivia: [],
},
{
type: 'Identifier',
name: 'yo04',
start: 0,
end: 0,
moduleId: 0,
trivia: [],
},
{
type: 'Identifier',
name: 'yo05',
start: 0,
end: 0,
moduleId: 0,
trivia: [],
},
{
type: 'Identifier',
name: 'yo06',
start: 0,
end: 0,
moduleId: 0,
trivia: [],
},
{
type: 'Identifier',
name: 'yo07',
start: 0,
end: 0,
moduleId: 0,
trivia: [],
},
{
type: 'Identifier',
name: 'yo08',
start: 0,
end: 0,
moduleId: 0,
trivia: [],
},
{
type: 'Identifier',
name: 'yo09',
start: 0,
end: 0,
moduleId: 0,
trivia: [],
},
] satisfies Node<Identifier>[]),
'yo',
2
@ -154,6 +217,7 @@ describe('Testing addSketchTo', () => {
end: 0,
moduleId: 0,
nonCodeMeta: { nonCodeNodes: {}, startNodes: [] },
trivia: [],
},
'yz'
)

View File

@ -278,6 +278,7 @@ export function mutateObjExpProp(
start: 0,
end: 0,
moduleId: 0,
trivia: [],
})
}
}
@ -890,6 +891,7 @@ export function createLiteral(value: LiteralValue | number): Node<Literal> {
moduleId: 0,
value,
raw,
trivia: [],
}
}
@ -899,6 +901,7 @@ export function createTagDeclarator(value: string): Node<TagDeclarator> {
start: 0,
end: 0,
moduleId: 0,
trivia: [],
value,
}
@ -910,6 +913,7 @@ export function createIdentifier(name: string): Node<Identifier> {
start: 0,
end: 0,
moduleId: 0,
trivia: [],
name,
}
@ -921,6 +925,7 @@ export function createPipeSubstitution(): Node<PipeSubstitution> {
start: 0,
end: 0,
moduleId: 0,
trivia: [],
}
}
@ -933,11 +938,13 @@ export function createCallExpressionStdLib(
start: 0,
end: 0,
moduleId: 0,
trivia: [],
callee: {
type: 'Identifier',
start: 0,
end: 0,
moduleId: 0,
trivia: [],
name,
},
@ -955,11 +962,13 @@ export function createCallExpressionStdLibKw(
start: 0,
end: 0,
moduleId: 0,
trivia: [],
callee: {
type: 'Identifier',
start: 0,
end: 0,
moduleId: 0,
trivia: [],
name,
},
@ -977,11 +986,13 @@ export function createCallExpression(
start: 0,
end: 0,
moduleId: 0,
trivia: [],
callee: {
type: 'Identifier',
start: 0,
end: 0,
moduleId: 0,
trivia: [],
name,
},
@ -997,6 +1008,7 @@ export function createArrayExpression(
start: 0,
end: 0,
moduleId: 0,
trivia: [],
nonCodeMeta: nonCodeMetaEmpty(),
elements,
@ -1011,6 +1023,7 @@ export function createPipeExpression(
start: 0,
end: 0,
moduleId: 0,
trivia: [],
body,
nonCodeMeta: nonCodeMetaEmpty(),
@ -1028,12 +1041,14 @@ export function createVariableDeclaration(
start: 0,
end: 0,
moduleId: 0,
trivia: [],
declaration: {
type: 'VariableDeclarator',
start: 0,
end: 0,
moduleId: 0,
trivia: [],
id: createIdentifier(varName),
init,
@ -1051,6 +1066,7 @@ export function createObjectExpression(properties: {
start: 0,
end: 0,
moduleId: 0,
trivia: [],
nonCodeMeta: nonCodeMetaEmpty(),
properties: Object.entries(properties).map(([key, value]) => ({
@ -1058,6 +1074,7 @@ export function createObjectExpression(properties: {
start: 0,
end: 0,
moduleId: 0,
trivia: [],
key: createIdentifier(key),
value,
@ -1074,6 +1091,7 @@ export function createUnaryExpression(
start: 0,
end: 0,
moduleId: 0,
trivia: [],
operator,
argument,
@ -1090,6 +1108,7 @@ export function createBinaryExpression([left, operator, right]: [
start: 0,
end: 0,
moduleId: 0,
trivia: [],
operator,
left,

View File

@ -1954,6 +1954,7 @@ export const updateStartProfileAtArgs: SketchLineHelper['updateArgs'] = ({
startNodes: [],
nonCodeNodes: [],
},
trivia: [],
},
pathToNode,
}
@ -2534,6 +2535,8 @@ function addTagKw(): addTagFn {
...primaryCallExp,
start: callExpr.node.start,
end: callExpr.node.end,
moduleId: callExpr.node.moduleId,
trivia: callExpr.node.trivia,
})
}

View File

@ -1769,6 +1769,7 @@ mod test {
start: 0,
end: 0,
module_id: ModuleId::default(),
trivia: Vec::new(),
},
return_type: None,
digest: None,

View File

@ -41,7 +41,7 @@ pub enum Definition<'a> {
Import(NodeRef<'a, ImportStatement>),
}
#[derive(Debug, Default, Clone, Deserialize, Serialize, PartialEq, Eq, ts_rs::TS)]
#[derive(Debug, Default, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS)]
#[ts(export)]
#[serde(rename_all = "camelCase")]
pub struct Node<T> {
@ -51,6 +51,8 @@ pub struct Node<T> {
pub end: usize,
#[serde(default, skip_serializing_if = "ModuleId::is_top_level")]
pub module_id: ModuleId,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub trivia: NodeList<NonCodeNode>,
}
impl<T> Node<T> {
@ -94,6 +96,7 @@ impl<T> Node<T> {
start,
end,
module_id,
trivia: Vec::new(),
}
}
@ -103,6 +106,7 @@ impl<T> Node<T> {
start: 0,
end: 0,
module_id: ModuleId::default(),
trivia: Vec::new(),
}
}
@ -112,6 +116,7 @@ impl<T> Node<T> {
start,
end,
module_id,
trivia: Vec::new(),
})
}
@ -642,6 +647,7 @@ impl From<&BodyItem> for SourceRange {
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
#[ts(export)]
#[serde(tag = "type")]
#[allow(clippy::large_enum_variant)]
pub enum Expr {
Literal(BoxNode<Literal>),
Identifier(BoxNode<Identifier>),
@ -3823,6 +3829,7 @@ const cylinder = startSketchOn('-XZ')
start: 0,
end: 0,
module_id: ModuleId::default(),
trivia: Vec::new(),
},
return_type: None,
digest: None,
@ -3852,6 +3859,7 @@ const cylinder = startSketchOn('-XZ')
start: 0,
end: 0,
module_id: ModuleId::default(),
trivia: Vec::new(),
},
return_type: None,
digest: None,
@ -3893,6 +3901,7 @@ const cylinder = startSketchOn('-XZ')
start: 0,
end: 0,
module_id: ModuleId::default(),
trivia: Vec::new(),
},
return_type: None,
digest: None,

View File

@ -308,6 +308,7 @@ fn annotation(i: &mut TokenSlice) -> PResult<Node<NonCodeNode>> {
value,
digest: None,
},
trivia: Vec::new(),
}),
comma_sep,
)
@ -426,6 +427,7 @@ fn pipe_expression(i: &mut TokenSlice) -> PResult<Node<PipeExpression>> {
non_code_meta,
digest: None,
},
trivia: Vec::new(),
})
}
@ -828,6 +830,7 @@ fn object_property_same_key_and_val(i: &mut TokenSlice) -> PResult<Node<ObjectPr
key,
digest: None,
},
trivia: Vec::new(),
})
}
@ -856,6 +859,7 @@ fn object_property(i: &mut TokenSlice) -> PResult<Node<ObjectProperty>> {
value: expr,
digest: None,
},
trivia: Vec::new(),
};
if sep.token_type == TokenType::Colon {
@ -1786,6 +1790,7 @@ fn return_stmt(i: &mut TokenSlice) -> PResult<Node<ReturnStatement>> {
end: argument.end(),
module_id: ret.module_id,
inner: ReturnStatement { argument, digest: None },
trivia: Vec::new(),
})
}
@ -2012,11 +2017,13 @@ fn declaration(i: &mut TokenSlice) -> PResult<BoxNode<VariableDeclaration>> {
init: val,
digest: None,
},
trivia: Vec::new(),
},
visibility,
kind,
digest: None,
},
trivia: Vec::new(),
}))
}
@ -2222,6 +2229,7 @@ fn unary_expression(i: &mut TokenSlice) -> PResult<Node<UnaryExpression>> {
argument,
digest: None,
},
trivia: Vec::new(),
})
}
@ -2302,6 +2310,7 @@ fn expression_stmt(i: &mut TokenSlice) -> PResult<Node<ExpressionStatement>> {
expression: val,
digest: None,
},
trivia: Vec::new(),
})
}
@ -2741,6 +2750,7 @@ fn fn_call(i: &mut TokenSlice) -> PResult<Node<CallExpression>> {
arguments: args,
digest: None,
},
trivia: Vec::new(),
})
}
@ -2770,6 +2780,7 @@ fn fn_call_kw(i: &mut TokenSlice) -> PResult<Node<CallExpressionKw>> {
arguments: args,
digest: None,
},
trivia: Vec::new(),
})
}

View File

@ -1,8 +1,6 @@
---
source: kcl/src/parsing/parser.rs
assertion_line: 4521
expression: actual
snapshot_kind: text
---
{
"body": [

View File

@ -1,8 +1,6 @@
---
source: kcl/src/parsing/parser.rs
assertion_line: 4522
expression: actual
snapshot_kind: text
---
{
"body": [

View File

@ -1,8 +1,6 @@
---
source: kcl/src/parsing/parser.rs
assertion_line: 4523
expression: actual
snapshot_kind: text
---
{
"body": [

View File

@ -1,8 +1,6 @@
---
source: kcl/src/parsing/parser.rs
assertion_line: 4524
expression: actual
snapshot_kind: text
---
{
"body": [

View File

@ -1,8 +1,6 @@
---
source: kcl/src/parsing/parser.rs
assertion_line: 4525
expression: actual
snapshot_kind: text
---
{
"body": [

View File

@ -1,7 +1,6 @@
---
source: kcl/src/parsing/parser.rs
expression: actual
snapshot_kind: text
---
{
"body": [

View File

@ -1,7 +1,6 @@
---
source: kcl/src/parsing/parser.rs
expression: actual
snapshot_kind: text
---
{
"body": [

View File

@ -1,7 +1,6 @@
---
source: kcl/src/simulation_tests.rs
description: Program memory after executing angled_line.kcl
snapshot_kind: text
---
{
"environments": [

View File

@ -1,7 +1,6 @@
---
source: kcl/src/simulation_tests.rs
description: Result of parsing artifact_graph_example_code1.kcl
snapshot_kind: text
---
{
"Ok": {

View File

@ -1,7 +1,6 @@
---
source: kcl/src/simulation_tests.rs
description: Program memory after executing artifact_graph_example_code1.kcl
snapshot_kind: text
---
{
"environments": [

View File

@ -1,7 +1,6 @@
---
source: kcl/src/simulation_tests.rs
description: Result of parsing artifact_graph_example_code_no_3d.kcl
snapshot_kind: text
---
{
"Ok": {

View File

@ -1,7 +1,6 @@
---
source: kcl/src/simulation_tests.rs
description: Program memory after executing artifact_graph_example_code_no_3d.kcl
snapshot_kind: text
---
{
"environments": [

View File

@ -1,7 +1,6 @@
---
source: kcl/src/simulation_tests.rs
description: Result of parsing artifact_graph_example_code_offset_planes.kcl
snapshot_kind: text
---
{
"Ok": {

View File

@ -1,7 +1,6 @@
---
source: kcl/src/simulation_tests.rs
description: Result of parsing artifact_graph_sketch_on_face_etc.kcl
snapshot_kind: text
---
{
"Ok": {

View File

@ -1,7 +1,6 @@
---
source: kcl/src/simulation_tests.rs
description: Program memory after executing artifact_graph_sketch_on_face_etc.kcl
snapshot_kind: text
---
{
"environments": [

View File

@ -1,7 +1,6 @@
---
source: kcl/src/simulation_tests.rs
description: Program memory after executing basic_fillet_cube_close_opposite.kcl
snapshot_kind: text
---
{
"environments": [

View File

@ -1,7 +1,6 @@
---
source: kcl/src/simulation_tests.rs
description: Program memory after executing basic_fillet_cube_end.kcl
snapshot_kind: text
---
{
"environments": [

View File

@ -1,7 +1,6 @@
---
source: kcl/src/simulation_tests.rs
description: Program memory after executing basic_fillet_cube_next_adjacent.kcl
snapshot_kind: text
---
{
"environments": [

View File

@ -1,7 +1,6 @@
---
source: kcl/src/simulation_tests.rs
description: Program memory after executing basic_fillet_cube_previous_adjacent.kcl
snapshot_kind: text
---
{
"environments": [

View File

@ -1,7 +1,6 @@
---
source: kcl/src/simulation_tests.rs
description: Program memory after executing basic_fillet_cube_start.kcl
snapshot_kind: text
---
{
"environments": [

View File

@ -1,7 +1,6 @@
---
source: kcl/src/simulation_tests.rs
description: Program memory after executing big_number_angle_to_match_length_x.kcl
snapshot_kind: text
---
{
"environments": [

View File

@ -1,7 +1,6 @@
---
source: kcl/src/simulation_tests.rs
description: Program memory after executing big_number_angle_to_match_length_y.kcl
snapshot_kind: text
---
{
"environments": [

View File

@ -1,7 +1,6 @@
---
source: kcl/src/simulation_tests.rs
description: Program memory after executing cube.kcl
snapshot_kind: text
---
{
"environments": [

View File

@ -1,7 +1,6 @@
---
source: kcl/src/simulation_tests.rs
description: Result of parsing cube_with_error.kcl
snapshot_kind: text
---
{
"Ok": {

View File

@ -1,7 +1,6 @@
---
source: kcl/src/simulation_tests.rs
description: Result of parsing fillet-and-shell.kcl
snapshot_kind: text
---
{
"Ok": {

View File

@ -1,7 +1,6 @@
---
source: kcl/src/simulation_tests.rs
description: Program memory after executing fillet-and-shell.kcl
snapshot_kind: text
---
{
"environments": [

View File

@ -1,7 +1,6 @@
---
source: kcl/src/simulation_tests.rs
description: Program memory after executing function_sketch.kcl
snapshot_kind: text
---
{
"environments": [

View File

@ -1,7 +1,6 @@
---
source: kcl/src/simulation_tests.rs
description: Program memory after executing function_sketch_with_position.kcl
snapshot_kind: text
---
{
"environments": [

View File

@ -1,7 +1,6 @@
---
source: kcl/src/simulation_tests.rs
description: Result of parsing import_cycle1.kcl
snapshot_kind: text
---
{
"Ok": {

View File

@ -1,7 +1,6 @@
---
source: kcl/src/simulation_tests.rs
description: Result of parsing import_function_not_sketch.kcl
snapshot_kind: text
---
{
"Ok": {

View File

@ -1,7 +1,6 @@
---
source: kcl/src/simulation_tests.rs
description: Program memory after executing import_function_not_sketch.kcl
snapshot_kind: text
---
{
"environments": [

View File

@ -1,7 +1,6 @@
---
source: kcl/src/simulation_tests.rs
description: Program memory after executing import_side_effect.kcl
snapshot_kind: text
---
{
"environments": [

View File

@ -1,7 +1,6 @@
---
source: kcl/src/simulation_tests.rs
description: Result of parsing import_whole.kcl
snapshot_kind: text
---
{
"Ok": {

View File

@ -1,7 +1,6 @@
---
source: kcl/src/simulation_tests.rs
description: Program memory after executing pentagon_fillet_sugar.kcl
snapshot_kind: text
---
{
"environments": [

View File

@ -1,7 +1,6 @@
---
source: kcl/src/simulation_tests.rs
description: Result of parsing pipe_as_arg.kcl
snapshot_kind: text
---
{
"Ok": {

View File

@ -1,7 +1,6 @@
---
source: kcl/src/simulation_tests.rs
description: Program memory after executing pipe_as_arg.kcl
snapshot_kind: text
---
{
"environments": [

View File

@ -1,7 +1,6 @@
---
source: kcl/src/simulation_tests.rs
description: Result of parsing poop_chute.kcl
snapshot_kind: text
---
{
"Ok": {

View File

@ -1,7 +1,6 @@
---
source: kcl/src/simulation_tests.rs
description: Program memory after executing poop_chute.kcl
snapshot_kind: text
---
{
"environments": [

View File

@ -1,7 +1,6 @@
---
source: kcl/src/simulation_tests.rs
description: Program memory after executing riddle_small.kcl
snapshot_kind: text
---
{
"environments": [

View File

@ -1,7 +1,6 @@
---
source: kcl/src/simulation_tests.rs
description: Program memory after executing sketch-on-chamfer-two-times-different-order.kcl
snapshot_kind: text
---
{
"environments": [

View File

@ -1,7 +1,6 @@
---
source: kcl/src/simulation_tests.rs
description: Program memory after executing sketch-on-chamfer-two-times.kcl
snapshot_kind: text
---
{
"environments": [

View File

@ -1,7 +1,6 @@
---
source: kcl/src/simulation_tests.rs
description: Program memory after executing sketch_in_object.kcl
snapshot_kind: text
---
{
"environments": [

View File

@ -1,7 +1,6 @@
---
source: kcl/src/simulation_tests.rs
description: Program memory after executing sketch_on_face.kcl
snapshot_kind: text
---
{
"environments": [

View File

@ -1,7 +1,6 @@
---
source: kcl/src/simulation_tests.rs
description: Program memory after executing sketch_on_face_after_fillets_referencing_face.kcl
snapshot_kind: text
---
{
"environments": [

View File

@ -1,7 +1,6 @@
---
source: kcl/src/simulation_tests.rs
description: Program memory after executing sketch_on_face_circle_tagged.kcl
snapshot_kind: text
---
{
"environments": [

View File

@ -1,7 +1,6 @@
---
source: kcl/src/simulation_tests.rs
description: Program memory after executing sketch_on_face_end.kcl
snapshot_kind: text
---
{
"environments": [

View File

@ -1,7 +1,6 @@
---
source: kcl/src/simulation_tests.rs
description: Program memory after executing sketch_on_face_end_negative_extrude.kcl
snapshot_kind: text
---
{
"environments": [

View File

@ -1,7 +1,6 @@
---
source: kcl/src/simulation_tests.rs
description: Program memory after executing sketch_on_face_start.kcl
snapshot_kind: text
---
{
"environments": [

View File

@ -1,8 +1,6 @@
---
source: kcl/src/simulation_tests.rs
assertion_line: 92
description: Program memory after executing tan_arc_x_line.kcl
snapshot_kind: text
---
{
"environments": [