@@ -4,75 +4,13 @@ * * Lexer for Polemy programming language. */ module polemy.lex; -import polemy._common; +import polemy._common; +import polemy.failure; import std.file : readText; import std.ctype : isspace, isalnum; -/*mixin*/ -template ExceptionWithPosition() -{ - const LexPosition pos; - this( const LexPosition pos, string msg, string file=null, size_t line=0, Throwable next=null ) - { - if(pos is null) - super(sprintf!"[??] %s"(msg), file, line, next); - else - super(sprintf!"[%s] %s"(pos, msg), file, line, next); - this.pos = pos; - } -} - -/// Thrown when encountered an EOF in the middle of a lexical token - -class UnexpectedEOF : Exception -{ - mixin ExceptionWithPosition; -} - -/// Thrown when encountered a lexical error - -class LexException : Exception -{ - mixin ExceptionWithPosition; -}; - -/// Represents a position in source codes - -class LexPosition -{ - immutable string filename; /// name of the source file - immutable int lineno; /// 1-origin - immutable int column; /// 1-origin - - mixin SimpleClass; - override string toString() const - { return sprintf!"%s:%d:%d"(filename, lineno, column); } - - static immutable LexPosition dummy; - static this(){ dummy = new immutable(LexPosition)("",0,0); } -} - -unittest -{ - auto p = new LexPosition("hello.cpp", 123, 45); - - assert_eq( p.filename, "hello.cpp" ); - assert_eq( p.lineno, 123 ); - assert_eq( p.column, 45 ); - assert_eq( text(p), "hello.cpp:123:45" ); - - assert( !__traits(compiles, new LexPosition) ); - assert( !__traits(compiles, p.filename="foo") ); - assert( !__traits(compiles, p.lineno =789) ); - assert( !__traits(compiles, p.column =222) ); - - auto q = new LexPosition("hello.cpp", 123, 46); - assert_lt( p, q ); - assert_ne( p, q ); -} - /// Represents a lexer token class Token {