1 | /*=============================================================================
|
---|
2 | Boost.Wave: A Standard compliant C++ preprocessor library
|
---|
3 |
|
---|
4 | Grammar for universal character validation (see C++ standard: Annex E)
|
---|
5 |
|
---|
6 | http://www.boost.org/
|
---|
7 |
|
---|
8 | Copyright (c) 2001-2005 Hartmut Kaiser. Distributed under the Boost
|
---|
9 | Software License, Version 1.0. (See accompanying file
|
---|
10 | LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
---|
11 | =============================================================================*/
|
---|
12 | #if !defined(CONVERT_TRIGRAPHS_HK050403_INCLUDED)
|
---|
13 | #define CONVERT_TRIGRAPHS_HK050403_INCLUDED
|
---|
14 |
|
---|
15 | #include <boost/wave/cpplexer/cpplexer_exceptions.hpp>
|
---|
16 |
|
---|
17 | ///////////////////////////////////////////////////////////////////////////////
|
---|
18 | namespace boost {
|
---|
19 | namespace wave {
|
---|
20 | namespace cpplexer {
|
---|
21 | namespace impl {
|
---|
22 |
|
---|
23 | ///////////////////////////////////////////////////////////////////////////////
|
---|
24 | //
|
---|
25 | //
|
---|
26 | //
|
---|
27 | ///////////////////////////////////////////////////////////////////////////////
|
---|
28 | template <typename StringT>
|
---|
29 | inline bool
|
---|
30 | is_trigraph(StringT const& trigraph)
|
---|
31 | {
|
---|
32 | if (trigraph.size() < 3 || '?' != trigraph[0] || '?' != trigraph[1])
|
---|
33 | return false;
|
---|
34 |
|
---|
35 | switch (trigraph[2]) {
|
---|
36 | case '\'': case '=': case '/': case '(':
|
---|
37 | case ')': case '<': case '>': case '!':
|
---|
38 | case '-':
|
---|
39 | break;
|
---|
40 |
|
---|
41 | default:
|
---|
42 | return false;
|
---|
43 | }
|
---|
44 |
|
---|
45 | return true;
|
---|
46 | }
|
---|
47 |
|
---|
48 | ///////////////////////////////////////////////////////////////////////////////
|
---|
49 | //
|
---|
50 | // convert_trigraph
|
---|
51 | //
|
---|
52 | // The function convert_trigraph() converts a single trigraph character
|
---|
53 | // sequence into the corresponding character.
|
---|
54 | //
|
---|
55 | // If the given character sequence doesn't form a valid trigraph sequence
|
---|
56 | // no conversion is performed.
|
---|
57 | //
|
---|
58 | ///////////////////////////////////////////////////////////////////////////////
|
---|
59 | template <typename StringT>
|
---|
60 | inline StringT
|
---|
61 | convert_trigraph(StringT const &trigraph, int line, int column,
|
---|
62 | StringT const &file_name)
|
---|
63 | {
|
---|
64 | StringT result (trigraph);
|
---|
65 |
|
---|
66 | if (is_trigraph(trigraph)) {
|
---|
67 | switch (trigraph[2]) {
|
---|
68 | case '\'': result = "^"; break;
|
---|
69 | case '=': result = "#"; break;
|
---|
70 | case '/': result = "\\"; break;
|
---|
71 | case '(': result = "["; break;
|
---|
72 | case ')': result = "]"; break;
|
---|
73 | case '<': result = "{"; break;
|
---|
74 | case '>': result = "}"; break;
|
---|
75 | case '!': result = "|"; break;
|
---|
76 | case '-': result = "~"; break;
|
---|
77 | }
|
---|
78 | }
|
---|
79 | return result;
|
---|
80 | }
|
---|
81 |
|
---|
82 | ///////////////////////////////////////////////////////////////////////////////
|
---|
83 | //
|
---|
84 | // convert_trigraphs
|
---|
85 | //
|
---|
86 | // The function convert_trigraph() converts all trigraphs in the given
|
---|
87 | // string into the corresponding characters.
|
---|
88 | //
|
---|
89 | // If one of the given character sequences doesn't form a valid trigraph
|
---|
90 | // sequence no conversion is performed.
|
---|
91 | //
|
---|
92 | ///////////////////////////////////////////////////////////////////////////////
|
---|
93 | template <typename StringT>
|
---|
94 | inline StringT
|
---|
95 | convert_trigraphs(StringT const &value, int line, int column,
|
---|
96 | StringT const &file_name)
|
---|
97 | {
|
---|
98 | StringT result;
|
---|
99 | typename StringT::size_type pos = 0;
|
---|
100 | typename StringT::size_type pos1 = value.find_first_of ("?", 0);
|
---|
101 | if (StringT::npos != pos1) {
|
---|
102 | do {
|
---|
103 | result += value.substr(pos, pos1-pos);
|
---|
104 | StringT trigraph (value.substr(pos1));
|
---|
105 | if (is_trigraph(trigraph)) {
|
---|
106 | result += convert_trigraph(trigraph, line, column, file_name);
|
---|
107 | pos1 = value.find_first_of ("?", pos = pos1+3);
|
---|
108 | }
|
---|
109 | else {
|
---|
110 | result += value[pos1];
|
---|
111 | pos1 = value.find_first_of ("?", pos = pos1+1);
|
---|
112 | }
|
---|
113 | } while (StringT::npos != pos1);
|
---|
114 | result += value.substr(pos);
|
---|
115 | }
|
---|
116 | else {
|
---|
117 | result = value;
|
---|
118 | }
|
---|
119 | return result;
|
---|
120 | }
|
---|
121 |
|
---|
122 | ///////////////////////////////////////////////////////////////////////////////
|
---|
123 | } // namespace impl
|
---|
124 | } // namespace cpplexer
|
---|
125 | } // namespace wave
|
---|
126 | } // namespace boost
|
---|
127 |
|
---|
128 | #endif // !defined(CONVERT_TRIGRAPHS_HK050403_INCLUDED)
|
---|
129 |
|
---|
130 |
|
---|