Oblivion Mod:Oblivion Scan Source

The UESPWiki – Your source for The Elder Scrolls since 1995
Jump to: navigation, search

Rather than just accessing the script's text data directly we'll create a simple abstraction which will allow us to easily use different sources (eg: string, file, raw data, etc...). This will be used by the tokenizer of the compiler which turns the script text into a series of tokens ready for parsing.


sourcepos_t[edit]

A simple type used to specify a specific line and character position in the source.

  struct sourcepos_t {   
     dword LinePos;
     dword CharPos;
  }


CScanSource[edit]

This is the abstract base class that specific scan source implementations will be derived from.

  class CScanSource {

          /* Current line/character position in the source */
     sourcepos_t m_Position;

          /* Are we at the end of the source */
     virtual bool IsEOF (void) = 0;

          /* Access a character */
     virtual char LookAhead (void) = 0;
     virtual char GetChar   (void) = 0;

         /* Move the start location */
     virtual void MoveToStart (void) = 0;
  }


CScanSourceString[edit]

This class is the standard implementation of the CScanSource interface using a string buffer as the text source.


Using a Scan Source[edit]

The following simple example defines a string scan string and iterates through it one character at a time.

  CScanSource* ScanSource = new CScanSourceString("Some script text...");
  char         ParseChar;

  ScanSource->MoveToStart();

  while (!ScanSource->IsEOF()) {
     ParseChar = ScanSource->GetChar();
  }

By using the CScanSource interface abstraction this piece of sample code will work regardless of the exact implementation used.