Typo: noneCode => nonCode (#607)
This commit is contained in:
@ -1512,7 +1512,7 @@ const yo = { a: { b: { c: '123' } } }
|
|||||||
// this is a comment
|
// this is a comment
|
||||||
const key = 'c'`
|
const key = 'c'`
|
||||||
const nonCodeMetaInstance = {
|
const nonCodeMetaInstance = {
|
||||||
type: 'NoneCodeNode',
|
type: 'NonCodeNode',
|
||||||
start: code.indexOf('\n// this is a comment'),
|
start: code.indexOf('\n// this is a comment'),
|
||||||
end: code.indexOf('const key'),
|
end: code.indexOf('const key'),
|
||||||
value: {
|
value: {
|
||||||
@ -1521,17 +1521,17 @@ const key = 'c'`
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
const { nonCodeMeta } = parser_wasm(code)
|
const { nonCodeMeta } = parser_wasm(code)
|
||||||
expect(nonCodeMeta.noneCodeNodes[0]).toEqual(nonCodeMetaInstance)
|
expect(nonCodeMeta.nonCodeNodes[0]).toEqual(nonCodeMetaInstance)
|
||||||
|
|
||||||
// extra whitespace won't change it's position (0) or value (NB the start end would have changed though)
|
// extra whitespace won't change it's position (0) or value (NB the start end would have changed though)
|
||||||
const codeWithExtraStartWhitespace = '\n\n\n' + code
|
const codeWithExtraStartWhitespace = '\n\n\n' + code
|
||||||
const { nonCodeMeta: nonCodeMeta2 } = parser_wasm(
|
const { nonCodeMeta: nonCodeMeta2 } = parser_wasm(
|
||||||
codeWithExtraStartWhitespace
|
codeWithExtraStartWhitespace
|
||||||
)
|
)
|
||||||
expect(nonCodeMeta2.noneCodeNodes[0].value).toStrictEqual(
|
expect(nonCodeMeta2.nonCodeNodes[0].value).toStrictEqual(
|
||||||
nonCodeMetaInstance.value
|
nonCodeMetaInstance.value
|
||||||
)
|
)
|
||||||
expect(nonCodeMeta2.noneCodeNodes[0].start).not.toBe(
|
expect(nonCodeMeta2.nonCodeNodes[0].start).not.toBe(
|
||||||
nonCodeMetaInstance.start
|
nonCodeMetaInstance.start
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
@ -1548,9 +1548,9 @@ const key = 'c'`
|
|||||||
const { body } = parser_wasm(code)
|
const { body } = parser_wasm(code)
|
||||||
const indexOfSecondLineToExpression = 2
|
const indexOfSecondLineToExpression = 2
|
||||||
const sketchNonCodeMeta = (body as any)[0].declarations[0].init.nonCodeMeta
|
const sketchNonCodeMeta = (body as any)[0].declarations[0].init.nonCodeMeta
|
||||||
.noneCodeNodes
|
.nonCodeNodes
|
||||||
expect(sketchNonCodeMeta[indexOfSecondLineToExpression]).toEqual({
|
expect(sketchNonCodeMeta[indexOfSecondLineToExpression]).toEqual({
|
||||||
type: 'NoneCodeNode',
|
type: 'NonCodeNode',
|
||||||
start: 106,
|
start: 106,
|
||||||
end: 166,
|
end: 166,
|
||||||
value: {
|
value: {
|
||||||
@ -1571,9 +1571,9 @@ const key = 'c'`
|
|||||||
|
|
||||||
const { body } = parser_wasm(code)
|
const { body } = parser_wasm(code)
|
||||||
const sketchNonCodeMeta = (body[0] as any).declarations[0].init.nonCodeMeta
|
const sketchNonCodeMeta = (body[0] as any).declarations[0].init.nonCodeMeta
|
||||||
.noneCodeNodes
|
.nonCodeNodes
|
||||||
expect(sketchNonCodeMeta[3]).toEqual({
|
expect(sketchNonCodeMeta[3]).toEqual({
|
||||||
type: 'NoneCodeNode',
|
type: 'NonCodeNode',
|
||||||
start: 125,
|
start: 125,
|
||||||
end: 141,
|
end: 141,
|
||||||
value: {
|
value: {
|
||||||
|
@ -33,5 +33,5 @@ export type SyntaxType =
|
|||||||
| 'PipeExpression'
|
| 'PipeExpression'
|
||||||
| 'PipeSubstitution'
|
| 'PipeSubstitution'
|
||||||
| 'Literal'
|
| 'Literal'
|
||||||
| 'NoneCodeNode'
|
| 'NonCodeNode'
|
||||||
| 'UnaryExpression'
|
| 'UnaryExpression'
|
||||||
|
@ -106,7 +106,7 @@ describe('Testing addSketchTo', () => {
|
|||||||
body: [],
|
body: [],
|
||||||
start: 0,
|
start: 0,
|
||||||
end: 0,
|
end: 0,
|
||||||
nonCodeMeta: { noneCodeNodes: {}, start: null },
|
nonCodeMeta: { nonCodeNodes: {}, start: null },
|
||||||
},
|
},
|
||||||
'yz'
|
'yz'
|
||||||
)
|
)
|
||||||
|
@ -537,7 +537,7 @@ export function createPipeExpression(
|
|||||||
start: 0,
|
start: 0,
|
||||||
end: 0,
|
end: 0,
|
||||||
body,
|
body,
|
||||||
nonCodeMeta: { noneCodeNodes: {}, start: null },
|
nonCodeMeta: { nonCodeNodes: {}, start: null },
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -322,7 +322,7 @@ export const useStore = create<StoreState>()(
|
|||||||
end: 0,
|
end: 0,
|
||||||
body: [],
|
body: [],
|
||||||
nonCodeMeta: {
|
nonCodeMeta: {
|
||||||
noneCodeNodes: {},
|
nonCodeNodes: {},
|
||||||
start: null,
|
start: null,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -554,7 +554,7 @@ async function executeCode({
|
|||||||
end: 0,
|
end: 0,
|
||||||
body: [],
|
body: [],
|
||||||
nonCodeMeta: {
|
nonCodeMeta: {
|
||||||
noneCodeNodes: {},
|
nonCodeNodes: {},
|
||||||
start: null,
|
start: null,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -23,7 +23,7 @@ pub struct Program {
|
|||||||
pub start: usize,
|
pub start: usize,
|
||||||
pub end: usize,
|
pub end: usize,
|
||||||
pub body: Vec<BodyItem>,
|
pub body: Vec<BodyItem>,
|
||||||
pub non_code_meta: NoneCodeMeta,
|
pub non_code_meta: NonCodeMeta,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Program {
|
impl Program {
|
||||||
@ -81,7 +81,7 @@ impl Program {
|
|||||||
"\n".to_string()
|
"\n".to_string()
|
||||||
};
|
};
|
||||||
|
|
||||||
let custom_white_space_or_comment = match self.non_code_meta.none_code_nodes.get(&index) {
|
let custom_white_space_or_comment = match self.non_code_meta.non_code_nodes.get(&index) {
|
||||||
Some(custom_white_space_or_comment) => custom_white_space_or_comment.format(&indentation),
|
Some(custom_white_space_or_comment) => custom_white_space_or_comment.format(&indentation),
|
||||||
None => String::new(),
|
None => String::new(),
|
||||||
};
|
};
|
||||||
@ -640,26 +640,26 @@ impl BinaryPart {
|
|||||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
|
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
|
||||||
#[ts(export)]
|
#[ts(export)]
|
||||||
#[serde(tag = "type")]
|
#[serde(tag = "type")]
|
||||||
pub struct NoneCodeNode {
|
pub struct NonCodeNode {
|
||||||
pub start: usize,
|
pub start: usize,
|
||||||
pub end: usize,
|
pub end: usize,
|
||||||
pub value: NoneCodeValue,
|
pub value: NonCodeValue,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl NoneCodeNode {
|
impl NonCodeNode {
|
||||||
pub fn value(&self) -> String {
|
pub fn value(&self) -> String {
|
||||||
match &self.value {
|
match &self.value {
|
||||||
NoneCodeValue::InlineComment { value } => value.clone(),
|
NonCodeValue::InlineComment { value } => value.clone(),
|
||||||
NoneCodeValue::BlockComment { value } => value.clone(),
|
NonCodeValue::BlockComment { value } => value.clone(),
|
||||||
NoneCodeValue::NewLineBlockComment { value } => value.clone(),
|
NonCodeValue::NewLineBlockComment { value } => value.clone(),
|
||||||
NoneCodeValue::NewLine => "\n\n".to_string(),
|
NonCodeValue::NewLine => "\n\n".to_string(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn format(&self, indentation: &str) -> String {
|
pub fn format(&self, indentation: &str) -> String {
|
||||||
match &self.value {
|
match &self.value {
|
||||||
NoneCodeValue::InlineComment { value } => format!(" // {}\n", value),
|
NonCodeValue::InlineComment { value } => format!(" // {}\n", value),
|
||||||
NoneCodeValue::BlockComment { value } => {
|
NonCodeValue::BlockComment { value } => {
|
||||||
let add_start_new_line = if self.start == 0 { "" } else { "\n" };
|
let add_start_new_line = if self.start == 0 { "" } else { "\n" };
|
||||||
if value.contains('\n') {
|
if value.contains('\n') {
|
||||||
format!("{}{}/* {} */\n", add_start_new_line, indentation, value)
|
format!("{}{}/* {} */\n", add_start_new_line, indentation, value)
|
||||||
@ -667,7 +667,7 @@ impl NoneCodeNode {
|
|||||||
format!("{}{}// {}\n", add_start_new_line, indentation, value)
|
format!("{}{}// {}\n", add_start_new_line, indentation, value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
NoneCodeValue::NewLineBlockComment { value } => {
|
NonCodeValue::NewLineBlockComment { value } => {
|
||||||
let add_start_new_line = if self.start == 0 { "" } else { "\n\n" };
|
let add_start_new_line = if self.start == 0 { "" } else { "\n\n" };
|
||||||
if value.contains('\n') {
|
if value.contains('\n') {
|
||||||
format!("{}{}/* {} */\n", add_start_new_line, indentation, value)
|
format!("{}{}/* {} */\n", add_start_new_line, indentation, value)
|
||||||
@ -675,7 +675,7 @@ impl NoneCodeNode {
|
|||||||
format!("{}{}// {}\n", add_start_new_line, indentation, value)
|
format!("{}{}// {}\n", add_start_new_line, indentation, value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
NoneCodeValue::NewLine => "\n\n".to_string(),
|
NonCodeValue::NewLine => "\n\n".to_string(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -683,7 +683,7 @@ impl NoneCodeNode {
|
|||||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
|
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
|
||||||
#[ts(export)]
|
#[ts(export)]
|
||||||
#[serde(tag = "type", rename_all = "camelCase")]
|
#[serde(tag = "type", rename_all = "camelCase")]
|
||||||
pub enum NoneCodeValue {
|
pub enum NonCodeValue {
|
||||||
/// An inline comment.
|
/// An inline comment.
|
||||||
/// An example of this is the following: `1 + 1 // This is an inline comment`.
|
/// An example of this is the following: `1 + 1 // This is an inline comment`.
|
||||||
InlineComment {
|
InlineComment {
|
||||||
@ -715,32 +715,32 @@ pub enum NoneCodeValue {
|
|||||||
#[derive(Debug, Default, Clone, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
|
#[derive(Debug, Default, Clone, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
|
||||||
#[ts(export)]
|
#[ts(export)]
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
pub struct NoneCodeMeta {
|
pub struct NonCodeMeta {
|
||||||
pub none_code_nodes: HashMap<usize, NoneCodeNode>,
|
pub non_code_nodes: HashMap<usize, NonCodeNode>,
|
||||||
pub start: Option<NoneCodeNode>,
|
pub start: Option<NonCodeNode>,
|
||||||
}
|
}
|
||||||
|
|
||||||
// implement Deserialize manually because we to force the keys of none_code_nodes to be usize
|
// implement Deserialize manually because we to force the keys of non_code_nodes to be usize
|
||||||
// and by default the ts type { [statementIndex: number]: NoneCodeNode } serializes to a string i.e. "0", "1", etc.
|
// and by default the ts type { [statementIndex: number]: NonCodeNode } serializes to a string i.e. "0", "1", etc.
|
||||||
impl<'de> Deserialize<'de> for NoneCodeMeta {
|
impl<'de> Deserialize<'de> for NonCodeMeta {
|
||||||
fn deserialize<D>(deserializer: D) -> Result<NoneCodeMeta, D::Error>
|
fn deserialize<D>(deserializer: D) -> Result<NonCodeMeta, D::Error>
|
||||||
where
|
where
|
||||||
D: serde::Deserializer<'de>,
|
D: serde::Deserializer<'de>,
|
||||||
{
|
{
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
struct NoneCodeMetaHelper {
|
struct NonCodeMetaHelper {
|
||||||
none_code_nodes: HashMap<String, NoneCodeNode>,
|
non_code_nodes: HashMap<String, NonCodeNode>,
|
||||||
start: Option<NoneCodeNode>,
|
start: Option<NonCodeNode>,
|
||||||
}
|
}
|
||||||
|
|
||||||
let helper = NoneCodeMetaHelper::deserialize(deserializer)?;
|
let helper = NonCodeMetaHelper::deserialize(deserializer)?;
|
||||||
let mut none_code_nodes = HashMap::new();
|
let mut non_code_nodes = HashMap::new();
|
||||||
for (key, value) in helper.none_code_nodes {
|
for (key, value) in helper.non_code_nodes {
|
||||||
none_code_nodes.insert(key.parse().map_err(serde::de::Error::custom)?, value);
|
non_code_nodes.insert(key.parse().map_err(serde::de::Error::custom)?, value);
|
||||||
}
|
}
|
||||||
Ok(NoneCodeMeta {
|
Ok(NonCodeMeta {
|
||||||
none_code_nodes,
|
non_code_nodes,
|
||||||
start: helper.start,
|
start: helper.start,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -2231,7 +2231,7 @@ pub struct PipeExpression {
|
|||||||
pub start: usize,
|
pub start: usize,
|
||||||
pub end: usize,
|
pub end: usize,
|
||||||
pub body: Vec<Value>,
|
pub body: Vec<Value>,
|
||||||
pub non_code_meta: NoneCodeMeta,
|
pub non_code_meta: NonCodeMeta,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl_value_meta!(PipeExpression);
|
impl_value_meta!(PipeExpression);
|
||||||
@ -2276,7 +2276,7 @@ impl PipeExpression {
|
|||||||
let indentation = options.get_indentation(indentation_level + 1);
|
let indentation = options.get_indentation(indentation_level + 1);
|
||||||
let mut s = statement.recast(options, indentation_level + 1, true);
|
let mut s = statement.recast(options, indentation_level + 1, true);
|
||||||
let non_code_meta = self.non_code_meta.clone();
|
let non_code_meta = self.non_code_meta.clone();
|
||||||
if let Some(non_code_meta_value) = non_code_meta.none_code_nodes.get(&index) {
|
if let Some(non_code_meta_value) = non_code_meta.non_code_nodes.get(&index) {
|
||||||
s += non_code_meta_value.format(&indentation).trim_end_matches('\n')
|
s += non_code_meta_value.format(&indentation).trim_end_matches('\n')
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2673,7 +2673,7 @@ show(part001)"#;
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_recast_with_std_and_non_stdlib() {
|
fn test_recast_with_std_and_non_stdlib() {
|
||||||
let some_program_string = r#"{"body":[{"type":"VariableDeclaration","start":0,"end":0,"declarations":[{"type":"VariableDeclarator","start":0,"end":0,"id":{"type":"Identifier","start":0,"end":0,"name":"part001"},"init":{"type":"PipeExpression","start":0,"end":0,"body":[{"type":"CallExpression","start":0,"end":0,"callee":{"type":"Identifier","start":0,"end":0,"name":"startSketchAt"},"function":{"type":"StdLib","func":{"name":"startSketchAt","summary":"","description":"","tags":[],"returnValue":{"type":"","required":false,"name":"","schema":{}},"args":[],"unpublished":false,"deprecated":false}},"optional":false,"arguments":[{"type":"Literal","start":0,"end":0,"value":"default","raw":"default"}]},{"type":"CallExpression","start":0,"end":0,"callee":{"type":"Identifier","start":0,"end":0,"name":"ry"},"function":{"type":"InMemory"},"optional":false,"arguments":[{"type":"Literal","start":0,"end":0,"value":90,"raw":"90"},{"type":"PipeSubstitution","start":0,"end":0}]},{"type":"CallExpression","start":0,"end":0,"callee":{"type":"Identifier","start":0,"end":0,"name":"line"},"function":{"type":"StdLib","func":{"name":"line","summary":"","description":"","tags":[],"returnValue":{"type":"","required":false,"name":"","schema":{}},"args":[],"unpublished":false,"deprecated":false}},"optional":false,"arguments":[{"type":"Literal","start":0,"end":0,"value":"default","raw":"default"},{"type":"PipeSubstitution","start":0,"end":0}]}],"nonCodeMeta":{"noneCodeNodes":{},"start":null}}}],"kind":"const"},{"type":"ExpressionStatement","start":0,"end":0,"expression":{"type":"CallExpression","start":0,"end":0,"callee":{"type":"Identifier","start":0,"end":0,"name":"show"},"function":{"type":"StdLib","func":{"name":"show","summary":"","description":"","tags":[],"returnValue":{"type":"","required":false,"name":"","schema":{}},"args":[],"unpublished":false,"deprecated":false}},"optional":false,"arguments":[{"type":"Identifier","start":0,"end":0,"name":"part001"}]}}],"start":0,"end":0,"nonCodeMeta":{"noneCodeNodes":{},"start":null}}"#;
|
let some_program_string = r#"{"body":[{"type":"VariableDeclaration","start":0,"end":0,"declarations":[{"type":"VariableDeclarator","start":0,"end":0,"id":{"type":"Identifier","start":0,"end":0,"name":"part001"},"init":{"type":"PipeExpression","start":0,"end":0,"body":[{"type":"CallExpression","start":0,"end":0,"callee":{"type":"Identifier","start":0,"end":0,"name":"startSketchAt"},"function":{"type":"StdLib","func":{"name":"startSketchAt","summary":"","description":"","tags":[],"returnValue":{"type":"","required":false,"name":"","schema":{}},"args":[],"unpublished":false,"deprecated":false}},"optional":false,"arguments":[{"type":"Literal","start":0,"end":0,"value":"default","raw":"default"}]},{"type":"CallExpression","start":0,"end":0,"callee":{"type":"Identifier","start":0,"end":0,"name":"ry"},"function":{"type":"InMemory"},"optional":false,"arguments":[{"type":"Literal","start":0,"end":0,"value":90,"raw":"90"},{"type":"PipeSubstitution","start":0,"end":0}]},{"type":"CallExpression","start":0,"end":0,"callee":{"type":"Identifier","start":0,"end":0,"name":"line"},"function":{"type":"StdLib","func":{"name":"line","summary":"","description":"","tags":[],"returnValue":{"type":"","required":false,"name":"","schema":{}},"args":[],"unpublished":false,"deprecated":false}},"optional":false,"arguments":[{"type":"Literal","start":0,"end":0,"value":"default","raw":"default"},{"type":"PipeSubstitution","start":0,"end":0}]}],"nonCodeMeta":{"nonCodeNodes":{},"start":null}}}],"kind":"const"},{"type":"ExpressionStatement","start":0,"end":0,"expression":{"type":"CallExpression","start":0,"end":0,"callee":{"type":"Identifier","start":0,"end":0,"name":"show"},"function":{"type":"StdLib","func":{"name":"show","summary":"","description":"","tags":[],"returnValue":{"type":"","required":false,"name":"","schema":{}},"args":[],"unpublished":false,"deprecated":false}},"optional":false,"arguments":[{"type":"Identifier","start":0,"end":0,"name":"part001"}]}}],"start":0,"end":0,"nonCodeMeta":{"nonCodeNodes":{},"start":null}}"#;
|
||||||
let some_program: crate::ast::types::Program = serde_json::from_str(some_program_string).unwrap();
|
let some_program: crate::ast::types::Program = serde_json::from_str(some_program_string).unwrap();
|
||||||
|
|
||||||
let recasted = some_program.recast(&Default::default(), 0);
|
let recasted = some_program.recast(&Default::default(), 0);
|
||||||
@ -2889,7 +2889,7 @@ const things = "things"
|
|||||||
let some_program_string = r#"let b = {
|
let some_program_string = r#"let b = {
|
||||||
"end": 141,
|
"end": 141,
|
||||||
"start": 125,
|
"start": 125,
|
||||||
"type": "NoneCodeNode",
|
"type": "NonCodeNode",
|
||||||
"value": "
|
"value": "
|
||||||
// a comment
|
// a comment
|
||||||
"
|
"
|
||||||
|
@ -3,8 +3,8 @@ use std::{collections::HashMap, str::FromStr};
|
|||||||
use crate::{
|
use crate::{
|
||||||
ast::types::{
|
ast::types::{
|
||||||
ArrayExpression, BinaryExpression, BinaryPart, BodyItem, CallExpression, ExpressionStatement,
|
ArrayExpression, BinaryExpression, BinaryPart, BodyItem, CallExpression, ExpressionStatement,
|
||||||
FunctionExpression, Identifier, Literal, LiteralIdentifier, MemberExpression, MemberObject, NoneCodeMeta,
|
FunctionExpression, Identifier, Literal, LiteralIdentifier, MemberExpression, MemberObject, NonCodeMeta,
|
||||||
NoneCodeNode, NoneCodeValue, ObjectExpression, ObjectKeyInfo, ObjectProperty, PipeExpression, PipeSubstitution,
|
NonCodeNode, NonCodeValue, ObjectExpression, ObjectKeyInfo, ObjectProperty, PipeExpression, PipeSubstitution,
|
||||||
Program, ReturnStatement, UnaryExpression, UnaryOperator, Value, VariableDeclaration, VariableDeclarator,
|
Program, ReturnStatement, UnaryExpression, UnaryOperator, Value, VariableDeclaration, VariableDeclarator,
|
||||||
VariableKind,
|
VariableKind,
|
||||||
},
|
},
|
||||||
@ -26,7 +26,7 @@ struct TokenReturn {
|
|||||||
struct TokenReturnWithNonCode {
|
struct TokenReturnWithNonCode {
|
||||||
token: Option<Token>,
|
token: Option<Token>,
|
||||||
index: usize,
|
index: usize,
|
||||||
non_code_node: Option<NoneCodeNode>,
|
non_code_node: Option<NonCodeNode>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Clone)]
|
#[derive(Debug, PartialEq, Clone)]
|
||||||
@ -57,7 +57,7 @@ struct ArrayReturn {
|
|||||||
struct PipeBodyReturn {
|
struct PipeBodyReturn {
|
||||||
body: Vec<Value>,
|
body: Vec<Value>,
|
||||||
last_index: usize,
|
last_index: usize,
|
||||||
non_code_meta: NoneCodeMeta,
|
non_code_meta: NonCodeMeta,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Clone)]
|
#[derive(Debug, PartialEq, Clone)]
|
||||||
@ -136,7 +136,7 @@ struct ReturnStatementResult {
|
|||||||
struct BodyResult {
|
struct BodyResult {
|
||||||
body: Vec<BodyItem>,
|
body: Vec<BodyItem>,
|
||||||
last_index: usize,
|
last_index: usize,
|
||||||
non_code_meta: NoneCodeMeta,
|
non_code_meta: NonCodeMeta,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Clone)]
|
#[derive(Debug, PartialEq, Clone)]
|
||||||
@ -183,8 +183,8 @@ impl Parser {
|
|||||||
let body = self.make_body(
|
let body = self.make_body(
|
||||||
0,
|
0,
|
||||||
vec![],
|
vec![],
|
||||||
NoneCodeMeta {
|
NonCodeMeta {
|
||||||
none_code_nodes: HashMap::new(),
|
non_code_nodes: HashMap::new(),
|
||||||
start: None,
|
start: None,
|
||||||
},
|
},
|
||||||
)?;
|
)?;
|
||||||
@ -269,7 +269,7 @@ impl Parser {
|
|||||||
Ok(index + 1)
|
Ok(index + 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn make_none_code_node(&self, index: usize) -> Result<(Option<NoneCodeNode>, usize), KclError> {
|
fn make_non_code_node(&self, index: usize) -> Result<(Option<NonCodeNode>, usize), KclError> {
|
||||||
let end_index = self.find_end_of_non_code_node(index)?;
|
let end_index = self.find_end_of_non_code_node(index)?;
|
||||||
let start_index = self.find_start_of_non_code_node(index)?;
|
let start_index = self.find_start_of_non_code_node(index)?;
|
||||||
let non_code_tokens = self.tokens[index..end_index].to_vec();
|
let non_code_tokens = self.tokens[index..end_index].to_vec();
|
||||||
@ -286,10 +286,10 @@ impl Parser {
|
|||||||
.contains("\n\n")
|
.contains("\n\n")
|
||||||
{
|
{
|
||||||
return Ok((
|
return Ok((
|
||||||
Some(NoneCodeNode {
|
Some(NonCodeNode {
|
||||||
start: self.tokens[start_index].start,
|
start: self.tokens[start_index].start,
|
||||||
end: self.tokens[end_index - 1].end,
|
end: self.tokens[end_index - 1].end,
|
||||||
value: NoneCodeValue::NewLine,
|
value: NonCodeValue::NewLine,
|
||||||
}),
|
}),
|
||||||
end_index - 1,
|
end_index - 1,
|
||||||
));
|
));
|
||||||
@ -330,17 +330,17 @@ impl Parser {
|
|||||||
let is_new_line_comment =
|
let is_new_line_comment =
|
||||||
start_end_string.starts_with('\n') || start_end_string.contains('\n') || start_index == 0 || index == 0;
|
start_end_string.starts_with('\n') || start_end_string.contains('\n') || start_index == 0 || index == 0;
|
||||||
|
|
||||||
let node = NoneCodeNode {
|
let node = NonCodeNode {
|
||||||
start: self.tokens[start_index].start,
|
start: self.tokens[start_index].start,
|
||||||
end: self.tokens[end_index - 1].end,
|
end: self.tokens[end_index - 1].end,
|
||||||
value: if start_end_string.starts_with("\n\n") && is_new_line_comment {
|
value: if start_end_string.starts_with("\n\n") && is_new_line_comment {
|
||||||
// Preserve if they want a whitespace line before the comment.
|
// Preserve if they want a whitespace line before the comment.
|
||||||
// But let's just allow one.
|
// But let's just allow one.
|
||||||
NoneCodeValue::NewLineBlockComment { value: full_string }
|
NonCodeValue::NewLineBlockComment { value: full_string }
|
||||||
} else if is_new_line_comment {
|
} else if is_new_line_comment {
|
||||||
NoneCodeValue::BlockComment { value: full_string }
|
NonCodeValue::BlockComment { value: full_string }
|
||||||
} else {
|
} else {
|
||||||
NoneCodeValue::InlineComment { value: full_string }
|
NonCodeValue::InlineComment { value: full_string }
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
Ok((Some(node), end_index - 1))
|
Ok((Some(node), end_index - 1))
|
||||||
@ -366,7 +366,7 @@ impl Parser {
|
|||||||
};
|
};
|
||||||
|
|
||||||
if is_not_code_token(token) {
|
if is_not_code_token(token) {
|
||||||
let non_code_node = self.make_none_code_node(new_index)?;
|
let non_code_node = self.make_non_code_node(new_index)?;
|
||||||
let new_new_index = non_code_node.1 + 1;
|
let new_new_index = non_code_node.1 + 1;
|
||||||
let bonus_non_code_node = non_code_node.0;
|
let bonus_non_code_node = non_code_node.0;
|
||||||
|
|
||||||
@ -1027,13 +1027,13 @@ impl Parser {
|
|||||||
&self,
|
&self,
|
||||||
index: usize,
|
index: usize,
|
||||||
previous_values: Vec<Value>,
|
previous_values: Vec<Value>,
|
||||||
previous_non_code_meta: Option<NoneCodeMeta>,
|
previous_non_code_meta: Option<NonCodeMeta>,
|
||||||
) -> Result<PipeBodyReturn, KclError> {
|
) -> Result<PipeBodyReturn, KclError> {
|
||||||
let non_code_meta = match previous_non_code_meta {
|
let non_code_meta = match previous_non_code_meta {
|
||||||
Some(meta) => meta,
|
Some(meta) => meta,
|
||||||
None => NoneCodeMeta {
|
None => NonCodeMeta {
|
||||||
start: None,
|
start: None,
|
||||||
none_code_nodes: HashMap::new(),
|
non_code_nodes: HashMap::new(),
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
let current_token = self.get_token(index)?;
|
let current_token = self.get_token(index)?;
|
||||||
@ -1060,10 +1060,10 @@ impl Parser {
|
|||||||
non_code_meta,
|
non_code_meta,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
let mut _non_code_meta: NoneCodeMeta;
|
let mut _non_code_meta: NonCodeMeta;
|
||||||
if let Some(node) = next_pipe.non_code_node {
|
if let Some(node) = next_pipe.non_code_node {
|
||||||
_non_code_meta = non_code_meta;
|
_non_code_meta = non_code_meta;
|
||||||
_non_code_meta.none_code_nodes.insert(previous_values.len(), node);
|
_non_code_meta.non_code_nodes.insert(previous_values.len(), node);
|
||||||
} else {
|
} else {
|
||||||
_non_code_meta = non_code_meta;
|
_non_code_meta = non_code_meta;
|
||||||
}
|
}
|
||||||
@ -1587,7 +1587,7 @@ impl Parser {
|
|||||||
&self,
|
&self,
|
||||||
token_index: usize,
|
token_index: usize,
|
||||||
previous_body: Vec<BodyItem>,
|
previous_body: Vec<BodyItem>,
|
||||||
previous_non_code_meta: NoneCodeMeta,
|
previous_non_code_meta: NonCodeMeta,
|
||||||
) -> Result<BodyResult, KclError> {
|
) -> Result<BodyResult, KclError> {
|
||||||
let mut non_code_meta = previous_non_code_meta;
|
let mut non_code_meta = previous_non_code_meta;
|
||||||
if self.tokens.is_empty() {
|
if self.tokens.is_empty() {
|
||||||
@ -1620,7 +1620,7 @@ impl Parser {
|
|||||||
if previous_body.is_empty() {
|
if previous_body.is_empty() {
|
||||||
non_code_meta.start = next_token.non_code_node;
|
non_code_meta.start = next_token.non_code_node;
|
||||||
} else {
|
} else {
|
||||||
non_code_meta.none_code_nodes.insert(previous_body.len(), node.clone());
|
non_code_meta.non_code_nodes.insert(previous_body.len(), node.clone());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return self.make_body(next_token.index, previous_body, non_code_meta);
|
return self.make_body(next_token.index, previous_body, non_code_meta);
|
||||||
@ -1628,14 +1628,14 @@ impl Parser {
|
|||||||
|
|
||||||
let next = self.next_meaningful_token(token_index, None)?;
|
let next = self.next_meaningful_token(token_index, None)?;
|
||||||
if let Some(node) = &next.non_code_node {
|
if let Some(node) = &next.non_code_node {
|
||||||
non_code_meta.none_code_nodes.insert(previous_body.len(), node.clone());
|
non_code_meta.non_code_nodes.insert(previous_body.len(), node.clone());
|
||||||
}
|
}
|
||||||
|
|
||||||
if token.token_type == TokenType::Keyword && VariableKind::from_str(&token.value).is_ok() {
|
if token.token_type == TokenType::Keyword && VariableKind::from_str(&token.value).is_ok() {
|
||||||
let declaration = self.make_variable_declaration(token_index)?;
|
let declaration = self.make_variable_declaration(token_index)?;
|
||||||
let next_thing = self.next_meaningful_token(declaration.last_index, None)?;
|
let next_thing = self.next_meaningful_token(declaration.last_index, None)?;
|
||||||
if let Some(node) = &next_thing.non_code_node {
|
if let Some(node) = &next_thing.non_code_node {
|
||||||
non_code_meta.none_code_nodes.insert(previous_body.len(), node.clone());
|
non_code_meta.non_code_nodes.insert(previous_body.len(), node.clone());
|
||||||
}
|
}
|
||||||
let mut _previous_body = previous_body;
|
let mut _previous_body = previous_body;
|
||||||
_previous_body.push(BodyItem::VariableDeclaration(VariableDeclaration {
|
_previous_body.push(BodyItem::VariableDeclaration(VariableDeclaration {
|
||||||
@ -1656,7 +1656,7 @@ impl Parser {
|
|||||||
let statement = self.make_return_statement(token_index)?;
|
let statement = self.make_return_statement(token_index)?;
|
||||||
let next_thing = self.next_meaningful_token(statement.last_index, None)?;
|
let next_thing = self.next_meaningful_token(statement.last_index, None)?;
|
||||||
if let Some(node) = &next_thing.non_code_node {
|
if let Some(node) = &next_thing.non_code_node {
|
||||||
non_code_meta.none_code_nodes.insert(previous_body.len(), node.clone());
|
non_code_meta.non_code_nodes.insert(previous_body.len(), node.clone());
|
||||||
}
|
}
|
||||||
let mut _previous_body = previous_body;
|
let mut _previous_body = previous_body;
|
||||||
_previous_body.push(BodyItem::ReturnStatement(ReturnStatement {
|
_previous_body.push(BodyItem::ReturnStatement(ReturnStatement {
|
||||||
@ -1680,7 +1680,7 @@ impl Parser {
|
|||||||
let expression = self.make_expression_statement(token_index)?;
|
let expression = self.make_expression_statement(token_index)?;
|
||||||
let next_thing = self.next_meaningful_token(expression.last_index, None)?;
|
let next_thing = self.next_meaningful_token(expression.last_index, None)?;
|
||||||
if let Some(node) = &next_thing.non_code_node {
|
if let Some(node) = &next_thing.non_code_node {
|
||||||
non_code_meta.none_code_nodes.insert(previous_body.len(), node.clone());
|
non_code_meta.non_code_nodes.insert(previous_body.len(), node.clone());
|
||||||
}
|
}
|
||||||
let mut _previous_body = previous_body;
|
let mut _previous_body = previous_body;
|
||||||
_previous_body.push(BodyItem::ExpressionStatement(ExpressionStatement {
|
_previous_body.push(BodyItem::ExpressionStatement(ExpressionStatement {
|
||||||
@ -1703,7 +1703,7 @@ impl Parser {
|
|||||||
&& next_thing_token.token_type == TokenType::Operator
|
&& next_thing_token.token_type == TokenType::Operator
|
||||||
{
|
{
|
||||||
if let Some(node) = &next_thing.non_code_node {
|
if let Some(node) = &next_thing.non_code_node {
|
||||||
non_code_meta.none_code_nodes.insert(previous_body.len(), node.clone());
|
non_code_meta.non_code_nodes.insert(previous_body.len(), node.clone());
|
||||||
}
|
}
|
||||||
let expression = self.make_expression_statement(token_index)?;
|
let expression = self.make_expression_statement(token_index)?;
|
||||||
let mut _previous_body = previous_body;
|
let mut _previous_body = previous_body;
|
||||||
@ -1734,8 +1734,8 @@ impl Parser {
|
|||||||
BodyResult {
|
BodyResult {
|
||||||
body: vec![],
|
body: vec![],
|
||||||
last_index: next_token_index,
|
last_index: next_token_index,
|
||||||
non_code_meta: NoneCodeMeta {
|
non_code_meta: NonCodeMeta {
|
||||||
none_code_nodes: HashMap::new(),
|
non_code_nodes: HashMap::new(),
|
||||||
start: None,
|
start: None,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -1743,8 +1743,8 @@ impl Parser {
|
|||||||
self.make_body(
|
self.make_body(
|
||||||
next_token_index,
|
next_token_index,
|
||||||
vec![],
|
vec![],
|
||||||
NoneCodeMeta {
|
NonCodeMeta {
|
||||||
none_code_nodes: HashMap::new(),
|
non_code_nodes: HashMap::new(),
|
||||||
start: None,
|
start: None,
|
||||||
},
|
},
|
||||||
)?
|
)?
|
||||||
@ -1870,16 +1870,16 @@ mod tests {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn test_make_none_code_node() {
|
fn test_make_non_code_node() {
|
||||||
let tokens = crate::tokeniser::lexer("log(5, \"hello\", aIdentifier)");
|
let tokens = crate::tokeniser::lexer("log(5, \"hello\", aIdentifier)");
|
||||||
let parser = Parser::new(tokens);
|
let parser = Parser::new(tokens);
|
||||||
let index = 4;
|
let index = 4;
|
||||||
let expected_output = (None, 4);
|
let expected_output = (None, 4);
|
||||||
assert_eq!(parser.make_none_code_node(index).unwrap(), expected_output);
|
assert_eq!(parser.make_non_code_node(index).unwrap(), expected_output);
|
||||||
|
|
||||||
let index = 7;
|
let index = 7;
|
||||||
let expected_output = (None, 7);
|
let expected_output = (None, 7);
|
||||||
assert_eq!(parser.make_none_code_node(index).unwrap(), expected_output);
|
assert_eq!(parser.make_non_code_node(index).unwrap(), expected_output);
|
||||||
let tokens = crate::tokeniser::lexer(
|
let tokens = crate::tokeniser::lexer(
|
||||||
r#"
|
r#"
|
||||||
const yo = { a: { b: { c: '123' } } }
|
const yo = { a: { b: { c: '123' } } }
|
||||||
@ -1889,28 +1889,28 @@ const key = 'c'"#,
|
|||||||
let parser = Parser::new(tokens);
|
let parser = Parser::new(tokens);
|
||||||
let index = 0;
|
let index = 0;
|
||||||
let expected_output = (None, 0);
|
let expected_output = (None, 0);
|
||||||
assert_eq!(parser.make_none_code_node(index).unwrap(), expected_output);
|
assert_eq!(parser.make_non_code_node(index).unwrap(), expected_output);
|
||||||
|
|
||||||
let index = 2;
|
let index = 2;
|
||||||
let expected_output = (None, 2);
|
let expected_output = (None, 2);
|
||||||
assert_eq!(parser.make_none_code_node(index).unwrap(), expected_output);
|
assert_eq!(parser.make_non_code_node(index).unwrap(), expected_output);
|
||||||
|
|
||||||
let index = 2;
|
let index = 2;
|
||||||
let expected_output = (None, 2);
|
let expected_output = (None, 2);
|
||||||
assert_eq!(parser.make_none_code_node(index).unwrap(), expected_output);
|
assert_eq!(parser.make_non_code_node(index).unwrap(), expected_output);
|
||||||
|
|
||||||
let index = 29;
|
let index = 29;
|
||||||
let expected_output = (
|
let expected_output = (
|
||||||
Some(NoneCodeNode {
|
Some(NonCodeNode {
|
||||||
start: 38,
|
start: 38,
|
||||||
end: 60,
|
end: 60,
|
||||||
value: NoneCodeValue::BlockComment {
|
value: NonCodeValue::BlockComment {
|
||||||
value: "this is a comment".to_string(),
|
value: "this is a comment".to_string(),
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
31,
|
31,
|
||||||
);
|
);
|
||||||
assert_eq!(parser.make_none_code_node(index).unwrap(), expected_output);
|
assert_eq!(parser.make_non_code_node(index).unwrap(), expected_output);
|
||||||
let tokens = crate::tokeniser::lexer(
|
let tokens = crate::tokeniser::lexer(
|
||||||
r#"const mySketch = startSketchAt([0,0])
|
r#"const mySketch = startSketchAt([0,0])
|
||||||
|> lineTo({ to: [0, 1], tag: 'myPath' }, %)
|
|> lineTo({ to: [0, 1], tag: 'myPath' }, %)
|
||||||
@ -1923,16 +1923,16 @@ const key = 'c'"#,
|
|||||||
let parser = Parser::new(tokens);
|
let parser = Parser::new(tokens);
|
||||||
let index = 57;
|
let index = 57;
|
||||||
let expected_output = (
|
let expected_output = (
|
||||||
Some(NoneCodeNode {
|
Some(NonCodeNode {
|
||||||
start: 106,
|
start: 106,
|
||||||
end: 166,
|
end: 166,
|
||||||
value: NoneCodeValue::BlockComment {
|
value: NonCodeValue::BlockComment {
|
||||||
value: "this is\n a comment\n spanning a few lines".to_string(),
|
value: "this is\n a comment\n spanning a few lines".to_string(),
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
59,
|
59,
|
||||||
);
|
);
|
||||||
assert_eq!(parser.make_none_code_node(index).unwrap(), expected_output);
|
assert_eq!(parser.make_non_code_node(index).unwrap(), expected_output);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -2905,8 +2905,8 @@ show(mySk1)"#;
|
|||||||
.make_body(
|
.make_body(
|
||||||
0,
|
0,
|
||||||
vec![],
|
vec![],
|
||||||
NoneCodeMeta {
|
NonCodeMeta {
|
||||||
none_code_nodes: HashMap::new(),
|
non_code_nodes: HashMap::new(),
|
||||||
start: None,
|
start: None,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
@ -2943,8 +2943,8 @@ show(mySk1)"#;
|
|||||||
})),
|
})),
|
||||||
})),
|
})),
|
||||||
})],
|
})],
|
||||||
non_code_meta: NoneCodeMeta {
|
non_code_meta: NonCodeMeta {
|
||||||
none_code_nodes: Default::default(),
|
non_code_nodes: Default::default(),
|
||||||
start: None,
|
start: None,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
@ -3278,8 +3278,8 @@ e
|
|||||||
}],
|
}],
|
||||||
kind: VariableKind::Const,
|
kind: VariableKind::Const,
|
||||||
})],
|
})],
|
||||||
non_code_meta: NoneCodeMeta {
|
non_code_meta: NonCodeMeta {
|
||||||
none_code_nodes: Default::default(),
|
non_code_nodes: Default::default(),
|
||||||
start: None,
|
start: None,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user