Improved Scanner

This commit is contained in:
Erin 2021-04-13 17:40:20 +02:00 committed by ondra05
parent ceaed053b4
commit 406c6b1297
2 changed files with 37 additions and 4 deletions

View file

@ -4,8 +4,10 @@ use clap::{App, Arg};
mod base_55;
mod parser;
pub mod tokens;
mod scanner;
use logos::Logos;
use scanner::Scanner;
fn main() {
let matches = App::new("AbleScript")
@ -27,10 +29,8 @@ fn main() {
let source = std::fs::read_to_string(file_path).unwrap();
// Print token type: `value`
let mut lex = tokens::Token::lexer(&source);
while let Some(token) = lex.next() {
println!("{:?}: `{}`", token, lex.slice());
}
let mut scanner = Scanner::new(&source);
scanner.scan();
}
None => {
println!("hi");

View file

@ -0,0 +1,33 @@
use std::ops::Range;
use logos::Logos;
use crate::tokens::{self, Token};
pub struct Scanner<'a> {
source: &'a str,
lexer: logos::Lexer<'a, Token>,
}
impl<'a> Scanner<'a> {
pub fn new(source: &'a str) -> Self {
Self {
source,
lexer: tokens::Token::lexer(source),
}
}
pub fn scan(&mut self) {
while let Some(tok) = self.lexer.next() {
if matches!(tok, Token::Error) {
self.throw_err(&self.lexer.span());
} else {
println!("Token: {:?}", tok);
}
}
}
fn throw_err(&self, location: &Range<usize>) {
let part = &self.source[location.clone()];
println!("Unknown keyword `{}` found on {:?}", part, location);
}
}