A Swift library for parsing BibTeX files.
Disclaimer: This library is still under development.
Prepare your input as a string. Example:
let input = """
@Article{max20,
author={Max},
title={SwiftyBibtex},
journal={New Repositories},
year={2020},
note={A Swift library for parsing BibTeX files.}
}
@String{me="Max"}
@Preamble{"Maintained by " # me}
@Comment{TODO: Add more entries}
"""
do {
let result = try SwiftyBibtex.parse(input)
result.publications
} catch {
print("Error parsing input: \(error)")
}
let publication = publications[0]
publication.publicationType // PublicationType.article
publication.citationKey // "max20"
publication.fields // ["author": "Max", ...]
publication.rangeInFile // (1:0)...(12:0)
Note: all keys in fields
are lowercased.
You can cast publications to any of the following types:
Article
Book
Booklet
InBook
InCollection
InProceedings
Manual
MasterThesis
Misc
PhdThesis
Proceedings
TechReport
Unpublished
Each type has a set of required and optional fields that can be accessed directly. All other fields can be accessed using the fields
property.
if let article = publication as? Article {
article.author // "Max"
article.title // "SwiftyBibtex"
article.journal // "New Repositories"
article.year // 2020
article.fields["note"] // Optional("A Swift library for parsing BibTeX files.")
}
let result = try SwiftyBibtex.parse(input)
result.preambles // ["Maintained by Max"]
result.comments // ["TODO: Add more entries"]
let result = try SwiftyBibtex.parse(input)
for warning in result.warnings {
print(warning.message)
}
for error in result.errors {
print(error.line)
print(error.charPositionInLine)
print(error.message)
}
Warnings and Errors are logged to the console automatically. You can alter this behavior by setting a different logging level:
let result = try SwiftyBibtex.parse(input, loggingLevel: .warn) // Log warnings and errors.
let result = try SwiftyBibtex.parse(input, loggingLevel: .error) // Log only errors.
let result = try SwiftyBibtex.parse(input, loggingLevel: .none) // Don't log anything.
Warnings are represented by one of the following types:
DuplicateCitationKeyWarning
MismatchedDataTypeWarning
MissingRequiredFieldsWarning
UnrecognizedPublicationTypeWarning
UnusedStringDefinitionWarning
Errors are represented by one of the following types:
ExtraneousInputParserError
MismatchedInputParserError
MissingSymbolParserError
NoViableAlternativeParserError
StringDefinitionNotFoundParserError
TokenRecognitionParserError
Casting a warning or an error to one of these types allows you to get more information about it:
if let extraneousInputError = error as? ExtraneousInputParserError {
print(extraneousInputError.offendingSymbol)
print(extraneousInputError.expectedSymbols)
}
Select File > Swift Packages > Add Package Dependency...
and enter the following URL:
https://github.com/MaxHaertwig/SwiftyBibtex.git
Open Package.swift
and add the following line to your package's dependencies:
.package(url: "https://github.com/MaxHaertwig/SwiftyBibtex.git", .branch("main"))
This library makes use of ANTLR to generate its underlying BibTeX parser. The lexer and parser grammers can be found in BibtexLexer.g4 and BibtexParser.g4 respectively. If you decide to change one of the grammer files, make sure to run the generate_bibtex_parser.sh
script to generate the new parser.
The ANTLR runtime (Antlr4) is included as a package dependency.
Feedback, Issues, and Pull Requests are always welcome.
SwiftyBibtex is available under the MIT license. See LICENSE for more info.