From a8256910d40a0eee85bf539a3f120c9d92485f3f Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Tue, 26 Jul 2016 23:50:23 +0200 Subject: o Working version with support for Arduino parts. --- kicad.cpp | 121 +++++++++++++++++++++++++------------------------------------- 1 file changed, 48 insertions(+), 73 deletions(-) (limited to 'kicad.cpp') 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 #include #include +#include +#include -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 -class ParseTreeProperty { -public: - virtual V get(Ref 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 _annotations; - -private: -}; - -} // namespace antlr - + return nullptr; } -using namespace std; -using namespace trygvis::antlr; - -namespace trygvis { -namespace kicad { - -opt netlist::find_component(string ref) const { +opt 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 netlist::find_component(string ref) const { return std::experimental::nullopt; } +vector netlist::find_usage_of(const string &ref) const { + vector 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 &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 -- cgit v1.2.3