| 1 |
/* |
| 2 |
* nextpnr -- Next Generation Place and Route |
| 3 |
* |
| 4 |
* Copyright (C) 2022 gatecat <[email protected]> |
| 5 |
* |
| 6 |
* Permission to use, copy, modify, and/or distribute this software for any |
| 7 |
* purpose with or without fee is hereby granted, provided that the above |
| 8 |
* copyright notice and this permission notice appear in all copies. |
| 9 |
* |
| 10 |
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 11 |
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 12 |
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 13 |
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 14 |
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 15 |
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 16 |
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 17 |
* |
| 18 |
*/ |
| 19 |
|
| 20 |
#ifndef FABULOUS_PARSING_H |
| 21 |
#define FABULOUS_PARSING_H |
| 22 |
|
| 23 |
#include <iostream> |
| 24 |
#include <limits> |
| 25 |
#include <string> |
| 26 |
#include "idstring.h" |
| 27 |
#include "nextpnr_assertions.h" |
| 28 |
#include "nextpnr_namespaces.h" |
| 29 |
|
| 30 |
NEXTPNR_NAMESPACE_BEGIN |
| 31 |
|
| 32 |
struct BaseCtx; |
| 33 |
|
| 34 |
// Lightweight NIH string_view |
| 35 |
struct parser_view |
| 36 |
{ |
| 37 |
char *m_ptr; |
| 38 |
size_t m_length; |
| 39 |
parser_view() : m_ptr(nullptr), m_length(0) {}; |
| 40 |
explicit parser_view(std::string &str) : m_ptr(&(str[0])), m_length(str.size()) {}; |
| 41 |
parser_view(char *ptr, size_t length) : m_ptr(ptr), m_length(length) {}; |
| 42 |
|
| 43 |
static constexpr size_t npos = std::numeric_limits<size_t>::max(); |
| 44 |
char operator[](size_t idx) |
| 45 |
{ |
| 46 |
NPNR_ASSERT(idx < m_length); |
| 47 |
return m_ptr[idx]; |
| 48 |
} |
| 49 |
|
| 50 |
size_t size() const { return m_length; } |
| 51 |
|
| 52 |
bool empty() const { return m_length == 0; } |
| 53 |
|
| 54 |
parser_view substr(size_t start, size_t length = npos) |
| 55 |
{ |
| 56 |
NPNR_ASSERT(start <= m_length); |
| 57 |
if (length == npos) |
| 58 |
length = m_length - start; |
| 59 |
NPNR_ASSERT(length <= m_length); |
| 60 |
return parser_view(m_ptr + start, length); |
| 61 |
} |
| 62 |
|
| 63 |
size_t find(char tok) const |
| 64 |
{ |
| 65 |
for (size_t i = 0; i < m_length; i++) |
| 66 |
if (m_ptr[i] == tok) |
| 67 |
return i; |
| 68 |
return npos; |
| 69 |
} |
| 70 |
|
| 71 |
size_t rfind(char tok) const |
| 72 |
{ |
| 73 |
for (size_t i = m_length; i > 0; i--) |
| 74 |
if (m_ptr[i - 1] == tok) |
| 75 |
return i - 1; |
| 76 |
return npos; |
| 77 |
} |
| 78 |
|
| 79 |
IdString to_id(const BaseCtx *ctx) |
| 80 |
{ |
| 81 |
// This isn't really ideal, let's hope one day we can move to C++20 and have proper string_views instead :3 |
| 82 |
char tmp = m_ptr[m_length]; |
| 83 |
m_ptr[m_length] = '\0'; |
| 84 |
IdString id = IdString(ctx, m_ptr); |
| 85 |
m_ptr[m_length] = tmp; |
| 86 |
return id; |
| 87 |
} |
| 88 |
|
| 89 |
long to_int() |
| 90 |
{ |
| 91 |
// This isn't really ideal, let's hope one day we can move to C++20 and have proper string_views instead :3 |
| 92 |
char tmp = m_ptr[m_length]; |
| 93 |
m_ptr[m_length] = '\0'; |
| 94 |
long l = strtol(m_ptr, nullptr, 0); |
| 95 |
m_ptr[m_length] = tmp; |
| 96 |
return l; |
| 97 |
} |
| 98 |
|
| 99 |
parser_view strip(const std::string &ws = " \r\n\t") |
| 100 |
{ |
| 101 |
char *ptr = m_ptr; |
| 102 |
size_t length = m_length; |
| 103 |
while (length > 0) { // strip front |
| 104 |
if (ws.find(*ptr) == std::string::npos) // not whitespace |
| 105 |
break; |
| 106 |
ptr++; |
| 107 |
length--; |
| 108 |
} |
| 109 |
while (length > 0) { // strip back |
| 110 |
if (ws.find(ptr[length - 1]) == std::string::npos) // not whitespace |
| 111 |
break; |
| 112 |
length--; |
| 113 |
} |
| 114 |
return parser_view(ptr, length); |
| 115 |
} |
| 116 |
|
| 117 |
char back() |
| 118 |
{ |
| 119 |
NPNR_ASSERT(m_length > 0); |
| 120 |
return m_ptr[m_length - 1]; |
| 121 |
} |
| 122 |
|
| 123 |
parser_view back(size_t count) |
| 124 |
{ |
| 125 |
NPNR_ASSERT(count <= m_length); |
| 126 |
return parser_view(m_ptr + (m_length - count), count); |
| 127 |
} |
| 128 |
|
| 129 |
bool starts_with(const std::string &st) |
| 130 |
{ |
| 131 |
if (m_length < st.size()) |
| 132 |
return false; |
| 133 |
for (size_t i = 0; i < st.length(); i++) |
| 134 |
if (m_ptr[i] != st[i]) |
| 135 |
return false; |
| 136 |
return true; |
| 137 |
} |
| 138 |
std::pair<parser_view, parser_view> split(char delim) const |
| 139 |
{ |
| 140 |
size_t pos = find(delim); |
| 141 |
NPNR_ASSERT(pos != npos); |
| 142 |
return std::make_pair(parser_view(m_ptr, pos), parser_view(m_ptr + pos + 1, m_length - (pos + 1))); |
| 143 |
} |
| 144 |
std::pair<parser_view, parser_view> rsplit(char delim) const |
| 145 |
{ |
| 146 |
size_t pos = rfind(delim); |
| 147 |
NPNR_ASSERT(pos != npos); |
| 148 |
return std::make_pair(parser_view(m_ptr, pos), parser_view(m_ptr + pos + 1, m_length - (pos + 1))); |
| 149 |
} |
| 150 |
}; |
| 151 |
|
| 152 |
struct CsvParser |
| 153 |
{ |
| 154 |
explicit CsvParser(std::istream &in) : in(in){}; |
| 155 |
std::istream ∈ |
| 156 |
std::string buf; |
| 157 |
parser_view view; |
| 158 |
bool fetch_next_line() |
| 159 |
{ |
| 160 |
while (!in.eof()) { |
| 161 |
std::getline(in, buf); |
| 162 |
view = parser_view(buf).strip(); |
| 163 |
size_t end_pos = view.find('#'); |
| 164 |
if (end_pos != parser_view::npos) |
| 165 |
view = view.substr(0, end_pos); |
| 166 |
view = view.strip(); |
| 167 |
if (!view.empty()) |
| 168 |
return true; |
| 169 |
} |
| 170 |
return false; |
| 171 |
} |
| 172 |
parser_view next_field() |
| 173 |
{ |
| 174 |
size_t next_delim = view.find(','); |
| 175 |
if (next_delim == parser_view::npos) { |
| 176 |
parser_view result = view.substr(0, next_delim); |
| 177 |
view = parser_view(); |
| 178 |
return result; |
| 179 |
} else { |
| 180 |
parser_view result = view.substr(0, next_delim); |
| 181 |
view = view.substr(next_delim + 1); |
| 182 |
return result; |
| 183 |
} |
| 184 |
} |
| 185 |
}; |
| 186 |
|
| 187 |
NEXTPNR_NAMESPACE_END |
| 188 |
|
| 189 |
#endif |