File: grammator_imports.py

package info (click to toggle)
python-baron 0.10.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 2,080 kB
  • sloc: python: 26,926; makefile: 126; sh: 27
file content (171 lines) | stat: -rw-r--r-- 5,872 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
from .utils import create_node_from_token


def include_imports(pg):
    @pg.production("small_stmt : import")
    @pg.production("small_stmt : from_import")
    def separator(pack):
        (statement,) = pack
        return statement

    @pg.production("import : IMPORT dotted_as_names")
    def importeu(pack):
        (import_, dotted_as_names) = pack
        return {
            "type": "import",
            "value": dotted_as_names,
            "first_formatting": import_.hidden_tokens_before,
            "second_formatting": import_.hidden_tokens_after
        }

    @pg.production("from_import : FROM dotted_name IMPORT from_import_target")
    def from_import_with_space(pack):
        (from_, dotted_name, import_, from_import_target) = pack
        return {
            "type": "from_import",
            "targets": from_import_target,
            "first_formatting": from_.hidden_tokens_after,
            "second_formatting": import_.hidden_tokens_before,
            "third_formatting": import_.hidden_tokens_after,
            "value": dotted_name
        }

    @pg.production("from_import_target : name_as_names")
    def from_import_target_name_as_names(pack):
        (name_as_names,) = pack
        return name_as_names

    @pg.production("from_import_target : LEFT_PARENTHESIS name_as_names RIGHT_PARENTHESIS")
    def from_import_parenthesis(pack):
        (left_parenthesis, name_as_names, right_parenthesis) = pack
        return left_parenthesis.hidden_tokens_before +\
            [{"type": "left_parenthesis", "value": "("}] +\
            left_parenthesis.hidden_tokens_after +\
            name_as_names +\
            right_parenthesis.hidden_tokens_before +\
            [{"type": "right_parenthesis", "value": ")"}] +\
            right_parenthesis.hidden_tokens_after

    @pg.production("from_import_target : STAR")
    def from_import_star(pack):
        (star,) = pack
        return [{
            "type": "star",
            "value": "*",
            "first_formatting": star.hidden_tokens_before,
            "second_formatting": star.hidden_tokens_after
        }]

    @pg.production("name_as_names : name_as_names name_as_name")
    def name_as_names_name_as_name(pack):
        (name_as_names, name_as_name) = pack
        return name_as_names + name_as_name

    @pg.production("name_as_names : name_as_name")
    def name_as_names(pack):
        (name_as_name,) = pack
        return name_as_name

    @pg.production("name_as_name : NAME AS NAME")
    def name_as_name_name_as_name(pack):
        (name, as_, name2) = pack
        return [{
            "type": "name_as_name",
            "value": name.value,
            "first_formatting": as_.hidden_tokens_before,
            "second_formatting": as_.hidden_tokens_after,
            "target": name2.value
        }]

    @pg.production("name_as_name : NAME")
    def name_as_name_name(pack):
        (name,) = pack
        return [{
            "type": "name_as_name",
            "value": name.value,
            "target": "",
            "first_formatting": [],
            "second_formatting": []
        }]

    @pg.production("name_as_name : NAME SPACE")
    def name_as_name_name_space(pack):
        (name, space) = pack
        return [{
            "type": "name_as_name",
            "target": None,
            "value": name.value,
            "first_formatting": [],
            "second_formatting": []
        }] + [create_node_from_token(space)]

    @pg.production("name_as_name : comma")
    def name_as_name_comma_space(pack):
        (comma,) = pack
        return [comma]

    @pg.production("dotted_as_names : dotted_as_names comma dotted_as_name")
    def dotted_as_names_dotted_as_names_dotted_as_name(pack):
        (dotted_as_names, comma, dotted_as_names2) = pack
        return dotted_as_names + [comma] + dotted_as_names2

    @pg.production("dotted_as_names : dotted_as_name")
    def dotted_as_names_dotted_as_name(pack):
        (dotted_as_name,) = pack
        return dotted_as_name

    @pg.production("dotted_as_name : dotted_name AS NAME")
    def dotted_as_name_as(pack):
        (dotted_name, as_, name) = pack
        return [{
            "type": "dotted_as_name",
            "value": dotted_name,
            "first_formatting": as_.hidden_tokens_before,
            "second_formatting": as_.hidden_tokens_after,
            "target": name.value,
        }]

    @pg.production("dotted_as_name : dotted_name")
    def dotted_as_name(pack):
        (dotted_name,) = pack
        return [{
            "type": "dotted_as_name",
            "value": dotted_name,
            "first_formatting": [],
            "second_formatting": [],
            "target": ""
        }]

    @pg.production("dotted_name : dotted_name dotted_name_element")
    def dotted_name_elements_element(pack):
        (dotted_name, dotted_name_element) = pack
        return dotted_name + dotted_name_element

    @pg.production("dotted_name : dotted_name_element")
    def dotted_name_element(pack):
        (dotted_name_element,) = pack
        return dotted_name_element

    @pg.production("dotted_name_element : NAME")
    @pg.production("dotted_name_element : SPACE")
    def dotted_name(pack):
        (token,) = pack
        return [create_node_from_token(token)]

    @pg.production("dotted_name_element : DOT")
    def dotted_name_dot(pack):
        (dot,) = pack
        return [{
            "type": "dot",
            "first_formatting": dot.hidden_tokens_before,
            "second_formatting": dot.hidden_tokens_after,
        }]

    @pg.production("dotted_name_element : ELLIPSIS")
    def dotted_name_dot_dot_dot(pack):
        ellipsis = pack[0]
        return [{
            "type": "ellipsis",
            "first_formatting": ellipsis.hidden_tokens_before,
            "second_formatting": ellipsis.hidden_tokens_after,
        }]