diff options
| author | Rowan Goemans <RB.Goemans@student.han.nl> | 2017-12-31 04:07:55 +0100 |
|---|---|---|
| committer | Christoph Schlosser <christophschlosser@users.noreply.github.com> | 2018-02-20 22:02:22 +0100 |
| commit | 31f6517aa5d8d3ccdd0b39880337c000943d08c1 (patch) | |
| tree | 2c3c5240470a592f4c0bd134d779f4f3c25d6b2b | |
| parent | 10aac7196a3a701860525d0f466896efe182d39a (diff) | |
| download | doxdocgen-31f6517aa5d8d3ccdd0b39880337c000943d08c1.tar.gz | |
-- Completed unit tests for operators. Conversion operators aren't working yet.
-- Renamed all C things to Cpp since that is really what they are.
-- Made empty files for the remaining unit tests.
| -rw-r--r-- | src/CodeParserController.ts | 4 | ||||
| -rw-r--r-- | src/Lang/C/CArgument.ts | 6 | ||||
| -rw-r--r-- | src/Lang/C/CParseTree.ts | 121 | ||||
| -rw-r--r-- | src/Lang/Cpp/CppArgument.ts | 6 | ||||
| -rw-r--r-- | src/Lang/Cpp/CppDocGen.ts (renamed from src/Lang/C/CDocGen.ts) | 26 | ||||
| -rw-r--r-- | src/Lang/Cpp/CppParseTree.ts | 124 | ||||
| -rw-r--r-- | src/Lang/Cpp/CppParser.ts (renamed from src/Lang/C/CParser.ts) | 143 | ||||
| -rw-r--r-- | src/Lang/Cpp/CppToken.ts (renamed from src/Lang/C/CToken.ts) | 8 | ||||
| -rw-r--r-- | src/test/CppTests/Attributes.test.ts (renamed from src/test/CTests/Attributes.test.ts) | 0 | ||||
| -rw-r--r-- | src/test/CppTests/Con-AndDestructor.test.ts (renamed from src/test/CTests/Con-AndDestructor.test.ts) | 0 | ||||
| -rw-r--r-- | src/test/CppTests/Config.tests.ts | 20 | ||||
| -rw-r--r-- | src/test/CppTests/FunctionPointer.test.ts (renamed from src/test/CTests/FunctionPointer.test.ts) | 0 | ||||
| -rw-r--r-- | src/test/CppTests/Operators.test.ts (renamed from src/test/CTests/Operators.test.ts) | 33 | ||||
| -rw-r--r-- | src/test/CppTests/Parameters.test.ts | 20 | ||||
| -rw-r--r-- | src/test/CppTests/ReturnTypes.test.ts (renamed from src/test/CTests/ReturnTypes.test.ts) | 0 | ||||
| -rw-r--r-- | src/test/CppTests/Templates.test.ts (renamed from src/test/CTests/Templates.test.ts) | 0 | ||||
| -rw-r--r-- | src/test/CppTests/TestSetup.ts (renamed from src/test/CTests/TestSetup.ts) | 4 | ||||
| -rw-r--r-- | src/test/CppTests/TrailingReturns.test.ts (renamed from src/test/CTests/TrailingReturns.test.ts) | 0 |
18 files changed, 290 insertions, 225 deletions
diff --git a/src/CodeParserController.ts b/src/CodeParserController.ts index 32d1d75..1b42f3d 100644 --- a/src/CodeParserController.ts +++ b/src/CodeParserController.ts @@ -10,7 +10,7 @@ import { } from "vscode"; import CodeParser from "./Common/ICodeParser"; import { Config } from "./Config"; -import CParser from "./Lang/C/CParser"; +import CppParser from "./Lang/Cpp/CppParser"; /** * * Checks if the event matches the specified guidelines and if a parser exists for this language @@ -86,7 +86,7 @@ export default class CodeParserController { switch (lang) { case "c": case "cpp": - parser = new CParser(this.cfg); + parser = new CppParser(this.cfg); break; default: // tslint:disable-next-line:no-console diff --git a/src/Lang/C/CArgument.ts b/src/Lang/C/CArgument.ts deleted file mode 100644 index 46f180c..0000000 --- a/src/Lang/C/CArgument.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { CParseTree } from "./CParseTree"; - -export class CArgument { - public name: string = null; - public type: CParseTree = new CParseTree(); -} diff --git a/src/Lang/C/CParseTree.ts b/src/Lang/C/CParseTree.ts deleted file mode 100644 index f3ec870..0000000 --- a/src/Lang/C/CParseTree.ts +++ /dev/null @@ -1,121 +0,0 @@ -import { CToken, CTokenType } from "./CToken"; - -export class CParseTree { - - /** - * Create a tree from CTokens. This consumes the CTokens. - * @param CTokens The CTokens to create a tree for. - * @param inNested If currently allready nesting. - */ - public static CreateTree(CTokens: CToken[], inNested: boolean = false): CParseTree { - const tree: CParseTree = new CParseTree(); - - while (CTokens.length > 0) { - const token: CToken = CTokens.shift(); - switch (token.type) { - case CTokenType.OpenParenthesis: - tree.nodes.push(this.CreateTree(CTokens, true)); - break; - case CTokenType.CloseParenthesis: - if (inNested === false) { - throw new Error("Unmatched closing parenthesis."); - } - return tree; - default: - tree.nodes.push(token); - break; - } - } - - if (inNested === true) { - throw new Error("No match found for an opening parenthesis."); - } - - return tree; - } - - public nodes: Array<CToken | CParseTree> = []; - - /** - * Compact empty branches. Example ((foo))(((bar))) will become (foo)(bar) - * @param tree The CParseTree to compact. Defaults to the current tree. - */ - public Compact(tree: CParseTree = this): CParseTree { - const newTree: CParseTree = new CParseTree(); - newTree.nodes = tree.nodes.map((n) => n); - const isNotCompact = (n) => n instanceof CParseTree && n.nodes.length === 1 && n.nodes[0] instanceof CParseTree; - - // Compact current level of nodes to the maximum amount. - while (newTree.nodes.some((n) => isNotCompact(n))) { - newTree.nodes = newTree.nodes - .map((n) => n instanceof CParseTree && isNotCompact(n) ? n.nodes[0] : n); - } - - // Compact all nested CParseTrees. - newTree.nodes = newTree.nodes - .map((n) => n instanceof CParseTree ? this.Compact(n) : n); - - return newTree; - } - - /** - * Copy CParseTree. - * @param tree The CParseTree to compact. Defaults to the current tree. - */ - public Copy(tree: CParseTree = this): CParseTree { - const newTree: CParseTree = new CParseTree(); - newTree.nodes = tree.nodes - .map((n) => n instanceof CToken ? n : this.Copy(n)); - return newTree; - } - - /** - * Create string from the CParseTree which is a representation of the original code. - * @param tree The CParseTree to compact. Defaults to the current tree. - */ - public Yield(tree: CParseTree = this): string { - let code: string = ""; - - for (const node of tree.nodes) { - if (node instanceof CParseTree) { - code += "(" + this.Yield(node) + ")"; - continue; - } - - switch (node.type) { - case CTokenType.Symbol: - code += code === "" ? node.value : " " + node.value; - break; - case CTokenType.Pointer: - code += node.value; - break; - case CTokenType.Reference: - code += node.value; - break; - case CTokenType.ArraySubscript: - code += node.value; - break; - case CTokenType.CurlyBlock: - code += node.value; - break; - case CTokenType.Assignment: - code += " " + node.value; - break; - case CTokenType.Comma: - code += node.value; - break; - case CTokenType.Arrow: - code += " " + node.value; - break; - case CTokenType.Ellipsis: - code += node.value; - break; - case CTokenType.Attribute: - code += code === "" ? node.value : " " + node.value; - break; - } - } - - return code; - } -} diff --git a/src/Lang/Cpp/CppArgument.ts b/src/Lang/Cpp/CppArgument.ts new file mode 100644 index 0000000..9b781c8 --- /dev/null +++ b/src/Lang/Cpp/CppArgument.ts @@ -0,0 +1,6 @@ +import { CppParseTree } from "./CppParseTree"; + +export class CppArgument { + public name: string = null; + public type: CppParseTree = new CppParseTree(); +} diff --git a/src/Lang/C/CDocGen.ts b/src/Lang/Cpp/CppDocGen.ts index 5488020..e715583 100644 --- a/src/Lang/C/CDocGen.ts +++ b/src/Lang/Cpp/CppDocGen.ts @@ -1,34 +1,34 @@ import { Position, Range, Selection, TextEditor, TextLine, WorkspaceEdit } from "vscode"; import { IDocGen } from "../../Common/IDocGen"; import { Config } from "../../Config"; -import { CArgument } from "./CArgument"; -import { CParseTree } from "./CParseTree"; -import { CToken, CTokenType } from "./CToken"; +import { CppArgument } from "./CppArgument"; +import { CppParseTree } from "./CppParseTree"; +import { CppToken, CppTokenType } from "./CppToken"; -export default class CDocGen implements IDocGen { +export default class CppDocGen implements IDocGen { protected activeEditor: TextEditor; protected readonly cfg: Config; - protected func: CArgument; + protected func: CppArgument; protected templateParams: string[]; - protected params: CArgument[]; + protected params: CppArgument[]; /** * @param {TextEditor} actEdit Active editor window * @param {Position} cursorPosition Where the cursor of the user currently is * @param {string[]} templateParams The template parameters of the declaration. - * @param {CArgument} func The type and name of the function to generate doxygen. + * @param {CppArgument} func The type and name of the function to generate doxygen. * Doesn't contain anything if it is not a function. - * @param {CArgument[]} params The parameters of the function. Doesn't contain anything if it is not a function. + * @param {CppArgument[]} params The parameters of the function. Doesn't contain anything if it is not a function. */ public constructor( actEdit: TextEditor, cursorPosition: Position, cfg: Config, templateParams: string[], - func: CArgument, - params: CArgument[], + func: CppArgument, + params: CppArgument[], ) { this.activeEditor = actEdit; this.cfg = cfg; @@ -85,15 +85,15 @@ export default class CDocGen implements IDocGen { // Check if return type is a pointer const ptrReturnIndex = this.func.type.nodes - .findIndex((n) => n instanceof CToken && n.type === CTokenType.Pointer); + .findIndex((n) => n instanceof CppToken && n.type === CppTokenType.Pointer); // Special case for void functions. const voidReturnIndex = this.func.type.nodes - .findIndex((n) => n instanceof CToken && n.type === CTokenType.Symbol && n.value === "void"); + .findIndex((n) => n instanceof CppToken && n.type === CppTokenType.Symbol && n.value === "void"); // Special case for bool return type. const boolReturnIndex: number = this.func.type.nodes - .findIndex((n) => n instanceof CToken && n.type === CTokenType.Symbol && n.value === "bool"); + .findIndex((n) => n instanceof CppToken && n.type === CppTokenType.Symbol && n.value === "bool"); if (boolReturnIndex !== -1 && this.cfg.boolReturnsTrueFalse === true) { params.push("true"); diff --git a/src/Lang/Cpp/CppParseTree.ts b/src/Lang/Cpp/CppParseTree.ts new file mode 100644 index 0000000..41fb8b2 --- /dev/null +++ b/src/Lang/Cpp/CppParseTree.ts @@ -0,0 +1,124 @@ +import { CppToken, CppTokenType } from "./CppToken"; + +export class CppParseTree { + + /** + * Create a tree from CppTokens. This consumes the CppTokens. + * @param CppTokens The CppTokens to create a tree for. + * @param inNested If currently allready nesting. + */ + public static CreateTree(CppTokens: CppToken[], inNested: boolean = false): CppParseTree { + const tree: CppParseTree = new CppParseTree(); + + while (CppTokens.length > 0) { + const token: CppToken = CppTokens.shift(); + switch (token.type) { + case CppTokenType.OpenParenthesis: + tree.nodes.push(this.CreateTree(CppTokens, true)); + break; + case CppTokenType.CloseParenthesis: + if (inNested === false) { + throw new Error("Unmatched closing parenthesis."); + } + return tree; + default: + tree.nodes.push(token); + break; + } + } + + if (inNested === true) { + throw new Error("No match found for an opening parenthesis."); + } + + return tree; + } + + public nodes: Array<CppToken | CppParseTree> = []; + + /** + * Compact empty branches. Example ((foo))(((bar))) will become (foo)(bar) + * @param tree The CppParseTree to compact. Defaults to the current tree. + */ + public Compact(tree: CppParseTree = this): CppParseTree { + const newTree: CppParseTree = new CppParseTree(); + newTree.nodes = tree.nodes.map((n) => n); + const isNotCompact = (n) => { + return n instanceof CppParseTree + && n.nodes.length === 1 && n.nodes[0] instanceof CppParseTree; + }; + + // Compact current level of nodes to the maximum amount. + while (newTree.nodes.some((n) => isNotCompact(n))) { + newTree.nodes = newTree.nodes + .map((n) => n instanceof CppParseTree && isNotCompact(n) ? n.nodes[0] : n); + } + + // Compact all nested CppParseTrees. + newTree.nodes = newTree.nodes + .map((n) => n instanceof CppParseTree ? this.Compact(n) : n); + + return newTree; + } + + /** + * Copy CppParseTree. + * @param tree The CppParseTree to compact. Defaults to the current tree. + */ + public Copy(tree: CppParseTree = this): CppParseTree { + const newTree: CppParseTree = new CppParseTree(); + newTree.nodes = tree.nodes + .map((n) => n instanceof CppToken ? n : this.Copy(n)); + return newTree; + } + + /** + * Create string from the CppParseTree which is a representation of the original code. + * @param tree The CppParseTree to compact. Defaults to the current tree. + */ + public Yield(tree: CppParseTree = this): string { + let code: string = ""; + + for (const node of tree.nodes) { + if (node instanceof CppParseTree) { + code += "(" + this.Yield(node) + ")"; + continue; + } + + switch (node.type) { + case CppTokenType.Symbol: + code += code === "" ? node.value : " " + node.value; + break; + case CppTokenType.Pointer: + code += node.value; + break; + case CppTokenType.Reference: + code += node.value; + break; + case CppTokenType.ArraySubscript: + code += node.value; + break; + case CppTokenType.CurlyBlock: + code += node.value; + break; + case CppTokenType.Assignment: + code += " " + node.value; + break; + case CppTokenType.Comma: + code += node.value; + break; + case CppTokenType.Arrow: + code += " " + node.value; + break; + case CppTokenType.Ellipsis: + code += node.value; + break; + case CppTokenType.Attribute: + code += code === "" ? node.value : " " + node.value; + break; + } + } + + return code; + } +} diff --git a/src/Lang/C/CParser.ts b/src/Lang/Cpp/CppParser.ts index 859dc6b..27abc8a 100644 --- a/src/Lang/C/CParser.ts +++ b/src/Lang/Cpp/CppParser.ts @@ -2,10 +2,10 @@ import { Position, TextDocumentContentChangeEvent, TextEditor, TextLine, workspa import ICodeParser from "../../Common/ICodeParser"; import { IDocGen } from "../../Common/IDocGen"; import { Config } from "../../Config"; -import { CArgument } from "./CArgument"; -import CDocGen from "./CDocGen"; -import { CParseTree } from "./CParseTree"; -import { CToken, CTokenType } from "./CToken"; +import { CppArgument } from "./CppArgument"; +import CppDocGen from "./CppDocGen"; +import { CppParseTree } from "./CppParseTree"; +import { CppToken, CppTokenType } from "./CppToken"; /** * @@ -15,7 +15,7 @@ import { CToken, CTokenType } from "./CToken"; * @class CParser * @implements {ICodeParser} */ -export default class CParser implements ICodeParser { +export default class CppParser implements ICodeParser { protected activeEditor: TextEditor; protected activeSelection: Position; protected readonly cfg: Config; @@ -134,7 +134,7 @@ export default class CParser implements ICodeParser { // Special case group up the fundamental types with the modifiers. // tslint:disable-next-line:max-line-length - let reMatch: string = (x.match("^(unsigned|signed|short|long|int|char|double)(\\s+(unsigned|signed|short|long|int|char|double))+") || [])[0]; + let reMatch: string = (x.match("^(unsigned|signed|short|long|int|char|double)(\\s+(unsigned|signed|short|long|int|char|double))+(?!a-z|A-Z|:|_)") || [])[0]; if (reMatch !== undefined) { return reMatch.trim(); } @@ -142,7 +142,7 @@ export default class CParser implements ICodeParser { // Regex to handle a part of all symbols and includes all symbol special cases. // This is run in a loop because template parts of a symbol can't be parsed using regex. // tslint:disable-next-line:max-line-length - const symbolRegex: string = "^([a-z|A-Z|:|_|~|\\d]*operator\\s*(\"\"_[a-z|A-Z]+|>>=|<<=|->\\*|\\+=|-=|\\*=|\\/=|%=|\\^=|&=|\\|=|<<|>>|==|!=|<=|->|>=|&&|\\|\\||\\+\\+|--|\\+|-|\\*|\\/|%|\\^|&|\||~|!|=|<|>|,|\\[\\s*\\]|\\(\\s*\\)|(new|delete)\\s*(\\[\\s*\\]){0,1}){0,1}|[a-z|A-Z|:|_|~|\\d]+)"; + const symbolRegex: string = "^([a-z|A-Z|:|_|~|\\d]*operator\\s*(\"\"_[a-z|A-Z|_|\\d]+|>>=|<<=|->\\*|\\+=|-=|\\*=|\\/=|%=|\\^=|&=|\\|=|<<|>>|==|!=|<=|->|>=|&&|\\|\\||\\+\\+|--|\\+|-|\\*|\\/|%|\\^|&|\||~|!|=|<|>|,|\\[\\s*\\]|\\(\\s*\\)|(new|delete)\\s*(\\[\\s*\\]){0,1}){0,1}|[a-z|A-Z|:|_|~|\\d]+)"; reMatch = (x.match(symbolRegex) || [])[0]; if (reMatch === undefined) { @@ -186,7 +186,7 @@ export default class CParser implements ICodeParser { // console.dir(err); } - // template parsing is simpler by using heuristics rather then CTokenizing first. + // template parsing is simpler by using heuristics rather then CppTokenizing first. const templateArgs: string[] = []; while (line.startsWith("template")) { const template: string = this.GetTemplate(line); @@ -196,14 +196,14 @@ export default class CParser implements ICodeParser { line = line.slice(template.length, line.length + 1).trim(); } - let args: [CArgument, CArgument[]] = [new CArgument(), []]; + let args: [CppArgument, CppArgument[]] = [new CppArgument(), []]; try { args = this.GetReturnAndArgs(line); } catch (err) { // console.dir(err); } - return new CDocGen( + return new CppDocGen( this.activeEditor, this.activeSelection, this.cfg, @@ -262,36 +262,36 @@ export default class CParser implements ICodeParser { throw new Error("More then 20 lines were gotten from editor and no end of expression was found."); } - private Tokenize(expression: string): CToken[] { - const CTokens: CToken[] = []; + private Tokenize(expression: string): CppToken[] { + const CppTokens: CppToken[] = []; expression = expression.replace(/^\s+|\s+$/g, ""); while (expression.length !== 0) { - const matches: CToken[] = Object.keys(this.lexerVocabulary) - .map((k): CToken => new CToken(CTokenType[k], this.lexerVocabulary[k](expression))) + const matches: CppToken[] = Object.keys(this.lexerVocabulary) + .map((k): CppToken => new CppToken(CppTokenType[k], this.lexerVocabulary[k](expression))) .filter((t) => t.value !== undefined); if (matches.length === 0) { - throw new Error("Next CToken couldn\'t be determined: " + expression); + throw new Error("Next CppToken couldn\'t be determined: " + expression); } else if (matches.length > 1) { - throw new Error("Multiple matches for next CToken: " + expression); + throw new Error("Multiple matches for next CppToken: " + expression); } - CTokens.push(matches[0]); + CppTokens.push(matches[0]); expression = expression.slice(matches[0].value.length, expression.length).replace(/^\s+|\s+$/g, ""); } - return CTokens; + return CppTokens; } - private GetReturnAndArgs(line: string): [CArgument, CArgument[]] { - // CTokenize rest of expression and remove comment CTokens; - const CTokens: CToken[] = this.Tokenize(line) - .filter((t) => t.type !== CTokenType.CommentBlock) - .filter((t) => t.type !== CTokenType.CommentLine); + private GetReturnAndArgs(line: string): [CppArgument, CppArgument[]] { + // CppTokenize rest of expression and remove comment CppTokens; + const CppTokens: CppToken[] = this.Tokenize(line) + .filter((t) => t.type !== CppTokenType.CommentBlock) + .filter((t) => t.type !== CppTokenType.CommentLine); // Create hierarchical tree based on the parenthesis. - const tree: CParseTree = CParseTree.CreateTree(CTokens).Compact(); + const tree: CppParseTree = CppParseTree.CreateTree(CppTokens).Compact(); // return argument. const func = this.GetArgument(tree); @@ -300,58 +300,59 @@ export default class CParser implements ICodeParser { if (func.name === null) { if (func.type.nodes.length !== 1) { throw new Error("Too many symbols found for constructor/descructor."); - } else if (func.type.nodes[0] instanceof CParseTree) { - throw new Error("One node found with just a CParseTree. Malformed input."); + } else if (func.type.nodes[0] instanceof CppParseTree) { + throw new Error("One node found with just a CppParseTree. Malformed input."); } - func.name = (func.type.nodes[0] as CToken).value; + func.name = (func.type.nodes[0] as CppToken).value; func.type.nodes = []; } - // Get arguments list as a CParseTree and create arguments from them. + // Get arguments list as a CppParseTree and create arguments from them. const params = this.GetArgumentList(tree) .map((a) => this.GetArgument(a)); return [func, params]; } - private RemoveUnusedTokens(tree: CParseTree): CParseTree { + private RemoveUnusedTokens(tree: CppParseTree): CppParseTree { tree = tree.Copy(); // First slice of everything after assignment since that will not be used. - const assignmentIndex = tree.nodes.findIndex((n) => n instanceof CToken && n.type === CTokenType.Assignment); + const assignmentIndex = tree.nodes + .findIndex((n) => n instanceof CppToken && n.type === CppTokenType.Assignment); if (assignmentIndex !== -1) { tree.nodes = tree.nodes.slice(0, assignmentIndex); } // Specifiers aren't needed so remove them. tree.nodes = tree.nodes - .filter((n) => n instanceof CParseTree || (n instanceof CToken && n.type !== CTokenType.Attribute)); + .filter((n) => n instanceof CppParseTree || (n instanceof CppToken && n.type !== CppTokenType.Attribute)); return tree; } - private GetArgumentList(tree: CParseTree): CParseTree[] { - const args: CParseTree[] = []; + private GetArgumentList(tree: CppParseTree): CppParseTree[] { + const args: CppParseTree[] = []; tree = this.RemoveUnusedTokens(tree); - let cursor: CParseTree = tree; + let cursor: CppParseTree = tree; while (this.IsFuncPtr(cursor.nodes) === true) { - cursor = cursor.nodes.find((n) => n instanceof CParseTree) as CParseTree; + cursor = cursor.nodes.find((n) => n instanceof CppParseTree) as CppParseTree; } - const argTree: CParseTree = cursor.nodes.find((n) => n instanceof CParseTree) as CParseTree; + const argTree: CppParseTree = cursor.nodes.find((n) => n instanceof CppParseTree) as CppParseTree; if (argTree === undefined) { throw new Error("Function arguments not found."); } // Split the argument tree on commas - let arg: CParseTree = new CParseTree(); + let arg: CppParseTree = new CppParseTree(); for (const node of argTree.nodes) { - if (node instanceof CToken && node.type === CTokenType.Comma) { + if (node instanceof CppToken && node.type === CppTokenType.Comma) { args.push(arg); - arg = new CParseTree(); + arg = new CppParseTree(); } else { arg.nodes.push(node); } @@ -364,33 +365,33 @@ export default class CParser implements ICodeParser { return args; } - private IsFuncPtr(nodes: Array<CToken | CParseTree>) { - return nodes.filter((n) => n instanceof CParseTree).length === 2; + private IsFuncPtr(nodes: Array<CppToken | CppParseTree>) { + return nodes.filter((n) => n instanceof CppParseTree).length === 2; } - private StripNonTypeNodes(tree: CParseTree) { + private StripNonTypeNodes(tree: CppParseTree) { tree.nodes = tree.nodes // All strippable keywords. .filter((n) => { - return !(n instanceof CToken - && n.type === CTokenType.Symbol + return !(n instanceof CppToken + && n.type === CppTokenType.Symbol && this.stripKeywords.find((k) => k === n.value) !== undefined); }); } - private GetArgumentFromTrailingReturn(tree: CParseTree, startTrailingReturn: number): CArgument { - const argument: CArgument = new CArgument(); + private GetArgumentFromTrailingReturn(tree: CppParseTree, startTrailingReturn: number): CppArgument { + const argument: CppArgument = new CppArgument(); - // Find index of auto prior to the first CParseTree. + // Find index of auto prior to the first CppParseTree. // If auto is not found something is going wrong since trailing return // requires auto. let autoIndex: number = -1; for (let i: number = 0; i < tree.nodes.length; i++) { const node = tree.nodes[i]; - if (node instanceof CParseTree) { + if (node instanceof CppParseTree) { break; } - if (node.type === CTokenType.Symbol && node.value === "auto") { + if (node.type === CppTokenType.Symbol && node.value === "auto") { autoIndex = i; break; } @@ -400,13 +401,13 @@ export default class CParser implements ICodeParser { throw new Error("Function declaration has trailing return but type is not auto."); } - // Get symbol between auto and CParseTree which is the argument name. It also may not be a keyword. + // Get symbol between auto and CppParseTree which is the argument name. It also may not be a keyword. for (let i: number = autoIndex + 1; i < tree.nodes.length; i++) { const node = tree.nodes[i]; - if (node instanceof CParseTree) { + if (node instanceof CppParseTree) { break; } - if (node.type === CTokenType.Symbol && this.keywords.find((k) => k === node.value) === undefined) { + if (node.type === CppTokenType.Symbol && this.keywords.find((k) => k === node.value) === undefined) { argument.name = node.value; break; } @@ -418,19 +419,19 @@ export default class CParser implements ICodeParser { return argument; } - private GetArgumentFromFuncPtr(tree: CParseTree): CArgument { - const argument: CArgument = new CArgument(); + private GetArgumentFromFuncPtr(tree: CppParseTree): CppArgument { + const argument: CppArgument = new CppArgument(); argument.type = tree; - let cursor: CParseTree = tree; + let cursor: CppParseTree = tree; while (this.IsFuncPtr(cursor.nodes) === true) { - cursor = cursor.nodes.find((n) => n instanceof CParseTree) as CParseTree; + cursor = cursor.nodes.find((n) => n instanceof CppParseTree) as CppParseTree; } - // Remove CParseTree. This can be if it is a function declaration. - const argumentsIndex = cursor.nodes.findIndex((n) => n instanceof CParseTree); + // Remove CppParseTree. This can be if it is a function declaration. + const argumentsIndex = cursor.nodes.findIndex((n) => n instanceof CppParseTree); if (argumentsIndex !== -1) { cursor.nodes.splice(argumentsIndex, 1); } @@ -439,11 +440,11 @@ export default class CParser implements ICodeParser { // Remove it from the tree and set the name to the argument name for (let i: number = 0; i < cursor.nodes.length; i++) { const node = cursor.nodes[i]; - if (node instanceof CParseTree) { + if (node instanceof CppParseTree) { continue; } - if (node.type === CTokenType.Symbol && this.keywords.find((k) => k === node.value) === undefined) { + if (node.type === CppTokenType.Symbol && this.keywords.find((k) => k === node.value) === undefined) { argument.name = node.value; cursor.nodes.splice(i, 1); } @@ -453,21 +454,21 @@ export default class CParser implements ICodeParser { return argument; } - private GetDefaultArgument(tree: CParseTree): CArgument { - const argument: CArgument = new CArgument(); + private GetDefaultArgument(tree: CppParseTree): CppArgument { + const argument: CppArgument = new CppArgument(); for (const node of tree.nodes) { - if (node instanceof CParseTree) { + if (node instanceof CppParseTree) { break; } const symbolCount = argument.type.nodes - .filter((n) => n instanceof CToken) - .map((n) => n as CToken) - .filter((n) => n.type === CTokenType.Symbol) + .filter((n) => n instanceof CppToken) + .map((n) => n as CppToken) + .filter((n) => n.type === CppTokenType.Symbol) .filter((n) => this.keywords.find((k) => k === n.value) === undefined) .length; - if (node.type === CTokenType.Symbol + if (node.type === CppTokenType.Symbol && this.keywords.find((k) => k === node.value) === undefined ) { if (symbolCount === 1 && argument.name === null) { @@ -485,15 +486,15 @@ export default class CParser implements ICodeParser { return argument; } - private GetArgument(tree: CParseTree): CArgument { + private GetArgument(tree: CppParseTree): CppArgument { // Copy tree structure leave original untouched. const copy = this.RemoveUnusedTokens(tree); // Special case with only ellipsis. C style variadic arguments if (copy.nodes.length === 1) { const node = copy.nodes[0]; - if (node instanceof CToken && node.type === CTokenType.Ellipsis) { - const argument: CArgument = new CArgument(); + if (node instanceof CppToken && node.type === CppTokenType.Ellipsis) { + const argument: CppArgument = new CppArgument(); argument.name = node.value; return argument; } @@ -501,7 +502,7 @@ export default class CParser implements ICodeParser { // Check if it is has a trailing return. const startTrailingReturn: number = copy.nodes - .findIndex((t) => t instanceof CToken ? t.type === CTokenType.Arrow : false); + .findIndex((t) => t instanceof CppToken ? t.type === CppTokenType.Arrow : false); // Special case trailing return. if (startTrailingReturn !== -1) { diff --git a/src/Lang/C/CToken.ts b/src/Lang/Cpp/CppToken.ts index 75214aa..f229e8f 100644 --- a/src/Lang/C/CToken.ts +++ b/src/Lang/Cpp/CppToken.ts @@ -1,4 +1,4 @@ -export enum CTokenType { +export enum CppTokenType { Symbol, Pointer, Reference, @@ -15,11 +15,11 @@ export enum CTokenType { Attribute, } -export class CToken { - public type: CTokenType; +export class CppToken { + public type: CppTokenType; public value: string; - constructor(type: CTokenType, value: string) { + constructor(type: CppTokenType, value: string) { this.type = type; this.value = value; } diff --git a/src/test/CTests/Attributes.test.ts b/src/test/CppTests/Attributes.test.ts index bfeaef1..bfeaef1 100644 --- a/src/test/CTests/Attributes.test.ts +++ b/ |