1
1
package normalizer
2
2
3
3
import (
4
- . "gopkg.in/bblfsh/sdk.v2/uast/transformer"
5
4
"gopkg.in/bblfsh/sdk.v2/uast"
5
+ . "gopkg.in/bblfsh/sdk.v2/uast/transformer"
6
6
)
7
7
8
8
var Preprocess = Transformers ([][]Transformer {
@@ -21,14 +21,17 @@ var Normalize = Transformers([][]Transformer{
21
21
// Preprocessors is a block of AST preprocessing rules rules.
22
22
var Preprocessors = []Mapping {
23
23
ObjectToNode {
24
- OffsetKey : "startOffset" ,
24
+ OffsetKey : "startOffset" ,
25
25
EndOffsetKey : "endOffset" ,
26
26
}.Mapping (),
27
27
}
28
28
29
29
func mapString (key string ) Mapping {
30
30
return MapSemantic (key , uast.String {}, MapObj (
31
- Obj {uast .KeyToken : Var ("val" )},
31
+ Obj {
32
+ uast .KeyToken : Var ("val" ),
33
+ "children" : Arr (),
34
+ },
32
35
Obj {
33
36
"Value" : Var ("val" ),
34
37
"Format" : String ("" ),
@@ -38,8 +41,13 @@ func mapString(key string) Mapping {
38
41
39
42
func mapIdentifier (key string ) Mapping {
40
43
return MapSemantic (key , uast.Identifier {}, MapObj (
41
- Obj {uast .KeyToken : Var ("val" )},
42
- Obj {"Name" : Var ("val" )},
44
+ Obj {
45
+ uast .KeyToken : Var ("val" ),
46
+ "children" : Arr (),
47
+ },
48
+ Obj {
49
+ "Name" : Var ("val" ),
50
+ },
43
51
))
44
52
}
45
53
@@ -50,19 +58,19 @@ var Normalizers = []Mapping{
50
58
"children" : Arr (
51
59
Obj {
52
60
uast .KeyType : Var ("_type_namedsymbol" ),
53
- uast .KeyPos : Var ("_pos_namedsymbol" ),
61
+ uast .KeyPos : Var ("_pos_namedsymbol" ),
54
62
"children" : Arr (
55
63
Obj {
56
64
uast .KeyType : Var ("_type_identifier" ),
57
- uast .KeyPos : Var ("_pos_identifier" ),
58
- "Name" : Var ("name" ),
65
+ uast .KeyPos : Var ("_pos_identifier" ),
66
+ "Name" : Var ("name" ),
59
67
},
60
68
),
61
69
},
62
70
Obj {
63
71
uast .KeyType : Var ("_type_groupelem" ),
64
- uast .KeyPos : Var ("_pos_groupelem" ),
65
- "children" : Var ("body" ),
72
+ uast .KeyPos : Var ("_pos_groupelem" ),
73
+ "children" : Var ("body" ),
66
74
},
67
75
),
68
76
},
@@ -73,8 +81,7 @@ var Normalizers = []Mapping{
73
81
"Name" : Var ("name" ),
74
82
}),
75
83
"Node" : UASTType (uast.Function {}, Obj {
76
- "Type" : UASTType (uast.FunctionType {}, Obj {
77
- }),
84
+ "Type" : UASTType (uast.FunctionType {}, Obj {}),
78
85
"Body" : UASTType (uast.Block {}, Obj {
79
86
"Statements" : Var ("body" ),
80
87
}),
@@ -84,10 +91,31 @@ var Normalizers = []Mapping{
84
91
},
85
92
)),
86
93
87
- mapString ("unevaluated_string2" ),
88
- mapString ("string" ),
89
94
mapString ("string_content" ),
90
- mapString ("backquote_shellcommand" ),
95
+ mapString ("string" ),
96
+
97
+ // replace "string" (aka string interpolation) with a single "string_content"
98
+ // to a single uast:String node (already replace by previous transform)
99
+ Map (
100
+ Obj {
101
+ uast .KeyType : String ("string" ),
102
+ uast .KeyToken : Any (), // escaped string, don't need it in Semantic mode
103
+ uast .KeyPos : Var ("pos" ), // same as in the child node
104
+ "children" : One (
105
+ Part ("inner" , Obj {
106
+ uast .KeyType : String (uast .TypeOf (uast.String {})),
107
+ uast .KeyPos : Any (), // position without quotes; don't need it
108
+ }),
109
+ ),
110
+ },
111
+ // TODO(dennwc): won't work for reversal
112
+ Part ("inner" , Obj {
113
+ uast .KeyType : String (uast .TypeOf (uast.String {})),
114
+ uast .KeyPos : Var ("pos" ), // position without quotes; don't need it
115
+ }),
116
+ ),
117
+
118
+ mapString ("unevaluated_string2" ),
91
119
mapString ("File_reference" ),
92
120
93
121
mapIdentifier ("word" ),
@@ -97,18 +125,18 @@ var Normalizers = []Mapping{
97
125
MapSemantic ("Comment" , uast.Comment {}, MapObj (
98
126
Obj {
99
127
uast .KeyToken : CommentText ([2 ]string {"#" , "" }, "comm" ),
100
- "children" : Arr (),
128
+ "children" : Arr (),
101
129
},
102
130
CommentNode (false , "comm" , nil ),
103
131
)),
104
132
105
133
MapSemantic ("file_reference" , uast.RuntimeImport {}, MapObj (
106
134
Obj {
107
135
uast .KeyToken : Var ("file" ),
136
+ "children" : Arr (),
108
137
},
109
138
Obj {
110
139
"Path" : Var ("file" ),
111
140
},
112
141
)),
113
-
114
142
}
0 commit comments