Debian Codegraph @

py/ymport/foamfile/Parser.cpp

raw
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 "Parser.hpp"
9 #include "VerifyMacros.hpp"
10
11 #include <boost/filesystem.hpp>
12
13 #include <stdarg.h>
14 #include <stdio.h>
15
16 namespace yade {
17 namespace ymport {
18 namespace foamfile {
19
20 Parser::Parser(const boost::filesystem::path& path, const std::string& klass, const std::string& object)
21 : m_klass(klass)
22 , m_object(object)
23 {
24 m_lexer = std::make_unique<Lexer>(path);
25
26 parseHeader();
27 }
28
29 void Parser::parseHeader()
30 {
31 expect({ "FoamFile" });
32 expect({ '{' });
33
34 enum MustCheck { VERSION = 0, FORMAT, OBJECT, CLASS };
35
36 bool isOk[4] = { false };
37
38 // Token::END will eventually appear.
39 while (true) {
40 Token token = m_lexer->getNextToken();
41
42 if (token.isEnd()) { error("Unexpected END."); }
43
44 if (token.asWordEquals("version")) {
45 // NOTE: Maybe compare as a word?
46 expect({ 2.0 });
47 expect({ ';' });
48 isOk[VERSION] = true;
49 } else if (token.asWordEquals("format")) {
50 expect({ "ascii" });
51 expect({ ';' });
52 isOk[FORMAT] = true;
53 } else if (token.asWordEquals("class")) {
54 expect({ m_klass });
55 expect({ ';' });
56 isOk[CLASS] = true;
57 } else if (token.asWordEquals("object")) {
58 expect({ m_object });
59 expect({ ';' });
60 isOk[OBJECT] = true;
61 } else if (token.asWordEquals("location")) {
62 skipUntil({ ';' });
63 } else if (token.asCharEquals('}')) {
64 break;
65 } else {
66 error("Unexpected token: %s", token.debugString().c_str());
67 }
68 }
69
70 if (!isOk[VERSION]) { error("No version was provided in the header."); }
71
72 if (!isOk[FORMAT]) { error("No format was provided in the header."); }
73
74 if (!isOk[CLASS]) { error("No class was provided in the header."); }
75
76 if (!isOk[OBJECT]) { error("No object was provided in the header."); }
77 }
78
79 void Parser::expect(Token token)
80 {
81 Token current = m_lexer->getNextToken();
82 if (!current.isEqual(token)) { error("Expected: %s, got: %s", token.debugString().c_str(), current.debugString().c_str()); }
83 }
84
85 void Parser::skip(int n)
86 {
87 for (int i = 0; i < n; ++i) {
88 m_lexer->getNextToken();
89 }
90 }
91
92 void Parser::skipUntil(Token token)
93 {
94 Token current;
95 while (!current.isEqual(token)) {
96 current = m_lexer->getNextToken();
97
98 if (token.isEnd()) { error("Unexpected END."); }
99 }
100 }
101
102 std::string Parser::getWord()
103 {
104 Token token = m_lexer->getNextToken();
105
106 if (!token.isWord()) { error("Expected 'WODD'', got: %s", token.debugString().c_str()); }
107
108 return token.asWord();
109 }
110
111 int Parser::getInt()
112 {
113 Token token = m_lexer->getNextToken();
114
115 if (!token.isInt()) { error("Expected 'INT'', got: %s", token.debugString().c_str()); }
116
117 return token.asInt();
118 }
119
120 double Parser::getDouble()
121 {
122 Token token = m_lexer->getNextToken();
123
124 if (!token.isDouble()) { error("Expected 'DOUBLE'', got: %s", token.debugString().c_str()); }
125
126 return token.asDouble();
127 }
128
129 double Parser::getNumber()
130 {
131 Token token = m_lexer->getNextToken();
132
133 if (!token.isNumber()) { error("Expected 'NUMBER'', got: %s", token.debugString().c_str()); }
134
135 return token.asNumber();
136 }
137
138 void Parser::error(const char* fmt, ...)
139 {
140 va_list args;
141 va_start(args, fmt);
142 size_t size = vsnprintf(NULL, 0, fmt, args);
143 va_end(args);
144
145 std::vector<char> data(size + 1);
146
147 va_start(args, fmt);
148 vsnprintf(data.data(), data.size(), fmt, args);
149 va_end(args);
150
151 std::string message = m_lexer->path().filename().string() + ":" + std::to_string(m_lexer->lineNumber()) + ": ";
152 message.append(std::string(data.begin(), data.end()));
153
154 throw std::runtime_error(message);
155 }
156
157 } // namespace foamfile
158 } // namespace ymport
159 } // namespace yade