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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/* DreemGL is a collaboration between Teeming Society & Samsung Electronics, sponsored by Samsung and others.
   Copyright 2015-2016 Teeming Society. Licensed under the Apache License, Version 2.0 (the "License"); You may not use this file except in compliance with the License.
   You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing,
   software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and limitations under the License.*/

define.class(function(require, exports){

        exports.parseDoc = function parseDoc(constructor){
                if (!constructor) return

                if (!this.BlankDoc) {
                        // Build a minimal correct version of the ClassDoc structure
                        this.BlankDoc = function BlankDoc(){
                                return {
                                        class_name:"",
                                        body_text: [], // array with strings. each string = paragraph
                                        examples: [],
                                        events: [],
                                        attributes: [],
                                        state_attributes: [],
                                        methods: [],
                                        inner_classes: [],
                                        base_class_chain: []
                                }
                        }
                }

                var class_doc = this.BlankDoc()

                var proto = constructor.prototype

                if (!proto) {
                        //xxx console.log('this has do constructor, what do?', constructor)
                        return class_doc;
                }

                var p = constructor

                // build parent chain
                while(p) {
                        if (p.prototype) {
                                var prot = Object.getPrototypeOf(p.prototype);
                                if (prot) {
                                        p = prot.constructor;
                                        class_doc.base_class_chain.push({name:p.name, path:p.module? (p.module.id? p.module.id:""):"", p: p});
                                } else {
                                        p = null;
                                }
                        } else {
                                p = null;
                        }
                }

                class_doc.class_name = proto.constructor.name

                if (!this.Parser) {
                        this.Parser = require("$system/parse/onejsparser")
                }

                if (!proto.constructor.body) {
                        return;
                }
                // ok lets add the comments at the top of the class
                var ast = this.Parser.parse(proto.constructor.body.toString());

                // lets process the inner classes
                // lets do an ast match what we want is
                var class_body = ast.steps[0]

                function grabFirstCommentBelow(commentarray){
                        var res = []
                        if (!commentarray) return res
                        var last1 = false
                        for(var i = 0; i < commentarray.length; i++) {
                                var com = commentarray[i]
                                if (com === 1){
                                        if(last1 === true){
                                                break
                                        }
                                        else {
                                                last1 = true
                                        }
                                }
                                else {
                                        last1 = false
                                        res.push(com)
                                }
                        }
                        return res
                }

                function grabFirstCommentAbove(commentarray){
                        var res = []
                        if (!commentarray) return res
                        var last1 = false
                        for (var i = commentarray.length -1;i>=0;i--) {
                                var com = commentarray[i];
                                if (com === 1){
                                        if(last1 === true){
                                                break
                                        }
                                        else {
                                                last1 = true
                                        }
                                }
                                else {
                                        last1 = false
                                        res.unshift(com.trim())
                                }
                        }
                        return res
                }
                var body_steps = class_body.body.steps

                if(!body_steps[0]) return class_doc

                var classcomment = grabFirstCommentBelow(body_steps[0].cmu)
                if (classcomment) {
                        if (!class_doc.body_text) {
                                class_doc.body_text = []
                        }
                        for (i=0;i<classcomment.length;i++) {
                                class_doc.body_text.push(classcomment[i]);
                        }
                }

                for (var i = 0; i < body_steps.length; i++) {
                        var step = body_steps[i]

                        if(step.type === 'Assign'){
                                if(step.left.type === 'Key' && step.left.object.type === 'This' && step.left.key.name === 'attributes' && step.right.type === 'Object'){
                                        for(var j = 0; j < step.right.keys.length; j++){
                                                var key = step.right.keys[j]
                                                var attrname = key.key.name
                                                var attr = proto._attributes[attrname]
                                                if (!attr) {
                                                        //TODO not sure why this one has no name sometimes, plx fix
                                                        continue;
                                                }

                                                var cmt = grabFirstCommentAbove(key.cmu)
                                                var defvaluename = attr.value

                                                var typename = "typeless";
                                                if (attr.type && attr.type.name) typename = attr.type.name.toString()

                                                if(typename === 'Event'){
                                                        class_doc.events.push({name: attrname, body_text: grabFirstCommentAbove(step.cmu)})
                                                }
                                                else{
                                                        class_doc.attributes.push({name: attrname, type:typename, defvalue: defvaluename, body_text: grabFirstCommentAbove(key.cmu)})
                                                }
                                        }
                                }
                                if(step.left.type === 'Key' && step.left.object.type === 'Key' && step.left.object.object.type === 'This' &&
                                        step.left.object.key.name === 'constructor' && step.left.key.name === 'examples' && step.right.type === 'Object'){
                                        for(var j = 0; j < step.right.keys.length; j++){
                                                var key = step.right.keys[j]

                                                var example = {}
                                                example.body_text = grabFirstCommentAbove(key.cmu)

                                                var examplename = key.key.name
                                                example.name = examplename
                                                example.examplefunc = proto.constructor.examples[examplename]
                                                class_doc.examples.push(example)
                                        }
                                }

                                if(step.left.type === 'Key' && step.left.object.type === 'This' && step.right.type === 'Function'){
                                        var stepleft = step.left
                                        var method = {name:stepleft.key.name, params:[]};
                                        var stepright = step.right;

                                        method.body_text = grabFirstCommentAbove(step.cmu)

                                        for(var p in stepright.params){
                                                var param = stepright.params[p]
                                                var paramname = param.id.name
                                                var paramtag = '<' + paramname + '>'

                                                var pbody_text = []

                                                if (param.cm1) {
                                                        for (var k = 0; k < param.cm1.length;k++) {
                                                                if (param.cm1[k] != 1) {
                                                                        pbody_text.push(param.cm1[k])
                                                                }
                                                        }
                                                }

                                                param = {name: paramname, body_text: pbody_text};

                                                var remaining = [];
                                                for(var a in method.body_text){
                                                        var L = method.body_text[a];
                                                        if (L.indexOf(paramtag) === 0) {
                                                                param.body_text.push(L.substr(paramtag.length).trim());
                                                        }
                                                        else{
                                                                remaining.push(L);
                                                        }
                                                }
                                                method.params.push(param)
                                                method.body_text = remaining
                                        }
                                        class_doc.methods.push(method)
                                }
                        }
                        else if (step.type ==="Call"){
                                if (step.fn.object.type ==="Id"){
                                        if (step.fn.object.name === "define"){
                                                if (step.fn.key.name === "class"){
                                                        var innerclassname = step.args[1].value
                                                        var newclass = parseDoc(proto[innerclassname])
                                                        newclass.class_name = innerclassname;
                                                        newclass.body_text = grabFirstCommentAbove(step.cmu)
                                                        class_doc.inner_classes.push(newclass)
                                                }
                                        }
                                }
                        }
                }
                return class_doc
        }

})