@@ -61,7 +61,7 @@ class Parser(implicit config: Config) extends Phase[InputStream, Tree]("parser",
61
61
*/
62
62
override def transform (in : InputStream ): Tree = {
63
63
val stream = CharStreams .fromStream(in)
64
- val lexer = new decaf.frontend.parsing.Lexer (stream)
64
+ val lexer = new decaf.frontend.parsing.Lexer (stream, this )
65
65
val tokens = new CommonTokenStream (lexer)
66
66
val parser = new DecafParser (tokens)
67
67
parser.addErrorListener(ErrorListener )
@@ -98,7 +98,7 @@ class Parser(implicit config: Config) extends Phase[InputStream, Tree]("parser",
98
98
99
99
override def syntaxError (recognizer : Recognizer [_, _], offendingSymbol : Any , lineNumber : Int ,
100
100
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 ))
102
102
}
103
103
}
104
104
@@ -108,20 +108,20 @@ class Parser(implicit config: Config) extends Phase[InputStream, Tree]("parser",
108
108
109
109
object TopLevelVisitor extends DecafParserBaseVisitor [TopLevel ] {
110
110
111
- override def visitTopLevel (ctx : DecafParser .TopLevelContext ): TopLevel = positioned(ctx) {
111
+ override def visitTopLevel (ctx : DecafParser .TopLevelContext ): TopLevel = {
112
112
val classes = ctx.classDef.map(_.accept(ClassDefVisitor ))
113
- TopLevel (classes)
113
+ TopLevel (classes).setPos(classes.head.pos)
114
114
}
115
115
}
116
116
117
117
object ClassDefVisitor extends DecafParserBaseVisitor [ClassDef ] {
118
118
119
- override def visitClassDef (ctx : DecafParser .ClassDefContext ): ClassDef = positioned(ctx) {
119
+ override def visitClassDef (ctx : DecafParser .ClassDefContext ): ClassDef = {
120
120
val id = ctx.id.accept(IdVisitor )
121
121
// NOTE: if an optional symbol (like extendsClause) is undefined, its corresponding field is null.
122
122
val parent = if (ctx.extendsClause != null ) Some (ctx.extendsClause.id.accept(IdVisitor )) else None
123
123
val fields = ctx.field.map(_.accept(FieldVisitor ))
124
- ClassDef (id, parent, fields)
124
+ ClassDef (id, parent, fields).setPos(getPos(ctx. CLASS .getSymbol))
125
125
}
126
126
}
127
127
@@ -275,43 +275,15 @@ class Parser(implicit config: Config) extends Phase[InputStream, Tree]("parser",
275
275
override def visitLiteral (ctx : DecafParser .LiteralContext ): Expr = ctx.lit.accept(this )
276
276
277
277
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)
288
279
}
289
280
290
281
override def visitBoolLit (ctx : DecafParser .BoolLitContext ): Expr = positioned(ctx) {
291
282
BoolLit (ctx.getText.toBoolean)
292
283
}
293
284
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))
315
287
316
288
override def visitNullLit (ctx : DecafParser .NullLitContext ): Expr = positioned(ctx) { NullLit () }
317
289
0 commit comments