Skip to content

Commit a3746b9

Browse files
committed
Fix #4449 Add a warning message when bibtex parser failed
1 parent c5eb270 commit a3746b9

File tree

7 files changed

+32
-16
lines changed

7 files changed

+32
-16
lines changed

src/completion/completer/citation.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import * as vscode from 'vscode'
2-
import * as fs from 'fs'
32
import { bibtexParser } from 'latex-utensils'
43
import { lw } from '../../lw'
54
import type { CitationField, CitationItem, CompletionArgs, CompletionItem, CompletionProvider } from '../../types'
@@ -245,9 +244,9 @@ async function parseBibFile(fileName: string) {
245244
return
246245
}
247246
const newEntry: CitationItem[] = []
248-
const bibtex = fs.readFileSync(fileName).toString()
247+
const bibtex = await lw.file.read(fileName)
249248
logger.log(`Parse BibTeX AST from ${fileName} .`)
250-
const ast = await lw.parser.parse.bib(bibtex)
249+
const ast = await lw.parser.parse.bib(vscode.Uri.file(fileName), bibtex ?? '')
251250
if (ast === undefined) {
252251
logger.log(`Parsed 0 bib entries from ${fileName}.`)
253252
lw.event.fire(lw.event.FileParsed, fileName)

src/core/commands.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,10 @@ export async function devParseBib() {
424424
if (vscode.window.activeTextEditor === undefined) {
425425
return
426426
}
427-
const ast = await lw.parser.parse.bib(vscode.window.activeTextEditor.document.getText())
427+
const ast = await lw.parser.parse.bib(
428+
vscode.window.activeTextEditor.document.uri,
429+
vscode.window.activeTextEditor.document.getText()
430+
)
428431
return vscode.workspace.openTextDocument({content: JSON.stringify(ast, null, 2), language: 'json'}).then(doc => vscode.window.showTextDocument(doc))
429432
}
430433

src/lint/bibtex-formatter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ async function formatDocument(document: vscode.TextDocument, sort: boolean, alig
5858
const columnOffset = range ? range.start.character : 0
5959

6060
logger.log('Parse active BibTeX document for AST.')
61-
const ast = await lw.parser.parse.bib(document.getText(range))
61+
const ast = await lw.parser.parse.bib(document.uri, document.getText(range))
6262
if (ast === undefined) {
6363
return []
6464
}

src/outline/structure/bibtex.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export async function buildBibTeX(document: vscode.TextDocument): Promise<TeXEle
2828
return []
2929
}
3030
logger.log('Parse active BibTeX document for AST.')
31-
const ast = await lw.parser.parse.bib(document.getText())
31+
const ast = await lw.parser.parse.bib(document.uri, document.getText())
3232
if (ast === undefined) {
3333
return []
3434
}

src/parse/parser.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as vscode from 'vscode'
12
import * as path from 'path'
23
import * as workerpool from 'workerpool'
34
import type * as Ast from '@unified-latex/unified-latex-types'
@@ -12,6 +13,7 @@ import { latexLogParser } from './parser/latexlog'
1213
import { toString } from '../../../resources/unified.js'
1314

1415
const logger = lw.log('Parser')
16+
const bibDiagnostics = vscode.languages.createDiagnosticCollection('BibTeX')
1517

1618
export const parser = {
1719
bib,
@@ -42,12 +44,22 @@ async function reset() {
4244
return (await proxy).reset(getMacroDefs(), getEnvDefs())
4345
}
4446

45-
async function bib(s: string, options?: bibtexParser.ParserOptions): Promise<bibtexParser.BibtexAst | undefined> {
46-
const ast = await (await proxy).parseBibTeX(s, options)
47-
if (ast instanceof Error) {
48-
logger.logError('Error when parsing bib file.', ast)
47+
async function bib(uri: vscode.Uri, s: string): Promise<bibtexParser.BibtexAst | undefined> {
48+
const ast = await (await proxy).parseBibTeX(s)
49+
if (typeof ast === 'string') {
50+
const err = JSON.parse(ast) as bibtexParser.SyntaxError
51+
logger.log(`Error when parsing bib file: found ${err.found} from ${err.location.start.line}:${err.location.start.column} to ${err.location.end.line}:${err.location.end.column}.`)
52+
bibDiagnostics.set(uri, [new vscode.Diagnostic(
53+
new vscode.Range(
54+
new vscode.Position(err.location.start.line - 1, err.location.start.column - 1),
55+
new vscode.Position(err.location.end.line - 1, err.location.end.column - 1)
56+
),
57+
`A BibTeX parsing error occurred. "${err.found}" is unexpected here. No BibTeX entries will be available.`,
58+
vscode.DiagnosticSeverity.Warning
59+
)])
4960
return undefined
5061
} else {
62+
bibDiagnostics.set(uri, [])
5163
return ast
5264
}
5365
}

src/parse/parser/unified.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type * as Ast from '@unified-latex/unified-latex-types'
33
// import { getParser } from '@unified-latex/unified-latex-util-parse'
44
// import { attachMacroArgs } from '@unified-latex/unified-latex-util-arguments'
55
import { bibtexParser } from 'latex-utensils'
6+
import type { SyntaxError } from 'latex-utensils/out/types/src/pegjs/pegjs_types.js'
67

78
// @ts-expect-error Load unified.js from /out/src/...
89
import { getParser, attachMacroArgs } from '../../../../resources/unified.js'
@@ -26,12 +27,12 @@ function reset(macros: Ast.MacroInfoRecord, environments: Ast.EnvInfoRecord) {
2627
unifiedParser = getParser({ macros, environments, flags: { autodetectExpl3AndAtLetter: true } })
2728
}
2829

29-
function parseBibTeX(s: string, options?: bibtexParser.ParserOptions): bibtexParser.BibtexAst | Error | undefined {
30+
function parseBibTeX(s: string): bibtexParser.BibtexAst | string | undefined {
3031
try {
31-
return bibtexParser.parse(s, options)
32+
return bibtexParser.parse(s)
3233
} catch (err) {
33-
if (err instanceof Error) {
34-
return err
34+
if (bibtexParser.isSyntaxError(err)) {
35+
return JSON.stringify(err)
3536
}
3637
return undefined
3738
}

test/units/11_parser_tex.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as vscode from 'vscode'
12
import * as path from 'path'
23
import * as sinon from 'sinon'
34
import { lw } from '../../src/lw'
@@ -26,7 +27,7 @@ describe(path.basename(__filename).split('.')[0] + ':', () => {
2627

2728
describe('lw.parser->bib', () => {
2829
it('should parse BibTeX content', async () => {
29-
const ast = await parser.bib('@article{key, author = "author"}')
30+
const ast = await parser.bib(vscode.Uri.file('/main.bib'), '@article{key, author = "author"}')
3031

3132
assert.ok(ast)
3233
assert.strictEqual(ast.content[0].entryType, 'article')
@@ -37,7 +38,7 @@ describe(path.basename(__filename).split('.')[0] + ':', () => {
3738
})
3839

3940
it('should log error when parsing BibTeX content fails', async () => {
40-
const ast = await parser.bib('@article{key, author = "author",')
41+
const ast = await parser.bib(vscode.Uri.file('/main.bib'), '@article{key, author = "author",')
4142

4243
assert.strictEqual(ast, undefined)
4344
assert.hasLog('Error when parsing bib file.')

0 commit comments

Comments
 (0)