Object | +---DLexer
The DLexer class implements a (simple) lexical scanner. The scanner scans a text stream for constant strings and regular expressions. There are two sets of services nextString+nextExpression and checkString+checkExpression. The first set check for the string or expression and if it is a match, the scanner location is moved to the next location in the source. The second set of services only return the result of the match, the client must call the service 'next' to move the scanner location.
#include <stdio.h> #include "ofc/DLexer.h" #include "ofc/DFile.h" int main(int argc, char *argv[]) { DLexer *lexer = [DLexer new]; DFile *file = [DFile new]; char name[] = "example.lex"; if ([file open :name :"r"]) { if ([lexer source :file :name]) { while (![lexer isEof]) { if ([lexer nextExpression :"#[a-z]+"]) // Check and process the possibility of a directive { printf("Scanned directive:%s\n", [lexer text]); [lexer nextLine]; // Skip remaining of the line } else if ([lexer checkString :"var"]) // Check for string 'var' { printf("Scanned string:%s\n", [lexer text]); [lexer next]; // Set 'var' processed [lexer nextWhiteSpace]; // Check and process optional whiteSpace [lexer nextExpression :"[.;]"]; // Check and process the optional end of line marker [lexer nextLine]; } else { [lexer nextExpression :"."]; // Unexpected, scan first char and .. printf("Unexpected data on line:%s\n", [lexer text]); // .. report it [lexer nextLine]; // Skip remaining } } } else printf("Could not start the lexer with \"%s\".\n", name); [file close]; } else printf("Could not open \"%s\" for scanning:%d\n", name, [file error]); [lexer free]; // Cleanup [file free]; return 0; }