Diff
Not logged in

Differences From Artifact [514757ca55e253b4]:

To Artifact [480bb741b8b1612b]:


2 * Authors: k.inaba 2 * Authors: k.inaba 3 * License: NYSL 0.9982 http://www.kmonos.net/nysl/ 3 * License: NYSL 0.9982 http://www.kmonos.net/nysl/ 4 * 4 * 5 * Lexer for Polemy programming language. 5 * Lexer for Polemy programming language. 6 */ 6 */ 7 module polemy.lex; 7 module polemy.lex; 8 import polemy._common; 8 import polemy._common; > 9 import polemy.failure; 9 import std.file : readText; 10 import std.file : readText; 10 import std.ctype : isspace, isalnum; 11 import std.ctype : isspace, isalnum; 11 12 12 /*mixin*/ < 13 template ExceptionWithPosition() < 14 { < 15 const LexPosition pos; < 16 this( const LexPosition pos, string msg, string file=null, size_t line=0 < 17 { < 18 if(pos is null) < 19 super(sprintf!"[??] %s"(msg), file, line, next); < 20 else < 21 super(sprintf!"[%s] %s"(pos, msg), file, line, next); < 22 this.pos = pos; < 23 } < 24 } < 25 < 26 /// Thrown when encountered an EOF in the middle of a lexical token < 27 < 28 class UnexpectedEOF : Exception < 29 { < 30 mixin ExceptionWithPosition; < 31 } < 32 < 33 /// Thrown when encountered a lexical error < 34 < 35 class LexException : Exception < 36 { < 37 mixin ExceptionWithPosition; < 38 }; < 39 < 40 /// Represents a position in source codes < 41 < 42 class LexPosition < 43 { < 44 immutable string filename; /// name of the source file < 45 immutable int lineno; /// 1-origin < 46 immutable int column; /// 1-origin < 47 < 48 mixin SimpleClass; < 49 override string toString() const < 50 { return sprintf!"%s:%d:%d"(filename, lineno, column); } < 51 < 52 static immutable LexPosition dummy; < 53 static this(){ dummy = new immutable(LexPosition)("<unnamed>",0,0); } < 54 } < 55 < 56 unittest < 57 { < 58 auto p = new LexPosition("hello.cpp", 123, 45); < 59 < 60 assert_eq( p.filename, "hello.cpp" ); < 61 assert_eq( p.lineno, 123 ); < 62 assert_eq( p.column, 45 ); < 63 assert_eq( text(p), "hello.cpp:123:45" ); < 64 < 65 assert( !__traits(compiles, new LexPosition) ); < 66 assert( !__traits(compiles, p.filename="foo") ); < 67 assert( !__traits(compiles, p.lineno =789) ); < 68 assert( !__traits(compiles, p.column =222) ); < 69 < 70 auto q = new LexPosition("hello.cpp", 123, 46); < 71 assert_lt( p, q ); < 72 assert_ne( p, q ); < 73 } < 74 < 75 /// Represents a lexer token 13 /// Represents a lexer token 76 14 77 class Token 15 class Token 78 { 16 { 79 immutable LexPosition pos; /// Position where the token occurred in t 17 immutable LexPosition pos; /// Position where the token occurred in t 80 immutable string str; /// The token string itself 18 immutable string str; /// The token string itself 81 immutable bool quoted; /// Was it a "quoted" token or unquoted? 19 immutable bool quoted; /// Was it a "quoted" token or unquoted?