KFoundation  v2.1
A Better Foundation Library for C++
 All Classes Functions Variables Typedefs Enumerations Enumerator Groups Pages
Public Member Functions | Protected Member Functions | List of all members
kfoundation::StreamParser Class Reference

Packs ample of basic functionalities to implement any predictive parser. More...

#include <kfoundation/StreamParser.h>

Inheritance diagram for kfoundation::StreamParser:
Inheritance graph
[legend]

Public Member Functions

 StreamParser (Ref< InputStream > input)
 Constructor, creates a parser that reads symbols from the given stream. More...
 
bool testChar (const UChar ch)
 Tests if the given character is next in the stream. More...
 
bool testAlphabet ()
 Thest if any of the given characters is next in the stream. More...
 
bool testAlphanumeric ()
 Tests if the next character is alphanumeric.
 
bool testSpace ()
 Test if the next character is space. More...
 
bool testNewLine ()
 Test if the next character is a newline character '\n'.
 
bool testEndOfStream ()
 Checks if the end of stream is reached.
 
bool testSequence (RefConst< UString > str)
 Checks if the given string is next in the stream. More...
 
kf_int8_t readChar (UChar ch)
 Checks if the given character is next in the stream, and reads it if so. More...
 
kf_int8_t readAlphabet (UChar &ch)
 Checks if any of the given characters is next in the stream, and reads it if so. More...
 
kf_int8_t readNumeric (UChar &ch)
 Checks if the next character in the stream is a digit, and reads it if so. More...
 
kf_int8_t readNumeric (kf_int8_t &digit)
 Checks if the next character in the stream is a digit, and reads it and converts it to its equivalant numeric value. More...
 
kf_int8_t readAlphanumeric (UChar &ch)
 Checks if the next character in the stream is a letter or a digit, and reads it and converts it to its equivalant numeric value. More...
 
kf_int8_t readIdentifierBeginChar (UChar &ch)
 Checks if the next character in the stream is a valid identifier begining character, and if so, reads it. More...
 
kf_int8_t readIdentifierChar (UChar &ch)
 Checks if the next character in the stream is a valid identifier character, and if so, reads it. More...
 
kf_int8_t readSpace ()
 Checks if the next character in the stream is a space, and reads it if so. More...
 
kf_int8_t readNewLine ()
 Checks if the next character in the stream is newline '
' and reads it if so. More...
 
kf_int8_t readAny (UChar &ch)
 Reads any character next in the stream unless the end of stream is reached. More...
 
kf_int8_t readSequence (RefConst< UString > str)
 Checks if the next sequence of characters match the given string, and reads them if so. More...
 
kf_int64_t readAllAlphabet (Ref< OutputStream > storage)
 Reads all the next characters that are alphabet and appends them to the given argument. More...
 
kf_int64_t readAllAlphanumeric (Ref< OutputStream > storage)
 Reads all the next characters that are alphanumeric and appends them to the given argument. More...
 
kf_int64_t readAllNumeric (Ref< OutputStream > storage)
 Reads all the next numeric characters and appends them to the given parameter. More...
 
kf_int64_t readNumber (Ref< OutputStream > storage)
 Checks if the next character(s) represent a number (+|-)?[0..9]+(. More...
 
kf_int64_t readNumber (kf_int64_t &output)
 Checks if the next character(s) represents an integer number and if so, reads and converts it to it equivalant integer value. More...
 
kf_int64_t readNumber (double &output)
 Checks if the next character(s) represents a real number and if so, reads and converts it to it equivalant integer value. More...
 
kf_int64_t readIdentifier (Ref< OutputStream > storage)
 Reads all the next characters in the stream that form an identifier, and appends them to the given argument. More...
 
kf_int64_t readAllBeforeSpace (Ref< OutputStream > storage)
 Reads all character before a space (or end of stream) is encountered. More...
 
kf_int64_t readAllBeforeNewLine (Ref< OutputStream > storage)
 Reads all character before a new line (or end of stream) is encountered. More...
 
kf_int64_t readAllBeforeSpaceOrNewLine (Ref< OutputStream > storage)
 Reads all character before space or new line (or end of stream) is encountered. More...
 
kf_int64_t readAllBeforeChar (const UChar ch, Ref< OutputStream > storage)
 Read all characters before the given character (or end of stream) is encountered. More...
 
kf_int64_t readAllBeforeCharSkipEscaped (const UChar ch, const UChar escape, Ref< OutputStream > storage)
 Reads all characters before the given character, but skips all the matching ones immidiately after the given excape character. More...
 
kf_int64_t readAllBeforeSequence (RefConst< UString > sequence, Ref< OutputStream > storage)
 Reads all character before the given sequence. More...
 
kf_int64_t skipSpaces ()
 Consumes all the spaces next in the stream. More...
 
kf_int64_t skipSpacesAndNewLines ()
 Consumes all the spaces and new line characters next in the stream. More...
 
const CodeLocationgetCodeLocation () const
 Returns the CodeLocation object corresponding to the current position in the stream.
 
- Public Member Functions inherited from kfoundation::KFObject
bool equals (RefConst< KFObject > other) const
 Checks if this object is the same as the one reffered by the given refrence.
 

Protected Member Functions

virtual bool isValidIdentifierBeginChar (const wchar_t &ch) const
 Checks if the given argument is a valid begining of an identifier name. More...
 
virtual bool isValidIdentifierChar (const wchar_t &ch) const
 Checks if given argument is can be used after the first character of an identifier name. More...
 
virtual bool isSpace (const wchar_t &ch) const
 Checks if the given character is a white space. More...
 

Detailed Description

Packs ample of basic functionalities to implement any predictive parser.

It can be directly instantiated, or be extended to implement a parser for the desired grammar. Protected methods isValidIdentifierBeginChar(), isValidIdentifierChar(), and isSpace() can be overriden if needed.

There are two set of methods for reading the input. The readXXX methods consume the stream characters if successful, and return the number read character. If faild the stream will remain at its current position and return value will be zero. The testXXX methods will test if the given argument is there next in the stream without consuming anything.

Constructor & Destructor Documentation

kfoundation::StreamParser::StreamParser ( Ref< InputStream input)

Constructor, creates a parser that reads symbols from the given stream.

Parameters
inputThe stream to parse.

Member Function Documentation

bool kfoundation::StreamParser::isSpace ( const wchar_t &  ch) const
protectedvirtual

Checks if the given character is a white space.

The default implementation returns `ch == ' '`. Override to customize the parser based on your particular needs.

Parameters
chThe character to be checked.
bool kfoundation::StreamParser::isValidIdentifierBeginChar ( const wchar_t &  ch) const
protectedvirtual

Checks if the given argument is a valid begining of an identifier name.

Override to customize the parser based on your particular needs. The default implementation returns `isAplhabet(ch) || ch == '_'`.

Parameters
chThe character to be tested.
bool kfoundation::StreamParser::isValidIdentifierChar ( const wchar_t &  ch) const
protectedvirtual

Checks if given argument is can be used after the first character of an identifier name.

Override to customize the parser based on your particular needs. The default implementation returns `isAlphanumeric(ch) || ch == '_'`.

Parameters
chThe character to be checked.
kf_int64_t kfoundation::StreamParser::readAllAlphabet ( Ref< OutputStream storage)

Reads all the next characters that are alphabet and appends them to the given argument.

Parameters
storageOutput, read octets will be appended to it.
Returns
The number of octets read.
kf_int64_t kfoundation::StreamParser::readAllAlphanumeric ( Ref< OutputStream storage)

Reads all the next characters that are alphanumeric and appends them to the given argument.

Parameters
storageOutput, read octets will be appended to it.
Returns
The number of octets read.
kf_int64_t kfoundation::StreamParser::readAllBeforeChar ( const UChar  ch,
Ref< OutputStream storage 
)

Read all characters before the given character (or end of stream) is encountered.

Parameters
tThe character to read until.
storageOutput, the read characters will be appended to it.
Returns
The number of read octets.
kf_int64_t kfoundation::StreamParser::readAllBeforeCharSkipEscaped ( const UChar  ch,
const UChar  escape,
Ref< OutputStream storage 
)

Reads all characters before the given character, but skips all the matching ones immidiately after the given excape character.

Parameters
tThe character to read until.
escapeThe escape character.
storageOutput, the read characters will be appended to it.
Returns
The number of read octets.
kf_int64_t kfoundation::StreamParser::readAllBeforeNewLine ( Ref< OutputStream storage)

Reads all character before a new line (or end of stream) is encountered.

Parameters
storageOutput, the octets read will be appended to it.
Returns
The number of read octets.
kf_int64_t kfoundation::StreamParser::readAllBeforeSequence ( RefConst< UString >  str,
Ref< OutputStream storage 
)

Reads all character before the given sequence.

Parameters
strThe sequence of characters to read until.
storageOutput, the read characters will be appended to it.
Returns
The number of read octets.
kf_int64_t kfoundation::StreamParser::readAllBeforeSpace ( Ref< OutputStream storage)

Reads all character before a space (or end of stream) is encountered.

Parameters
storageOutput, the octets read will be appended to it.
Returns
The number of read octets.
kf_int64_t kfoundation::StreamParser::readAllBeforeSpaceOrNewLine ( Ref< OutputStream storage)

Reads all character before space or new line (or end of stream) is encountered.

Parameters
storageOutput, the octets read will be appended to it.
Returns
The number of read octets.
kf_int64_t kfoundation::StreamParser::readAllNumeric ( Ref< OutputStream storage)

Reads all the next numeric characters and appends them to the given parameter.

Parameters
storageOutput, read octets will be appended to it.
Returns
The number of read octets.
kf_int8_t kfoundation::StreamParser::readAlphabet ( UChar ch)

Checks if any of the given characters is next in the stream, and reads it if so.

Parameters
charsThe list of character to check against.
nThe number of characters in the given list.
octetsIf not NULL, the read octets are written on the buffer pointed by this argument. Default value is NULL.
Returns
The number of read octets. Checks if the next character in the stream is an alphabet, and reads it if so.
Parameters
chOutput, will be assigned with the read character.
octetsIf not NULL, the read octets are written on the buffer pointed by this argument. Default value is NULL.
Returns
The number of read octets.
kf_int8_t kfoundation::StreamParser::readAlphanumeric ( UChar ch)

Checks if the next character in the stream is a letter or a digit, and reads it and converts it to its equivalant numeric value.

Parameters
chOutput, will be assigned to the read character.
octetsIf not NULL, the read octets are written on the buffer pointed by this argument. Default value is NULL.
Returns
The number of read octets.
kf_int8_t kfoundation::StreamParser::readAny ( UChar ch)

Reads any character next in the stream unless the end of stream is reached.

Parameters
chOutput, will be assigned to the character read.
octetsIf not NULL, the read octets are written on the buffer pointed by this argument. Default value is NULL.
Returns
The number of octets read.
kf_int8_t kfoundation::StreamParser::readChar ( UChar  ch)

Checks if the given character is next in the stream, and reads it if so.

Parameters
tThe character to be expected next in the stream.
octetsIf not NULL, the read octets are written on the buffer pointed by this argument. Default value is NULL.
Returns
The number of read octets.
kf_int64_t kfoundation::StreamParser::readIdentifier ( Ref< OutputStream storage)

Reads all the next characters in the stream that form an identifier, and appends them to the given argument.

Override isValidIdentifierBeginChar() and isValidIdentifierChar() to customize the behavior of this method.

Parameters
storageOutput, the octets read will be appended to it.
Returns
The number of read octets.
kf_int8_t kfoundation::StreamParser::readIdentifierBeginChar ( UChar ch)

Checks if the next character in the stream is a valid identifier begining character, and if so, reads it.

Override isValidIdentifierBeginChar() to customize the behavior of this method.

Parameters
chOutput, will be assigned to the read character.
octetsIf not NULL, the read octets are written on the buffer pointed by this argument. Default value is NULL.
Returns
The number of read octets.
kf_int8_t kfoundation::StreamParser::readIdentifierChar ( UChar ch)

Checks if the next character in the stream is a valid identifier character, and if so, reads it.

Override isValidIdentifierBeginChar() to customize the behavior of this method.

Parameters
chOutput, will be assigned to the read character.
octetsIf not NULL, the read octets are written on the buffer pointed by this argument. Default value is NULL.
Returns
The number of read octets.
kf_int8_t kfoundation::StreamParser::readNewLine ( )

Checks if the next character in the stream is newline '
' and reads it if so.

Returns
The number of read character.
kf_int64_t kfoundation::StreamParser::readNumber ( Ref< OutputStream storage)

Checks if the next character(s) represent a number (+|-)?[0..9]+(.

[0..9]*)?, and reads them if so. Read characters are appended to the given parameter.

Parameters
storageOutput, the read octets will be appended to it.
Returns
The number of octets read.
kf_int64_t kfoundation::StreamParser::readNumber ( kf_int64_t output)

Checks if the next character(s) represents an integer number and if so, reads and converts it to it equivalant integer value.

Parameters
outputThe integer number read.
Returns
The number of octets read.
kf_int64_t kfoundation::StreamParser::readNumber ( double &  output)

Checks if the next character(s) represents a real number and if so, reads and converts it to it equivalant integer value.

Parameters
outputThe integer number read.
Returns
The number of octets read.
kf_int8_t kfoundation::StreamParser::readNumeric ( UChar ch)

Checks if the next character in the stream is a digit, and reads it if so.

Parameters
chOutput, will be assigned with the read character.
octetsIf not NULL, the read octets are written on the buffer pointed by this argument. Default value is NULL.
Returns
The number of read octets.
kf_int8_t kfoundation::StreamParser::readNumeric ( kf_int8_t digit)

Checks if the next character in the stream is a digit, and reads it and converts it to its equivalant numeric value.

Parameters
digitOutput, will be assigned to the numeric equivalant of the read character.
Returns
The number of read octets.
kf_int8_t kfoundation::StreamParser::readSequence ( RefConst< UString >  str)

Checks if the next sequence of characters match the given string, and reads them if so.

Parameters
strThe sequence of characters to check agains.
Returns
The number of octets read.
kf_int8_t kfoundation::StreamParser::readSpace ( )

Checks if the next character in the stream is a space, and reads it if so.

Override isSpace() to customzie the behavior of this function.

Returns
The number of read octers.
kf_int64_t kfoundation::StreamParser::skipSpaces ( )

Consumes all the spaces next in the stream.

Returns
The number of read octets.
kf_int64_t kfoundation::StreamParser::skipSpacesAndNewLines ( )

Consumes all the spaces and new line characters next in the stream.

Returns
The number of read octets.
bool kfoundation::StreamParser::testAlphabet ( )

Thest if any of the given characters is next in the stream.

Parameters
charsThe list of characters to test against.
nThe number of characters in the given list. Thest if the next character is alphabet.
bool kfoundation::StreamParser::testChar ( const UChar  ch)

Tests if the given character is next in the stream.

Parameters
chThe character to test.
bool kfoundation::StreamParser::testSequence ( RefConst< UString >  str)

Checks if the given string is next in the stream.

Parameters
strThe sequence of characters to check against.
bool kfoundation::StreamParser::testSpace ( )

Test if the next character is space.

Override isSpace() to customize the behavior of this function.


The documentation for this class was generated from the following files: