Skip to content
This repository was archived by the owner on Mar 8, 2020. It is now read-only.

Commit 9a0143a

Browse files
author
Juanjo Alvarez
committed
Num nodes un-sugarcoated
1 parent 380d63c commit 9a0143a

69 files changed

Lines changed: 520 additions & 802 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

ANNOTATION.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
| /self::\*\[@InternalType='Module'\]//\*\[@InternalType='USub'\] | OpNegative |
3939
| /self::\*\[@InternalType='Module'\]//\*\[@InternalType='Str'\] | StringLiteral, Expression |
4040
| /self::\*\[@InternalType='Module'\]//\*\[@InternalType='Bytes'\] | ByteStringLiteral, Expression |
41-
| /self::\*\[@InternalType='Module'\]//\*\[@InternalType='NumLiteral'\] | NumberLiteral, Expression |
41+
| /self::\*\[@InternalType='Module'\]//\*\[@InternalType='Num'\] | NumberLiteral, Expression |
4242
| /self::\*\[@InternalType='Module'\]//\*\[@InternalType='BoolLiteral'\] | BooleanLiteral, Expression |
4343
| /self::\*\[@InternalType='Module'\]//\*\[@InternalType='JoinedStr'\] | StringLiteral, Expression |
4444
| /self::\*\[@InternalType='Module'\]//\*\[@InternalType='JoinedStr'\]/\*\[@InternalType='FormattedValue'\] | Expression, Incomplete |
@@ -149,7 +149,6 @@
149149
| /self::\*\[@InternalType='Module'\]//\*\[@InternalType='While'\]/\*\[@internalRole\]\[@internalRole='test'\] | WhileCondition |
150150
| /self::\*\[@InternalType='Module'\]//\*\[@InternalType='While'\]/\*\[@InternalType='While\.orelse'\] | IfElse |
151151
| /self::\*\[@InternalType='Module'\]//\*\[@InternalType='Pass'\] | Noop, Statement |
152-
| /self::\*\[@InternalType='Module'\]//\*\[@InternalType='Num'\] | NumberLiteral, Expression |
153152
| /self::\*\[@InternalType='Module'\]//\*\[@InternalType='Assert'\] | Assert, Statement |
154153
| /self::\*\[@InternalType='Module'\]//\*\[@InternalType='Exec'\] | Call, Expression |
155154
| /self::\*\[@InternalType='Module'\]//\*\[@InternalType='Exec'\]/\*\[@internalRole\]\[@internalRole='body'\] | CallPositionalArgument |

driver/normalizer/annotation.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ var AnnotationRules = On(Any).Self(
9090

9191
On(HasInternalType(pyast.Str)).Roles(StringLiteral, Expression),
9292
On(HasInternalType(pyast.Bytes)).Roles(ByteStringLiteral, Expression),
93-
On(HasInternalType(pyast.NumLiteral)).Roles(NumberLiteral, Expression),
93+
On(HasInternalType(pyast.Num)).Roles(NumberLiteral, Expression),
9494
On(HasInternalType(pyast.BoolLiteral)).Roles(BooleanLiteral, Expression),
9595
On(HasInternalType(pyast.JoinedStr)).Roles(StringLiteral, Expression).Children(
9696
On(HasInternalType(pyast.FormattedValue)).Roles(Expression, Incomplete),
@@ -288,7 +288,6 @@ var AnnotationRules = On(Any).Self(
288288
On(HasInternalType("While.orelse")).Roles(IfElse),
289289
),
290290
On(HasInternalType(pyast.Pass)).Roles(Noop, Statement),
291-
On(HasInternalType(pyast.Num)).Roles(NumberLiteral, Expression),
292291
// FIXME: this is the annotated assignment (a: annotation = 3) not exactly Assignment
293292
// it also lacks AssignmentValue and AssignmentVariable (see how to add them)
294293
On(HasInternalType(pyast.Assert)).Roles(Assert, Statement),

driver/normalizer/parser.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ var ToNoder = &native.ObjectToNoder{
1919
"id": true, // Name nodes
2020
"attr": true, // something.attr
2121
"arg": true, // function arguments
22-
"LiteralValue": true, // num/constant literal
22+
"LiteralValue": true, // boolean/None literal
2323
"s": true, // string/byte
24+
"n": true, // numeric literal
2425
"noop_line": true, // Comment/Noop (non significative whitespace)
2526
},
2627
SyntheticTokens: map[string]string{

driver/normalizer/pyast/pyast.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ const (
102102
NotEq = "NotEq"
103103
NotIn = "NotIn"
104104
Num = "Num"
105-
NumLiteral = "NumLiteral"
106105
Operator = "operator"
107106
Or = "Or"
108107
Param = "Param"

native/python_package/python_driver/astimprove.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ def remainder_noops(self):
286286

287287

288288
_TOKEN_KEYS = set(
289-
("module", "name", "id", "attr", "arg", "LiteralValue", "s")
289+
("module", "name", "id", "attr", "arg", "LiteralValue", "s", "n")
290290
)
291291

292292
_SYNTHETIC_TOKENS = {
@@ -499,22 +499,22 @@ def visit_NameConstant(self, node):
499499
node["ast_type"] = "NameConstant"
500500
return node
501501

502-
def visit_Num(self, node):
503-
if isinstance(node["n"], int):
504-
ret_dict = { "NumType": "int", "LiteralValue": node["n"] }
505-
elif isinstance(node["n"], float):
506-
ret_dict = { "NumType": "float", "LiteralValue": node["n"] }
507-
elif isinstance(node["n"], complex):
508-
ret_dict = {
509-
"NumType": "complex",
510-
"LiteralValue": {"real": node["n"]["real"],
511-
"imaginary": node["n"]["imag"]},
512-
}
513-
514-
node["ast_type"] = "NumLiteral"
515-
node.update(ret_dict)
516-
node.pop("n", None)
517-
return node
502+
# def visit_Num(self, node):
503+
# if isinstance(node["n"], int):
504+
# ret_dict = { "NumType": "int", "LiteralValue": node["n"] }
505+
# elif isinstance(node["n"], float):
506+
# ret_dict = { "NumType": "float", "LiteralValue": node["n"] }
507+
# elif isinstance(node["n"], complex):
508+
# ret_dict = {
509+
# "NumType": "complex",
510+
# "LiteralValue": {"real": node["n"]["real"],
511+
# "imaginary": node["n"]["imag"]},
512+
# }
513+
514+
# node["ast_type"] = "NumLiteral"
515+
# node.update(ret_dict)
516+
# node.pop("n", None)
517+
# return node
518518

519519
def visit_other(self, node):
520520
for field in node.get("_fields", []):

tests/annotations.py.native

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,12 @@
2929
"lineno": 1
3030
},
3131
"value": {
32-
"LiteralValue": 1,
33-
"NumType": "int",
34-
"ast_type": "NumLiteral",
32+
"ast_type": "Num",
3533
"col_offset": 10,
3634
"end_col_offset": 10,
3735
"end_lineno": 1,
38-
"lineno": 1
36+
"lineno": 1,
37+
"n": 1
3938
}
4039
},
4140
{
@@ -117,13 +116,12 @@
117116
"end_lineno": 4,
118117
"lineno": 4,
119118
"value": {
120-
"LiteralValue": 0,
121-
"NumType": "float",
122-
"ast_type": "NumLiteral",
119+
"ast_type": "Num",
123120
"col_offset": 12,
124121
"end_col_offset": 14,
125122
"end_lineno": 4,
126-
"lineno": 4
123+
"lineno": 4,
124+
"n": 0
127125
}
128126
}
129127
],

tests/annotations.py.uast

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ Module {
5252
. . . . . . internalRole: target
5353
. . . . . }
5454
. . . . }
55-
. . . . 2: NumLiteral {
55+
. . . . 2: Num {
5656
. . . . . Roles: NumberLiteral,Expression
5757
. . . . . TOKEN "1"
5858
. . . . . StartPosition: {
@@ -66,7 +66,6 @@ Module {
6666
. . . . . . Col: 10
6767
. . . . . }
6868
. . . . . Properties: {
69-
. . . . . . NumType: int
7069
. . . . . . internalRole: value
7170
. . . . . }
7271
. . . . }
@@ -244,7 +243,7 @@ Module {
244243
. . . . . . . . Col: 10
245244
. . . . . . . }
246245
. . . . . . . Children: {
247-
. . . . . . . . 0: NumLiteral {
246+
. . . . . . . . 0: Num {
248247
. . . . . . . . . Roles: NumberLiteral,Expression
249248
. . . . . . . . . TOKEN "0"
250249
. . . . . . . . . StartPosition: {
@@ -258,7 +257,6 @@ Module {
258257
. . . . . . . . . . Col: 14
259258
. . . . . . . . . }
260259
. . . . . . . . . Properties: {
261-
. . . . . . . . . . NumType: float
262260
. . . . . . . . . . internalRole: value
263261
. . . . . . . . . }
264262
. . . . . . . . }

tests/aritmeticops.py.native

Lines changed: 42 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,24 @@
1313
"ast_type": "BinOp",
1414
"col_offset": 1,
1515
"left": {
16-
"LiteralValue": 1,
17-
"NumType": "int",
18-
"ast_type": "NumLiteral",
16+
"ast_type": "Num",
1917
"col_offset": 1,
2018
"end_col_offset": 1,
2119
"end_lineno": 1,
22-
"lineno": 1
20+
"lineno": 1,
21+
"n": 1
2322
},
2423
"lineno": 1,
2524
"op": {
2625
"ast_type": "Add"
2726
},
2827
"right": {
29-
"LiteralValue": 2,
30-
"NumType": "int",
31-
"ast_type": "NumLiteral",
28+
"ast_type": "Num",
3229
"col_offset": 3,
3330
"end_col_offset": 3,
3431
"end_lineno": 1,
35-
"lineno": 1
32+
"lineno": 1,
33+
"n": 2
3634
}
3735
}
3836
},
@@ -44,26 +42,24 @@
4442
"ast_type": "BinOp",
4543
"col_offset": 1,
4644
"left": {
47-
"LiteralValue": 1,
48-
"NumType": "int",
49-
"ast_type": "NumLiteral",
45+
"ast_type": "Num",
5046
"col_offset": 1,
5147
"end_col_offset": 1,
5248
"end_lineno": 2,
53-
"lineno": 2
49+
"lineno": 2,
50+
"n": 1
5451
},
5552
"lineno": 2,
5653
"op": {
5754
"ast_type": "Sub"
5855
},
5956
"right": {
60-
"LiteralValue": 2,
61-
"NumType": "int",
62-
"ast_type": "NumLiteral",
57+
"ast_type": "Num",
6358
"col_offset": 3,
6459
"end_col_offset": 3,
6560
"end_lineno": 2,
66-
"lineno": 2
61+
"lineno": 2,
62+
"n": 2
6763
}
6864
}
6965
},
@@ -75,26 +71,24 @@
7571
"ast_type": "BinOp",
7672
"col_offset": 1,
7773
"left": {
78-
"LiteralValue": 1,
79-
"NumType": "int",
80-
"ast_type": "NumLiteral",
74+
"ast_type": "Num",
8175
"col_offset": 1,
8276
"end_col_offset": 1,
8377
"end_lineno": 3,
84-
"lineno": 3
78+
"lineno": 3,
79+
"n": 1
8580
},
8681
"lineno": 3,
8782
"op": {
8883
"ast_type": "Mult"
8984
},
9085
"right": {
91-
"LiteralValue": 2,
92-
"NumType": "int",
93-
"ast_type": "NumLiteral",
86+
"ast_type": "Num",
9487
"col_offset": 3,
9588
"end_col_offset": 3,
9689
"end_lineno": 3,
97-
"lineno": 3
90+
"lineno": 3,
91+
"n": 2
9892
}
9993
}
10094
},
@@ -106,26 +100,24 @@
106100
"ast_type": "BinOp",
107101
"col_offset": 1,
108102
"left": {
109-
"LiteralValue": 1,
110-
"NumType": "int",
111-
"ast_type": "NumLiteral",
103+
"ast_type": "Num",
112104
"col_offset": 1,
113105
"end_col_offset": 1,
114106
"end_lineno": 4,
115-
"lineno": 4
107+
"lineno": 4,
108+
"n": 1
116109
},
117110
"lineno": 4,
118111
"op": {
119112
"ast_type": "Div"
120113
},
121114
"right": {
122-
"LiteralValue": 2,
123-
"NumType": "int",
124-
"ast_type": "NumLiteral",
115+
"ast_type": "Num",
125116
"col_offset": 3,
126117
"end_col_offset": 3,
127118
"end_lineno": 4,
128-
"lineno": 4
119+
"lineno": 4,
120+
"n": 2
129121
}
130122
}
131123
},
@@ -137,26 +129,24 @@
137129
"ast_type": "BinOp",
138130
"col_offset": 1,
139131
"left": {
140-
"LiteralValue": 1,
141-
"NumType": "int",
142-
"ast_type": "NumLiteral",
132+
"ast_type": "Num",
143133
"col_offset": 1,
144134
"end_col_offset": 1,
145135
"end_lineno": 5,
146-
"lineno": 5
136+
"lineno": 5,
137+
"n": 1
147138
},
148139
"lineno": 5,
149140
"op": {
150141
"ast_type": "FloorDiv"
151142
},
152143
"right": {
153-
"LiteralValue": 2,
154-
"NumType": "int",
155-
"ast_type": "NumLiteral",
144+
"ast_type": "Num",
156145
"col_offset": 4,
157146
"end_col_offset": 4,
158147
"end_lineno": 5,
159-
"lineno": 5
148+
"lineno": 5,
149+
"n": 2
160150
}
161151
}
162152
},
@@ -168,26 +158,24 @@
168158
"ast_type": "BinOp",
169159
"col_offset": 1,
170160
"left": {
171-
"LiteralValue": 1,
172-
"NumType": "int",
173-
"ast_type": "NumLiteral",
161+
"ast_type": "Num",
174162
"col_offset": 1,
175163
"end_col_offset": 1,
176164
"end_lineno": 6,
177-
"lineno": 6
165+
"lineno": 6,
166+
"n": 1
178167
},
179168
"lineno": 6,
180169
"op": {
181170
"ast_type": "Mod"
182171
},
183172
"right": {
184-
"LiteralValue": 2,
185-
"NumType": "int",
186-
"ast_type": "NumLiteral",
173+
"ast_type": "Num",
187174
"col_offset": 3,
188175
"end_col_offset": 3,
189176
"end_lineno": 6,
190-
"lineno": 6
177+
"lineno": 6,
178+
"n": 2
191179
}
192180
}
193181
},
@@ -199,26 +187,24 @@
199187
"ast_type": "BinOp",
200188
"col_offset": 1,
201189
"left": {
202-
"LiteralValue": 1,
203-
"NumType": "int",
204-
"ast_type": "NumLiteral",
190+
"ast_type": "Num",
205191
"col_offset": 1,
206192
"end_col_offset": 1,
207193
"end_lineno": 7,
208-
"lineno": 7
194+
"lineno": 7,
195+
"n": 1
209196
},
210197
"lineno": 7,
211198
"op": {
212199
"ast_type": "Pow"
213200
},
214201
"right": {
215-
"LiteralValue": 2,
216-
"NumType": "int",
217-
"ast_type": "NumLiteral",
202+
"ast_type": "Num",
218203
"col_offset": 4,
219204
"end_col_offset": 4,
220205
"end_lineno": 7,
221-
"lineno": 7
206+
"lineno": 7,
207+
"n": 2
222208
}
223209
}
224210
}

0 commit comments

Comments
 (0)