Differences From Artifact [514757ca55e253b4]:
- File
polemy/lex.d
- 2010-11-20 12:57:15 - part of checkin [3f6f41b558] on branch trunk - ast - table conversion (NOT AT ALL TESTED) (user: kinaba) [annotate]
To Artifact [480bb741b8b1612b]:
- File
polemy/lex.d
- 2010-11-20 16:35:14 - part of checkin [3464a035ec] on branch trunk - source code cleanup (user: kinaba) [annotate]
2 2 * Authors: k.inaba
3 3 * License: NYSL 0.9982 http://www.kmonos.net/nysl/
4 4 *
5 5 * Lexer for Polemy programming language.
6 6 */
7 7 module polemy.lex;
8 8 import polemy._common;
9 +import polemy.failure;
9 10 import std.file : readText;
10 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, Throwable next=null )
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 13 /// Represents a lexer token
76 14
77 15 class Token
78 16 {
79 17 immutable LexPosition pos; /// Position where the token occurred in the source
80 18 immutable string str; /// The token string itself
81 19 immutable bool quoted; /// Was it a "quoted" token or unquoted?