| 1 |
// file : cutl/compiler/sloc-counter.txx |
| 2 |
// copyright : Copyright (c) 2009-2013 Code Synthesis Tools CC |
| 3 |
// license : MIT; see accompanying LICENSE file |
| 4 |
|
| 5 |
#include <cctype> // std::isspace |
| 6 |
|
| 7 |
namespace cutl |
| 8 |
{ |
| 9 |
namespace compiler |
| 10 |
{ |
| 11 |
template <typename C> |
| 12 |
sloc_counter<C>:: |
| 13 |
sloc_counter (code_stream<C>& out) |
| 14 |
: out_ (out), |
| 15 |
count_ (0), |
| 16 |
prev_ ('\0'), |
| 17 |
code_counted_ (false), |
| 18 |
construct_ (con_code) |
| 19 |
{ |
| 20 |
} |
| 21 |
|
| 22 |
template <typename C> |
| 23 |
void sloc_counter<C>:: |
| 24 |
put (C c) |
| 25 |
{ |
| 26 |
construct old (construct_); |
| 27 |
|
| 28 |
switch (construct_) |
| 29 |
{ |
| 30 |
case con_code: |
| 31 |
{ |
| 32 |
code (c); |
| 33 |
break; |
| 34 |
} |
| 35 |
case con_c_com: |
| 36 |
{ |
| 37 |
c_comment (c); |
| 38 |
break; |
| 39 |
} |
| 40 |
case con_cxx_com: |
| 41 |
{ |
| 42 |
cxx_comment (c); |
| 43 |
break; |
| 44 |
} |
| 45 |
case con_char_lit: |
| 46 |
{ |
| 47 |
char_literal (c); |
| 48 |
break; |
| 49 |
} |
| 50 |
case con_string_lit: |
| 51 |
{ |
| 52 |
string_literal (c); |
| 53 |
break; |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
// There are cases when a previous character has been already |
| 58 |
// 'used' and therefore can not be used again. Good example |
| 59 |
// would be '/* *//'. Here, the second slash doesn't start |
| 60 |
// C++ comment since it was already used by C comment. |
| 61 |
// |
| 62 |
// To account for this we are going to set prev_ to '\0' when |
| 63 |
// the mode changes. |
| 64 |
// |
| 65 |
|
| 66 |
prev_ = (old == construct_) ? c : '\0'; |
| 67 |
|
| 68 |
out_.put (c); |
| 69 |
} |
| 70 |
|
| 71 |
template <typename C> |
| 72 |
void sloc_counter<C>:: |
| 73 |
unbuffer () |
| 74 |
{ |
| 75 |
} |
| 76 |
|
| 77 |
template <typename C> |
| 78 |
void sloc_counter<C>:: |
| 79 |
code (C c) |
| 80 |
{ |
| 81 |
bool count (true); |
| 82 |
|
| 83 |
switch (c) |
| 84 |
{ |
| 85 |
case '/': |
| 86 |
{ |
| 87 |
if (prev_ == '/') |
| 88 |
{ |
| 89 |
construct_ = con_cxx_com; |
| 90 |
count = false; |
| 91 |
} |
| 92 |
else |
| 93 |
{ |
| 94 |
// This slash can be a beginning of a comment but we |
| 95 |
// yet have no way to know. Will have to examine it later |
| 96 |
// (see below). |
| 97 |
// |
| 98 |
count = false; |
| 99 |
} |
| 100 |
|
| 101 |
break; |
| 102 |
} |
| 103 |
case '*': |
| 104 |
{ |
| 105 |
if (prev_ == '/') |
| 106 |
{ |
| 107 |
construct_ = con_c_com; |
| 108 |
count = false; |
| 109 |
} |
| 110 |
break; |
| 111 |
} |
| 112 |
case '\'': |
| 113 |
{ |
| 114 |
construct_ = con_char_lit; |
| 115 |
break; |
| 116 |
} |
| 117 |
case '"': |
| 118 |
{ |
| 119 |
construct_ = con_string_lit; |
| 120 |
break; |
| 121 |
} |
| 122 |
case '\n': |
| 123 |
{ |
| 124 |
code_counted_ = false; // Reset for a new line. |
| 125 |
count = false; |
| 126 |
break; |
| 127 |
} |
| 128 |
default: |
| 129 |
{ |
| 130 |
if (std::isspace (c)) |
| 131 |
count = false; |
| 132 |
break; |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
if (!code_counted_) |
| 137 |
{ |
| 138 |
if (count) |
| 139 |
{ |
| 140 |
count_++; |
| 141 |
code_counted_ = true; |
| 142 |
} |
| 143 |
else if (prev_ == '/' && construct_ == con_code) |
| 144 |
{ |
| 145 |
// This condition accounts for the fact that we cannot count |
| 146 |
// '/' right away since it can be a beginning of a comment. |
| 147 |
// |
| 148 |
count_++; |
| 149 |
code_counted_ = (c != '\n'); |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
template <typename C> |
| 155 |
void sloc_counter<C>:: |
| 156 |
c_comment (C c) |
| 157 |
{ |
| 158 |
switch (c) |
| 159 |
{ |
| 160 |
case '/': |
| 161 |
{ |
| 162 |
if (prev_ == '*') |
| 163 |
construct_ = con_code; |
| 164 |
break; |
| 165 |
} |
| 166 |
case '\n': |
| 167 |
{ |
| 168 |
code_counted_ = false; // Reset for a new line. |
| 169 |
break; |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
template <typename C> |
| 175 |
void sloc_counter<C>:: |
| 176 |
cxx_comment (C c) |
| 177 |
{ |
| 178 |
switch (c) |
| 179 |
{ |
| 180 |
case '\n': |
| 181 |
{ |
| 182 |
construct_ = con_code; |
| 183 |
code_counted_ = false; // Reset for a new line. |
| 184 |
break; |
| 185 |
} |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
template <typename C> |
| 190 |
void sloc_counter<C>:: |
| 191 |
char_literal (C c) |
| 192 |
{ |
| 193 |
switch (c) |
| 194 |
{ |
| 195 |
case '\'': |
| 196 |
{ |
| 197 |
if (prev_ != '\\') |
| 198 |
construct_ = con_code; |
| 199 |
break; |
| 200 |
} |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
template <typename C> |
| 205 |
void sloc_counter<C>:: |
| 206 |
string_literal (C c) |
| 207 |
{ |
| 208 |
switch (c) |
| 209 |
{ |
| 210 |
case '"': |
| 211 |
{ |
| 212 |
if (prev_ != '\\') |
| 213 |
construct_ = con_code; |
| 214 |
break; |
| 215 |
} |
| 216 |
case '\n': |
| 217 |
{ |
| 218 |
count_++; |
| 219 |
break; |
| 220 |
} |
| 221 |
} |
| 222 |
} |
| 223 |
} |
| 224 |
} |