Skip to content

Commit f6fce55

Browse files
committed
Add AD extended command parsing
1 parent 042c867 commit f6fce55

File tree

14 files changed

+641
-9
lines changed

14 files changed

+641
-9
lines changed
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 AD : public Command {
7+
private:
8+
std::string apertureId;
9+
10+
AD() = delete;
11+
12+
public:
13+
AD(const std::string_view& apertureId);
14+
std::string getNodeName() const override;
15+
std::string getApertureId() const;
16+
};
17+
} // namespace gerber
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#pragma once
2+
#include "gerber/ast/aperture/AD.hpp"
3+
#include <optional>
4+
#include <string>
5+
6+
namespace gerber {
7+
class ADC : public AD {
8+
private:
9+
double diameter;
10+
std::optional<double> holeDiameter;
11+
12+
public:
13+
ADC(const std::string_view& apertureId, double diameter, std::optional<double> holeDiameter
14+
);
15+
std::string getNodeName() const override;
16+
double getDiameter() const;
17+
std::optional<double> getHoleDiameter() const;
18+
};
19+
} // namespace gerber
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#pragma once
2+
#include "gerber/ast/aperture/AD.hpp"
3+
#include <optional>
4+
#include <string>
5+
6+
namespace gerber {
7+
class ADO : public AD {
8+
private:
9+
double width;
10+
double height;
11+
std::optional<double> holeDiameter;
12+
13+
public:
14+
ADO(const std::string_view& apertureId,
15+
double width,
16+
double height,
17+
std::optional<double> holeDiameter);
18+
19+
std::string getNodeName() const override;
20+
double getWidth() const;
21+
double getHeight() const;
22+
std::optional<double> getHoleDiameter() const;
23+
};
24+
} // namespace gerber
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#pragma once
2+
#include "gerber/ast/aperture/AD.hpp"
3+
#include <optional>
4+
#include <string>
5+
6+
namespace gerber {
7+
class ADP : public AD {
8+
private:
9+
double outerDiameter;
10+
double verticesCount;
11+
std::optional<double> rotation;
12+
std::optional<double> holeDiameter;
13+
14+
public:
15+
ADP(const std::string_view& apertureId,
16+
double outerDiameter,
17+
double verticesCount,
18+
std::optional<double> rotation,
19+
std::optional<double> holeDiameter);
20+
21+
std::string getNodeName() const override;
22+
double getOuterDiameter() const;
23+
double getVerticesCount() const;
24+
std::optional<double> getRotation() const;
25+
std::optional<double> getHoleDiameter() const;
26+
};
27+
} // namespace gerber
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#pragma once
2+
#include "gerber/ast/aperture/AD.hpp"
3+
#include <optional>
4+
#include <string>
5+
6+
namespace gerber {
7+
class ADR : public AD {
8+
private:
9+
double width;
10+
double height;
11+
std::optional<double> holeDiameter;
12+
13+
public:
14+
ADR(const std::string_view& apertureId,
15+
double width,
16+
double height,
17+
std::optional<double> holeDiameter);
18+
19+
std::string getNodeName() const override;
20+
double getWidth() const;
21+
double getHeight() const;
22+
std::optional<double> getHoleDiameter() const;
23+
};
24+
} // namespace gerber

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
#include "./file.hpp"
55
#include "./node.hpp"
66

7+
#include "./aperture/AD.hpp"
8+
#include "./aperture/ADC.hpp"
9+
#include "./aperture/ADO.hpp"
10+
#include "./aperture/ADR.hpp"
11+
#include "./aperture/ADP.hpp"
12+
713
#include "./d_codes/D01.hpp"
814
#include "./d_codes/D02.hpp"
915
#include "./d_codes/D03.hpp"

cpp/gerber/include/gerber/parser.hpp

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ namespace gerber {
2222
std::string_view full_source;
2323
location_t global_index;
2424
// Regular expressions cache
25+
// Aperture
26+
std::regex ad_header_regex{"^%ADD([1-9][0-9]*)([a-zA-Z0-9_]+),"};
2527
// D-codes
2628
std::regex d_code_regex{"^[Dd]0*([1-9][0-9]*)\\*"};
2729
// G-codes
@@ -30,15 +32,76 @@ namespace gerber {
3032
// Properties
3133
std::regex fs_regex{"^%FS([TL])([IA])X([0-9])([0-9])Y([0-9])([0-9])\\*%"};
3234
std::regex mo_regex{"^%MO(IN|MM)\\*%"};
35+
// Helper regex
36+
std::regex float_regex{"([+-]?((([0-9]+)(\\.[0-9]*)?)|(\\.[0-9]+)))"};
3337

3438
public:
3539
Parser();
3640

37-
File parse(const std::string& source);
41+
File parse(const std::string& source);
42+
43+
private:
3844
location_t parse_global(const std::string_view& source, const location_t& index);
3945
[[noreturn]] void throw_syntax_error();
40-
offset_t parse_g_code(const std::string_view& gerber, const location_t& index);
41-
offset_t parse_d_code(const std::string_view& source, const location_t& index);
46+
47+
offset_t parse_aperture(const std::string_view& source);
48+
offset_t parse_aperture_definition(const std::string_view& source);
49+
offset_t parse_standard_aperture_c_tail(
50+
const std::string_view& source, const std::string& aperture_id
51+
);
52+
53+
template <typename aperture_type>
54+
offset_t parse_standard_aperture_r_like_tail(
55+
const std::string_view& source, const std::string& aperture_id
56+
) {
57+
std::string_view rest = source;
58+
offset_t offset = 0;
59+
60+
auto width = consume_float(rest, offset);
61+
consume_char(rest, offset, 'X');
62+
auto height = consume_float(rest, offset);
63+
64+
std::optional<double> holeDiameter = std::nullopt;
65+
if (try_consume_char(rest, offset, 'X')) {
66+
holeDiameter = consume_float(rest, offset);
67+
}
68+
69+
consume_char(rest, offset, '*');
70+
consume_char(rest, offset, '%');
71+
72+
commands.push_back(
73+
std::make_shared<aperture_type>(aperture_id, width, height, holeDiameter)
74+
);
75+
return offset;
76+
}
77+
78+
offset_t parse_standard_aperture_p_tail(
79+
const std::string_view& source, const std::string& aperture_id
80+
);
81+
82+
/**
83+
* Parse a float from the beginning of the source string. Move source and offset
84+
* to point to the next character after the float. If there is no valid float
85+
* literal throw SyntaxError.
86+
*/
87+
double consume_float(std::string_view& source, offset_t& offset);
88+
/**
89+
* Check if next character in the source string is the expected one. If it is,
90+
* move source and offset to point to the next character. Otherwise throw SyntaxError.
91+
*/
92+
char consume_char(std::string_view& source, offset_t& offset, char expected);
93+
/**
94+
* Check if next character in the source string is the expected one. If it is,
95+
* move source, offset to point to the next character and return True. Otherwise
96+
* return False.
97+
*/
98+
bool try_consume_char(std::string_view& source, offset_t& offset, char expected);
99+
100+
offset_t match_char(const std::string_view& source, char expected);
101+
std::string match_float(const std::string_view& source);
102+
103+
offset_t parse_g_code(const std::string_view& gerber, const location_t& index);
104+
offset_t parse_d_code(const std::string_view& source, const location_t& index);
42105

43106
template <typename coordinate_type>
44107
offset_t parse_coordinate(const std::string_view& source, const location_t& index) {

cpp/gerber/src/ast/aperture/AD.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include "gerber/ast/aperture/AD.hpp"
2+
3+
namespace gerber {
4+
AD::AD(const std::string_view& apertureId_) :
5+
apertureId(apertureId_) {}
6+
7+
std::string AD::getNodeName() const {
8+
return "AD";
9+
}
10+
11+
std::string AD::getApertureId() const {
12+
return apertureId;
13+
}
14+
} // namespace gerber

cpp/gerber/src/ast/aperture/ADC.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
#include "gerber/ast/aperture/ADC.hpp"
3+
4+
namespace gerber {
5+
ADC::ADC(
6+
const std::string_view& apertureId_, double diameter_, std::optional<double> holeDiameter_
7+
) :
8+
AD(apertureId_),
9+
diameter(diameter_),
10+
holeDiameter(holeDiameter_) {}
11+
12+
std::string ADC::getNodeName() const {
13+
return "ADC";
14+
}
15+
16+
double ADC::getDiameter() const {
17+
return diameter;
18+
}
19+
20+
std::optional<double> ADC::getHoleDiameter() const {
21+
return holeDiameter;
22+
}
23+
} // namespace gerber

cpp/gerber/src/ast/aperture/ADO.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
#include "gerber/ast/aperture/ADO.hpp"
3+
4+
namespace gerber {
5+
ADO::ADO(
6+
const std::string_view& apertureId_,
7+
double width_,
8+
double height_,
9+
std::optional<double> holeDiameter_
10+
) :
11+
AD(apertureId_),
12+
width(width_),
13+
height(height_),
14+
holeDiameter(holeDiameter_) {}
15+
16+
std::string ADO::getNodeName() const {
17+
return "ADO";
18+
}
19+
20+
double ADO::getWidth() const {
21+
return width;
22+
}
23+
24+
double ADO::getHeight() const {
25+
return height;
26+
}
27+
28+
std::optional<double> ADO::getHoleDiameter() const {
29+
return holeDiameter;
30+
}
31+
} // namespace gerber

0 commit comments

Comments
 (0)