Browse Source

trying to use buffer again

pull/1/head
Jason Williams 6 years ago
parent
commit
f27f4f96e7
  1. 12
      src/bin/bin.rs
  2. 33
      src/lib/syntax/lexer.rs

12
src/bin/bin.rs

@ -1,6 +1,12 @@
extern crate js;
use js::syntax::lexer::Lexer;
use std::fs::read_to_string; use std::fs::read_to_string;
use std::fs::File;
use std::io::BufReader;
pub fn main() { pub fn main() {
let buffer = read_to_string("test.js").unwrap(); let mut f = File::open("test.js").expect("file not found");
println!("{}", buffer); let mut reader = BufReader::new(f);
} let mut lexer = Lexer::new(reader);
lexer.lex()
}

33
src/lib/syntax/lexer.rs

@ -1,20 +1,22 @@
use std::io::{BufRead, BufReader, ErrorKind};
use std::str::Chars;
use syntax::ast::punc::Punctuator; use syntax::ast::punc::Punctuator;
use syntax::ast::token::{Token, TokenData}; use syntax::ast::token::{Token, TokenData};
/// A javascript Lexer /// A javascript Lexer
pub struct Lexer { pub struct Lexer<B> {
// The list fo tokens generated so far // The list fo tokens generated so far
pub tokens: Vec<Token>, pub tokens: Vec<Token>,
// The current line number in the script // The current line number in the script
line_number: u64, line_number: u64,
// the current column number in the script // the current column number in the script
column_number: u64, column_number: u64,
// the reader // The full string
buffer: String, buffer: B,
} }
impl Lexer { impl<B: BufRead> Lexer<B> {
pub fn new(buffer: String) -> Lexer { pub fn new(buffer: B) -> Lexer<B> {
Lexer { Lexer {
tokens: Vec::new(), tokens: Vec::new(),
line_number: 1, line_number: 1,
@ -33,22 +35,17 @@ impl Lexer {
self.push_token(TokenData::TPunctuator(punc)); self.push_token(TokenData::TPunctuator(punc));
} }
/// Processes an input stream from a string into an array of tokens fn next(&mut self) -> char {
pub fn lex_str(script: String) -> Vec<Token> { let mut buffer = [0; 1];
let mut lexer = Lexer::new(script); self.buffer.read_exact(&mut buffer).unwrap();
lexer.tokens let result = buffer as char;
result
} }
fn next(&mut self) -> Option<char> { pub fn lex(&mut self) {
self.buffer.chars().next()
}
pub fn lex(&mut self) -> Result<(), &str> {
loop { loop {
let ch = match self.next() { let ch = self.next();
Some(ch) => ch, println!("{}", ch.unwrap());
None => return Err("oh my days"),
};
} }
} }
} }

Loading…
Cancel
Save