src
print.cc
Go to the documentation of this file.
1 #include <iostream>
2 
3 #include "src/codegen/print.h"
4 #include "src/conf/opt.h"
5 #include "src/globals.h"
7 
8 namespace re2c
9 {
10 
11 bool is_print (uint32_t c)
12 {
13  return c >= 0x20 && c < 0x7F;
14 }
15 
16 bool is_space (uint32_t c)
17 {
18  switch (c)
19  {
20  case '\t':
21  case '\f':
22  case '\v':
23  case '\n':
24  case '\r':
25  case ' ':
26  return true;
27  default:
28  return false;
29  }
30 }
31 
32 char hexCh(uint32_t c)
33 {
34  static const char * sHex = "0123456789ABCDEF";
35  return sHex[c & 0x0F];
36 }
37 
38 void prtChOrHex(std::ostream& o, uint32_t c)
39 {
40  if (opts->encoding.type () != Enc::EBCDIC
41  && (is_print (c) || is_space (c)))
42  {
43  o << '\'';
44  prtCh(o, c);
45  o << '\'';
46  }
47  else
48  {
49  prtHex(o, c);
50  }
51 }
52 
53 void prtHex(std::ostream& o, uint32_t c)
54 {
55  o << "0x";
56  const uint32_t cunit_size = opts->encoding.szCodeUnit ();
57  if (cunit_size >= 4)
58  {
59  o << hexCh (c >> 28u)
60  << hexCh (c >> 24u)
61  << hexCh (c >> 20u)
62  << hexCh (c >> 16u);
63  }
64  if (cunit_size >= 2)
65  {
66  o << hexCh (c >> 12u)
67  << hexCh (c >> 8u);
68  }
69  o << hexCh (c >> 4u)
70  << hexCh (c);
71 }
72 
73 void prtCh(std::ostream& o, uint32_t c)
74 {
75  const bool dot = opts->target == opt_t::DOT;
76 
77  switch (c)
78  {
79  case '\'':
80  o << (dot ? "'" : "\\'");
81  break;
82 
83  case '"':
84  o << (dot ? "\\\"" : "\"");
85  break;
86 
87  case '\n':
88  o << (dot ? "\\\\n" : "\\n");
89  break;
90 
91  case '\t':
92  o << (dot ? "\\\\t" : "\\t");
93  break;
94 
95  case '\v':
96  o << (dot ? "\\\\v" : "\\v");
97  break;
98 
99  case '\b':
100  o << (dot ? "\\\\b" : "\\b");
101  break;
102 
103  case '\r':
104  o << (dot ? "\\\\r" : "\\r");
105  break;
106 
107  case '\f':
108  o << (dot ? "\\\\f" : "\\f");
109  break;
110 
111  case '\a':
112  o << (dot ? "\\\\a" :"\\a");
113  break;
114 
115  case '\\':
116  o << "\\\\"; // both .dot and C/C++ code expect "\\"
117  break;
118 
119  default:
120  o << static_cast<char> (c);
121  break;
122  }
123 }
124 
125 void prtChOrHexForSpan(std::ostream& o, uint32_t c)
126 {
127  if (opts->encoding.type () != Enc::EBCDIC
128  && is_print (c)
129  && (c != ']'))
130  {
131  prtCh(o, c);
132  }
133  else
134  {
135  prtHex(o, c);
136  }
137 }
138 
139 void printSpan(std::ostream& o, uint32_t lb, uint32_t ub)
140 {
141  o << "[";
142  if ((ub - lb) == 1)
143  {
144  prtChOrHexForSpan(o, lb);
145  }
146  else
147  {
148  prtChOrHexForSpan(o, lb);
149  o << "-";
150  prtChOrHexForSpan(o, ub - 1);
151  }
152  o << "]";
153 }
154 
155 } // end namespace re2c
156 
void prtCh(std::ostream &o, uint32_t c)
Definition: print.cc:73
type_t type() const
Definition: enc.h:185
void printSpan(std::ostream &o, uint32_t lb, uint32_t ub)
Definition: print.cc:139
uint32_t szCodeUnit() const
Definition: enc.h:152
bool is_space(uint32_t c)
Definition: print.cc:16
bool is_print(uint32_t c)
Definition: print.cc:11
opt_t::target_t target
Definition: opt.h:118
Enc encoding
Definition: opt.h:118
Opt opts
Definition: opt.cc:7
void prtChOrHexForSpan(std::ostream &o, uint32_t c)
Definition: print.cc:125
void prtChOrHex(std::ostream &o, uint32_t c)
Definition: print.cc:38
char hexCh(uint32_t c)
Definition: print.cc:32
Definition: bitmap.cc:10
void prtHex(std::ostream &o, uint32_t c)
Definition: print.cc:53