From b446b9e596fc0487b88597d8e818dae61512f309 Mon Sep 17 00:00:00 2001 From: Rowan Goemans Date: Fri, 29 Dec 2017 00:15:59 +0100 Subject: -- Added more unit tests. -- Added support for alignas. --- src/CodeParser/CParser/CParser.ts | 44 ++++++++++++++++++++++----------------- src/CodeParser/CParser/Token.ts | 2 +- 2 files changed, 26 insertions(+), 20 deletions(-) (limited to 'src/CodeParser') diff --git a/src/CodeParser/CParser/CParser.ts b/src/CodeParser/CParser/CParser.ts index 3203b40..34ffba4 100644 --- a/src/CodeParser/CParser/CParser.ts +++ b/src/CodeParser/CParser/CParser.ts @@ -22,7 +22,7 @@ export default class CParser implements ICodeParser { private typeKeywords: string[]; private stripKeywords: string[]; private keywords: string[]; - private noexcepts: string[]; + private specifiers: string[]; private lexerVocabulary; constructor() { @@ -33,6 +33,7 @@ export default class CParser implements ICodeParser { ]; this.stripKeywords = [ + "final", "static", "inline", "friend", @@ -44,9 +45,10 @@ export default class CParser implements ICodeParser { "typename", ]; - this.noexcepts = [ + this.specifiers = [ "noexcept", "throw", + "alignas", ]; // Non type keywords will be stripped from the final return type. @@ -85,36 +87,36 @@ export default class CParser implements ICodeParser { return startEndOffset[1] === 0 ? undefined : x.slice(0, startEndOffset[1]); }, Ellipsis: (x: string): string => (x.match("^\\.\\.\\.") || [])[0], - Noexcept: (x: string): string => { - const foundIndex: number = this.noexcepts + OpenParenthesis: (x: string): string => (x.match("^\\(") || [])[0], + Pointer: (x: string): string => (x.match("^\\*") || [])[0], + Reference: (x: string): string => (x.match("^&") || [])[0], + Specifier: (x: string): string => { + const foundIndex: number = this.specifiers .findIndex((n: string) => x.startsWith(n) === true); if (foundIndex === -1) { return undefined; } - if (x.slice(this.noexcepts[foundIndex].length).trim().startsWith("(") === false) { - return x.slice(0, this.noexcepts[foundIndex].length); + if (x.slice(this.specifiers[foundIndex].length).trim().startsWith("(") === false) { + return x.slice(0, this.specifiers[foundIndex].length); } const startEndOffset: number[] = this.GetSubExprStartEnd(x, 0, "(", ")"); return startEndOffset[1] === 0 ? undefined : x.slice(0, startEndOffset[1]); }, - OpenParenthesis: (x: string): string => (x.match("^\\(") || [])[0], - Pointer: (x: string): string => (x.match("^\\*") || [])[0], - Reference: (x: string): string => (x.match("^&") || [])[0], Symbol: (x: string): string => { // Handle access specifiers since they aren't really symbols. if (x.startsWith("public:") || x.startsWith("protected:") || x.startsWith("private:")) { return undefined; } - // Handle noexcept and throw since they aren't normal symbols. - const noExceptFound: number = this.noexcepts + // Handle specifiers + const specifierFound: number = this.specifiers .findIndex((n: string) => x.startsWith(n) === true); - if (noExceptFound !== -1) { + if (specifierFound !== -1) { return undefined; } @@ -156,10 +158,10 @@ export default class CParser implements ICodeParser { break; } - symbol += reMatch.trim(); + symbol += reMatch; } - return symbol.trim(); + return symbol.replace(/\s+$/, ""); }, }; } @@ -348,11 +350,15 @@ export default class CParser implements ICodeParser { tree.nodes = tree.nodes.slice(0, assignmentIndex); } - // Noexcept isn't needed so slice everything after it. - const noexcept = tree.nodes - .findIndex((n) => n instanceof Token && n.Type === TokenType.Noexcept); - if (noexcept !== -1) { - tree.nodes = tree.nodes.slice(0, noexcept); + // Specifiers aren't needed so remove them. + while (true) { + const specifierIndex = tree.nodes + .findIndex((n) => n instanceof Token && n.Type === TokenType.Specifier); + if (specifierIndex !== -1) { + tree.nodes.splice(specifierIndex, 1); + } else { + break; + } } return tree; diff --git a/src/CodeParser/CParser/Token.ts b/src/CodeParser/CParser/Token.ts index c9d3065..0cbd412 100644 --- a/src/CodeParser/CParser/Token.ts +++ b/src/CodeParser/CParser/Token.ts @@ -13,7 +13,7 @@ export enum TokenType { CommentLine, Ellipsis, Attribute, - Noexcept, + Specifier, } export class Token { -- cgit v1.2.3