summaryrefslogtreecommitdiffstats
path: root/kalzium/src/parser.cpp
blob: a23b107885a258ea63721e5a0cfe3d0837819d1a (plain)
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
/***************************************************************************
    copyright            : (C) 2005 by Inge Wallin
    email                : [email protected]
 ***************************************************************************/
/***************************************************************************
 *                                                                         *
 *   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; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/


#include <ctype.h>

#include <kdebug.h>

#include "parser.h"


Parser::Parser()
{
    start(TQString());
}


Parser::Parser(const TQString& _str)
{
    start(_str);
}


Parser::~Parser()
{
}


void
Parser::start(const TQString& _str)
{
    m_str = _str;

    if (_str.isNull()) {
	m_index     = -1;
	m_nextChar  = -1;
	m_nextToken = -1;
    }
    else {
	m_index     = 0;
	m_nextChar  = m_str.at(0).latin1();
	getNextToken();
    }
}


// ----------------------------------------------------------------


// Skip whitespace, and try to parse the following characters as an int.
//
// Return true if successful.  

bool
Parser::parseInt(int *_result)
{
    int  sign = 1;

    skipWhitespace();

    if (m_nextChar == '-') {
	sign = -1;
	getNextChar();
    }

    if (!isdigit(m_nextChar))
	return false;

    int  result = 0;
    while (isdigit(m_nextChar)) {
	result = result * 10 + (m_nextChar - '0');
	getNextChar();
    }

    *_result = sign * result;
    return true;
}


// Skip whitespace, and try to parse the following characters as a
// simple float of the type -?[0-9]+'.'?[0-9]*
//
// Return true if successful.  

bool
Parser::parseSimpleFloat(double *_result)
{
    double  sign = 1.0;

    skipWhitespace();
    if (m_nextChar == '-') {
	sign = -1.0;
	getNextChar();
    }

    if (!isdigit(m_nextChar))
	return false;

    double  result = 0.0;

    // The integer.
    while (isdigit(m_nextChar)) {
	result = result * 10.0 + (double) (m_nextChar - '0');
	getNextChar();
    }
    *_result = result;

    if (m_nextChar != '.' || !isdigit(getNextChar())) {
	*_result = sign * result;
	return true;
    }
	
    double  decimal = 0.1;
    while (isdigit(m_nextChar)) {
	result += decimal * (double) (m_nextChar - '0');
	decimal /= 10.0;
	getNextChar();
    }

    *_result = sign * result;
    return true;
}


// ----------------------------------------------------------------
//                           protected methods


int
Parser::getNextChar()
{
    if (m_index == -1)
	return -1;

    // If end of string, then reset the parser.
    if (m_index == (int) m_str.length()) {
	m_index    = -1;
	m_nextChar = -1;
    }
    else 
	m_nextChar = m_str.at(++m_index).latin1();

    // Take care of null-terminated strings.
    if (m_nextChar == 0) {
	m_index    = -1;
	m_nextChar = -1;
    }

    //kdDebug() << "Parser::getNextChar(): char = " << m_nextChar << endl;

    return m_nextChar;
}


int
Parser::skipWhitespace()
{
    while (TQChar(m_nextChar).isSpace())
	getNextChar();

    return m_nextChar;
}


// Get the next token.  This corresponds to the lexical analyzer of a
// standard parser, e.g as generated by lex.
//
// This basic parser supports integers and simple
// floats.  Reimplement this method to extend it.

int
Parser::getNextToken()
{
    int  saveIndex = m_index;

    skipWhitespace();
    if (isdigit(nextChar())) {
	// At this point we know that there is a valid number in the
	// string.  The only question now, is whether it is an int or a
	// float.

	parseInt(&m_intVal);

	skipWhitespace();
	if (nextChar() == '.') {
	    m_index = saveIndex;

	    // No need to check since we already know it is correct.
	    (void) parseSimpleFloat(&m_floatVal);
	    m_nextToken = FLOAT_TOKEN;
	}
	else
	    m_nextToken = INT_TOKEN;
    }

    else if (nextChar() != -1) {
	// Any character.
	m_nextToken = nextChar();
	getNextChar();
    }

    else
	// End of string.
	m_nextToken = -1;

    return m_nextToken;
}