1
0

Add command and pipe syntax

This commit is contained in:
Jeff 2024-01-25 08:27:24 -05:00
parent fe1f007692
commit 5bdb9f116f
8 changed files with 24445 additions and 18843 deletions

View File

@ -1,24 +1,24 @@
use crate::{AbstractTree, Format, Identifier, Result}; use serde::{Deserialize, Serialize};
use crate::{AbstractTree, Error, Format, Identifier, Map, Result, Type, Value};
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub struct Command { pub struct Command {
binary_name: String, binary_name: String,
arguments: Vec<String>, arguments: Vec<String>,
} }
impl AbstractTree for Command { impl AbstractTree for Command {
fn from_syntax( fn from_syntax(node: tree_sitter::Node, source: &str, context: &crate::Map) -> Result<Self> {
node: tree_sitter::Node, Error::expect_syntax_node(source, "command", node)?;
source: &str, let identifier_node = node.child(1)
context: &crate::Map, }
) -> crate::Result<Self> {
fn run(&self, source: &str, context: &Map) -> Result<Value> {
todo!() todo!()
} }
fn run(&self, source: &str, context: &crate::Map) -> crate::Result<crate::Value> { fn expected_type(&self, context: &Map) -> Result<Type> {
todo!()
}
fn expected_type(&self, context: &crate::Map) -> crate::Result<crate::Type> {
todo!() todo!()
} }
} }

View File

@ -23,7 +23,8 @@ pub fn interpret(source: &str) -> Result<Value> {
/// ///
/// A context is a [Map] instance, which is dust's /// A context is a [Map] instance, which is dust's
/// [BTreeMap][std::collections::btree_map::BTreeMap] that is used internally /// [BTreeMap][std::collections::btree_map::BTreeMap] that is used internally
/// for the `<map>` type. Any value can be set /// for the `<map>` type. Any value can be set, including functions and nested
/// maps.
/// ///
/// # Examples /// # Examples
/// ///

View File

@ -10,7 +10,7 @@ Simple Command
(statement (statement
(expression (expression
(command (command
(identifier))))) (command_text)))))
================================================================================ ================================================================================
Nested Command Nested Command
@ -24,9 +24,8 @@ Nested Command
(statement (statement
(expression (expression
(command (command
(identifier) (command_text)
(command (command_text)))))
(identifier))))))
================================================================================ ================================================================================
Command with Arguments Command with Arguments
@ -42,19 +41,13 @@ Command with Arguments
(statement (statement
(expression (expression
(command (command
(identifier) (command_text)
(command_argument) (command_text)
(command_argument)))) (command_text)
(statement (command_text)
(expression (command_text)
(command (command_text)
(identifier) (command_text)))))
(command_argument))))
(statement
(expression
(command
(identifier)
(command_argument)))))
================================================================================ ================================================================================
Nested Command with Arguments Nested Command with Arguments
@ -68,8 +61,7 @@ Nested Command with Arguments
(statement (statement
(expression (expression
(command (command
(identifier) (command_text)
(command (command_text)
(identifier) (command_text)
(command_argument) (command_text)))))
(command_argument))))))

View File

@ -0,0 +1,42 @@
================================================================================
Simple Command Pipe
================================================================================
*ls | *less
--------------------------------------------------------------------------------
(root
(statement
(pipe
(command
(command_text))
(command
(command_text)))))
================================================================================
Simple Function Pipe
================================================================================
fs:read('file.txt') | output()
--------------------------------------------------------------------------------
(root
(statement
(pipe
(function_call
(function_expression
(index
(index_expression
(value
(built_in_value)))
(index_expression
(identifier))))
(expression
(value
(string))))
(function_call
(function_expression
(value
(built_in_value)))))))

View File

@ -23,6 +23,7 @@ module.exports = grammar({
$.index_assignment, $.index_assignment,
$.match, $.match,
$.return, $.return,
$.pipe,
$.while, $.while,
), ),
optional(';'), optional(';'),
@ -64,26 +65,32 @@ module.exports = grammar({
), ),
), ),
command: $ => pipe: $ =>
prec.right( prec(
1,
seq( seq(
'*',
$.identifier,
repeat(
choice( choice(
$.command_argument,
$.command, $.command,
$.function_call,
), ),
'|',
choice(
$.command,
$.pipe,
$.function_call,
), ),
), ),
), ),
command_argument: $ => command: $ =>
choice( prec.right(
/\w+/, seq(
seq('-', /\w+/), '*',
seq('--', /\w+/), repeat($.command_text),
), ),
),
command_text: $ => /\S+/,
block: $ => block: $ =>
seq( seq(

View File

@ -58,6 +58,10 @@
"type": "SYMBOL", "type": "SYMBOL",
"name": "return" "name": "return"
}, },
{
"type": "SYMBOL",
"name": "pipe"
},
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "while" "name": "while"
@ -178,6 +182,49 @@
} }
} }
}, },
"pipe": {
"type": "PREC",
"value": 1,
"content": {
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "command"
},
{
"type": "SYMBOL",
"name": "function_call"
}
]
},
{
"type": "STRING",
"value": "|"
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "command"
},
{
"type": "SYMBOL",
"name": "pipe"
},
{
"type": "SYMBOL",
"name": "function_call"
}
]
}
]
}
},
"command": { "command": {
"type": "PREC_RIGHT", "type": "PREC_RIGHT",
"value": 0, "value": 0,
@ -188,63 +235,19 @@
"type": "STRING", "type": "STRING",
"value": "*" "value": "*"
}, },
{
"type": "SYMBOL",
"name": "identifier"
},
{ {
"type": "REPEAT", "type": "REPEAT",
"content": { "content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL", "type": "SYMBOL",
"name": "command_argument" "name": "command_text"
},
{
"type": "SYMBOL",
"name": "command"
}
]
} }
} }
] ]
} }
}, },
"command_argument": { "command_text": {
"type": "CHOICE",
"members": [
{
"type": "PATTERN", "type": "PATTERN",
"value": "\\w+" "value": "\\S+"
},
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "-"
},
{
"type": "PATTERN",
"value": "\\w+"
}
]
},
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "--"
},
{
"type": "PATTERN",
"value": "\\w+"
}
]
}
]
}, },
"block": { "block": {
"type": "SEQ", "type": "SEQ",

View File

@ -62,28 +62,15 @@
"fields": {}, "fields": {},
"children": { "children": {
"multiple": true, "multiple": true,
"required": true, "required": false,
"types": [ "types": [
{ {
"type": "command", "type": "command_text",
"named": true
},
{
"type": "command_argument",
"named": true
},
{
"type": "identifier",
"named": true "named": true
} }
] ]
} }
}, },
{
"type": "command_argument",
"named": true,
"fields": {}
},
{ {
"type": "else", "type": "else",
"named": true, "named": true,
@ -515,6 +502,29 @@
] ]
} }
}, },
{
"type": "pipe",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": true,
"types": [
{
"type": "command",
"named": true
},
{
"type": "function_call",
"named": true
},
{
"type": "pipe",
"named": true
}
]
}
},
{ {
"type": "range", "type": "range",
"named": true, "named": true,
@ -596,6 +606,10 @@
"type": "match", "type": "match",
"named": true "named": true
}, },
{
"type": "pipe",
"named": true
},
{ {
"type": "return", "type": "return",
"named": true "named": true
@ -797,10 +811,6 @@
"type": "-", "type": "-",
"named": false "named": false
}, },
{
"type": "--",
"named": false
},
{ {
"type": "-=", "type": "-=",
"named": false "named": false
@ -889,6 +899,10 @@
"type": "collection", "type": "collection",
"named": false "named": false
}, },
{
"type": "command_text",
"named": true
},
{ {
"type": "else", "type": "else",
"named": false "named": false
@ -907,11 +921,11 @@
}, },
{ {
"type": "float", "type": "float",
"named": true "named": false
}, },
{ {
"type": "float", "type": "float",
"named": false "named": true
}, },
{ {
"type": "for", "type": "for",
@ -995,11 +1009,11 @@
}, },
{ {
"type": "string", "type": "string",
"named": true "named": false
}, },
{ {
"type": "string", "type": "string",
"named": false "named": true
}, },
{ {
"type": "struct", "type": "struct",
@ -1017,6 +1031,10 @@
"type": "{", "type": "{",
"named": false "named": false
}, },
{
"type": "|",
"named": false
},
{ {
"type": "||", "type": "||",
"named": false "named": false

File diff suppressed because it is too large Load Diff