KiCad PCB EDA Suite
Loading...
Searching...
No Matches
library_table_grammar.h
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 * @author Jon Evans <[email protected]>
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#ifndef LIBRARY_TABLE_GRAMMAR_H
22#define LIBRARY_TABLE_GRAMMAR_H
23
24#include <pegtl.hpp>
25
26/*
27 * Grammar for a KiCad library stored in s-expression format. This parser is generic and
28 * supports all known types of library tables -- the differences in code between things like
29 * symbol and footprint libraries are handled elsewhere in the architecture.
30 */
31
33{
34using namespace tao::pegtl;
35using eof = tao::pegtl::eof;
36
37namespace KEYWORDS
38{
39 struct LIB : TAO_PEGTL_STRING( "lib" ) {};
40 struct NAME : TAO_PEGTL_STRING( "name" ) {};
41 struct TYPE : TAO_PEGTL_STRING( "type" ) {};
42 struct URI : TAO_PEGTL_STRING( "uri" ) {};
43 struct OPTIONS : TAO_PEGTL_STRING( "options" ) {};
44 struct DESCR : TAO_PEGTL_STRING( "descr" ) {};
45 struct HIDDEN : TAO_PEGTL_STRING( "hidden" ) {};
46 struct DISABLED : TAO_PEGTL_STRING( "disabled" ) {};
47
48 struct SYM_LIB_TABLE : TAO_PEGTL_STRING( "sym_lib_table" ) {};
49 struct FP_LIB_TABLE : TAO_PEGTL_STRING( "fp_lib_table" ) {};
50 struct DESIGN_BLOCK_LIB_TABLE : TAO_PEGTL_STRING( "design_block_lib_table" ) {};
51 struct VERSION : TAO_PEGTL_STRING( "version" ) {};
52}
53
54struct LPAREN : TAO_PEGTL_STRING( "(" ) {};
55struct RPAREN : TAO_PEGTL_STRING( ")" ) {};
56
57// An s-expression identifier token
58struct TOKEN : plus< not_one< '(', ')', ' ', '\t', '\n', '\r' > > {};
59
60struct QUOTED_TEXT : if_must< one< '"' >, until< one< '"' > > > {};
61
62// Inner expression
63struct SEXPR_CONTENT : star<
64 sor< QUOTED_TEXT, TOKEN, seq< LPAREN, must< SEXPR_CONTENT, RPAREN > > >
65 > {};
66
67// Outer (top-level) expression
68struct SEXPR : if_must< LPAREN, SEXPR_CONTENT, RPAREN > {};
69
70struct TABLE_TYPE : sor<
71 KEYWORDS::SYM_LIB_TABLE,
72 KEYWORDS::FP_LIB_TABLE,
73 KEYWORDS::DESIGN_BLOCK_LIB_TABLE
74 > {};
75
76struct LIB_PROPERTY_KEY : sor<
77 KEYWORDS::NAME,
78 KEYWORDS::TYPE,
79 KEYWORDS::URI,
80 KEYWORDS::OPTIONS,
81 KEYWORDS::DESCR
82 > {};
83
84// Allow both quoted and non-quoted (whitespace-free) strings
85struct PROPERTY_VALUE : sor< QUOTED_TEXT, TOKEN > {};
86
87// (key value) or (key "value")
88struct LIB_PROPERTY : seq<
89 LPAREN,
90 LIB_PROPERTY_KEY,
91 plus< space >,
92 PROPERTY_VALUE,
93 RPAREN
94 > {};
95
96// (version %d)
97struct TABLE_VERSION : seq<
98 LPAREN,
99 KEYWORDS::VERSION,
100 plus< space >,
101 PROPERTY_VALUE,
102 RPAREN
103 > {};
104
105struct HIDDEN_MARKER : seq< LPAREN, KEYWORDS::HIDDEN, RPAREN > {};
106struct DISABLED_MARKER : seq< LPAREN, KEYWORDS::DISABLED, RPAREN > {};
107
108// (lib (name ...)(type ...)...)
109struct LIB_ROW : if_must<
110 pad< LPAREN, space >,
111 KEYWORDS::LIB,
112 plus< space >,
113 plus< pad< LIB_PROPERTY, space > >,
114 pad_opt<HIDDEN_MARKER, space>,
115 pad_opt<DISABLED_MARKER, space>,
116 pad< RPAREN, space>
117 > {};
118
119struct LIB_TABLE : if_must<
120 LPAREN,
121 TABLE_TYPE,
122 pad_opt< TABLE_VERSION, space >,
123 star< pad< LIB_ROW, space > >,
124 pad< RPAREN, space >
125 > {};
126
127struct LIB_TABLE_FILE : seq< LIB_TABLE, eof > {};
128
129} // namespace LIBRARY_TABLE_GRAMMAR
130
131#endif //LIBRARY_TABLE_GRAMMAR_H