package minijava /** This trait applies to every object to which a corresponding position * in the input source file can be associated. This information is useful * in error messages, for instance. */ trait Positional { self => var row: Int = -1 var col: Int = -1 def setPos(row: Int, col: Int): Unit = { this.row = row this.col = col } /** Copies the position from another Positional object. Returns the * object on which the setPos method was called. */ def setPos(pos: Positional): self.type = { this.row = pos.row this.col = pos.col self } def setNextRow: Unit = { row = row + 1; col = 0 } def setNextColumn: Unit = { col = col + 1 } } /** This class is useful to maintain positional information without * associating it a concrete object. */ class Position(r: Int, c: Int) extends Positional { row = r col = c override def toString: String = "Position(" + row + "," + col + ")" } /** Factory object for the above class. */ object Position { def apply(r: Int, c: Int): Position = new Position(r,c) def unapply(pos: Position): (Int,Int) = (pos.row, pos.col) }