summaryrefslogtreecommitdiffstats
path: root/src/CodeParser/CParser.ts
blob: f26aa5f5962316cfb32f9bd64dffdf449cc2a308 (plain)
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
import { Position, TextDocumentContentChangeEvent, TextEditor, TextLine, workspace } from "vscode";
import { Config, ConfigType } from "../Config";
import Generator from "../DocGen/CGen";
import { IDocGen } from "../DocGen/DocGen";
import ICodeParser from "./CodeParser";

/**
 *
 * Parses C code for methods and signatures
 *
 * @export
 * @class CParser
 * @implements {ICodeParser}
 */
export default class CParser implements ICodeParser {
    protected activeEditor: TextEditor;
    protected activeSelection: Position;

    /**
     * @inheritdoc
     */
    public Parse(activeEdit: TextEditor, event: TextDocumentContentChangeEvent): IDocGen {
        this.activeEditor = activeEdit;
        this.activeSelection = this.activeEditor.selection.active;

        const activeLine: TextLine = this.activeEditor.document.lineAt(this.activeEditor.selection.active.line);

        const line: string = this.getLogicalLine();

        // Not a method
        if (line.length === 0) {
            return null;
        }

        const returnValue: string[] = this.getReturn(line);

        const params: string[] = this.getParams(line);
        const tparams: string[] = this.getTemplateParams(line);

        const cppGenerator: IDocGen = new Generator(
            this.activeEditor,
            this.activeSelection,
            params,
            tparams,
            returnValue,
        );
        return cppGenerator;
    }

    /***************************************************************************
                                    Implementation
     ***************************************************************************/
    protected getLogicalLine(): string {
        let logicalLine: string = "";

        let nextLine: Position = new Position(this.activeSelection.line + 1, this.activeSelection.character);

        let nextLineTxt: string = this.activeEditor.document.lineAt(nextLine.line).text.trim();

        // VSCode may enter a * on itself, we don't want that in our method
        if (nextLineTxt === "*") {
            nextLineTxt = "";
        }

        while (nextLineTxt.length === 0) { // Get first method line
            nextLine = new Position(nextLine.line + 1, nextLine.character);
            nextLineTxt = this.activeEditor.document.lineAt(nextLine.line).text.trim();
        }

        logicalLine += nextLineTxt;

        // Get method end line
        while (nextLineTxt.indexOf(";") === -1 && nextLineTxt.indexOf("{") === -1) { // Check for method end
            nextLine = new Position(nextLine.line + 1, nextLine.character);
            nextLineTxt = this.activeEditor.document.lineAt(nextLine.line).text.trim();

            logicalLine += " " + nextLineTxt;
        }

        // Not a method but some code in the file
        if (logicalLine.indexOf(")") === -1) {
            return "";
        }

        return logicalLine;
    }

    protected getReturn(method: string): string[] {
        const retVals: string[] = [];

        // Remove the compiler keywords from the signature
        const sign: string = method.replace(/(static)|(inline)|(friend)|(virtual)|(extern)|(explicit)|(const)/g, "");
        // Remove the parameters from the signature
        const returnSignature = sign.slice(0, sign.indexOf("(")).trim();

        if (returnSignature.indexOf(" ") === -1) { // Constructor or similar
            return retVals;
        }

        const returnType: string = returnSignature.substr(0, returnSignature.lastIndexOf(" "));

        switch (returnType) {
            case "bool":
                retVals.push("true");
                retVals.push("false");
                break;
            case "void":
                break;
            default:
                retVals.push(returnType);
                break;
        }

        return retVals;
    }

    protected getParams(method: string): string[] {
        const params: string[] = [];

        const endOfCtorIdx: number = method.search(/\)\s*:/g);

        let func: string = "";

        if (endOfCtorIdx !== -1) {
            func = method.substring(0, endOfCtorIdx + 1);
        } else {
            func = method;
        }

        // Get parameters from enclosing brackets
        const parameters: string = func.substring(func.indexOf("(") + 1, // Get text after opening bracket
                                                  func.lastIndexOf(")")); // Remove closing bracket

        if (parameters.length === 0) { // No parameters
            return params;
        }

        let functionPointer: boolean = false;

        let paramArr: string[] = parameters.split(",");
        paramArr = paramArr.map((item: string) => {
            // Remove any special C++ characters
            const clean: string = item.trim().replace(/[&*\[\]]/g, "");

            // function pointer special case
            if (item.indexOf("(") !== -1) {
                if (item.indexOf(")") !== -1) {
                    // Get the name of the function pointer
                    const funPtr: string = clean.substring(item.indexOf("("), item.indexOf(")"))
                                                .replace(/[()]/g, ""); // Remove the brackets

                    functionPointer = true;
                    return funPtr.trim();
                }
                return null;
            }

            // Ignore all params until the closing bracket of the function pointer params
            if (item.indexOf(")") !== -1 && functionPointer) {
                functionPointer = false;
                return null;
            }

            return clean.split(" ").pop();
        });

        return paramArr;
    }

    protected getTemplateParams(method: string): string[] {
        // Todo implement parsing of template parameters.
        const tparams: string[] = [];
        return tparams;
    }
}