summaryrefslogtreecommitdiffstats
path: root/kommander/widget/parsenode.h
blob: 42ffbf71ca01a97c9a3448b6bcf875457c8a2202 (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
/***************************************************************************
                    parsenode.cpp - Single parsed item
                             -------------------
    copyright          : (C) 2004      Michal Rudolf <[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.                                   *
 *                                                                         *
 ***************************************************************************/

#ifndef _HAVE_PARSENODE_H_
#define _HAVE_PARSENODE_H_

#include <tqstring.h>
 
namespace Parse
{
  enum Keyword {For, To, Step, End, While, Do, Foreach, In, If, Then, Else, Elseif, Endif, Switch, Case, 
    Break, Continue, Exit, Dot, Semicolon, Comma, Assign, Less, LessEqual, Greater, GreaterEqual, Equal, NotEqual, 
    Not, And, Or, False, True, LeftParenthesis, RightParenthesis, LeftBracket, RightBracket,
    Plus, Minus, Multiply, Divide, Mod, LastRealKeyword = Mod, Variable, Invalid};

  enum KeywordGroup {GroupComparison, GroupAdd, GroupMultiply, GroupMisc};
  enum ValueType {ValueString, ValueInt, ValueDouble, ValueValue = ValueDouble, ValueKeyword,
     ValueNone, ValueError};

  enum Mode{Execute, CheckOnly};

  enum Flow{FlowStandard, FlowContinue, FlowBreak, FlowExit};
}

class ParseNode {
public:
  /* Default constructor */
  ParseNode();
  /* Create node from string */
  ParseNode(const TQString& s);
  /* Create node from integer */
  ParseNode(int i);
  /* Create node from integer */
  ParseNode(uint i);
  /* Create node from double */
  ParseNode(double d);
  /* Create node from keyword */
  ParseNode(Parse::Keyword k);
  /* Create node from keyword and variable name */
  ParseNode(Parse::Keyword k, const TQString& s);
  /* Create error node with optional error message */
  static ParseNode error(const TQString& s);

  /* Return current type */
  Parse::ValueType type() const;
  /* Return current keyword if appropriate */
  Parse::Keyword keyword() const;
  /* Cast value to string */
  TQString toString() const;
  /* Cast value to integer */
  int toInt() const;
  /* Cast value to double */
  double toDouble() const;
  /* Cast value to bool */
  bool toBool() const;
  /* Check if a value is valid */
  bool isValid() const;
  /* Check if current value is a keyword */
  bool isKeyword() const;
  /* Check if current value is a given keyword */
  bool isKeyword(Parse::Keyword k) const;
  /* Check if current value is a variable */
  bool isVariable() const;
  /* Return the name of variable */
  TQString variableName() const;
  /* Return error message if applicable */
  TQString errorMessage() const;
  /* Calculate common type for two nodes */
  Parse::ValueType commonType(const ParseNode& p) const;
  /* Find common type and compare values */
  int compare(const ParseNode& p) const;    
  /* Various comparing functions */
  bool operator==(int i) const;
  bool operator==(bool b) const;
  bool operator==(const TQString& s) const;
  bool operator==(const ParseNode& p) const;
  bool operator!=(const ParseNode& p) const;
  bool operator>=(const ParseNode& p) const;
  bool operator<=(const ParseNode& p) const;
  bool operator>(const ParseNode& p) const;
  bool operator<(const ParseNode& p) const;
  /* set value as integer */
  void setValue(int i);
  /* set value as double */
  void setValue(double d);
  /* set value as string */
  void setValue(const TQString& s);
  /* set value as variable */
  void setVariable(const TQString& name);
  /* check if it is correct value */
  bool isValue() const;
  /* for setting some context information, f. e. for bug reporting */
  void setContext(int c);
  /* get current context */
  int context() const;
  
private:
  Parse::ValueType m_type;
  union {
    int m_int;
    double m_double;
    Parse::Keyword m_keyword;
  };
  TQString m_string;
  int m_context;
};


#endif