File indexing completed on 2024-04-21 03:41:37
0001 /* 0002 SPDX-FileCopyrightText: 2005 Inge Wallin <inge@lysator.liu.se> 0003 SPDX-License-Identifier: GPL-2.0-or-later 0004 */ 0005 0006 #ifndef PARSER_H 0007 #define PARSER_H 0008 0009 #include "science_export.h" 0010 0011 #include <QString> 0012 0013 /** 0014 * @class Parser 0015 * This is a general purpose parser originally written by Inge Wallin. 0016 * 0017 * It is intended to be subclassed; see MoleculeParser. 0018 * 0019 * @author Inge Wallin 0020 */ 0021 class SCIENCE_EXPORT Parser 0022 { 0023 public: 0024 /** 0025 * Constructor 0026 */ 0027 Parser(); 0028 0029 /** 0030 * Constructor 0031 * 0032 * @param _str @ref start the parsing with @p _str 0033 */ 0034 explicit Parser(const QString &_str); 0035 0036 /** 0037 * Destructor 0038 */ 0039 virtual ~Parser(); 0040 0041 /** 0042 * Start a new parse. 0043 */ 0044 void start(const QString &_str); 0045 0046 /** 0047 * Peek at the next character; 0048 */ 0049 int nextChar() const 0050 { 0051 return m_nextChar; 0052 } 0053 0054 /** 0055 * Peek at the next token. 0056 */ 0057 int nextToken() const 0058 { 0059 return m_nextToken; 0060 } 0061 0062 /** 0063 * Get the value stored for different types of tokens. 0064 */ 0065 int intVal() const 0066 { 0067 return m_intVal; 0068 } 0069 float floatVal() const 0070 { 0071 return m_floatVal; 0072 } 0073 0074 private: 0075 // Try to parse some special datatypes. 0076 bool parseInt(int *_result); 0077 bool parseSimpleFloat(double *_result); 0078 0079 protected: 0080 /** 0081 * All characters are their own token value per default. 0082 * Extend this list in your subclass to make a more advanced parser. 0083 */ 0084 static const int INT_TOKEN = 257; 0085 0086 /** 0087 * All characters are their own token value per default. 0088 * Extend this list in your subclass to make a more advanced parser. 0089 */ 0090 static const int FLOAT_TOKEN = 258; 0091 0092 /** 0093 * Make the next character the current one. 0094 */ 0095 int getNextChar(); 0096 0097 /** 0098 * Make the next non-space character the current one. 0099 */ 0100 int skipWhitespace(); 0101 0102 /** 0103 * Fetches the next token. 0104 */ 0105 virtual int getNextToken(); 0106 0107 private: 0108 QString m_str; 0109 int m_index; 0110 int m_nextChar; 0111 0112 protected: 0113 // Lexical analysis and token handling. These members need to be 0114 // protected instead of private since we want to be able to 0115 // reimplement getNextToken(). 0116 0117 /** 0118 * The next token to be used in the parser. 0119 */ 0120 int m_nextToken; 0121 0122 // Values for the respective token. These could be made into a 0123 // union, but I don't think it is necessary to bother, since they 0124 // are so few and we don't instantiate a lot of copies of the 0125 // parser. 0126 0127 /** 0128 * Valid if m_nextToken == INT_TOKEN 0129 */ 0130 int m_intVal; // Valid if m_nextToken == INT_TOKEN 0131 0132 /** 0133 * Valid if m_nextToken == FLOAT_TOKEN 0134 */ 0135 double m_floatVal; // Valid if m_nextToken == FLOAT_TOKEN 0136 }; 0137 0138 #endif // PARSER_H