Diff
Not logged in

Differences From Artifact [a96449f7398ef311]:

To Artifact [1725bdb3bf054565]:


7 7 module polemy.lex; 8 8 import polemy._common; 9 9 import std.file : readText; 10 10 import std.ctype : isspace, isalnum; 11 11 12 12 /// Exception from this module 13 13 14 -class LexException : Exception 14 +/*mixin*/ 15 +template ExceptionWithPosition() 15 16 { 16 17 const LexPosition pos; 17 - 18 18 this( const LexPosition pos, string msg, string file=null, size_t line=0, Throwable next=null ) 19 19 { super(sprintf!"[%s] %s"(pos, msg), file, line, next); this.pos = pos; } 20 +} 21 + 22 +class UnexpectedEOF : Exception 23 +{ 24 + mixin ExceptionWithPosition; 25 +} 26 + 27 +class LexException : Exception 28 +{ 29 + mixin ExceptionWithPosition; 20 30 }; 21 31 22 32 /// Represents a position in a source code 23 33 24 34 class LexPosition 25 35 { 26 36 immutable string filename; /// name of the source file ................................................................................ 158 168 bool isLetter (dchar c) { return !isSpace(c) && !isSymbol(c); } 159 169 } 160 170 161 171 string readQuoted(const LexPosition pos){char[] buf; return readQuoted(pos,buf);} 162 172 string readQuoted(const LexPosition pos, ref char[] buf) 163 173 { 164 174 if( reader.empty ) 165 - throw genex!LexException(pos, "EOF found while lexing a quoted-string"); 175 + throw genex!UnexpectedEOF(pos, "Quoted string not terminated"); 166 176 dchar c = reader.front; 167 177 reader.popFront; 168 178 if( c == '"' ) 169 179 return assumeUnique(buf); 170 180 if( c == '\\' && !reader.empty ) { 171 181 if( reader.front=='"' ) { 172 182 reader.popFront; ................................................................................ 289 299 assert_eq( lexf.front.str, "import" ); 290 300 assert_eq( lexf.front.pos.lineno, 8 ); 291 301 assert_eq( lexf.front.pos.column, 1 ); 292 302 } 293 303 294 304 unittest 295 305 { 296 - assert_throw!LexException( lexerFromString(`"`) ); 306 + assert_throw!UnexpectedEOF( lexerFromString(`"`) ); 297 307 } 298 308 299 309 unittest 300 310 { 301 311 auto lex = lexerFromString(`my # comment should`~"\r\n"~`# hey!! 302 312 be ignored. 303 313 hahaha"hihihi""hu\\\"huhu"#123 aa