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) + pp.Optional(
22 pp.Literal("ULL") | pp.Literal("LL") | pp.Literal("L")
24 floatNum = pp.Regex(r"\d+(\.\d*)?([eE]\d+)?") + pp.Optional(pp.Literal("f"))
25 char = pp.Literal("'") + pp.Word(pp.printables, exact=1) + pp.Literal("'")
26 arrayIndex = integer | ident
28 lbracket = pp.Literal("(").suppress()
29 rbracket = pp.Literal(")").suppress()
30 lbrace = pp.Literal("{").suppress()
31 rbrace = pp.Literal("}").suppress()
32 comma = pp.Literal(",").suppress()
33 equals = pp.Literal("=").suppress()
34 dot = pp.Literal(".").suppress()
35 semicolon = pp.Literal(";").suppress()
37 # initializer := { [member = ] (variable | expression | { initializer } ) }
41 pp.Optional("unsigned")
42 + pp.oneOf("int long short float double char u8 i8 void")
43 + pp.Optional(pp.Word("*"), default="")
45 typeCast = pp.Combine("(" + (typeSpec | typeName) + ")").suppress()
48 pp.OneOrMore(pp.QuotedString(quoteChar='"', escChar="\\", multiline=True)),
51 literal = pp.Optional(typeCast) + (integer | floatNum | char | string)
52 var = pp.Combine(pp.Optional(typeCast) + varName + pp.Optional("[" + arrayIndex + "]"))
54 # This could be more complete, but suffices for our uses
57 """Parse and render a block of text into a Python dictionary."""
61 """Compiled PyParsing BNF"""
66 super(Parser, self).__init__()
67 self._parser = self.BNF()
70 raise NotImplementedError
73 raise NotImplementedError
75 def parse(self, input):
76 item = self._parser.parseString(input).asList()
77 return self.item(item)
80 """Parser for function-like macros - without the closing semi-colon."""
83 class ParserFunctionMacro(Parser):
85 # VLIB_CONFIG_FUNCTION (unix_config, "unix")
87 params = pp.Group(pp.ZeroOrMore(expr + comma) + expr)
88 macroParams = lbracket + params + rbracket
90 return macroName + macroParams
96 "function": item[1][0],
102 """Parser for function-like macros with a closing semi-colon."""
105 class ParseFunctionMacroStmt(ParserFunctionMacro):
107 # VLIB_CONFIG_FUNCTION (unix_config, "unix");
108 function_macro = super(ParseFunctionMacroStmt, self).BNF()
109 mi = function_macro + semicolon
110 mi.ignore(pp.cppStyleComment)
116 Parser for our struct initializers which are composed from a
117 function-like macro, equals sign, and then a normal C struct initializer
122 class MacroInitializer(ParserFunctionMacro):
124 # VLIB_CLI_COMMAND (show_sr_tunnel_command, static) = {
125 # .path = "show sr tunnel",
126 # .short_help = "show sr tunnel [name <sr-tunnel-name>]",
127 # .function = show_sr_tunnel_fn,
132 dot + varName + pp.Optional("[" + arrayIndex + "]"), adjacent=False
136 entry = pp.Group(pp.Optional(member + equals, default="") + value)
137 entries = (pp.ZeroOrMore(entry + comma) + entry + pp.Optional(comma)) | (
138 pp.ZeroOrMore(entry + comma)
141 cs << (lbrace + entries + rbrace)
144 params = pp.Group(pp.ZeroOrMore(expr + comma) + expr)
145 macroParams = lbracket + params + rbracket
147 function_macro = super(MacroInitializer, self).BNF()
148 mi = function_macro + equals + pp.Group(cs) + semicolon
149 mi.ignore(pp.cppStyleComment)
153 def item(self, item):
161 for param in item[2]:
162 r["value"][param[0]] = html.escape(param[1])