1 # Licensed under the Apache License, Version 2.0 (the "License");
2 # you may not use this file except in compliance with the License.
3 # You may obtain a copy of the License at:
5 # http://www.apache.org/licenses/LICENSE-2.0
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS,
9 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 # See the License for the specific language governing permissions and
11 # limitations under the License.
14 import pyparsing as pp
16 # Some useful primitives
17 ident = pp.Word(pp.alphas + "_", pp.alphas + pp.nums + "_")
18 intNum = pp.Word(pp.nums)
19 hexNum = pp.Literal("0x") + pp.Word(pp.hexnums)
20 octalNum = pp.Literal("0") + pp.Word("01234567")
21 integer = (hexNum | octalNum | intNum) + \
22 pp.Optional(pp.Literal("ULL") | pp.Literal("LL") | pp.Literal("L"))
23 floatNum = pp.Regex(r'\d+(\.\d*)?([eE]\d+)?') + pp.Optional(pp.Literal("f"))
24 char = pp.Literal("'") + pp.Word(pp.printables, exact=1) + pp.Literal("'")
25 arrayIndex = integer | ident
27 lbracket = pp.Literal("(").suppress()
28 rbracket = pp.Literal(")").suppress()
29 lbrace = pp.Literal("{").suppress()
30 rbrace = pp.Literal("}").suppress()
31 comma = pp.Literal(",").suppress()
32 equals = pp.Literal("=").suppress()
33 dot = pp.Literal(".").suppress()
34 semicolon = pp.Literal(";").suppress()
36 # initializer := { [member = ] (variable | expression | { initializer } ) }
39 typeSpec = pp.Optional("unsigned") + \
40 pp.oneOf("int long short float double char u8 i8 void") + \
41 pp.Optional(pp.Word("*"), default="")
42 typeCast = pp.Combine( "(" + ( typeSpec | typeName ) + ")" ).suppress()
44 string = pp.Combine(pp.OneOrMore(pp.QuotedString(quoteChar='"',
45 escChar='\\', multiline=True)), adjacent=False)
46 literal = pp.Optional(typeCast) + (integer | floatNum | char | string)
47 var = pp.Combine(pp.Optional(typeCast) + varName +
48 pp.Optional("[" + arrayIndex + "]"))
50 # This could be more complete, but suffices for our uses
51 expr = (literal | var)
53 """Parse and render a block of text into a Python dictionary."""
55 """Compiled PyParsing BNF"""
59 super(Parser, self).__init__()
60 self._parser = self.BNF()
63 raise NotImplementedError
66 raise NotImplementedError
68 def parse(self, input):
69 item = self._parser.parseString(input).asList()
70 return self.item(item)
73 """Parser for function-like macros - without the closing semi-colon."""
74 class ParserFunctionMacro(Parser):
76 # VLIB_CONFIG_FUNCTION (unix_config, "unix")
78 params = pp.Group(pp.ZeroOrMore(expr + comma) + expr)
79 macroParams = lbracket + params + rbracket
81 return macroName + macroParams
87 "function": item[1][0],
93 """Parser for function-like macros with a closing semi-colon."""
94 class ParseFunctionMacroStmt(ParserFunctionMacro):
96 # VLIB_CONFIG_FUNCTION (unix_config, "unix");
97 function_macro = super(ParseFunctionMacroStmt, self).BNF()
98 mi = function_macro + semicolon
99 mi.ignore(pp.cppStyleComment)
105 Parser for our struct initializers which are composed from a
106 function-like macro, equals sign, and then a normal C struct initializer
109 class MacroInitializer(ParserFunctionMacro):
111 # VLIB_CLI_COMMAND (show_sr_tunnel_command, static) = {
112 # .path = "show sr tunnel",
113 # .short_help = "show sr tunnel [name <sr-tunnel-name>]",
114 # .function = show_sr_tunnel_fn,
119 member = pp.Combine(dot + varName + pp.Optional("[" + arrayIndex + "]"),
123 entry = pp.Group(pp.Optional(member + equals, default="") + value)
124 entries = (pp.ZeroOrMore(entry + comma) + entry + pp.Optional(comma)) | \
125 (pp.ZeroOrMore(entry + comma))
127 cs << (lbrace + entries + rbrace)
130 params = pp.Group(pp.ZeroOrMore(expr + comma) + expr)
131 macroParams = lbracket + params + rbracket
133 function_macro = super(MacroInitializer, self).BNF()
134 mi = function_macro + equals + pp.Group(cs) + semicolon
135 mi.ignore(pp.cppStyleComment)
139 def item(self, item):
147 for param in item[2]:
148 r["value"][param[0]] = html.escape(param[1])