Simplify grammar structure

This commit is contained in:
Jeff 2023-11-10 20:44:03 -05:00
parent 71c169a1cf
commit f0fb16607c
31 changed files with 29020 additions and 85617 deletions

View File

@ -1,42 +0,0 @@
use rayon::prelude::*;
use serde::{Deserialize, Serialize};
use tree_sitter::Node;
use crate::{AbstractTree, Block, Map, Result, Value};
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub struct Async {
block: Block,
}
impl AbstractTree for Async {
fn from_syntax_node(source: &str, node: Node) -> Result<Self> {
debug_assert_eq!("async", node.kind());
let block_node = node.child(1).unwrap();
let block = Block::from_syntax_node(source, block_node)?;
Ok(Async { block })
}
fn run(&self, source: &str, context: &mut Map) -> Result<Value> {
let statements = self.block.statements();
statements
.into_par_iter()
.enumerate()
.find_map_first(|(index, statement)| {
let mut context = context.clone();
let result = statement.run(source, &mut context);
if result.is_err() {
Some(result)
} else if index == statements.len() - 1 {
Some(result)
} else {
None
}
})
.unwrap_or(Ok(Value::Empty))
}
}

View File

@ -1,27 +1,26 @@
use rayon::prelude::*;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tree_sitter::Node; use tree_sitter::Node;
use crate::{AbstractTree, Result, Statement}; use crate::{AbstractTree, Result, Statement, Value};
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub struct Block { pub struct Block {
pub statements: Vec<Statement>, is_async: bool,
} statements: Vec<Statement>,
impl Block {
pub fn statements(&self) -> &Vec<Statement> {
&self.statements
}
} }
impl AbstractTree for Block { impl AbstractTree for Block {
fn from_syntax_node(source: &str, node: Node) -> Result<Self> { fn from_syntax_node(source: &str, node: Node) -> Result<Self> {
debug_assert_eq!("block", node.kind()); debug_assert_eq!("block", node.kind());
let first_child = node.child(0).unwrap();
let is_async = first_child.kind() == "async";
let statement_count = node.child_count(); let statement_count = node.child_count();
let mut statements = Vec::with_capacity(statement_count); let mut statements = Vec::with_capacity(statement_count);
for index in 0..statement_count { for index in 0..statement_count - 1 {
let child_node = node.child(index).unwrap(); let child_node = node.child(index).unwrap();
if child_node.kind() == "statement" { if child_node.kind() == "statement" {
@ -30,10 +29,33 @@ impl AbstractTree for Block {
} }
} }
Ok(Block { statements }) Ok(Block {
is_async,
statements,
})
} }
fn run(&self, source: &str, context: &mut crate::Map) -> crate::Result<crate::Value> { fn run(&self, source: &str, context: &mut crate::Map) -> crate::Result<crate::Value> {
if self.is_async {
let statements = &self.statements;
statements
.into_par_iter()
.enumerate()
.find_map_first(|(index, statement)| {
let mut context = context.clone();
let result = statement.run(source, &mut context);
if result.is_err() {
Some(result)
} else if index == statements.len() - 1 {
Some(result)
} else {
None
}
})
.unwrap_or(Ok(Value::Empty))
} else {
for statement in &self.statements[0..self.statements.len() - 1] { for statement in &self.statements[0..self.statements.len() - 1] {
statement.run(source, context)?; statement.run(source, context)?;
} }
@ -44,3 +66,4 @@ impl AbstractTree for Block {
Ok(final_value) Ok(final_value)
} }
} }
}

View File

@ -50,13 +50,13 @@ impl AbstractTree for Index {
Ok(item) Ok(item)
} }
Value::Map(map) => { Value::Map(mut map) => {
let value = if let Expression::Identifier(identifier) = &self.index { let value = if let Expression::Identifier(identifier) = &self.index {
let key = identifier.inner(); let key = identifier.inner();
map.variables()?.get(key).cloned().unwrap_or(Value::Empty) map.variables()?.get(key).cloned().unwrap_or(Value::Empty)
} else { } else {
let value = self.index.run(source, context)?; let value = self.index.run(source, &mut map)?;
let key = value.as_string()?; let key = value.as_string()?;
map.variables()?.get(key).cloned().unwrap_or(Value::Empty) map.variables()?.get(key).cloned().unwrap_or(Value::Empty)
@ -96,7 +96,7 @@ mod tests {
#[test] #[test]
fn evaluate_complex_index() { fn evaluate_complex_index() {
let test = evaluate("{x = [1 2 3]; y = || => {0}; x:((y));}").unwrap(); let test = evaluate("x = [1 2 3]; y = || => {0}; x:((y));").unwrap();
assert_eq!(Value::Integer(1), test); assert_eq!(Value::Integer(1), test);
} }

View File

@ -7,7 +7,6 @@
//! examples. //! examples.
pub mod assignment; pub mod assignment;
pub mod r#async;
pub mod block; pub mod block;
pub mod built_in_function; pub mod built_in_function;
pub mod expression; pub mod expression;
@ -31,9 +30,8 @@ pub mod r#while;
pub use { pub use {
assignment::*, block::*, built_in_function::*, expression::*, filter::*, find::*, assignment::*, block::*, built_in_function::*, expression::*, filter::*, find::*,
function_call::*, identifier::*, if_else::*, index::*, insert::*, logic::*, math::*, function_call::*, identifier::*, if_else::*, index::*, insert::*, logic::*, math::*, r#for::*,
r#async::*, r#for::*, r#match::*, r#while::*, remove::*, select::*, statement::*, transform::*, r#match::*, r#while::*, remove::*, select::*, statement::*, transform::*, value_node::*,
value_node::*,
}; };
use tree_sitter::Node; use tree_sitter::Node;

View File

@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize};
use tree_sitter::Node; use tree_sitter::Node;
use crate::{ use crate::{
AbstractTree, Assignment, Async, Error, Expression, Filter, Find, For, IfElse, Insert, Map, AbstractTree, Assignment, Block, Error, Expression, Filter, Find, For, IfElse, Insert, Map,
Match, Remove, Result, Select, Transform, Value, While, Match, Remove, Result, Select, Transform, Value, While,
}; };
@ -17,7 +17,7 @@ pub enum Statement {
IfElse(Box<IfElse>), IfElse(Box<IfElse>),
Match(Match), Match(Match),
While(Box<While>), While(Box<While>),
Async(Box<Async>), Block(Box<Block>),
For(Box<For>), For(Box<For>),
Transform(Box<Transform>), Transform(Box<Transform>),
Filter(Box<Filter>), Filter(Box<Filter>),
@ -49,7 +49,7 @@ impl AbstractTree for Statement {
"while" => Ok(Statement::While(Box::new(While::from_syntax_node( "while" => Ok(Statement::While(Box::new(While::from_syntax_node(
source, child, source, child,
)?))), )?))),
"async" => Ok(Statement::Async(Box::new(Async::from_syntax_node( "block" => Ok(Statement::Block(Box::new(Block::from_syntax_node(
source, child, source, child,
)?))), )?))),
"for" => Ok(Statement::For(Box::new(For::from_syntax_node( "for" => Ok(Statement::For(Box::new(For::from_syntax_node(
@ -89,7 +89,7 @@ impl AbstractTree for Statement {
Statement::IfElse(if_else) => if_else.run(source, context), Statement::IfElse(if_else) => if_else.run(source, context),
Statement::Match(r#match) => r#match.run(source, context), Statement::Match(r#match) => r#match.run(source, context),
Statement::While(r#while) => r#while.run(source, context), Statement::While(r#while) => r#while.run(source, context),
Statement::Async(run) => run.run(source, context), Statement::Block(block) => block.run(source, context),
Statement::For(r#for) => r#for.run(source, context), Statement::For(r#for) => r#for.run(source, context),
Statement::Transform(transform) => transform.run(source, context), Statement::Transform(transform) => transform.run(source, context),
Statement::Filter(filter) => filter.run(source, context), Statement::Filter(filter) => filter.run(source, context),

View File

@ -6,7 +6,7 @@ use std::fmt::{self, Debug, Formatter};
use tree_sitter::{Parser, Tree as TSTree}; use tree_sitter::{Parser, Tree as TSTree};
use crate::{language, AbstractTree, Block, Map, Result, Value}; use crate::{language, AbstractTree, Map, Result, Statement, Value};
/// Evaluate the given source code. /// Evaluate the given source code.
/// ///
@ -89,9 +89,9 @@ impl<'context, 'code> Evaluator<'context, 'code> {
let root_node = cursor.node(); let root_node = cursor.node();
let mut prev_result = Ok(Value::Empty); let mut prev_result = Ok(Value::Empty);
for block_node in root_node.children(&mut cursor) { for statement_node in root_node.children(&mut cursor) {
let block = Block::from_syntax_node(self.source, block_node)?; let statement = Statement::from_syntax_node(self.source, statement_node)?;
prev_result = block.run(self.source, self.context); prev_result = statement.run(self.source, self.context);
} }
prev_result prev_result
@ -227,7 +227,7 @@ mod tests {
} }
#[test] #[test]
fn evaluate_if_else_else_if_else_if_else_if_else() { fn evaluate_if_else_if_else_if_else_if_else() {
assert_eq!( assert_eq!(
evaluate( evaluate(
" "
@ -253,7 +253,7 @@ mod tests {
assert_eq!( assert_eq!(
evaluate( evaluate(
" "
foobar = |message| => message foobar = |message| => { message }
(foobar 'Hiya') (foobar 'Hiya')
", ",
), ),

View File

@ -1,13 +1,12 @@
================== ================================================================================
Simple Async Statements Simple Async Statements
================== ================================================================================
async { (output 'Whaddup') } async { (output 'Whaddup') }
--- --------------------------------------------------------------------------------
(root (root
(block
(statement (statement
(async (async
(block (block
@ -17,11 +16,11 @@ async { (output 'Whaddup') }
(built_in_function (built_in_function
(expression (expression
(value (value
(string)))))))))))) (string)))))))))))
================== ================================================================================
Complex Async Statements Complex Async Statements
================== ================================================================================
async { async {
if 1 % 2 == 0 { if 1 % 2 == 0 {
@ -33,10 +32,9 @@ async {
'foobar' 'foobar'
} }
--- --------------------------------------------------------------------------------
(root (root
(block
(statement (statement
(async (async
(block (block
@ -72,4 +70,4 @@ async {
(statement (statement
(expression (expression
(value (value
(string))))))))) (string))))))))

View File

@ -1,32 +1,30 @@
================== ================================================================================
Simple Function Call Simple Function Call
================== ================================================================================
(output 'hi') (output 'hi')
--- --------------------------------------------------------------------------------
(root (root
(block
(statement (statement
(expression (expression
(function_call (function_call
(built_in_function (built_in_function
(expression (expression
(value (value
(string))))))))) (string))))))))
================== ================================================================================
Nested Function Call Nested Function Call
================== ================================================================================
(assert_equal (random_integer) 4) (assert_equal (random_integer) 4)
assert_equal random_integer 4 assert_equal random_integer 4
--- --------------------------------------------------------------------------------
(root (root
(block
(statement (statement
(expression (expression
(function_call (function_call
@ -46,4 +44,4 @@ assert_equal random_integer 4
(built_in_function (built_in_function
(expression (expression
(value (value
(integer)))))))))))) (integer)))))))))))

View File

@ -8,10 +8,9 @@ not_a_comment
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
(root (root
(block
(statement (statement
(expression (expression
(identifier))))) (identifier))))
================================================================================ ================================================================================
Partial Line Comments Partial Line Comments
@ -22,10 +21,9 @@ not_a_comment # comment
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
(root (root
(block
(statement (statement
(expression (expression
(identifier))))) (identifier))))
================================================================================ ================================================================================
Multiline Comments Multiline Comments
@ -38,11 +36,10 @@ not_a_comment #
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
(root (root
(block
(statement (statement
(expression (expression
(identifier))) (identifier)))
(statement (statement
(expression (expression
(value (value
(string)))))) (string)))))

View File

@ -1,15 +1,14 @@
================== ================================================================================
Simple Filter Loop Simple Filter Loop
================== ================================================================================
filter i in [1, 2, 3] { filter i in [1, 2, 3] {
i <= 1 i <= 1
} }
--- --------------------------------------------------------------------------------
(root (root
(block
(statement (statement
(filter (filter
(identifier) (identifier)
@ -34,11 +33,11 @@ filter i in [1, 2, 3] {
(logic_operator) (logic_operator)
(expression (expression
(value (value
(integer))))))))))) (integer))))))))))
================== ================================================================================
Nested Filter Loop Nested Filter Loop
================== ================================================================================
filter i in big_list { filter i in big_list {
filter j in i { filter j in i {
@ -46,10 +45,9 @@ filter i in big_list {
} }
} }
--- --------------------------------------------------------------------------------
(root (root
(block
(statement (statement
(filter (filter
(identifier) (identifier)
@ -70,4 +68,4 @@ filter i in big_list {
(logic_operator) (logic_operator)
(expression (expression
(value (value
(integer)))))))))))))) (integer)))))))))))))

View File

@ -9,7 +9,6 @@ find i in [1, 2, 3] {
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
(root (root
(block
(statement (statement
(find (find
(identifier) (identifier)
@ -34,7 +33,7 @@ find i in [1, 2, 3] {
(logic_operator) (logic_operator)
(expression (expression
(value (value
(integer))))))))))) (integer))))))))))
================================================================================ ================================================================================
Nested Find Loop Nested Find Loop
@ -56,7 +55,6 @@ find i in ["one", "two", "three"] {
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
(root (root
(block
(statement (statement
(find (find
(identifier) (identifier)
@ -116,4 +114,4 @@ find i in ["one", "two", "three"] {
(statement (statement
(expression (expression
(value (value
(boolean))))))))))))) (boolean))))))))))))

View File

@ -2,12 +2,13 @@
Simple For Loop Simple For Loop
================================================================================ ================================================================================
for i in [1, 2, 3] output i for i in [1, 2, 3] {
output i
}
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
(root (root
(block
(statement (statement
(for (for
(identifier) (identifier)
@ -29,18 +30,21 @@ for i in [1, 2, 3] output i
(function_call (function_call
(built_in_function (built_in_function
(expression (expression
(identifier))))))))))) (identifier))))))))))
================================================================================ ================================================================================
Nested For Loop Nested For Loop
================================================================================ ================================================================================
for list in list_of_lists for item in list output item for list in list_of_lists {
for item in list {
(output item)
}
}
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
(root (root
(block
(statement (statement
(for (for
(identifier) (identifier)
@ -58,4 +62,4 @@ for list in list_of_lists for item in list output item
(function_call (function_call
(built_in_function (built_in_function
(expression (expression
(identifier)))))))))))))) (identifier)))))))))))))

View File

@ -2,12 +2,11 @@
Simple Function Simple Function
================================================================================ ================================================================================
=> "Hiya" => { "Hiya" }
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
(root (root
(block
(statement (statement
(expression (expression
(value (value
@ -16,18 +15,17 @@ Simple Function
(statement (statement
(expression (expression
(value (value
(string))))))))))) (string))))))))))
================================================================================ ================================================================================
Function Assignment Function Assignment
================================================================================ ================================================================================
x = => "Hiya" x = => { "Hiya" }
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
(root (root
(block
(statement (statement
(assignment (assignment
(identifier) (identifier)
@ -40,7 +38,7 @@ x = => "Hiya"
(statement (statement
(expression (expression
(value (value
(string))))))))))))) (string))))))))))))
================================================================================ ================================================================================
Function Call Function Call
@ -51,14 +49,13 @@ Function Call
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
(root (root
(block
(statement (statement
(expression (expression
(function_call (function_call
(identifier) (identifier)
(expression (expression
(value (value
(string)))))))) (string)))))))
================================================================================ ================================================================================
Complex Function Complex Function
@ -72,7 +69,6 @@ Complex Function
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
(root (root
(block
(statement (statement
(expression (expression
(value (value
@ -92,7 +88,7 @@ Complex Function
(function_call (function_call
(built_in_function (built_in_function
(expression (expression
(identifier))))))))))))) (identifier))))))))))))
================================================================================ ================================================================================
Complex Function Call Complex Function Call
@ -110,7 +106,6 @@ Complex Function Call
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
(root (root
(block
(statement (statement
(expression (expression
(function_call (function_call
@ -133,4 +128,4 @@ Complex Function Call
(statement (statement
(expression (expression
(value (value
(integer)))))))))))) (integer)))))))))))

View File

@ -1,15 +1,14 @@
================== ================================================================================
Simple Identifiers Simple Identifiers
================== ================================================================================
x x
_y _y
__xyz__ __xyz__
--- --------------------------------------------------------------------------------
(root (root
(block
(statement (statement
(expression (expression
(identifier))) (identifier)))
@ -18,4 +17,4 @@ __xyz__
(identifier))) (identifier)))
(statement (statement
(expression (expression
(identifier))))) (identifier))))

View File

@ -1,13 +1,12 @@
================== ================================================================================
Simple If Simple If
================== ================================================================================
if true { "True" } if true { "True" }
--- --------------------------------------------------------------------------------
(root (root
(block
(statement (statement
(if_else (if_else
(if (if
@ -18,18 +17,17 @@ if true { "True" }
(statement (statement
(expression (expression
(value (value
(string)))))))))) (string)))))))))
================== ================================================================================
Complex If Complex If
================== ================================================================================
if 1 == 1 && 2 == 2 && 3 == 3 "True" if 1 == 1 && 2 == 2 && 3 == 3 { "True" }
--- --------------------------------------------------------------------------------
(root (root
(block
(statement (statement
(if_else (if_else
(if (if
@ -70,22 +68,23 @@ if 1 == 1 && 2 == 2 && 3 == 3 "True"
(statement (statement
(expression (expression
(value (value
(string)))))))))) (string)))))))))
================== ================================================================================
Nested If Nested If
================== ================================================================================
if true if true {
if 42 == 12 if 42 == 12 {
'hiya' 'hiya'
else } else {
'bye' 'bye'
}
}
--- --------------------------------------------------------------------------------
(root (root
(block
(statement (statement
(if_else (if_else
(if (if
@ -115,18 +114,17 @@ if true
(statement (statement
(expression (expression
(value (value
(string)))))))))))))) (string)))))))))))))
================== ================================================================================
If Else If Else
================== ================================================================================
if false "True" else "False" if false { "True" } else { "False" }
--- --------------------------------------------------------------------------------
(root (root
(block
(statement (statement
(if_else (if_else
(if (if
@ -143,11 +141,11 @@ if false "True" else "False"
(statement (statement
(expression (expression
(value (value
(string)))))))))) (string)))))))))
================== ================================================================================
If Else If If Else If
================== ================================================================================
if 1 == 1 { if 1 == 1 {
"math is fun" "math is fun"
@ -155,10 +153,9 @@ if 1 == 1 {
"math is broken" "math is broken"
} }
--- --------------------------------------------------------------------------------
(root (root
(block
(statement (statement
(if_else (if_else
(if (if
@ -190,25 +187,25 @@ if 1 == 1 {
(statement (statement
(expression (expression
(value (value
(string)))))))))) (string)))))))))
================== ================================================================================
If Else Else If Else If Else Else If Else
================== ================================================================================
if false if false {
"no" "no"
else if false } else if false {
"no" "no"
else if 1 + 1 == 9 } else if 1 + 1 == 9 {
"not the answer" "not the answer"
else } else {
"42" "42"
}
--- --------------------------------------------------------------------------------
(root (root
(block
(statement (statement
(if_else (if_else
(if (if
@ -255,4 +252,4 @@ else
(statement (statement
(expression (expression
(value (value
(string)))))))))) (string)))))))))

View File

@ -1,6 +1,6 @@
================== ================================================================================
Simple Indexes Simple Indexes
================== ================================================================================
dust_data:1:name dust_data:1:name
@ -8,10 +8,9 @@ creature:total_clams
foobar:1:42 foobar:1:42
--- --------------------------------------------------------------------------------
(root (root
(block
(statement (statement
(expression (expression
(index (index
@ -43,18 +42,17 @@ foobar:1:42
(integer))))) (integer)))))
(expression (expression
(value (value
(integer)))))))) (integer)))))))
================== ================================================================================
Nested Indexes Nested Indexes
================== ================================================================================
[['answers' 'foobar'], 42, 666]:0:1:0..2 [['answers' 'foobar'], 42, 666]:0:1:0..2
--- --------------------------------------------------------------------------------
(root (root
(block
(statement (statement
(expression (expression
(index (index
@ -91,4 +89,4 @@ Nested Indexes
(integer))) (integer)))
(expression (expression
(value (value
(integer)))))))) (integer)))))))

View File

@ -1,13 +1,12 @@
================== ================================================================================
List Declaration List Declaration
================== ================================================================================
['answer', 42] ['answer', 42]
--- --------------------------------------------------------------------------------
(root (root
(block
(statement (statement
(expression (expression
(value (value
@ -17,18 +16,17 @@ List Declaration
(string))) (string)))
(expression (expression
(value (value
(integer))))))))) (integer))))))))
================== ================================================================================
List Nesting List Nesting
================== ================================================================================
['answers', [42, [666]]] ['answers', [42, [666]]]
--- --------------------------------------------------------------------------------
(root (root
(block
(statement (statement
(expression (expression
(value (value
@ -47,4 +45,4 @@ List Nesting
(list (list
(expression (expression
(value (value
(integer))))))))))))))) (integer))))))))))))))

View File

@ -1,26 +1,26 @@
================== ================================================================================
Simple Map Simple Map
================== ================================================================================
{ answer = 42 } { answer = 42 }
--- --------------------------------------------------------------------------------
(root (root
(statement
(block (block
(statement (statement
(expression (assignment
(value
(map
(identifier) (identifier)
(assignment_operator)
(statement (statement
(expression (expression
(value (value
(integer)))))))))) (integer)))))))))
================== ================================================================================
Nested Maps Nested Maps
================== ================================================================================
x = { x = {
y = { y = {
@ -32,19 +32,19 @@ x = {
f = 12 f = 12
} }
--- --------------------------------------------------------------------------------
(root (root
(block
(statement (statement
(assignment (assignment
(identifier) (identifier)
(assignment_operator) (assignment_operator)
(statement (statement
(expression (block
(value (statement
(map (assignment
(identifier) (identifier)
(assignment_operator)
(statement (statement
(expression (expression
(value (value
@ -63,9 +63,12 @@ x = {
(statement (statement
(expression (expression
(value (value
(string)))))))))))) (string))))))))))))))
(statement
(assignment
(identifier) (identifier)
(assignment_operator)
(statement (statement
(expression (expression
(value (value
(integer)))))))))))) (integer)))))))))))

View File

@ -1,13 +1,12 @@
================== ================================================================================
\== \==
================== ================================================================================
3 == 1 + 1 + 1 3 == 1 + 1 + 1
--- --------------------------------------------------------------------------------
(root (root
(block
(statement (statement
(expression (expression
(logic (logic
@ -29,18 +28,17 @@
(math_operator) (math_operator)
(expression (expression
(value (value
(integer)))))))))) (integer)))))))))
================== ================================================================================
&& &&
================== ================================================================================
4 + 2 == 42 && true 4 + 2 == 42 && true
--- --------------------------------------------------------------------------------
(root (root
(block
(statement (statement
(expression (expression
(logic (logic
@ -62,18 +60,17 @@
(logic_operator) (logic_operator)
(expression (expression
(value (value
(boolean)))))))))) (boolean)))))))))
================== ================================================================================
\|| \||
================== ================================================================================
4 + 2 == 42 || true 4 + 2 == 42 || true
--- --------------------------------------------------------------------------------
(root (root
(block
(statement (statement
(expression (expression
(logic (logic
@ -95,4 +92,4 @@
(logic_operator) (logic_operator)
(expression (expression
(value (value
(boolean)))))))))) (boolean)))))))))

View File

@ -1,15 +1,14 @@
================== ================================================================================
Simple Reduce Loop Simple Reduce Loop
================== ================================================================================
reduce i to acc in [1, 2, 3] { reduce i to acc in [1, 2, 3] {
acc += i acc += i
} }
--- --------------------------------------------------------------------------------
(root (root
(block
(statement (statement
(reduce (reduce
(identifier) (identifier)
@ -33,20 +32,19 @@ reduce i to acc in [1, 2, 3] {
(assignment_operator) (assignment_operator)
(statement (statement
(expression (expression
(identifier)))))))))) (identifier)))))))))
================== ================================================================================
Nested Reduce Loop Nested Reduce Loop
================== ================================================================================
reduce i to acc in ["one", "two", "three"] { reduce i to acc in ["one", "two", "three"] {
acc += i acc += i
} }
--- --------------------------------------------------------------------------------
(root (root
(block
(statement (statement
(reduce (reduce
(identifier) (identifier)
@ -70,4 +68,4 @@ reduce i to acc in ["one", "two", "three"] {
(assignment_operator) (assignment_operator)
(statement (statement
(expression (expression
(identifier)))))))))) (identifier)))))))))

View File

@ -1,15 +1,14 @@
================== ================================================================================
Simple Remove Simple Remove
================== ================================================================================
remove i from [1, 2, 3] { remove i from [1, 2, 3] {
i <= 2 i <= 2
} }
--- --------------------------------------------------------------------------------
(root (root
(block
(statement (statement
(remove (remove
(identifier) (identifier)
@ -34,11 +33,11 @@ remove i from [1, 2, 3] {
(logic_operator) (logic_operator)
(expression (expression
(value (value
(integer))))))))))) (integer))))))))))
================== ================================================================================
Nested Remove Nested Remove
================== ================================================================================
remove i from big_list { remove i from big_list {
remove j from i { remove j from i {
@ -46,10 +45,9 @@ remove i from big_list {
} }
} }
--- --------------------------------------------------------------------------------
(root (root
(block
(statement (statement
(remove (remove
(identifier) (identifier)
@ -70,4 +68,4 @@ remove i from big_list {
(logic_operator) (logic_operator)
(expression (expression
(value (value
(integer)))))))))))))) (integer)))))))))))))

View File

@ -1,15 +1,14 @@
================== ================================================================================
Simple Statements Simple Statements
================== ================================================================================
1 1
"one"; "one";
x x
--- --------------------------------------------------------------------------------
(root (root
(block
(statement (statement
(expression (expression
(value (value
@ -20,19 +19,18 @@ x
(string)))) (string))))
(statement (statement
(expression (expression
(identifier))))) (identifier))))
================== ================================================================================
Simple Assignment Simple Assignment
================== ================================================================================
x = 1; x = 1;
y = "one" y = "one"
--- --------------------------------------------------------------------------------
(root (root
(block
(statement (statement
(assignment (assignment
(identifier) (identifier)
@ -48,11 +46,11 @@ y = "one"
(statement (statement
(expression (expression
(value (value
(string)))))))) (string)))))))
================== ================================================================================
Complex Assignment Complex Assignment
================== ================================================================================
x = if 1 + 1 == 2 { x = if 1 + 1 == 2 {
'yo' 'yo'
@ -60,10 +58,9 @@ x = if 1 + 1 == 2 {
'no' 'no'
} }
--- --------------------------------------------------------------------------------
(root (root
(block
(statement (statement
(assignment (assignment
(identifier) (identifier)
@ -96,18 +93,17 @@ x = if 1 + 1 == 2 {
(statement (statement
(expression (expression
(value (value
(string)))))))))))) (string)))))))))))
================== ================================================================================
Expression Precedence Expression Precedence
================== ================================================================================
x = 3 == 1 + 2 + 2 x = 3 == 1 + 2 + 2
--- --------------------------------------------------------------------------------
(root (root
(block
(statement (statement
(assignment (assignment
(identifier) (identifier)
@ -133,4 +129,4 @@ x = 3 == 1 + 2 + 2
(math_operator) (math_operator)
(expression (expression
(value (value
(integer)))))))))))) (integer)))))))))))

View File

@ -1,6 +1,6 @@
================== ================================================================================
Table Declaration Table Declaration
================== ================================================================================
table |messages numbers| [ table |messages numbers| [
['hiya' 42] ['hiya' 42]
@ -8,10 +8,9 @@ table |messages numbers| [
['bar' 99.99] ['bar' 99.99]
] ]
--- --------------------------------------------------------------------------------
(root (root
(block
(statement (statement
(expression (expression
(value (value
@ -48,20 +47,19 @@ table |messages numbers| [
(string))) (string)))
(expression (expression
(value (value
(float))))))))))))))) (float))))))))))))))
================== ================================================================================
Table Access Table Access
================== ================================================================================
select |number| from foobar { select |number| from foobar {
text == 'answer' text == 'answer'
} }
--- --------------------------------------------------------------------------------
(root (root
(block
(statement (statement
(select (select
(identifier_list (identifier_list
@ -77,20 +75,19 @@ select |number| from foobar {
(logic_operator) (logic_operator)
(expression (expression
(value (value
(string))))))))))) (string))))))))))
================== ================================================================================
Table Insert Table Insert
================== ================================================================================
insert into foobar [ insert into foobar [
['bob was here', 0] ['bob was here', 0]
] ]
--- --------------------------------------------------------------------------------
(root (root
(block
(statement (statement
(insert (insert
(identifier) (identifier)
@ -105,4 +102,4 @@ insert into foobar [
(string))) (string)))
(expression (expression
(value (value
(integer))))))))))))) (integer))))))))))))

View File

@ -9,7 +9,6 @@ transform i in [1, 2, 3] {
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
(root (root
(block
(statement (statement
(transform (transform
(identifier) (identifier)
@ -31,7 +30,7 @@ transform i in [1, 2, 3] {
(function_call (function_call
(built_in_function (built_in_function
(expression (expression
(identifier))))))))))) (identifier))))))))))
================================================================================ ================================================================================
Nested Transform Loop Nested Transform Loop
@ -46,7 +45,6 @@ transform i in [['one'] ['two'] ['three']] {
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
(root (root
(block
(statement (statement
(transform (transform
(identifier) (identifier)
@ -85,4 +83,4 @@ transform i in [['one'] ['two'] ['three']] {
(statement (statement
(expression (expression
(value (value
(string)))))))))))))) (string)))))))))))))

View File

@ -1,14 +1,13 @@
================== ================================================================================
Booleans Booleans
================== ================================================================================
true true
false false
--- --------------------------------------------------------------------------------
(root (root
(block
(statement (statement
(expression (expression
(value (value
@ -16,19 +15,18 @@ false
(statement (statement
(expression (expression
(value (value
(boolean)))))) (boolean)))))
================== ================================================================================
Integers Integers
================== ================================================================================
1 2 3 1 2 3
456 7 456 7
--- --------------------------------------------------------------------------------
(root (root
(block
(statement (statement
(expression (expression
(value (value
@ -48,18 +46,17 @@ Integers
(statement (statement
(expression (expression
(value (value
(integer)))))) (integer)))))
================== ================================================================================
Strings Strings
================== ================================================================================
"one" 'two' "three" `four` 'five' "one" 'two' "three" `four` 'five'
--- --------------------------------------------------------------------------------
(root (root
(block
(statement (statement
(expression (expression
(value (value
@ -79,4 +76,4 @@ Strings
(statement (statement
(expression (expression
(value (value
(string)))))) (string)))))

View File

@ -9,7 +9,6 @@ while true {
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
(root (root
(block
(statement (statement
(while (while
(expression (expression
@ -22,7 +21,7 @@ while true {
(built_in_function (built_in_function
(expression (expression
(value (value
(string)))))))))))) (string)))))))))))
================================================================================ ================================================================================
Nested While Loop Nested While Loop
@ -37,7 +36,6 @@ while (true) {
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
(root (root
(block
(statement (statement
(while (while
(expression (expression
@ -62,4 +60,4 @@ while (true) {
(statement (statement
(expression (expression
(value (value
(integer)))))))))))))) (integer)))))))))))))

View File

@ -7,7 +7,6 @@ Simple Yield
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
(root (root
(block
(statement (statement
(expression (expression
(yield (yield
@ -15,7 +14,7 @@ Simple Yield
(value (value
(integer))) (integer)))
(function_call (function_call
(built_in_function))))))) (built_in_function))))))
================================================================================ ================================================================================
Long Yield Long Yield
@ -30,7 +29,6 @@ Long Yield
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
(root (root
(block
(statement (statement
(expression (expression
(yield (yield
@ -59,4 +57,4 @@ Long Yield
(function_call (function_call
(identifier)))) (identifier))))
(function_call (function_call
(built_in_function))))))) (built_in_function))))))

View File

@ -10,19 +10,21 @@ module.exports = grammar({
], ],
rules: { rules: {
root: $ => repeat1($.block), root: $ => prec(1, repeat1($.statement)),
_comment: $ => /[#][^#\n]*[#|\n]/, _comment: $ => /[#][^#\n]*[#|\n]/,
block: $ => prec.right(choice( block: $ => seq(
optional('async'),
'{',
repeat1($.statement), repeat1($.statement),
seq('{', repeat1($.statement), '}'), '}',
)), ),
statement: $ => prec.right(seq( statement: $ => prec.right(seq(
choice( choice(
$.assignment, $.assignment,
$.async, $.block,
$.expression, $.expression,
$.filter, $.filter,
$.find, $.find,
@ -268,11 +270,6 @@ module.exports = grammar({
$.expression, $.expression,
)), )),
async: $ => seq(
'async',
$.block,
),
identifier_list: $ => prec.right(choice( identifier_list: $ => prec.right(choice(
seq( seq(
'|', '|',

View File

@ -3,10 +3,14 @@
"word": "identifier", "word": "identifier",
"rules": { "rules": {
"root": { "root": {
"type": "PREC",
"value": 1,
"content": {
"type": "REPEAT1", "type": "REPEAT1",
"content": { "content": {
"type": "SYMBOL", "type": "SYMBOL",
"name": "block" "name": "statement"
}
} }
}, },
"_comment": { "_comment": {
@ -14,19 +18,6 @@
"value": "[#][^#\\n]*[#|\\n]" "value": "[#][^#\\n]*[#|\\n]"
}, },
"block": { "block": {
"type": "PREC_RIGHT",
"value": 0,
"content": {
"type": "CHOICE",
"members": [
{
"type": "REPEAT1",
"content": {
"type": "SYMBOL",
"name": "statement"
}
},
{
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
@ -45,9 +36,6 @@
"value": "}" "value": "}"
} }
] ]
}
]
}
}, },
"statement": { "statement": {
"type": "PREC_RIGHT", "type": "PREC_RIGHT",
@ -66,6 +54,10 @@
"type": "SYMBOL", "type": "SYMBOL",
"name": "async" "name": "async"
}, },
{
"type": "SYMBOL",
"name": "block"
},
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "expression" "name": "expression"

View File

@ -538,7 +538,7 @@
"required": true, "required": true,
"types": [ "types": [
{ {
"type": "block", "type": "statement",
"named": true "named": true
} }
] ]
@ -583,6 +583,10 @@
"type": "async", "type": "async",
"named": true "named": true
}, },
{
"type": "block",
"named": true
},
{ {
"type": "expression", "type": "expression",
"named": true "named": true

File diff suppressed because it is too large Load Diff