Rename field to avoid name collisions
This commit is contained in:
@ -10,7 +10,7 @@ use pretty_assertions::assert_eq;
|
|||||||
fn basic() {
|
fn basic() {
|
||||||
let actual = parse!("const y = 4");
|
let actual = parse!("const y = 4");
|
||||||
let expected = UnboxedNode {
|
let expected = UnboxedNode {
|
||||||
kind: Program {
|
inner: Program {
|
||||||
body: vec![BodyItem::VariableDeclaration(UnboxedNode::new(
|
body: vec![BodyItem::VariableDeclaration(UnboxedNode::new(
|
||||||
VariableDeclaration {
|
VariableDeclaration {
|
||||||
declarations: vec![UnboxedNode::new(
|
declarations: vec![UnboxedNode::new(
|
||||||
|
|||||||
@ -50,22 +50,26 @@ pub enum Definition<'a> {
|
|||||||
#[ts(export)]
|
#[ts(export)]
|
||||||
pub struct UnboxedNode<T> {
|
pub struct UnboxedNode<T> {
|
||||||
#[serde(flatten)]
|
#[serde(flatten)]
|
||||||
pub kind: T,
|
pub inner: T,
|
||||||
pub start: usize,
|
pub start: usize,
|
||||||
pub end: usize,
|
pub end: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> UnboxedNode<T> {
|
impl<T> UnboxedNode<T> {
|
||||||
pub fn new(kind: T, start: usize, end: usize) -> Self {
|
pub fn new(inner: T, start: usize, end: usize) -> Self {
|
||||||
Self { kind, start, end }
|
Self { inner, start, end }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn no_src(kind: T) -> Self {
|
pub fn no_src(inner: T) -> Self {
|
||||||
Self { kind, start: 0, end: 0 }
|
Self {
|
||||||
|
inner,
|
||||||
|
start: 0,
|
||||||
|
end: 0,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn boxed(kind: T, start: usize, end: usize) -> Node<T> {
|
pub fn boxed(inner: T, start: usize, end: usize) -> Node<T> {
|
||||||
Box::new(UnboxedNode { kind, start, end })
|
Box::new(UnboxedNode { inner, start, end })
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn as_source_ranges(&self) -> Vec<SourceRange> {
|
pub fn as_source_ranges(&self) -> Vec<SourceRange> {
|
||||||
@ -77,19 +81,19 @@ impl<T> Deref for UnboxedNode<T> {
|
|||||||
type Target = T;
|
type Target = T;
|
||||||
|
|
||||||
fn deref(&self) -> &Self::Target {
|
fn deref(&self) -> &Self::Target {
|
||||||
&self.kind
|
&self.inner
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> DerefMut for UnboxedNode<T> {
|
impl<T> DerefMut for UnboxedNode<T> {
|
||||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||||
&mut self.kind
|
&mut self.inner
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: fmt::Display> fmt::Display for UnboxedNode<T> {
|
impl<T: fmt::Display> fmt::Display for UnboxedNode<T> {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
self.kind.fmt(f)
|
self.inner.fmt(f)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1348,11 +1352,11 @@ impl From<&UnboxedNode<VariableDeclaration>> for Vec<CompletionItem> {
|
|||||||
completions.push(CompletionItem {
|
completions.push(CompletionItem {
|
||||||
label: variable.id.name.to_string(),
|
label: variable.id.name.to_string(),
|
||||||
label_details: None,
|
label_details: None,
|
||||||
kind: Some(match declaration.kind.kind {
|
kind: Some(match declaration.inner.kind {
|
||||||
VariableKind::Const => CompletionItemKind::CONSTANT,
|
VariableKind::Const => CompletionItemKind::CONSTANT,
|
||||||
VariableKind::Fn => CompletionItemKind::FUNCTION,
|
VariableKind::Fn => CompletionItemKind::FUNCTION,
|
||||||
}),
|
}),
|
||||||
detail: Some(declaration.kind.kind.to_string()),
|
detail: Some(declaration.inner.kind.to_string()),
|
||||||
documentation: None,
|
documentation: None,
|
||||||
deprecated: None,
|
deprecated: None,
|
||||||
preselect: None,
|
preselect: None,
|
||||||
@ -3309,7 +3313,7 @@ const cylinder = startSketchOn('-XZ')
|
|||||||
digest: None,
|
digest: None,
|
||||||
}],
|
}],
|
||||||
body: UnboxedNode {
|
body: UnboxedNode {
|
||||||
kind: Program {
|
inner: Program {
|
||||||
body: Vec::new(),
|
body: Vec::new(),
|
||||||
non_code_meta: Default::default(),
|
non_code_meta: Default::default(),
|
||||||
digest: None,
|
digest: None,
|
||||||
@ -3335,7 +3339,7 @@ const cylinder = startSketchOn('-XZ')
|
|||||||
digest: None,
|
digest: None,
|
||||||
}],
|
}],
|
||||||
body: UnboxedNode {
|
body: UnboxedNode {
|
||||||
kind: Program {
|
inner: Program {
|
||||||
body: Vec::new(),
|
body: Vec::new(),
|
||||||
non_code_meta: Default::default(),
|
non_code_meta: Default::default(),
|
||||||
digest: None,
|
digest: None,
|
||||||
@ -3372,7 +3376,7 @@ const cylinder = startSketchOn('-XZ')
|
|||||||
},
|
},
|
||||||
],
|
],
|
||||||
body: UnboxedNode {
|
body: UnboxedNode {
|
||||||
kind: Program {
|
inner: Program {
|
||||||
body: Vec::new(),
|
body: Vec::new(),
|
||||||
non_code_meta: Default::default(),
|
non_code_meta: Default::default(),
|
||||||
digest: None,
|
digest: None,
|
||||||
@ -3403,7 +3407,7 @@ const cylinder = startSketchOn('-XZ')
|
|||||||
// We want to get the bool and verify it is a bool.
|
// We want to get the bool and verify it is a bool.
|
||||||
|
|
||||||
let BodyItem::ExpressionStatement(UnboxedNode {
|
let BodyItem::ExpressionStatement(UnboxedNode {
|
||||||
kind:
|
inner:
|
||||||
ExpressionStatement {
|
ExpressionStatement {
|
||||||
expression,
|
expression,
|
||||||
digest: None,
|
digest: None,
|
||||||
|
|||||||
@ -3482,7 +3482,7 @@ let w = f() + f()
|
|||||||
let func_expr = &UnboxedNode::no_src(FunctionExpression {
|
let func_expr = &UnboxedNode::no_src(FunctionExpression {
|
||||||
params,
|
params,
|
||||||
body: UnboxedNode {
|
body: UnboxedNode {
|
||||||
kind: crate::ast::types::Program {
|
inner: crate::ast::types::Program {
|
||||||
body: Vec::new(),
|
body: Vec::new(),
|
||||||
non_code_meta: Default::default(),
|
non_code_meta: Default::default(),
|
||||||
digest: None,
|
digest: None,
|
||||||
|
|||||||
@ -45,7 +45,7 @@ fn program(i: TokenSlice) -> PResult<UnboxedNode<Program>> {
|
|||||||
|
|
||||||
// Add the shebang to the non-code meta.
|
// Add the shebang to the non-code meta.
|
||||||
if let Some(shebang) = shebang {
|
if let Some(shebang) = shebang {
|
||||||
out.non_code_meta.kind.start_nodes.insert(0, shebang);
|
out.non_code_meta.inner.start_nodes.insert(0, shebang);
|
||||||
}
|
}
|
||||||
// Match original parser behaviour, for now.
|
// Match original parser behaviour, for now.
|
||||||
// Once this is merged and stable, consider changing this as I think it's more accurate
|
// Once this is merged and stable, consider changing this as I think it's more accurate
|
||||||
@ -79,7 +79,7 @@ fn non_code_node(i: TokenSlice) -> PResult<UnboxedNode<NonCodeNode>> {
|
|||||||
.parse_next(i)?;
|
.parse_next(i)?;
|
||||||
let has_empty_line = count_in('\n', &leading_whitespace.value) >= 2;
|
let has_empty_line = count_in('\n', &leading_whitespace.value) >= 2;
|
||||||
non_code_node_no_leading_whitespace
|
non_code_node_no_leading_whitespace
|
||||||
.verify_map(|node: UnboxedNode<NonCodeNode>| match node.kind.value {
|
.verify_map(|node: UnboxedNode<NonCodeNode>| match node.inner.value {
|
||||||
NonCodeValue::BlockComment { value, style } => Some(UnboxedNode::new(
|
NonCodeValue::BlockComment { value, style } => Some(UnboxedNode::new(
|
||||||
NonCodeNode {
|
NonCodeNode {
|
||||||
value: if has_empty_line {
|
value: if has_empty_line {
|
||||||
@ -198,7 +198,7 @@ fn pipe_expression(i: TokenSlice) -> PResult<UnboxedNode<PipeExpression>> {
|
|||||||
Ok(UnboxedNode {
|
Ok(UnboxedNode {
|
||||||
start: values.first().unwrap().start(),
|
start: values.first().unwrap().start(),
|
||||||
end: values.last().unwrap().end().max(max_noncode_end),
|
end: values.last().unwrap().end().max(max_noncode_end),
|
||||||
kind: PipeExpression {
|
inner: PipeExpression {
|
||||||
body: values,
|
body: values,
|
||||||
non_code_meta,
|
non_code_meta,
|
||||||
digest: None,
|
digest: None,
|
||||||
@ -599,7 +599,7 @@ fn object_property(i: TokenSlice) -> PResult<UnboxedNode<ObjectProperty>> {
|
|||||||
Ok(UnboxedNode {
|
Ok(UnboxedNode {
|
||||||
start: key.start,
|
start: key.start,
|
||||||
end: expr.end(),
|
end: expr.end(),
|
||||||
kind: ObjectProperty {
|
inner: ObjectProperty {
|
||||||
key,
|
key,
|
||||||
value: expr,
|
value: expr,
|
||||||
digest: None,
|
digest: None,
|
||||||
@ -932,7 +932,7 @@ fn noncode_just_after_code(i: TokenSlice) -> PResult<UnboxedNode<NonCodeNode>> {
|
|||||||
if has_empty_line {
|
if has_empty_line {
|
||||||
// There's an empty line between the body item and the comment,
|
// There's an empty line between the body item and the comment,
|
||||||
// This means the comment is a NewLineBlockComment!
|
// This means the comment is a NewLineBlockComment!
|
||||||
let value = match nc.kind.value {
|
let value = match nc.inner.value {
|
||||||
NonCodeValue::Shebang { value } => NonCodeValue::Shebang { value },
|
NonCodeValue::Shebang { value } => NonCodeValue::Shebang { value },
|
||||||
// Change block comments to inline, as discussed above
|
// Change block comments to inline, as discussed above
|
||||||
NonCodeValue::BlockComment { value, style } => NonCodeValue::NewLineBlockComment { value, style },
|
NonCodeValue::BlockComment { value, style } => NonCodeValue::NewLineBlockComment { value, style },
|
||||||
@ -941,14 +941,14 @@ fn noncode_just_after_code(i: TokenSlice) -> PResult<UnboxedNode<NonCodeNode>> {
|
|||||||
x @ NonCodeValue::NewLineBlockComment { .. } => x,
|
x @ NonCodeValue::NewLineBlockComment { .. } => x,
|
||||||
x @ NonCodeValue::NewLine => x,
|
x @ NonCodeValue::NewLine => x,
|
||||||
};
|
};
|
||||||
UnboxedNode::new(NonCodeNode { value, ..nc.kind }, nc.start.saturating_sub(1), nc.end)
|
UnboxedNode::new(NonCodeNode { value, ..nc.inner }, nc.start.saturating_sub(1), nc.end)
|
||||||
} else if has_newline {
|
} else if has_newline {
|
||||||
// Nothing has to change, a single newline does not need preserving.
|
// Nothing has to change, a single newline does not need preserving.
|
||||||
nc
|
nc
|
||||||
} else {
|
} else {
|
||||||
// There's no newline between the body item and comment,
|
// There's no newline between the body item and comment,
|
||||||
// so if this is a comment, it must be inline with code.
|
// so if this is a comment, it must be inline with code.
|
||||||
let value = match nc.kind.value {
|
let value = match nc.inner.value {
|
||||||
NonCodeValue::Shebang { value } => NonCodeValue::Shebang { value },
|
NonCodeValue::Shebang { value } => NonCodeValue::Shebang { value },
|
||||||
// Change block comments to inline, as discussed above
|
// Change block comments to inline, as discussed above
|
||||||
NonCodeValue::BlockComment { value, style } => NonCodeValue::InlineComment { value, style },
|
NonCodeValue::BlockComment { value, style } => NonCodeValue::InlineComment { value, style },
|
||||||
@ -957,10 +957,10 @@ fn noncode_just_after_code(i: TokenSlice) -> PResult<UnboxedNode<NonCodeNode>> {
|
|||||||
x @ NonCodeValue::NewLineBlockComment { .. } => x,
|
x @ NonCodeValue::NewLineBlockComment { .. } => x,
|
||||||
x @ NonCodeValue::NewLine => x,
|
x @ NonCodeValue::NewLine => x,
|
||||||
};
|
};
|
||||||
UnboxedNode::new(NonCodeNode { value, ..nc.kind }, nc.start, nc.end)
|
UnboxedNode::new(NonCodeNode { value, ..nc.inner }, nc.start, nc.end)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.map(|nc| UnboxedNode::new(nc.kind, nc.start.saturating_sub(1), nc.end))
|
.map(|nc| UnboxedNode::new(nc.inner, nc.start.saturating_sub(1), nc.end))
|
||||||
.parse_next(i)?;
|
.parse_next(i)?;
|
||||||
Ok(nc)
|
Ok(nc)
|
||||||
}
|
}
|
||||||
@ -1125,7 +1125,7 @@ pub fn function_body(i: TokenSlice) -> PResult<UnboxedNode<Program>> {
|
|||||||
}
|
}
|
||||||
end = nc.end;
|
end = nc.end;
|
||||||
if body.is_empty() {
|
if body.is_empty() {
|
||||||
non_code_meta.kind.start_nodes.push(nc);
|
non_code_meta.inner.start_nodes.push(nc);
|
||||||
} else {
|
} else {
|
||||||
non_code_meta.insert(body.len() - 1, nc);
|
non_code_meta.insert(body.len() - 1, nc);
|
||||||
}
|
}
|
||||||
@ -1197,7 +1197,7 @@ fn import_stmt(i: TokenSlice) -> PResult<Node<ImportStatement>> {
|
|||||||
|
|
||||||
let path = string_literal(i)?;
|
let path = string_literal(i)?;
|
||||||
let end = path.end;
|
let end = path.end;
|
||||||
let path_string = match path.kind.value {
|
let path_string = match path.inner.value {
|
||||||
LiteralValue::String(s) => s,
|
LiteralValue::String(s) => s,
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
};
|
};
|
||||||
@ -1217,7 +1217,7 @@ fn import_stmt(i: TokenSlice) -> PResult<Node<ImportStatement>> {
|
|||||||
ImportStatement {
|
ImportStatement {
|
||||||
items,
|
items,
|
||||||
path: path_string,
|
path: path_string,
|
||||||
raw_path: path.kind.raw,
|
raw_path: path.inner.raw,
|
||||||
digest: None,
|
digest: None,
|
||||||
},
|
},
|
||||||
start,
|
start,
|
||||||
@ -1286,7 +1286,7 @@ pub fn return_stmt(i: TokenSlice) -> PResult<UnboxedNode<ReturnStatement>> {
|
|||||||
Ok(UnboxedNode {
|
Ok(UnboxedNode {
|
||||||
start,
|
start,
|
||||||
end: argument.end(),
|
end: argument.end(),
|
||||||
kind: ReturnStatement { argument, digest: None },
|
inner: ReturnStatement { argument, digest: None },
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1443,7 +1443,7 @@ fn declaration(i: TokenSlice) -> PResult<UnboxedNode<VariableDeclaration>> {
|
|||||||
declarations: vec![UnboxedNode {
|
declarations: vec![UnboxedNode {
|
||||||
start: id.start,
|
start: id.start,
|
||||||
end,
|
end,
|
||||||
kind: VariableDeclarator {
|
inner: VariableDeclarator {
|
||||||
id,
|
id,
|
||||||
init: val,
|
init: val,
|
||||||
digest: None,
|
digest: None,
|
||||||
@ -1589,7 +1589,7 @@ fn unary_expression(i: TokenSlice) -> PResult<UnboxedNode<UnaryExpression>> {
|
|||||||
Ok(UnboxedNode {
|
Ok(UnboxedNode {
|
||||||
start: op_token.start,
|
start: op_token.start,
|
||||||
end: argument.end(),
|
end: argument.end(),
|
||||||
kind: UnaryExpression {
|
inner: UnaryExpression {
|
||||||
operator,
|
operator,
|
||||||
argument,
|
argument,
|
||||||
digest: None,
|
digest: None,
|
||||||
@ -1670,7 +1670,7 @@ fn expression_stmt(i: TokenSlice) -> PResult<UnboxedNode<ExpressionStatement>> {
|
|||||||
Ok(UnboxedNode {
|
Ok(UnboxedNode {
|
||||||
start: val.start(),
|
start: val.start(),
|
||||||
end: val.end(),
|
end: val.end(),
|
||||||
kind: ExpressionStatement {
|
inner: ExpressionStatement {
|
||||||
expression: val,
|
expression: val,
|
||||||
digest: None,
|
digest: None,
|
||||||
},
|
},
|
||||||
@ -1978,7 +1978,7 @@ fn fn_call(i: TokenSlice) -> PResult<UnboxedNode<CallExpression>> {
|
|||||||
Ok(UnboxedNode {
|
Ok(UnboxedNode {
|
||||||
start: fn_name.start,
|
start: fn_name.start,
|
||||||
end,
|
end,
|
||||||
kind: CallExpression {
|
inner: CallExpression {
|
||||||
callee: fn_name,
|
callee: fn_name,
|
||||||
arguments: args,
|
arguments: args,
|
||||||
optional: false,
|
optional: false,
|
||||||
@ -2041,7 +2041,7 @@ mod tests {
|
|||||||
fn test_vardec_no_keyword() {
|
fn test_vardec_no_keyword() {
|
||||||
let tokens = crate::token::lexer("x = 4").unwrap();
|
let tokens = crate::token::lexer("x = 4").unwrap();
|
||||||
let vardec = declaration(&mut tokens.as_slice()).unwrap();
|
let vardec = declaration(&mut tokens.as_slice()).unwrap();
|
||||||
assert_eq!(vardec.kind.kind, VariableKind::Const);
|
assert_eq!(vardec.inner.kind, VariableKind::Const);
|
||||||
let vardec = vardec.declarations.first().unwrap();
|
let vardec = vardec.declarations.first().unwrap();
|
||||||
assert_eq!(vardec.id.name, "x");
|
assert_eq!(vardec.id.name, "x");
|
||||||
let Expr::Literal(init_val) = &vardec.init else {
|
let Expr::Literal(init_val) = &vardec.init else {
|
||||||
@ -2070,7 +2070,7 @@ mod tests {
|
|||||||
let mut slice = tokens.as_slice();
|
let mut slice = tokens.as_slice();
|
||||||
let expr = function_expression.parse_next(&mut slice).unwrap();
|
let expr = function_expression.parse_next(&mut slice).unwrap();
|
||||||
assert_eq!(expr.params, vec![]);
|
assert_eq!(expr.params, vec![]);
|
||||||
let comment_start = expr.body.non_code_meta.kind.start_nodes.first().unwrap();
|
let comment_start = expr.body.non_code_meta.inner.start_nodes.first().unwrap();
|
||||||
let comment0 = &expr.body.non_code_meta.non_code_nodes.get(&0).unwrap()[0];
|
let comment0 = &expr.body.non_code_meta.non_code_nodes.get(&0).unwrap()[0];
|
||||||
let comment1 = &expr.body.non_code_meta.non_code_nodes.get(&1).unwrap()[0];
|
let comment1 = &expr.body.non_code_meta.non_code_nodes.get(&1).unwrap()[0];
|
||||||
assert_eq!(comment_start.value(), "comment 0");
|
assert_eq!(comment_start.value(), "comment 0");
|
||||||
@ -2099,7 +2099,7 @@ comment */
|
|||||||
const mySk1 = startSketchAt([0, 0])"#;
|
const mySk1 = startSketchAt([0, 0])"#;
|
||||||
let tokens = crate::token::lexer(test_program).unwrap();
|
let tokens = crate::token::lexer(test_program).unwrap();
|
||||||
let program = program.parse(&tokens).unwrap();
|
let program = program.parse(&tokens).unwrap();
|
||||||
let mut starting_comments = program.kind.non_code_meta.kind.start_nodes;
|
let mut starting_comments = program.inner.non_code_meta.inner.start_nodes;
|
||||||
assert_eq!(starting_comments.len(), 2);
|
assert_eq!(starting_comments.len(), 2);
|
||||||
let start0 = starting_comments.remove(0);
|
let start0 = starting_comments.remove(0);
|
||||||
let start1 = starting_comments.remove(0);
|
let start1 = starting_comments.remove(0);
|
||||||
@ -2116,15 +2116,15 @@ const mySk1 = startSketchAt([0, 0])"#;
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_comment_in_pipe() {
|
fn test_comment_in_pipe() {
|
||||||
let tokens = crate::token::lexer(r#"const x = y() |> /*hi*/ z(%)"#).unwrap();
|
let tokens = crate::token::lexer(r#"const x = y() |> /*hi*/ z(%)"#).unwrap();
|
||||||
let mut body = program.parse(&tokens).unwrap().kind.body;
|
let mut body = program.parse(&tokens).unwrap().inner.body;
|
||||||
let BodyItem::VariableDeclaration(mut item) = body.remove(0) else {
|
let BodyItem::VariableDeclaration(mut item) = body.remove(0) else {
|
||||||
panic!("expected vardec");
|
panic!("expected vardec");
|
||||||
};
|
};
|
||||||
let val = item.declarations.remove(0).kind.init;
|
let val = item.declarations.remove(0).inner.init;
|
||||||
let Expr::PipeExpression(pipe) = val else {
|
let Expr::PipeExpression(pipe) = val else {
|
||||||
panic!("expected pipe");
|
panic!("expected pipe");
|
||||||
};
|
};
|
||||||
let mut noncode = pipe.kind.non_code_meta;
|
let mut noncode = pipe.inner.non_code_meta;
|
||||||
assert_eq!(noncode.non_code_nodes.len(), 1);
|
assert_eq!(noncode.non_code_nodes.len(), 1);
|
||||||
let comment = noncode.non_code_nodes.remove(&0).unwrap().pop().unwrap();
|
let comment = noncode.non_code_nodes.remove(&0).unwrap().pop().unwrap();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
@ -2215,7 +2215,7 @@ const mySk1 = startSketchAt([0, 0])"#;
|
|||||||
let tokens = crate::token::lexer(test_input).unwrap();
|
let tokens = crate::token::lexer(test_input).unwrap();
|
||||||
let mut slice = tokens.as_slice();
|
let mut slice = tokens.as_slice();
|
||||||
let UnboxedNode {
|
let UnboxedNode {
|
||||||
kind: PipeExpression {
|
inner: PipeExpression {
|
||||||
body, non_code_meta, ..
|
body, non_code_meta, ..
|
||||||
},
|
},
|
||||||
..
|
..
|
||||||
@ -2243,7 +2243,7 @@ const mySk1 = startSketchAt([0, 0])"#;
|
|||||||
"#;
|
"#;
|
||||||
|
|
||||||
let tokens = crate::token::lexer(test_program).unwrap();
|
let tokens = crate::token::lexer(test_program).unwrap();
|
||||||
let Program { non_code_meta, .. } = function_body.parse(&tokens).unwrap().kind;
|
let Program { non_code_meta, .. } = function_body.parse(&tokens).unwrap().inner;
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
vec![UnboxedNode::new(
|
vec![UnboxedNode::new(
|
||||||
NonCodeNode {
|
NonCodeNode {
|
||||||
@ -2256,7 +2256,7 @@ const mySk1 = startSketchAt([0, 0])"#;
|
|||||||
0,
|
0,
|
||||||
20,
|
20,
|
||||||
)],
|
)],
|
||||||
non_code_meta.kind.start_nodes,
|
non_code_meta.inner.start_nodes,
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
@ -2351,7 +2351,7 @@ const mySk1 = startSketchAt([0, 0])"#;
|
|||||||
// The RHS should be a binary expression.
|
// The RHS should be a binary expression.
|
||||||
let actual = binary_expression.parse(&tokens).unwrap();
|
let actual = binary_expression.parse(&tokens).unwrap();
|
||||||
assert_eq!(actual.operator, BinaryOperator::Mul);
|
assert_eq!(actual.operator, BinaryOperator::Mul);
|
||||||
let BinaryPart::BinaryExpression(rhs) = actual.kind.right else {
|
let BinaryPart::BinaryExpression(rhs) = actual.inner.right else {
|
||||||
panic!("Expected RHS to be another binary expression");
|
panic!("Expected RHS to be another binary expression");
|
||||||
};
|
};
|
||||||
assert_eq!(rhs.operator, BinaryOperator::Sub);
|
assert_eq!(rhs.operator, BinaryOperator::Sub);
|
||||||
@ -2380,7 +2380,7 @@ const mySk1 = startSketchAt([0, 0])"#;
|
|||||||
Err(e) => panic!("Could not parse test {i}: {e:#?}"),
|
Err(e) => panic!("Could not parse test {i}: {e:#?}"),
|
||||||
Ok(a) => a,
|
Ok(a) => a,
|
||||||
};
|
};
|
||||||
let Expr::BinaryExpression(_expr) = actual.declarations.remove(0).kind.init else {
|
let Expr::BinaryExpression(_expr) = actual.declarations.remove(0).inner.init else {
|
||||||
panic!(
|
panic!(
|
||||||
"Expected test {i} to be a binary expression but it wasn't, it was {:?}",
|
"Expected test {i} to be a binary expression but it wasn't, it was {:?}",
|
||||||
actual.declarations[0]
|
actual.declarations[0]
|
||||||
@ -2408,12 +2408,12 @@ const mySk1 = startSketchAt([0, 0])"#;
|
|||||||
// The RHS should be a binary expression.
|
// The RHS should be a binary expression.
|
||||||
let outer = binary_expression.parse(&tokens).unwrap();
|
let outer = binary_expression.parse(&tokens).unwrap();
|
||||||
assert_eq!(outer.operator, BinaryOperator::Mul);
|
assert_eq!(outer.operator, BinaryOperator::Mul);
|
||||||
let BinaryPart::BinaryExpression(middle) = outer.kind.right else {
|
let BinaryPart::BinaryExpression(middle) = outer.inner.right else {
|
||||||
panic!("Expected RHS to be another binary expression");
|
panic!("Expected RHS to be another binary expression");
|
||||||
};
|
};
|
||||||
|
|
||||||
assert_eq!(middle.operator, BinaryOperator::Div);
|
assert_eq!(middle.operator, BinaryOperator::Div);
|
||||||
let BinaryPart::BinaryExpression(inner) = middle.kind.left else {
|
let BinaryPart::BinaryExpression(inner) = middle.inner.left else {
|
||||||
panic!("expected nested binary expression");
|
panic!("expected nested binary expression");
|
||||||
};
|
};
|
||||||
assert_eq!(inner.operator, BinaryOperator::Sub);
|
assert_eq!(inner.operator, BinaryOperator::Sub);
|
||||||
@ -2426,11 +2426,11 @@ const mySk1 = startSketchAt([0, 0])"#;
|
|||||||
let tokens = crate::token::lexer(test).unwrap();
|
let tokens = crate::token::lexer(test).unwrap();
|
||||||
let actual = binary_expression.parse(&tokens).unwrap();
|
let actual = binary_expression.parse(&tokens).unwrap();
|
||||||
assert_eq!(actual.operator, BinaryOperator::Sub);
|
assert_eq!(actual.operator, BinaryOperator::Sub);
|
||||||
let BinaryPart::Literal(left) = actual.kind.left else {
|
let BinaryPart::Literal(left) = actual.inner.left else {
|
||||||
panic!("should be expression");
|
panic!("should be expression");
|
||||||
};
|
};
|
||||||
assert_eq!(left.value, 1u32.into());
|
assert_eq!(left.value, 1u32.into());
|
||||||
let BinaryPart::Literal(right) = actual.kind.right else {
|
let BinaryPart::Literal(right) = actual.inner.right else {
|
||||||
panic!("should be expression");
|
panic!("should be expression");
|
||||||
};
|
};
|
||||||
assert_eq!(right.value, 2u32.into());
|
assert_eq!(right.value, 2u32.into());
|
||||||
@ -2686,7 +2686,7 @@ const mySk1 = startSketchAt([0, 0])"#;
|
|||||||
let tokens = crate::token::lexer(input).unwrap();
|
let tokens = crate::token::lexer(input).unwrap();
|
||||||
let actual = parameters.parse(&tokens);
|
let actual = parameters.parse(&tokens);
|
||||||
assert!(actual.is_ok(), "could not parse test {i}");
|
assert!(actual.is_ok(), "could not parse test {i}");
|
||||||
let actual_ids: Vec<_> = actual.unwrap().into_iter().map(|p| p.identifier.kind.name).collect();
|
let actual_ids: Vec<_> = actual.unwrap().into_iter().map(|p| p.identifier.inner.name).collect();
|
||||||
assert_eq!(actual_ids, expected);
|
assert_eq!(actual_ids, expected);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -2708,7 +2708,7 @@ const mySk1 = startSketchAt([0, 0])"#;
|
|||||||
for test in tests {
|
for test in tests {
|
||||||
// Run the original parser
|
// Run the original parser
|
||||||
let tokens = crate::token::lexer(test).unwrap();
|
let tokens = crate::token::lexer(test).unwrap();
|
||||||
let mut expected_body = crate::parser::Parser::new(tokens.clone()).ast().unwrap().kind.body;
|
let mut expected_body = crate::parser::Parser::new(tokens.clone()).ast().unwrap().inner.body;
|
||||||
assert_eq!(expected_body.len(), 1);
|
assert_eq!(expected_body.len(), 1);
|
||||||
let BodyItem::VariableDeclaration(expected) = expected_body.pop().unwrap() else {
|
let BodyItem::VariableDeclaration(expected) = expected_body.pop().unwrap() else {
|
||||||
panic!("Expected variable declaration");
|
panic!("Expected variable declaration");
|
||||||
@ -2719,12 +2719,12 @@ const mySk1 = startSketchAt([0, 0])"#;
|
|||||||
assert_eq!(expected, actual);
|
assert_eq!(expected, actual);
|
||||||
|
|
||||||
// Inspect its output in more detail.
|
// Inspect its output in more detail.
|
||||||
assert_eq!(actual.kind.kind, VariableKind::Const);
|
assert_eq!(actual.inner.kind, VariableKind::Const);
|
||||||
assert_eq!(actual.start, 0);
|
assert_eq!(actual.start, 0);
|
||||||
assert_eq!(actual.declarations.len(), 1);
|
assert_eq!(actual.declarations.len(), 1);
|
||||||
let decl = actual.declarations.pop().unwrap();
|
let decl = actual.declarations.pop().unwrap();
|
||||||
assert_eq!(decl.id.name, "myVar");
|
assert_eq!(decl.id.name, "myVar");
|
||||||
let Expr::Literal(value) = decl.kind.init else {
|
let Expr::Literal(value) = decl.inner.init else {
|
||||||
panic!("value should be a literal")
|
panic!("value should be a literal")
|
||||||
};
|
};
|
||||||
assert_eq!(value.end, test.len());
|
assert_eq!(value.end, test.len());
|
||||||
@ -2735,7 +2735,7 @@ const mySk1 = startSketchAt([0, 0])"#;
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_math_parse() {
|
fn test_math_parse() {
|
||||||
let tokens = crate::token::lexer(r#"5 + "a""#).unwrap();
|
let tokens = crate::token::lexer(r#"5 + "a""#).unwrap();
|
||||||
let actual = crate::parser::Parser::new(tokens).ast().unwrap().kind.body;
|
let actual = crate::parser::Parser::new(tokens).ast().unwrap().inner.body;
|
||||||
let expr = UnboxedNode::boxed(
|
let expr = UnboxedNode::boxed(
|
||||||
BinaryExpression {
|
BinaryExpression {
|
||||||
operator: BinaryOperator::Add,
|
operator: BinaryOperator::Add,
|
||||||
|
|||||||
@ -38,11 +38,11 @@ impl Program {
|
|||||||
.fold(String::new(), |mut output, (index, recast_str)| {
|
.fold(String::new(), |mut output, (index, recast_str)| {
|
||||||
let start_string = if index == 0 {
|
let start_string = if index == 0 {
|
||||||
// We need to indent.
|
// We need to indent.
|
||||||
if self.non_code_meta.kind.start_nodes.is_empty() {
|
if self.non_code_meta.inner.start_nodes.is_empty() {
|
||||||
indentation.to_string()
|
indentation.to_string()
|
||||||
} else {
|
} else {
|
||||||
self.non_code_meta
|
self.non_code_meta
|
||||||
.kind
|
.inner
|
||||||
.start_nodes
|
.start_nodes
|
||||||
.iter()
|
.iter()
|
||||||
.map(|start| start.format(&indentation))
|
.map(|start| start.format(&indentation))
|
||||||
|
|||||||
Reference in New Issue
Block a user