Debian Codegraph @

mini_soong/parser.py

raw symbols SCIP
1 #!/usr/bin/python3
2 # Soong parser
3 #
4 # Based on an implementation of a simple JSON parser shipped with pyparsing
5 #
6 # Copyright 2006, 2007, 2016 Paul McGuire
7 # Copyright 2020 Andrej Shadura
8 #
9 # SPDX-License-Identifier: MIT
10 #
11 # Permission is hereby granted, free of charge, to any person obtaining
12 # a copy of this software and associated documentation files (the
13 # "Software"), to deal in the Software without restriction, including
14 # without limitation the rights to use, copy, modify, merge, publish,
15 # distribute, sublicense, and/or sell copies of the Software, and to
16 # permit persons to whom the Software is furnished to do so, subject to
17 # the following conditions:
18 #
19 # The above copyright notice and this permission notice shall be
20 # included in all copies or substantial portions of the Software.
21 #
22 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
25 # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
26 # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
27 # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
28 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30 import pyparsing as pp
31 from pyparsing import pyparsing_common as ppc
32 from typing import Mapping, Sequence
33 from .model import Assignment, Module
34
35 def make_keyword(kwd_str, kwd_value):
36 return pp.Keyword(kwd_str).setParseAction(pp.replaceWith(kwd_value))
37
38 TRUE = make_keyword("true", True)
39 FALSE = make_keyword("false", False)
40 NULL = make_keyword("null", None)
41
42 LBRACK, RBRACK, LBRACE, RBRACE, COLON, EQUALS, COMMA, PLUS = map(pp.Suppress, "[]{}:=,+")
43
44 quotedString = pp.quotedString().setParseAction(pp.removeQuotes)
45 number = ppc.integer()
46
47 def add_things(tokens):
48 return [tokens[0] + tokens[1]]
49
50 numberExp = (number + PLUS + number).setParseAction(add_things)
51
52 bracedMap = pp.Forward().setName("dictionary")
53 singleValue = pp.Forward()
54 listElements = pp.delimitedList(quotedString) + pp.Optional(COMMA)
55 stringList = pp.Group(LBRACK + pp.Optional(listElements, []) + RBRACK)
56
57 variables = dict()
58
59 def set_variable(tokens):
60 global variables
61 variable, value = tokens[0]
62 variables[variable] = value
63 return []
64
65 def resolve_var(tokens):
66 global variables
67 variable, = tokens[0]
68 return [variables[variable]]
69
70 def add_vars(tokens):
71 global variables
72 left, right = tokens[0]
73 return [variables[left] + variables[right]]
74
75 variableReference = pp.Group(ppc.identifier).setParseAction(resolve_var)
76
77 variableExp = pp.Group(ppc.identifier + PLUS + ppc.identifier).setParseAction(add_vars)
78
79 stringListExp = (stringList + PLUS + stringList).setParseAction(add_things)
80
81 singleValue << (
82 quotedString | numberExp | number | pp.Group(bracedMap) | TRUE | FALSE | NULL | variableExp | variableReference | stringListExp | stringList
83 )
84 singleValue.setName("value")
85
86
87 memberDef = pp.Group((quotedString | ppc.identifier) + COLON + singleValue)
88 mapMembers = pp.delimitedList(memberDef) + pp.Optional(COMMA)
89 mapMembers.setName("dictionary members")
90 bracedMap << pp.Dict(LBRACE + pp.Optional(mapMembers) + RBRACE)
91
92 comment = pp.cppStyleComment
93
94 soongAssignment = pp.Group(ppc.identifier + EQUALS + singleValue)
95 soongAssignment.setParseAction(set_variable)
96 soongModule = pp.Group(ppc.identifier + pp.Group(bracedMap)).setParseAction(Module)
97 soongStatement = soongAssignment | soongModule
98 soong = soongStatement[...]
99 soong.ignore(comment)