Skip to content

Commit

Permalink
Add explicit EOF to top-level parser rule
Browse files Browse the repository at this point in the history
* Fixes #1154
* Generally helps catch trailing syntax errors
* Performance-neutral relative to previous grammar
* Recommended by antlr4 devs, can help performance in some cases
* See antlr/antlr4#1540
  • Loading branch information
albert-magyar committed Oct 29, 2019
1 parent 7b18778 commit 59d07a7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main/antlr4/FIRRTL.g4
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import firrtl.LexerHelper;

// Does there have to be at least one module?
circuit
: 'circuit' id ':' info? INDENT module* DEDENT
: 'circuit' id ':' info? INDENT module* DEDENT EOF
;

module
Expand Down
23 changes: 23 additions & 0 deletions src/test/scala/firrtlTests/ParserSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,29 @@ class ParserSpec extends FirrtlFlatSpec {
Driver.execute(manager)
}
}

"Trailing syntax errors" should "be caught in the parser" in {
val input = s"""
|circuit Foo:
| module Bar:
| input a: UInt<1>
|output b: UInt<1>
| b <- a
|
| module Foo:
| input a: UInt<1>
| output b: UInt<1>
| inst bar of Bar
| bar.a <- a
| b <- bar.b
""".stripMargin
val manager = new ExecutionOptionsManager("test") with HasFirrtlOptions {
firrtlOptions = FirrtlExecutionOptions(firrtlSource = Some(input))
}
a [SyntaxErrorsException] shouldBe thrownBy {
Driver.execute(manager)
}
}
}

class ParserPropSpec extends FirrtlPropSpec {
Expand Down

0 comments on commit 59d07a7

Please sign in to comment.