This repository has been archived by the owner on Oct 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathfunctionParser.ts
157 lines (145 loc) · 4.55 KB
/
functionParser.ts
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
export class paramDeclaration {
constructor(public paramName, public paramType) {
this.paramName = paramName;
this.paramType = paramType;
}
}
export function getParameterText(paramList: paramDeclaration[], returnText: string): string {
var textToInsert: string = "";
textToInsert = textToInsert + '/**\n *';
paramList.forEach(element => {
if (element.paramName != '') {
textToInsert = textToInsert + ' @param ';
//if (element.paramType != '') {
textToInsert = textToInsert + '{' + element.paramType + '}' + ' ';
//}
textToInsert = textToInsert + element.paramName + '\n' + ' *';
}
});
if (returnText != '') {
textToInsert = textToInsert + ' @returns ' + returnText + '\n' + ' *';
}
textToInsert = textToInsert + '/';
return textToInsert;
}
export function getReturns(text: string): string {
var returnText: string = '';
text = text.replace(/\s/g, '');
var lastIndex = text.lastIndexOf(':');
var lastBrace = text.lastIndexOf(')');
if (lastIndex > lastBrace) {
//we have a return type
//read to end of string
var index = lastIndex + 1;
var splicedText = text.slice(index, text.length);
returnText = splicedText.match(/[a-zA-Z][a-zA-Z0-9$_]*/).toString();
// while (index < text.length) && (text..charAt(index).al.m
// returnText = returnText + text.charAt(index);
// index++;
// }
}
return returnText;
}
export function stripComments(text: string): string {
var uncommentedText: string = '';
var index = 0;
while (index != text.length) {
if ((text.charAt(index) == '/') && (text.charAt(index + 1) == '*')) {
//parse comment
if ((index + 2) != text.length) { //Check for the corner case that the selected text contains a /* right at the end
index = index + 2;
while ((text.charAt(index) != '*') && (text.charAt(index + 1) != '/')) {
index++;
}
}
index = index + 2;
}
else if ((text.charAt(index) == '/') && (text.charAt(index + 1) == '/')) {
//read to end of line
while ((text.charAt(index) != '\n') && (index < text.length)) {
index++;
}
}
else {
uncommentedText = uncommentedText + text.charAt(index);
index++;
}
}
return uncommentedText;
}
//Assumes that the string passed in starts with ( and continues to ) and does not contain any comments or white space
export function getParameters(text: string): paramDeclaration[] {
var paramList: paramDeclaration[] = [];
//Start by looking for the function name declaration
var index = 0;
text = text.replace(/\s/g, '');
//Now we are at the first non whitespace character
//if it is not a '(' then this is not a valid function declaration
if (text.charAt(index) == '(') {
//count the number of matching opening and closing braces. Keep parsing until 0
var numBraces = 1;
index++;
while ((numBraces != 0) && (index != text.length)) {
//Now we are at a non whitespace character. Assume it is the parameter name
var name: string = '';
while ((text.charAt(index) != ':') && (text.charAt(index) != ',') && (text.charAt(index) != ')') && (index < text.length)) {
name = name + text.charAt(index);
index++;
}
if (index < text.length) {
//Now we are at a : or a ',', skip then read until a , to get the param type
var type: string = '';
if (text.charAt(index) == ':') {
index++;
//we have a type to process
if (text.charAt(index) == '(') {
var startNumBraces = numBraces;
numBraces++;
type = type + text.charAt(index);
index++;
//we have encountered a function type
//read all the way through until the numBraces = startNumBraces
while ((numBraces != startNumBraces) && (index < text.length)) {
if (text.charAt(index) == ')') {
numBraces--;
}
else if (text.charAt(index) == '(') {
numBraces++;
}
type = type + text.charAt(index);
index++;
}
if (index < text.length) {
//Now read up to either a , or a )
while ((text.charAt(index) != ',') && (text.charAt(index) != ')')) {
type = type + text.charAt(index);
index++;
}
if (text.charAt(index) == ')') {
numBraces--;
}
}
}
else {
while ((text.charAt(index) != ',') && (text.charAt(index) != ')') && (index != text.length)) {
type = type + text.charAt(index);
index++;
}
if (text.charAt(index) == ')') {
numBraces--;
}
}
}
else {
//no type is specified
type = '';
}
paramList.push(new paramDeclaration(name, type));
if (index < text.length) {
index++;
}
}
}
}
return paramList;
}