Skip to content

Commit

Permalink
Fix BSV parsing of naked expressions
Browse files Browse the repository at this point in the history
in statement and case-expression contexts (Issue 646)
  • Loading branch information
quark17 committed Dec 21, 2023
1 parent 4680b41 commit 96080b5
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/comp/Parser/BSV/CVParser.lhs
Original file line number Diff line number Diff line change
Expand Up @@ -2261,7 +2261,12 @@ if both fail, the error messages will come from the more likely alternative.
> pImperativeLet decl_atts bind_atts flags pos)
> <|> (pKeyword SV_KW_match >> noAttrs_ >>
> pImperativePatternDecl flags)
> <|> try pVarTypeCases
> <|> try (do
> -- require a match of the whole statement, to avoid parsing just
> -- the start of a naked expression that begins with a variable
> s <- pVarTypeCases
> lookAhead pSemi
> return s)
> <|> try (do
> when (not $ allowNakedExpr flags) (fail "naked expression")
> e <- pExpression
Expand Down Expand Up @@ -3391,6 +3396,8 @@ The same, but reads an output port name instead of a type
> ((pImperativeWithCallRegWrite flags pos primy >>= noBindAttrs)
> <|> (pImperativeWithCallEq flags primy >>= noBindAttrs)
> <|> pImperativeWithCallBind bind_atts flags primy
> -- Naked expressions are accepted here, so that a helpful error
> -- for that situation can be given (EForbiddenNakedExprInExprBlock).
> <|> (pImperativeWithExprNakedExpr flags pos primy >>= noBindAttrs))
> pImperativeWithVarCall bind_attrs flags vars =
> failWithErr (getIdOrTuplePosition vars, EForbiddenTuple)
Expand Down
14 changes: 14 additions & 0 deletions testsuite/bsc.syntax/bsv05/NakedExpr_CaseExpr.bsv
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
typedef enum {Foo, Bar, Foobar} FooNum deriving(Bits, Eq);

module mkCase(Empty);
Reg#(int) r <- mkReg(0);
Reg#(FooNum) r_foo <- mkReg(Foo);

rule change_foo;
r_foo <= case (r_foo) matches
Foo: Bar;
Bar: r == 42 ? Foobar : Bar;
Foobar: Foobar;
endcase;
endrule
endmodule
11 changes: 11 additions & 0 deletions testsuite/bsc.syntax/bsv05/NakedExpr_Stmt.bsv
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module mkTest (Empty);
int x = 1;
int y = 2;
x == y ? mkSub1 : mkSub2;
endmodule

module mkSub1 (Empty);
endmodule

module mkSub2 (Empty);
endmodule
19 changes: 18 additions & 1 deletion testsuite/bsc.syntax/bsv05/bsv05.exp
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,6 @@ compile_pass ImperativeVariableBind_NewLetType.bsv

# -----


# Tests for decoding UTF8 and erroring out on non-UTF8 files
compile_pass UTF8Code.bsv
compile_pass UTF8LineComment.bsv
Expand All @@ -422,3 +421,21 @@ compile_fail_error UTF8BadCons1.bsv P0005
compile_fail_error UTF8BadCons2.bsv P0005
compile_pass 風呂敷.bsv

# -----

# GitHub Issue #646
#
# In a statement context that allows naked expressions,
# the parser should accept the full expression and not
# stop early on a prefix that also happens to be an
# expression. For example, "x == y ? e1 : e2" should
# parse and not stop early on "x" or "x == y".

compile_pass NakedExpr_Stmt.bsv

# The arms of case expressions are parsed as statements
# allowing naked expression, so test there, too
#
compile_pass NakedExpr_CaseExpr.bsv

# ---------------

0 comments on commit 96080b5

Please sign in to comment.