diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2016-08-05 15:03:14 +0200 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2016-08-05 15:03:14 +0200 |
commit | 535d856a39b177642724bcfe6009985bf4262dbd (patch) | |
tree | 4162702b31cff209a859ad22de426c7b83d38830 /core | |
parent | ff87ea9045a6e0979311ae7b25055d6b53b0a13d (diff) | |
download | kicad-utils-535d856a39b177642724bcfe6009985bf4262dbd.tar.gz kicad-utils-535d856a39b177642724bcfe6009985bf4262dbd.tar.bz2 kicad-utils-535d856a39b177642724bcfe6009985bf4262dbd.tar.xz kicad-utils-535d856a39b177642724bcfe6009985bf4262dbd.zip |
o More flexible parsing. More rules needs to be updated.
Diffstat (limited to 'core')
-rw-r--r-- | core/KicadNetLexer.g4 | 2 | ||||
-rw-r--r-- | core/KicadNetParser.g4 | 2 | ||||
-rw-r--r-- | core/README.md | 10 | ||||
-rw-r--r-- | core/include/trygvis/kicad.h | 3 | ||||
-rw-r--r-- | core/kicad.cpp | 18 |
5 files changed, 29 insertions, 6 deletions
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<std::string> &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<std::string> 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<typename T> + static + Ref<T> onlyOne(const vector<Ref<T>> 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); } |