Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement cpp_include parser #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/main/organizer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
ConstDefinition,
CppIncludeDefinition,
EnumDefinition,
ExceptionDefinition,
IncludeDefinition,
Expand All @@ -15,6 +16,7 @@ import {
export function organize(raw: ThriftDocument): ThriftDocument {
const namespaces: Array<NamespaceDefinition> = []
const includes: Array<IncludeDefinition> = []
const cppIncludes: Array<CppIncludeDefinition> = []
const constants: Array<ConstDefinition> = []
const enums: Array<EnumDefinition> = []
const typedefs: Array<TypedefDefinition> = []
Expand All @@ -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
Comment on lines +38 to 44
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: Duplicate case for SyntaxType.CppIncludeDefinition. Remove the second case.

Expand Down Expand Up @@ -76,6 +82,7 @@ export function organize(raw: ThriftDocument): ThriftDocument {
body: [
...namespaces,
...includes,
...cppIncludes,
...enums,
...typedefs,
...constants,
Expand Down
25 changes: 25 additions & 0 deletions src/main/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
ConstList,
ConstMap,
ConstValue,
CppIncludeDefinition,
DoubleConstant,
EnumDefinition,
EnumMember,
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -139,6 +141,9 @@ export function createParser(
case SyntaxType.IncludeKeyword:
return parseInclude()

case SyntaxType.CppIncludeKeyword:
return parseCppInclude()

case SyntaxType.ConstKeyword:
return parseConst()

Expand Down Expand Up @@ -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<Comment> = getComments()
Expand Down
2 changes: 2 additions & 0 deletions src/tests/parser/fixtures/cpp_include.thrift
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
cpp_include 'test'

13 changes: 13 additions & 0 deletions src/tests/parser/parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Token> = 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)
Expand Down
37 changes: 37 additions & 0 deletions src/tests/parser/solutions/cpp_include.solution.json
Original file line number Diff line number Diff line change
@@ -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
}
}
}
]
}