Advertisement
Guest User

SchematicFileGrammar_def.hpp

a guest
Mar 2nd, 2017
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.58 KB | None | 0 0
  1. #pragma once
  2. #include <Parse/BoostSpiritConfig.h>
  3.  
  4. #include <boost/spirit/home/x3.hpp>
  5. #include <boost/spirit/home/x3/support/utility/annotate_on_success.hpp>
  6. #include "SchematicFileParserConfig.hpp"
  7.  
  8. #include <boost/fusion/adapted.hpp>
  9. // #include <boost/fusion/include/io.hpp>
  10. // #include <boost/fusion/include/vector.hpp>
  11.  
  12. #include "SchematicFileAST.hpp"
  13. #include "SchematicFileAST_Adapted.hpp"
  14. #include "SchematicFileGrammar.hpp"
  15. #include "SchematicFileGrammarTypes.hpp"
  16.  
  17. #include <vector>
  18. #include "error_handler.hpp"
  19.  
  20. #include <boost/optional/optional_io.hpp>
  21.  
  22. namespace client {
  23. namespace KicadFileParser_grammar {
  24.  
  25. namespace x3    = boost::spirit::x3;
  26. namespace ascii = boost::spirit::x3::ascii;
  27.  
  28.     using x3::uint_;
  29.     using x3::char_;
  30.     using x3::float_;
  31.     using x3::lit;
  32.  
  33.     using x3::int_;
  34.     using x3::lit;
  35.     using x3::double_;
  36.     using x3::lexeme;
  37.     using x3::no_skip;
  38.     using x3::omit;
  39.     using x3::repeat;
  40.     using x3::with;
  41.     using x3::eps;
  42.     using x3::get;
  43.     using x3::_attr;
  44.     using x3::_pass;
  45.     using x3::_val;
  46.     using x3::hex;
  47.  
  48.     boost::spirit::x3::int_parser<unsigned, 16, 2, 2> hex_; // This allows for a hex doublet
  49.  
  50.     // Visit 3
  51.     schematic_type const                    schematic                    = "schematic";
  52.     bitmapImage_type const                  bitmapImage                  = "bitmapImage";
  53.     busEntry_type const                     busEntry                     = "busEntry";
  54.     wireSegment_type const                  wireSegment                  = "wireSegment";
  55.     textNote_type const                     textNote                     = "textNote";
  56.     schematicHeader_type const              schematicHeader              = "schematicHeader";
  57.     schematicDescription_type const         schematicDescription         = "schematicDescription";
  58.     globalLabel_type const                  globalLabel                  = "globalLabel";
  59.     busSegment_type const                   busSegment                   = "busSegment";
  60.     label_type const                        label                        = "label";
  61.     componentAlternateReference_type const  componentAlternateReference  = "componentAlternateReference";
  62.     componentField_type const               componentField               = "componentField";
  63.     component_type const                    component                    = "component";
  64.     dottedLineSegment_type const            dottedLineSegment            = "dottedLineSegment";
  65.     heirarchicalSheetSymbolLabel_type const heirarchicalSheetSymbolLabel = "heirarchicalSheetSymbolLabel";
  66.     heirarchicalSheetSymbol_type const      heirarchicalSheetSymbol      = "heirarchicalSheetSymbol";
  67.     heirarchicalLabel_type const            heirarchicalLabel            = "heirarchicalLabel";
  68.     junction_type const                     junction                     = "junction";
  69.     noConnect_type const                    noConnect                    = "noConnect";
  70.     nonQuotedString_type const              nonQuotedString              = "nonQuotedString";
  71.     nonQuotedStringMultiWord_type const     nonQuotedStringMultiWord     = "nonQuotedStringMultiWord";
  72.     optionalChar_type const                 optionalChar                 = "optionalChar";
  73.     quotedString_type const                 quotedString                 = "quotedString";
  74.  
  75.     // Small generic parsers
  76.     const auto nonQuotedString_def          = lexeme[*(char_ - lit(" ")) > lit(" ")];
  77.     const auto nonQuotedStringMultiWord_def = lexeme[*(char_ - lit("\n")) > lit("\n")];
  78.     const auto quotedString_def             = lexeme[lit('"') > *(char_ - lit('"')) > lit('"')];
  79.     const auto optionalChar_def             = no_skip[lit(' ') > (char_ - (lit(' ') | lit("\n")))];
  80.  
  81.     const auto bitmapImage_def =
  82.         lit("$Bitmap")
  83.         > lit("Pos") > uint_ > uint_
  84.         > lit("Scale") > float_
  85.         > lit("Data")
  86.         > *(hex_)            // The Data itself
  87.         > lit("EndData")
  88.         > lit("$EndBitmap"); // x and y
  89.  
  90.     const auto busEntry_def =
  91.         lit("Entry")
  92.         > nonQuotedString
  93.         > nonQuotedString
  94.         > uint_ > uint_
  95.         > uint_ > uint_;
  96.  
  97.     const auto busSegment_def =
  98.         lit("Wire") >> lit("Bus") >> lit("Line")
  99.         > uint_ > uint_
  100.         > uint_ > uint_;
  101.  
  102.     const auto wireSegment_def =
  103.         lit("Wire") >> lit("Wire") >> lit("Line")
  104.         > uint_ > uint_  // Start X and y
  105.         > uint_ > uint_; // End X and Y
  106.  
  107.     const auto textNote_def =
  108.         lit("Text")
  109.         >> lit("Notes")
  110.         > uint_ > uint_
  111.         > char_ > uint_
  112.         > nonQuotedString > uint_
  113.         >  nonQuotedStringMultiWord;
  114.  
  115.     const auto componentAlternateReference_def =
  116.         lit("AR")
  117.         > lit("Path=") > quotedString
  118.         > lit("Ref=") > quotedString
  119.         > lit("Part=") > quotedString
  120.  
  121.         > lit("AR")
  122.         > lit("Path=") > quotedString
  123.         > lit("Ref=") > quotedString
  124.         > lit("Part=") > quotedString;
  125.  
  126.     const auto label_def =
  127.         lit("Text")
  128.         >> lit("Label")
  129.         > uint_ > uint_
  130.         > char_ > uint_
  131.         > lit("~")
  132.         > uint_
  133.         > nonQuotedStringMultiWord;
  134.  
  135.     const auto schematicDescription_def =
  136.         lit("$Descr") > char_ > int_ > int_  > int_
  137.         > lit("encoding") > lexeme[+(char_ - "\n") > "\n"]
  138.         > lit("Sheet") > int_ > int_
  139.         > lit("Title") > quotedString
  140.         > lit("Date") > quotedString
  141.         > lit("Rev") > quotedString
  142.         > lit("Comp") > quotedString
  143.         > lit("Comment1") > quotedString
  144.         > lit("Comment2") > quotedString
  145.         > lit("Comment3") > quotedString
  146.         > lit("Comment4") > quotedString
  147.         > lit("$EndDescr");
  148.     const auto schematicHeader_def =
  149.  
  150.         lit("EESchema Schematic File Version")
  151.         > int_
  152.         > +lexeme[(lit("LIBS:") > *(char_ - lit("\n ")) > lit("\n "))] // Dump the libs
  153.         > lit("EELAYER")
  154.         > int_
  155.         > int_
  156.         > lit("EELAYER")
  157.         > lit("END")
  158.         > schematicDescription;
  159.  
  160.     const auto componentField_def =
  161.         lit("F")
  162.         > uint_
  163.         > quotedString
  164.         > char_
  165.         > int_
  166.         > int_
  167.         > uint_
  168.         > uint_
  169.         > char_
  170.         > nonQuotedString
  171.         > -quotedString;
  172.     const auto dottedLineSegment_def =
  173.         lit("Wire") >> lit("Notes") >> lit("Line")
  174.         > uint_ > uint_
  175.         > uint_ > uint_;
  176.  
  177.     const auto heirarchicalLabel_def =
  178.         lit("Text")
  179.         >> lit("HLabel")
  180.         > uint_ > uint_
  181.         > char_
  182.         > uint_
  183.         > nonQuotedString
  184.         > lit("~")
  185.         > uint_
  186.         > nonQuotedStringMultiWord;
  187.  
  188.     // #TODO rename this to symbol fields
  189.     const auto heirarchicalSheetSymbolLabel_def=
  190.         lexeme[lit("F") > uint_]
  191.         > quotedString
  192.         > -(char_ - int_)
  193.         > -(char_ - int_)
  194.         > uint_
  195.         > -uint_
  196.         > -uint_;
  197.  
  198.     const auto heirarchicalSheetSymbol_def =
  199.         lit("$Sheet")
  200.         > lit("S")
  201.         > uint_
  202.         > uint_
  203.         > uint_
  204.         > uint_
  205.         > lit("U")
  206.         > hex
  207.         > *(heirarchicalSheetSymbolLabel)
  208.         > lit("$EndSheet");
  209.  
  210.     const auto component_def =
  211.         lit("$Comp")
  212.         > lit("L") > nonQuotedString > nonQuotedString
  213.         > lit("U") > uint_ > uint_ > hex
  214.         > lit("P") > uint_ > uint_
  215.         > -componentAlternateReference
  216.         > *componentField
  217.         > uint_ > uint_ > uint_
  218.         > int_ > int_ > int_ > int_
  219.         > lit("$EndComp");
  220.  
  221.     const auto junction_def =
  222.         lit("Connection")
  223.         > lit("~")
  224.         > uint_ > uint_; // x and ye
  225.  
  226.     const auto noConnect_def =
  227.         lit("NoConn") > lit("~")
  228.         > uint_ > uint_; // x and y
  229.  
  230.     const auto globalLabel_def =
  231.         lit("Text")
  232.         >> lit("GLabel")
  233.         > uint_ > uint_
  234.         > char_ > uint_ > nonQuotedString > nonQuotedString;
  235.  
  236.     const auto schematic_def =
  237.         schematicHeader
  238.         > *(
  239.             label
  240.             | textNote
  241.             | noConnect
  242.             | wireSegment
  243.             | busSegment
  244.             | dottedLineSegment
  245.             | busEntry
  246.             | heirarchicalLabel
  247.             | globalLabel
  248.             | junction
  249.             | heirarchicalSheetSymbol
  250.             | bitmapImage
  251.             | component
  252.             )
  253.         > lit("$EndSCHEMATC");
  254.  
  255.     BOOST_SPIRIT_DEFINE
  256.     (
  257.         schematic,
  258.         bitmapImage,
  259.         busEntry,
  260.         wireSegment,
  261.         textNote,
  262.         schematicHeader,
  263.         schematicDescription,
  264.         busSegment,
  265.         componentAlternateReference,
  266.         componentField,
  267.         globalLabel,
  268.         junction,
  269.         heirarchicalSheetSymbol,
  270.         heirarchicalSheetSymbolLabel,
  271.         heirarchicalLabel,
  272.         component,
  273.         label,
  274.         noConnect,
  275.         dottedLineSegment,
  276.         quotedString,
  277.         nonQuotedString,
  278.         nonQuotedStringMultiWord,
  279.         optionalChar
  280.  
  281.     )
  282.  
  283.     struct schematic_class : error_handler_base, x3::annotate_on_success {};
  284.     struct bitmapImage_class : x3::annotate_on_success {};
  285.     struct busEntry_class : x3::annotate_on_success {};
  286.     struct wireSegment_class : x3::annotate_on_success {};
  287.     struct textNote_class : x3::annotate_on_success {};
  288.     struct schematicDescription_class : x3::annotate_on_success {};
  289.     struct schematicHeader_class : x3::annotate_on_success {}; // #TODO add rectshape to lib
  290.     struct globalLabel_class : x3::annotate_on_success {};
  291.     struct busSegment_class : x3::annotate_on_success {};
  292.     struct componentField_class : x3::annotate_on_success {};
  293.     struct componentAlternateReference_class : x3::annotate_on_success {};
  294.  
  295.     struct component_class : x3::annotate_on_success {};
  296.     struct heirarchicalSheetSymbol_class : x3::annotate_on_success {};
  297.     struct heirarchicalSheetSymbolLabel_class : x3::annotate_on_success {};
  298.     struct heirarchicalLabel_class : x3::annotate_on_success {};
  299.     struct noConnect_class : x3::annotate_on_success {};
  300.     struct label_class : x3::annotate_on_success {};
  301.  
  302.     struct dottedLineSegment_class : x3::annotate_on_success {};
  303.     struct junction_class : x3::annotate_on_success {};
  304.     struct quotedString_class : x3::annotate_on_success {};
  305.     struct nonQuotedString_class : x3::annotate_on_success {};
  306.     struct nonQuotedStringMultiWord_class : x3::annotate_on_success {};
  307.     struct optionalChar_class : x3::annotate_on_success {};
  308. }
  309. }
  310.  
  311. namespace client {
  312.     const KicadFileParser_grammar::schematic_type&schematicFile()
  313.     {
  314.         return KicadFileParser_grammar::schematic;
  315.     }
  316. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement