Artifact Content
Not logged in

Artifact 701f5b1b899607f242de31801cedde45d2a2eda1


     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  alias immutable(LexPosition_t) LexPosition;
    13  
    14  /// Represents a position in source codes
    15  
    16  class LexPosition_t
    17  {
    18  	immutable string filename; /// name of the source file
    19  	immutable int    lineno;   /// 1-origin
    20  	immutable int    column;   /// 1-origin
    21  
    22  	mixin SimpleClass;
    23  	override string toString() const
    24  	{
    25  		return sprintf!("%s:%d:%d")(filename, lineno, column);
    26  	}
    27  
    28  	static LexPosition dummy;
    29  	static this(){ dummy = new LexPosition("<unnamed>",0,0); }
    30  }
    31  
    32  unittest
    33  {
    34  	auto p = new LexPosition("hello.cpp", 123, 45);
    35  
    36  	assert_eq( p.filename, "hello.cpp" );
    37  	assert_eq( p.lineno, 123 );
    38  	assert_eq( p.column, 45 );
    39  	assert_eq( text(p), "hello.cpp:123:45" );
    40  
    41  	assert( !__traits(compiles, new LexPosition) );
    42  	assert( !__traits(compiles, p.filename="foo") );
    43  	assert( !__traits(compiles, p.lineno  =789) );
    44  	assert( !__traits(compiles, p.column  =222) );
    45  
    46  	auto q = new LexPosition("hello.cpp", 123, 46);
    47  	assert_lt( p, q );
    48  	assert_ne( p, q );
    49  }
    50  
    51  /*mixin*/
    52  template ExceptionWithPosition()
    53  {
    54  	LexPosition pos;
    55  	this( LexPosition pos, string msg, string file=null, size_t line=0, Throwable next=null )
    56  	{
    57  		if(pos is null)
    58  			super(sprintf!("[??] %s")(msg), file, line, next);
    59  		else
    60  			super(sprintf!("[%s] %s")(pos, msg), file, line, next);
    61  		this.pos = pos;
    62  	}
    63  }
    64  
    65  class UnexpectedEOF : Exception { mixin ExceptionWithPosition; } /// EOF during lexing/parsing
    66  class LexException : Exception { mixin ExceptionWithPosition; } /// Lexer errors
    67  class ParseException : Exception { mixin ExceptionWithPosition; } /// Parser errors
    68  class RuntimeException : Exception { mixin ExceptionWithPosition; } /// Evaluator errors