aboutsummaryrefslogtreecommitdiff
path: root/kicad.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'kicad.cpp')
-rw-r--r--kicad.cpp121
1 files changed, 48 insertions, 73 deletions
diff --git a/kicad.cpp b/kicad.cpp
index 2af9bc6..0a6931d 100644
--- a/kicad.cpp
+++ b/kicad.cpp
@@ -1,79 +1,31 @@
#include "trygvis/kicad.h"
+#include "trygvis/antlr.h"
+#include "trygvis/string_utils.h"
#include <KicadNetLexer.h>
#include <KicadNetParser.h>
#include <KicadNetParserBaseListener.h>
+#include <exception>
+#include <stdexcept>
-namespace trygvis {
-namespace antlr {
-
-// This namespace is shared copied code
-
-using ParseTree = antlr4::tree::ParseTree;
-
-class MissingParseTreeProperty : public std::out_of_range {
-public:
- explicit MissingParseTreeProperty(const std::string &what) : out_of_range(what) { }
-};
-
-template<typename V, bool debug = false>
-class ParseTreeProperty {
-public:
- virtual V get(Ref<ParseTree> node) {
- return get(node.get());
- }
-
- virtual V get(ParseTree *const node) {
- if (!debug) {
- return _annotations.at(node);
- }
-
- try {
-// cerr << "node = " << node->getText() << endl;
- return _annotations.at(node);
- } catch (std::out_of_range &e) {
- std::cerr << "get(" << node << "), text=" << node->getText() << std::endl;
- std::stringstream buf;
- buf << "out of range: " << node << ", text=" << node->getText();
- auto msg = buf.str();
- std::cerr << msg << std::endl;
- throw MissingParseTreeProperty(msg);
- }
- }
-
- virtual void put(ParseTree *const node, V value) {
- if (debug) {
- std::cerr << "put(" << node << ", " << value << "), text: " << node->getText() << std::endl;
- }
- _annotations[node] = value;
- }
+using namespace std;
+using namespace trygvis::antlr;
+using namespace trygvis::string_utils;
- virtual V removeFrom(ParseTree *const node) {
- auto it = _annotations.find(node);
+namespace trygvis {
+namespace kicad {
+namespace netlist {
- if (it == _annotations.end()) {
- throw MissingParseTreeProperty(node->getText());
+const node *net::node_for_ref(const std::string &ref) const {
+ for (auto &node: nodes) {
+ if (node.ref == ref) {
+ return &node;
}
-
- return it->second;
}
-protected:
- std::map<ParseTree *, V> _annotations;
-
-private:
-};
-
-} // namespace antlr
-
+ return nullptr;
}
-using namespace std;
-using namespace trygvis::antlr;
-
-namespace trygvis {
-namespace kicad {
-
-opt<const component *> netlist::find_component(string ref) const {
+opt<const component *> netlist::find_component(const string &ref) const {
for (const component &c :components) {
int x = c.ref.compare(ref);
if (x == 0) {
@@ -83,6 +35,24 @@ opt<const component *> netlist::find_component(string ref) const {
return std::experimental::nullopt;
}
+vector<const net *> netlist::find_usage_of(const string &ref) const {
+ vector<const net *> usage;
+
+ for (auto &net : nets) {
+ if (net.nodes.size() <= 1) {
+ continue;
+ }
+
+ for (auto &node: net.nodes) {
+ if (node.ref == ref) {
+ usage.push_back(&net);
+ }
+ }
+ }
+
+ return usage;
+}
+
static
int parse(const Ref<antlr4::tree::TerminalNode> &integer) {
unsigned long long i = strtoull(integer->getText().c_str(), NULL, 10);
@@ -117,14 +87,18 @@ public:
auto code = parse(ctx->code()->INTEGER());
auto name = strings.get(ctx->name()->string());
+ if (startsWith(name, "/")) {
+ name = name.substr(1);
+ }
+
// cerr << "exitNet: " << "code=" << code << ", name=" << name << ", nodes=" << nodes.size() << endl;
- if (nodes.size() > 1) {
- cerr << "Net#" << code << ": " << name << endl;
- for (auto &node: nodes) {
- cerr << " Node: " << node.ref << "#" << node.pin << endl;
- }
- }
+// if (nodes.size() > 1) {
+// cerr << "Net#" << code << ": " << name << endl;
+// for (auto &node: nodes) {
+// cerr << " Node: " << node.ref << "#" << node.pin << endl;
+// }
+// }
nets.emplace_back(code, name, nodes);
nodes.clear();
}
@@ -142,9 +116,10 @@ public:
auto ref = strings.get(ctx->ref()->string());
auto value = strings.get(ctx->value()->string());
- cerr << "exitComponent, ref=" << ref << ", value=" << value << endl;
+ lib_source ls{strings.get(ctx->libsource()->lib()->string()),
+ strings.get(ctx->libsource()->part()->string())};
- components.emplace_back(ref, value);
+ components.emplace_back(ref, value, ls);
}
virtual void exitStringId(KicadNetParser::StringIdContext *ctx) override {
@@ -214,6 +189,6 @@ void kicad_net_loader::setDebug(bool debug) {
debug_ = debug;
}
-
+} // namespace netlist
} // namespace trygvis
} // namespace kicad