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

Allow namespace star #8

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
8 changes: 8 additions & 0 deletions src/main/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
KeywordType,
ListType,
MapType,
NamespaceScope,
ParseError,
PropertyAssignment,
ScanError,
Expand Down Expand Up @@ -84,6 +85,13 @@ export function createIdentifier(
return { type: SyntaxType.Identifier, value, loc, annotations }
}

export function createNamespaceScope(
value: string,
loc: TextLocation,
): NamespaceScope {
return { type: SyntaxType.NamespaceScope, value, loc }
}

export function creataePropertyAssignment(
name: ConstValue,
initializer: ConstValue,
Expand Down
28 changes: 22 additions & 6 deletions src/main/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
ListType,
MapType,
NamespaceDefinition,
NamespaceScope,
ParametersDefinition,
PropertyAssignment,
ServiceDefinition,
Expand Down Expand Up @@ -53,6 +54,7 @@ import {
createIntegerLiteral,
createKeywordFieldType,
createMapFieldType,
createNamespaceScope,
createParseError,
createStringLiteral,
createTextLocation,
Expand Down Expand Up @@ -394,18 +396,32 @@ export function createParser(
return null
}

// NamespaceScope → '*' | Identifier
Copy link

Choose a reason for hiding this comment

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

style: Consider adding a comment explaining the purpose of this function

function parseNamespaceScope(): NamespaceScope {
if (currentToken().type === SyntaxType.StarToken) {
const loc = currentToken().loc
advance()
return createNamespaceScope('*', loc)
}
Comment on lines +401 to +405
Copy link

Choose a reason for hiding this comment

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

logic: Check for SyntaxType.StarToken before consuming


const _scopeToken: Token | null = consume(SyntaxType.Identifier)
const scopeToken: Token = requireValue(
_scopeToken,
`Unable to find scope identifier for namespace`,
)

return createNamespaceScope(scopeToken.text, scopeToken.loc)
}

// Namespace → 'namespace' ( NamespaceScope Identifier )
function parseNamespace(): NamespaceDefinition {
const _keywordToken: Token | null = consume(SyntaxType.NamespaceKeyword)
const keywordToken: Token = requireValue(
_keywordToken,
`'namespace' keyword expected`,
)
const _scopeToken: Token | null = consume(SyntaxType.Identifier)
const scopeToken: Token = requireValue(
_scopeToken,
`Unable to find scope identifier for namespace`,
)

const scope: NamespaceScope = parseNamespaceScope()
Copy link

Choose a reason for hiding this comment

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

logic: Add error handling for invalid namespace scopes


const _nameToken: Token | null = consume(SyntaxType.Identifier)
const nameToken: Token = requireValue(
Expand All @@ -415,7 +431,7 @@ export function createParser(

return {
type: SyntaxType.NamespaceDefinition,
scope: createIdentifier(scopeToken.text, scopeToken.loc),
scope,
name: createIdentifier(nameToken.text, nameToken.loc),
comments: getComments(),
loc: createTextLocation(keywordToken.loc.start, nameToken.loc.end),
Expand Down
6 changes: 5 additions & 1 deletion src/main/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,12 @@ export function createScanner(
nextLine()
break

case '*':
addToken(SyntaxType.StarToken)
break

case '&':
// Thirft supports (undocumented by the grammar) a syntax for c-style pointers
// Thrift supports (undocumented by the grammar) a syntax for c-style pointers
// Pointers are indicated by the '&' token. As these are not relevant to JavaScript we
// drop them here. This may not be the best thing to do, perhaps should leave them in
// the parse tree and allow consumers to deal.
Expand Down
8 changes: 7 additions & 1 deletion src/main/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export type ConstValue =

export interface NamespaceDefinition extends PrimarySyntax {
type: SyntaxType.NamespaceDefinition
scope: Identifier
scope: NamespaceScope
name: Identifier
}

Expand Down Expand Up @@ -318,6 +318,11 @@ export interface Identifier extends SyntaxNode {
annotations?: Annotations
}

export interface NamespaceScope extends SyntaxNode {
type: SyntaxType.NamespaceScope
value: string
}

export enum ErrorType {
ParseError = 'ParseError',
ScanError = 'ScanError',
Expand All @@ -328,6 +333,7 @@ export enum SyntaxType {
ThriftErrors = 'ThriftErrors',

Identifier = 'Identifier',
NamespaceScope = 'NamespaceScope',
FieldID = 'FieldID',

// Statements
Expand Down
1 change: 1 addition & 0 deletions src/tests/parser/fixtures/namespace.thrift
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
namespace js test
namespace js.ts test
namespace * test
54 changes: 51 additions & 3 deletions src/tests/parser/solutions/namespace.solution.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{
"type": "NamespaceDefinition",
"scope": {
"type": "Identifier",
"type": "NamespaceScope",
"value": "js",
"loc": {
"start": {
Expand Down Expand Up @@ -52,7 +52,7 @@
{
"type": "NamespaceDefinition",
"scope": {
"type": "Identifier",
"type": "NamespaceScope",
"value": "js.ts",
"loc": {
"start": {
Expand Down Expand Up @@ -96,6 +96,54 @@
"index": 38
}
}
},
{
"type": "NamespaceDefinition",
"scope": {
"type": "NamespaceScope",
"value": "*",
"loc": {
"start": {
"line": 3,
"column": 11,
"index": 49
},
"end": {
"line": 3,
"column": 12,
"index": 50
}
}
},
"name": {
"type": "Identifier",
"value": "test",
"loc": {
"start": {
"line": 3,
"column": 13,
"index": 51
},
"end": {
"line": 3,
"column": 17,
"index": 55
}
}
},
"comments": [],
"loc": {
"start": {
"line": 3,
"column": 1,
"index": 39
},
"end": {
"line": 3,
"column": 17,
"index": 55
}
}
}
]
}
}