mpw-shell/phase3.lemon

238 lines
5.4 KiB
Plaintext
Raw Normal View History

2016-01-30 17:44:42 +00:00
/*
>, < redirection is handled later, after environment expansion.
(also, redirection can be in the middle of a command.)
*/
%include {
#include "phase3_parser.h"
2016-01-30 17:44:42 +00:00
#include "command.h"
#define LEMON_SUPER phase3
2016-01-30 17:44:42 +00:00
}
2016-01-30 17:44:42 +00:00
%code {
std::unique_ptr<phase3> phase3::make() {
2016-01-30 17:44:42 +00:00
return std::make_unique<yypParser>();
}
2016-02-05 02:57:17 +00:00
bool phase3::continuation() const {
2016-02-05 02:57:17 +00:00
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;
if (e.major == LOOP) return true;
2016-06-16 04:04:29 +00:00
if (e.major == FOR) return true;
2016-07-26 18:20:11 +00:00
if (e.major == PIPE) return true;
if (e.major == PIPE_PIPE) return true;
if (e.major == AMP_AMP) return true;
2016-02-05 02:57:17 +00:00
}
return false;
}
void phase3::parse_accept() {
error = false;
}
void phase3::parse_failure() {
error = false;
}
void phase3::syntax_error(int yymajor, std::string &yyminor) {
/*
switch (yymajor) {
case END:
fprintf(stderr, "### MPW Shell - Extra END command.\n");
break;
case RPAREN:
fprintf(stderr, "### MPW Shell - Extra ) command.\n");
break;
case ELSE:
case ELSE_IF:
fprintf(stderr, "### MPW Shell - ELSE must be within IF ... END.\n");
break;
default:
fprintf(stderr, "### Parse error near %s\n", yyminor.c_str());
break;
}
*/
fprintf(stderr, "### MPW Shell - Parse error near %s\n", yymajor ? yyminor.c_str() : "EOF");
error = true;
}
2016-01-30 17:44:42 +00:00
}
%left PIPE_PIPE AMP_AMP.
%left PIPE.
%token_type {std::string}
%default_type {command_ptr}
%type start {void}
%type command_list {void}
/* these are put into a queue for immmediate execution */
2016-02-05 02:45:04 +00:00
start ::= command_list.
2016-02-02 01:38:29 +00:00
2016-01-30 17:44:42 +00:00
2016-02-05 02:45:04 +00:00
command_list ::= .
command_list ::= command_list sep .
command_list ::= command_list command(C) sep . {
2016-01-30 17:44:42 +00:00
if (C) command_queue.emplace_back(std::move(C));
}
2016-02-02 01:38:29 +00:00
2016-01-30 17:44:42 +00:00
/*
compound_list is identical to command_list, but it is not executed immediately.
*/
%type compound_list { command_ptr_vector }
2016-02-02 01:38:29 +00:00
2016-02-05 02:45:04 +00:00
compound_list ::= .
2016-08-05 14:34:05 +00:00
compound_list(L) ::= compound_list(L) sep.
2016-02-02 01:38:29 +00:00
2016-08-05 14:34:05 +00:00
compound_list(L) ::= compound_list(L) command(C) sep . {
if (C) L.emplace_back(std::move(C));
2016-02-02 01:38:29 +00:00
}
2016-02-05 02:45:04 +00:00
2016-01-30 17:44:42 +00:00
sep ::= SEMI.
sep ::= NL.
%type command { command_ptr }
2016-02-02 01:38:29 +00:00
/* nb -- ||, &&, | -- both sides are optional. This does not. */
2016-01-30 17:44:42 +00:00
command(RV) ::= command(L) PIPE_PIPE opt_nl command(R). {
RV = std::make_unique<or_command>(std::move(L), std::move(R));
}
2016-02-02 01:38:29 +00:00
command(RV) ::= command(L) AMP_AMP opt_nl command(R). {
2016-01-30 17:44:42 +00:00
RV = std::make_unique<and_command>(std::move(L), std::move(R));
}
2016-07-26 18:20:11 +00:00
command(RV) ::= command(L) PIPE opt_nl command(R). {
2016-01-30 17:44:42 +00:00
RV = std::make_unique<pipe_command>(std::move(L), std::move(R));
}
2016-08-05 14:34:05 +00:00
command(C) ::= term(C).
2016-01-30 17:44:42 +00:00
term(RV) ::= COMMAND(C). { RV = std::make_unique<simple_command>(std::move(C)); }
2016-02-02 01:38:29 +00:00
term(RV) ::= EVALUATE(C). { RV = std::make_unique<evaluate_command>(std::move(C)); }
term(RV) ::= BREAK(C). { RV = std::make_unique<break_command>(std::move(C)); }
term(RV) ::= CONTINUE(C). { RV = std::make_unique<continue_command>(std::move(C)); }
2016-08-05 14:34:05 +00:00
term(C) ::= if_command(C).
term(C) ::= begin_command(C).
term(C) ::= paren_command(C).
term(C) ::= loop_command(C).
term(C) ::= for_command(C).
2016-01-30 17:44:42 +00:00
2016-07-23 15:40:40 +00:00
/* lexer error (mismatched quotes, etc) */
term(RV) ::= ERROR(C). {
RV = std::make_unique<error_command>(@C, std::move(C));
}
2016-02-02 01:38:29 +00:00
/*
* fall back to an end error. w/o fallback, it will cause a parse conflict.
*/
/*
%fallback ERROR END RPAREN ELSE ELSE_IF.
2016-07-23 15:40:40 +00:00
2016-02-02 01:38:29 +00:00
*/
2016-07-19 16:37:25 +00:00
/*
term(RV) ::= error RPAREN.
term(RV) ::= error END.
term(RV) ::= LPAREN error RPAREN.
term(RV) ::= BEGIN error END.
term(RV) ::= IF error END.
term(RV) ::= LOOP error END.
term(RV) ::= FOR error END.
*/
2016-02-02 01:38:29 +00:00
/* compound list ends with a separator. paren command does not need the final separator */
2016-02-05 02:45:04 +00:00
%type paren_list { command_ptr_vector }
2016-08-05 14:34:05 +00:00
paren_list(L) ::= compound_list(L) .
2016-01-30 17:44:42 +00:00
2016-08-05 14:34:05 +00:00
paren_list(L) ::= compound_list(L) command(C) . {
L.emplace_back(std::move(C));
2016-02-02 01:38:29 +00:00
}
2016-02-05 02:45:04 +00:00
paren_command(RV) ::= LPAREN(T) paren_list(L) RPAREN(E). {
2016-02-02 01:38:29 +00:00
RV = std::make_unique<begin_command>(@T, std::move(L), std::move(T), std::move(E));
}
2016-02-05 02:45:04 +00:00
begin_command(RV) ::= BEGIN(T) sep compound_list(L) END(E). {
2016-02-02 01:38:29 +00:00
RV = std::make_unique<begin_command>(@T, std::move(L), std::move(T), std::move(E));
2016-01-30 17:44:42 +00:00
}
loop_command(RV) ::= LOOP(T) sep compound_list(L) END(E). {
RV = std::make_unique<loop_command>(@T, std::move(L), std::move(T), std::move(E));
}
2016-06-16 04:04:29 +00:00
for_command(RV) ::= FOR(T) sep compound_list(L) END(E). {
RV = std::make_unique<for_command>(@T, std::move(L), std::move(T), std::move(E));
}
2016-02-05 02:45:04 +00:00
if_command(RV) ::= IF(I) sep compound_list(L) END(E). {
2016-01-30 17:44:42 +00:00
if_command::clause_vector_type v;
v.emplace_back(std::make_unique<if_else_clause>(IF, std::move(L), std::move(I)));
RV = std::make_unique<if_command>(
std::move(v),
std::move(E)
);
}
2016-02-05 02:45:04 +00:00
if_command(RV) ::= IF(I) sep compound_list(L) else_command(EC) END(E). {
2016-01-30 17:44:42 +00:00
if_command::clause_vector_type v;
v.emplace_back(std::make_unique<if_else_clause>(IF, std::move(L), std::move(I)));
for(auto &c : EC) { v.emplace_back(std::move(c)); }
RV = std::make_unique<if_command>(
std::move(v), std::move(E));
}
%token_class else ELSE_IF ELSE.
%type else_command { if_command::clause_vector_type }
2016-02-05 02:45:04 +00:00
else_command(RV) ::= else(E) sep compound_list(L). {
2016-01-30 17:44:42 +00:00
RV.emplace_back(std::make_unique<if_else_clause>(@E, std::move(L), std::move(E)));
}
2016-02-05 02:45:04 +00:00
2016-08-05 14:34:05 +00:00
else_command(EC) ::= else_command(EC) else(E) sep compound_list(L). {
EC.emplace_back(std::make_unique<if_else_clause>(@E, std::move(L), std::move(E)));
2016-01-30 17:44:42 +00:00
}
opt_nl ::= .
2016-02-05 02:45:04 +00:00
opt_nl ::= opt_nl NL .