diff options
| author | Kamyar Mohajerani | 2016-07-21 23:40:34 +0430 |
|---|---|---|
| committer | Jack Koenig | 2016-07-21 12:10:34 -0700 |
| commit | ab340febdc7a5418da945f9b79624d36e66e26db (patch) | |
| tree | 04e4aef30081fdd419281d69be4b141fd49b4b1f /src/main/scala/firrtl/Parser.scala | |
| parent | b7de40e23161a7346fea90576f07b5c200c2675b (diff) | |
Indentation support for the ANTLR parser (as discussed in #192) (#194)
Indentation support for the ANTLR parser
- some clean-up of the parser code (TODO: file input could be improved, more clean-up)
- get rid of Translator and specify all syntactic rules in antlr4 grammer
- support for else-when shorthand in the grammar
- rename Begin to Block which makes more sense
Diffstat (limited to 'src/main/scala/firrtl/Parser.scala')
| -rw-r--r-- | src/main/scala/firrtl/Parser.scala | 47 |
1 files changed, 33 insertions, 14 deletions
diff --git a/src/main/scala/firrtl/Parser.scala b/src/main/scala/firrtl/Parser.scala index dc8d6875..aa6ea63f 100644 --- a/src/main/scala/firrtl/Parser.scala +++ b/src/main/scala/firrtl/Parser.scala @@ -26,39 +26,53 @@ MODIFICATIONS. */ package firrtl -import org.antlr.v4.runtime._; -import org.antlr.v4.runtime.atn._; +import java.io.{ByteArrayInputStream, SequenceInputStream} + +import org.antlr.v4.runtime._ +import org.antlr.v4.runtime.atn._ import com.typesafe.scalalogging.LazyLogging import firrtl.ir._ -import Utils.{time} -import antlr._ +import firrtl.Utils.time +import firrtl.antlr.{FIRRTLParser, _} class ParserException(message: String) extends Exception(message) + case class ParameterNotSpecifiedException(message: String) extends ParserException(message) + case class ParameterRedefinedException(message: String) extends ParserException(message) + case class InvalidStringLitException(message: String) extends ParserException(message) + case class InvalidEscapeCharException(message: String) extends ParserException(message) -object Parser extends LazyLogging -{ + +object Parser extends LazyLogging { /** Takes Iterator over lines of FIRRTL, returns FirrtlNode (root node is Circuit) */ def parse(lines: Iterator[String], infoMode: InfoMode = UseInfo): Circuit = { - val fixedInput = time("Translator") { Translator.addBrackets(lines) } - val antlrStream = new ANTLRInputStream(fixedInput.result) - val lexer = new FIRRTLLexer(antlrStream) - val tokens = new CommonTokenStream(lexer) - val parser = new FIRRTLParser(tokens) - time("ANTLR Parser") { parser.getInterpreter.setPredictionMode(PredictionMode.SLL) } + val parser = { + import scala.collection.JavaConverters._ + val inStream = new SequenceInputStream( + lines.map{s => new ByteArrayInputStream((s + "\n").getBytes("UTF-8")) }.asJavaEnumeration + ) + val lexer = new FIRRTLLexer(new ANTLRInputStream(inStream)) + new FIRRTLParser(new CommonTokenStream(lexer)) + } + + time("ANTLR Parser") { + parser.getInterpreter.setPredictionMode(PredictionMode.SLL) + } // Concrete Syntax Tree val cst = parser.circuit val numSyntaxErrors = parser.getNumberOfSyntaxErrors - if (numSyntaxErrors > 0) throw new ParserException(s"${numSyntaxErrors} syntax error(s) detected") + if (numSyntaxErrors > 0) throw new ParserException(s"$numSyntaxErrors syntax error(s) detected") val visitor = new Visitor(infoMode) - val ast = time("Visitor") { visitor.visit(cst) } match { + val ast = time("Visitor") { + visitor.visit(cst) + } match { case c: Circuit => c case x => throw new ClassCastException("Error! AST not rooted with Circuit node!") } @@ -69,8 +83,13 @@ object Parser extends LazyLogging def parse(lines: Seq[String]): Circuit = parse(lines.iterator) sealed abstract class InfoMode + case object IgnoreInfo extends InfoMode + case object UseInfo extends InfoMode + case class GenInfo(filename: String) extends InfoMode + case class AppendInfo(filename: String) extends InfoMode + } |
