Skip to content

Commit 639cdbe

Browse files
committed
Add coordinate parsing
1 parent c920e16 commit 639cdbe

File tree

15 files changed

+13866
-24
lines changed

15 files changed

+13866
-24
lines changed

cpp/gerber/include/gerber/ast/ast.hpp

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,31 @@
1-
#include "gerber/ast/command.hpp"
2-
#include "gerber/ast/enums.hpp"
3-
#include "gerber/ast/extended_command.hpp"
4-
#include "gerber/ast/file.hpp"
5-
#include "gerber/ast/node.hpp"
1+
#include "./command.hpp"
2+
#include "./enums.hpp"
3+
#include "./extended_command.hpp"
4+
#include "./file.hpp"
5+
#include "./node.hpp"
66

7-
#include "gerber/ast/g_codes/G01.hpp"
8-
#include "gerber/ast/g_codes/G02.hpp"
9-
#include "gerber/ast/g_codes/G03.hpp"
10-
#include "gerber/ast/g_codes/G04.hpp"
11-
#include "gerber/ast/g_codes/G36.hpp"
12-
#include "gerber/ast/g_codes/G37.hpp"
13-
#include "gerber/ast/g_codes/G54.hpp"
14-
#include "gerber/ast/g_codes/G55.hpp"
15-
#include "gerber/ast/g_codes/G70.hpp"
16-
#include "gerber/ast/g_codes/G71.hpp"
17-
#include "gerber/ast/g_codes/G74.hpp"
18-
#include "gerber/ast/g_codes/G75.hpp"
19-
#include "gerber/ast/g_codes/G90.hpp"
20-
#include "gerber/ast/g_codes/G91.hpp"
7+
#include "./g_codes/G01.hpp"
8+
#include "./g_codes/G02.hpp"
9+
#include "./g_codes/G03.hpp"
10+
#include "./g_codes/G04.hpp"
11+
#include "./g_codes/G36.hpp"
12+
#include "./g_codes/G37.hpp"
13+
#include "./g_codes/G54.hpp"
14+
#include "./g_codes/G55.hpp"
15+
#include "./g_codes/G70.hpp"
16+
#include "./g_codes/G71.hpp"
17+
#include "./g_codes/G74.hpp"
18+
#include "./g_codes/G75.hpp"
19+
#include "./g_codes/G90.hpp"
20+
#include "./g_codes/G91.hpp"
2121

22-
#include "gerber/ast/load/LP.hpp"
22+
#include "./load/LP.hpp"
2323

24-
#include "gerber/ast/properties/FS.hpp"
25-
#include "gerber/ast/properties/MO.hpp"
24+
#include "./other/coordinate.hpp"
25+
#include "./other/coordinate_i.hpp"
26+
#include "./other/coordinate_j.hpp"
27+
#include "./other/coordinate_x.hpp"
28+
#include "./other/coordinate_y.hpp"
29+
30+
#include "./properties/FS.hpp"
31+
#include "./properties/MO.hpp"
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#pragma once
2+
#include "gerber/ast/command.hpp"
3+
#include <string>
4+
5+
namespace gerber {
6+
class Coordinate : public Command {
7+
private:
8+
std::string value;
9+
10+
Coordinate() = delete;
11+
12+
public:
13+
Coordinate(const std::string_view& value);
14+
std::string getNodeName() const override;
15+
std::string getValue() const;
16+
};
17+
} // namespace gerber
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#pragma once
2+
#include "./coordinate.hpp"
3+
#include <string>
4+
5+
namespace gerber {
6+
class CoordinateI : public Coordinate {
7+
public:
8+
using Coordinate::Coordinate;
9+
std::string getNodeName() const override;
10+
};
11+
} // namespace gerber
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#pragma once
2+
#include "./coordinate.hpp"
3+
#include <string>
4+
5+
namespace gerber {
6+
class CoordinateJ : public Coordinate {
7+
public:
8+
using Coordinate::Coordinate;
9+
std::string getNodeName() const override;
10+
};
11+
} // namespace gerber
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#pragma once
2+
#include "./coordinate.hpp"
3+
#include <string>
4+
5+
namespace gerber {
6+
class CoordinateX : public Coordinate {
7+
public:
8+
using Coordinate::Coordinate;
9+
std::string getNodeName() const override;
10+
};
11+
} // namespace gerber
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#pragma once
2+
#include "./coordinate.hpp"
3+
#include <string>
4+
5+
namespace gerber {
6+
class CoordinateY : public Coordinate {
7+
public:
8+
using Coordinate::Coordinate;
9+
std::string getNodeName() const override;
10+
};
11+
} // namespace gerber

cpp/gerber/include/gerber/parser.hpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,32 @@ namespace gerber {
4040
[[noreturn]] void throw_syntax_error();
4141
offset_t parse_g_code(const std::string_view& gerber, const location_t& index);
4242

43+
template <typename coordinate_type>
44+
offset_t parse_coordinate(const std::string_view& source, const location_t& index) {
45+
// Shortest possible X coordinate is X0* or alike.
46+
if (source.length() < 3) {
47+
throw_syntax_error();
48+
}
49+
50+
auto length = parse_integer(source.substr(1));
51+
commands.push_back(std::make_shared<coordinate_type>(source.substr(1, length)));
52+
53+
return 1 + length;
54+
}
55+
56+
offset_t parse_integer(const std::string_view& source);
57+
58+
template <typename lambda_t>
59+
offset_t parse_while_predicate(lambda_t char_predicate, const std::string_view& source) {
60+
offset_t substring_length = 0;
61+
auto source_length = source.length();
62+
63+
while (substring_length < source_length && char_predicate(source[substring_length])) {
64+
substring_length++;
65+
}
66+
return substring_length;
67+
}
68+
4369
offset_t parse_extended_command(const std::string_view& source, const location_t& index);
4470
offset_t parse_load_command(const std::string_view& source, const location_t& index);
4571
offset_t parse_fs_command(const std::string_view& source, const location_t& index);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include "gerber/ast/other/coordinate.hpp"
2+
3+
namespace gerber {
4+
Coordinate::Coordinate(const std::string_view& value) :
5+
value(value) {}
6+
7+
std::string Coordinate::getNodeName() const {
8+
return "Coordinate";
9+
}
10+
11+
std::string Coordinate::getValue() const {
12+
return value;
13+
}
14+
} // namespace gerber
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include "gerber/ast/other/coordinate_i.hpp"
2+
3+
namespace gerber {
4+
std::string CoordinateI::getNodeName() const {
5+
return "I";
6+
}
7+
} // namespace gerber
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include "gerber/ast/other/coordinate_j.hpp"
2+
3+
namespace gerber {
4+
std::string CoordinateJ::getNodeName() const {
5+
return "J";
6+
}
7+
} // namespace gerber

0 commit comments

Comments
 (0)