@@ -1,11 +1,12 @@ +/** + * Authors: k.inaba + * License: NYSL 0.9982 http://www.kmonos.net/nysl/ + * + * Lexer for Polemy programming language. + */ module polemy.lex; import polemy._common; -/* - * Author: k.inaba - * License: NYSL 0.9982 (http://www.kmonos.net/nysl/ - * Lexer for the polemy programming language - */ import std.file : readText; import std.string : munch; import std.ctype; @@ -13,11 +14,11 @@ /// Represents a position in a source code class LexPosition { - immutable string filename; ///< name of the source file - immutable int lineno; ///< line number: 1, 2, ... - immutable int column; ///< column: 1, 2, ... + immutable string filename; /// name of the source file + immutable int lineno; /// line number, 1, 2, ... + immutable int column; /// column, 1, 2, ... override string toString() const { return sprintf!"%s:%d:%d"(filename, lineno, column); } @@ -47,11 +48,11 @@ class Token { enum Kind {identifier, stringLiteral, number}; - immutable LexPosition pos; ///< position where the token occurred in the source - immutable string str; ///< the token string itself - immutable Kind kind; ///< which kind of token? + immutable LexPosition pos; /// position where the token occurred in the source + immutable string str; /// the token string itself + immutable Kind kind; /// which kind of token? mixin SimpleConstructor; mixin SimpleCompare; } @@ -87,25 +88,29 @@ /// Lexer is a forward range of Tokens class Lexer -{ +{ + /// Range primitive bool empty() /*@property*/ { return current is null; } + /// Range primitive Token front() /*@property*/ { return std.exception.enforce(current, "Lexer has already reached the end"); } + /// Range primitive void popFront() /*@property*/ { std.exception.enforce(current, "Lexer has already reached the end"); current = readNext(); } + /// Range primitive Lexer save() /*@property*/ { return new Lexer(buffer, filename, lineno, column, current); } @@ -329,15 +334,16 @@ unittest { //!! be sure to run the unittest on the root of the source directory auto lexf = lexerFromFile("polemy/lex.d"); + lexf = find!`a.str == "module"`(lexf); assert( lexf.front.str == "module", lexf.front.str ); assert( lexf.front.pos.filename == "polemy/lex.d" ); - assert( lexf.front.pos.lineno == 1 ); + assert( lexf.front.pos.lineno == 7 ); assert( lexf.front.pos.column == 1 ); lexf.popFront; assert( lexf.front.str == "polemy" ); - assert( lexf.front.pos.lineno == 1 ); + assert( lexf.front.pos.lineno == 7 ); assert( lexf.front.pos.column == 8 ); lexf.popFront; assert( lexf.front.str == "." ); lexf.popFront; @@ -345,9 +351,9 @@ lexf.popFront; assert( lexf.front.str == ";" ); lexf.popFront; assert( lexf.front.str == "import" ); - assert( lexf.front.pos.lineno == 2 ); + assert( lexf.front.pos.lineno == 8 ); assert( lexf.front.pos.column == 1 ); } unittest