-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added multiple headers to include/dulcificum/gcode/ast for AST handling using variants and templates. Also added function to parse gcode into AST. These changes allow transforming gcode to AST which can be used for further analysis or translation to other formats. Added example usage to the main translator application. Contribute to CURA-10561
- Loading branch information
1 parent
0a546e7
commit 4a338f1
Showing
8 changed files
with
195 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#ifndef DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_AST_AST_H | ||
#define DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_AST_AST_H | ||
|
||
#include "dulcificum/gcode/ast/g0.h" | ||
#include "dulcificum/gcode/ast/g1.h" | ||
|
||
#include <variant> | ||
#include <vector> | ||
#include <string_view> | ||
|
||
namespace dulcificum::gcode::ast | ||
{ | ||
|
||
using Unknown = Entry<R"((.*))">; | ||
using Comment = Entry<R"(;(.*))">; | ||
using node_t = std::variant<Unknown, Comment, G0, G1>; | ||
using ast_t = std::vector<node_t>; | ||
|
||
node_t factory(size_t index, std::string line) | ||
{ | ||
if (line.starts_with(';')) | ||
{ | ||
return Comment { index, line }; | ||
} | ||
if (line.starts_with("G0")) | ||
{ | ||
return G0 { index, line }; | ||
} | ||
if (line.starts_with("G1")) | ||
{ | ||
return G1 { index, line }; | ||
} | ||
return Unknown { index, line }; | ||
} | ||
|
||
} // namespace dulcificum::gcode::ast | ||
|
||
#endif // DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_AST_AST_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#ifndef DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_AST_ENTRY_H | ||
#define DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_AST_ENTRY_H | ||
|
||
#include "dulcificum/utils/char_range_literal.h" | ||
|
||
#include <cstddef> | ||
#include <ctre.hpp> | ||
#include <string> | ||
|
||
namespace dulcificum::gcode::ast | ||
{ | ||
|
||
template<dulcificum::utils::CharRangeLiteral Pattern> | ||
class Entry | ||
{ | ||
public: | ||
Entry() = delete; | ||
Entry(size_t index, std::string line) | ||
: index{ index } | ||
, line{ std::move(line) } {}; | ||
|
||
size_t index; | ||
std::string line; | ||
|
||
constexpr auto get() | ||
{ | ||
return ctre::match<Pattern.value>(line); | ||
} | ||
}; | ||
|
||
} // namespace dulcificum::gcode::ast | ||
|
||
#endif // DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_AST_ENTRY_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#ifndef DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_AST_G0_H | ||
#define DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_AST_G0_H | ||
|
||
#include "dulcificum/gcode/ast/entry.h" | ||
|
||
namespace dulcificum::gcode::ast | ||
{ | ||
class G0 : public Entry<R"(G0((?:\sX(?<X>\d+(?:\.\d+)?))|(?:\sY(?<Y>\d+(?:\.\d+)?))|(?:\sZ(?<Z>\d+(?:\.\d+)?))|(?:\sE(?<E>\d+(?:\.\d+)?))|(?:\sF(?<F>\d+(?:\.\d+)?)))*$)"> | ||
{ | ||
public: | ||
G0() = delete; | ||
G0(size_t index, std::string line) | ||
: Entry{ index, std::move(line) } {}; | ||
}; | ||
} // namespace dulcificum::gcode::ast | ||
|
||
#endif // DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_AST_G0_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#ifndef DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_AST_G1_H | ||
#define DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_AST_G1_H | ||
|
||
#include "dulcificum/gcode/ast/entry.h" | ||
|
||
namespace dulcificum::gcode::ast | ||
{ | ||
class G1 : public Entry<R"(G1((?:\sX(?<X>\d+(?:\.\d+)?))|(?:\sY(?<Y>\d+(?:\.\d+)?))|(?:\sZ(?<Z>\d+(?:\.\d+)?))|(?:\sE(?<E>\d+(?:\.\d+)?))|(?:\sF(?<F>\d+(?:\.\d+)?)))*$)"> | ||
{ | ||
public: | ||
G1() = delete; | ||
G1(size_t index, std::string line) | ||
: Entry{ index, std::move(line) } {}; | ||
}; | ||
} // namespace dulcificum::gcode::ast | ||
|
||
#endif // DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_AST_G1_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#ifndef DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_PARSE_H | ||
#define DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_PARSE_H | ||
|
||
#include "dulcificum/gcode/ast/ast.h" | ||
|
||
#include <spdlog/spdlog.h> | ||
|
||
#include <cstddef> | ||
#include <sstream> | ||
#include <string> | ||
#include <string_view> | ||
|
||
namespace dulcificum::gcode | ||
{ | ||
|
||
ast::ast_t parse(std::string_view content) | ||
{ | ||
std::istringstream stream(content.data()); | ||
std::string line; | ||
ast::ast_t ast; | ||
|
||
size_t index{ 0 }; | ||
while (std::getline(stream, line)) | ||
{ | ||
ast.emplace_back(ast::factory(index++, line)); | ||
} | ||
|
||
return ast; | ||
} | ||
|
||
|
||
} // namespace dulcificum::gcode | ||
|
||
#endif // DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_PARSE_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#ifndef DULCIFICUM_INCLUDE_DULCIFICUM_UTILS_CHAR_RANGE_LITERAL_H | ||
#define DULCIFICUM_INCLUDE_DULCIFICUM_UTILS_CHAR_RANGE_LITERAL_H | ||
|
||
#include <algorithm> | ||
#include <cstddef> | ||
|
||
namespace dulcificum::utils | ||
{ | ||
|
||
// NOLINTBEGIN | ||
template<std::size_t N> | ||
struct CharRangeLiteral | ||
{ | ||
constexpr CharRangeLiteral(const char (&str)[N]) noexcept | ||
{ | ||
std::copy_n(str, N, value); | ||
} | ||
char value[N]; ///< The character array holding the literal | ||
}; | ||
// NOLINTEND | ||
|
||
} // namespace dulcificum::utils | ||
|
||
#endif // DULCIFICUM_INCLUDE_DULCIFICUM_UTILS_CHAR_RANGE_LITERAL_H |