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 crate::{
|
||||
value_node::ValueNode, AbstractTree, Error, Format, FunctionCall, Identifier, Index, Logic,
|
||||
Map, Math, New, Result, SyntaxNode, Type, Value, Yield,
|
||||
value_node::ValueNode, AbstractTree, Command, Error, Format, FunctionCall, Identifier, Index,
|
||||
Logic, Map, Math, New, Result, SyntaxNode, Type, Value, Yield,
|
||||
};
|
||||
|
||||
/// Abstract representation of an expression statement.
|
||||
@ -20,6 +20,7 @@ pub enum Expression {
|
||||
FunctionCall(Box<FunctionCall>),
|
||||
Yield(Box<Yield>),
|
||||
New(New),
|
||||
Command(Command),
|
||||
}
|
||||
|
||||
impl AbstractTree for Expression {
|
||||
|
@ -10,6 +10,7 @@ pub mod assignment;
|
||||
pub mod assignment_operator;
|
||||
pub mod block;
|
||||
pub mod built_in_value;
|
||||
pub mod command;
|
||||
pub mod expression;
|
||||
pub mod r#for;
|
||||
pub mod function_call;
|
||||
@ -34,7 +35,7 @@ pub mod r#while;
|
||||
pub mod r#yield;
|
||||
|
||||
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::*,
|
||||
index::*, index_assignment::IndexAssignment, index_expression::*, logic::*, logic_operator::*,
|
||||
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"
|
||||
//! functions or by constructing your own Evaluator.
|
||||
//! You can use this library externally by calling either of the "interpret"
|
||||
//! functions or by constructing your own Interpreter.
|
||||
use tree_sitter::{Parser, Tree as TSTree, TreeCursor};
|
||||
|
||||
use crate::{language, AbstractTree, Error, Format, Map, Result, Root, SyntaxNode, Value};
|
||||
|
||||
/// Interpret the given source code.
|
||||
///
|
||||
/// Returns a vector of results from evaluating the source code. Each comment
|
||||
/// and statemtent will have its own result.
|
||||
/// Interpret the given source code. Returns the value of last statement or the
|
||||
/// first error encountered.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
@ -23,6 +21,10 @@ pub fn interpret(source: &str) -> Result<Value> {
|
||||
|
||||
/// 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
|
||||
///
|
||||
/// ```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
|
||||
//! line binary.
|
||||
//! The Dust library is used to parse, format and run dust source code.
|
||||
//!
|
||||
//! Using this library is simple and straightforward, see the [inferface] module for instructions on
|
||||
//! interpreting Dust code. Most of the language's features are implemented in the [tools] module.
|
||||
//! See the [interpret] module for more information.
|
||||
pub use crate::{
|
||||
abstract_tree::*, built_in_functions::BuiltInFunction, error::*, interpret::*, value::*,
|
||||
};
|
||||
@ -28,18 +26,6 @@ pub fn language() -> Language {
|
||||
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)]
|
||||
mod tests {
|
||||
#[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,
|
||||
$.yield,
|
||||
$.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: $ =>
|
||||
seq(
|
||||
optional('async'),
|
||||
|
@ -142,6 +142,10 @@
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"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": {
|
||||
"type": "SEQ",
|
||||
"members": [
|
||||
|
@ -56,6 +56,34 @@
|
||||
"named": true,
|
||||
"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",
|
||||
"named": true,
|
||||
@ -98,6 +126,10 @@
|
||||
"multiple": false,
|
||||
"required": true,
|
||||
"types": [
|
||||
{
|
||||
"type": "command",
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "function_call",
|
||||
"named": true
|
||||
@ -765,6 +797,10 @@
|
||||
"type": "-",
|
||||
"named": false
|
||||
},
|
||||
{
|
||||
"type": "--",
|
||||
"named": false
|
||||
},
|
||||
{
|
||||
"type": "-=",
|
||||
"named": false
|
||||
@ -871,11 +907,11 @@
|
||||
},
|
||||
{
|
||||
"type": "float",
|
||||
"named": false
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "float",
|
||||
"named": true
|
||||
"named": false
|
||||
},
|
||||
{
|
||||
"type": "for",
|
||||
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user