xo-reader2: support 0-argument apply-expressions
This commit is contained in:
parent
c221a31efe
commit
6795c1bc15
7 changed files with 138 additions and 77 deletions
|
|
@ -116,7 +116,7 @@ namespace xo {
|
|||
case applyexprstatetype::invalid: return "invalid";
|
||||
case applyexprstatetype::apply_0: return "expr";
|
||||
case applyexprstatetype::apply_1: return "lparen";
|
||||
case applyexprstatetype::apply_2: return "expr";
|
||||
case applyexprstatetype::apply_2: return "expr|rparen";
|
||||
case applyexprstatetype::apply_3: return "comma|rparen";
|
||||
case applyexprstatetype::N: break;
|
||||
}
|
||||
|
|
@ -134,6 +134,9 @@ namespace xo {
|
|||
case tokentype::tk_leftparen:
|
||||
this->on_leftparen_token(tk, p_psm);
|
||||
return;
|
||||
case tokentype::tk_rightparen:
|
||||
this->on_rightparen_token(tk, p_psm);
|
||||
return;
|
||||
case tokentype::tk_symbol:
|
||||
case tokentype::tk_def:
|
||||
case tokentype::tk_if:
|
||||
|
|
@ -147,7 +150,6 @@ namespace xo {
|
|||
case tokentype::tk_i64:
|
||||
case tokentype::tk_bool:
|
||||
case tokentype::tk_invalid:
|
||||
case tokentype::tk_rightparen:
|
||||
case tokentype::tk_leftbracket:
|
||||
case tokentype::tk_rightbracket:
|
||||
case tokentype::tk_leftbrace:
|
||||
|
|
@ -186,7 +188,25 @@ namespace xo {
|
|||
if (applystate_ == applyexprstatetype::apply_1) {
|
||||
this->applystate_ = applyexprstatetype::apply_2;
|
||||
|
||||
DExpectExprSsm::start(p_psm);
|
||||
DExpectExprSsm::start(false /*!allow_defs*/,
|
||||
false /*!cxl_on_rightbrace*/,
|
||||
true /*cxl_on_rightparen*/,
|
||||
p_psm);
|
||||
return;
|
||||
}
|
||||
|
||||
Super::on_token(tk, p_psm);
|
||||
}
|
||||
|
||||
void
|
||||
DApplySsm::on_rightparen_token(const Token & tk,
|
||||
ParserStateMachine * p_psm)
|
||||
{
|
||||
if (applystate_ == applyexprstatetype::apply_2) {
|
||||
obj<AExpression> apply = this->assemble_expr(p_psm->expr_alloc());
|
||||
|
||||
p_psm->pop_ssm();
|
||||
p_psm->on_parsed_expression(apply);
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -226,28 +246,7 @@ namespace xo {
|
|||
args_expr_v_->push_back(expr_gco);
|
||||
|
||||
if (tk.tk_type() == tokentype::tk_rightparen) {
|
||||
// begin assemble_expr()..
|
||||
|
||||
std::uint32_t n_args = args_expr_v_->size();
|
||||
|
||||
DApplyExpr * apply
|
||||
= (DApplyExpr::scaffold
|
||||
(mm,
|
||||
TypeRef::dwim(TypeRef::prefix_type::from_chars("apply"),
|
||||
nullptr),
|
||||
fn_expr_,
|
||||
n_args));
|
||||
|
||||
for (std::uint32_t i_arg = 0; i_arg < n_args; ++i_arg) {
|
||||
auto arg_expr
|
||||
= args_expr_v_->at(i_arg).to_facet<AExpression>();
|
||||
|
||||
apply->assign_arg(i_arg, arg_expr);
|
||||
}
|
||||
|
||||
// ..end assemble_expr()
|
||||
|
||||
obj<AExpression,DApplyExpr> apply_ex(apply);
|
||||
obj<AExpression> apply_ex = this->assemble_expr(p_psm->expr_alloc());
|
||||
|
||||
p_psm->pop_ssm();
|
||||
p_psm->on_parsed_expression(apply_ex);
|
||||
|
|
@ -266,6 +265,35 @@ namespace xo {
|
|||
Super::on_parsed_expression_with_token(expr, tk, p_psm);
|
||||
}
|
||||
|
||||
obj<AExpression>
|
||||
DApplySsm::assemble_expr(obj<AAllocator> mm)
|
||||
{
|
||||
// begin assemble_expr()..
|
||||
|
||||
std::uint32_t n_args = args_expr_v_->size();
|
||||
|
||||
DApplyExpr * apply
|
||||
= (DApplyExpr::scaffold
|
||||
(mm,
|
||||
TypeRef::dwim(TypeRef::prefix_type::from_chars("apply"),
|
||||
nullptr),
|
||||
fn_expr_,
|
||||
n_args));
|
||||
|
||||
for (std::uint32_t i_arg = 0; i_arg < n_args; ++i_arg) {
|
||||
auto arg_expr
|
||||
= args_expr_v_->at(i_arg).to_facet<AExpression>();
|
||||
|
||||
apply->assign_arg(i_arg, arg_expr);
|
||||
}
|
||||
|
||||
// ..end assemble_expr()
|
||||
|
||||
obj<AExpression,DApplyExpr> apply_ex(apply);
|
||||
|
||||
return apply_ex;
|
||||
}
|
||||
|
||||
#ifdef NOT_YET
|
||||
void
|
||||
apply_xs::on_expr(bp<Expression> expr,
|
||||
|
|
|
|||
|
|
@ -42,41 +42,48 @@ namespace xo {
|
|||
namespace scm {
|
||||
|
||||
DExpectExprSsm::DExpectExprSsm(bool allow_defs,
|
||||
bool cxl_on_rightbrace)
|
||||
bool cxl_on_rightbrace,
|
||||
bool cxl_on_rightparen)
|
||||
: allow_defs_{allow_defs},
|
||||
cxl_on_rightbrace_{cxl_on_rightbrace}
|
||||
cxl_on_rightbrace_{cxl_on_rightbrace},
|
||||
cxl_on_rightparen_{cxl_on_rightparen}
|
||||
{
|
||||
}
|
||||
|
||||
DExpectExprSsm *
|
||||
DExpectExprSsm::_make(DArena & mm,
|
||||
bool allow_defs,
|
||||
bool cxl_on_rightbrace)
|
||||
bool cxl_on_rightbrace,
|
||||
bool cxl_on_rightparen)
|
||||
{
|
||||
void * mem = mm.alloc(typeseq::id<DExpectExprSsm>(),
|
||||
sizeof(DExpectExprSsm));
|
||||
|
||||
return new (mem) DExpectExprSsm(allow_defs,
|
||||
cxl_on_rightbrace);
|
||||
cxl_on_rightbrace,
|
||||
cxl_on_rightparen);
|
||||
}
|
||||
|
||||
obj<ASyntaxStateMachine,DExpectExprSsm>
|
||||
DExpectExprSsm::make(DArena & mm,
|
||||
bool allow_defs,
|
||||
bool cxl_on_rightbrace)
|
||||
bool cxl_on_rightbrace,
|
||||
bool cxl_on_rightparen)
|
||||
{
|
||||
return obj<ASyntaxStateMachine,DExpectExprSsm>(_make(mm, allow_defs, cxl_on_rightbrace));
|
||||
return obj<ASyntaxStateMachine,DExpectExprSsm>(_make(mm, allow_defs, cxl_on_rightbrace, cxl_on_rightparen));
|
||||
}
|
||||
|
||||
void
|
||||
DExpectExprSsm::start(DArena & mm,
|
||||
bool allow_defs,
|
||||
DExpectExprSsm::start(bool allow_defs,
|
||||
bool cxl_on_rightbrace,
|
||||
bool cxl_on_rightparen,
|
||||
ParserStateMachine * p_psm)
|
||||
{
|
||||
auto & mm = p_psm->parser_alloc();
|
||||
|
||||
DArena::Checkpoint ckp = mm.checkpoint();
|
||||
|
||||
auto ssm = DExpectExprSsm::make(mm, allow_defs, cxl_on_rightbrace);
|
||||
auto ssm = DExpectExprSsm::make(mm, allow_defs, cxl_on_rightbrace, cxl_on_rightparen);
|
||||
|
||||
p_psm->push_ssm(ckp, ssm);
|
||||
}
|
||||
|
|
@ -84,9 +91,9 @@ namespace xo {
|
|||
void
|
||||
DExpectExprSsm::start(ParserStateMachine * p_psm)
|
||||
{
|
||||
start(p_psm->parser_alloc(),
|
||||
false /*!allow_defs*/,
|
||||
start(false /*!allow_defs*/,
|
||||
false /*!cxl_on_rightbrace*/,
|
||||
false /*cxl_on_rightparen*/,
|
||||
p_psm);
|
||||
}
|
||||
|
||||
|
|
@ -117,6 +124,10 @@ namespace xo {
|
|||
this->on_leftparen_token(tk, p_psm);
|
||||
return;
|
||||
|
||||
case tokentype::tk_rightparen:
|
||||
this->on_rightparen_token(tk, p_psm);
|
||||
return;
|
||||
|
||||
case tokentype::tk_leftbrace:
|
||||
this->on_leftbrace_token(tk, p_psm);
|
||||
return;
|
||||
|
|
@ -158,7 +169,6 @@ namespace xo {
|
|||
case tokentype::tk_singleassign:
|
||||
case tokentype::tk_colon:
|
||||
case tokentype::tk_semicolon:
|
||||
case tokentype::tk_rightparen:
|
||||
case tokentype::tk_leftbracket:
|
||||
case tokentype::tk_rightbracket:
|
||||
case tokentype::tk_rightbrace:
|
||||
|
|
@ -206,6 +216,21 @@ namespace xo {
|
|||
p_psm->on_token(tk);
|
||||
}
|
||||
|
||||
void
|
||||
DExpectExprSsm::on_rightparen_token(const Token & tk,
|
||||
ParserStateMachine * p_psm)
|
||||
{
|
||||
if (cxl_on_rightparen_) {
|
||||
/* abandon expression, delegate rightparen to parent */
|
||||
|
||||
p_psm->pop_ssm();
|
||||
p_psm->on_token(tk);
|
||||
return;
|
||||
}
|
||||
|
||||
Super::on_token(tk, p_psm);
|
||||
}
|
||||
|
||||
void
|
||||
DExpectExprSsm::on_leftbrace_token(const Token & tk,
|
||||
ParserStateMachine * p_psm)
|
||||
|
|
@ -482,8 +507,8 @@ namespace xo {
|
|||
"DExpectExprSsm",
|
||||
refrtag("allow_defs", allow_defs_),
|
||||
refrtag("cxl_on_rightbrace", cxl_on_rightbrace_),
|
||||
refrtag("expect", this->get_expect_str())
|
||||
);
|
||||
refrtag("cxl_on_rightparen", cxl_on_rightparen_),
|
||||
refrtag("expect", this->get_expect_str()));
|
||||
}
|
||||
|
||||
#ifdef NOT_YET
|
||||
|
|
@ -494,18 +519,6 @@ namespace xo {
|
|||
if_else_xs::start(p_psm);
|
||||
}
|
||||
|
||||
void
|
||||
expect_expr_xs::on_leftparen_token(const token_type & /*tk*/,
|
||||
parserstatemachine * p_psm)
|
||||
{
|
||||
scope log(XO_DEBUG(p_psm->debug_flag()));
|
||||
|
||||
//constexpr const char * self_name = "exprstate::on_leftparen";
|
||||
|
||||
/* push lparen_0 to remember to look for subsequent rightparen. */
|
||||
paren_xs::start(p_psm);
|
||||
}
|
||||
|
||||
void
|
||||
expect_expr_xs::on_rightbrace_token(const token_type & tk,
|
||||
parserstatemachine * p_psm)
|
||||
|
|
@ -561,14 +574,6 @@ namespace xo {
|
|||
<< xtag("cxl_on_rightbrace", cxl_on_rightbrace_)
|
||||
<< ">";
|
||||
}
|
||||
|
||||
bool
|
||||
expect_expr_xs::pretty_print(const xo::print::ppindentinfo & ppii) const
|
||||
{
|
||||
return ppii.pps()->pretty_struct(ppii, "expect_expr_xs",
|
||||
refrtag("allow_defs", allow_defs_),
|
||||
refrtag("cxl_on_rightbrace", cxl_on_rightbrace_));
|
||||
}
|
||||
#endif
|
||||
|
||||
} /*namespace scm*/
|
||||
|
|
|
|||
|
|
@ -159,10 +159,7 @@ namespace xo {
|
|||
* by rightparen. empty parentheses '()'
|
||||
* do not denote anything, in expression context
|
||||
**/
|
||||
DExpectExprSsm::start(p_psm->parser_alloc(),
|
||||
false /*!allow_defs*/,
|
||||
false /*cx_on_rightbrace*/,
|
||||
p_psm);
|
||||
DExpectExprSsm::start(p_psm);
|
||||
|
||||
return;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -218,7 +218,7 @@ namespace xo {
|
|||
if (!lhs_) {
|
||||
return "expr1|leftparen";
|
||||
} else if (op_type_ == optype::invalid) {
|
||||
return "oper|semicolon|rightparen|rightbrace";
|
||||
return "oper|semicolon|leftparen|rightparen|rightbrace";
|
||||
} else {
|
||||
return "expr2|leftparen";
|
||||
}
|
||||
|
|
|
|||
|
|
@ -29,9 +29,9 @@ namespace xo {
|
|||
/* want to accept anything that starts an expression,
|
||||
* except that rightbrace '}' ends it
|
||||
*/
|
||||
DExpectExprSsm::start(p_psm->parser_alloc(),
|
||||
true /*allow_defs*/,
|
||||
DExpectExprSsm::start(true /*allow_defs*/,
|
||||
true /*cxl_on_rightbrace*/,
|
||||
false /*!cxl_on_rightparen*/,
|
||||
p_psm);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue