From 371ce08ef692433928ba3e8b814ea35464131a7f Mon Sep 17 00:00:00 2001 From: Kelvin Sherlock Date: Thu, 4 Feb 2016 21:57:17 -0500 Subject: [PATCH] continuation prompt. --- lempar.cxx | 2 ++ mpw-shell.cpp | 8 +++++--- phase2-parser.lemon | 16 +++++++++++++++- phase2.h | 5 +++++ 4 files changed, 27 insertions(+), 4 deletions(-) diff --git a/lempar.cxx b/lempar.cxx index 1ddc3a8..f70e47d 100644 --- a/lempar.cxx +++ b/lempar.cxx @@ -335,6 +335,8 @@ class yypParser : public LEMON_SUPER { } #endif + const yyStackEntry *begin() const { return &yystack[0]; } + const yyStackEntry *end() const { return &yystack[yyidx > 0 ? yyidx + 1: 0]; } protected: private: diff --git a/mpw-shell.cpp b/mpw-shell.cpp index c6a8b0f..a311336 100644 --- a/mpw-shell.cpp +++ b/mpw-shell.cpp @@ -96,7 +96,7 @@ int read_fd(phase1 &p, int fd) { return 0; } -int interactive(phase1 &p) { +int interactive(phase1 &p, phase2& p2) { std::string history_file = root(); history_file += ".history"; @@ -104,7 +104,9 @@ int interactive(phase1 &p) { for(;;) { - char *cp = readline("# "); + const char *prompt = "# "; + if (p2.continuation()) prompt = "> "; + char *cp = readline(prompt); if (!cp) break; std::string s(cp); @@ -233,7 +235,7 @@ int main(int argc, char **argv) { } if (isatty(STDIN_FILENO)) - interactive(p1); + interactive(p1, p2); else read_fd(p1, STDIN_FILENO); p2.finish(); diff --git a/phase2-parser.lemon b/phase2-parser.lemon index 1dc1eba..ae3668a 100644 --- a/phase2-parser.lemon +++ b/phase2-parser.lemon @@ -9,7 +9,7 @@ #include "phase2.h" #include "command.h" #define LEMON_SUPER phase2_parser - +#include "phase2-parser.h" } %code { @@ -17,6 +17,20 @@ std::unique_ptr phase2_parser::make() { return std::make_unique(); } + +bool phase2_parser::continuation() const { + yypParser *self = (yypParser *)this; + + for (const auto &e : *self) { + if (e.major == BEGIN) return true; + if (e.major == LPAREN) return true; + if (e.major == IF) return true; + if (e.major == AMP_AMP) return true; + if (e.major == PIPE_PIPE) return true; + } + return false; +} + } %left PIPE_PIPE AMP_AMP. diff --git a/phase2.h b/phase2.h index e33f18e..4eb049d 100644 --- a/phase2.h +++ b/phase2.h @@ -22,6 +22,7 @@ public: virtual void parse_accept() override final; virtual void parse_failure() override final; + bool continuation() const; private: friend class phase2; @@ -65,6 +66,10 @@ public: return *this; } + bool continuation() const { + return parser ? parser->continuation() : false; + } + private: void parse(int, std::string &&);