Overview
Examples
Screenshots
Comparisons
Applications
Download
Manual
Status & Roadmap
FAQ
Authors & License
Forums
Wiki
Funding Ultimate++
Search on this site











SourceForge.net Logo



class CParser

 

CParser is simple yet very useful lexical analyzer suitable for building descent parsers of languages with C-like syntax.

CParser works on '\0' terminated text in memory (not Stream), so to parse the file you have to load it into the memory first. Text has to exist during the time it is processed by CParser (no copy is made).

Many CParser methods can throw CParser::Error exception to indicate the failure to parse required symbol. When using CParser to build concrete parser, it is common to use this exception (preferably via ThrowError method) to indicate errors as well.

CParser objects cannot be transfered (copied).

 

 

struct Error : public Exc

 

Type used as CParser exception. Contains single String with error description.

 

Derived from Exc

 

Error(const char *s)

Constructor.

s

Error message.

 

 

struct Pos

 

Position in parsed text.

 

Pos(const char *ptr = NULL, int line = 1, String fn = Null)

Constructor.

ptr

Pointer to the position in the input text

line

Line number.

fn

Filename.

 

const char *ptr

Pointer to the position in the input text

 

int line

Line number.

 

String fn

Filename.

 

 

CParser(const char *ptr)

Constructs the CParser.

ptr

Pointer to the input text.

 

CParser(const char *ptr, const char *fn, int line = 1)

Constructs the CParser, with additional information for the text. The additional info can be used when reporting error.

ptr

Pointer to the input text.

fn

The name of file (in fact, can be anything, value is just stored).

line

First line number.

 

CParser()

Constructs the CParser. Input text has to be assigned using the SetPos method.

 

void ThrowError(const char *s)

Throws CParser::Error.

s

Error message.

 

void NoSkipSpaces()

Sets CParser to the mode where white-spaces are not automatically skipped, but have to be skipped by Spaces method.

 

void SkipSpaces()

Sets CParser to the mode where white-spaces are automatically skipped. First skip is performed when position in input text is assigned via constructor or SetPos, then the skip is performed after any symbol.

 

bool Spaces()

Skips white-spaces.

Return value

true if there were white-space to skip.

 

char PeekChar()

Returns the current single character.

Return value

Current character.

 

char GetChar()

Advances the position in the input text by one character.

Return value

Character at position before advancing it.

 

bool IsChar(char c)

Tests whether there is a specific character at the current position.

c

Character to test.

Return value

true on match.

 

bool IsChar2(char c1, char c2)

Tests whether there is a specific character pair at the current position.

c1

First character of pair.

c2

Second character of pair.

Return value

true on match.

 

bool IsChar3(char c1, char c2, char c3)

Test for a specific character triplet at the current position.

c1

First character of triplet.

c2

Second character of triplet.

c3

Third character of triplet.

Return value

true on match.

 

bool Char(char c)

Tests for a single character at the current position. If there is match, position is advanced.

c

Character to test.

Return value

true on match.

 

bool Char2(char c1, char c2)

Tests for a character pair at the current position. If there is match, position is advanced by two characters.

c1

First character of pair.

c2

Second character of pair.

Return value

true on match.

 

bool Char3(char c1, char c2, char c3)

Tests for a character triplet at the current position. If there is match, position is advanced by three characters.

c1

First character of triplet.

c2

Second character of triplet.

c3

Third character of triplet.

Return value

true on match.

 

void PassChar(char c) throw(Error)

Calls Char(c). If it returns false, throws error.

c

Character to test.

 

void PassChar2(char c1, char c2) throw(Error)

Calls Char2(c1, c2). If it returns false, throws error.

c1

First character of pair.

c2

Second character of pair.

 

void PassChar3(char c1, char c2, char c3) throw(Error)

Calls Char2(c1, c2, c3). If it returns false, throws the Error.

c1

First character of triplet.

c2

Second character of triplet.

c3

Third character of triplet.

 

bool Id(const char *s)

Tests for given C-like identifier. If there is match, advances position by strlen(s) characters.

s

Identifier.

Return value

true on match.

 

void PassId(const char *s) throw(Error)

Invokes the Id method with s as parameter. If it returns false, throws the Error.

s

Identifier.

 

bool IsId()

Tests whether there is any C-like identifier at the current position.

Return value

true if there is identifier.

 

String ReadId() throw(Error)

Reads C-like identifier from the current position. If there is none, an Error is thrown.

Return value

Identifier.

 

String ReadIdt() throw(Error)

Special variant of ReadId that considers different non-alphanumeric characters to be the part of identifier as long as they form C++ normal or template based type.

Return value

Identifier.

 

bool IsInt()

Test for integer at current position - there either must be digit, or '+' or '-' sign followed by any number of spaces and digit.

Return value

true if there is integer.

 

int ReadInt() throw(Error)

Reads the integer from the current position. If IsInt is false, throws an Error.

Return value

Integer.

 

bool IsNumber()

Tests for sign-less number at current position - there must be digit at current position.

Return value

true if there is number.

 

bool IsNumber(int base)

Tests for sign-less number with given base - there must be digit or letter 'A' - 'Z' or 'a' - 'z', where range is limit by acutal base (e.g. for base 12 letters 'a' 'A' 'b' 'B' are allowed).

base

Numeric base.

Return value

true if there is number with given numeric base.

 

uint32 ReadNumber(int base = 10) throw(Error)

Reads a number with the given numeric base. If IsNumber(base) is false, throws an Error.

base

Numeric base.

Return value

Number.

 

bool IsDouble()

Test for floating point number at current position - there either must be digit, or '+' or '-' sign followed by any number of spaces and digit.

Return value

true if there is the floating point number.

 

double ReadDouble() throw(Error)

Reads a floating point number with C based lexical rules.

Return value

Floating point number.

 

bool IsString()

Tests for C-like string literal at the current position. Same as IsChar('\"');

Return value

true when there is string literal.

 

String ReadOneString(bool chkend = false) throw(Error)

Reads C-like string literal from current position (follow C lexical rules, including escape codes). Literals on different lines are not concatenated (unlike C).

chkend

When false, ReadOneString is more permissive as it allows unterminated string literals - string is then also delimited by end of line or text.

Return value

String literal.

 

String ReadString(bool chkend = false) throw(Error)

Reads C-like string literal from current position (follow C lexical rules, including escape codes). Literals on different lines are concatenated (as in C).

chkend

When false, ReadOneString is more permissive as it allows unterminated string literals - string is then also delimited by end of line or text.

Return value

String literal.

 

String ReadOneString(int delim, bool chkend = false) throw(Error)

Reads C-like string literal from current position (follow C lexical rules, including escape codes) with different delimiter than '\"'. Literals on different lines are not concatenated (unlike C).

delim

Delimiter.

chkend

When false, ReadOneString is more permissive as it allows unterminated string literals - string is then also delimited by end of line or text.

Return value

String literal.

 

String ReadString(int delim, bool chkend = false) throw(Error)

Reads C-like string literal from current position (follow C lexical rules, including escape codes). with different delimiter than '\"'. Literals on different lines are concatenated (as in C).

delim

Delimiter.

chkend

When false, ReadOneString is more permissive as it allows unterminated string literals - string is then also delimited by end of line or text.

Return value

String literal.

 

void SkipTerm()

Skips a single symbol. Decimal numbers, identifiers and string literals are skipped as whole symbols, otherwise input position is advanced by 1 character.

 

const char *GetPtr()

Gets a pointer to the current position.

Return value

Pointer to current position.

 

Pos GetPos()

Gets the current position,.

Return value

Current position. It contains the pointer as well as the line number and the filename.

 

void SetPos(const CParser::Pos& p)

Sets the current position.

pos

New current position. Can be in different text than previously used in CParser.

 

bool IsEof() const

Test for the end of input text.

Return value

true when current position is a the end of input text ('\0' character).

 

operator bool() const

Return value

!IsEof().

 

int GetLine() const

Return value

Current line.

 

String GetFileName() const

Return value

Actual filename.

 

 

 

 

C-like string literal formatting

 

AsCString routines produce C-like literals (compatible with CParser) from character data:

 

String AsCString(const char *s, const char *end, int linemax = INT_MAX, const char *linepfx = NULL, bool smart = false)

Creates C-like literal.

s

Pointer to characters.

end

End of characters array ('\0' characters are allowed inside data).

linemax

Maximal length of line. If this is exceeded, ending "\"\n" and linepfx is inserted and literal continues on the new line.

linepfx

Pointer to zero-terminated text to be inserted at the beginning of the line when the line length is exceeded.

smart

true activates smarter breaking into lines - breaks at spaces are preferred.

Return value

C-like literal.

 

String AsCString(const char *s, int linemax = INT_MAX, const char *linepfx = NULL, bool smart = false)

Creates C-like literal from zero terminated character data. Same as AsCString(s, s + strlen(s), linemax, linepfx, smart).

 

String AsCString(const String& s, int linemax = INT_MAX, const char *linepfx = NULL, bool smart = false)

Creates C-like literal from String. String can contain zero characters. Same as AsCString(s.Begin(), s.End(), linemax, linepfx, smart).