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

Handle files with BOM in lexer. #285

Merged
merged 3 commits into from
Sep 18, 2021
Merged
Changes from 1 commit
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
11 changes: 10 additions & 1 deletion src/dreammaker/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,10 @@ enum Directive {
Stringy,
}

fn has_bom(slice: &[u8]) -> bool {
slice.len() > 3 && slice[0] == 0xEF && slice[1] == 0xBB && slice[2] == 0xBF
PJB3005 marked this conversation as resolved.
Show resolved Hide resolved
}

fn buffer_read<R: Read>(file: FileId, mut read: R) -> Result<Vec<u8>, DMError> {
let mut buffer = Vec::new();

Expand Down Expand Up @@ -630,9 +634,14 @@ impl<'ctx> HasLocation for Lexer<'ctx> {
impl<'ctx> Lexer<'ctx> {
/// Create a new lexer from a byte stream.
pub fn new<I: Into<Cow<'ctx, [u8]>>>(context: &'ctx Context, file_number: FileId, input: I) -> Self {
let mut cow = input.into();
if has_bom(&cow) {
cow = Cow::from(cow[3..].to_owned());
PJB3005 marked this conversation as resolved.
Show resolved Hide resolved
}

Lexer {
context,
input: LocationTracker::new(file_number, input.into()),
input: LocationTracker::new(file_number, cow),
next: None,
final_newline: false,
at_line_head: true,
Expand Down