DBC Framework
DBC parsing and CAN signal decoding framework
Loading...
Searching...
No Matches
dbc_attributes.cpp
1// SPDX-License-Identifier: MIT
2#include "dbc_statement_handler.h"
3#include <charconv>
4#include <cmath>
5#include <limits>
6#include <set>
7
8namespace {
9 struct Token {
10 std::string text;
11 bool quoted = false;
12 };
13
14 std::vector<Token> Tokenize(const std::string &text) {
15 std::vector<Token> tokens;
16 std::size_t index = 0U;
17 while (index < text.size()) {
18 const char character = text[index++];
19 if (character == ' ' || character == '\t')
20 continue;
21 Token token;
22 if (character == '"') {
23 token.quoted = true;
24 bool closed = false;
25 while (index < text.size()) {
26 char next = text[index++];
27 if (next == '"') {
28 closed = true;
29 break;
30 }
31 if (next == '\\') {
32 if (index == text.size())
33 throw std::runtime_error("Incomplete attribute escape.");
34 next = text[index++];
35 if (next != '"' && next != '\\')
36 throw std::runtime_error("Invalid attribute escape.");
37 }
38 token.text += next;
39 }
40 if (!closed)
41 throw std::runtime_error("Unterminated attribute string.");
42 if (index < text.size() && text[index] != ' ' && text[index] != '\t' &&
43 text[index] != ',' && text[index] != ';')
44 throw std::runtime_error("Missing separator after attribute string.");
45 } else if (character == ';' || character == ',') {
46 token.text = character;
47 } else {
48 token.text = character;
49 while (index < text.size() && text[index] != ' ' && text[index] != '\t' &&
50 text[index] != ';' && text[index] != ',') {
51 if (text[index] == '"')
52 throw std::runtime_error("Missing separator before attribute string.");
53 token.text += text[index++];
54 }
55 }
56 tokens.push_back(token);
57 }
58 if (tokens.empty() || tokens.back().quoted || tokens.back().text != ";")
59 throw std::runtime_error("Attribute statement requires a semicolon.");
60 tokens.pop_back();
61 return tokens;
62 }
63
64 class AttributeTokens {
65 public:
66 explicit AttributeTokens(const std::string &line) : tokens(Tokenize(line)) {}
67 Token Take() {
68 if (position == tokens.size())
69 throw std::runtime_error("Incomplete attribute statement.");
70 return tokens[position++];
71 }
72 std::string Word() {
73 const Token token = Take();
74 if (token.quoted || token.text == "," || token.text == ";")
75 throw std::runtime_error("Expected an attribute keyword or number.");
76 return token.text;
77 }
78 std::string Quoted() {
79 const Token token = Take();
80 if (!token.quoted)
81 throw std::runtime_error("Expected a quoted attribute name or string.");
82 return token.text;
83 }
84 bool Empty() const { return position == tokens.size(); }
85 void End() const {
86 if (!Empty())
87 throw std::runtime_error("Unexpected trailing attribute fields.");
88 }
89
90 private:
91 std::vector<Token> tokens;
92 std::size_t position = 0U;
93 };
94
95 bool IsScope(const std::string &text) {
96 return text == "BO_" || text == "SG_" || text == "BU_" || text == "EV_";
97 }
98
99 AttributeScope ParseScope(const std::string &text) {
100 if (text == "BO_") return AttributeScope::Frame;
101 if (text == "SG_") return AttributeScope::Signal;
102 if (text == "BU_") return AttributeScope::Node;
103 if (text == "EV_") return AttributeScope::Environment;
104 throw std::runtime_error("Unsupported attribute scope: " + text);
105 }
106
107 template<class Numeric>
108 Numeric ParseNumber(const std::string &text) {
109 const char *begin = text.data();
110 const char *end = begin + text.size();
111 if (begin != end && *begin == '+') {
112 ++begin;
113 if (begin != end && (*begin == '-' || *begin == '+'))
114 throw std::runtime_error("Invalid attribute number: " + text);
115 }
116 Numeric value = 0;
117 const std::from_chars_result parsed = std::from_chars(begin, end, value);
118 if (begin == end || parsed.ec != std::errc() || parsed.ptr != end || !std::isfinite(value))
119 throw std::runtime_error("Invalid attribute number: " + text);
120 return value;
121 }
122
123 AttributeValue Value(const Token &token, const AttributeDefinition &definition, bool isDefault) {
124 if (definition.type == AttributeType::String) {
125 if (!token.quoted) throw std::runtime_error("Attribute STRING requires quoted text.");
126 return token.text;
127 }
128 if (definition.type == AttributeType::Enum && isDefault && token.quoted) {
129 for (std::size_t index = 0U; index < definition.enumValues.size(); ++index) {
130 if (definition.enumValues[index] == token.text)
131 return static_cast<std::int64_t>(index);
132 }
133 throw std::runtime_error("Unknown attribute ENUM default label.");
134 }
135 if (token.quoted) throw std::runtime_error("Numeric attribute requires an unquoted number.");
136 if (definition.type == AttributeType::Float) {
137 const double value = ParseNumber<double>(token.text);
138 if (value < definition.minimum || value > definition.maximum)
139 throw std::runtime_error("Attribute FLOAT value outside definition range.");
140 return value;
141 }
142 const std::int64_t value = ParseNumber<std::int64_t>(token.text);
143 if (value < definition.integerMinimum || value > definition.integerMaximum)
144 throw std::runtime_error("Attribute integer/ENUM value outside definition range.");
145 return value;
146 }
147
148 void Define(AttributeTokens &tokens, DbcDatabase &database) {
149 AttributeDefinition definition;
150 const Token first = tokens.Take();
151 std::string name = first.text;
152 if (!first.quoted) {
153 definition.scope = ParseScope(first.text);
154 name = tokens.Quoted();
155 }
156 if (name.empty()) throw std::runtime_error("Empty attribute name.");
157 const std::string type = tokens.Word();
158 if (type == "INT" || type == "HEX") {
159 definition.type = type == "INT" ? AttributeType::Integer : AttributeType::Hex;
160 definition.integerMinimum = ParseNumber<std::int64_t>(tokens.Word());
161 definition.integerMaximum = ParseNumber<std::int64_t>(tokens.Word());
162 if (definition.integerMinimum > definition.integerMaximum)
163 throw std::runtime_error("Reversed attribute integer range.");
164 } else if (type == "FLOAT") {
165 definition.type = AttributeType::Float;
166 definition.minimum = ParseNumber<double>(tokens.Word());
167 definition.maximum = ParseNumber<double>(tokens.Word());
168 if (definition.minimum > definition.maximum)
169 throw std::runtime_error("Reversed attribute FLOAT range.");
170 } else if (type == "STRING") {
171 definition.type = AttributeType::String;
172 } else if (type == "ENUM") {
173 definition.type = AttributeType::Enum;
174 std::set<std::string> labels;
175 for (;;) {
176 const std::string label = tokens.Quoted();
177 if (!labels.insert(label).second)
178 throw std::runtime_error("Duplicate attribute ENUM label.");
179 definition.enumValues.push_back(label);
180 if (tokens.Empty()) break;
181 const Token separator = tokens.Take();
182 if (separator.quoted || separator.text != ",") throw std::runtime_error("Expected ENUM comma.");
183 }
184 definition.integerMaximum = static_cast<std::int64_t>(definition.enumValues.size() - 1U);
185 } else {
186 throw std::runtime_error("Unsupported attribute definition type: " + type);
187 }
188 tokens.End();
189 if (name == "VFrameFormat" && (definition.scope != AttributeScope::Frame || definition.type != AttributeType::Enum))
190 throw std::runtime_error("VFrameFormat requires a BO_ ENUM definition.");
191 if (name == "GenMsgCycleTime" && (definition.scope != AttributeScope::Frame ||
192 (definition.type != AttributeType::Integer && definition.type != AttributeType::Hex)))
193 throw std::runtime_error("GenMsgCycleTime requires a BO_ integer definition.");
194 if (!database.attributeDefinitions.emplace(name, definition).second)
195 throw std::runtime_error("Duplicate attribute definition: " + name);
196 }
197
198 AttributeDefinition &FindDefinition(DbcDatabase &database, const std::string &name) {
199 const std::map<std::string, AttributeDefinition>::iterator found = database.attributeDefinitions.find(name);
200 if (found == database.attributeDefinitions.end())
201 throw std::runtime_error("Undefined attribute: " + name);
202 return found->second;
203 }
204
205 void ParseId(const std::string &text, AttributeAssignment &assignment) {
206 const std::int64_t raw = ParseNumber<std::int64_t>(text);
207 if (raw < 0 || raw > std::numeric_limits<std::uint32_t>::max() || (raw & 0x60000000U) != 0)
208 throw std::runtime_error("Invalid attribute CAN ID.");
209 const std::uint32_t id = static_cast<std::uint32_t>(raw);
210 assignment.canId = id & 0x1fffffffU;
211 assignment.isExtended = (id & 0x80000000U) != 0U || id > 0x7ffU;
212 }
213
214 AttributeMap &Target(DbcDatabase &database, const AttributeAssignment &assignment) {
215 if (assignment.scope == AttributeScope::Database) return database.attributes;
216 for (FrameInfo &frame: database.frames) {
217 if (frame.canId != assignment.canId || frame.isExtended != assignment.isExtended) continue;
218 if (assignment.scope == AttributeScope::Frame) return frame.attributes;
219 for (SignalInfo &signal: frame.signals) {
220 if (signal.name == assignment.targetName) return signal.attributes;
221 }
222 throw std::runtime_error("Unresolved attribute signal: " + assignment.targetName);
223 }
224 throw std::runtime_error("Unresolved attribute frame.");
225 }
226
227 void ValidateCycleTime(const std::string &name, const AttributeValue &value) {
228 if (name != "GenMsgCycleTime") return;
229 const std::int64_t milliseconds = std::get<std::int64_t>(value);
230 if (milliseconds < 0 || milliseconds > std::numeric_limits<std::uint32_t>::max())
231 throw std::runtime_error("GenMsgCycleTime must fit unsigned 32-bit milliseconds.");
232 }
233
234 void ApplyDefault(DbcDatabase &database, const std::string &name, const AttributeDefinition &definition) {
235 if (!definition.defaultValue) return;
236 if (definition.scope == AttributeScope::Database) {
237 database.attributes[name] = *definition.defaultValue;
238 return;
239 }
240 if (definition.scope != AttributeScope::Frame && definition.scope != AttributeScope::Signal)
241 return;
242 for (FrameInfo &frame: database.frames) {
243 if (definition.scope == AttributeScope::Frame) frame.attributes[name] = *definition.defaultValue;
244 if (definition.scope == AttributeScope::Signal) {
245 for (SignalInfo &signal: frame.signals) signal.attributes[name] = *definition.defaultValue;
246 }
247 }
248 }
249}// namespace
250
251void DbcAttributeHandler::Handle(const std::string &line, DbcParseContext &context) const {
252 context.pendingAttributes.push_back({line, context.lineNumber});
253}
254
255void DbcParseContext::ResolveAttributes(const std::string &filePath, std::vector<DbcDiagnostic> &diagnostics) {
256 std::map<std::string, std::size_t> defaultLines;
257 std::vector<std::size_t> assignmentLines;
258 // Resolve each phase independently of source ordering.
259 const char *keywords[] = {"BA_DEF_", "BA_DEF_DEF_", "BA_"};
260 for (const char *phase: keywords) {
261 for (const PendingAttribute &pending: pendingAttributes) {
262 const std::string keyword = pending.statement.substr(0U, pending.statement.find_first_of(" \t"));
263 if (keyword != phase) continue;
264 try {
265 AttributeTokens tokens(pending.statement);
266 tokens.Word();
267 if (keyword == "BA_DEF_") {
268 Define(tokens, database);
269 continue;
270 }
271 const std::string name = tokens.Quoted();
272 AttributeDefinition &definition = FindDefinition(database, name);
273 if (keyword == "BA_DEF_DEF_") {
274 if (definition.defaultValue) throw std::runtime_error("Duplicate attribute default: " + name);
275 definition.defaultValue = Value(tokens.Take(), definition, true);
276 ValidateCycleTime(name, *definition.defaultValue);
277 tokens.End();
278 defaultLines[name] = pending.lineNumber;
279 ApplyDefault(database, name, definition);
280 continue;
281 }
282 AttributeAssignment assignment;
283 assignment.name = name;
284 Token value = tokens.Take();
285 if (!value.quoted && IsScope(value.text)) {
286 assignment.scope = ParseScope(value.text);
287 if (assignment.scope == AttributeScope::Frame || assignment.scope == AttributeScope::Signal)
288 ParseId(tokens.Word(), assignment);
289 if (assignment.scope != AttributeScope::Frame)
290 assignment.targetName = tokens.Word();
291 value = tokens.Take();
292 }
293 if (assignment.scope != definition.scope) throw std::runtime_error("Attribute assignment scope differs from definition.");
294 assignment.value = Value(value, definition, false);
295 ValidateCycleTime(name, assignment.value);
296 tokens.End();
297 // Repeated explicit assignments use the last value, matching comments.
298 if (assignment.scope != AttributeScope::Node && assignment.scope != AttributeScope::Environment)
299 Target(database, assignment)[name] = assignment.value;
300 database.attributeAssignments.push_back(assignment);
301 assignmentLines.push_back(pending.lineNumber);
302 } catch (const std::exception &error) {
303 throw DbcDiagnosticError(DbcDiagnosticCode::InvalidAttribute, filePath, pending.lineNumber, error.what());
304 }
305 }
306 }
307 for (FrameInfo &frame: database.frames) {
308 for (const AttributeMap::value_type &attribute: frame.attributes) {
309 if (attribute.first != "VFrameFormat" && attribute.first != "GenMsgCycleTime") continue;
310 std::size_t line = defaultLines[attribute.first];
311 for (std::size_t index = 0U; index < database.attributeAssignments.size(); ++index) {
312 const AttributeAssignment &assignment = database.attributeAssignments[index];
313 if (assignment.name == attribute.first && assignment.scope == AttributeScope::Frame &&
314 assignment.canId == frame.canId && assignment.isExtended == frame.isExtended)
315 line = assignmentLines[index];
316 }
317 const std::int64_t value = std::get<std::int64_t>(attribute.second);
318 if (attribute.first == "GenMsgCycleTime") {
319 frame.cycleTimeMs = static_cast<std::uint32_t>(value);
320 continue;
321 }
322 frame.frameFormat = database.attributeDefinitions.at(attribute.first).enumValues[static_cast<std::size_t>(value)];
323 const std::string &format = *frame.frameFormat;
324 std::string warning;
325 if (format != "StandardCAN" && format != "ExtendedCAN")
326 warning = "Unsupported VFrameFormat '" + format + "' is preserved; decoding may be incomplete.";
327 else if ((format == "ExtendedCAN") != frame.isExtended)
328 warning = "VFrameFormat '" + format + "' conflicts with the BO_ CAN ID; ID interpretation is unchanged.";
329 if (!warning.empty())
330 diagnostics.push_back({DbcDiagnosticSeverity::Warning, DbcDiagnosticCode::UnsupportedAttribute, filePath, line, warning});
331 }
332 }
333}
void Handle(const std::string &line, DbcParseContext &context) const override
Process a trimmed statement using state belonging to this parse.
Explicit assignment; defaults remain on definitions.
Definition attributes.h:59
std::uint32_t canId
Definition attributes.h:62
std::string targetName
Definition attributes.h:64
AttributeScope scope
Definition attributes.h:61
AttributeValue value
Definition attributes.h:65
std::string name
Definition attributes.h:60
Attribute type, permitted range, and optional default.
Definition attributes.h:42
std::vector< std::string > enumValues
Definition attributes.h:49
std::int64_t integerMaximum
Definition attributes.h:46
AttributeType type
Definition attributes.h:44
AttributeScope scope
Definition attributes.h:43
std::int64_t integerMinimum
Definition attributes.h:45
std::optional< AttributeValue > defaultValue
Definition attributes.h:50
Parsed DBC content stored in a simple internal form.
std::map< std::string, AttributeDefinition > attributeDefinitions
std::vector< FrameInfo > frames
AttributeMap attributes
std::vector< AttributeAssignment > attributeAssignments
void ResolveAttributes(const std::string &filePath, std::vector< DbcDiagnostic > &diagnostics)
Resolve definitions, defaults, then assignments; append format warnings.
std::vector< PendingAttribute > pendingAttributes
Describes one CAN frame from a DBC file.
Definition frame_info.h:25
Attribute statement retained for ordered resolution.
Describes one signal inside a DBC frame.
Definition signal_info.h:25