| 1 |
/************************************************************************* |
| 2 |
* 2022 Tóth János * |
| 3 |
* * |
| 4 |
* This program is free software; it is licensed under the terms of the * |
| 5 |
* GNU General Public License v2 or later. See file LICENSE for details. * |
| 6 |
*************************************************************************/ |
| 7 |
|
| 8 |
#include "Lexer.hpp" |
| 9 |
#include "VerifyMacros.hpp" |
| 10 |
|
| 11 |
#include <boost/regex.hpp> |
| 12 |
|
| 13 |
#include <fcntl.h> |
| 14 |
#include <sys/stat.h> |
| 15 |
#include <sys/types.h> |
| 16 |
#include <unistd.h> |
| 17 |
|
| 18 |
#include <locale.h> |
| 19 |
#include <stdarg.h> |
| 20 |
#include <stdio.h> |
| 21 |
|
| 22 |
namespace yade { |
| 23 |
namespace ymport { |
| 24 |
namespace foamfile { |
| 25 |
|
| 26 |
Lexer::Lexer(const boost::filesystem::path& path) |
| 27 |
: m_path(path) |
| 28 |
{ |
| 29 |
m_oldLocale = setlocale(LC_NUMERIC, nullptr); |
| 30 |
|
| 31 |
// Force '.' as the radix point. |
| 32 |
// This is neccessary for the string->double conversion, |
| 33 |
// e.g.: my system uses ',' as a decimal separator. |
| 34 |
setlocale(LC_NUMERIC, "C"); |
| 35 |
|
| 36 |
// NOTE: Read in chunks? If the file is too large loading it in the memory can be a problem. |
| 37 |
readFullFile(); |
| 38 |
|
| 39 |
removeSingleLineComments(); |
| 40 |
removeMultiLineComments(); |
| 41 |
|
| 42 |
m_currentCharIndex = 0; |
| 43 |
m_lineNumber = 1; |
| 44 |
} |
| 45 |
|
| 46 |
Lexer::~Lexer() { setlocale(LC_NUMERIC, m_oldLocale); } |
| 47 |
|
| 48 |
// Do it in a POSIX way, this must work regardelss of distro or version. |
| 49 |
void Lexer::readFullFile() |
| 50 |
{ |
| 51 |
int fd = open(m_path.string().c_str(), O_RDONLY); |
| 52 |
if (fd == -1) { error("Unable to open file."); } |
| 53 |
|
| 54 |
struct stat st; |
| 55 |
fstat(fd, &st); |
| 56 |
|
| 57 |
VERIFY(st.st_size > 0); |
| 58 |
|
| 59 |
try { |
| 60 |
m_data.resize(st.st_size); |
| 61 |
} catch (...) { |
| 62 |
close(fd); |
| 63 |
error("Unable to allocate buffer."); |
| 64 |
} |
| 65 |
|
| 66 |
ssize_t n = read(fd, &m_data[0], m_data.size()); |
| 67 |
|
| 68 |
if (n == -1 || (size_t)n != m_data.size()) { |
| 69 |
close(fd); |
| 70 |
error("Unable to read from file."); |
| 71 |
} |
| 72 |
|
| 73 |
close(fd); |
| 74 |
} |
| 75 |
|
| 76 |
bool Lexer::isWhiteSpace(char c) { return c == ' ' || c == '\t' || c == '\n'; } |
| 77 |
|
| 78 |
void Lexer::consumeWhiteSpaces() |
| 79 |
{ |
| 80 |
for (; m_currentCharIndex < m_data.length(); m_currentCharIndex++) { |
| 81 |
char curr = m_data[m_currentCharIndex]; |
| 82 |
if (curr == '\n') { m_lineNumber++; } |
| 83 |
|
| 84 |
if (!isWhiteSpace(curr)) { break; } |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
Token Lexer::getNextToken() |
| 89 |
{ |
| 90 |
consumeWhiteSpaces(); |
| 91 |
if (m_currentCharIndex >= m_data.length() - 1) { return { Token::Type::END }; } |
| 92 |
|
| 93 |
// Blob consists one or more characters, they will become 'word's, 'int's or 'double's. |
| 94 |
auto isBlobChar = [](char c) { |
| 95 |
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_' || c == '-' || c == '+' |
| 96 |
|| c == '.'; |
| 97 |
}; |
| 98 |
|
| 99 |
// Char is exactly one character. |
| 100 |
auto isCharChar = [](char c) { return c == ';' || c == '{' || c == '}' || c == '(' || c == ')' || c == '<' || c == '>'; }; |
| 101 |
|
| 102 |
enum State { EMPTY = 0, BLOB, STRING }; |
| 103 |
|
| 104 |
State state = EMPTY; |
| 105 |
std::string currentTokenData; |
| 106 |
for (; m_currentCharIndex < m_data.length(); m_currentCharIndex++) { |
| 107 |
char curr = m_data[m_currentCharIndex]; |
| 108 |
|
| 109 |
if (isWhiteSpace(curr)) { break; } |
| 110 |
|
| 111 |
if (state == EMPTY) { |
| 112 |
if (isBlobChar(curr)) { |
| 113 |
state = BLOB; |
| 114 |
currentTokenData += curr; |
| 115 |
} else if (isCharChar(curr)) { |
| 116 |
m_currentCharIndex++; |
| 117 |
return { curr }; |
| 118 |
} else if (curr == '"') { |
| 119 |
currentTokenData += curr; |
| 120 |
state = STRING; |
| 121 |
} else { |
| 122 |
error("Unknown character: '%c'", curr); |
| 123 |
} |
| 124 |
} else if (state == BLOB) { |
| 125 |
if (isBlobChar(curr)) { |
| 126 |
currentTokenData += curr; |
| 127 |
} else { |
| 128 |
break; |
| 129 |
} |
| 130 |
// string will "eat" everything |
| 131 |
} else if (state == STRING) { |
| 132 |
if (curr == '"') { |
| 133 |
currentTokenData += curr; |
| 134 |
m_currentCharIndex++; |
| 135 |
break; |
| 136 |
} else { |
| 137 |
currentTokenData += curr; |
| 138 |
} |
| 139 |
} else { |
| 140 |
UNREACHABLE(); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
VERIFY(currentTokenData.length() > 0); |
| 145 |
|
| 146 |
boost::regex re_int("[-+]?[0-9]+"); |
| 147 |
boost::regex re_dbl("[-+]?[0-9]?[.][0-9]+"); |
| 148 |
|
| 149 |
if (boost::regex_match(currentTokenData, re_int)) { |
| 150 |
int i = 0; |
| 151 |
try { |
| 152 |
i = std::stoi(currentTokenData); |
| 153 |
} catch (std::exception& ex) { |
| 154 |
error("%s", ex.what()); |
| 155 |
} |
| 156 |
|
| 157 |
return { i }; // int |
| 158 |
} else if (boost::regex_match(currentTokenData, re_dbl)) { |
| 159 |
double d = 0; |
| 160 |
try { |
| 161 |
d = std::stod(currentTokenData); |
| 162 |
} catch (std::exception& ex) { |
| 163 |
error("%s", ex.what()); |
| 164 |
} |
| 165 |
|
| 166 |
return { d }; // double |
| 167 |
} else { |
| 168 |
return { currentTokenData }; // word |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
void Lexer::removeSingleLineComments() |
| 173 |
{ |
| 174 |
size_t len = 0; |
| 175 |
bool inComment = false; |
| 176 |
size_t start = 0; |
| 177 |
m_lineNumber = 1; |
| 178 |
for (size_t i = 0; i < m_data.length() - 1; ++i) { |
| 179 |
char c1 = m_data[i]; |
| 180 |
char c2 = m_data[i + 1]; |
| 181 |
|
| 182 |
if (c1 == '\n') { m_lineNumber++; } |
| 183 |
|
| 184 |
if (c1 == '/' && c2 == '/') { |
| 185 |
inComment = true; |
| 186 |
start = i; |
| 187 |
len = 0; |
| 188 |
} |
| 189 |
|
| 190 |
if (inComment) { |
| 191 |
len++; |
| 192 |
if (c1 == '\n') { |
| 193 |
m_data.replace(start, len, "\n"); |
| 194 |
inComment = false; |
| 195 |
i = 0; |
| 196 |
} |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
if (inComment && m_data[m_data.length() - 1] == '\n') { |
| 201 |
len++; |
| 202 |
m_data.replace(start, len, "\n"); |
| 203 |
inComment = false; |
| 204 |
} |
| 205 |
|
| 206 |
if (inComment) { error("Non-terminated single line comment."); } |
| 207 |
} |
| 208 |
|
| 209 |
void Lexer::removeMultiLineComments() |
| 210 |
{ |
| 211 |
size_t len = 0; |
| 212 |
bool inComment = false; |
| 213 |
size_t numberOfNewLines = 0; |
| 214 |
size_t start = 0; |
| 215 |
m_lineNumber = 1; |
| 216 |
for (size_t i = 0; i < m_data.length() - 1; ++i) { |
| 217 |
char c1 = m_data[i]; |
| 218 |
char c2 = m_data[i + 1]; |
| 219 |
|
| 220 |
if (c1 == '\n') { m_lineNumber++; } |
| 221 |
|
| 222 |
if (c1 == '/' && c2 == '*') { |
| 223 |
numberOfNewLines = 0; |
| 224 |
inComment = true; |
| 225 |
start = i; |
| 226 |
len = 0; |
| 227 |
} |
| 228 |
|
| 229 |
if (inComment) { |
| 230 |
len++; |
| 231 |
|
| 232 |
if (c1 == '\n') { |
| 233 |
numberOfNewLines++; |
| 234 |
} else if (c1 == '*' && c2 == '/') { |
| 235 |
len++; |
| 236 |
std::string newLines = ""; |
| 237 |
|
| 238 |
for (size_t j = 0; j < numberOfNewLines; ++j) { |
| 239 |
newLines += '\n'; |
| 240 |
} |
| 241 |
|
| 242 |
m_data.replace(start, len, newLines); |
| 243 |
inComment = false; |
| 244 |
i = 0; |
| 245 |
} |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
if (inComment) { error("Non-terminated multi line comment."); } |
| 250 |
} |
| 251 |
|
| 252 |
void Lexer::error(const char* fmt, ...) |
| 253 |
{ |
| 254 |
va_list args; |
| 255 |
|
| 256 |
va_start(args, fmt); |
| 257 |
size_t size = vsnprintf(NULL, 0, fmt, args); |
| 258 |
va_end(args); |
| 259 |
|
| 260 |
std::vector<char> data(size + 1); |
| 261 |
|
| 262 |
va_start(args, fmt); |
| 263 |
vsnprintf(data.data(), data.size(), fmt, args); |
| 264 |
va_end(args); |
| 265 |
|
| 266 |
std::string message = m_path.filename().string() + ":" + std::to_string(m_lineNumber) + ": "; |
| 267 |
message.append(std::string(data.begin(), data.end())); |
| 268 |
|
| 269 |
throw std::runtime_error(message); |
| 270 |
} |
| 271 |
|
| 272 |
} // namespace foamfile |
| 273 |
} // namespace ymport |
| 274 |
} // namespace yade |