coan  6.0.1
A C/C++ Configuration Analyzer
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
canonical.h
Go to the documentation of this file.
1 #ifndef CANONICAL_H
2 #define CANONICAL_H
3 #pragma once
4 /***************************************************************************
5  * Copyright (C) 2007-2013 Mike Kinghan, imk@burroingroingjoing.com *
6  * All rights reserved. *
7  * *
8  * Contributed originally by Mike Kinghan, imk@burroingroingjoing.com *
9  * *
10  * Redistribution and use in source and binary forms, with or without *
11  * modification, are permitted provided that the following conditions *
12  * are met: *
13  * *
14  * Redistributions of source code must retain the above copyright *
15  * notice, this list of conditions and the following disclaimer. *
16  * *
17  * Redistributions in binary form must reproduce the above copyright *
18  * notice, this list of conditions and the following disclaimer in the *
19  * documentation and/or other materials provided with the distribution. *
20  * *
21  * Neither the name of Mike Kinghan nor the names of its contributors *
22  * may be used to endorse or promote products derived from this software *
23  * without specific prior written permission. *
24  * *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT *
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS *
28  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE *
29  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, *
30  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, *
31  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS *
32  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED *
33  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,*
34  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF *
35  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH *
36  * DAMAGE. *
37  * *
38  **************************************************************************/
39 
40 #include "chew.h"
41 #include "traits.h"
42 #include <string>
43 
44 struct symbol;
45 
52 struct macro_argument {};
54 
56 namespace innards {
57 
61  operator std::string () const {
62  return _canonical;
63  }
64 
65 protected:
66 
68  bool cxx() const;
69 
71  std::string _canonical;
72 };
73 
74 } // namespace innards
75 
81 template<class What>
83 
88  template<class CharSeq>
89  explicit canonical(CharSeq & seq) {
90  canonicalize(seq);
91  }
92 
99  template<class CharSeq>
100  explicit canonical(chewer<CharSeq> & chew) {
102  ">:[");
103  canonicalize(chew);
104  }
105 
106 private:
107 
114  template<class CharSeq>
115  void canonicalize(chewer<CharSeq> & chew);
116 
121  template<class CharSeq>
122  void canonicalize(CharSeq & seq) {
123  chewer<CharSeq> chew(cxx(),seq);
124  canonicalize(chew);
125  }
126 };
127 
129 
130 template<>
131 template<class CharSeq>
133 {
134  chew(greyspace);
135  if (!chew) {
136  return;
137  }
138  for ( ;; ) {
139  _canonical += *chew;
140  if (!++chew) {
141  break;
142  }
143  chew(continuation);
144  if (!chew) {
145  break;
146  }
147  size_t mark = size_t(chew);
148  chew(greyspace);
149  if (mark != size_t(chew)) {
150  _canonical += ' ';
151  }
152  if (!chew) {
153  break;
154  }
155  }
156  size_t len = _canonical.length();
157  if (_canonical[len - 1] == ' ') {
158  _canonical.resize(len - 1);
159  }
160 }
161 
162 template<>
163 template<class CharSeq>
165 {
166  int paren_balance = 0;
167  for (chew(c_comment); chew;
168  ++chew,chew(c_comment)) {
169  if (*chew == '(') {
170  ++paren_balance;
171  _canonical += '(';
172  continue;
173  }
174  if (*chew == ')') {
175  if (--paren_balance < 0) {
176  break;
177  }
178  _canonical += ')';
179  continue;
180  }
181  if (*chew == ',') {
182  if (paren_balance <= 0) {
183  break;
184  }
185  _canonical += ',';
186  continue;
187  }
188  if (!isspace(*chew)) {
189  _canonical += *chew;
190  continue;
191  }
192  if (_canonical.size()) {
193  auto last = _canonical.back();
194  if (!isspace(last) && (last == '#' || !ispunct(last))) {
195  _canonical += ' ';
196  }
197  }
198  }
199 }
200 
202 
203 #endif /* EOF */
template struct traits::is_random_access_char_sequence<T> exports a static const boolean member value...
Definition: traits.h:166
void back(size_t n)
Retreat the scanning position an amount.
Definition: chew.h:450
void canonicalize(CharSeq &seq)
Canonicalize from a CharSeq>
Definition: canonical.h:122
A tag class for parameterizing template class canonical<What>
Definition: canonical.h:53
chew_mode::c_comment const c_comment
An exemplar chew_mode::c_comment
Definition: chew.h:217
canonical(chewer< CharSeq > &chew)
Explicitly construct from a chewer<CharSeq>
Definition: canonical.h:100
chew_mode::continuation const continuation
An exemplar chew_mode::continuation
Definition: chew.h:213
chew_mode::greyspace const greyspace
An exemplar chew_mode::greyspace
Definition: chew.h:211
`template struct chewer<CharSeq> is a cursor-like type that is associated with a character-sequence t...
Definition: chew.h:248
template class canonical<What> encapsulates the canonical representation of values of type What...
Definition: canonical.h:82
void canonicalize(chewer< CharSeq > &chew)
Canonicalize from a chewer<CharSeq>
A base for classes representing canonical forms of various types.
Definition: canonical.h:59
struct symbol encapsulates a preprocessor symbol's state
Definition: symbol.h:58
std::string _canonical
String storing the canonical representation of a value.
Definition: canonical.h:71
bool cxx() const
Canonicalizing C/C++ source?
canonical(CharSeq &seq)
Explicitly construct from a character-sequence.
Definition: canonical.h:89