Skip to content

Commit

Permalink
Fix one of two parse bugs found by oguz-ismail
Browse files Browse the repository at this point in the history
This is the one where bc errored if a function definition directly
followed another, which is supposed to be allowed. This fixes that.

Signed-off-by: Gavin Howard <[email protected]>
  • Loading branch information
YzenaLLC committed Nov 22, 2021
1 parent b20fe46 commit 5b2fe30
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/bc_parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ static bool bc_parse_inst_isLeaf(BcInst t) {
* that can legally end a statement. In bc's case, it could be a newline, a
* semicolon, and a brace in certain cases.
* @param p The parser.
* @return True if the token is a legal delimiter.
*/
static bool bc_parse_isDelimiter(const BcParse *p) {

Expand Down Expand Up @@ -127,6 +128,23 @@ static bool bc_parse_isDelimiter(const BcParse *p) {
return good;
}

/**
* Returns true if we are in top level of a function body. The POSIX grammar
* is defined such that anything is allowed after a function body, so we must
* use this function to detect that case when ending a function body.
* @param p The parser.
* @return True if we are in the top level of parsing a function body.
*/
static bool bc_parse_TopFunc(const BcParse *p) {

bool good = p->flags.len == 2;

uint16_t val = BC_PARSE_FLAG_BRACE | BC_PARSE_FLAG_FUNC_INNER;
val |= BC_PARSE_FLAG_FUNC;

return good && BC_PARSE_TOP_FLAG(p) == val;
}

/**
* Sets a previously defined exit label. What are labels? See the bc Parsing
* section of the Development manual (manuals/development.md).
Expand Down Expand Up @@ -881,7 +899,7 @@ static void bc_parse_endBody(BcParse *p, bool brace) {
bc_lex_next(&p->l);

// If the next token is not a delimiter, that is a problem.
if (BC_ERR(!bc_parse_isDelimiter(p)))
if (BC_ERR(!bc_parse_isDelimiter(p) && !bc_parse_TopFunc(p)))
bc_parse_err(p, BC_ERR_PARSE_TOKEN);
}

Expand Down

0 comments on commit 5b2fe30

Please sign in to comment.