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 12 /// Exception from this module
13 13
14 14 class LexException : Exception
15 15 {
16 16 const LexPosition pos;
17 17
18 - this( const LexPosition pos, string msg, string file="", int line=0 )
19 - { super(sprintf!"[%s] %s"(pos, msg), file, line); this.pos = pos; }
18 + this( const LexPosition pos, string msg, string file="", int line=0, Throwable next=null )
19 + { super(sprintf!"[%s] %s"(pos, msg), file, line, next); this.pos = pos; }
20 20 };
21 21
22 22 /// Represents a position in a source code
23 23
24 24 class LexPosition
25 25 {
26 26 immutable string filename; /// name of the source file
................................................................................
34 34 static immutable LexPosition dummy;
35 35 static this(){ dummy = new immutable(LexPosition)("<unnamed>",0,0); }
36 36 }
37 37
38 38 unittest
39 39 {
40 40 auto p = new LexPosition("hello.cpp", 123, 45);
41 - auto q = new LexPosition("hello.cpp", 123, 46);
42 41
43 42 assert_eq( p.filename, "hello.cpp" );
44 43 assert_eq( p.lineno, 123 );
45 44 assert_eq( p.column, 45 );
46 45 assert_eq( to!string(p), "hello.cpp:123:45" );
47 - assert_lt( p, q );
48 - assert_ne( p, q );
49 46
50 47 assert( !__traits(compiles, new LexPosition) );
51 48 assert( !__traits(compiles, p.filename="foo") );
52 49 assert( !__traits(compiles, p.lineno =789) );
53 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 57 /// Represents a lexer token
57 58
58 59 class Token
59 60 {
60 61 immutable LexPosition pos; /// Position where the token occurred in the source
................................................................................
157 158 bool isLetter (dchar c) { return !isSpace(c) && !isSymbol(c); }
158 159 }
159 160
160 161 string readQuoted(const LexPosition pos){char[] buf; return readQuoted(pos,buf);}
161 162 string readQuoted(const LexPosition pos, ref char[] buf)
162 163 {
163 164 if( reader.empty )
164 - throw new LexException(pos, "EOF found while lexing a quoted-string");
165 + throw genex!LexException(pos, "EOF found while lexing a quoted-string");
165 166 dchar c = reader.front;
166 167 reader.popFront;
167 168 if( c == '"' )
168 169 return assumeUnique(buf);
169 170 if( c == '\\' && !reader.empty ) {
170 171 if( reader.front=='"' ) {
171 172 reader.popFront;
................................................................................
223 224 }
224 225 }
225 226 }
226 227
227 228 unittest
228 229 {
229 230 assert( std.range.isForwardRange!(Lexer) );
231 + assert( is(ElementType!(Lexer) == Token) );
230 232 }
231 233
232 234 unittest
233 235 {
234 236 auto lex = lexerFromString("this is a \t\r\n pen :-( @@; ");
235 237 Token[] ts = std.array.array(lex);
236 238