PHP Classes

File: src/eMacros/Exception/ParseException.php

Recommend this page to a friend!
  Classes of Emmanuel Antico   eMacros   src/eMacros/Exception/ParseException.php   Download  
File: src/eMacros/Exception/ParseException.php
Role: Class source
Content type: text/plain
Description: Class source
Class: eMacros
PHP LISP language interpreter
Author: By
Last change:
Date: 10 years ago
Size: 924 bytes
 

Contents

Class file image Download
<?php
namespace eMacros\Exception;

class
ParseException extends \Exception {
    public
$code, $offset, $sourceFile;
   
    public function
__construct($code, $offset, $file = null) {
       
$this->code = $code;
       
$this->offset = $offset;
       
$this->sourceFile = $file;
       
       
//build message
       
$this->message = is_null($file) ? sprintf("Parse error found on line %d, column %d.", $this->getSourceLine(), $this->getSourceColumn())
        :
sprintf("Parse error on file '%s' (Line %d, Column %d).", $file, $this->getSourceLine(), $this->getSourceColumn());
    }
   
    public function
getSourceFile() {
        return
$this->sourceFile;
    }
   
    public function
getSourceLine() {
        if (
$this->offset <= 0) {
            return
1;
        }
       
        return
substr_count($this->code, "\n", 0, $this->offset) + 1;
    }
   
    public function
getSourceColumn() {
       
$pos = strrpos(substr($this->code, 0, $this->offset), "\n");
        return
$this->offset - ($pos === false ? -1 : $pos);
    }
}
?>