Differences From Artifact [bee9af8d0f8f7348]:
- File
polemy/lex.d
- 2010-11-08 12:26:39 - part of checkin [80ff567c75] on branch trunk - Testing easyAST. (user: kinaba) [annotate]
To Artifact [50586221a28fd499]:
- File
polemy/lex.d
- 2010-11-08 14:59:30 - part of checkin [b985f3bf91] on branch trunk - refactored parser to change AST to ML-like one. (user: kinaba) [annotate]
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 class LexException : Exception
15 { 15 {
16 this( const LexPosition pos, string msg ) <
17 { super(sprintf!"%s [%s]"(msg, pos)); this.pos = pos; } <
18 const LexPosition pos; 16 const LexPosition pos;
> 17
> 18 private this( const LexPosition pos, string msg )
> 19 { super(sprintf!"%s [%s]"(msg, pos)); this.pos = pos; }
19 }; 20 };
20 21
21 /// Represents a position in a source code 22 /// Represents a position in a source code
22 23
23 class LexPosition 24 class LexPosition
24 { 25 {
25 immutable string filename; /// name of the source file 26 immutable string filename; /// name of the source file
26 immutable int lineno; /// line number, 1, 2, ... | 27 immutable int lineno; /// 1-origin
27 immutable int column; /// column, 1, 2, ... | 28 immutable int column; /// 1-origin
28 29
29 override string toString() const 30 override string toString() const
30 { return sprintf!"%s:%d:%d"(filename, lineno, column); } 31 { return sprintf!"%s:%d:%d"(filename, lineno, column); }
31 32
32 mixin SimpleConstructor; 33 mixin SimpleConstructor;
33 mixin SimpleCompare; 34 mixin SimpleCompare;
34 35
................................................................................................................................................................................
83 84
84 assert( !__traits(compiles, new Token) ); 85 assert( !__traits(compiles, new Token) );
85 assert( !__traits(compiles, t.pos=p) ); 86 assert( !__traits(compiles, t.pos=p) );
86 assert( !__traits(compiles, t.str=789) ); 87 assert( !__traits(compiles, t.str=789) );
87 assert( !__traits(compiles, t.quoted=true) ); 88 assert( !__traits(compiles, t.quoted=true) );
88 } 89 }
89 90
90 /// Named Construtor for Lexer | 91 /// Named Construtors for Lexer
91 92
92 auto lexerFromFile(T...)( string filename, T rest ) 93 auto lexerFromFile(T...)( string filename, T rest )
93 { 94 {
94 return lexerFromString( std.file.readText(filename), filename, rest ); 95 return lexerFromString( std.file.readText(filename), filename, rest );
95 } 96 }
96 97
97 /// Named Construtor for Lexer <
98 <
99 auto lexerFromString(CharSeq)( CharSeq str, string filename="<unnamed>", int lin 98 auto lexerFromString(CharSeq)( CharSeq str, string filename="<unnamed>", int lin
100 { 99 {
101 return new LexerT!(PositionedReader!CharSeq)( 100 return new LexerT!(PositionedReader!CharSeq)(
102 PositionedReader!CharSeq(str, filename, lineno, column) 101 PositionedReader!CharSeq(str, filename, lineno, column)
103 ); 102 );
104 } 103 }
105 104
106 /// Standard Lexer Type (all users have to know is that this is a forward range | 105 /// Standard Lexer Type (all you have to know is that this is a forward range of
107 106
108 alias LexerT!(PositionedReader!string) Lexer; 107 alias LexerT!(PositionedReader!string) Lexer;
109 108
110 /// Lexer Implementation 109 /// Lexer Implementation
111 110
112 class LexerT(Reader) 111 class LexerT(Reader)
113 if( isForwardRange!(Reader) && is(ElementType!(Reader) == dchar) ) 112 if( isForwardRange!(Reader) && is(ElementType!(Reader) == dchar) )