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

WIP - add senum to thrift-parser #83

Open
wants to merge 2 commits 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
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@
"coverage": true,
"coverage.lcov": true
},
"typescript.tsdk": "node_modules/typescript/lib"
"mochaExplorer.files": "src/**/*.spec.ts",
"mochaExplorer.ui": "bdd"
}
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/main/organizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
ExceptionDefinition,
IncludeDefinition,
NamespaceDefinition,
SenumDefinition,
ServiceDefinition,
StructDefinition,
SyntaxType,
Expand All @@ -17,6 +18,7 @@ export function organize(raw: ThriftDocument): ThriftDocument {
const includes: Array<IncludeDefinition> = []
const constants: Array<ConstDefinition> = []
const enums: Array<EnumDefinition> = []
const senums: Array<SenumDefinition> = []
const typedefs: Array<TypedefDefinition> = []
const structs: Array<StructDefinition> = []
const unions: Array<UnionDefinition> = []
Expand Down Expand Up @@ -45,6 +47,10 @@ export function organize(raw: ThriftDocument): ThriftDocument {
enums.push(next)
break

case SyntaxType.SenumDefinition:
senums.push(next)
break

case SyntaxType.StructDefinition:
structs.push(next)
break
Expand Down
95 changes: 95 additions & 0 deletions src/main/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import {
NamespaceDefinition,
ParametersDefinition,
PropertyAssignment,
SenumDefinition,
SenumMember,
ServiceDefinition,
SetType,
StructDefinition,
Expand Down Expand Up @@ -76,6 +78,7 @@ function isStatementBeginning(token: Token): boolean {
case SyntaxType.ServiceKeyword:
case SyntaxType.TypedefKeyword:
case SyntaxType.EnumKeyword:
case SyntaxType.SenumKeyword:
return true

default:
Expand Down Expand Up @@ -159,6 +162,8 @@ export function createParser(

case SyntaxType.EnumKeyword:
return parseEnum()
case SyntaxType.SenumKeyword:
return parseSenum()

case SyntaxType.CommentBlock:
case SyntaxType.CommentLine:
Expand Down Expand Up @@ -652,6 +657,96 @@ export function createParser(
}
}

// SenumDefinition → 'senum' Identifier '{' SenumMember* '} Annotations?'
function parseSenum(): SenumDefinition {
const leadingComments: Array<Comment> = getComments()
const _keywordToken: Token | null = consume(SyntaxType.SenumKeyword)
const keywordToken: Token = requireValue(
_keywordToken,
`'senum' keyword expected`,
)
const _nameToken: Token | null = consume(SyntaxType.Identifier)
const nameToken: Token = requireValue(
_nameToken,
`Expected identifier for senum definition`,
)

const openBrace: Token | null = consume(SyntaxType.LeftBraceToken)
requireValue(openBrace, `Expected opening brace`)

const members: Array<SenumMember> = parseSenumMembers()
const _closeBrace: Token | null = consume(SyntaxType.RightBraceToken)
const closeBrace: Token = requireValue(
_closeBrace,
`Expected closing brace`,
)

const annotations: Annotations | undefined = parseAnnotations()

const loc: TextLocation = {
start: keywordToken.loc.start,
end: closeBrace.loc.end,
}

return {
type: SyntaxType.SenumDefinition,
name: createIdentifier(nameToken.text, nameToken.loc),
members,
annotations,
comments: leadingComments,
loc,
}
}

function parseSenumMembers(): Array<SenumMember> {
const members: Array<SenumMember> = []
while (!check(SyntaxType.RightBraceToken)) {
if (check(SyntaxType.CommentBlock, SyntaxType.CommentLine)) {
advance()
} else {
members.push(parseSenumMember())

// consume list separator if there is one
readListSeparator()
if (isStatementBeginning(currentToken())) {
throw reportError(
`Closing curly brace expected, but new statement found`,
)
} else if (check(SyntaxType.EOF)) {
throw reportError(
`Closing curly brace expected but reached end of file`,
)
}
}
}

return members
}

// SenumMember → (Identifier Annotations? ListSeparator?)*
function parseSenumMember(): SenumMember {
const _nameToken: Token | null = consume(SyntaxType.Identifier)
const nameToken: Token = requireValue(
_nameToken,
`SenumMember must have identifier`,
)

const loc: TextLocation | null = createTextLocation(
nameToken.loc.start,
nameToken.loc.end,
)

const annotations: Annotations | undefined = parseAnnotations()

return {
type: SyntaxType.SenumMember,
name: createIdentifier(nameToken.text, nameToken.loc),
annotations,
comments: getComments(),
loc,
}
}

// StructLike → ('struct' | 'union' | 'exception') Identifier 'xsd_all'? '{' Field* '} Annotations?'
function parseStructLikeInterface(): StructLike {
const leadingComments: Array<Comment> = getComments()
Expand Down
16 changes: 16 additions & 0 deletions src/main/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export type ThriftStatement =
| ConstDefinition
| StructDefinition
| EnumDefinition
| SenumDefinition
| ExceptionDefinition
| UnionDefinition
| TypedefDefinition
Expand Down Expand Up @@ -220,6 +221,19 @@ export interface EnumMember extends PrimarySyntax {
annotations?: Annotations
}

export interface SenumDefinition extends PrimarySyntax {
type: SyntaxType.SenumDefinition
name: Identifier
members: Array<SenumMember>
annotations?: Annotations
}

export interface SenumMember extends PrimarySyntax {
type: SyntaxType.SenumMember
name: Identifier
annotations?: Annotations
}

export interface TypedefDefinition extends PrimarySyntax {
type: SyntaxType.TypedefDefinition
name: Identifier
Expand Down Expand Up @@ -337,6 +351,7 @@ export enum SyntaxType {
ConstDefinition = 'ConstDefinition',
StructDefinition = 'StructDefinition',
EnumDefinition = 'EnumDefinition',
SenumDefinition = 'SenumDefinition',
ServiceDefinition = 'ServiceDefinition',
ExceptionDefinition = 'ExceptionDefinition',
TypedefDefinition = 'TypedefDefinition',
Expand All @@ -363,6 +378,7 @@ export enum SyntaxType {
ConstList = 'ConstList',
ConstMap = 'ConstMap',
EnumMember = 'EnumMember',
SenumMember = 'SenumMember',

// Literals
CommentLine = 'CommentLine',
Expand Down
6 changes: 6 additions & 0 deletions src/tests/parser/fixtures/annotations.thrift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ typedef i32 IntegerWithFormattedAnnotations (
)
typedef i32 ( another.annotation = "bar" ) annotatedType
typedef AnnotatedEnum ( another.annotation = "bar" ) annotatedIdentifier
typedef AnnotatedSenum ( another.annotation = "bar" ) annotatedSenumIdentifier
typedef i32 IntegerWithTightlyFormattedAnnotations (annotation="foo",foo,bar,another.annotation="bar")
const i32 ConstantWithAnnotation = 9853 ( annotation = "foo", another.annotation = "bar" )

Expand All @@ -15,6 +16,11 @@ enum AnnotatedEnum {
TWO ( annotation = "foo", another.annotation = "bar" )
} ( annotation = "foo" )

senum AnnotatedSenum {
ONE ( annotation = "foo" ),
TWO ( annotation = "foo", another.annotation = "bar" )
} ( annotation = "foo" )

struct Work {
1: i32 num1 = 0 ( annotation = "foo" ),
2: i32 ( annotation = "foo" ) num2,
Expand Down
11 changes: 11 additions & 0 deletions src/tests/parser/fixtures/comments-mapping.thrift
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,24 @@ enum Operation {
MULTIPLY
}

/**
* Senum comment
*/
senum StringOnlyOperation {
ADD,
// Senum field comment
MULTIPLY
}

/**
* Struct comment
*/
struct Work {
1: i32 num1 = 0,
// Struct field comment
2: Operation op,
// Struct field comment
3: StringOnlyOperation op1,
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/tests/parser/fixtures/senum-commented.thrift
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
senum Tes {
ONE,
# TWO
}
4 changes: 4 additions & 0 deletions src/tests/parser/fixtures/senum.thrift
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
senum Tes {
ONE,
TWO
}
26 changes: 26 additions & 0 deletions src/tests/parser/parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,32 @@ describe('Parser', () => {
assert.deepEqual(objectify(thrift), expected)
})

it('should correctly parse the syntax of a senum', () => {
const content: string = loadSource('senum')
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('senum')

assert.deepEqual(objectify(thrift), expected)
})

it('should correctly parse an senum with field commented out', () => {
const content: string = loadSource('senum-commented')
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('senum-commented')

assert.deepEqual(objectify(thrift), expected)
})

it('should correctly parse the syntax of a simple service', () => {
const content: string = loadSource('service')
const scanner: Scanner = createScanner(content)
Expand Down
Loading