aboutsummaryrefslogtreecommitdiffstats
path: root/include/lexer.h
diff options
context:
space:
mode:
authorDouglas B. Rumbaugh <doug@douglasrumbaugh.com>2025-10-31 23:41:32 -0400
committerDouglas B. Rumbaugh <doug@douglasrumbaugh.com>2025-10-31 23:41:32 -0400
commit06a02a3a50baf261a0f1c998bfd02269c3ed45de (patch)
tree00aa66e09a31b2563221c385e5ac129a57082729 /include/lexer.h
downloadhush-06a02a3a50baf261a0f1c998bfd02269c3ed45de.tar.gz
Initial commit
Diffstat (limited to 'include/lexer.h')
-rw-r--r--include/lexer.h75
1 files changed, 75 insertions, 0 deletions
diff --git a/include/lexer.h b/include/lexer.h
new file mode 100644
index 0000000..bec44a9
--- /dev/null
+++ b/include/lexer.h
@@ -0,0 +1,75 @@
+/*
+ * Header file for the HUSH lexical analysis
+ * module
+ */
+#ifndef H_HUSH_LEXER
+#define H_HUSH_LEXER
+
+#include <ctype.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdbool.h>
+#include <stdio.h>
+
+#include "config.h"
+
+enum token_type {
+ TKN_COMMAND,
+ TKN_OUT_REDIR,
+ TKN_IN_REDIR,
+ TKN_PIPE,
+ TKN_FILENAME,
+ TKN_ARG,
+ TKN_INVALID,
+ TKN_VARKEY,
+ TKN_VARVAL
+};
+
+typedef struct token {
+ char *text;
+ enum token_type type;
+ struct token *next;
+} token;
+
+/*
+ * Accepts a null-terminate string representing
+ * a full command and returns the pointer to the
+ * head of a list of parsed tokens. The provided
+ * string must have been trimmed to remove all
+ * leading and trailing whitespace characters.
+ *
+ * The tokens contain pointers into the provided
+ * command string, and so the parsed command is
+ * only valid so long as the provided string has
+ * not been altered.
+ *
+ * This function's behavior is undefined if the
+ * input string is *not* null-terminated, or if
+ * it has leading or trailing whitespace.
+ */
+
+token *parse_command(char *cmdstr);
+
+/*
+ * Accepts the pointer to the head of a list
+ * of parsed tokens and validates that the
+ * tokens are syntactically valid. If the
+ * sequence is valid, NULL will be returned.
+ * If the sequence is invalid, a pointer to
+ * the first invalid token will be returned.
+ */
+token *validate_command(token *cmd);
+
+/*
+ * Removes all leading and trailing whitespace from
+ * the provided string, and returns a pointer to the
+ * beginning of the trimmed string. Necessary as the
+ * first stage of parsing.
+ */
+char *trim(char *str);
+
+void print_parsed_command(FILE *file, token *cmd);
+
+void destroy_tokens(token *tokens);
+
+#endif