1
0

Fix command tests and parsing

This commit is contained in:
Jeff 2024-02-18 11:38:35 -05:00
parent 01bdaa308d
commit a5f3127bcf
6 changed files with 22 additions and 77 deletions

View File

@ -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<Value, RuntimeError> {
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)?))
}
}

View File

@ -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();
}

View File

@ -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

View File

@ -101,7 +101,7 @@ module.exports = grammar({
),
),
command_text: $ => /\S+/,
command_text: $ => /[^\s;]+/,
command_argument: $ =>
choice(

View File

@ -293,7 +293,7 @@
},
"command_text": {
"type": "PATTERN",
"value": "\\S+"
"value": "[^\\s;]+"
},
"command_argument": {
"type": "CHOICE",

View File

@ -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);