-
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.
Add Gcode abstract syntax tree (AST) representations
New classes are added to define the structure of Gcode commands, comments and related entities in an AST. This includes common structure like comments, commands and optional values plus specific types for the G0 and G1 instructions. The definitions allow parsing Gcode to construct an intermediate representation that can be further processed or translated. In addition to the AST implementation, the main application (translator_main) is updated to include a preliminary use of the parser, demonstrating its functionality. The commit also includes updates to CMakeLists.txt and conanfile.py to bring in necessary dependencies. Contribute to CURA-10561
- Loading branch information
1 parent
8632ed7
commit 4c50256
Showing
13 changed files
with
262 additions
and
2 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
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,12 @@ | ||
#ifndef DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_AST_AST_H | ||
#define DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_AST_AST_H | ||
|
||
#include "dulcificum/gcode/ast/words/words.h" | ||
|
||
namespace dulcificum::gcode::ast | ||
{ | ||
auto const all_rule = boost::spirit::x3::rule<struct class_all, command_t>{} = g0_rule | g1_rule; // NOLINT | ||
|
||
} | ||
|
||
#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,28 @@ | ||
#ifndef DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_AST_RULES_H | ||
#define DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_AST_RULES_H | ||
|
||
#include <boost/spirit/home/x3.hpp> | ||
|
||
namespace dulcificum::gcode::ast | ||
{ | ||
|
||
// TODO: Figure out if the I should maybe use this pattern | ||
//struct rules { | ||
// const boost::spirit::x3::rule<struct x, double> x_rule; | ||
// | ||
// rules() : x_rule{ "x" } | ||
// { | ||
// x_rule = 'X' >> boost::spirit::x3::double_; | ||
// } | ||
//}; | ||
// NOLINTBEGIN | ||
auto const x_rule = boost::spirit::x3::rule<struct x, double>{ "x" } = 'X' >> boost::spirit::x3::double_; | ||
auto const y_rule = boost::spirit::x3::rule<struct y, double>{ "y" } = 'Y' >> boost::spirit::x3::double_; | ||
auto const z_rule = boost::spirit::x3::rule<struct z, double>{ "z" } = 'Z' >> boost::spirit::x3::double_; | ||
auto const e_rule = boost::spirit::x3::rule<struct e, double>{ "e" } = 'E' >> boost::spirit::x3::double_; | ||
auto const f_rule = boost::spirit::x3::rule<struct f, double>{ "f" } = 'F' >> boost::spirit::x3::double_; | ||
// NOLINTEND | ||
|
||
} // namespace dulcificum::gcode::ast | ||
|
||
#endif // DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_AST_RULES_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,39 @@ | ||
#ifndef DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_WORDS_G0_H | ||
#define DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_WORDS_G0_H | ||
#include "dulcificum/gcode/ast/rules.h" | ||
#include "dulcificum/gcode/ast/words/command.h" | ||
#include "dulcificum/gcode/ast/words/optional_values.h" | ||
namespace dulcificum::gcode::ast | ||
{ | ||
struct G0 : public Command<"G0"> | ||
{ | ||
std::optional<double> X{}; ///< X coordinate | ||
std::optional<double> Y{}; ///< Y coordinate | ||
std::optional<double> Z{}; ///< Z coordinate | ||
std::optional<double> F{}; ///< Feedrate | ||
}; | ||
|
||
|
||
// Construct the parser rules | ||
|
||
details::OptionalValues G0_VALUES; | ||
|
||
auto G0_ASSIGN_OPTIONAL_VALUES = [](auto& ctx) | ||
{ | ||
_val(ctx).X = G0_VALUES.val_map["x"]; | ||
_val(ctx).Y = G0_VALUES.val_map["y"]; | ||
_val(ctx).Z = G0_VALUES.val_map["z"]; | ||
_val(ctx).F = G0_VALUES.val_map["f"]; | ||
}; | ||
|
||
// clang-format off | ||
auto const g0_rule = boost::spirit::x3::rule<struct g0, G0>{ "g0" } = G0::word.data() | ||
>> *(x_rule[G0_VALUES.assign("x", G0_VALUES.val_map)] | ||
| y_rule[G0_VALUES.assign("y", G0_VALUES.val_map)] | ||
| z_rule[G0_VALUES.assign("z", G0_VALUES.val_map)] | ||
| f_rule[G0_VALUES.assign("f", G0_VALUES.val_map)]) | ||
>> boost::spirit::x3::eps[G0_ASSIGN_OPTIONAL_VALUES]; | ||
// clang-format on | ||
|
||
} // namespace dulcificum::gcode::ast | ||
#endif // DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_WORDS_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,44 @@ | ||
#ifndef DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_WORDS_G1_H | ||
#define DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_WORDS_G1_H | ||
|
||
#include "dulcificum/gcode/ast/rules.h" | ||
#include "dulcificum/gcode/ast/words/command.h" | ||
|
||
namespace dulcificum::gcode::ast | ||
{ | ||
struct G1 : public Command<"G1"> | ||
{ | ||
std::optional<double> X{}; ///< X coordinate | ||
std::optional<double> Y{}; ///< Y coordinate | ||
std::optional<double> Z{}; ///< Z coordinate | ||
std::optional<double> E{}; ///< E position | ||
std::optional<double> F{}; ///< Feedrate | ||
}; | ||
|
||
|
||
// Construct the parser rules | ||
|
||
details::OptionalValues G1_VALUES; | ||
|
||
auto G1_ASSIGN_OPTIONAL_VALUES = [](auto& ctx) | ||
{ | ||
_val(ctx).X = G0_VALUES.val_map["x"]; | ||
_val(ctx).Y = G0_VALUES.val_map["y"]; | ||
_val(ctx).Z = G0_VALUES.val_map["z"]; | ||
_val(ctx).E = G0_VALUES.val_map["e"]; | ||
_val(ctx).F = G0_VALUES.val_map["f"]; | ||
}; | ||
|
||
// clang-format off | ||
auto const g1_rule = boost::spirit::x3::rule<struct g1, G1>{ "g1" } = G1::word.data() | ||
>> *(x_rule[G1_VALUES.assign("x", G1_VALUES.val_map)] | ||
| y_rule[G1_VALUES.assign("y", G1_VALUES.val_map)] | ||
| z_rule[G1_VALUES.assign("z", G1_VALUES.val_map)] | ||
| e_rule[G1_VALUES.assign("e", G1_VALUES.val_map)] | ||
| f_rule[G1_VALUES.assign("f", G1_VALUES.val_map)]) | ||
>> boost::spirit::x3::eps[G1_ASSIGN_OPTIONAL_VALUES]; | ||
// clang-format on | ||
|
||
} // namespace dulcificum::gcode::ast | ||
|
||
#endif // DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_WORDS_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,21 @@ | ||
#ifndef DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_WORDS_COMMAND_H | ||
#define DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_WORDS_COMMAND_H | ||
|
||
#include "dulcificum/gcode/ast/words/comment.h" | ||
#include "dulcificum/gcode/ast/words/entry.h" | ||
#include "dulcificum/utils/char_range_literal.h" | ||
|
||
#include <optional> | ||
|
||
namespace dulcificum::gcode::ast | ||
{ | ||
template<utils::CharRangeLiteral Word> | ||
struct Command : public Entry | ||
{ | ||
static constexpr std::string_view word{ Word.value }; ///< The specific word | ||
std::optional<Comment> comment; ///< Optional comment related to the command | ||
}; | ||
|
||
} // namespace dulcificum::gcode::details | ||
|
||
#endif // DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_WORDS_COMMAND_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,15 @@ | ||
#ifndef DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_WORDS_COMMENT_H | ||
#define DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_WORDS_COMMENT_H | ||
|
||
#include "dulcificum/gcode/ast/words/entry.h" | ||
|
||
namespace dulcificum::gcode::ast | ||
{ | ||
struct Comment : public Entry | ||
{ | ||
static constexpr std::string_view word{ ";" }; | ||
std::string msg; ///< Message contained in the comment | ||
}; | ||
} // namespace dulcificum::gcode::details | ||
|
||
#endif // DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_WORDS_COMMENT_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_WORDS_ENTRY_H | ||
#define DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_WORDS_ENTRY_H | ||
|
||
#include <boost/spirit/home/x3/support/ast/position_tagged.hpp> | ||
|
||
namespace dulcificum::gcode::ast | ||
{ | ||
struct Entry : public boost::spirit::x3::position_tagged | ||
{ | ||
std::size_t index{ 0 }; ///< Represents the index in the source | ||
std::string raw_value; ///< Raw value as represented in the source | ||
}; | ||
|
||
} // namespace dulcificum::gcode::details | ||
|
||
|
||
#endif // DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_WORDS_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,29 @@ | ||
#ifndef DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_AST_WORDS_OPTIONAL_VALUES_H | ||
#define DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_AST_WORDS_OPTIONAL_VALUES_H | ||
|
||
#include <map> | ||
#include <string> | ||
|
||
namespace dulcificum::gcode::ast::details | ||
{ | ||
|
||
struct OptionalValues | ||
{ | ||
std::map<std::string, double> val_map; | ||
struct AssignCtxValToVar | ||
{ | ||
template<typename T> | ||
auto operator()(const std::string& key, T& map) const | ||
{ | ||
return [&, key](auto& ctx) | ||
{ | ||
map[key] = _attr(ctx); | ||
}; | ||
} | ||
} assign; | ||
}; | ||
|
||
} // namespace dulcificum::gcode::ast::details | ||
|
||
|
||
#endif // DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_AST_WORDS_OPTIONAL_VALUES_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,15 @@ | ||
#ifndef DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_WORDS_WORDS_H | ||
#define DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_WORDS_WORDS_H | ||
|
||
#include "dulcificum/gcode/ast/words/G0.h" | ||
#include "dulcificum/gcode/ast/words/G1.h" | ||
|
||
#include <variant> | ||
|
||
namespace dulcificum::gcode::ast | ||
{ | ||
using command_t = std::variant<G0, G1>; | ||
} | ||
|
||
|
||
#endif // DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_WORDS_WORDS_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 |