Artifact Content
Not logged in

Artifact ceae1b272549ae1ad3906fe7a3a2a71a8859f08e


     1  /**
     2   * Authors: k.inaba
     3   * License: NYSL 0.9982 http://www.kmonos.net/nysl/
     4   *
     5   * Error Information for Polemy Programming Language
     6   */
     7  module polemy.failure;
     8  import polemy._common;
     9  
    10  /// Represents a position in source codes
    11  
    12  class LexPosition
    13  {
    14  	immutable string filename; /// name of the source file
    15  	immutable int    lineno;   /// 1-origin
    16  	immutable int    column;   /// 1-origin
    17  
    18  	mixin SimpleClass;
    19  	override string toString() const
    20  	{
    21  		return sprintf!("%s:%d:%d")(filename, lineno, column);
    22  	}
    23  
    24  	static immutable LexPosition dummy;
    25  	static this(){ dummy = new immutable(LexPosition)("<unnamed>",0,0); }
    26  }
    27  
    28  unittest
    29  {
    30  	auto p = new LexPosition("hello.cpp", 123, 45);
    31  
    32  	assert_eq( p.filename, "hello.cpp" );
    33  	assert_eq( p.lineno, 123 );
    34  	assert_eq( p.column, 45 );
    35  	assert_eq( text(p), "hello.cpp:123:45" );
    36  
    37  	assert( !__traits(compiles, new LexPosition) );
    38  	assert( !__traits(compiles, p.filename="foo") );
    39  	assert( !__traits(compiles, p.lineno  =789) );
    40  	assert( !__traits(compiles, p.column  =222) );
    41  
    42  	auto q = new LexPosition("hello.cpp", 123, 46);
    43  	assert_lt( p, q );
    44  	assert_ne( p, q );
    45  }
    46  
    47  /*mixin*/
    48  template ExceptionWithPosition()
    49  {
    50  	const LexPosition pos;
    51  	this( const LexPosition pos, string msg, string file=null, size_t line=0, Throwable next=null )
    52  	{
    53  		if(pos is null)
    54  			super(sprintf!("[???????] %s")(msg), file, line, next);
    55  		else
    56  			super(sprintf!("[%s] %s")(pos, msg), file, line, next);
    57  		this.pos = pos;
    58  	}
    59  }
    60  
    61  class UnexpectedEOF : Exception { mixin ExceptionWithPosition; } /// EOF during lexing/parsing
    62  class LexException : Exception { mixin ExceptionWithPosition; } /// Lexer errors
    63  class ParseException : Exception { mixin ExceptionWithPosition; } /// Parser errors
    64  class RuntimeException : Exception { mixin ExceptionWithPosition; } /// Evaluator errors