Skip to content

Commit 9004af1

Browse files
author
Avi SZYCHTER
committed
More robust DBC parsing
1 parent b0b578c commit 9004af1

File tree

5 files changed

+268
-162
lines changed

5 files changed

+268
-162
lines changed

include/ParsingUtils.h

Lines changed: 15 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -6,80 +6,29 @@
66
#include <iostream>
77
#include "CANDatabaseException.h"
88

9-
void throwError(const std::string& category, const std::string& description,
10-
unsigned long long line) {
11-
throw CANDatabaseException(
12-
category + ": " + description + " at line " + std::to_string(line + 1)
13-
);
14-
}
9+
void throw_error(const std::string& category, const std::string& description,
10+
unsigned long long line);
1511

16-
void warning(const std::string& description, unsigned long long line) {
17-
std::cerr << "WARNING: "
18-
<< description
19-
<< " at line "
20-
<< line + 1
21-
<< std::endl;
22-
}
12+
void warning(const std::string& description, unsigned long long line) ;
2313

24-
void skipIf(Tokenizer& tokenizer, const std::string& token) {
25-
if(tokenizer.getNextToken() == token)
26-
return;
14+
const Token&
15+
assert_token(Tokenizer& tokenizer, const std::string& token);
2716

28-
throwError(
29-
"Syntax error",
30-
"expected \"" + token + "\" but got \"" +
31-
tokenizer.getCurrentToken().image + "\"",
32-
tokenizer.lineCount()
33-
);
34-
}
17+
const Token&
18+
assert_token(Tokenizer& tokenizer, Token::Type targetType);
3519

36-
void assertToken(const Tokenizer& tokenizer, const std::string& token) {
37-
if(tokenizer.getCurrentToken() == token)
38-
return;
20+
const Token&
21+
assert_current_token(const Tokenizer& tokenizer, const std::string& token);
3922

40-
throwError(
41-
"Syntax error",
42-
"expected \"" + token + "\" but got \"" +
43-
tokenizer.getCurrentToken().image + "\"",
44-
tokenizer.lineCount()
45-
);
46-
}
23+
const Token&
24+
assert_current_token(const Tokenizer& tokenizer, Token::Type type);
4725

26+
bool is_current_token(const Tokenizer& tokenizer, const std::string& token);
4827

49-
Token checkCurrentTokenType(const Token& toCheck, Token::Type targetType,
50-
unsigned long long line) {
51-
if(toCheck == targetType)
52-
return toCheck;
28+
bool is_current_token(const Tokenizer& tokenizer, Token::Type token);
5329

54-
throwError(
55-
"Syntax error",
56-
"unexpected \"" + toCheck.image + "\"",
57-
line
58-
);
30+
bool is_token(Tokenizer& tokenizer, const std::string& token);
5931

60-
// Removes a compilation warning
61-
return toCheck;
62-
}
32+
bool is_token(Tokenizer& tokenizer, Token::Type token);
6333

64-
Token checkCurrentTokenType(const Token& toCheck,
65-
const std::string& token,
66-
unsigned long long line) {
67-
if(toCheck == token)
68-
return toCheck;
69-
70-
throwError(
71-
"Syntax error",
72-
"unexpected \"" + toCheck.image + "\"",
73-
line
74-
);
75-
76-
// Removes a compilation warning
77-
return toCheck;
78-
}
79-
80-
Token checkTokenType(Tokenizer& tokenizer, Token::Type targetType) {
81-
return checkCurrentTokenType(tokenizer.getNextToken(),
82-
targetType,
83-
tokenizer.lineCount());
84-
}
8534
#endif

include/Tokenizer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ class Tokenizer {
1111
Tokenizer();
1212
virtual ~Tokenizer() = default;
1313

14-
Token getNextToken();
15-
Token getCurrentToken() const;
14+
const Token& getNextToken();
15+
const Token& getCurrentToken() const;
1616

1717
void skipLine();
1818
void skipUntil(const std::string& token);

0 commit comments

Comments
 (0)