~ubuntu-branches/ubuntu/quantal/mysql-workbench/quantal

« back to all changes in this revision

Viewing changes to library/sql-parser/yy_gen-tool/yy_gen-tool/grammar_tree_item.cpp

  • Committer: Package Import Robot
  • Author(s): Dmitry Smirnov
  • Date: 2012-03-01 21:57:30 UTC
  • Revision ID: package-import@ubuntu.com-20120301215730-o7y8av8y38n162ro
Tags: upstream-5.2.38+dfsg
ImportĀ upstreamĀ versionĀ 5.2.38+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "grammar_tree_item.h"
 
2
#include <algorithm>
 
3
#include <sstream>
 
4
 
 
5
Grammar_tree_item::Grammar_tree_item(const char *text/*, Item_kind item_kind*/)
 
6
:
 
7
  _text(text)/*,
 
8
  _item_kind(item_kind)*/
 
9
{
 
10
}
 
11
 
 
12
Grammar_tree_item::Grammar_tree_item()
 
13
:
 
14
  _text("")/*,
 
15
  _item_kind(item_kind)*/
 
16
{
 
17
}
 
18
 
 
19
Grammar_tree_item::~Grammar_tree_item(void)
 
20
{
 
21
}
 
22
 
 
23
std::string Grammar_tree_item::text() const
 
24
{
 
25
  return _text;
 
26
}
 
27
 
 
28
std::string Grammar_tree_item::text_unquoted() const
 
29
{
 
30
  if (!_text.empty() && _text[0] == '\'')
 
31
  {
 
32
    std::stringstream oss;
 
33
    oss << (int)_text[1];
 
34
    return oss.str();
 
35
  }
 
36
  else
 
37
    return _text;
 
38
}
 
39
 
 
40
void Grammar_tree_item::text(const std::string &text)
 
41
{
 
42
  _text= text;
 
43
}
 
44
 
 
45
bool Grammar_tree_item::empty() const
 
46
{
 
47
  return _text.empty();
 
48
}
 
49
 
 
50
bool Grammar_tree_item::non_prec_child_exists() const
 
51
{
 
52
  bool result= false;
 
53
  
 
54
  bool prev_item_was_prec_directive= false;
 
55
  for (Item_list::const_iterator i= _items.begin(); i != _items.end(); ++i)
 
56
  {
 
57
    if ((*i)->is_prec_directive())
 
58
      prev_item_was_prec_directive= true;
 
59
    else if (prev_item_was_prec_directive || (*i)->empty())
 
60
      prev_item_was_prec_directive= false;
 
61
    else
 
62
    {
 
63
      result= true;
 
64
      break;
 
65
    }
 
66
  }
 
67
 
 
68
  return result;
 
69
}
 
70
 
 
71
bool Grammar_tree_item::has_more_then_one_meaningful_child() const
 
72
{
 
73
  bool result= false;
 
74
  
 
75
  bool prev_item_was_prec_directive= false;
 
76
  bool meaningful_child_was_found= false;
 
77
  for (Item_list::const_iterator i= _items.begin(); i != _items.end(); ++i)
 
78
  {
 
79
    if ((*i)->is_prec_directive())
 
80
      prev_item_was_prec_directive= true;
 
81
    else if (prev_item_was_prec_directive)
 
82
      prev_item_was_prec_directive= false;
 
83
    else if (!meaningful_child_was_found)
 
84
      meaningful_child_was_found= true;
 
85
    else
 
86
    {
 
87
      result= true;
 
88
      break;
 
89
    }
 
90
  }
 
91
 
 
92
  return result;
 
93
}
 
94
 
 
95
bool Grammar_tree_item::is_terminal() const
 
96
{
 
97
  bool result= true;
 
98
 
 
99
  for (std::string::const_iterator i= _text.begin(); i != _text.end(); ++i)
 
100
  {
 
101
    if (isalpha(*i))
 
102
      if (islower(*i))
 
103
      {
 
104
        result= false;
 
105
        break;
 
106
      }
 
107
  }
 
108
 
 
109
  return result;
 
110
}
 
111
 
 
112
bool Grammar_tree_item::is_last_non_prec_child(const Grammar_tree_item *item) const
 
113
{
 
114
  char neq_count= 0;
 
115
 
 
116
  if (_items.back() != item)
 
117
  {
 
118
    // coping with msvs'2005 bug, its rbegin() implementation returns garbage instead of last element
 
119
    Item_list::const_iterator i= _items.end();
 
120
    do
 
121
    {
 
122
      --i;
 
123
 
 
124
      if (item == (*i))
 
125
        return (0 == neq_count);
 
126
      else if ((*i)->is_prec_directive())
 
127
        neq_count= 0;
 
128
      else if (1 < ++neq_count)
 
129
        return false;
 
130
    }
 
131
    while (i != _items.begin());
 
132
  }
 
133
 
 
134
  return (0 == neq_count);
 
135
}
 
136
 
 
137
bool Grammar_tree_item::is_prec_directive() const
 
138
{
 
139
  if (_text.empty() || ('%' != _text[0]))
 
140
    return false;
 
141
  return (0 == _stricmp("%prec", _text.c_str()));
 
142
}
 
143
 
 
144
/*
 
145
Item_kind Grammar_tree_item::item_kind() const
 
146
{
 
147
  return _item_kind;
 
148
}
 
149
*/
 
150
const Grammar_tree_item::Item_list * Grammar_tree_item::items() const
 
151
{
 
152
  return &_items;
 
153
}
 
154
 
 
155
void Grammar_tree_item::add_item_as_last(const Grammar_tree_item *item)
 
156
{
 
157
  _items.push_back(item);
 
158
}
 
159
 
 
160
void Grammar_tree_item::add_item_as_first(const Grammar_tree_item *item)
 
161
{
 
162
  _items.push_front(item);
 
163
}
 
164
 
 
165
void Grammar_tree_item::flush(std::ostream& os) const
 
166
{
 
167
  os << "<elem name= '" << _text << "'>";
 
168
  for (Item_list::const_iterator i= _items.begin(); i != _items.end(); ++i)
 
169
    (*i)->flush(os);
 
170
  os << "</elem>";
 
171
}