1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
|
// Copyright (c) 2002-2007 Peter Karlsson
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 2
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#ifndef __LEXER_H
#define __LEXER_H
#include <string>
#if !defined(HAVE_IMPLICIT_NAMESPACE)
using namespace std;
#endif
/**
* Linked list of lexical tokens. This class describes a linked list of tokens
* returned from the lexical analyzer, which is run on the template files.
* This class will never be instantiated by itself, only subclasses of it will
* ever be instantiated.
*/
class Token
{
public:
/** Standard destructor. This will destruct the entire linked list pointed
* to by this node. Must only be called on the first node in the list.
*/
virtual ~Token() { delete m_next_p; };
/** Poor man's RTTI. Check if this is a literal string token. */
virtual bool IsLiteral() const { return false; }
/** Poor man's RTTI. Check if this is a section token. */
virtual bool IsSection() const { return false; }
/** Poor man's RTTI. Check if this is a variable token. */
virtual bool IsVariable() const { return false; }
/** Poor man's RTTI. Check if this is a setting token. */
virtual bool IsSetting() const { return false; }
/** Retrieve pointer to next token in the list. */
Token *Next() const { return m_next_p; }
/** Parse a line. To parse a template line, this method is called.
*
* @param line A line of the template file to parse.
* @param in_settings Set to true if parsing a settings section.
* @param error Set to true if a parsing error occurs.
* @return A linked list of tokens describing this line.
*/
static Token *Parse(const string &line, bool in_settings, bool &error);
protected:
/** Standard constructor. Not accessible to any class, except for its
* subclasses, to prevent it from being misused, as this class should
* never be instantiated directly. */
Token() : m_next_p(NULL) {};
private:
/** Pointer to next token in the list. */
Token *m_next_p;
/** Helper method for parser. @see Parse */
static Token *Parse2(const string line, bool &error);
};
/** Lexical token describing a literal string. */
class Literal : public Token
{
public:
/** Poor man's RTTI. Check if this is a literal string token. */
virtual bool IsLiteral() const { return true; }
/** Retrieve the string literal described by this token. */
string GetLiteral() const { return m_literal; }
/** Check if this literal token should be followed by a linebreak. */
bool HasLineBreak() const { return m_linebreak; }
private:
friend class Token;
/** Constructor. Initialize the string literal. */
Literal(string s, bool lineend = false);
/** String literal described by this token. */
string m_literal;
/** String literal ends with line break. */
bool m_linebreak;
};
/** Lexical token describing a report section. */
class Section : public Token
{
public:
/** List of sections known. */
enum sectiontype
{
// Common sections
Common, IfEmpty, IfNotNews, IfNews,
// Toplist sections
Original, Quoters, Writers, TopNets, TopDomains, Received,
Subjects, Programs, Week, Day,
// Configuration sections
Localization
};
/** Poor man's RTTI. Check if this is a section token. */
virtual bool IsSection() const { return true; }
/** Retrieve report section described by this token. */
sectiontype GetSection() const { return m_section; }
private:
friend class Token;
/** Constructor. Initialize the section from the string describing it. */
Section(string s, bool &error);
/** Report section described by this token. */
sectiontype m_section;
};
/** Lexical token describing a report variable. */
class Variable : public Token
{
public:
/** List of variable types known. */
enum variabletype
{
Version, Copyright, IfReceived, IfAreas, Place, Name, Written,
BytesWritten, Ratio, BytesTotal, BytesQuoted,
TotalAreas, TotalPeople, TotalNets, TotalDomains,
TotalSubjects, TotalPrograms, EarliestReceived, LastReceived,
EarliestWritten, LastWritten, BytesOriginal, PerMessage, Fidonet,
TopDomain, Received, ReceiveRatio, Subject, Program, Bar
};
/** Poor man's RTTI. Check if this is a variable token. */
virtual bool IsVariable() const { return true; }
/** Retrieve variable type described by this token. */
variabletype GetType() const { return m_type; }
/** Retrieve display width set for this token, or zero if none. */
streamsize GetWidth() const { return m_width; }
private:
friend class Token;
/** Constructor. Initialize the variable from the string describing it. */
Variable(string s, bool &error);
/** Helper method to the constructor. */
void SetWidth(string);
/** Helper method to the constructor. */
void SetVariable(string, bool &error);
/** Variable described by this token. */
variabletype m_type;
/** Display width set for this token. */
streamsize m_width;
};
/** Lexical token describing a setting. */
class Setting : public Token
{
public:
/** List of settings known. */
enum settingtype
{
Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday
};
/** Poor man's RTTI. Check if this is a setting token. */
virtual bool IsSetting() const { return true; }
/** Retrieve setting type described by this token. */
settingtype GetType() const { return m_type; }
/** Retrieve setting data described by this token. */
string GetValue() const { return m_value; }
private:
friend class Token;
/** Constructor. Initialize the setting from the string describing it. */
Setting(string s, bool &error);
/** Helper method to the constructor. */
void SetType(string, bool &error);
/** Helper method to the constructor. */
void SetValue(string s) { m_value = s; }
/** Setting described by this token. */
settingtype m_type;
/** Value of the setting. */
string m_value;
};
#endif
|