You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
3.5 KiB

  1. #ifndef PTREE_H_
  2. #define PTREE_H_
  3. /*
  4. * This program source code file is part of KiCad, a free EDA CAD application.
  5. *
  6. * Copyright (C) 2013 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
  7. * Copyright (C) 2013 KiCad Developers, see CHANGELOG.TXT for contributors.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, you may find one here:
  21. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  22. * or you may search the http://www.gnu.org website for the version 2 license,
  23. * or you may write to the Free Software Foundation, Inc.,
  24. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  25. */
  26. /*
  27. Implement "KiCad s-epression" support for boost::property_tree's ptree, the 8
  28. bit string version of property_tree. The ram resident structure of the ptree is
  29. mostly compatible with one created using the xml_parser from
  30. boost::property_tree, with slight differences in the way atoms are stored. The
  31. result is you can use Format() to convert from xml to s-expression, but not the
  32. other way around. You can write a simple s-expression beautifier in just a few
  33. lines of code.
  34. The main value however is the s-expression parser, i.e. Scan(), which is an
  35. alternative to crafting a custom recursive descent parser for a particular
  36. grammar. The tipping point depends on whether you want to read only a small
  37. portion of a much larger document. If so, then using the ptree will likely be a
  38. "faster to code" route. Documentation on how to navigate a ptree can be found on
  39. the boost website and there are a number of examples in the
  40. pcbnew/eagle_plugin.cpp file in this project. Powerful path navigation support
  41. makes it easy to extract a subset of a ptree.
  42. */
  43. #include <boost/property_tree/ptree_fwd.hpp>
  44. #include <richio.h>
  45. #include <dsnlexer.h>
  46. typedef boost::property_tree::ptree PTREE;
  47. typedef const PTREE CPTREE;
  48. /**
  49. * Function Scan
  50. * fills an empty PTREE with information from a KiCad s-expresion stream. Use
  51. * a DSNLEXER with an empty keyword table as @a aLexer. Useful for parsing
  52. * s-expression files or strings of arbitrary grammars, say from a file or clipboard.
  53. * The s-expression must be "KiCad compatible". See Documentation/s-expressions.txt
  54. * for this KiCad compatible definition (it is the non-specctra mode).
  55. * And also see in tools/property_tree.cpp for example usage.
  56. *
  57. * <code>
  58. *
  59. * FILE* fp = fopen( argv[1], "r" );
  60. *
  61. * static const KEYWORD empty_keywords[1] = {};
  62. *
  63. * DSNLEXER lexer( empty_keywords, 0, fp, wxString( FROM_UTF8( argv[1] ) ) );
  64. *
  65. * try
  66. * {
  67. * PTREE doc;
  68. * Scan( &doc, &lexer );
  69. * }
  70. * catch( IO_ERROR ioe )
  71. * {
  72. * fprintf( stderr, "%s\n", TO_UTF8( ioe.errorText ) );
  73. * }
  74. *
  75. * </code>
  76. */
  77. void Scan( PTREE* aTree, DSNLEXER* aLexer ) throw ( IO_ERROR );
  78. /**
  79. * Function Format
  80. * outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
  81. */
  82. void Format( OUTPUTFORMATTER* out, int aNestLevel, int aCtl, CPTREE& aTree ) throw( IO_ERROR );
  83. #endif // PTREE_H_