Add command and pipe syntax
This commit is contained in:
parent
fe1f007692
commit
5bdb9f116f
@ -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 {
|
||||
binary_name: String,
|
||||
arguments: Vec<String>,
|
||||
}
|
||||
|
||||
impl AbstractTree for Command {
|
||||
fn from_syntax(
|
||||
node: tree_sitter::Node,
|
||||
source: &str,
|
||||
context: &crate::Map,
|
||||
) -> crate::Result<Self> {
|
||||
fn from_syntax(node: tree_sitter::Node, source: &str, context: &crate::Map) -> Result<Self> {
|
||||
Error::expect_syntax_node(source, "command", node)?;
|
||||
let identifier_node = node.child(1)
|
||||
}
|
||||
|
||||
fn run(&self, source: &str, context: &Map) -> Result<Value> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn run(&self, source: &str, context: &crate::Map) -> crate::Result<crate::Value> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn expected_type(&self, context: &crate::Map) -> crate::Result<crate::Type> {
|
||||
fn expected_type(&self, context: &Map) -> Result<Type> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
@ -23,7 +23,8 @@ pub fn interpret(source: &str) -> Result<Value> {
|
||||
///
|
||||
/// A context is a [Map] instance, which is dust's
|
||||
/// [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
|
||||
///
|
||||
|
@ -10,7 +10,7 @@ Simple Command
|
||||
(statement
|
||||
(expression
|
||||
(command
|
||||
(identifier)))))
|
||||
(command_text)))))
|
||||
|
||||
================================================================================
|
||||
Nested Command
|
||||
@ -24,9 +24,8 @@ Nested Command
|
||||
(statement
|
||||
(expression
|
||||
(command
|
||||
(identifier)
|
||||
(command
|
||||
(identifier))))))
|
||||
(command_text)
|
||||
(command_text)))))
|
||||
|
||||
================================================================================
|
||||
Command with Arguments
|
||||
@ -42,19 +41,13 @@ Command with Arguments
|
||||
(statement
|
||||
(expression
|
||||
(command
|
||||
(identifier)
|
||||
(command_argument)
|
||||
(command_argument))))
|
||||
(statement
|
||||
(expression
|
||||
(command
|
||||
(identifier)
|
||||
(command_argument))))
|
||||
(statement
|
||||
(expression
|
||||
(command
|
||||
(identifier)
|
||||
(command_argument)))))
|
||||
(command_text)
|
||||
(command_text)
|
||||
(command_text)
|
||||
(command_text)
|
||||
(command_text)
|
||||
(command_text)
|
||||
(command_text)))))
|
||||
|
||||
================================================================================
|
||||
Nested Command with Arguments
|
||||
@ -68,8 +61,7 @@ Nested Command with Arguments
|
||||
(statement
|
||||
(expression
|
||||
(command
|
||||
(identifier)
|
||||
(command
|
||||
(identifier)
|
||||
(command_argument)
|
||||
(command_argument))))))
|
||||
(command_text)
|
||||
(command_text)
|
||||
(command_text)
|
||||
(command_text)))))
|
||||
|
42
tree-sitter-dust/corpus/pipe.txt
Normal file
42
tree-sitter-dust/corpus/pipe.txt
Normal 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)))))))
|
@ -23,6 +23,7 @@ module.exports = grammar({
|
||||
$.index_assignment,
|
||||
$.match,
|
||||
$.return,
|
||||
$.pipe,
|
||||
$.while,
|
||||
),
|
||||
optional(';'),
|
||||
@ -64,26 +65,32 @@ module.exports = grammar({
|
||||
),
|
||||
),
|
||||
|
||||
command: $ =>
|
||||
prec.right(
|
||||
pipe: $ =>
|
||||
prec(
|
||||
1,
|
||||
seq(
|
||||
'*',
|
||||
$.identifier,
|
||||
repeat(
|
||||
choice(
|
||||
$.command_argument,
|
||||
$.command,
|
||||
$.function_call,
|
||||
),
|
||||
'|',
|
||||
choice(
|
||||
$.command,
|
||||
$.pipe,
|
||||
$.function_call,
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
command_argument: $ =>
|
||||
choice(
|
||||
/\w+/,
|
||||
seq('-', /\w+/),
|
||||
seq('--', /\w+/),
|
||||
command: $ =>
|
||||
prec.right(
|
||||
seq(
|
||||
'*',
|
||||
repeat($.command_text),
|
||||
),
|
||||
),
|
||||
|
||||
command_text: $ => /\S+/,
|
||||
|
||||
block: $ =>
|
||||
seq(
|
||||
|
@ -58,6 +58,10 @@
|
||||
"type": "SYMBOL",
|
||||
"name": "return"
|
||||
},
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "pipe"
|
||||
},
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"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": {
|
||||
"type": "PREC_RIGHT",
|
||||
"value": 0,
|
||||
@ -188,63 +235,19 @@
|
||||
"type": "STRING",
|
||||
"value": "*"
|
||||
},
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "identifier"
|
||||
},
|
||||
{
|
||||
"type": "REPEAT",
|
||||
"content": {
|
||||
"type": "CHOICE",
|
||||
"members": [
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "command_argument"
|
||||
},
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "command"
|
||||
}
|
||||
]
|
||||
"name": "command_text"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"command_argument": {
|
||||
"type": "CHOICE",
|
||||
"members": [
|
||||
{
|
||||
"command_text": {
|
||||
"type": "PATTERN",
|
||||
"value": "\\w+"
|
||||
},
|
||||
{
|
||||
"type": "SEQ",
|
||||
"members": [
|
||||
{
|
||||
"type": "STRING",
|
||||
"value": "-"
|
||||
},
|
||||
{
|
||||
"type": "PATTERN",
|
||||
"value": "\\w+"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "SEQ",
|
||||
"members": [
|
||||
{
|
||||
"type": "STRING",
|
||||
"value": "--"
|
||||
},
|
||||
{
|
||||
"type": "PATTERN",
|
||||
"value": "\\w+"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
"value": "\\S+"
|
||||
},
|
||||
"block": {
|
||||
"type": "SEQ",
|
||||
|
@ -62,28 +62,15 @@
|
||||
"fields": {},
|
||||
"children": {
|
||||
"multiple": true,
|
||||
"required": true,
|
||||
"required": false,
|
||||
"types": [
|
||||
{
|
||||
"type": "command",
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "command_argument",
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "identifier",
|
||||
"type": "command_text",
|
||||
"named": true
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "command_argument",
|
||||
"named": true,
|
||||
"fields": {}
|
||||
},
|
||||
{
|
||||
"type": "else",
|
||||
"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",
|
||||
"named": true,
|
||||
@ -596,6 +606,10 @@
|
||||
"type": "match",
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "pipe",
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "return",
|
||||
"named": true
|
||||
@ -797,10 +811,6 @@
|
||||
"type": "-",
|
||||
"named": false
|
||||
},
|
||||
{
|
||||
"type": "--",
|
||||
"named": false
|
||||
},
|
||||
{
|
||||
"type": "-=",
|
||||
"named": false
|
||||
@ -889,6 +899,10 @@
|
||||
"type": "collection",
|
||||
"named": false
|
||||
},
|
||||
{
|
||||
"type": "command_text",
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "else",
|
||||
"named": false
|
||||
@ -907,11 +921,11 @@
|
||||
},
|
||||
{
|
||||
"type": "float",
|
||||
"named": true
|
||||
"named": false
|
||||
},
|
||||
{
|
||||
"type": "float",
|
||||
"named": false
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "for",
|
||||
@ -995,11 +1009,11 @@
|
||||
},
|
||||
{
|
||||
"type": "string",
|
||||
"named": true
|
||||
"named": false
|
||||
},
|
||||
{
|
||||
"type": "string",
|
||||
"named": false
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "struct",
|
||||
@ -1017,6 +1031,10 @@
|
||||
"type": "{",
|
||||
"named": false
|
||||
},
|
||||
{
|
||||
"type": "|",
|
||||
"named": false
|
||||
},
|
||||
{
|
||||
"type": "||",
|
||||
"named": false
|
||||
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user