Skip to content

Commit

Permalink
Allow parsing Latin-1 encoded files in Lua example
Browse files Browse the repository at this point in the history
  • Loading branch information
0x2a-42 committed Dec 24, 2024
1 parent 800380c commit 593bdde
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion examples/lua/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,23 @@ use codespan_reporting::term::termcolor::{ColorChoice, StandardStream};
use codespan_reporting::term::{self, Config};
use logos::Logos;
use parser::*;
use std::io::Read;

fn main() -> std::io::Result<()> {
let args: Vec<String> = std::env::args().collect();
if args.len() != 2 {
std::process::exit(1);
}

let source = std::fs::read_to_string(&args[1])?;
let mut buf = Vec::new();
std::fs::File::open(&args[1])?.read_to_end(&mut buf)?;
let source = if std::str::from_utf8(&buf).is_ok() {
unsafe { String::from_utf8_unchecked(buf) }
} else {
// ISO 8859-1
buf.iter().map(|&c| c as char).collect()
};

let mut diags = vec![];
let (tokens, ranges) = tokenize(Token::lexer(&source), &mut diags);
let cst = Parser::parse(&source, tokens, ranges, &mut diags);
Expand Down

0 comments on commit 593bdde

Please sign in to comment.