Line data Source code
1 : /**
2 : * @file dbc_parser.h
3 : * @brief DBC file parser and interpretation options.
4 : *
5 : * Created: 2026-03-13
6 : * Author: Deeaitch (Dim. Himro)
7 : *
8 : * Licensed under the MIT License.
9 : * See LICENSE file in the project root for full license text.
10 : */
11 :
12 : #ifndef DBC_PARSER_H
13 : #define DBC_PARSER_H
14 :
15 : #include <string>
16 :
17 : #include "dbc_diagnostic.h"
18 :
19 : /** Explicit interpretation applied to extended frames in this file. */
20 : struct DbcParserOptions {
21 : bool enableJ1939 = false; /**< Derive J1939 PGNs from extended CAN IDs. */
22 : };
23 :
24 : /**
25 : * @brief Minimal DBC parser.
26 : *
27 : * Supports:
28 : * - BO_
29 : * - SG_
30 : * - CM_ BO_
31 : * - CM_ SG_
32 : * - basic M / mN multiplexing
33 : * - VAL_ signal value descriptions
34 : * - BA_DEF_, BA_DEF_DEF_, BA_ typed attributes, frame format and cycle time
35 : *
36 : * Frame/signal comments support multiline quoted text and deferred references.
37 : *
38 : * Reports unsupported statements as warnings, except SG_MUL_VAL_ which is
39 : * an error. VERSION, NS_, BS_, BU_ and NS_ symbol declarations stay quiet.
40 : */
41 : class DbcParser {
42 : public:
43 : /** @brief Create a parser with explicit extended-frame interpretation options. */
44 930 : explicit DbcParser(DbcParserOptions options = {}) : options(options) {}
45 :
46 : /**
47 : * @brief Parse DBC file.
48 : * @param filePath Path to input file.
49 : * @return Parsed database.
50 : * @throws std::runtime_error on file or parse errors.
51 : * Line parse errors include the file path and one-based line number.
52 : * Duplicate normalized CAN IDs of the same frame type and duplicate signal
53 : * names within a frame are rejected at the repeated definition's line.
54 : */
55 : DbcDatabase ParseFile(const std::string &filePath) const;
56 :
57 : /**
58 : * @brief Parse with warnings and a structured first error instead of throwing.
59 : * @return Valid database with diagnostics; empty database on error.
60 : * File errors use line zero; statement errors use physical one-based lines.
61 : * Warnings indicate ignored syntax and possible incomplete decoding.
62 : */
63 : DbcParseResult ParseFileWithDiagnostics(const std::string &filePath) const;
64 :
65 : private:
66 : DbcParserOptions options;
67 : };
68 :
69 : #endif /* DBC_PARSER_H */
|