@@ -16,21 +16,23 @@ this( const LexPosition pos, string msg, string file=null, size_t line=0, Throwable next=null ) { super(sprintf!"[%s] %s"(pos, msg), file, line, next); this.pos = pos; } } -/// +/// Thrown when encountered an EOF in the middle of a lexical token + class UnexpectedEOF : Exception { mixin ExceptionWithPosition; } -/// +/// Thrown when encountered a lexical error + class LexException : Exception { mixin ExceptionWithPosition; }; -/// Represents a position in a source code +/// Represents a position in source codes class LexPosition { immutable string filename; /// name of the source file @@ -96,14 +98,14 @@ } /// Named Construtors for Lexer -Lexer lexerFromFile(T...)( string filename, T rest ) +Lexer lexerFromFile(T...)( string filename, T ln_cn ) { - return lexerFromString( std.file.readText(filename), filename, rest ); + return lexerFromString( std.file.readText(filename), filename, ln_cn ); } -/// Named Construtors for Lexer +/// Named Construtor for Lexer LexerT!(PositionedReader!CharSeq) /* ddoc doesn't recognize auto return... bugzilla:2581 */ lexerFromString(CharSeq)( CharSeq str, string filename="", int lineno=1, int column=1 ) { @@ -111,16 +113,16 @@ PositionedReader!CharSeq(str, filename, lineno, column) ); } -/// Standard Lexer Type (all you have to know is that this is a forward range of Tokens) +/// Standard Lexer Type (all you have to know is that this is a forward range of Tokens!) alias LexerT!(PositionedReader!string) Lexer; /// Lexer Implementation class LexerT(Reader) - if( isForwardRange!(Reader) && is(ElementType!(Reader) == dchar) ) + if( isForwardRange!(Reader) && is(ElementType!(Reader)==dchar) ) { /// Range primitive bool empty() /*@property*/ { @@ -152,9 +154,9 @@ Token current; invariant() { - assert( reader.empty || !std.ctype.isspace(reader.front) ); + assert( reader.empty || !isSpace(reader.front) ); } this( Reader reader, Token current = null ) { @@ -162,9 +164,10 @@ readWhile!isSpace(); this.current = (current is null ? readNext() : current); } - public static { + public static + { bool isSpace (dchar c) { return std.ctype.isspace(c)!=0; } bool isSymbol (dchar c) { return 0x21<=c && c<=0x7f && !std.ctype.isalnum(c) && c!='_' && c!='\''; } bool isSSymbol (dchar c) { return "()[]{};@".canFind(c); } bool isMSymbol (dchar c) { return isSymbol(c) && !isSSymbol(c) && c!='"' && c!='#'; } @@ -377,11 +380,10 @@ /// Forward range for reader character by character, /// keeping track of position information and caring \r\n -> \n conversion. -private struct PositionedReader(CharSeq) - if( isForwardRange!(CharSeq) && is(ElementType!(CharSeq) == dchar) ) + if( isForwardRange!(CharSeq) && is(ElementType!(CharSeq)==dchar) ) { CharSeq buffer; string filename; int lineno;