From 78f46a454530495006aa997b93332a7e527cce27 Mon Sep 17 00:00:00 2001 From: jasonwilliams Date: Mon, 13 Aug 2018 11:32:26 +0100 Subject: [PATCH] finally got buffer working (using UTF-8) --- src/bin/bin.rs | 7 ++----- src/lib/syntax/lexer.rs | 27 +++++++++++++++------------ 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/bin/bin.rs b/src/bin/bin.rs index cdc8482941..58640ba685 100644 --- a/src/bin/bin.rs +++ b/src/bin/bin.rs @@ -1,12 +1,9 @@ extern crate js; use js::syntax::lexer::Lexer; use std::fs::read_to_string; -use std::fs::File; -use std::io::BufReader; pub fn main() { - let mut f = File::open("test.js").expect("file not found"); - let mut reader = BufReader::new(f); - let mut lexer = Lexer::new(reader); + let buffer = read_to_string("test.js").unwrap(); + let mut lexer = Lexer::new(&buffer); lexer.lex() } diff --git a/src/lib/syntax/lexer.rs b/src/lib/syntax/lexer.rs index 550f2772b0..ab7ad88ace 100644 --- a/src/lib/syntax/lexer.rs +++ b/src/lib/syntax/lexer.rs @@ -1,10 +1,9 @@ -use std::io::{BufRead, BufReader, ErrorKind}; use std::str::Chars; use syntax::ast::punc::Punctuator; use syntax::ast::token::{Token, TokenData}; /// A javascript Lexer -pub struct Lexer { +pub struct Lexer<'a> { // The list fo tokens generated so far pub tokens: Vec, // The current line number in the script @@ -12,16 +11,16 @@ pub struct Lexer { // the current column number in the script column_number: u64, // The full string - buffer: B, + buffer: Chars<'a>, } -impl Lexer { - pub fn new(buffer: B) -> Lexer { +impl<'a> Lexer<'a> { + pub fn new(buffer: &'a str) -> Lexer<'a> { Lexer { tokens: Vec::new(), line_number: 1, column_number: 0, - buffer: buffer, + buffer: buffer.chars(), } } /// Push tokens onto the token queue @@ -35,17 +34,21 @@ impl Lexer { self.push_token(TokenData::TPunctuator(punc)); } - fn next(&mut self) -> char { - let mut buffer = [0; 1]; - self.buffer.read_exact(&mut buffer).unwrap(); - let result = buffer as char; - result + fn next(&mut self) -> Option { + self.buffer.next() } pub fn lex(&mut self) { loop { let ch = self.next(); - println!("{}", ch.unwrap()); + match ch { + Some(c) => { + println!("{}", c); + } + None => { + break; + } + } } } }