1
/***************************************************************************
2
copyright : (C) 2005 by Inge Wallin
3
email : inge@lysator.liu.se
4
***************************************************************************/
5
/***************************************************************************
7
* This program is free software; you can redistribute it and/or modify *
8
* it under the terms of the GNU General Public License as published by *
9
* the Free Software Foundation; either version 2 of the License, or *
10
* (at your option) any later version. *
12
***************************************************************************/
27
Parser::Parser(const QString& _str)
39
Parser::start(const QString& _str)
50
m_nextChar = m_str.at(0).toLatin1();
56
// ----------------------------------------------------------------
59
// Skip whitespace, and try to parse the following characters as an int.
61
// Return true if successful.
64
Parser::parseInt(int *_result)
70
if (m_nextChar == '-') {
75
if (!isdigit(m_nextChar))
79
while (isdigit(m_nextChar)) {
80
result = result * 10 + (m_nextChar - '0');
84
*_result = sign * result;
89
// Skip whitespace, and try to parse the following characters as a
90
// simple float of the type -?[0-9]+'.'?[0-9]*
92
// Return true if successful.
95
Parser::parseSimpleFloat(double *_result)
100
if (m_nextChar == '-') {
105
if (!isdigit(m_nextChar))
111
while (isdigit(m_nextChar)) {
112
result = result * 10.0 + (double) (m_nextChar - '0');
117
if (m_nextChar != '.' || !isdigit(getNextChar())) {
118
*_result = sign * result;
122
double decimal = 0.1;
123
while (isdigit(m_nextChar)) {
124
result += decimal * (double) (m_nextChar - '0');
129
*_result = sign * result;
134
// ----------------------------------------------------------------
139
Parser::getNextChar()
141
// kDebug() << "Parser::getNextChar(): char = " << m_nextChar;
142
// kDebug() << "m_str.size() " << m_str.size() << " with m_str: " << m_str << " and m_index: " << m_index;
149
// If end of string, then reset the parser.
150
if (m_index == m_str.size()) {
155
m_nextChar = m_str.at(m_index).toLatin1();
157
// Take care of null-terminated strings.
158
if (m_nextChar == 0) {
168
Parser::skipWhitespace()
170
while (QChar(m_nextChar).isSpace())
177
// Get the next token. This corresponds to the lexical analyzer of a
178
// standard parser, e.g as generated by lex.
180
// This basic parser supports integers and simple
181
// floats. Reimplement this method to extend it.
184
Parser::getNextToken()
186
int saveIndex = m_index;
189
if (isdigit(nextChar())) {
190
// At this point we know that there is a valid number in the
191
// string. The only question now, is whether it is an int or a
197
if (nextChar() == '.') {
200
// No need to check since we already know it is correct.
201
(void) parseSimpleFloat(&m_floatVal);
202
m_nextToken = FLOAT_TOKEN;
205
m_nextToken = INT_TOKEN;
208
else if (nextChar() != -1) {
210
m_nextToken = nextChar();