diff options
Diffstat (limited to '')
-rw-r--r-- | test/drive_shlexer.cc | 61 |
1 files changed, 41 insertions, 20 deletions
diff --git a/test/drive_shlexer.cc b/test/drive_shlexer.cc index fe6f15f..ffbfc32 100644 --- a/test/drive_shlexer.cc +++ b/test/drive_shlexer.cc @@ -35,7 +35,7 @@ using namespace std; const char* ST_TOKEN_NAMES[] = { - "err", + "eof", "wsp", "esc", "dst", @@ -47,6 +47,22 @@ const char* ST_TOKEN_NAMES[] = { "til", }; +static void +put_underline(FILE* file, string_fragment frag) +{ + for (int lpc = 0; lpc < frag.sf_end; lpc++) { + if (lpc == frag.sf_begin) { + fputc('^', stdout); + } else if (lpc == (frag.sf_end - 1)) { + fputc('^', stdout); + } else if (lpc > frag.sf_begin) { + fputc('-', stdout); + } else { + fputc(' ', stdout); + } + } +} + int main(int argc, char* argv[]) { @@ -56,39 +72,44 @@ main(int argc, char* argv[]) } shlex lexer(argv[1], strlen(argv[1])); - string_fragment cap; - shlex_token_t token; + bool done = false; printf(" %s\n", argv[1]); - while (lexer.tokenize(cap, token)) { - int lpc; + while (!done) { + auto tokenize_res = lexer.tokenize(); + if (tokenize_res.isErr()) { + auto te = tokenize_res.unwrapErr(); + + printf("err "); + put_underline(stdout, te.te_source); + printf(" -- %s\n", te.te_msg); + break; + } - printf("%s ", ST_TOKEN_NAMES[(int) token]); - for (lpc = 0; lpc < cap.sf_end; lpc++) { - if (lpc == cap.sf_begin) { - fputc('^', stdout); - } else if (lpc == (cap.sf_end - 1)) { - fputc('^', stdout); - } else if (lpc > cap.sf_begin) { - fputc('-', stdout); - } else { - fputc(' ', stdout); - } + auto tr = tokenize_res.unwrap(); + if (tr.tr_token == shlex_token_t::eof) { + done = true; } + printf("%s ", ST_TOKEN_NAMES[(int) tr.tr_token]); + put_underline(stdout, tr.tr_frag); printf("\n"); } lexer.reset(); std::string result; - if (lexer.eval(result, map<string, string>())) { + std::map<std::string, scoped_value_t> vars; + if (lexer.eval(result, scoped_resolver{&vars})) { printf("eval -- %s\n", result.c_str()); } lexer.reset(); - std::vector<std::string> sresult; - if (lexer.split(sresult, map<string, string>())) { + auto split_res = lexer.split(scoped_resolver{&vars}); + if (split_res.isOk()) { + auto sresult = split_res.unwrap(); printf("split:\n"); for (size_t lpc = 0; lpc < sresult.size(); lpc++) { - printf(" %zu -- %s\n", lpc, sresult[lpc].c_str()); + printf("% 3zu ", lpc); + put_underline(stdout, sresult[lpc].se_origin); + printf(" -- %s\n", sresult[lpc].se_value.c_str()); } } |