From c7071b74a9b9726107bd8899293e6bc7f43eaa95 Mon Sep 17 00:00:00 2001 From: Xinyu Chen Date: Sat, 16 Sep 2023 20:11:18 -0700 Subject: [PATCH] Implement cpp_include parser --- src/main/organizer.ts | 7 ++++ src/main/parser.ts | 25 +++++++++++++ src/tests/parser/fixtures/cpp_include.thrift | 2 + src/tests/parser/parser.spec.ts | 13 +++++++ .../solutions/cpp_include.solution.json | 37 +++++++++++++++++++ 5 files changed, 84 insertions(+) create mode 100644 src/tests/parser/fixtures/cpp_include.thrift create mode 100644 src/tests/parser/solutions/cpp_include.solution.json diff --git a/src/main/organizer.ts b/src/main/organizer.ts index 7c7b798..8088f6a 100644 --- a/src/main/organizer.ts +++ b/src/main/organizer.ts @@ -1,5 +1,6 @@ import { ConstDefinition, + CppIncludeDefinition, EnumDefinition, ExceptionDefinition, IncludeDefinition, @@ -15,6 +16,7 @@ import { export function organize(raw: ThriftDocument): ThriftDocument { const namespaces: Array = [] const includes: Array = [] + const cppIncludes: Array = [] const constants: Array = [] const enums: Array = [] const typedefs: Array = [] @@ -33,6 +35,10 @@ export function organize(raw: ThriftDocument): ThriftDocument { includes.push(next) break + case SyntaxType.CppIncludeDefinition: + cppIncludes.push(next) + break + case SyntaxType.CppIncludeDefinition: // We're not generating C++ break @@ -76,6 +82,7 @@ export function organize(raw: ThriftDocument): ThriftDocument { body: [ ...namespaces, ...includes, + ...cppIncludes, ...enums, ...typedefs, ...constants, diff --git a/src/main/parser.ts b/src/main/parser.ts index a1c3718..73c9880 100644 --- a/src/main/parser.ts +++ b/src/main/parser.ts @@ -6,6 +6,7 @@ import { ConstList, ConstMap, ConstValue, + CppIncludeDefinition, DoubleConstant, EnumDefinition, EnumMember, @@ -69,6 +70,7 @@ function isStatementBeginning(token: Token): boolean { switch (token.type) { case SyntaxType.NamespaceKeyword: case SyntaxType.IncludeKeyword: + case SyntaxType.CppIncludeDefinition: case SyntaxType.ConstKeyword: case SyntaxType.StructKeyword: case SyntaxType.UnionKeyword: @@ -139,6 +141,9 @@ export function createParser( case SyntaxType.IncludeKeyword: return parseInclude() + case SyntaxType.CppIncludeKeyword: + return parseCppInclude() + case SyntaxType.ConstKeyword: return parseConst() @@ -193,6 +198,26 @@ export function createParser( } } + function parseCppInclude(): CppIncludeDefinition { + const _keywordToken: Token | null = consume(SyntaxType.CppIncludeKeyword) + const keywordToken = requireValue( + _keywordToken, + `'cpp_include' keyword expected`, + ) + const _pathToken: Token | null = consume(SyntaxType.StringLiteral) + const pathToken = requireValue( + _pathToken, + `Cpp include statement must include a path as string literal`, + ) + + return { + type: SyntaxType.CppIncludeDefinition, + path: createStringLiteral(pathToken.text, pathToken.loc), + comments: getComments(), + loc: createTextLocation(keywordToken.loc.start, pathToken.loc.end), + } + } + // ServiceDefinition → 'service' Identifier ( 'extends' Identifier )? '{' Function* '} Annotations?' function parseService(): ServiceDefinition { const leadingComments: Array = getComments() diff --git a/src/tests/parser/fixtures/cpp_include.thrift b/src/tests/parser/fixtures/cpp_include.thrift new file mode 100644 index 0000000..00c067e --- /dev/null +++ b/src/tests/parser/fixtures/cpp_include.thrift @@ -0,0 +1,2 @@ +cpp_include 'test' + diff --git a/src/tests/parser/parser.spec.ts b/src/tests/parser/parser.spec.ts index 956d8fa..f3b046a 100644 --- a/src/tests/parser/parser.spec.ts +++ b/src/tests/parser/parser.spec.ts @@ -128,6 +128,19 @@ describe('Parser', () => { assert.deepEqual(objectify(thrift), expected) }) + it('should correctly parse the syntax of an cpp_include', () => { + const content: string = loadSource('cpp_include') + const scanner: Scanner = createScanner(content) + const tokens: Array = scanner.scan() + + const parser: Parser = createParser(tokens) + const thrift: ThriftDocument = parser.parse() + + const expected: any = loadSolution('cpp_include') + + assert.deepEqual(objectify(thrift), expected) + }) + it('should correctly parse the syntax of a namespace definition', () => { const content: string = loadSource('namespace') const scanner: Scanner = createScanner(content) diff --git a/src/tests/parser/solutions/cpp_include.solution.json b/src/tests/parser/solutions/cpp_include.solution.json new file mode 100644 index 0000000..4520938 --- /dev/null +++ b/src/tests/parser/solutions/cpp_include.solution.json @@ -0,0 +1,37 @@ +{ + "type": "ThriftDocument", + "body": [ + { + "type": "CppIncludeDefinition", + "path": { + "type": "StringLiteral", + "value": "test", + "loc": { + "start": { + "line": 1, + "column": 13, + "index": 12 + }, + "end": { + "line": 1, + "column": 19, + "index": 18 + } + } + }, + "comments": [], + "loc": { + "start": { + "line": 1, + "column": 1, + "index": 0 + }, + "end": { + "line": 1, + "column": 19, + "index": 18 + } + } + } + ] +}