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

Fix block comment parsing #9

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
39 changes: 15 additions & 24 deletions src/main/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,45 +308,36 @@ export function createScanner(
function multilineComment(): void {
let comment: string = ''
let cursor: number = 0
let isNewLine = true

while (true) {
if (
current() === '\n' ||
isAtEnd() ||
(current() !== '/' && current() !== '*' && current() !== ' ')
) {
break
} else {
advance()
}
}
// Skip first two characters: '/' and '*'
advance()
advance()

while (true) {
if (current() === '\n') {
nextLine()
// A comment goes until we find a comment terminator (*/).
if ((current() === '*' && peek() === '/') || isAtEnd()) {
advance()
break
}

if (
comment.charAt(cursor - 1) === '\n' &&
(peek() === ' ' || peek() === '*')
) {
if (isNewLine && (current() === ' ' || current() === '*')) {
/**
* We ignore stars and spaces after a new line to normalize comment formatting.
* We're only keeping the text of the comment without the extranious formatting.
*/
} else {
isNewLine = false
comment += current()
cursor += 1
}

advance()

// A comment goes until we find a comment terminator (*/).
if ((peek() === '*' && peekNext() === '/') || isAtEnd()) {
advance()
advance()
break
if (current() === '\n') {
isNewLine = true
nextLine()
}

advance()
}

addToken(SyntaxType.CommentBlock, comment.trim())
Expand Down
14 changes: 14 additions & 0 deletions src/tests/parser/fixtures/empty-comments.thrift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
/* */
/** */
/*
*
*/
/**
*
*/
/*
*/
/**
*/
const string test = 'test'
21 changes: 21 additions & 0 deletions src/tests/parser/parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ function loadSolution(name: string): object {
)
}

function saveSolution(fileName: string, obj: any) {
fs.writeFileSync(
path.join(__dirname, `./solutions/${fileName}.solution.json`),
JSON.stringify(obj, null, 4),
'utf-8',
)
}

function objectify(thrift: ThriftDocument): object {
return JSON.parse(JSON.stringify(thrift))
}
Expand Down Expand Up @@ -414,6 +422,19 @@ describe('Parser', () => {
assert.deepEqual(objectify(thrift), expected)
})

it('should correctly handle empty block comments', () => {
const content: string = loadSource('empty-comments')
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('empty-comments')

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

it('should correctly parse annotations', () => {
const content: string = loadSource('annotations')
const scanner: Scanner = createScanner(content)
Expand Down
Loading