more work on the parser, still misbehaving, needs more testing and refactoring later
This commit is contained in:
+21
-38
@@ -1,42 +1,25 @@
|
||||
import { parseCommand } from "@core/util/parser";
|
||||
|
||||
const message: string = "Hello, World!";
|
||||
console.log(message);
|
||||
|
||||
function parse(input: string) {
|
||||
let buffer: string[] = [];
|
||||
const tokens: string[] = [];
|
||||
let state: string = "";
|
||||
let escape = false;
|
||||
|
||||
for (const char of input) {
|
||||
if (state.length === 0) {
|
||||
if (char === " ") {
|
||||
tokens.push(buffer.join(""));
|
||||
buffer = [];
|
||||
continue;
|
||||
}
|
||||
if (char === "'" || char === '"') {
|
||||
state = char;
|
||||
continue;
|
||||
}
|
||||
buffer.push(char);
|
||||
continue;
|
||||
} else {
|
||||
if (char === state) {
|
||||
state = "";
|
||||
tokens.push(buffer.join(""));
|
||||
buffer = [];
|
||||
continue;
|
||||
}
|
||||
if (char === "\\") {
|
||||
escape = true;
|
||||
continue;
|
||||
}
|
||||
buffer.push(char);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
tokens.push(buffer.join(""));
|
||||
return tokens;
|
||||
// prettier-ignore
|
||||
{
|
||||
//console.log(parseCommand('ls'))
|
||||
//console.log(parseCommand('ls /tmp'))
|
||||
//console.log(parseCommand('ls -l'))
|
||||
//console.log(parseCommand('ls -la'))
|
||||
//console.log(parseCommand('ls -la /tmp'))
|
||||
//console.log(parseCommand('ls -l -a'))
|
||||
//console.log(parseCommand('grep --color=auto'))
|
||||
//console.log(parseCommand('grep --color=auto -r'))
|
||||
console.log(parseCommand('echo -- --version'))
|
||||
console.log(parseCommand('echo -- -f'))
|
||||
console.log(parseCommand('echo --'))
|
||||
console.log(parseCommand("echo 'hello world'"))
|
||||
console.log(parseCommand(`echo \\"hello world\\"`))
|
||||
console.log(parseCommand(`echo 'it\\'s a test'`))
|
||||
console.log(parseCommand("echo \"it's a \\\"test\\\"\""))
|
||||
console.log(parseCommand("ls -la -- --tmp"))
|
||||
console.log(parseCommand("rm -rf --no-preserve-root /"))
|
||||
}
|
||||
|
||||
console.log(parse('"Hello World"'));
|
||||
|
||||
Reference in New Issue
Block a user