From 535d856a39b177642724bcfe6009985bf4262dbd Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Fri, 5 Aug 2016 15:03:14 +0200 Subject: o More flexible parsing. More rules needs to be updated. --- core/KicadNetLexer.g4 | 2 +- core/KicadNetParser.g4 | 2 +- core/README.md | 10 ++++++++++ core/include/trygvis/kicad.h | 3 +++ core/kicad.cpp | 18 ++++++++++++++---- 5 files changed, 29 insertions(+), 6 deletions(-) create mode 100644 core/README.md (limited to 'core') diff --git a/core/KicadNetLexer.g4 b/core/KicadNetLexer.g4 index e10f7e7..6d3ce95 100644 --- a/core/KicadNetLexer.g4 +++ b/core/KicadNetLexer.g4 @@ -26,7 +26,7 @@ STRING: '"' ~["]* '"'; INTEGER: [0-9]+; ID - : [/+~\_\-\.\*\?/a-zA-Z0-9]+ + : [/+~\_\-\.\*\?/a-zA-Z0-9:]+ ; BlockComment diff --git a/core/KicadNetParser.g4 b/core/KicadNetParser.g4 index 3971111..9914084 100644 --- a/core/KicadNetParser.g4 +++ b/core/KicadNetParser.g4 @@ -27,7 +27,7 @@ code: ; component: - '(' 'comp' ref value libsource keyValue* ')' + '(' 'comp' (ref | value | libsource | keyValue)* ')' ; field: diff --git a/core/README.md b/core/README.md new file mode 100644 index 0000000..585cca1 --- /dev/null +++ b/core/README.md @@ -0,0 +1,10 @@ +# Building + + mkdir build + cd build + cmake .. \ + -DAntlr4_DIR=$HOME/opt/antlr4-cpp/lib/cmake/Antlr4 \ + -DCMAKE_INSTALL_PREFIX=$HOME/opt/kicad-utils \ + -DCMAKE_INSTALL_RPATH_USE_LINK_PATH=YES + make + make install diff --git a/core/include/trygvis/kicad.h b/core/include/trygvis/kicad.h index 0c5005d..bae250b 100644 --- a/core/include/trygvis/kicad.h +++ b/core/include/trygvis/kicad.h @@ -78,6 +78,9 @@ public: explicit kicad_parse_exception(const std::vector &messages) : runtime_error("Parse error"), messages(messages) {} + explicit kicad_parse_exception(const std::string &message) : + runtime_error("Parse error"), messages({message}) {} + ~kicad_parse_exception() {} const std::vector messages; diff --git a/core/kicad.cpp b/core/kicad.cpp index 1a6ca2f..a553b62 100644 --- a/core/kicad.cpp +++ b/core/kicad.cpp @@ -112,12 +112,22 @@ public: nodes.emplace_back(ref, pin); } + template + static + Ref onlyOne(const vector> items, const antlr4::ParserRuleContext *ctx, const string &parent, const string &item) { + if(items.size() != 1) { + throw kicad_parse_exception("Bad netlist: expected only one " + item + " inside of " + parent + ". Line: " + to_string(ctx->start->getLine()) + ":" + to_string(ctx->start->getCharPositionInLine())); + } + return items[0]; + } + virtual void exitComponent(KicadNetParser::ComponentContext *ctx) override { - auto ref = strings.get(ctx->ref()->string()); - auto value = strings.get(ctx->value()->string()); + auto ref = strings.get(onlyOne(ctx->ref(), ctx, "comp", "ref")->string()); + auto value = strings.get(onlyOne(ctx->value(), ctx, "comp", "value")->string()); + auto libsource = onlyOne(ctx->libsource(), ctx, "comp", "libsource"); - lib_source ls{strings.get(ctx->libsource()->lib()->string()), - strings.get(ctx->libsource()->part()->string())}; + lib_source ls{strings.get(libsource->lib()->string()), + strings.get(libsource->part()->string())}; components.emplace_back(ref, value, ls); } -- cgit v1.2.3