Merge pull request #8 from erindesu/master
This commit is contained in:
commit
35586aa700
15
src/main.rs
15
src/main.rs
|
@ -1,11 +1,10 @@
|
||||||
extern crate clap;
|
|
||||||
use clap::{App, Arg};
|
|
||||||
|
|
||||||
mod base_55;
|
mod base_55;
|
||||||
mod parser;
|
mod parser;
|
||||||
pub mod tokens;
|
mod scanner;
|
||||||
|
mod tokens;
|
||||||
|
|
||||||
use logos::Logos;
|
use clap::{App, Arg};
|
||||||
|
use scanner::Scanner;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let matches = App::new("AbleScript")
|
let matches = App::new("AbleScript")
|
||||||
|
@ -27,10 +26,8 @@ fn main() {
|
||||||
let source = std::fs::read_to_string(file_path).unwrap();
|
let source = std::fs::read_to_string(file_path).unwrap();
|
||||||
|
|
||||||
// Print token type: `value`
|
// Print token type: `value`
|
||||||
let mut lex = tokens::Token::lexer(&source);
|
let mut scanner = Scanner::new(&source);
|
||||||
while let Some(token) = lex.next() {
|
scanner.scan();
|
||||||
println!("{:?}: `{}`", token, lex.slice());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
println!("hi");
|
println!("hi");
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::tokens::{Abool, Token};
|
use crate::tokens::Abool;
|
||||||
|
|
||||||
pub fn abool2num(abool: Abool) -> i32 {
|
pub fn abool2num(abool: Abool) -> i32 {
|
||||||
match abool {
|
match abool {
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue