/
parse.ts
173 lines (143 loc) · 4.67 KB
/
parse.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/**
* A parser for Haskell.
*/
/**
* EasyDoc.
*/
import { IParams } from "../../interfaces";
import { BaseParse } from "../parse";
/**
* A parser for Haskell.
*
* @export
* @class HaskellParse
* @extends {BaseParse}
*/
export class HaskellParse extends BaseParse {
/**
* Creates an instance of HaskellParse.
*
* @param {string} documentText The text of the document.
* @param {string} docType The type of documentation to make.
* @memberof HaskellParse
*/
constructor(documentText: string, docType: string) {
super(documentText, docType);
}
/**
* The parsed block of what to document.
*
* @abstract
* @param {string[]} rows The rows to get the block from.
* @returns {string[]} The rows of the block.
* @memberof HaskellParse
*/
public parseBlock(newlineRows: string[]): string[] {
const lines = this.splitLines(newlineRows);
const functionRows: string[] = [lines[0]];
const regex = /^(\s)*/g;
const startIndentation = lines[0].match(regex);
for (const line of lines.slice(1)) {
const indentation = line.match(regex);
if (line.length === 0) {
continue;
}
if (indentation <= startIndentation) {
break;
}
functionRows.push(line);
}
return functionRows;
}
/**
* The parsed params.
*
* @abstract
* @param {string} params The params to parse.
* @returns {IParams} The parsed params.
* @memberof HaskellParse
*/
public parseParams(params: string): IParams {
if (params === undefined) {
return {
paramList: undefined,
template: undefined,
};
}
const paramList = params.replace(/[^,\w:]+/g, "").split(",");
const templateList = paramList.map((param) => `$[${param}]`);
const template = templateList.join(", ");
if (paramList.length === 1 && paramList[0].length === 0) {
return {
paramList: undefined,
template: undefined,
};
}
return {
paramList,
template,
};
}
/**
* The parsed information gathered from the parent node.
*
* @param {number} childIndex The function's start index.
* @returns {{ [key: string]: string }} A regular expression group consisting of
* the name of the parent and what constructor it is.
* @memberof HaskellParse
*/
public parseParent(childIndex: number): { [key: string]: string } {
const newlineRows = this.documentText.split("\n");
let tempStr = this.escapeStrings(newlineRows);
tempStr = this.escapeComments(tempStr.split("\n"));
const tmp = tempStr.split("\n");
const returningString = tmp.slice(0, childIndex);
const indentationRegex = /^(\s)*/g;
const startIndentation = this.documentText[childIndex].match(indentationRegex);
let blockIndex = 0;
for (const line of returningString.slice().reverse()) {
const indentation = line.match(indentationRegex);
if (line.length === 0) {
continue;
}
if (indentation < startIndentation) {
break;
}
blockIndex++;
}
const parentLine = returningString[returningString.length - blockIndex];
this.regex.lastIndex = 0;
const parent = this.regex.exec(parentLine);
// tslint:disable-next-line:max-line-length
return parent !== null ? parent.groups : {export: undefined, abstract: undefined, default: undefined, cosnt: undefined, name: undefined};
}
/**
* Split the lines to what the language actually represents.
*
* @private
* @param {string[]} rows The rows to split.
* @returns {string[]} The real representation of the rows.
* @memberof HaskellParse
*/
private splitLines(rows: string[]): string[] {
let escapeNewLine = false;
const outStr: string[] = [];
for (const line of rows) {
if (outStr.length >= 1) {
if (escapeNewLine) {
const i = outStr.length - 1;
outStr[i] = outStr[i].concat(line.trim());
escapeNewLine = false;
} else {
outStr.push(line);
if (line.trim().endsWith("\\")) {
escapeNewLine = true;
}
}
} else {
outStr.push(line);
}
}
return outStr;
}
}