Class DLexer

Inherits from:
Object
Declared in:
DLexer.h

Class Hierarchy

    Object
      |
      +---DLexer

Class Description

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.

Example:
#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;
}
Last modified:
16-Aug-2008 (DLexer.h)

Instance Variables

private DList *_sources
the stack with the sources
private DSource *_source
the source for the scanner
private DText *_text
the last scanned text
private BOOL _eoff
is end of file reached ?
private int _scanned
the length of the scanned text
private DRegEx *_whiteSpace
the white space expression
private DRegEx *_expression
the normally used regular expression
private BOOL _caseSensitive
is the lexer case sensitive (def: YES)

Method Index


generated 06-Sep-2008 by ObjcDoc 3.0.0