From a5f3127bcf2fe1151e4f354bcfd1ab201fc35bcf Mon Sep 17 00:00:00 2001 From: Jeff Date: Sun, 18 Feb 2024 11:38:35 -0500 Subject: [PATCH] Fix command tests and parsing --- src/abstract_tree/command.rs | 9 +++-- tests/commands.rs | 12 ++---- tree-sitter-dust/corpus/commands.txt | 59 +--------------------------- tree-sitter-dust/grammar.js | 2 +- tree-sitter-dust/src/grammar.json | 2 +- tree-sitter-dust/src/parser.c | 15 ++++--- 6 files changed, 22 insertions(+), 77 deletions(-) diff --git a/src/abstract_tree/command.rs b/src/abstract_tree/command.rs index 98c8034..78b68f2 100644 --- a/src/abstract_tree/command.rs +++ b/src/abstract_tree/command.rs @@ -1,4 +1,4 @@ -use std::process; +use std::process::{self, Stdio}; use serde::{Deserialize, Serialize}; use tree_sitter::Node as SyntaxNode; @@ -53,18 +53,19 @@ impl AbstractTree for Command { } fn validate(&self, _source: &str, _context: &Context) -> Result<(), ValidationError> { - todo!() + Ok(()) } fn run(&self, _source: &str, _context: &Context) -> Result { let output = process::Command::new(&self.command_text) .args(&self.command_arguments) + .stdout(Stdio::piped()) + .stderr(Stdio::inherit()) .spawn()? .wait_with_output()? .stdout; - let string = String::from_utf8(output)?; - Ok(Value::String(string)) + Ok(Value::String(String::from_utf8(output)?)) } } diff --git a/tests/commands.rs b/tests/commands.rs index b6a4bbf..98a462c 100644 --- a/tests/commands.rs +++ b/tests/commands.rs @@ -1,20 +1,14 @@ use dust_lang::{interpret, Value}; -use std::fs::{remove_file, write}; - #[test] fn simple_command() { - assert_eq!(interpret("^echo hi"), Ok(Value::String("".to_string()))) + assert_eq!(interpret("^echo hi"), Ok(Value::String("hi\n".to_string()))) } #[test] fn assign_command_output() { - write("target/test.txt", "123").unwrap(); - assert_eq!( - interpret("x = ^cat target/test.txt; x"), - Ok(Value::String("123".to_string())) + interpret("x = ^ls; length(str:lines(x))"), + Ok(Value::Integer(11)) ); - - remove_file("target/test.txt").unwrap(); } diff --git a/tree-sitter-dust/corpus/commands.txt b/tree-sitter-dust/corpus/commands.txt index d7da01f..eaa2e9e 100644 --- a/tree-sitter-dust/corpus/commands.txt +++ b/tree-sitter-dust/corpus/commands.txt @@ -59,33 +59,6 @@ Command Sequence with Arguments -------------------------------------------------------------------------------- -(root - (statement - (statement_kind - (expression - (command - (command_text) - (command_argument) - (command_argument) - (command_argument) - (command_argument))))) - (statement - (statement_kind - (expression - (command - (command_text) - (command_argument) - (command_argument)))))) - -================================================================================ -Command Sequence with Arguments -================================================================================ - -^cargo run -- -c "output('hi there')" -^ls --long -a - --------------------------------------------------------------------------------- - (root (statement (statement_kind @@ -108,7 +81,7 @@ Command Sequence with Arguments Command Assignment ================================================================================ -ls_output = ^ls --long -a; +ls_output = ^ls; cat_output = ^cat Cargo.toml; -------------------------------------------------------------------------------- @@ -123,9 +96,7 @@ cat_output = ^cat Cargo.toml; (statement_kind (expression (command - (command_text) - (command_argument) - (command_argument)))))))) + (command_text)))))))) (statement (statement_kind (assignment @@ -146,32 +117,6 @@ ls_output = ^ls --long -a; ls_output -------------------------------------------------------------------------------- -(root - (statement - (statement_kind - (assignment - (identifier) - (assignment_operator) - (statement - (statement_kind - (expression - (command - (command_text) - (command_argument) - (command_argument)))))))) - (statement - (statement_kind - (expression - (identifier))))) - -================================================================================ -Command with Semicolon -================================================================================ - -ls_output = ^ls --long -a; ls_output - --------------------------------------------------------------------------------- - (root (statement (statement_kind diff --git a/tree-sitter-dust/grammar.js b/tree-sitter-dust/grammar.js index 3b5c906..093986a 100644 --- a/tree-sitter-dust/grammar.js +++ b/tree-sitter-dust/grammar.js @@ -101,7 +101,7 @@ module.exports = grammar({ ), ), - command_text: $ => /\S+/, + command_text: $ => /[^\s;]+/, command_argument: $ => choice( diff --git a/tree-sitter-dust/src/grammar.json b/tree-sitter-dust/src/grammar.json index 27e0339..6953c39 100644 --- a/tree-sitter-dust/src/grammar.json +++ b/tree-sitter-dust/src/grammar.json @@ -293,7 +293,7 @@ }, "command_text": { "type": "PATTERN", - "value": "\\S+" + "value": "[^\\s;]+" }, "command_argument": { "type": "CHOICE", diff --git a/tree-sitter-dust/src/parser.c b/tree-sitter-dust/src/parser.c index d1cb36b..18f39e3 100644 --- a/tree-sitter-dust/src/parser.c +++ b/tree-sitter-dust/src/parser.c @@ -2320,7 +2320,8 @@ static bool ts_lex(TSLexer *lexer, TSStateId state) { lookahead == '\n' || lookahead == '\r' || lookahead == ' ') SKIP(22) - if (lookahead != 0) ADVANCE(76); + if (lookahead != 0 && + lookahead != ';') ADVANCE(76); END_STATE(); case 23: if (lookahead == '&') ADVANCE(154); @@ -2558,7 +2559,8 @@ static bool ts_lex(TSLexer *lexer, TSStateId state) { if (lookahead == '|') ADVANCE(51); if (lookahead == '\t' || lookahead == '\r' || - lookahead == ' ') ADVANCE(41); + lookahead == ' ' || + lookahead == ';') ADVANCE(41); if (lookahead != 0) ADVANCE(75); END_STATE(); case 52: @@ -2585,7 +2587,8 @@ static bool ts_lex(TSLexer *lexer, TSStateId state) { lookahead != '\t' && lookahead != '\n' && lookahead != '\r' && - lookahead != ' ') ADVANCE(76); + lookahead != ' ' && + lookahead != ';') ADVANCE(76); END_STATE(); case 55: ACCEPT_TOKEN(anon_sym_SEMI); @@ -2736,7 +2739,8 @@ static bool ts_lex(TSLexer *lexer, TSStateId state) { if (lookahead == '|') ADVANCE(51); if (lookahead == '\t' || lookahead == '\r' || - lookahead == ' ') ADVANCE(41); + lookahead == ' ' || + lookahead == ';') ADVANCE(41); if (lookahead != 0) ADVANCE(75); END_STATE(); case 76: @@ -2745,7 +2749,8 @@ static bool ts_lex(TSLexer *lexer, TSStateId state) { lookahead != '\t' && lookahead != '\n' && lookahead != '\r' && - lookahead != ' ') ADVANCE(76); + lookahead != ' ' && + lookahead != ';') ADVANCE(76); END_STATE(); case 77: ACCEPT_TOKEN(aux_sym_command_argument_token1);