Add command syntax; Write docs
This commit is contained in:
parent
58bbbb749e
commit
fe1f007692
30
src/abstract_tree/command.rs
Normal file
30
src/abstract_tree/command.rs
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
use crate::{AbstractTree, Format, Identifier, Result};
|
||||||
|
|
||||||
|
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> {
|
||||||
|
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> {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Format for Command {
|
||||||
|
fn format(&self, output: &mut String, indent_level: u8) {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
}
|
@ -1,8 +1,8 @@
|
|||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
value_node::ValueNode, AbstractTree, Error, Format, FunctionCall, Identifier, Index, Logic,
|
value_node::ValueNode, AbstractTree, Command, Error, Format, FunctionCall, Identifier, Index,
|
||||||
Map, Math, New, Result, SyntaxNode, Type, Value, Yield,
|
Logic, Map, Math, New, Result, SyntaxNode, Type, Value, Yield,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Abstract representation of an expression statement.
|
/// Abstract representation of an expression statement.
|
||||||
@ -20,6 +20,7 @@ pub enum Expression {
|
|||||||
FunctionCall(Box<FunctionCall>),
|
FunctionCall(Box<FunctionCall>),
|
||||||
Yield(Box<Yield>),
|
Yield(Box<Yield>),
|
||||||
New(New),
|
New(New),
|
||||||
|
Command(Command),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AbstractTree for Expression {
|
impl AbstractTree for Expression {
|
||||||
|
@ -10,6 +10,7 @@ pub mod assignment;
|
|||||||
pub mod assignment_operator;
|
pub mod assignment_operator;
|
||||||
pub mod block;
|
pub mod block;
|
||||||
pub mod built_in_value;
|
pub mod built_in_value;
|
||||||
|
pub mod command;
|
||||||
pub mod expression;
|
pub mod expression;
|
||||||
pub mod r#for;
|
pub mod r#for;
|
||||||
pub mod function_call;
|
pub mod function_call;
|
||||||
@ -34,7 +35,7 @@ pub mod r#while;
|
|||||||
pub mod r#yield;
|
pub mod r#yield;
|
||||||
|
|
||||||
pub use {
|
pub use {
|
||||||
assignment::*, assignment_operator::*, block::*, built_in_value::*, expression::*,
|
assignment::*, assignment_operator::*, block::*, built_in_value::*, command::*, expression::*,
|
||||||
function_call::*, function_expression::*, function_node::*, identifier::*, if_else::*,
|
function_call::*, function_expression::*, function_node::*, identifier::*, if_else::*,
|
||||||
index::*, index_assignment::IndexAssignment, index_expression::*, logic::*, logic_operator::*,
|
index::*, index_assignment::IndexAssignment, index_expression::*, logic::*, logic_operator::*,
|
||||||
math::*, math_operator::*, new::*, r#for::*, r#match::*, r#type::*, r#while::*, r#yield::*,
|
math::*, math_operator::*, new::*, r#for::*, r#match::*, r#type::*, r#while::*, r#yield::*,
|
||||||
|
@ -1,15 +1,13 @@
|
|||||||
//! The top level of Dust's API with functions to interpret Dust code.
|
//! Tools to run and/or format dust source code.
|
||||||
//!
|
//!
|
||||||
//! You can use this library externally by calling either of the "eval"
|
//! You can use this library externally by calling either of the "interpret"
|
||||||
//! functions or by constructing your own Evaluator.
|
//! functions or by constructing your own Interpreter.
|
||||||
use tree_sitter::{Parser, Tree as TSTree, TreeCursor};
|
use tree_sitter::{Parser, Tree as TSTree, TreeCursor};
|
||||||
|
|
||||||
use crate::{language, AbstractTree, Error, Format, Map, Result, Root, SyntaxNode, Value};
|
use crate::{language, AbstractTree, Error, Format, Map, Result, Root, SyntaxNode, Value};
|
||||||
|
|
||||||
/// Interpret the given source code.
|
/// Interpret the given source code. Returns the value of last statement or the
|
||||||
///
|
/// first error encountered.
|
||||||
/// Returns a vector of results from evaluating the source code. Each comment
|
|
||||||
/// and statemtent will have its own result.
|
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
@ -23,6 +21,10 @@ pub fn interpret(source: &str) -> Result<Value> {
|
|||||||
|
|
||||||
/// Interpret the given source code with the given context.
|
/// Interpret the given source code with the given context.
|
||||||
///
|
///
|
||||||
|
/// 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
|
||||||
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
|
18
src/lib.rs
18
src/lib.rs
@ -1,8 +1,6 @@
|
|||||||
//! The Dust library is used to implement the Dust language, `src/main.rs` implements the command
|
//! The Dust library is used to parse, format and run dust source code.
|
||||||
//! line binary.
|
|
||||||
//!
|
//!
|
||||||
//! Using this library is simple and straightforward, see the [inferface] module for instructions on
|
//! See the [interpret] module for more information.
|
||||||
//! interpreting Dust code. Most of the language's features are implemented in the [tools] module.
|
|
||||||
pub use crate::{
|
pub use crate::{
|
||||||
abstract_tree::*, built_in_functions::BuiltInFunction, error::*, interpret::*, value::*,
|
abstract_tree::*, built_in_functions::BuiltInFunction, error::*, interpret::*, value::*,
|
||||||
};
|
};
|
||||||
@ -28,18 +26,6 @@ pub fn language() -> Language {
|
|||||||
unsafe { tree_sitter_dust() }
|
unsafe { tree_sitter_dust() }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The content of the [`node-types.json`][] file for this grammar.
|
|
||||||
///
|
|
||||||
/// [`node-types.json`]: https://tree-sitter.github.io/tree-sitter/using-parsers#static-node-types
|
|
||||||
pub const NODE_TYPES: &str = include_str!("../tree-sitter-dust/src/node-types.json");
|
|
||||||
|
|
||||||
// Uncomment these to include any queries that this grammar contains
|
|
||||||
|
|
||||||
// pub const HIGHLIGHTS_QUERY: &'static str = include_str!("../../queries/highlights.scm");
|
|
||||||
// pub const INJECTIONS_QUERY: &'static str = include_str!("../../queries/injections.scm");
|
|
||||||
// pub const LOCALS_QUERY: &'static str = include_str!("../../queries/locals.scm");
|
|
||||||
// pub const TAGS_QUERY: &'static str = include_str!("../../queries/tags.scm");
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
|
75
tree-sitter-dust/corpus/commands.txt
Normal file
75
tree-sitter-dust/corpus/commands.txt
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
================================================================================
|
||||||
|
Simple Command
|
||||||
|
================================================================================
|
||||||
|
|
||||||
|
*ls
|
||||||
|
|
||||||
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
(root
|
||||||
|
(statement
|
||||||
|
(expression
|
||||||
|
(command
|
||||||
|
(identifier)))))
|
||||||
|
|
||||||
|
================================================================================
|
||||||
|
Nested Command
|
||||||
|
================================================================================
|
||||||
|
|
||||||
|
*less *ls
|
||||||
|
|
||||||
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
(root
|
||||||
|
(statement
|
||||||
|
(expression
|
||||||
|
(command
|
||||||
|
(identifier)
|
||||||
|
(command
|
||||||
|
(identifier))))))
|
||||||
|
|
||||||
|
================================================================================
|
||||||
|
Command with Arguments
|
||||||
|
================================================================================
|
||||||
|
|
||||||
|
*ls --long -a;
|
||||||
|
(*git status)
|
||||||
|
*git log;
|
||||||
|
|
||||||
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
(root
|
||||||
|
(statement
|
||||||
|
(expression
|
||||||
|
(command
|
||||||
|
(identifier)
|
||||||
|
(command_argument)
|
||||||
|
(command_argument))))
|
||||||
|
(statement
|
||||||
|
(expression
|
||||||
|
(command
|
||||||
|
(identifier)
|
||||||
|
(command_argument))))
|
||||||
|
(statement
|
||||||
|
(expression
|
||||||
|
(command
|
||||||
|
(identifier)
|
||||||
|
(command_argument)))))
|
||||||
|
|
||||||
|
================================================================================
|
||||||
|
Nested Command with Arguments
|
||||||
|
================================================================================
|
||||||
|
|
||||||
|
*less *ls --long -a
|
||||||
|
|
||||||
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
(root
|
||||||
|
(statement
|
||||||
|
(expression
|
||||||
|
(command
|
||||||
|
(identifier)
|
||||||
|
(command
|
||||||
|
(identifier)
|
||||||
|
(command_argument)
|
||||||
|
(command_argument))))))
|
@ -50,6 +50,7 @@ module.exports = grammar({
|
|||||||
$.value,
|
$.value,
|
||||||
$.yield,
|
$.yield,
|
||||||
$.new,
|
$.new,
|
||||||
|
$.command,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
|
||||||
@ -63,6 +64,27 @@ module.exports = grammar({
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
|
||||||
|
command: $ =>
|
||||||
|
prec.right(
|
||||||
|
seq(
|
||||||
|
'*',
|
||||||
|
$.identifier,
|
||||||
|
repeat(
|
||||||
|
choice(
|
||||||
|
$.command_argument,
|
||||||
|
$.command,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
|
||||||
|
command_argument: $ =>
|
||||||
|
choice(
|
||||||
|
/\w+/,
|
||||||
|
seq('-', /\w+/),
|
||||||
|
seq('--', /\w+/),
|
||||||
|
),
|
||||||
|
|
||||||
block: $ =>
|
block: $ =>
|
||||||
seq(
|
seq(
|
||||||
optional('async'),
|
optional('async'),
|
||||||
|
@ -142,6 +142,10 @@
|
|||||||
{
|
{
|
||||||
"type": "SYMBOL",
|
"type": "SYMBOL",
|
||||||
"name": "new"
|
"name": "new"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "SYMBOL",
|
||||||
|
"name": "command"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
@ -174,6 +178,74 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"command": {
|
||||||
|
"type": "PREC_RIGHT",
|
||||||
|
"value": 0,
|
||||||
|
"content": {
|
||||||
|
"type": "SEQ",
|
||||||
|
"members": [
|
||||||
|
{
|
||||||
|
"type": "STRING",
|
||||||
|
"value": "*"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "SYMBOL",
|
||||||
|
"name": "identifier"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "REPEAT",
|
||||||
|
"content": {
|
||||||
|
"type": "CHOICE",
|
||||||
|
"members": [
|
||||||
|
{
|
||||||
|
"type": "SYMBOL",
|
||||||
|
"name": "command_argument"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "SYMBOL",
|
||||||
|
"name": "command"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"command_argument": {
|
||||||
|
"type": "CHOICE",
|
||||||
|
"members": [
|
||||||
|
{
|
||||||
|
"type": "PATTERN",
|
||||||
|
"value": "\\w+"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"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",
|
||||||
"members": [
|
"members": [
|
||||||
|
@ -56,6 +56,34 @@
|
|||||||
"named": true,
|
"named": true,
|
||||||
"fields": {}
|
"fields": {}
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"type": "command",
|
||||||
|
"named": true,
|
||||||
|
"fields": {},
|
||||||
|
"children": {
|
||||||
|
"multiple": true,
|
||||||
|
"required": true,
|
||||||
|
"types": [
|
||||||
|
{
|
||||||
|
"type": "command",
|
||||||
|
"named": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "command_argument",
|
||||||
|
"named": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "identifier",
|
||||||
|
"named": true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "command_argument",
|
||||||
|
"named": true,
|
||||||
|
"fields": {}
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"type": "else",
|
"type": "else",
|
||||||
"named": true,
|
"named": true,
|
||||||
@ -98,6 +126,10 @@
|
|||||||
"multiple": false,
|
"multiple": false,
|
||||||
"required": true,
|
"required": true,
|
||||||
"types": [
|
"types": [
|
||||||
|
{
|
||||||
|
"type": "command",
|
||||||
|
"named": true
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"type": "function_call",
|
"type": "function_call",
|
||||||
"named": true
|
"named": true
|
||||||
@ -765,6 +797,10 @@
|
|||||||
"type": "-",
|
"type": "-",
|
||||||
"named": false
|
"named": false
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"type": "--",
|
||||||
|
"named": false
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"type": "-=",
|
"type": "-=",
|
||||||
"named": false
|
"named": false
|
||||||
@ -871,11 +907,11 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"type": "float",
|
"type": "float",
|
||||||
"named": false
|
"named": true
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"type": "float",
|
"type": "float",
|
||||||
"named": true
|
"named": false
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"type": "for",
|
"type": "for",
|
||||||
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user