From e0783c234814b856a03815ffa50b6a71f6d21a32 Mon Sep 17 00:00:00 2001 From: Lephenixnoir Date: Mon, 18 Apr 2022 11:11:01 +0100 Subject: [PATCH] shell: exhaust parser after parsing error --- shell/e.cpp | 4 +++- shell/m.cpp | 3 ++- shell/parser.cpp | 21 +++++++++++++-------- shell/parser.h | 2 ++ 4 files changed, 20 insertions(+), 10 deletions(-) diff --git a/shell/e.cpp b/shell/e.cpp index c1aea82..337d870 100644 --- a/shell/e.cpp +++ b/shell/e.cpp @@ -35,8 +35,10 @@ static _e_args parse_e(Session &session, Parser &parser) if(space) while(!parser.at_end()) args.values.push_back(parser.expr(space)); - else + else { FxOS_log(ERR, "virtual space '%s' does not exist", args.space_name); + parser.exhaust(); + } // TODO: Error message when session specified in _e does not exist diff --git a/shell/m.cpp b/shell/m.cpp index 577736c..a45ef6f 100644 --- a/shell/m.cpp +++ b/shell/m.cpp @@ -12,7 +12,8 @@ static FxOS::Symbol parse_ms(Session &session, Parser &parser) if(parser.lookahead().type == T::SYSCALL) { s.type = FxOS::Symbol::Syscall; s.value = parser.expect(T::SYSCALL).value.NUM; - } else { + } + else { s.type = FxOS::Symbol::Address; s.value = parser.expr(session.current_space); } diff --git a/shell/parser.cpp b/shell/parser.cpp index 48732aa..c5f7319 100644 --- a/shell/parser.cpp +++ b/shell/parser.cpp @@ -2,6 +2,7 @@ #include "errors.h" #include #include +#include //--- // Lexing tools @@ -99,10 +100,6 @@ Token Parser::lookahead() const bool Parser::at_end() const { - /* When parsing to complete we try to go infinitely far, so we ignore - T::END. We supply T::SEPARATOR to complete the compound commands */ - if(m_complete) - return (m_la.type == T::SEPARATOR); return (m_la.type == T::SEPARATOR || m_la.type == T::END); } @@ -113,6 +110,12 @@ void Parser::end() throw SyntaxError("expected end of command"); } +void Parser::exhaust() +{ + while(!at_end()) + feed(); +} + void Parser::skip_separators() { while(m_la.type == T::SEPARATOR) @@ -282,11 +285,13 @@ void Parser::accept_options() std::string name = opt.substr(0, opt.find('=')); if(!m_options.count(name)) { - throw CommandError("unrecognized option {}", name); + if(!m_complete) + FxOS_log(ERR, "unrecognized option %s", name); + } + else { + std::string value = opt.substr(opt.find('=') + 1); + m_options[name](value); } - - std::string value = opt.substr(opt.find('=') + 1); - m_options[name](value); } } diff --git a/shell/parser.h b/shell/parser.h index 4f5a630..b77c0ba 100644 --- a/shell/parser.h +++ b/shell/parser.h @@ -115,6 +115,8 @@ public: Token lookahead() const; /* Require that command is finished */ void end(); + /* Exhaust all input (usually after encountering an error) */ + void exhaust(); /* Expect a token of this type, or of any of the listed types */ Token expect(T type, bool ignore_spaces=true);