11package normalizer
22
33import (
4- . "gopkg.in/bblfsh/sdk.v2/uast/transformer"
54 "gopkg.in/bblfsh/sdk.v2/uast"
5+ . "gopkg.in/bblfsh/sdk.v2/uast/transformer"
66)
77
88var Preprocess = Transformers ([][]Transformer {
@@ -21,14 +21,17 @@ var Normalize = Transformers([][]Transformer{
2121// Preprocessors is a block of AST preprocessing rules rules.
2222var Preprocessors = []Mapping {
2323 ObjectToNode {
24- OffsetKey : "startOffset" ,
24+ OffsetKey : "startOffset" ,
2525 EndOffsetKey : "endOffset" ,
2626 }.Mapping (),
2727}
2828
2929func mapString (key string ) Mapping {
3030 return MapSemantic (key , uast.String {}, MapObj (
31- Obj {uast .KeyToken : Var ("val" )},
31+ Obj {
32+ uast .KeyToken : Var ("val" ),
33+ "children" : Arr (),
34+ },
3235 Obj {
3336 "Value" : Var ("val" ),
3437 "Format" : String ("" ),
@@ -38,8 +41,13 @@ func mapString(key string) Mapping {
3841
3942func mapIdentifier (key string ) Mapping {
4043 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+ },
4351 ))
4452}
4553
@@ -50,19 +58,19 @@ var Normalizers = []Mapping{
5058 "children" : Arr (
5159 Obj {
5260 uast .KeyType : Var ("_type_namedsymbol" ),
53- uast .KeyPos : Var ("_pos_namedsymbol" ),
61+ uast .KeyPos : Var ("_pos_namedsymbol" ),
5462 "children" : Arr (
5563 Obj {
5664 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" ),
5967 },
6068 ),
6169 },
6270 Obj {
6371 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" ),
6674 },
6775 ),
6876 },
@@ -73,8 +81,7 @@ var Normalizers = []Mapping{
7381 "Name" : Var ("name" ),
7482 }),
7583 "Node" : UASTType (uast.Function {}, Obj {
76- "Type" : UASTType (uast.FunctionType {}, Obj {
77- }),
84+ "Type" : UASTType (uast.FunctionType {}, Obj {}),
7885 "Body" : UASTType (uast.Block {}, Obj {
7986 "Statements" : Var ("body" ),
8087 }),
@@ -84,10 +91,31 @@ var Normalizers = []Mapping{
8491 },
8592 )),
8693
87- mapString ("unevaluated_string2" ),
88- mapString ("string" ),
8994 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" ),
91119 mapString ("File_reference" ),
92120
93121 mapIdentifier ("word" ),
@@ -97,18 +125,18 @@ var Normalizers = []Mapping{
97125 MapSemantic ("Comment" , uast.Comment {}, MapObj (
98126 Obj {
99127 uast .KeyToken : CommentText ([2 ]string {"#" , "" }, "comm" ),
100- "children" : Arr (),
128+ "children" : Arr (),
101129 },
102130 CommentNode (false , "comm" , nil ),
103131 )),
104132
105133 MapSemantic ("file_reference" , uast.RuntimeImport {}, MapObj (
106134 Obj {
107135 uast .KeyToken : Var ("file" ),
136+ "children" : Arr (),
108137 },
109138 Obj {
110139 "Path" : Var ("file" ),
111140 },
112141 )),
113-
114142}
0 commit comments