Modify return/break syntax; Change Value::String
This commit is contained in:
parent
25e3941315
commit
cc76ca89cc
@ -90,8 +90,9 @@ impl AbstractTree for As {
|
|||||||
Value::List(list) => Value::List(list),
|
Value::List(list) => Value::List(list),
|
||||||
Value::String(string) => {
|
Value::String(string) => {
|
||||||
let chars = string
|
let chars = string
|
||||||
|
.read()?
|
||||||
.chars()
|
.chars()
|
||||||
.map(|char| Value::String(char.to_string()))
|
.map(|char| Value::string(char))
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
Value::List(List::with_items(chars))
|
Value::List(List::with_items(chars))
|
||||||
|
@ -65,7 +65,7 @@ impl AbstractTree for Command {
|
|||||||
.wait_with_output()?
|
.wait_with_output()?
|
||||||
.stdout;
|
.stdout;
|
||||||
|
|
||||||
Ok(Value::String(String::from_utf8(output)?))
|
Ok(Value::string(String::from_utf8(output)?))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -99,7 +99,7 @@ impl AbstractTree for Index {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let index_value = self.index.run(source, context)?;
|
let index_value = self.index.run(source, context)?;
|
||||||
let identifier = Identifier::new(index_value.as_string()?);
|
let identifier = Identifier::new(index_value.as_string()?.as_str());
|
||||||
|
|
||||||
if let Some(value) = map.get(&identifier) {
|
if let Some(value) = map.get(&identifier) {
|
||||||
value
|
value
|
||||||
@ -114,7 +114,7 @@ impl AbstractTree for Index {
|
|||||||
}
|
}
|
||||||
Value::String(string) => {
|
Value::String(string) => {
|
||||||
let index = self.index.run(source, context)?.as_integer()? as usize;
|
let index = self.index.run(source, context)?.as_integer()? as usize;
|
||||||
let item = string.chars().nth(index).unwrap_or_default();
|
let item = string.read()?.chars().nth(index).unwrap_or_default();
|
||||||
|
|
||||||
Ok(Value::string(item.to_string()))
|
Ok(Value::string(item.to_string()))
|
||||||
}
|
}
|
||||||
|
@ -50,7 +50,7 @@ impl AbstractTree for IndexAssignment {
|
|||||||
identifier
|
identifier
|
||||||
} else {
|
} else {
|
||||||
let index_run = self.index.index.run(source, context)?;
|
let index_run = self.index.index.run(source, context)?;
|
||||||
let expected_identifier = Identifier::new(index_run.as_string()?);
|
let expected_identifier = Identifier::new(index_run.as_string()?.as_str());
|
||||||
|
|
||||||
return Err(RuntimeError::ValidationFailure(
|
return Err(RuntimeError::ValidationFailure(
|
||||||
ValidationError::VariableIdentifierNotFound(expected_identifier),
|
ValidationError::VariableIdentifierNotFound(expected_identifier),
|
||||||
|
@ -46,7 +46,7 @@ impl Callable for Fs {
|
|||||||
RuntimeError::expect_argument_amount(self.name(), 1, arguments.len())?;
|
RuntimeError::expect_argument_amount(self.name(), 1, arguments.len())?;
|
||||||
|
|
||||||
let path = arguments.first().unwrap().as_string()?;
|
let path = arguments.first().unwrap().as_string()?;
|
||||||
let mut file = File::open(path)?;
|
let mut file = File::open(path.as_str())?;
|
||||||
let file_size = file.metadata()?.len() as usize;
|
let file_size = file.metadata()?.len() as usize;
|
||||||
let mut file_content = String::with_capacity(file_size);
|
let mut file_content = String::with_capacity(file_size);
|
||||||
|
|
||||||
|
@ -54,7 +54,7 @@ impl Callable for Json {
|
|||||||
let value = arguments.first().unwrap();
|
let value = arguments.first().unwrap();
|
||||||
let json_string = serde_json::to_string(value)?;
|
let json_string = serde_json::to_string(value)?;
|
||||||
|
|
||||||
Ok(Value::String(json_string))
|
Ok(Value::string(json_string))
|
||||||
}
|
}
|
||||||
Json::CreatePretty => {
|
Json::CreatePretty => {
|
||||||
RuntimeError::expect_argument_amount(self.name(), 1, arguments.len())?;
|
RuntimeError::expect_argument_amount(self.name(), 1, arguments.len())?;
|
||||||
@ -62,13 +62,13 @@ impl Callable for Json {
|
|||||||
let value = arguments.first().unwrap();
|
let value = arguments.first().unwrap();
|
||||||
let json_string = serde_json::to_string_pretty(value)?;
|
let json_string = serde_json::to_string_pretty(value)?;
|
||||||
|
|
||||||
Ok(Value::String(json_string))
|
Ok(Value::string(json_string))
|
||||||
}
|
}
|
||||||
Json::Parse => {
|
Json::Parse => {
|
||||||
RuntimeError::expect_argument_amount(self.name(), 1, arguments.len())?;
|
RuntimeError::expect_argument_amount(self.name(), 1, arguments.len())?;
|
||||||
|
|
||||||
let json_string = arguments.first().unwrap().as_string()?;
|
let json_string = arguments.first().unwrap().as_string()?;
|
||||||
let value = serde_json::from_str(json_string)?;
|
let value = serde_json::from_str(json_string.as_str())?;
|
||||||
|
|
||||||
Ok(value)
|
Ok(value)
|
||||||
}
|
}
|
||||||
|
@ -221,19 +221,17 @@ impl Callable for StrFunction {
|
|||||||
RuntimeError::expect_argument_amount(self.name(), 2, arguments.len())?;
|
RuntimeError::expect_argument_amount(self.name(), 2, arguments.len())?;
|
||||||
|
|
||||||
let string = arguments.first().unwrap().as_string()?;
|
let string = arguments.first().unwrap().as_string()?;
|
||||||
let pattern_string = arguments.get(1).unwrap().as_string()?;
|
let pattern = arguments.get(1).unwrap().as_string()?;
|
||||||
let pattern = pattern_string.as_str();
|
|
||||||
|
|
||||||
Value::Boolean(string.ends_with(pattern))
|
Value::Boolean(string.ends_with(pattern.as_str()))
|
||||||
}
|
}
|
||||||
StrFunction::Find => {
|
StrFunction::Find => {
|
||||||
RuntimeError::expect_argument_amount(self.name(), 2, arguments.len())?;
|
RuntimeError::expect_argument_amount(self.name(), 2, arguments.len())?;
|
||||||
|
|
||||||
let string = arguments.first().unwrap().as_string()?;
|
let string = arguments.first().unwrap().as_string()?;
|
||||||
let pattern_string = arguments.get(1).unwrap().as_string()?;
|
let pattern = arguments.get(1).unwrap().as_string()?;
|
||||||
let pattern = pattern_string.as_str();
|
|
||||||
let find = string
|
let find = string
|
||||||
.find(pattern)
|
.find(pattern.as_str())
|
||||||
.map(|index| Value::Integer(index as i64));
|
.map(|index| Value::Integer(index as i64));
|
||||||
|
|
||||||
if let Some(index) = find {
|
if let Some(index) = find {
|
||||||
@ -271,9 +269,9 @@ impl Callable for StrFunction {
|
|||||||
let index = arguments.get(1).unwrap().as_integer()? as usize;
|
let index = arguments.get(1).unwrap().as_integer()? as usize;
|
||||||
let insertion = arguments.get(2).unwrap().as_string()?;
|
let insertion = arguments.get(2).unwrap().as_string()?;
|
||||||
|
|
||||||
string.insert_str(index, insertion);
|
string.insert_str(index, insertion.as_str());
|
||||||
|
|
||||||
Value::String(string)
|
Value::none()
|
||||||
}
|
}
|
||||||
StrFunction::Lines => {
|
StrFunction::Lines => {
|
||||||
RuntimeError::expect_argument_amount(self.name(), 1, arguments.len())?;
|
RuntimeError::expect_argument_amount(self.name(), 1, arguments.len())?;
|
||||||
@ -339,9 +337,9 @@ impl Callable for StrFunction {
|
|||||||
let end = range[1].as_integer()? as usize;
|
let end = range[1].as_integer()? as usize;
|
||||||
let pattern = arguments.get(2).unwrap().as_string()?;
|
let pattern = arguments.get(2).unwrap().as_string()?;
|
||||||
|
|
||||||
string.replace_range(start..end, pattern);
|
string.replace_range(start..end, pattern.as_str());
|
||||||
|
|
||||||
Value::String(string)
|
Value::string(string)
|
||||||
}
|
}
|
||||||
StrFunction::Retain => {
|
StrFunction::Retain => {
|
||||||
RuntimeError::expect_argument_amount(self.name(), 2, arguments.len())?;
|
RuntimeError::expect_argument_amount(self.name(), 2, arguments.len())?;
|
||||||
@ -576,13 +574,9 @@ impl Callable for StrFunction {
|
|||||||
let input_string = arguments.first().unwrap().as_string()?;
|
let input_string = arguments.first().unwrap().as_string()?;
|
||||||
let new_length = arguments.get(1).unwrap().as_integer()? as usize;
|
let new_length = arguments.get(1).unwrap().as_integer()? as usize;
|
||||||
|
|
||||||
let new_string = input_string
|
let new_string = input_string.chars().take(new_length).collect::<String>();
|
||||||
.chars()
|
|
||||||
.take(new_length)
|
|
||||||
.map(|char| char.to_string())
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
Value::String(new_string)
|
Value::string(new_string)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -1,4 +1,7 @@
|
|||||||
use std::fmt::{self, Display, Formatter};
|
use std::{
|
||||||
|
fmt::{self, Display, Formatter},
|
||||||
|
sync::PoisonError,
|
||||||
|
};
|
||||||
|
|
||||||
use colored::Colorize;
|
use colored::Colorize;
|
||||||
use lyneate::Report;
|
use lyneate::Report;
|
||||||
@ -261,6 +264,12 @@ impl ValidationError {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T> From<PoisonError<T>> for ValidationError {
|
||||||
|
fn from(_: PoisonError<T>) -> Self {
|
||||||
|
ValidationError::RwLock(RwLockError)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl From<RwLockError> for ValidationError {
|
impl From<RwLockError> for ValidationError {
|
||||||
fn from(_error: RwLockError) -> Self {
|
fn from(_error: RwLockError) -> Self {
|
||||||
ValidationError::RwLock(RwLockError)
|
ValidationError::RwLock(RwLockError)
|
||||||
|
@ -18,6 +18,7 @@ use std::{
|
|||||||
fmt::{self, Display, Formatter},
|
fmt::{self, Display, Formatter},
|
||||||
marker::PhantomData,
|
marker::PhantomData,
|
||||||
ops::RangeInclusive,
|
ops::RangeInclusive,
|
||||||
|
sync::{Arc, RwLock, RwLockReadGuard},
|
||||||
};
|
};
|
||||||
|
|
||||||
pub use self::{
|
pub use self::{
|
||||||
@ -36,7 +37,7 @@ pub mod struct_instance;
|
|||||||
/// Every dust variable has a key and a Value. Variables are represented by
|
/// Every dust variable has a key and a Value. Variables are represented by
|
||||||
/// storing them in a VariableMap. This means the map of variables is itself a
|
/// storing them in a VariableMap. This means the map of variables is itself a
|
||||||
/// value that can be treated as any other.
|
/// value that can be treated as any other.
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug)]
|
||||||
pub enum Value {
|
pub enum Value {
|
||||||
Boolean(bool),
|
Boolean(bool),
|
||||||
Enum(EnumInstance),
|
Enum(EnumInstance),
|
||||||
@ -46,7 +47,7 @@ pub enum Value {
|
|||||||
List(List),
|
List(List),
|
||||||
Map(Map),
|
Map(Map),
|
||||||
Range(RangeInclusive<i64>),
|
Range(RangeInclusive<i64>),
|
||||||
String(String),
|
String(Arc<RwLock<String>>),
|
||||||
Struct(StructInstance),
|
Struct(StructInstance),
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -64,7 +65,7 @@ impl Value {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn string<T: Into<String>>(string: T) -> Self {
|
pub fn string<T: Into<String>>(string: T) -> Self {
|
||||||
Value::String(string.into())
|
Value::String(Arc::new(RwLock::new(string.into())))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn range(start: i64, end: i64) -> Self {
|
pub fn range(start: i64, end: i64) -> Self {
|
||||||
@ -157,11 +158,11 @@ impl Value {
|
|||||||
self == &Value::none()
|
self == &Value::none()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Borrows the value stored in `self` as `&String`, or returns `Err` if
|
/// Borrows the value stored in `self` as `&str`, or returns `Err` if
|
||||||
/// `self` is not a `Value::String`.
|
/// `self` is not a `Value::String`.
|
||||||
pub fn as_string(&self) -> Result<&String, ValidationError> {
|
pub fn as_string(&self) -> Result<RwLockReadGuard<String>, ValidationError> {
|
||||||
match self {
|
match self {
|
||||||
Value::String(string) => Ok(string),
|
Value::String(string) => Ok(string.read()?),
|
||||||
value => Err(ValidationError::ExpectedString {
|
value => Err(ValidationError::ExpectedString {
|
||||||
actual: value.clone(),
|
actual: value.clone(),
|
||||||
}),
|
}),
|
||||||
@ -274,7 +275,12 @@ impl Value {
|
|||||||
|
|
||||||
Ok(Value::List(list))
|
Ok(Value::List(list))
|
||||||
}
|
}
|
||||||
(Value::String(left), Value::String(right)) => Ok(Value::String(left + &right)),
|
(Value::String(left), Value::String(right)) => {
|
||||||
|
let left = left.read()?.to_string();
|
||||||
|
let right = right.read()?;
|
||||||
|
|
||||||
|
Ok(Value::string(left + right.as_str()))
|
||||||
|
}
|
||||||
(left, right) => Err(ValidationError::CannotAdd {
|
(left, right) => Err(ValidationError::CannotAdd {
|
||||||
left,
|
left,
|
||||||
right,
|
right,
|
||||||
@ -360,7 +366,13 @@ impl PartialEq for Value {
|
|||||||
(Value::Integer(left), Value::Integer(right)) => left == right,
|
(Value::Integer(left), Value::Integer(right)) => left == right,
|
||||||
(Value::Float(left), Value::Float(right)) => left == right,
|
(Value::Float(left), Value::Float(right)) => left == right,
|
||||||
(Value::Boolean(left), Value::Boolean(right)) => left == right,
|
(Value::Boolean(left), Value::Boolean(right)) => left == right,
|
||||||
(Value::String(left), Value::String(right)) => left == right,
|
(Value::String(left), Value::String(right)) => {
|
||||||
|
if let (Ok(left), Ok(right)) = (left.read(), right.read()) {
|
||||||
|
left.as_str() == right.as_str()
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
(Value::List(left), Value::List(right)) => left == right,
|
(Value::List(left), Value::List(right)) => left == right,
|
||||||
(Value::Map(left), Value::Map(right)) => left == right,
|
(Value::Map(left), Value::Map(right)) => left == right,
|
||||||
(Value::Function(left), Value::Function(right)) => left == right,
|
(Value::Function(left), Value::Function(right)) => left == right,
|
||||||
@ -381,7 +393,13 @@ impl PartialOrd for Value {
|
|||||||
impl Ord for Value {
|
impl Ord for Value {
|
||||||
fn cmp(&self, other: &Self) -> Ordering {
|
fn cmp(&self, other: &Self) -> Ordering {
|
||||||
match (self, other) {
|
match (self, other) {
|
||||||
(Value::String(left), Value::String(right)) => left.cmp(right),
|
(Value::String(left), Value::String(right)) => {
|
||||||
|
if let (Ok(left), Ok(right)) = (left.read(), right.read()) {
|
||||||
|
left.cmp(&right)
|
||||||
|
} else {
|
||||||
|
Ordering::Equal
|
||||||
|
}
|
||||||
|
}
|
||||||
(Value::String(_), _) => Ordering::Greater,
|
(Value::String(_), _) => Ordering::Greater,
|
||||||
(Value::Float(left), Value::Float(right)) => left.total_cmp(right),
|
(Value::Float(left), Value::Float(right)) => left.total_cmp(right),
|
||||||
(Value::Integer(left), Value::Integer(right)) => left.cmp(right),
|
(Value::Integer(left), Value::Integer(right)) => left.cmp(right),
|
||||||
@ -424,7 +442,13 @@ impl Serialize for Value {
|
|||||||
S: Serializer,
|
S: Serializer,
|
||||||
{
|
{
|
||||||
match self {
|
match self {
|
||||||
Value::String(inner) => serializer.serialize_str(inner),
|
Value::String(inner) => {
|
||||||
|
let inner = inner
|
||||||
|
.read()
|
||||||
|
.map_err(|_| serde::ser::Error::custom("failed to get read lock on string"))?;
|
||||||
|
|
||||||
|
serializer.serialize_str(inner.as_str())
|
||||||
|
}
|
||||||
Value::Float(inner) => serializer.serialize_f64(*inner),
|
Value::Float(inner) => serializer.serialize_f64(*inner),
|
||||||
Value::Integer(inner) => serializer.serialize_i64(*inner),
|
Value::Integer(inner) => serializer.serialize_i64(*inner),
|
||||||
Value::Boolean(inner) => serializer.serialize_bool(*inner),
|
Value::Boolean(inner) => serializer.serialize_bool(*inner),
|
||||||
@ -461,10 +485,33 @@ impl Serialize for Value {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Clone for Value {
|
||||||
|
fn clone(&self) -> Self {
|
||||||
|
log::trace!("Cloning value {self}");
|
||||||
|
|
||||||
|
match self {
|
||||||
|
Value::Boolean(boolean) => Value::Boolean(*boolean),
|
||||||
|
Value::Enum(r#enum) => Value::Enum(r#enum.clone()),
|
||||||
|
Value::Float(float) => Value::Float(*float),
|
||||||
|
Value::Function(function) => Value::Function(function.clone()),
|
||||||
|
Value::Integer(integer) => Value::Integer(*integer),
|
||||||
|
Value::List(list) => Value::List(list.clone()),
|
||||||
|
Value::Map(map) => Value::Map(map.clone()),
|
||||||
|
Value::Range(range) => Value::Range(range.clone()),
|
||||||
|
Value::String(string) => Value::String(string.clone()),
|
||||||
|
Value::Struct(r#struct) => Value::Struct(r#struct.clone()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Display for Value {
|
impl Display for Value {
|
||||||
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
Value::String(string) => write!(f, "{string}"),
|
Value::String(string) => {
|
||||||
|
let string = string.read().map_err(|_| fmt::Error)?;
|
||||||
|
|
||||||
|
write!(f, "{}", string.as_str())
|
||||||
|
}
|
||||||
Value::Float(float) => write!(f, "{float}"),
|
Value::Float(float) => write!(f, "{float}"),
|
||||||
Value::Integer(int) => write!(f, "{int}"),
|
Value::Integer(int) => write!(f, "{int}"),
|
||||||
Value::Boolean(boolean) => write!(f, "{boolean}"),
|
Value::Boolean(boolean) => write!(f, "{boolean}"),
|
||||||
@ -531,7 +578,7 @@ impl TryFrom<Value> for String {
|
|||||||
|
|
||||||
fn try_from(value: Value) -> std::result::Result<Self, Self::Error> {
|
fn try_from(value: Value) -> std::result::Result<Self, Self::Error> {
|
||||||
if let Value::String(string) = value {
|
if let Value::String(string) = value {
|
||||||
Ok(string)
|
Ok(string.read()?.clone())
|
||||||
} else {
|
} else {
|
||||||
Err(RuntimeError::ValidationFailure(
|
Err(RuntimeError::ValidationFailure(
|
||||||
ValidationError::ExpectedString { actual: value },
|
ValidationError::ExpectedString { actual: value },
|
||||||
|
12
tests/as.rs
12
tests/as.rs
@ -8,12 +8,12 @@ fn string_as_string_list() {
|
|||||||
assert_eq!(
|
assert_eq!(
|
||||||
interpret("'foobar' as [str]"),
|
interpret("'foobar' as [str]"),
|
||||||
Ok(Value::List(List::with_items(vec![
|
Ok(Value::List(List::with_items(vec![
|
||||||
Value::String("f".to_string()),
|
Value::string("f"),
|
||||||
Value::String("o".to_string()),
|
Value::string("o"),
|
||||||
Value::String("o".to_string()),
|
Value::string("o"),
|
||||||
Value::String("b".to_string()),
|
Value::string("b"),
|
||||||
Value::String("a".to_string()),
|
Value::string("a"),
|
||||||
Value::String("r".to_string()),
|
Value::string("r"),
|
||||||
])))
|
])))
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -40,10 +40,10 @@ fn find() {
|
|||||||
fn insert() {
|
fn insert() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
interpret("str:insert('ac', 1, 'b')"),
|
interpret("str:insert('ac', 1, 'b')"),
|
||||||
Ok(Value::String("abc".to_string()))
|
Ok(Value::string("abc"))
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
interpret("str:insert('foo', 3, 'bar')"),
|
interpret("str:insert('foo', 3, 'bar')"),
|
||||||
Ok(Value::String("foobar".to_string()))
|
Ok(Value::string("foobar"))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,7 @@ fn override_built_ins() {
|
|||||||
Ok(Value::Enum(EnumInstance::new(
|
Ok(Value::Enum(EnumInstance::new(
|
||||||
Identifier::new("Option"),
|
Identifier::new("Option"),
|
||||||
Identifier::new("Some"),
|
Identifier::new("Some"),
|
||||||
Some(Value::String("foo".to_string())),
|
Some(Value::string("foo")),
|
||||||
)))
|
)))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -65,7 +65,7 @@ fn result() {
|
|||||||
Ok(Value::Enum(EnumInstance::new(
|
Ok(Value::Enum(EnumInstance::new(
|
||||||
Identifier::new("Result"),
|
Identifier::new("Result"),
|
||||||
Identifier::new("Error"),
|
Identifier::new("Error"),
|
||||||
Some(Value::String("uh-oh!".to_string())),
|
Some(Value::string("uh-oh!")),
|
||||||
)))
|
)))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,7 @@ use dust_lang::{interpret, Value};
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn simple_command() {
|
fn simple_command() {
|
||||||
assert_eq!(interpret("^echo hi"), Ok(Value::String("hi\n".to_string())))
|
assert_eq!(interpret("^echo hi"), Ok(Value::string("hi\n")))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -18,7 +18,7 @@ fn simple_struct() {
|
|||||||
let mut map = Map::new();
|
let mut map = Map::new();
|
||||||
|
|
||||||
map.set(Identifier::new("bar"), Value::Integer(0));
|
map.set(Identifier::new("bar"), Value::Integer(0));
|
||||||
map.set(Identifier::new("baz"), Value::String("hiya".to_string()));
|
map.set(Identifier::new("baz"), Value::string("hiya"));
|
||||||
|
|
||||||
let expected = Ok(Value::Struct(StructInstance::new(
|
let expected = Ok(Value::Struct(StructInstance::new(
|
||||||
Identifier::new("Foo"),
|
Identifier::new("Foo"),
|
||||||
|
@ -43,6 +43,9 @@ Block with Return
|
|||||||
(expression
|
(expression
|
||||||
(value
|
(value
|
||||||
(integer)))))
|
(integer)))))
|
||||||
|
(statement
|
||||||
|
(statement_kind
|
||||||
|
(return)))
|
||||||
(statement
|
(statement
|
||||||
(statement_kind
|
(statement_kind
|
||||||
(expression
|
(expression
|
||||||
|
@ -14,19 +14,33 @@ module.exports = grammar({
|
|||||||
statement: $ =>
|
statement: $ =>
|
||||||
prec.left(
|
prec.left(
|
||||||
seq(
|
seq(
|
||||||
optional(
|
|
||||||
choice('return', 'break'),
|
|
||||||
),
|
|
||||||
$.statement_kind,
|
$.statement_kind,
|
||||||
optional(';'),
|
optional(';'),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
|
||||||
|
break: $ =>
|
||||||
|
prec.left(
|
||||||
|
seq(
|
||||||
|
'break',
|
||||||
|
optional($.statement),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
|
||||||
|
return: $ =>
|
||||||
|
prec.left(
|
||||||
|
seq(
|
||||||
|
'return',
|
||||||
|
optional($.statement),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
|
||||||
statement_kind: $ =>
|
statement_kind: $ =>
|
||||||
prec.left(
|
prec.left(
|
||||||
choice(
|
choice(
|
||||||
$.assignment,
|
$.assignment,
|
||||||
$.block,
|
$.block,
|
||||||
|
$.break,
|
||||||
$.expression,
|
$.expression,
|
||||||
$.for,
|
$.for,
|
||||||
$.if_else,
|
$.if_else,
|
||||||
@ -34,6 +48,7 @@ module.exports = grammar({
|
|||||||
$.loop_node,
|
$.loop_node,
|
||||||
$.match,
|
$.match,
|
||||||
$.pipe,
|
$.pipe,
|
||||||
|
$.return,
|
||||||
$.while,
|
$.while,
|
||||||
$.type_definition,
|
$.type_definition,
|
||||||
),
|
),
|
||||||
@ -329,10 +344,7 @@ module.exports = grammar({
|
|||||||
),
|
),
|
||||||
|
|
||||||
loop_node: $ =>
|
loop_node: $ =>
|
||||||
seq(
|
seq('loop', $.block),
|
||||||
'loop',
|
|
||||||
$.block,
|
|
||||||
),
|
|
||||||
|
|
||||||
while: $ =>
|
while: $ =>
|
||||||
seq(
|
seq(
|
||||||
@ -372,7 +384,7 @@ module.exports = grammar({
|
|||||||
// Custom type with arguments
|
// Custom type with arguments
|
||||||
seq(
|
seq(
|
||||||
$.identifier,
|
$.identifier,
|
||||||
$.type_arguments
|
$.type_arguments,
|
||||||
),
|
),
|
||||||
|
|
||||||
// Map with exact fields
|
// Map with exact fields
|
||||||
@ -473,10 +485,7 @@ module.exports = grammar({
|
|||||||
seq(
|
seq(
|
||||||
'<',
|
'<',
|
||||||
repeat1(
|
repeat1(
|
||||||
seq(
|
seq($.type, optional(',')),
|
||||||
$.type,
|
|
||||||
optional(','),
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
'>',
|
'>',
|
||||||
),
|
),
|
||||||
@ -493,7 +502,9 @@ module.exports = grammar({
|
|||||||
repeat1(
|
repeat1(
|
||||||
seq(
|
seq(
|
||||||
$.identifier,
|
$.identifier,
|
||||||
optional($.type_arguments),
|
optional(
|
||||||
|
$.type_arguments,
|
||||||
|
),
|
||||||
optional(','),
|
optional(','),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
@ -23,27 +23,6 @@
|
|||||||
"content": {
|
"content": {
|
||||||
"type": "SEQ",
|
"type": "SEQ",
|
||||||
"members": [
|
"members": [
|
||||||
{
|
|
||||||
"type": "CHOICE",
|
|
||||||
"members": [
|
|
||||||
{
|
|
||||||
"type": "CHOICE",
|
|
||||||
"members": [
|
|
||||||
{
|
|
||||||
"type": "STRING",
|
|
||||||
"value": "return"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"type": "STRING",
|
|
||||||
"value": "break"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"type": "BLANK"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"type": "SYMBOL",
|
"type": "SYMBOL",
|
||||||
"name": "statement_kind"
|
"name": "statement_kind"
|
||||||
@ -63,6 +42,56 @@
|
|||||||
]
|
]
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"break": {
|
||||||
|
"type": "PREC_LEFT",
|
||||||
|
"value": 0,
|
||||||
|
"content": {
|
||||||
|
"type": "SEQ",
|
||||||
|
"members": [
|
||||||
|
{
|
||||||
|
"type": "STRING",
|
||||||
|
"value": "break"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "CHOICE",
|
||||||
|
"members": [
|
||||||
|
{
|
||||||
|
"type": "SYMBOL",
|
||||||
|
"name": "statement"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "BLANK"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"return": {
|
||||||
|
"type": "PREC_LEFT",
|
||||||
|
"value": 0,
|
||||||
|
"content": {
|
||||||
|
"type": "SEQ",
|
||||||
|
"members": [
|
||||||
|
{
|
||||||
|
"type": "STRING",
|
||||||
|
"value": "return"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "CHOICE",
|
||||||
|
"members": [
|
||||||
|
{
|
||||||
|
"type": "SYMBOL",
|
||||||
|
"name": "statement"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "BLANK"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
"statement_kind": {
|
"statement_kind": {
|
||||||
"type": "PREC_LEFT",
|
"type": "PREC_LEFT",
|
||||||
"value": 0,
|
"value": 0,
|
||||||
@ -77,6 +106,10 @@
|
|||||||
"type": "SYMBOL",
|
"type": "SYMBOL",
|
||||||
"name": "block"
|
"name": "block"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"type": "SYMBOL",
|
||||||
|
"name": "break"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"type": "SYMBOL",
|
"type": "SYMBOL",
|
||||||
"name": "expression"
|
"name": "expression"
|
||||||
@ -105,6 +138,10 @@
|
|||||||
"type": "SYMBOL",
|
"type": "SYMBOL",
|
||||||
"name": "pipe"
|
"name": "pipe"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"type": "SYMBOL",
|
||||||
|
"name": "return"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"type": "SYMBOL",
|
"type": "SYMBOL",
|
||||||
"name": "while"
|
"name": "while"
|
||||||
|
@ -70,6 +70,21 @@
|
|||||||
"named": true,
|
"named": true,
|
||||||
"fields": {}
|
"fields": {}
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"type": "break",
|
||||||
|
"named": true,
|
||||||
|
"fields": {},
|
||||||
|
"children": {
|
||||||
|
"multiple": false,
|
||||||
|
"required": false,
|
||||||
|
"types": [
|
||||||
|
{
|
||||||
|
"type": "statement",
|
||||||
|
"named": true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"type": "command",
|
"type": "command",
|
||||||
"named": true,
|
"named": true,
|
||||||
@ -598,6 +613,21 @@
|
|||||||
]
|
]
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"type": "return",
|
||||||
|
"named": true,
|
||||||
|
"fields": {},
|
||||||
|
"children": {
|
||||||
|
"multiple": false,
|
||||||
|
"required": false,
|
||||||
|
"types": [
|
||||||
|
{
|
||||||
|
"type": "statement",
|
||||||
|
"named": true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"type": "root",
|
"type": "root",
|
||||||
"named": true,
|
"named": true,
|
||||||
@ -644,6 +674,10 @@
|
|||||||
"type": "block",
|
"type": "block",
|
||||||
"named": true
|
"named": true
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"type": "break",
|
||||||
|
"named": true
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"type": "expression",
|
"type": "expression",
|
||||||
"named": true
|
"named": true
|
||||||
@ -672,6 +706,10 @@
|
|||||||
"type": "pipe",
|
"type": "pipe",
|
||||||
"named": true
|
"named": true
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"type": "return",
|
||||||
|
"named": true
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"type": "type_definition",
|
"type": "type_definition",
|
||||||
"named": true
|
"named": true
|
||||||
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user