more work on the parser, still misbehaving, needs more testing and refactoring later
This commit is contained in:
@@ -0,0 +1,137 @@
|
|||||||
|
type ParserError = UnclosedQuoteError;
|
||||||
|
|
||||||
|
interface UnclosedQuoteError {
|
||||||
|
type: "UnclosedQuote";
|
||||||
|
message: string;
|
||||||
|
timestamp: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ParsedCommand {
|
||||||
|
command: string;
|
||||||
|
flags: string[];
|
||||||
|
args: string[];
|
||||||
|
}
|
||||||
|
|
||||||
|
function tokenize(input: string): string[] {
|
||||||
|
enum State {
|
||||||
|
Normal,
|
||||||
|
SingleQuote,
|
||||||
|
DoubleQuote,
|
||||||
|
Escape,
|
||||||
|
}
|
||||||
|
|
||||||
|
const tokens: string[] = [];
|
||||||
|
let buffer: string[] = [];
|
||||||
|
let state = State.Normal;
|
||||||
|
let escapeReturnState = State.Normal;
|
||||||
|
|
||||||
|
function flushBuffer() {
|
||||||
|
if (buffer.length > 0) {
|
||||||
|
tokens.push(buffer.join(""));
|
||||||
|
buffer.length = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const char of input) {
|
||||||
|
switch (state) {
|
||||||
|
case State.Normal:
|
||||||
|
if (char === '"') {
|
||||||
|
flushBuffer();
|
||||||
|
state = State.DoubleQuote;
|
||||||
|
} else if (char === "'") {
|
||||||
|
flushBuffer();
|
||||||
|
state = State.SingleQuote;
|
||||||
|
} else if (char === "\\") {
|
||||||
|
escapeReturnState = State.Normal;
|
||||||
|
state = State.Escape;
|
||||||
|
} else if (char === " ") {
|
||||||
|
flushBuffer();
|
||||||
|
} else {
|
||||||
|
buffer.push(char);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case State.SingleQuote:
|
||||||
|
if (char === "'") {
|
||||||
|
flushBuffer();
|
||||||
|
state = State.Normal;
|
||||||
|
} else {
|
||||||
|
buffer.push(char);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case State.DoubleQuote:
|
||||||
|
if (char === '"') {
|
||||||
|
flushBuffer();
|
||||||
|
state = State.Normal;
|
||||||
|
} else if (char === "\\") {
|
||||||
|
escapeReturnState = State.DoubleQuote;
|
||||||
|
state = State.Escape;
|
||||||
|
} else {
|
||||||
|
buffer.push(char);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case State.Escape:
|
||||||
|
if (escapeReturnState === State.DoubleQuote) {
|
||||||
|
if (char === "$" || char === "`" || char === '"' || char === "\\" || char === "\n") {
|
||||||
|
if (char !== "\n") {
|
||||||
|
buffer.push(char);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
buffer.push("\\");
|
||||||
|
buffer.push(char);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
buffer.push(char);
|
||||||
|
}
|
||||||
|
state = escapeReturnState;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (state === State.DoubleQuote || state === State.SingleQuote) {
|
||||||
|
throw {
|
||||||
|
type: "UnclosedQuote",
|
||||||
|
message: `Tokenizer returned with an unclosed quote`,
|
||||||
|
timestamp: Date.now(),
|
||||||
|
} satisfies UnclosedQuoteError;
|
||||||
|
}
|
||||||
|
|
||||||
|
flushBuffer();
|
||||||
|
return tokens;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function parseCommand(input: string): ParsedCommand {
|
||||||
|
const tokens = tokenize(input);
|
||||||
|
const command = tokens[0] ?? "";
|
||||||
|
const flags: string[] = [];
|
||||||
|
const args: string[] = [];
|
||||||
|
|
||||||
|
let pastEndOfOptions = false;
|
||||||
|
|
||||||
|
tokens.shift();
|
||||||
|
|
||||||
|
for (const token of tokens) {
|
||||||
|
if (token === "--") pastEndOfOptions = true;
|
||||||
|
|
||||||
|
if (pastEndOfOptions) {
|
||||||
|
args.push(token);
|
||||||
|
} else {
|
||||||
|
if (token.startsWith("--")) {
|
||||||
|
flags.push(token.slice(2));
|
||||||
|
} else if (token.startsWith("-")) {
|
||||||
|
const newToken = token.slice(1);
|
||||||
|
const chars = newToken.split("");
|
||||||
|
for (const flag of chars) {
|
||||||
|
flags.push(flag);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
args.push(token);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
command,
|
||||||
|
flags,
|
||||||
|
args,
|
||||||
|
};
|
||||||
|
}
|
||||||
+21
-38
@@ -1,42 +1,25 @@
|
|||||||
|
import { parseCommand } from "@core/util/parser";
|
||||||
|
|
||||||
const message: string = "Hello, World!";
|
const message: string = "Hello, World!";
|
||||||
console.log(message);
|
console.log(message);
|
||||||
|
|
||||||
function parse(input: string) {
|
// prettier-ignore
|
||||||
let buffer: string[] = [];
|
{
|
||||||
const tokens: string[] = [];
|
//console.log(parseCommand('ls'))
|
||||||
let state: string = "";
|
//console.log(parseCommand('ls /tmp'))
|
||||||
let escape = false;
|
//console.log(parseCommand('ls -l'))
|
||||||
|
//console.log(parseCommand('ls -la'))
|
||||||
for (const char of input) {
|
//console.log(parseCommand('ls -la /tmp'))
|
||||||
if (state.length === 0) {
|
//console.log(parseCommand('ls -l -a'))
|
||||||
if (char === " ") {
|
//console.log(parseCommand('grep --color=auto'))
|
||||||
tokens.push(buffer.join(""));
|
//console.log(parseCommand('grep --color=auto -r'))
|
||||||
buffer = [];
|
console.log(parseCommand('echo -- --version'))
|
||||||
continue;
|
console.log(parseCommand('echo -- -f'))
|
||||||
}
|
console.log(parseCommand('echo --'))
|
||||||
if (char === "'" || char === '"') {
|
console.log(parseCommand("echo 'hello world'"))
|
||||||
state = char;
|
console.log(parseCommand(`echo \\"hello world\\"`))
|
||||||
continue;
|
console.log(parseCommand(`echo 'it\\'s a test'`))
|
||||||
}
|
console.log(parseCommand("echo \"it's a \\\"test\\\"\""))
|
||||||
buffer.push(char);
|
console.log(parseCommand("ls -la -- --tmp"))
|
||||||
continue;
|
console.log(parseCommand("rm -rf --no-preserve-root /"))
|
||||||
} 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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(parse('"Hello World"'));
|
|
||||||
|
|||||||
Reference in New Issue
Block a user