/src/lexer.d
D | 189 lines | 132 code | 40 blank | 17 comment | 14 complexity | 93dc3a522553822edffeabfa840be317 MD5 | raw file
Possible License(s): MIT
- module lexer;
- import std.stdio;
- import std.string;
- import std.conv;
- enum TOK { UNKNOWN, EOF, BLANK, KEYWORD, STRING, NUMBER, ASSIGN, OPENBRACE, CLOSEDBRACE, SEMICOLON, COMMENT };
- struct token {
- TOK type;
- char[] string;
- int line;
- }
- class Lexer {
- char[] block;
- int i;
- uint line=1;
- this( char[] aBlock ) {
- block = aBlock;
- }
- bool keyword(inout token t) {
- while(true) {
- if( inPattern(block[i], "0-9a-zA-Z_") ) {
- t.string ~= block[i];
- }
- else return(true);
- i++;
- if(i>block.length) return(false);
- }
- }
- bool string(inout token t) {
- while(true) {
- if( block[i] != '\"' ) {
- t.string ~= block[i];
- }
- else { i++; return(true); }
- i++;
- if(i>block.length) return(false);
- }
- }
- bool integer(inout token t) {
- while(true) {
- if( inPattern(block[i], "0-9--") ) {
- t.string ~= block[i];
- }
- else { i++; return(true); }
- i++;
- if(i>block.length) return(false);
- }
- }
- bool comment( inout token t ) {
- while(true) {
- if( block[i] != '\n' ) {
- t.string ~= block[i];
- }
- else { i++; return(true); }
- i++;
- if(i>block.length) return(false);
- }
- }
- /*
- bool block( inout tokern t ) {
- int level=0;
- while(true) {
- if( block[i] == '{' ) level++;
- if( block[i] == '}' ) level--;
- if( block[i] != '}' && level >= 0 ) {
- t.string ~= block[i];
- }
- else { i++; return(true); }
- i++;
- if( i>block.length ) return(false);
- }
- }
- */
- void nextToken(inout token t) {
- t.string="";
- while(true) {
- switch(block[i]) {
- case 0:
- case 0x1A:
- t.type=TOK.EOF;
- return;
- case 'a' : case 'b' : case 'c' : case 'd' :
- case 'e' : case 'f' : case 'g' : case 'h' :
- case 'i' : case 'j' : case 'k' : case 'l' :
- case 'm' : case 'n' : case 'o' : case 'p' :
- case 'q' : case 'r' : case 's' : case 't' :
- case 'u' : case 'v' : case 'w' : case 'x' :
- case 'y' : case 'z' :
- case 'A' : case 'B' : case 'C' : case 'D' :
- case 'E' : case 'F' : case 'G' : case 'H' :
- case 'I' : case 'J' : case 'K' : case 'L' :
- case 'M' : case 'N' : case 'O' : case 'P' :
- case 'Q' : case 'R' : case 'S' : case 'T' :
- case 'U' : case 'V' : case 'W' : case 'X' :
- case 'Y' : case 'Z' : case '_' :
- t.type=TOK.KEYWORD;
- keyword(t);
- return;
- case '\r' :
- i++;
- continue;
- case '\n' :
- line++;
- t.line=line;
- i++;
- continue;
- case ' ' :
- case '\t' :
- case '\v' :
- t.type=TOK.BLANK;
- i++;
- continue;
- case '{' :
- t.type=TOK.OPENBRACE;
- i++;
- return;
- case '}' :
- t.type=TOK.CLOSEDBRACE;
- i++;
- return;
- case '"' :
- t.type=TOK.STRING;
- i++;
- string(t);
- return;
- case '=' :
- i++;
- t.type=TOK.ASSIGN;
- return;
- case ';' :
- i++;
- t.type=TOK.COMMENT;
- comment(t);
- return;
- case '0' : case '1' : case '2' : case '3' :
- case '4' : case '5' : case '6' : case '7' :
- case '8' : case '9' : case '-' :
- t.type=TOK.NUMBER;
- integer(t);
- return;
- default :
- t.type = TOK.UNKNOWN;
- i++;
- return;
- }
- }
- }
- }
-