3464a035ec 2010-11-20 kinaba: /** 3464a035ec 2010-11-20 kinaba: * Authors: k.inaba 3464a035ec 2010-11-20 kinaba: * License: NYSL 0.9982 http://www.kmonos.net/nysl/ 3464a035ec 2010-11-20 kinaba: * 3464a035ec 2010-11-20 kinaba: * Error Information for Polemy Programming Language 3464a035ec 2010-11-20 kinaba: */ 3464a035ec 2010-11-20 kinaba: module polemy.failure; 3464a035ec 2010-11-20 kinaba: import polemy._common; 3464a035ec 2010-11-20 kinaba: 3464a035ec 2010-11-20 kinaba: /// Represents a position in source codes 3464a035ec 2010-11-20 kinaba: 3464a035ec 2010-11-20 kinaba: class LexPosition 3464a035ec 2010-11-20 kinaba: { 3464a035ec 2010-11-20 kinaba: immutable string filename; /// name of the source file 3464a035ec 2010-11-20 kinaba: immutable int lineno; /// 1-origin 3464a035ec 2010-11-20 kinaba: immutable int column; /// 1-origin 3464a035ec 2010-11-20 kinaba: 3464a035ec 2010-11-20 kinaba: mixin SimpleClass; 3464a035ec 2010-11-20 kinaba: override string toString() const 3464a035ec 2010-11-20 kinaba: { 3464a035ec 2010-11-20 kinaba: return sprintf!("%s:%d:%d")(filename, lineno, column); 3464a035ec 2010-11-20 kinaba: } 3464a035ec 2010-11-20 kinaba: 3464a035ec 2010-11-20 kinaba: static immutable LexPosition dummy; 3464a035ec 2010-11-20 kinaba: static this(){ dummy = new immutable(LexPosition)("<unnamed>",0,0); } 3464a035ec 2010-11-20 kinaba: } 3464a035ec 2010-11-20 kinaba: 3464a035ec 2010-11-20 kinaba: unittest 3464a035ec 2010-11-20 kinaba: { 3464a035ec 2010-11-20 kinaba: auto p = new LexPosition("hello.cpp", 123, 45); 3464a035ec 2010-11-20 kinaba: 3464a035ec 2010-11-20 kinaba: assert_eq( p.filename, "hello.cpp" ); 3464a035ec 2010-11-20 kinaba: assert_eq( p.lineno, 123 ); 3464a035ec 2010-11-20 kinaba: assert_eq( p.column, 45 ); 3464a035ec 2010-11-20 kinaba: assert_eq( text(p), "hello.cpp:123:45" ); 3464a035ec 2010-11-20 kinaba: 3464a035ec 2010-11-20 kinaba: assert( !__traits(compiles, new LexPosition) ); 3464a035ec 2010-11-20 kinaba: assert( !__traits(compiles, p.filename="foo") ); 3464a035ec 2010-11-20 kinaba: assert( !__traits(compiles, p.lineno =789) ); 3464a035ec 2010-11-20 kinaba: assert( !__traits(compiles, p.column =222) ); 3464a035ec 2010-11-20 kinaba: 3464a035ec 2010-11-20 kinaba: auto q = new LexPosition("hello.cpp", 123, 46); 3464a035ec 2010-11-20 kinaba: assert_lt( p, q ); 3464a035ec 2010-11-20 kinaba: assert_ne( p, q ); 3464a035ec 2010-11-20 kinaba: } 3464a035ec 2010-11-20 kinaba: 3464a035ec 2010-11-20 kinaba: /*mixin*/ 3464a035ec 2010-11-20 kinaba: template ExceptionWithPosition() 3464a035ec 2010-11-20 kinaba: { 3464a035ec 2010-11-20 kinaba: const LexPosition pos; 3464a035ec 2010-11-20 kinaba: this( const LexPosition pos, string msg, string file=null, size_t line=0, Throwable next=null ) 3464a035ec 2010-11-20 kinaba: { 3464a035ec 2010-11-20 kinaba: if(pos is null) 3464a035ec 2010-11-20 kinaba: super(sprintf!("[???????] %s")(msg), file, line, next); 3464a035ec 2010-11-20 kinaba: else 3464a035ec 2010-11-20 kinaba: super(sprintf!("[%s] %s")(pos, msg), file, line, next); 3464a035ec 2010-11-20 kinaba: this.pos = pos; 3464a035ec 2010-11-20 kinaba: } 3464a035ec 2010-11-20 kinaba: } 3464a035ec 2010-11-20 kinaba: 3464a035ec 2010-11-20 kinaba: class UnexpectedEOF : Exception { mixin ExceptionWithPosition; } /// EOF during lexing/parsing 3464a035ec 2010-11-20 kinaba: class LexException : Exception { mixin ExceptionWithPosition; } /// Lexer errors 3464a035ec 2010-11-20 kinaba: class ParseException : Exception { mixin ExceptionWithPosition; } /// Parser errors 3464a035ec 2010-11-20 kinaba: class RuntimeException : Exception { mixin ExceptionWithPosition; } /// Evaluator errors