From 81ff84ad73c8ce03bcd11f995aaf31674c2dffe2 Mon Sep 17 00:00:00 2001 From: Erin Date: Tue, 13 Apr 2021 17:40:20 +0200 Subject: [PATCH 1/2] Improved Scanner --- src/main.rs | 8 ++++---- src/scanner.rs | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index 814bebbe..ebb24b58 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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"); diff --git a/src/scanner.rs b/src/scanner.rs index e69de29b..1e968262 100644 --- a/src/scanner.rs +++ b/src/scanner.rs @@ -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) { + let part = &self.source[location.clone()]; + println!("Unknown keyword `{}` found on {:?}", part, location); + } +} From 1db4bd74cf3392c8dafa323ce94a258e4de19c40 Mon Sep 17 00:00:00 2001 From: Erin Date: Tue, 13 Apr 2021 17:43:54 +0200 Subject: [PATCH 2/2] clippy + fmt --- src/main.rs | 7 ++----- src/parser.rs | 2 +- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/src/main.rs b/src/main.rs index ebb24b58..5d8f61a7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,9 @@ -extern crate clap; -use clap::{App, Arg}; - mod base_55; mod parser; -pub mod tokens; mod scanner; +mod tokens; -use logos::Logos; +use clap::{App, Arg}; use scanner::Scanner; fn main() { diff --git a/src/parser.rs b/src/parser.rs index 56fa18f3..1c4be808 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1,4 +1,4 @@ -use crate::tokens::{Abool, Token}; +use crate::tokens::Abool; pub fn abool2num(abool: Abool) -> i32 { match abool {