File: lexer.cpp

package info (click to toggle)
turqstat 3.0-2
  • links: PTS
  • area: main
  • in suites: lenny, squeeze
  • size: 2,112 kB
  • ctags: 1,328
  • sloc: cpp: 17,929; perl: 252; makefile: 223; ansic: 75; sh: 16
file content (276 lines) | stat: -rw-r--r-- 8,846 bytes parent folder | download
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// 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.

#include <stdlib.h>

#include "utility.h"
#include "lexer.h"

Token *Token::Parse(const string &line, bool in_settings, bool &error)
{
    // Parse a single line.
    if (';' == line[0])
    {
        if (';' == line[1] && !in_settings)
        {
            // Double semi-colon means that line starts with a semi-colon
            return Parse2(line.substr(1), error);
        }
        else
        {
            // Line is a comment
            return NULL;
        }
    }
    else if ('[' == line[0])
    {
        // Line is a section name
        string::size_type endbracket = line.find(']');
        if (string::npos == endbracket)
        {
            error = true;
            return NULL;
        }
        else
        {
            return new Section(line.substr(1, endbracket - 1), error);
        }
    }
	else if (in_settings)
	{
		if (line != "")
		{
			return new Setting(line, error);
		}
	}

    // Call real parser
    return Parse2(line, error);
}

Token *Token::Parse2(string line, bool &error)
{
    // Parse a regular line
    string::size_type pos = 0;
    Token *head = NULL;

    if ('@' == line[0])
    {
        // Find end of variable and parse it
        pos = line.find('@', 1);
        if (1 == pos)
        {
            // Literal @ sign.
            pos = line.find('@', 2);
            if (string::npos == pos)
            {
                head = new Literal(line.substr(1), true);
            }
            else
            {
                head = new Literal(line.substr(1, pos - 1));
            }
        }
        else if (string::npos == pos)
        {
            // Incorrect syntax
            error = true;
            return NULL;
        }
        else
        {
            // Real variable
            head = new Variable(line.substr(1, pos - 1), error);
            pos ++;
        }
    }
    else
    {
        // String literal up to next variable or end-of-line
        pos = line.find('@');
        if (string::npos == pos)
        {
            head = new Literal(line, true);
        }
        else
        {
            head = new Literal(line.substr(0, pos));
        }
    }

    if (pos != string::npos && pos < line.length())
    {
        head->m_next_p = Parse2(line.substr(pos), error);
    }
	else
	{
		if (head->IsVariable())
		{
			// If the last token on a line is a variable, add a final
			// linebreak token
			head->m_next_p = new Literal("", true);
		}
	}

    return head;
}

Literal::Literal(string s, bool lineend)
    : Token(),
      m_literal(s),
      m_linebreak(lineend)
{
    // If the line ends with a single backslash, remove the linebreak.
    // If the line ends with double backslashes, change them into one.
    if (lineend)
    {
        string::size_type len = s.length();
        if (len && m_literal[len - 1] == '\\')
        {
            // No matter what we erase the last character if it was a
            // backslash.
            m_literal.erase(len - 1);
            m_linebreak = m_literal[len - 2] == '\\';
        }
    }
}

Section::Section(string s, bool &error)
    : Token()
{
    // Translate token string into enumeration value.
    if (0 == fcompare(s, "Common"))          m_section = Common;
    else if (0 == fcompare(s, "IfEmpty"))    m_section = IfEmpty;
    else if (0 == fcompare(s, "IfNotNews"))  m_section = IfNotNews;
    else if (0 == fcompare(s, "IfNews"))     m_section = IfNews;
    else if (0 == fcompare(s, "Original"))   m_section = Original;
    else if (0 == fcompare(s, "Quoters"))    m_section = Quoters;
    else if (0 == fcompare(s, "Writers"))    m_section = Writers;
    else if (0 == fcompare(s, "TopNets"))    m_section = TopNets;
    else if (0 == fcompare(s, "TopDomains")) m_section = TopDomains;
    else if (0 == fcompare(s, "Received"))   m_section = Received;
    else if (0 == fcompare(s, "Subjects"))   m_section = Subjects;
    else if (0 == fcompare(s, "Programs"))   m_section = Programs;
    else if (0 == fcompare(s, "Week"))       m_section = Week;
    else if (0 == fcompare(s, "Day"))        m_section = Day;
	else if (0 == fcompare(s, "Localization")) m_section = Localization;
    else error = true;
}

Variable::Variable(string s, bool &error)
    : Token(),
      m_width(0)
{
    // Find parameters
    string::size_type bracket1 = s.find('[');
    string::size_type bracket2 = s.find(']', bracket1 + 1);
    string::size_type namelen = bracket1;

    while (bracket1 != string::npos && bracket2 != string::npos &&
           bracket2 > bracket1 + 1)
    {
        // Valid indexing
        if (isdigit(s[bracket1 + 1]))
        {
            SetWidth(s.substr(bracket1 + 1, bracket2 - bracket1 - 1));
        }
        else
        {
            error = true;
        }

        // Find next bracket, if any
        bracket1 = s.find('[', bracket2 + 1);
        bracket2 = s.find(']', bracket1 + 1);
    }

    if (namelen != string::npos)
    {
        SetVariable(s.substr(0, namelen), error);
    }
    else
    {
        SetVariable(s, error);
    }
}

void Variable::SetWidth(string s)
{
    m_width = atoi(s.c_str());
}

void Variable::SetVariable(string s, bool &error)
{
    // Translate token string into enumeration value.
    if (0 == fcompare(s, "Version"))                 m_type = Version;
    else if (0 == fcompare(s, "Copyright"))          m_type = Copyright;
    else if (0 == fcompare(s, "IfReceived"))         m_type = IfReceived;
    else if (0 == fcompare(s, "IfAreas"))            m_type = IfAreas;
    else if (0 == fcompare(s, "Place"))              m_type = Place;
    else if (0 == fcompare(s, "Name"))               m_type = Name;
    else if (0 == fcompare(s, "Written"))            m_type = Written;
    else if (0 == fcompare(s, "BytesWritten"))       m_type = BytesWritten;
    else if (0 == fcompare(s, "Ratio"))              m_type = Ratio;
    else if (0 == fcompare(s, "BytesTotal"))         m_type = BytesTotal;
    else if (0 == fcompare(s, "BytesQuoted"))        m_type = BytesQuoted;
    else if (0 == fcompare(s, "TotalAreas"))         m_type = TotalAreas;
    else if (0 == fcompare(s, "TotalPeople"))        m_type = TotalPeople;
    else if (0 == fcompare(s, "TotalNets"))          m_type = TotalNets;
    else if (0 == fcompare(s, "TotalDomains"))       m_type = TotalDomains;
    else if (0 == fcompare(s, "TotalSubjects"))      m_type = TotalSubjects;
    else if (0 == fcompare(s, "TotalPrograms"))      m_type = TotalPrograms;
    else if (0 == fcompare(s, "EarliestReceived"))   m_type = EarliestReceived;
    else if (0 == fcompare(s, "LastReceived"))       m_type = LastReceived;
    else if (0 == fcompare(s, "EarliestWritten"))    m_type = EarliestWritten;
    else if (0 == fcompare(s, "LastWritten"))        m_type = LastWritten;
    else if (0 == fcompare(s, "BytesOriginal"))      m_type = BytesOriginal;
    else if (0 == fcompare(s, "PerMessage"))         m_type = PerMessage;
    else if (0 == fcompare(s, "Fidonet"))            m_type = Fidonet;
    else if (0 == fcompare(s, "TopDomain"))          m_type = TopDomain;
    else if (0 == fcompare(s, "Received"))           m_type = Received;
    else if (0 == fcompare(s, "ReceiveRatio"))       m_type = ReceiveRatio;
    else if (0 == fcompare(s, "Subject"))            m_type = Subject;
    else if (0 == fcompare(s, "Program"))            m_type = Program;
    else if (0 == fcompare(s, "Bar"))                m_type = Bar;
    else error = true;
}

Setting::Setting(string s, bool &error)
{
	// Find divider
	string::size_type equals = s.find('=');
	if (equals == string::npos)
	{
		error = true;
	}
	else
	{
		SetType(s.substr(0, equals), error);
		SetValue(s.substr(equals + 1));
	}	
}

void Setting::SetType(string s, bool &error)
{
	// Translate setting string into enumeration value
	if (0 == fcompare(s, "Mon"))			m_type = Monday;
	else if (0 == fcompare(s, "Tue"))		m_type = Tuesday;
	else if (0 == fcompare(s, "Wed"))		m_type = Wednesday;
	else if (0 == fcompare(s, "Thu"))		m_type = Thursday;
	else if (0 == fcompare(s, "Fri"))		m_type = Friday;
	else if (0 == fcompare(s, "Sat"))		m_type = Saturday;
	else if (0 == fcompare(s, "Sun"))		m_type = Sunday;
	else error = true;
}