Compare commits

...

2 Commits

Author SHA1 Message Date
c3ac4d085e No more JSON numbers in AST types 2023-10-27 13:39:57 -07:00
f60a06dffc Fix typos and unnecessary import paths 2023-10-27 11:32:43 -07:00
6 changed files with 163 additions and 101 deletions

View File

@ -1,12 +1,15 @@
//! Data types for the AST. //! Data types for the AST.
use std::{collections::HashMap, fmt::Write}; use std::{
collections::HashMap,
fmt::{Formatter, Write},
};
use anyhow::Result; use anyhow::{anyhow, Result};
use parse_display::{Display, FromStr}; use parse_display::{Display, FromStr};
use schemars::JsonSchema; use schemars::JsonSchema;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::Map; use serde_json::{Map, Number as JNumber, Value as JValue};
use tower_lsp::lsp_types::{CompletionItem, CompletionItemKind, DocumentSymbol, Range as LspRange, SymbolKind}; use tower_lsp::lsp_types::{CompletionItem, CompletionItemKind, DocumentSymbol, Range as LspRange, SymbolKind};
use crate::{ use crate::{
@ -371,13 +374,13 @@ impl BodyItem {
} }
} }
impl From<BodyItem> for crate::executor::SourceRange { impl From<BodyItem> for SourceRange {
fn from(item: BodyItem) -> Self { fn from(item: BodyItem) -> Self {
Self([item.start(), item.end()]) Self([item.start(), item.end()])
} }
} }
impl From<&BodyItem> for crate::executor::SourceRange { impl From<&BodyItem> for SourceRange {
fn from(item: &BodyItem) -> Self { fn from(item: &BodyItem) -> Self {
Self([item.start(), item.end()]) Self([item.start(), item.end()])
} }
@ -534,13 +537,13 @@ impl Value {
} }
} }
impl From<Value> for crate::executor::SourceRange { impl From<Value> for SourceRange {
fn from(value: Value) -> Self { fn from(value: Value) -> Self {
Self([value.start(), value.end()]) Self([value.start(), value.end()])
} }
} }
impl From<&Value> for crate::executor::SourceRange { impl From<&Value> for SourceRange {
fn from(value: &Value) -> Self { fn from(value: &Value) -> Self {
Self([value.start(), value.end()]) Self([value.start(), value.end()])
} }
@ -558,13 +561,13 @@ pub enum BinaryPart {
MemberExpression(Box<MemberExpression>), MemberExpression(Box<MemberExpression>),
} }
impl From<BinaryPart> for crate::executor::SourceRange { impl From<BinaryPart> for SourceRange {
fn from(value: BinaryPart) -> Self { fn from(value: BinaryPart) -> Self {
Self([value.start(), value.end()]) Self([value.start(), value.end()])
} }
} }
impl From<&BinaryPart> for crate::executor::SourceRange { impl From<&BinaryPart> for SourceRange {
fn from(value: &BinaryPart) -> Self { fn from(value: &BinaryPart) -> Self {
Self([value.start(), value.end()]) Self([value.start(), value.end()])
} }
@ -640,7 +643,7 @@ impl BinaryPart {
pipe_info: &mut PipeInfo, pipe_info: &mut PipeInfo,
ctx: &ExecutorContext, ctx: &ExecutorContext,
) -> Result<MemoryItem, KclError> { ) -> Result<MemoryItem, KclError> {
// We DO NOT set this gloablly because if we did and this was called inside a pipe it would // We DO NOT set this globally because if we did and this was called inside a pipe it would
// stop the execution of the pipe. // stop the execution of the pipe.
// THIS IS IMPORTANT. // THIS IS IMPORTANT.
let mut new_pipe_info = pipe_info.clone(); let mut new_pipe_info = pipe_info.clone();
@ -930,7 +933,7 @@ impl CallExpression {
binary_expression.get_result(memory, pipe_info, ctx).await? binary_expression.get_result(memory, pipe_info, ctx).await?
} }
Value::CallExpression(call_expression) => { Value::CallExpression(call_expression) => {
// We DO NOT set this gloablly because if we did and this was called inside a pipe it would // We DO NOT set this globally because if we did and this was called inside a pipe it would
// stop the execution of the pipe. // stop the execution of the pipe.
// THIS IS IMPORTANT. // THIS IS IMPORTANT.
let mut new_pipe_info = pipe_info.clone(); let mut new_pipe_info = pipe_info.clone();
@ -1312,10 +1315,70 @@ impl VariableDeclarator {
pub struct Literal { pub struct Literal {
pub start: usize, pub start: usize,
pub end: usize, pub end: usize,
pub value: serde_json::Value, pub value: LiteralValue,
pub raw: String, pub raw: String,
} }
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
#[ts(export)]
#[serde(tag = "type")]
pub enum LiteralValue {
// TODO: Use Cow<str> here so that static strings don't require alloc
String(String),
Number(JNumber),
}
impl std::fmt::Display for LiteralValue {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
JValue::from(self.to_owned()).fmt(f)
}
}
impl From<LiteralValue> for JValue {
fn from(value: LiteralValue) -> Self {
match value {
LiteralValue::String(x) => JValue::String(x),
LiteralValue::Number(x) => JValue::Number(x),
}
}
}
impl From<&'static str> for LiteralValue {
fn from(value: &'static str) -> Self {
Self::String(value.to_owned())
}
}
impl From<String> for LiteralValue {
fn from(value: String) -> Self {
Self::String(value)
}
}
impl From<JNumber> for LiteralValue {
fn from(value: JNumber) -> Self {
Self::Number(value)
}
}
impl From<u64> for LiteralValue {
fn from(value: u64) -> Self {
Self::Number(value.into())
}
}
impl From<i64> for LiteralValue {
fn from(value: i64) -> Self {
Self::Number(value.into())
}
}
impl From<f64> for LiteralValue {
fn from(value: f64) -> Self {
JNumber::from_f64(value)
.map(Self::Number)
.expect("Cannot use NaN or Inf here. TODO stop using json numbers here")
}
}
impl_value_meta!(Literal); impl_value_meta!(Literal);
impl From<Literal> for Value { impl From<Literal> for Value {
@ -1325,7 +1388,7 @@ impl From<Literal> for Value {
} }
impl Literal { impl Literal {
pub fn new(value: serde_json::Value) -> Self { pub fn new(value: LiteralValue) -> Self {
Self { Self {
start: 0, start: 0,
end: 0, end: 0,
@ -1343,7 +1406,7 @@ impl Literal {
} }
fn recast(&self) -> String { fn recast(&self) -> String {
if let serde_json::Value::String(value) = &self.value { if let JValue::String(value) = &self.value {
let quote = if self.raw.trim().starts_with('"') { '"' } else { '\'' }; let quote = if self.raw.trim().starts_with('"') { '"' } else { '\'' };
format!("{}{}{}", quote, value, quote) format!("{}{}{}", quote, value, quote)
} else { } else {
@ -1355,7 +1418,7 @@ impl Literal {
impl From<Literal> for MemoryItem { impl From<Literal> for MemoryItem {
fn from(literal: Literal) -> Self { fn from(literal: Literal) -> Self {
MemoryItem::UserVal(UserVal { MemoryItem::UserVal(UserVal {
value: literal.value.clone(), value: literal.value.into(),
meta: vec![Metadata { meta: vec![Metadata {
source_range: literal.into(), source_range: literal.into(),
}], }],
@ -1366,7 +1429,7 @@ impl From<Literal> for MemoryItem {
impl From<&Box<Literal>> for MemoryItem { impl From<&Box<Literal>> for MemoryItem {
fn from(literal: &Box<Literal>) -> Self { fn from(literal: &Box<Literal>) -> Self {
MemoryItem::UserVal(UserVal { MemoryItem::UserVal(UserVal {
value: literal.value.clone(), value: literal.value.into(),
meta: vec![Metadata { meta: vec![Metadata {
source_range: literal.into(), source_range: literal.into(),
}], }],
@ -1552,7 +1615,7 @@ impl ArrayExpression {
binary_expression.get_result(memory, pipe_info, ctx).await? binary_expression.get_result(memory, pipe_info, ctx).await?
} }
Value::CallExpression(call_expression) => { Value::CallExpression(call_expression) => {
// We DO NOT set this gloablly because if we did and this was called inside a pipe it would // We DO NOT set this globally because if we did and this was called inside a pipe it would
// stop the execution of the pipe. // stop the execution of the pipe.
// THIS IS IMPORTANT. // THIS IS IMPORTANT.
let mut new_pipe_info = pipe_info.clone(); let mut new_pipe_info = pipe_info.clone();
@ -1703,7 +1766,7 @@ impl ObjectExpression {
binary_expression.get_result(memory, pipe_info, ctx).await? binary_expression.get_result(memory, pipe_info, ctx).await?
} }
Value::CallExpression(call_expression) => { Value::CallExpression(call_expression) => {
// We DO NOT set this gloablly because if we did and this was called inside a pipe it would // We DO NOT set this globally because if we did and this was called inside a pipe it would
// stop the execution of the pipe. // stop the execution of the pipe.
// THIS IS IMPORTANT. // THIS IS IMPORTANT.
let mut new_pipe_info = pipe_info.clone(); let mut new_pipe_info = pipe_info.clone();
@ -1831,13 +1894,13 @@ impl MemberObject {
} }
} }
impl From<MemberObject> for crate::executor::SourceRange { impl From<MemberObject> for SourceRange {
fn from(obj: MemberObject) -> Self { fn from(obj: MemberObject) -> Self {
Self([obj.start(), obj.end()]) Self([obj.start(), obj.end()])
} }
} }
impl From<&MemberObject> for crate::executor::SourceRange { impl From<&MemberObject> for SourceRange {
fn from(obj: &MemberObject) -> Self { fn from(obj: &MemberObject) -> Self {
Self([obj.start(), obj.end()]) Self([obj.start(), obj.end()])
} }
@ -1867,13 +1930,13 @@ impl LiteralIdentifier {
} }
} }
impl From<LiteralIdentifier> for crate::executor::SourceRange { impl From<LiteralIdentifier> for SourceRange {
fn from(id: LiteralIdentifier) -> Self { fn from(id: LiteralIdentifier) -> Self {
Self([id.start(), id.end()]) Self([id.start(), id.end()])
} }
} }
impl From<&LiteralIdentifier> for crate::executor::SourceRange { impl From<&LiteralIdentifier> for SourceRange {
fn from(id: &LiteralIdentifier) -> Self { fn from(id: &LiteralIdentifier) -> Self {
Self([id.start(), id.end()]) Self([id.start(), id.end()])
} }
@ -1940,10 +2003,10 @@ impl MemberExpression {
let array_json = array.get_json_value()?; let array_json = array.get_json_value()?;
if let serde_json::Value::Array(array) = array_json { if let JValue::Array(array) = array_json {
if let Some(value) = array.get(index) { if let Some(value) = array.get(index) {
Ok(MemoryItem::UserVal(UserVal { Ok(MemoryItem::UserVal(UserVal {
value: value.clone(), value: value.into(),
meta: vec![Metadata { meta: vec![Metadata {
source_range: self.into(), source_range: self.into(),
}], }],
@ -1966,11 +2029,11 @@ impl MemberExpression {
let property_name = match &self.property { let property_name = match &self.property {
LiteralIdentifier::Identifier(identifier) => identifier.name.to_string(), LiteralIdentifier::Identifier(identifier) => identifier.name.to_string(),
LiteralIdentifier::Literal(literal) => { LiteralIdentifier::Literal(literal) => {
let value = literal.value.clone(); let value = literal.value.into();
// Parse this as a string. // Parse this as a string.
if let serde_json::Value::String(string) = value { if let JValue::String(string) = value {
string string
} else if let serde_json::Value::Number(_) = &value { } else if let JValue::Number(_) = &value {
// It can also be a number if we are getting a member of an array. // It can also be a number if we are getting a member of an array.
return self.get_result_array(memory, parse_json_number_as_usize(&value, self.into())?); return self.get_result_array(memory, parse_json_number_as_usize(&value, self.into())?);
} else { } else {
@ -1992,10 +2055,10 @@ impl MemberExpression {
let object_json = object.get_json_value()?; let object_json = object.get_json_value()?;
if let serde_json::Value::Object(map) = object_json { if let JValue::Object(map) = object_json {
if let Some(value) = map.get(&property_name) { if let Some(value) = map.get(&property_name) {
Ok(MemoryItem::UserVal(UserVal { Ok(MemoryItem::UserVal(UserVal {
value: value.clone(), value: value.into(),
meta: vec![Metadata { meta: vec![Metadata {
source_range: self.into(), source_range: self.into(),
}], }],
@ -2133,7 +2196,7 @@ impl BinaryExpression {
pipe_info: &mut PipeInfo, pipe_info: &mut PipeInfo,
ctx: &ExecutorContext, ctx: &ExecutorContext,
) -> Result<MemoryItem, KclError> { ) -> Result<MemoryItem, KclError> {
// We DO NOT set this gloablly because if we did and this was called inside a pipe it would // We DO NOT set this globally because if we did and this was called inside a pipe it would
// stop the execution of the pipe. // stop the execution of the pipe.
// THIS IS IMPORTANT. // THIS IS IMPORTANT.
let mut new_pipe_info = pipe_info.clone(); let mut new_pipe_info = pipe_info.clone();
@ -2156,7 +2219,7 @@ impl BinaryExpression {
parse_json_value_as_string(&left_json_value), parse_json_value_as_string(&left_json_value),
parse_json_value_as_string(&right_json_value), parse_json_value_as_string(&right_json_value),
) { ) {
let value = serde_json::Value::String(format!("{}{}", left, right)); let value = JValue::String(format!("{}{}", left, right)).into();
return Ok(MemoryItem::UserVal(UserVal { return Ok(MemoryItem::UserVal(UserVal {
value, value,
meta: vec![Metadata { meta: vec![Metadata {
@ -2169,7 +2232,7 @@ impl BinaryExpression {
let left = parse_json_number_as_f64(&left_json_value, self.left.clone().into())?; let left = parse_json_number_as_f64(&left_json_value, self.left.clone().into())?;
let right = parse_json_number_as_f64(&right_json_value, self.right.clone().into())?; let right = parse_json_number_as_f64(&right_json_value, self.right.clone().into())?;
let value: serde_json::Value = match self.operator { let value: JNumber = match self.operator {
BinaryOperator::Add => (left + right).into(), BinaryOperator::Add => (left + right).into(),
BinaryOperator::Sub => (left - right).into(), BinaryOperator::Sub => (left - right).into(),
BinaryOperator::Mul => (left * right).into(), BinaryOperator::Mul => (left * right).into(),
@ -2178,7 +2241,7 @@ impl BinaryExpression {
}; };
Ok(MemoryItem::UserVal(UserVal { Ok(MemoryItem::UserVal(UserVal {
value, value: value.into(),
meta: vec![Metadata { meta: vec![Metadata {
source_range: self.into(), source_range: self.into(),
}], }],
@ -2192,8 +2255,8 @@ impl BinaryExpression {
} }
} }
pub fn parse_json_number_as_f64(j: &serde_json::Value, source_range: SourceRange) -> Result<f64, KclError> { pub fn parse_json_number_as_f64(j: &JValue, source_range: SourceRange) -> Result<f64, KclError> {
if let serde_json::Value::Number(n) = &j { if let JValue::Number(n) = &j {
n.as_f64().ok_or_else(|| { n.as_f64().ok_or_else(|| {
KclError::Syntax(KclErrorDetails { KclError::Syntax(KclErrorDetails {
source_ranges: vec![source_range], source_ranges: vec![source_range],
@ -2208,8 +2271,8 @@ pub fn parse_json_number_as_f64(j: &serde_json::Value, source_range: SourceRange
} }
} }
pub fn parse_json_number_as_usize(j: &serde_json::Value, source_range: SourceRange) -> Result<usize, KclError> { pub fn parse_json_number_as_usize(j: &JValue, source_range: SourceRange) -> Result<usize, KclError> {
if let serde_json::Value::Number(n) = &j { if let JValue::Number(n) = &j {
Ok(n.as_i64().ok_or_else(|| { Ok(n.as_i64().ok_or_else(|| {
KclError::Syntax(KclErrorDetails { KclError::Syntax(KclErrorDetails {
source_ranges: vec![source_range], source_ranges: vec![source_range],
@ -2224,8 +2287,8 @@ pub fn parse_json_number_as_usize(j: &serde_json::Value, source_range: SourceRan
} }
} }
pub fn parse_json_value_as_string(j: &serde_json::Value) -> Option<String> { pub fn parse_json_value_as_string(j: &JValue) -> Option<String> {
if let serde_json::Value::String(n) = &j { if let JValue::String(n) = &j {
Some(n.clone()) Some(n.clone())
} else { } else {
None None
@ -2307,7 +2370,7 @@ impl UnaryExpression {
pipe_info: &mut PipeInfo, pipe_info: &mut PipeInfo,
ctx: &ExecutorContext, ctx: &ExecutorContext,
) -> Result<MemoryItem, KclError> { ) -> Result<MemoryItem, KclError> {
// We DO NOT set this gloablly because if we did and this was called inside a pipe it would // We DO NOT set this globally because if we did and this was called inside a pipe it would
// stop the execution of the pipe. // stop the execution of the pipe.
// THIS IS IMPORTANT. // THIS IS IMPORTANT.
let mut new_pipe_info = pipe_info.clone(); let mut new_pipe_info = pipe_info.clone();

View File

@ -10,7 +10,7 @@ use serde::{Deserialize, Serialize};
use tower_lsp::lsp_types::{Position as LspPosition, Range as LspRange}; use tower_lsp::lsp_types::{Position as LspPosition, Range as LspRange};
use crate::{ use crate::{
ast::types::{BodyItem, Function, FunctionExpression, Value}, ast::types::{BodyItem, Function, FunctionExpression, LiteralValue, Value},
engine::{EngineConnection, EngineManager}, engine::{EngineConnection, EngineManager},
errors::{KclError, KclErrorDetails}, errors::{KclError, KclErrorDetails},
}; };
@ -282,7 +282,7 @@ impl DefaultPlanes {
#[ts(export)] #[ts(export)]
#[serde(tag = "type", rename_all = "camelCase")] #[serde(tag = "type", rename_all = "camelCase")]
pub struct UserVal { pub struct UserVal {
pub value: serde_json::Value, pub value: LiteralValue,
#[serde(rename = "__meta")] #[serde(rename = "__meta")]
pub meta: Vec<Metadata>, pub meta: Vec<Metadata>,
} }
@ -336,7 +336,7 @@ impl From<MemoryItem> for Vec<SourceRange> {
impl MemoryItem { impl MemoryItem {
pub fn get_json_value(&self) -> Result<serde_json::Value, KclError> { pub fn get_json_value(&self) -> Result<serde_json::Value, KclError> {
if let MemoryItem::UserVal(user_val) = self { if let MemoryItem::UserVal(user_val) = self {
Ok(user_val.value.clone()) Ok(user_val.value.into())
} else { } else {
serde_json::to_value(self).map_err(|err| { serde_json::to_value(self).map_err(|err| {
KclError::Semantic(KclErrorDetails { KclError::Semantic(KclErrorDetails {

View File

@ -575,7 +575,7 @@ impl ReversePolishNotation {
), ),
MathExpression::ExtendedLiteral(lit) => ( MathExpression::ExtendedLiteral(lit) => (
BinaryPart::Literal(Box::new(Literal { BinaryPart::Literal(Box::new(Literal {
value: lit.value.clone(), value: lit.value.into(),
start: lit.start, start: lit.start,
end: lit.end, end: lit.end,
raw: lit.raw.clone(), raw: lit.raw.clone(),
@ -613,7 +613,7 @@ impl ReversePolishNotation {
), ),
MathExpression::ExtendedLiteral(lit) => ( MathExpression::ExtendedLiteral(lit) => (
BinaryPart::Literal(Box::new(Literal { BinaryPart::Literal(Box::new(Literal {
value: lit.value.clone(), value: lit.value.into(),
start: lit.start, start: lit.start,
end: lit.end, end: lit.end,
raw: lit.raw.clone(), raw: lit.raw.clone(),
@ -714,13 +714,13 @@ mod test {
start: 0, start: 0,
end: 5, end: 5,
left: BinaryPart::Literal(Box::new(Literal { left: BinaryPart::Literal(Box::new(Literal {
value: serde_json::Value::Number(serde_json::Number::from(1)), value: 1.into(),
raw: "1".to_string(), raw: "1".to_string(),
start: 0, start: 0,
end: 1, end: 1,
})), })),
right: BinaryPart::Literal(Box::new(Literal { right: BinaryPart::Literal(Box::new(Literal {
value: serde_json::Value::Number(serde_json::Number::from(2)), value: 2.into(),
raw: "2".to_string(), raw: "2".to_string(),
start: 4, start: 4,
end: 5, end: 5,
@ -741,13 +741,13 @@ mod test {
start: 0, start: 0,
end: 3, end: 3,
left: BinaryPart::Literal(Box::new(Literal { left: BinaryPart::Literal(Box::new(Literal {
value: serde_json::Value::Number(serde_json::Number::from(1)), value: 1.into(),
raw: "1".to_string(), raw: "1".to_string(),
start: 0, start: 0,
end: 1, end: 1,
})), })),
right: BinaryPart::Literal(Box::new(Literal { right: BinaryPart::Literal(Box::new(Literal {
value: serde_json::Value::Number(serde_json::Number::from(2)), value: 2.into(),
raw: "2".to_string(), raw: "2".to_string(),
start: 2, start: 2,
end: 3, end: 3,
@ -768,13 +768,13 @@ mod test {
start: 0, start: 0,
end: 4, end: 4,
left: BinaryPart::Literal(Box::new(Literal { left: BinaryPart::Literal(Box::new(Literal {
value: serde_json::Value::Number(serde_json::Number::from(1)), value: 1.into(),
raw: "1".to_string(), raw: "1".to_string(),
start: 0, start: 0,
end: 1, end: 1,
})), })),
right: BinaryPart::Literal(Box::new(Literal { right: BinaryPart::Literal(Box::new(Literal {
value: serde_json::Value::Number(serde_json::Number::from(2)), value: 2.into(),
raw: "2".to_string(), raw: "2".to_string(),
start: 3, start: 3,
end: 4, end: 4,
@ -795,7 +795,7 @@ mod test {
start: 0, start: 0,
end: 9, end: 9,
left: BinaryPart::Literal(Box::new(Literal { left: BinaryPart::Literal(Box::new(Literal {
value: serde_json::Value::Number(serde_json::Number::from(1)), value: 1.into(),
raw: "1".to_string(), raw: "1".to_string(),
start: 0, start: 0,
end: 1, end: 1,
@ -805,13 +805,13 @@ mod test {
start: 4, start: 4,
end: 9, end: 9,
left: BinaryPart::Literal(Box::new(Literal { left: BinaryPart::Literal(Box::new(Literal {
value: serde_json::Value::Number(serde_json::Number::from(2)), value: 2.into(),
raw: "2".to_string(), raw: "2".to_string(),
start: 4, start: 4,
end: 5, end: 5,
})), })),
right: BinaryPart::Literal(Box::new(Literal { right: BinaryPart::Literal(Box::new(Literal {
value: serde_json::Value::Number(serde_json::Number::from(3)), value: 3.into(),
raw: "3".to_string(), raw: "3".to_string(),
start: 8, start: 8,
end: 9, end: 9,
@ -833,7 +833,7 @@ mod test {
start: 0, start: 0,
end: 13, end: 13,
left: BinaryPart::Literal(Box::new(Literal { left: BinaryPart::Literal(Box::new(Literal {
value: serde_json::Value::Number(serde_json::Number::from(1)), value: 1.into(),
raw: "1".to_string(), raw: "1".to_string(),
start: 0, start: 0,
end: 1, end: 1,
@ -843,13 +843,13 @@ mod test {
start: 6, start: 6,
end: 11, end: 11,
left: BinaryPart::Literal(Box::new(Literal { left: BinaryPart::Literal(Box::new(Literal {
value: serde_json::Value::Number(serde_json::Number::from(2)), value: 2.into(),
raw: "2".to_string(), raw: "2".to_string(),
start: 6, start: 6,
end: 7, end: 7,
})), })),
right: BinaryPart::Literal(Box::new(Literal { right: BinaryPart::Literal(Box::new(Literal {
value: serde_json::Value::Number(serde_json::Number::from(3)), value: 3.into(),
raw: "3".to_string(), raw: "3".to_string(),
start: 10, start: 10,
end: 11, end: 11,
@ -875,7 +875,7 @@ mod test {
start: 0, start: 0,
end: 13, end: 13,
left: BinaryPart::Literal(Box::new(Literal { left: BinaryPart::Literal(Box::new(Literal {
value: serde_json::Value::Number(serde_json::Number::from(1)), value: 1.into(),
raw: "1".to_string(), raw: "1".to_string(),
start: 0, start: 0,
end: 1, end: 1,
@ -885,13 +885,13 @@ mod test {
start: 6, start: 6,
end: 11, end: 11,
left: BinaryPart::Literal(Box::new(Literal { left: BinaryPart::Literal(Box::new(Literal {
value: serde_json::Value::Number(serde_json::Number::from(2)), value: 2.into(),
raw: "2".to_string(), raw: "2".to_string(),
start: 6, start: 6,
end: 7, end: 7,
})), })),
right: BinaryPart::Literal(Box::new(Literal { right: BinaryPart::Literal(Box::new(Literal {
value: serde_json::Value::Number(serde_json::Number::from(3)), value: 3.into(),
raw: "3".to_string(), raw: "3".to_string(),
start: 10, start: 10,
end: 11, end: 11,
@ -899,7 +899,7 @@ mod test {
})), })),
})), })),
right: BinaryPart::Literal(Box::new(Literal { right: BinaryPart::Literal(Box::new(Literal {
value: serde_json::Value::Number(serde_json::Number::from(4)), value: 4.into(),
raw: "4".to_string(), raw: "4".to_string(),
start: 16, start: 16,
end: 17, end: 17,
@ -920,7 +920,7 @@ mod test {
start: 0, start: 0,
end: 17, end: 17,
left: BinaryPart::Literal(Box::new(Literal { left: BinaryPart::Literal(Box::new(Literal {
value: serde_json::Value::Number(serde_json::Number::from(1)), value: 1.into(),
raw: "1".to_string(), raw: "1".to_string(),
start: 0, start: 0,
end: 1, end: 1,
@ -934,20 +934,20 @@ mod test {
start: 6, start: 6,
end: 11, end: 11,
left: BinaryPart::Literal(Box::new(Literal { left: BinaryPart::Literal(Box::new(Literal {
value: serde_json::Value::Number(serde_json::Number::from(2)), value: 2.into(),
raw: "2".to_string(), raw: "2".to_string(),
start: 6, start: 6,
end: 7, end: 7,
})), })),
right: BinaryPart::Literal(Box::new(Literal { right: BinaryPart::Literal(Box::new(Literal {
value: serde_json::Value::Number(serde_json::Number::from(3)), value: 3.into(),
raw: "3".to_string(), raw: "3".to_string(),
start: 10, start: 10,
end: 11, end: 11,
})), })),
})), })),
right: BinaryPart::Literal(Box::new(Literal { right: BinaryPart::Literal(Box::new(Literal {
value: serde_json::Value::Number(serde_json::Number::from(4)), value: 4.into(),
raw: "4".to_string(), raw: "4".to_string(),
start: 16, start: 16,
end: 17, end: 17,
@ -968,7 +968,7 @@ mod test {
start: 0, start: 0,
end: 24, end: 24,
left: BinaryPart::Literal(Box::new(Literal { left: BinaryPart::Literal(Box::new(Literal {
value: serde_json::Value::Number(serde_json::Number::from(1)), value: 1.into(),
raw: "1".to_string(), raw: "1".to_string(),
start: 0, start: 0,
end: 1, end: 1,
@ -986,27 +986,27 @@ mod test {
start: 7, start: 7,
end: 12, end: 12,
left: BinaryPart::Literal(Box::new(Literal { left: BinaryPart::Literal(Box::new(Literal {
value: serde_json::Value::Number(serde_json::Number::from(2)), value: 2.into(),
raw: "2".to_string(), raw: "2".to_string(),
start: 7, start: 7,
end: 8, end: 8,
})), })),
right: BinaryPart::Literal(Box::new(Literal { right: BinaryPart::Literal(Box::new(Literal {
value: serde_json::Value::Number(serde_json::Number::from(3)), value: 3.into(),
raw: "3".to_string(), raw: "3".to_string(),
start: 11, start: 11,
end: 12, end: 12,
})), })),
})), })),
right: BinaryPart::Literal(Box::new(Literal { right: BinaryPart::Literal(Box::new(Literal {
value: serde_json::Value::Number(serde_json::Number::from(4)), value: 4.into(),
raw: "4".to_string(), raw: "4".to_string(),
start: 17, start: 17,
end: 18, end: 18,
})), })),
})), })),
right: BinaryPart::Literal(Box::new(Literal { right: BinaryPart::Literal(Box::new(Literal {
value: serde_json::Value::Number(serde_json::Number::from(5)), value: 5.into(),
raw: "5".to_string(), raw: "5".to_string(),
start: 21, start: 21,
end: 22, end: 22,
@ -1027,7 +1027,7 @@ mod test {
start: 0, start: 0,
end: 17, end: 17,
left: BinaryPart::Literal(Box::new(Literal { left: BinaryPart::Literal(Box::new(Literal {
value: serde_json::Value::Number(serde_json::Number::from(1)), value: 1.into(),
raw: "1".to_string(), raw: "1".to_string(),
start: 0, start: 0,
end: 1, end: 1,
@ -1037,13 +1037,13 @@ mod test {
start: 8, start: 8,
end: 13, end: 13,
left: BinaryPart::Literal(Box::new(Literal { left: BinaryPart::Literal(Box::new(Literal {
value: serde_json::Value::Number(serde_json::Number::from(2)), value: 2.into(),
raw: "2".to_string(), raw: "2".to_string(),
start: 8, start: 8,
end: 9, end: 9,
})), })),
right: BinaryPart::Literal(Box::new(Literal { right: BinaryPart::Literal(Box::new(Literal {
value: serde_json::Value::Number(serde_json::Number::from(3)), value: 3.into(),
raw: "3".to_string(), raw: "3".to_string(),
start: 12, start: 12,
end: 13, end: 13,
@ -1189,13 +1189,13 @@ mod test {
start: 0, start: 0,
end: code.find(")))").unwrap() + 3, end: code.find(")))").unwrap() + 3,
left: BinaryPart::Literal(Box::new(Literal { left: BinaryPart::Literal(Box::new(Literal {
value: serde_json::Value::Number(serde_json::Number::from(2)), value: 2.into(),
raw: "2".to_string(), raw: "2".to_string(),
start: 0, start: 0,
end: 1, end: 1,
})), })),
right: BinaryPart::Literal(Box::new(Literal { right: BinaryPart::Literal(Box::new(Literal {
value: serde_json::Value::Number(serde_json::Number::from(3)), value: 3.into(),
raw: "3".to_string(), raw: "3".to_string(),
start: 7, start: 7,
end: 8, end: 8,
@ -1243,7 +1243,7 @@ mod test {
start: 0, start: 0,
end: 9, end: 9,
left: BinaryPart::Literal(Box::new(Literal { left: BinaryPart::Literal(Box::new(Literal {
value: serde_json::Value::Number(serde_json::Number::from(1)), value: 1.into(),
raw: "1".to_string(), raw: "1".to_string(),
start: 0, start: 0,
end: 1, end: 1,
@ -1253,13 +1253,13 @@ mod test {
start: 4, start: 4,
end: 9, end: 9,
left: BinaryPart::Literal(Box::new(Literal { left: BinaryPart::Literal(Box::new(Literal {
value: serde_json::Value::Number(serde_json::Number::from(2)), value: 2.into(),
raw: "2".to_string(), raw: "2".to_string(),
start: 4, start: 4,
end: 5, end: 5,
})), })),
right: BinaryPart::Literal(Box::new(Literal { right: BinaryPart::Literal(Box::new(Literal {
value: serde_json::Value::Number(serde_json::Number::from(3)), value: 3.into(),
raw: "3".to_string(), raw: "3".to_string(),
start: 8, start: 8,
end: 9, end: 9,

View File

@ -246,7 +246,7 @@ impl Parser {
Ok(Literal { Ok(Literal {
start: token.start, start: token.start,
end: token.end, end: token.end,
value, value: value.into(),
raw: token.value.clone(), raw: token.value.clone(),
}) })
} }
@ -1833,7 +1833,7 @@ mod tests {
use pretty_assertions::assert_eq; use pretty_assertions::assert_eq;
use super::*; use super::*;
use crate::ast::types::BinaryOperator; use crate::ast::types::{BinaryOperator, LiteralValue};
#[test] #[test]
fn test_make_identifier() { fn test_make_identifier() {
@ -1986,7 +1986,7 @@ const key = 'c'"#,
Literal { Literal {
start: 4, start: 4,
end: 5, end: 5,
value: serde_json::Value::Number(5.into()), value: LiteralValue::Number(5.into()),
raw: "5".to_string() raw: "5".to_string()
}, },
literal literal
@ -1996,7 +1996,7 @@ const key = 'c'"#,
Literal { Literal {
start: 7, start: 7,
end: 14, end: 14,
value: serde_json::Value::String("hello".to_string()), value: "hello".into(),
raw: "\"hello\"".to_string() raw: "\"hello\"".to_string()
}, },
literal literal
@ -2014,13 +2014,13 @@ const key = 'c'"#,
left: BinaryPart::Literal(Box::new(Literal { left: BinaryPart::Literal(Box::new(Literal {
start: 0, start: 0,
end: 1, end: 1,
value: serde_json::Value::Number(serde_json::Number::from(5)), value: 5.into(),
raw: "5".to_owned(), raw: "5".to_owned(),
})), })),
right: BinaryPart::Literal(Box::new(Literal { right: BinaryPart::Literal(Box::new(Literal {
start: 4, start: 4,
end: 7, end: 7,
value: serde_json::Value::String("a".to_owned()), value: "a".into(),
raw: r#""a""#.to_owned(), raw: r#""a""#.to_owned(),
})), })),
}; };
@ -2690,14 +2690,14 @@ show(mySk1)"#;
left: BinaryPart::Literal(Box::new(Literal { left: BinaryPart::Literal(Box::new(Literal {
start: 0, start: 0,
end: 1, end: 1,
value: serde_json::Value::Number(serde_json::Number::from(5)), value: 5.into(),
raw: "5".to_string(), raw: "5".to_string(),
})), })),
operator: BinaryOperator::Add, operator: BinaryOperator::Add,
right: BinaryPart::Literal(Box::new(Literal { right: BinaryPart::Literal(Box::new(Literal {
start: 3, start: 3,
end: 4, end: 4,
value: serde_json::Value::Number(serde_json::Number::from(6)), value: 6.into(),
raw: "6".to_string(), raw: "6".to_string(),
})), })),
})), })),

View File

@ -10,10 +10,10 @@ use winnow::{
use crate::{ use crate::{
ast::types::{ ast::types::{
ArrayExpression, BinaryExpression, BinaryOperator, BinaryPart, BodyItem, CallExpression, CommentStyle, ArrayExpression, BinaryExpression, BinaryOperator, BinaryPart, BodyItem, CallExpression, CommentStyle,
ExpressionStatement, FunctionExpression, Identifier, Literal, LiteralIdentifier, MemberExpression, ExpressionStatement, FunctionExpression, Identifier, Literal, LiteralIdentifier, LiteralValue,
MemberObject, NonCodeMeta, NonCodeNode, NonCodeValue, ObjectExpression, ObjectProperty, PipeExpression, MemberExpression, MemberObject, NonCodeMeta, NonCodeNode, NonCodeValue, ObjectExpression, ObjectProperty,
PipeSubstitution, Program, ReturnStatement, UnaryExpression, UnaryOperator, Value, VariableDeclaration, PipeExpression, PipeSubstitution, Program, ReturnStatement, UnaryExpression, UnaryOperator, Value,
VariableDeclarator, VariableKind, VariableDeclaration, VariableDeclarator, VariableKind,
}, },
errors::{KclError, KclErrorDetails}, errors::{KclError, KclErrorDetails},
executor::SourceRange, executor::SourceRange,
@ -25,7 +25,7 @@ use crate::{
mod error; mod error;
type PResult<O, E = error::ContextError> = winnow::prelude::PResult<O, E>; type PResult<O, E = ContextError> = winnow::prelude::PResult<O, E>;
lazy_static::lazy_static! { lazy_static::lazy_static! {
static ref STDLIB: StdLib = StdLib::new(); static ref STDLIB: StdLib = StdLib::new();
@ -216,7 +216,7 @@ pub fn string_literal(i: TokenSlice) -> PResult<Literal> {
.try_map(|token: Token| match token.token_type { .try_map(|token: Token| match token.token_type {
TokenType::String => { TokenType::String => {
let s = token.value[1..token.value.len() - 1].to_string(); let s = token.value[1..token.value.len() - 1].to_string();
Ok((JValue::String(s), token)) Ok((LiteralValue::String(s), token))
} }
_ => Err(KclError::Syntax(KclErrorDetails { _ => Err(KclError::Syntax(KclErrorDetails {
source_ranges: token.as_source_ranges(), source_ranges: token.as_source_ranges(),
@ -239,7 +239,7 @@ fn unsigned_number_literal(i: TokenSlice) -> PResult<Literal> {
.try_map(|token: Token| match token.token_type { .try_map(|token: Token| match token.token_type {
TokenType::Number => { TokenType::Number => {
if let Ok(x) = token.value.parse::<i64>() { if let Ok(x) = token.value.parse::<i64>() {
return Ok((JValue::Number(JNumber::from(x)), token)); return Ok((JNumber::from(x), token));
} }
let x: f64 = token.value.parse().map_err(|_| { let x: f64 = token.value.parse().map_err(|_| {
KclError::Syntax(KclErrorDetails { KclError::Syntax(KclErrorDetails {
@ -249,7 +249,7 @@ fn unsigned_number_literal(i: TokenSlice) -> PResult<Literal> {
})?; })?;
match JNumber::from_f64(x) { match JNumber::from_f64(x) {
Some(n) => Ok((JValue::Number(n), token)), Some(n) => Ok((n, token)),
None => Err(KclError::Syntax(KclErrorDetails { None => Err(KclError::Syntax(KclErrorDetails {
source_ranges: token.as_source_ranges(), source_ranges: token.as_source_ranges(),
message: format!("Invalid float: {}", token.value), message: format!("Invalid float: {}", token.value),
@ -266,7 +266,7 @@ fn unsigned_number_literal(i: TokenSlice) -> PResult<Literal> {
Ok(Literal { Ok(Literal {
start: token.start, start: token.start,
end: token.end, end: token.end,
value, value: LiteralValue::Number(value),
raw: token.value.clone(), raw: token.value.clone(),
}) })
} }
@ -407,7 +407,7 @@ fn integer_range(i: TokenSlice) -> PResult<Vec<Value>> {
Value::Literal(Box::new(Literal { Value::Literal(Box::new(Literal {
start: token0.start, start: token0.start,
end: token0.end, end: token0.end,
value: JValue::Number(num.into()), value: num.into(),
raw: num.to_string(), raw: num.to_string(),
})) }))
}) })
@ -1408,7 +1408,7 @@ const mySk1 = startSketchAt([0, 0])"#;
let tokens = crate::token::lexer(r#"const x = y() |> /*hi*/ z(%)"#); let tokens = crate::token::lexer(r#"const x = y() |> /*hi*/ z(%)"#);
let mut body = program.parse(&tokens).unwrap().body; let mut body = program.parse(&tokens).unwrap().body;
let BodyItem::VariableDeclaration(mut item) = body.remove(0) else { let BodyItem::VariableDeclaration(mut item) = body.remove(0) else {
panic!("expected vardec"); panic!("expected variable declaration");
}; };
let val = item.declarations.remove(0).init; let val = item.declarations.remove(0).init;
let Value::PipeExpression(pipe) = val else { let Value::PipeExpression(pipe) = val else {
@ -1461,7 +1461,7 @@ const mySk1 = startSketchAt([0, 0])"#;
argument: Value::Literal(Box::new(Literal { argument: Value::Literal(Box::new(Literal {
start: 32, start: 32,
end: 33, end: 33,
value: JValue::Number(JNumber::from(2)), value: 2.into(),
raw: "2".to_owned(), raw: "2".to_owned(),
})), })),
})], })],
@ -1616,7 +1616,7 @@ const mySk1 = startSketchAt([0, 0])"#;
BinaryPart::Literal(Box::new(Literal { BinaryPart::Literal(Box::new(Literal {
start: 9, start: 9,
end: 10, end: 10,
value: JValue::Number(JNumber::from(3)), value: 3.into(),
raw: "3".to_owned(), raw: "3".to_owned(),
})) }))
); );

View File

@ -4,14 +4,13 @@ use anyhow::Result;
use derive_docs::stdlib; use derive_docs::stdlib;
use schemars::JsonSchema; use schemars::JsonSchema;
use super::utils::between;
use crate::{ use crate::{
errors::{KclError, KclErrorDetails}, errors::{KclError, KclErrorDetails},
executor::{MemoryItem, SketchGroup}, executor::{MemoryItem, SketchGroup},
std::Args, std::Args,
}; };
use super::utils::between;
/// Returns the segment end of x. /// Returns the segment end of x.
pub async fn segment_end_x(args: Args) -> Result<MemoryItem, KclError> { pub async fn segment_end_x(args: Args) -> Result<MemoryItem, KclError> {
let (segment_name, sketch_group) = args.get_segment_name_sketch_group()?; let (segment_name, sketch_group) = args.get_segment_name_sketch_group()?;