Differences From Artifact [df057f5d35e6bc8a]:
- File
polemy/lex.d
- 2010-11-09 05:19:20 - part of checkin [8de5b49cdf] on branch trunk - split tricks module into a separate package. (user: kinaba) [annotate]
To Artifact [bda39a8af81ee5d9]:
- File
polemy/lex.d
- 2010-11-09 06:02:32 - part of checkin [7de80acfb8] on branch trunk - Added ultra tenuki REPL (user: kinaba) [annotate]
11 11
12 /// Exception from this module 12 /// Exception from this module
13 13
14 class LexException : Exception 14 class LexException : Exception
15 { 15 {
16 const LexPosition pos; 16 const LexPosition pos;
17 17
18 this( const LexPosition pos, string msg, string file="", int line=0 ) | 18 this( const LexPosition pos, string msg, string file="", int line=0, Thr
19 { super(sprintf!"[%s] %s"(pos, msg), file, line); this.pos = pos | 19 { super(sprintf!"[%s] %s"(pos, msg), file, line, next); this.pos
20 }; 20 };
21 21
22 /// Represents a position in a source code 22 /// Represents a position in a source code
23 23
24 class LexPosition 24 class LexPosition
25 { 25 {
26 immutable string filename; /// name of the source file 26 immutable string filename; /// name of the source file
................................................................................................................................................................................
34 static immutable LexPosition dummy; 34 static immutable LexPosition dummy;
35 static this(){ dummy = new immutable(LexPosition)("<unnamed>",0,0); } 35 static this(){ dummy = new immutable(LexPosition)("<unnamed>",0,0); }
36 } 36 }
37 37
38 unittest 38 unittest
39 { 39 {
40 auto p = new LexPosition("hello.cpp", 123, 45); 40 auto p = new LexPosition("hello.cpp", 123, 45);
41 auto q = new LexPosition("hello.cpp", 123, 46); <
42 41
43 assert_eq( p.filename, "hello.cpp" ); 42 assert_eq( p.filename, "hello.cpp" );
44 assert_eq( p.lineno, 123 ); 43 assert_eq( p.lineno, 123 );
45 assert_eq( p.column, 45 ); 44 assert_eq( p.column, 45 );
46 assert_eq( to!string(p), "hello.cpp:123:45" ); 45 assert_eq( to!string(p), "hello.cpp:123:45" );
47 assert_lt( p, q ); <
48 assert_ne( p, q ); <
49 46
50 assert( !__traits(compiles, new LexPosition) ); 47 assert( !__traits(compiles, new LexPosition) );
51 assert( !__traits(compiles, p.filename="foo") ); 48 assert( !__traits(compiles, p.filename="foo") );
52 assert( !__traits(compiles, p.lineno =789) ); 49 assert( !__traits(compiles, p.lineno =789) );
53 assert( !__traits(compiles, p.column =222) ); 50 assert( !__traits(compiles, p.column =222) );
> 51
> 52 auto q = new LexPosition("hello.cpp", 123, 46);
> 53 assert_lt( p, q );
> 54 assert_ne( p, q );
54 } 55 }
55 56
56 /// Represents a lexer token 57 /// Represents a lexer token
57 58
58 class Token 59 class Token
59 { 60 {
60 immutable LexPosition pos; /// Position where the token occurred in t 61 immutable LexPosition pos; /// Position where the token occurred in t
................................................................................................................................................................................
157 bool isLetter (dchar c) { return !isSpace(c) && !isSymbol(c); } 158 bool isLetter (dchar c) { return !isSpace(c) && !isSymbol(c); }
158 } 159 }
159 160
160 string readQuoted(const LexPosition pos){char[] buf; return readQuoted(p 161 string readQuoted(const LexPosition pos){char[] buf; return readQuoted(p
161 string readQuoted(const LexPosition pos, ref char[] buf) 162 string readQuoted(const LexPosition pos, ref char[] buf)
162 { 163 {
163 if( reader.empty ) 164 if( reader.empty )
164 throw new LexException(pos, "EOF found while lexing a qu | 165 throw genex!LexException(pos, "EOF found while lexing a
165 dchar c = reader.front; 166 dchar c = reader.front;
166 reader.popFront; 167 reader.popFront;
167 if( c == '"' ) 168 if( c == '"' )
168 return assumeUnique(buf); 169 return assumeUnique(buf);
169 if( c == '\\' && !reader.empty ) { 170 if( c == '\\' && !reader.empty ) {
170 if( reader.front=='"' ) { 171 if( reader.front=='"' ) {
171 reader.popFront; 172 reader.popFront;
................................................................................................................................................................................
223 } 224 }
224 } 225 }
225 } 226 }
226 227
227 unittest 228 unittest
228 { 229 {
229 assert( std.range.isForwardRange!(Lexer) ); 230 assert( std.range.isForwardRange!(Lexer) );
> 231 assert( is(ElementType!(Lexer) == Token) );
230 } 232 }
231 233
232 unittest 234 unittest
233 { 235 {
234 auto lex = lexerFromString("this is a \t\r\n pen :-( @@; "); 236 auto lex = lexerFromString("this is a \t\r\n pen :-( @@; ");
235 Token[] ts = std.array.array(lex); 237 Token[] ts = std.array.array(lex);
236 238