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
|
/***************************************************************************
copyright : (C) 2005 by Inge Wallin
email : inge@lysator.liu.se
***************************************************************************/
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
#ifndef PARSER_H
#define PARSER_H
#include "libkdeedu_science_export.h"
#include <QtCore/QString>
/**
* @class Parser
* This is a general purpose parser originally written by Inge Wallin.
*
* It is intended to be subclassed; see MoleculeParser.
*
* @author Inge Wallin
*/
class SCIENCE_EXPORT Parser
{
public:
/**
* Constructor
*/
Parser();
/**
* Constructor
*
* @param _str @ref start the parsing with @p _str
*/
explicit Parser(const QString& _str);
/**
* Destructor
*/
virtual ~Parser();
/**
* Start a new parse.
*/
void start(const QString& _str);
/**
* Peek at the next character;
*/
int nextChar() const
{
return m_nextChar;
}
/**
* Peek at the next token.
*/
int nextToken() const
{
return m_nextToken;
}
/**
* Get the value stored for different types of tokens.
*/
int intVal() const
{
return m_intVal;
}
float floatVal() const
{
return m_floatVal;
}
private:
// Try to parse some special datatypes.
bool parseInt(int *_result);
bool parseSimpleFloat(double *_result);
protected:
/**
* All characters are their own token value per default.
* Extend this list in your subclass to make a more advanced parser.
*/
static const int INT_TOKEN = 257;
/**
* All characters are their own token value per default.
* Extend this list in your subclass to make a more advanced parser.
*/
static const int FLOAT_TOKEN = 258;
/**
* Make the next character the current one.
*/
int getNextChar();
/**
* Make the next non-space character the current one.
*/
int skipWhitespace();
/**
* Fetches the next token.
*/
virtual int getNextToken();
private:
QString m_str;
int m_index;
int m_nextChar;
protected:
// Lexical analysis and token handling. These members need to be
// protected instead of private since we want to be able to
// reimplement getNextToken().
/**
* The next token to be used in the parser.
*/
int m_nextToken;
// Values for the respective token. These could be made into a
// union, but I don't think it is necessary to bother, since they
// are so few and we don't instantiate a lot of copies of the
// parser.
/**
* Valid if m_nextToken == INT_TOKEN
*/
int m_intVal; // Valid if m_nextToken == INT_TOKEN
/**
* Valid if m_nextToken == FLOAT_TOKEN
*/
double m_floatVal; // Valid if m_nextToken == FLOAT_TOKEN
};
#endif // PARSER_H
|