Diff
Not logged in

Differences From Artifact [a96449f7398ef311]:

To Artifact [1725bdb3bf054565]:


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