diff options
Diffstat (limited to 'userland/minibox/utilities/sh/lexer.c')
-rw-r--r-- | userland/minibox/utilities/sh/lexer.c | 54 |
1 files changed, 22 insertions, 32 deletions
diff --git a/userland/minibox/utilities/sh/lexer.c b/userland/minibox/utilities/sh/lexer.c index 72011e6..4ad2389 100644 --- a/userland/minibox/utilities/sh/lexer.c +++ b/userland/minibox/utilities/sh/lexer.c @@ -1,6 +1,6 @@ +#include "lexer.h" #include <assert.h> #include <ctype.h> -#include "lexer.h" #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -14,43 +14,34 @@ void free_tokens(struct TOKEN *token) { } } -int is_nonspecial_char(char c) { +int is_special_char(char c) { if (!isprint(c)) - return 0; + return 1; if (isspace(c)) - return 0; - if (isalnum(c)) return 1; - return ('>' != c && '|' != c && '&' != c); + if (isalnum(c)) + return 0; + return !(('>' != c && '|' != c && '&' != c)); } -int parse_chars(const char **code_ptr, struct TOKEN *cur) { - const char *code = *code_ptr; - if (!is_nonspecial_char(*code)) +int parse_chars(struct sv *code_ptr, struct TOKEN *cur) { + struct sv code = *code_ptr; + if (is_special_char(sv_peek(code))) return 0; cur->type = TOKEN_CHARS; - int i = 0; - for (; *code; code++, i++) { - if (!is_nonspecial_char(*code)) { - break; - } - assert(i < 256); - cur->string_rep[i] = *code; - } - cur->string_rep[i] = '\0'; + cur->string_rep = sv_split_function(code, &code, is_special_char); *code_ptr = code; return 1; } // Operands such as: &, &&, |, || etc // Is operands the right word? -int parse_operand(const char **code_ptr, struct TOKEN *cur) { - const char *code = *code_ptr; +int parse_operand(struct sv *code_ptr, struct TOKEN *cur) { + struct sv code = *code_ptr; #define TRY_PARSE_STRING(_s, _token) \ - if (0 == strncmp(code, _s, strlen(_s))) { \ - cur->type = _token; \ - strcpy(cur->string_rep, _s); \ - code += strlen(_s); \ + if (sv_partial_eq(code, C_TO_SV(_s))) { \ + cur->type = TOKEN_AND; \ + cur->string_rep = sv_take(code, &code, strlen(_s)); \ goto complete_return; \ } TRY_PARSE_STRING("&&", TOKEN_AND); @@ -68,20 +59,19 @@ complete_return: return 1; } -void skip_whitespace(const char **code_ptr) { - const char *code = *code_ptr; - for (; isspace(*code); code++) - ; - *code_ptr = code; +void skip_whitespace(struct sv *s) { + *s = sv_skip_chars(*s, " \t\n\r"); } -struct TOKEN *lex(const char *code) { +struct TOKEN *lex(struct sv code) { struct TOKEN *head = NULL; struct TOKEN *prev = NULL; - for (; *code;) { + for (; !sv_isempty(code);) { skip_whitespace(&code); - if (!*code) + if (sv_isempty(code)) { break; + } + struct TOKEN *cur = malloc(sizeof(struct TOKEN)); cur->next = NULL; if (prev) |