@@ -61,7 +61,7 @@ class Parser(implicit config: Config) extends Phase[InputStream, Tree]("parser",
6161 */
6262 override def transform (in : InputStream ): Tree = {
6363 val stream = CharStreams .fromStream(in)
64- val lexer = new decaf.frontend.parsing.Lexer (stream)
64+ val lexer = new decaf.frontend.parsing.Lexer (stream, this )
6565 val tokens = new CommonTokenStream (lexer)
6666 val parser = new DecafParser (tokens)
6767 parser.addErrorListener(ErrorListener )
@@ -98,7 +98,7 @@ class Parser(implicit config: Config) extends Phase[InputStream, Tree]("parser",
9898
9999 override def syntaxError (recognizer : Recognizer [_, _], offendingSymbol : Any , lineNumber : Int ,
100100 charPositionInLine : Int , msg : String , e : RecognitionException ): Unit = {
101- issue( new SyntaxError (msg, new Pos (lineNumber, charPositionInLine + 1 ) ))
101+ throw new SyntaxError (msg, new Pos (lineNumber, charPositionInLine + 1 ))
102102 }
103103 }
104104
@@ -108,20 +108,20 @@ class Parser(implicit config: Config) extends Phase[InputStream, Tree]("parser",
108108
109109 object TopLevelVisitor extends DecafParserBaseVisitor [TopLevel ] {
110110
111- override def visitTopLevel (ctx : DecafParser .TopLevelContext ): TopLevel = positioned(ctx) {
111+ override def visitTopLevel (ctx : DecafParser .TopLevelContext ): TopLevel = {
112112 val classes = ctx.classDef.map(_.accept(ClassDefVisitor ))
113- TopLevel (classes)
113+ TopLevel (classes).setPos(classes.head.pos)
114114 }
115115 }
116116
117117 object ClassDefVisitor extends DecafParserBaseVisitor [ClassDef ] {
118118
119- override def visitClassDef (ctx : DecafParser .ClassDefContext ): ClassDef = positioned(ctx) {
119+ override def visitClassDef (ctx : DecafParser .ClassDefContext ): ClassDef = {
120120 val id = ctx.id.accept(IdVisitor )
121121 // NOTE: if an optional symbol (like extendsClause) is undefined, its corresponding field is null.
122122 val parent = if (ctx.extendsClause != null ) Some (ctx.extendsClause.id.accept(IdVisitor )) else None
123123 val fields = ctx.field.map(_.accept(FieldVisitor ))
124- ClassDef (id, parent, fields)
124+ ClassDef (id, parent, fields).setPos(getPos(ctx. CLASS .getSymbol))
125125 }
126126 }
127127
@@ -275,43 +275,15 @@ class Parser(implicit config: Config) extends Phase[InputStream, Tree]("parser",
275275 override def visitLiteral (ctx : DecafParser .LiteralContext ): Expr = ctx.lit.accept(this )
276276
277277 override def visitIntLit (ctx : DecafParser .IntLitContext ): Expr = positioned(ctx) {
278- val literal = ctx.getText
279- var value = - 1
280- try {
281- value = literal.toInt
282- } catch {
283- case _ : NumberFormatException => // not a valid 32-bit integer
284- issue(new IntTooLargeError (literal, getPos(ctx.INT_LIT .getSymbol)))
285- }
286-
287- IntLit (value)
278+ IntLit (ctx.getText.toInt)
288279 }
289280
290281 override def visitBoolLit (ctx : DecafParser .BoolLitContext ): Expr = positioned(ctx) {
291282 BoolLit (ctx.getText.toBoolean)
292283 }
293284
294- override def visitStringLit (ctx : DecafParser .StringLitContext ): Expr = {
295- val buffer = new StringBuilder
296- val startPos = getPos(ctx.OPEN_STRING .getSymbol)
297- buffer += '"'
298- ctx.stringChar.foreach { node =>
299- if (node.ERROR_NEWLINE != null ) { // handle new line in string
300- issue(new NewlineInStrError (buffer.toString, getPos(node.ERROR_NEWLINE .getSymbol)))
301- }
302- if (node.BAD_ESC != null ) { // handle bad escape character
303- issue(new BadEscCharError (getPos(node.BAD_ESC .getSymbol)))
304- }
305- buffer ++= node.getText
306- }
307-
308- if (ctx.UNTERM_STRING != null ) { // handle unterminated string
309- issue(new UntermStrError (buffer.toString, startPos))
310- }
311-
312- buffer += '"'
313- StringLit (buffer.toString).setPos(startPos)
314- }
285+ override def visitStringLit (ctx : DecafParser .StringLitContext ): Expr =
286+ StringLit (ctx.CLOSE_STRING .getText).setPos(getPos(ctx.OPEN_STRING .getSymbol))
315287
316288 override def visitNullLit (ctx : DecafParser .NullLitContext ): Expr = positioned(ctx) { NullLit () }
317289
0 commit comments