OpenStructure
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
python_context_parser.hh
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 // This file is part of the OpenStructure project <www.openstructure.org>
3 //
4 // Copyright (C) 2008-2011 by the OpenStructure authors
5 //
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License as published by the Free
8 // Software Foundation; either version 3.0 of the License, or (at your option)
9 // any later version.
10 // This library is distributed in the hope that it will be useful, but WITHOUT
11 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13 // details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with this library; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 //------------------------------------------------------------------------------
19 #ifndef PYTHON_CONTEXT_PARSER_HH
20 #define PYTHON_CONTEXT_PARSER_HH
21 
22 /*
23  Author: Marco Biasini
24 */
25 
26 #include "python_tokenizer.hh"
27 #include <vector>
28 #include <iostream>
29 
30 
31 namespace ost { namespace gui {
32 
34 {
35 public:
37  : parent_(NULL)
38  { }
39 
41  { this->Clear(); }
42 
43  bool IsRoot() const
44  {
45  return parent_ == NULL;
46  }
47 
48  const Range& GetRange() const
49  { return range_; }
50 
51  const std::vector<PythonContext*>& GetSubContexts() const
52  { return sub_contexts_; }
53 
54  void SetRange( const Range& range)
55  { range_ = range; }
56 
57  const PythonContext* GetParent() const
58  { return parent_; }
59 
60  void AddToken(const PythonToken& token)
61  {
62  range_.length = token.GetRange().End()-range_.location;
63  token_list_.push_back(token);
64  }
65 
66  void Print()
67  {
68  for (size_t i = 0; i < token_list_.size(); ++i) {
69  std::cout << token_list_[i].GetValue().toStdString() << std::endl;
70  }
71  }
72 
73  bool FindLastToken(PythonToken::Type type,
74  const QString& value,
75  PythonToken& token);
76 
77  bool FindFirstToken(PythonToken::Type type,
78  const QString& value,
79  PythonToken& token);
80 
81  void AddContext(PythonContext* context)
82  {
83  sub_contexts_.push_back(context);
84  sub_contexts_.back()->parent_ = this;
85  }
86 
87  bool GetContextAtLocation(size_t location, PythonContext*& context);
88 
89  void Clear();
90 private:
91 
92  Range range_;
93  std::vector<PythonContext*> sub_contexts_;
94  PythonContext* parent_;
95  std::vector<PythonToken> token_list_;
96 };
97 
99 {
100 public:
102 
104  {
105  delete root_;
106  }
107 
108  void Print()
109  {
110  this->PrintRecursively(root_,0);
111  }
112 
113  void Parse(const QString& command);
114 
115  const QString& GetCommand() const
116  { return command_; }
117 
118  bool GetContextAtLocation(size_t location,
119  PythonContext*& context)
120  {
121  return root_->GetContextAtLocation(location,context);
122  }
123 
124 protected:
125  void Parse(PythonContext*& context);
126 
127 
128 
129 private:
130  void PrintRecursively(const PythonContext* context, int level)
131  {
132  typedef std::vector<PythonContext*>::const_iterator const_it;
133  if (context->GetRange().length > 0) {
134  for (int j = 0; j < level; ++j)
135  std::cout<< "-" ;
136  try {
137  std::cout << command_.mid(context->GetRange().location,
138  context->GetRange().length).toStdString()
139  << std::endl;;
140  } catch(...) { }
141  }
142 
143  const_it i = context->GetSubContexts().begin();
144  for (;i!=context->GetSubContexts().end(); ++i) {
145  this->PrintRecursively(*i,level+1);
146  }
147  }
148  QString command_;
149  PythonTokenizer tokenizer_;
150  PythonContext* root_;
151 };
152 
153 }}
154 #endif
const QString & GetCommand() const
const std::vector< PythonContext * > & GetSubContexts() const
const Range & GetRange() const
const Range & GetRange() const
#define DLLEXPORT_OST_GUI
void AddToken(const PythonToken &token)
bool GetContextAtLocation(size_t location, PythonContext *&context)
void AddContext(PythonContext *context)
ImageStateConstModIPAlgorithm< ClearFnc > Clear
Definition: clear.hh:47
size_t End() const
void SetRange(const Range &range)
const PythonContext * GetParent() const