summaryrefslogtreecommitdiffstats
path: root/src/CodeParser
diff options
context:
space:
mode:
authorChristoph Schlosser <christoph@linux.com>2017-10-07 14:57:01 +0200
committerChristoph Schlosser <christoph@linux.com>2017-10-07 15:16:34 +0200
commit338446ad72d3581745020aa78783f4b4d4afe4cb (patch)
tree170202da2a0bb7dc97ab03d6119836e84ebb96f5 /src/CodeParser
parentd71125855f1a8e082d4080f40f0ee2cde2e3302d (diff)
downloaddoxdocgen-338446ad72d3581745020aa78783f4b4d4afe4cb.tar.gz
Add C parser and generator
Diffstat (limited to 'src/CodeParser')
-rw-r--r--src/CodeParser/CParser.ts131
-rw-r--r--src/CodeParser/CodeParserController.ts4
-rw-r--r--src/CodeParser/CppParser.ts123
3 files changed, 139 insertions, 119 deletions
diff --git a/src/CodeParser/CParser.ts b/src/CodeParser/CParser.ts
new file mode 100644
index 0000000..09fd5da
--- /dev/null
+++ b/src/CodeParser/CParser.ts
@@ -0,0 +1,131 @@
+import { Position, TextDocumentContentChangeEvent, TextEditor, TextLine } from "vscode";
+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 method: string = this.getMethodText();
+
+ // Not a method
+ if (method.length === 0) {
+ return null;
+ }
+
+ const returnValue: string[] = this.getReturn(method);
+
+ const params: string[] = this.getParams(method);
+
+ const cppGenerator: IDocGen = new Generator(this.activeEditor, this.activeSelection, params, returnValue);
+ return cppGenerator;
+ }
+
+ /***************************************************************************
+ Implementation
+ ***************************************************************************/
+
+ protected getMethodText(): string {
+ let method: 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();
+ }
+
+ method += nextLineTxt;
+
+ // Get method end line
+ while (nextLineTxt.indexOf(")") === -1 &&
+ (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();
+
+ method += " " + nextLineTxt;
+ }
+
+ // Not a method but some code in the file
+ if (method.indexOf(")") === -1) {
+ return "";
+ }
+
+ return method;
+ }
+
+ protected getReturn(method: string): string[] {
+ const retVals: string[] = [];
+
+ // Remove the parameters from the signature
+ const returnSignature: string = method.slice(0, method.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[] = [];
+
+ // Get parameters from enclosing brackets
+ const parameters: string = method.slice(method.indexOf("(")) // Get opening bracket
+ .slice(1, method.indexOf(")")) // Remove opening bracket
+ .split(")")[0]; // Get closing bracket
+
+ if (parameters.length === 0) { // No parameters
+ return params;
+ }
+
+ let paramArr: string[] = parameters.split(",");
+ paramArr = paramArr.map((item: string) => {
+ // Remove any special C++ characters
+ const clean: string = item.trim().replace(/[&*\[\]]/g, "");
+
+ return clean.split(" ").pop();
+ });
+
+ return paramArr;
+ }
+}
diff --git a/src/CodeParser/CodeParserController.ts b/src/CodeParser/CodeParserController.ts
index 6583cde..7ed113e 100644
--- a/src/CodeParser/CodeParserController.ts
+++ b/src/CodeParser/CodeParserController.ts
@@ -1,5 +1,6 @@
import { Disposable, Position, TextDocumentContentChangeEvent, TextEditor, TextLine, window, workspace } from "vscode";
import CodeParser from "./CodeParser";
+import CParser from "./CParser";
import CppParser from "./CppParser";
/**
@@ -88,6 +89,9 @@ export default class CodeParserController {
let parser: CodeParser;
switch (lang) {
+ case "c":
+ parser = new CParser();
+ break;
case "cpp":
parser = new CppParser();
break;
diff --git a/src/CodeParser/CppParser.ts b/src/CodeParser/CppParser.ts
index e194371..2d5985b 100644
--- a/src/CodeParser/CppParser.ts
+++ b/src/CodeParser/CppParser.ts
@@ -1,7 +1,7 @@
import { Position, TextDocumentContentChangeEvent, TextEditor, TextLine } from "vscode";
-import CppGenerator from "../DocGen/CppGen";
+import Generator from "../DocGen/CGen";
import { IDocGen } from "../DocGen/DocGen";
-import ICodeParser from "./CodeParser";
+import CParser from "./CParser";
/**
*
@@ -11,121 +11,6 @@ import ICodeParser from "./CodeParser";
* @class CppParser
* @implements {ICodeParser}
*/
-export default class CppParser implements ICodeParser {
- private activeEditor: TextEditor;
- private 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 method: string = this.getMethodText();
-
- // Not a method
- if (method.length === 0) {
- return null;
- }
-
- const returnValue: string[] = this.getReturn(method);
-
- const params: string[] = this.getParams(method);
-
- const cppGenerator: IDocGen = new CppGenerator(this.activeEditor, this.activeSelection, params, returnValue);
- return cppGenerator;
- }
-
- /***************************************************************************
- Implementation
- ***************************************************************************/
-
- private getMethodText(): string {
- let method: 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();
- }
-
- method += nextLineTxt;
-
- // Get method end line
- while (nextLineTxt.indexOf(")") === -1 &&
- (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();
-
- method += " " + nextLineTxt;
- }
-
- // Not a method but some code in the file
- if (method.indexOf(")") === -1) {
- return "";
- }
-
- return method;
- }
-
- private getReturn(method: string): string[] {
- const retVals: string[] = [];
-
- // Remove the parameters from the signature
- const returnSignature: string = method.slice(0, method.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;
- }
-
- private getParams(method: string): string[] {
- const params: string[] = [];
-
- // Get parameters from enclosing brackets
- const parameters: string = method.slice(method.indexOf("(")) // Get opening bracket
- .slice(1, method.indexOf(")")) // Remove opening bracket
- .split(")")[0]; // Get closing bracket
-
- if (parameters.length === 0) { // No parameters
- return params;
- }
-
- let paramArr: string[] = parameters.split(",");
- paramArr = paramArr.map((item: string) => {
- // Remove any special C++ characters
- const clean: string = item.trim().replace(/[&*\[\]]/g, "");
-
- return clean.split(" ").pop();
- });
-
- return paramArr;
- }
+export default class CppParser extends CParser {
+ // For now C++ is the same as C
}