EBGeometry
Compact, header-only C++ library for fast evaluation of signed distance functions
Loading...
Searching...
No Matches
EBGeometry_LanguageSyntax.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Robert Marskar <robert.marskar@sintef.no>
2//
3// SPDX-License-Identifier: BSD-3-Clause
4
11#ifndef EBGEOMETRY_LANGUAGESYNTAX_HPP
12#define EBGEOMETRY_LANGUAGESYNTAX_HPP
13
14// Std includes
15#include <cstdint>
16#include <cstdlib>
17#include <memory>
18#include <stdexcept>
19#include <string>
20#include <string_view>
21#include <vector>
22
23// Our includes
24#include "EBGeometry_Macros.hpp"
25
26namespace EBGeometry {
27namespace Language {
28
33struct Span
34{
38 std::string m_file;
39
43 uint32_t m_begin = 0;
44
48 uint32_t m_end = 0;
49
53 uint32_t m_line = 0;
54
58 uint32_t m_column = 0;
59};
60
65{
69 enum class Severity
70 {
71 Error,
72 Warning
73 };
74
79
84
88 std::string m_message;
89
94 [[nodiscard]] EBGEOMETRY_HOST
95 std::string
96 str() const
97 {
98 const std::string where =
100 : m_span.m_file + ":" + std::to_string(m_span.m_line) + ":" + std::to_string(m_span.m_column);
101
102 return where + ": " + (m_severity == Severity::Error ? "error: " : "warning: ") + m_message;
103 }
104};
105
110class Error : public std::runtime_error
111{
112public:
118 explicit Error(std::vector<Diagnostic> a_diagnostics)
119 : std::runtime_error(joined(a_diagnostics)), m_diagnostics(std::move(a_diagnostics))
120 {}
121
128 Error(const Span& a_span, const std::string& a_message)
129 : Error(std::vector<Diagnostic>{Diagnostic{Diagnostic::Severity::Error, a_span, a_message}})
130 {}
131
136 [[nodiscard]] EBGEOMETRY_HOST
137 const std::vector<Diagnostic>&
138 diagnostics() const noexcept
139 {
140 return m_diagnostics;
141 }
142
143private:
147 std::vector<Diagnostic> m_diagnostics;
148
154 [[nodiscard]] EBGEOMETRY_HOST
155 static std::string
156 joined(const std::vector<Diagnostic>& a_diagnostics)
157 {
158 std::string text;
159
160 for (const auto severity : {Diagnostic::Severity::Error, Diagnostic::Severity::Warning}) {
161 for (const Diagnostic& d : a_diagnostics) {
162 if (d.m_severity == severity) {
163 text += (text.empty() ? "" : "\n") + d.str();
164 }
165 }
166 }
167
168 return text;
169 }
170};
171
176namespace Syntax {
177
181enum class Token : uint8_t
182{
183 End,
184 Newline,
185 Name,
186 Number,
187 String,
188 Symbol
189};
190
195struct Lexeme
196{
200 Token m_token = Token::End;
201
205 std::string m_text;
206
210 double m_number = 0.0;
211
215 Span m_span;
216};
217
221struct Comment
222{
226 std::string m_text;
227
231 Span m_span;
232
236 bool m_ownLine = true;
237};
238
249[[nodiscard]] EBGEOMETRY_HOST
250inline std::vector<Lexeme>
251tokenize(std::string_view a_text, const std::string& a_file, std::vector<Comment>* a_comments = nullptr);
252
256enum class NodeKind : uint8_t
257{
258 Number,
259 String,
260 Bool,
261 Name,
262 Vector,
263 Pair,
264 Negate,
265 Add,
266 Subtract,
267 Multiply,
268 Divide,
269 Call,
270 Method,
271 Overlay,
272 Region
273};
274
278struct Node
279{
283 NodeKind m_kind = NodeKind::Number;
284
288 double m_number = 0.0;
289
293 std::string m_text;
294
298 Span m_span;
299
303 Span m_nameSpan;
304
308 std::vector<Node> m_children;
309
313 std::vector<std::string> m_keys;
314
318 std::string m_material;
319
323 Span m_materialSpan;
324};
325
329struct Property
330{
334 std::string m_key;
335
339 Span m_keySpan;
340
344 Node m_value;
345};
346
350struct Properties
351{
355 bool m_written = false;
356
360 std::vector<Property> m_entries;
361
365 Span m_span;
366};
367
371struct DomainItem
372{
376 bool m_subdomain = true;
377
381 std::string m_name;
382
386 Span m_nameSpan;
387
391 Span m_span;
392
396 std::vector<Node> m_list;
397
401 Properties m_properties;
402};
403
407enum class StatementKind : uint8_t
408{
409 Value,
410 Object,
411 Material,
412 Domain,
413 Couple,
414 Include,
415 View,
416 Label
417};
418
422struct Statement
423{
427 StatementKind m_kind = StatementKind::Value;
428
432 bool m_param = false;
433
437 std::string m_name;
438
442 Span m_nameSpan;
443
447 Span m_span;
448
452 Node m_expr;
453
457 Properties m_properties;
458
462 std::vector<DomainItem> m_items;
463
467 std::string m_coupleKind;
468
472 std::string m_ends[4];
473
477 Span m_endSpans[4];
478};
479
483struct File
484{
488 std::string m_name;
489
493 std::string m_text;
494
498 uint32_t m_version = 0;
499
503 std::vector<Statement> m_statements;
504
508 std::vector<Comment> m_comments;
509};
510
517[[nodiscard]] EBGEOMETRY_HOST
518inline File
519parse(std::string_view a_text, const std::string& a_file);
520
527[[nodiscard]] EBGEOMETRY_HOST
528inline Node
529parseExpression(std::string_view a_text, const std::string& a_name);
530
536[[nodiscard]] EBGEOMETRY_HOST
537inline bool
538isReserved(std::string_view a_name) noexcept;
539
540} // namespace Syntax
541} // namespace Language
542} // namespace EBGeometry
543
544#include "EBGeometry_LanguageSyntaxImplem.hpp"
545
546#endif
#define EBGEOMETRY_HOST
Mark a function as callable from host code only (no-op on host compilers).
Definition EBGeometry_GPU.hpp:83
Utility macros for EBGeometry.
A computational domain over a tape, for a downstream code (a solver EBGeometry does not provide): the...
Definition EBGeometry_Domain.hpp:63
What reading a model throws when it has errors: every error found (not only the first),...
Definition EBGeometry_LanguageSyntax.hpp:111
Error(std::vector< Diagnostic > a_diagnostics)
Error with its diagnostics.
Definition EBGeometry_LanguageSyntax.hpp:118
Error(const Span &a_span, const std::string &a_message)
One error at a span.
Definition EBGeometry_LanguageSyntax.hpp:128
const std::vector< Diagnostic > & diagnostics() const noexcept
The diagnostics, errors first.
Definition EBGeometry_LanguageSyntax.hpp:138
Model< T > parse(std::string_view a_text, const std::string &a_name, Pool &a_pool, const Overrides &a_overrides={})
Read a model from text.
Namespace containing all of EBGeometry's functionality.
Definition EBGeometry_Affine.hpp:26
Expr< ExprPrecision< A > > Overlay(const std::vector< A > &a_operands)
Regions by priority, a priority union: each operand is a region of its own, later operands on top of ...
One error or warning, at a span.
Definition EBGeometry_LanguageSyntax.hpp:65
Severity m_severity
Error or warning.
Definition EBGeometry_LanguageSyntax.hpp:78
std::string m_message
What is wrong, in words.
Definition EBGeometry_LanguageSyntax.hpp:88
std::string str() const
The diagnostic as one line: "file:line:column: error: message" ("file: error: message" for a whole fi...
Definition EBGeometry_LanguageSyntax.hpp:96
Span m_span
Where.
Definition EBGeometry_LanguageSyntax.hpp:83
Severity
Whether a diagnostic stops the model.
Definition EBGeometry_LanguageSyntax.hpp:70
@ Warning
The model is built; something looks unintended.
@ Error
The model cannot be built.
Where a piece of text is: its file, its first and one-past-last byte, and the line and column of its ...
Definition EBGeometry_LanguageSyntax.hpp:34
uint32_t m_begin
The first byte.
Definition EBGeometry_LanguageSyntax.hpp:43
uint32_t m_end
One past the last byte.
Definition EBGeometry_LanguageSyntax.hpp:48
std::string m_file
The file, as the model names it (the path given to read(), or the name given to parse()).
Definition EBGeometry_LanguageSyntax.hpp:38
uint32_t m_line
The line of the first byte, from 1; 0 for a span that is a whole file or no file (an override).
Definition EBGeometry_LanguageSyntax.hpp:53
uint32_t m_column
The column of the first byte, from 1, in bytes.
Definition EBGeometry_LanguageSyntax.hpp:58