Differences From Artifact [4e484112cd314d8d]:
- File
polemy/lex.d
- 2010-11-10 12:38:54 - part of checkin [38fcc662be] on branch trunk - cleaned up documentation comments (user: kinaba) [annotate]
To Artifact [a4ea515a0f014c9c]:
- File
polemy/lex.d
- 2010-11-13 02:48:58 - part of checkin [1c01f44f52] on branch trunk - simplepatternmatch (user: kinaba) [annotate]
13 13 template ExceptionWithPosition()
14 14 {
15 15 const LexPosition pos;
16 16 this( const LexPosition pos, string msg, string file=null, size_t line=0, Throwable next=null )
17 17 { super(sprintf!"[%s] %s"(pos, msg), file, line, next); this.pos = pos; }
18 18 }
19 19
20 -///
20 +/// Thrown when encountered an EOF in the middle of a lexical token
21 +
21 22 class UnexpectedEOF : Exception
22 23 {
23 24 mixin ExceptionWithPosition;
24 25 }
25 26
26 -///
27 +/// Thrown when encountered a lexical error
28 +
27 29 class LexException : Exception
28 30 {
29 31 mixin ExceptionWithPosition;
30 32 };
31 33
32 -/// Represents a position in a source code
34 +/// Represents a position in source codes
33 35
34 36 class LexPosition
35 37 {
36 38 immutable string filename; /// name of the source file
37 39 immutable int lineno; /// 1-origin
38 40 immutable int column; /// 1-origin
39 41
................................................................................
93 95 assert( !__traits(compiles, t.pos=p) );
94 96 assert( !__traits(compiles, t.str=789) );
95 97 assert( !__traits(compiles, t.quoted=true) );
96 98 }
97 99
98 100 /// Named Construtors for Lexer
99 101
100 -Lexer lexerFromFile(T...)( string filename, T rest )
102 +Lexer lexerFromFile(T...)( string filename, T ln_cn )
101 103 {
102 - return lexerFromString( std.file.readText(filename), filename, rest );
104 + return lexerFromString( std.file.readText(filename), filename, ln_cn );
103 105 }
104 106
105 -/// Named Construtors for Lexer
107 +/// Named Construtor for Lexer
106 108
107 109 LexerT!(PositionedReader!CharSeq) /* ddoc doesn't recognize auto return... bugzilla:2581 */
108 110 lexerFromString(CharSeq)( CharSeq str, string filename="<unnamed>", int lineno=1, int column=1 )
109 111 {
110 112 return new LexerT!(PositionedReader!CharSeq)(
111 113 PositionedReader!CharSeq(str, filename, lineno, column)
112 114 );
113 115 }
114 116
115 -/// Standard Lexer Type (all you have to know is that this is a forward range of Tokens)
117 +/// Standard Lexer Type (all you have to know is that this is a forward range of Tokens!)
116 118
117 119 alias LexerT!(PositionedReader!string) Lexer;
118 120
119 121 /// Lexer Implementation
120 122
121 123 class LexerT(Reader)
122 - if( isForwardRange!(Reader) && is(ElementType!(Reader) == dchar) )
124 + if( isForwardRange!(Reader) && is(ElementType!(Reader)==dchar) )
123 125 {
124 126 /// Range primitive
125 127 bool empty() /*@property*/
126 128 {
127 129 return current is null;
128 130 }
129 131
................................................................................
149 151 private: // implementation
150 152
151 153 Reader reader;
152 154 Token current;
153 155
154 156 invariant()
155 157 {
156 - assert( reader.empty || !std.ctype.isspace(reader.front) );
158 + assert( reader.empty || !isSpace(reader.front) );
157 159 }
158 160
159 161 this( Reader reader, Token current = null )
160 162 {
161 163 this.reader = reader;
162 164 readWhile!isSpace();
163 165 this.current = (current is null ? readNext() : current);
164 166 }
165 167
166 - public static {
168 + public static
169 + {
167 170 bool isSpace (dchar c) { return std.ctype.isspace(c)!=0; }
168 171 bool isSymbol (dchar c) { return 0x21<=c && c<=0x7f && !std.ctype.isalnum(c) && c!='_' && c!='\''; }
169 172 bool isSSymbol (dchar c) { return "()[]{};@".canFind(c); }
170 173 bool isMSymbol (dchar c) { return isSymbol(c) && !isSSymbol(c) && c!='"' && c!='#'; }
171 174 bool isLetter (dchar c) { return !isSpace(c) && !isSymbol(c); }
172 175 }
173 176
................................................................................
374 377 assert( lex.empty );
375 378 assert_eq( lexerFromString(`-@`).front.str, "-" );
376 379 }
377 380
378 381 /// Forward range for reader character by character,
379 382 /// keeping track of position information and caring \r\n -> \n conversion.
380 383
381 -private
382 384 struct PositionedReader(CharSeq)
383 - if( isForwardRange!(CharSeq) && is(ElementType!(CharSeq) == dchar) )
385 + if( isForwardRange!(CharSeq) && is(ElementType!(CharSeq)==dchar) )
384 386 {
385 387 CharSeq buffer;
386 388 string filename;
387 389 int lineno;
388 390 int column;
389 391
390 392 /// Range primitive