fiddling with a parser

This commit is contained in:
2020-10-11 21:12:59 -07:00
parent 86ce4821a4
commit bae64b0851
6 changed files with 132 additions and 49 deletions

View File

@@ -10,6 +10,8 @@ extern crate nalgebra as na;
extern crate rand;
extern crate specs;
extern crate time;
#[macro_use]
extern crate nom;
use std::path::Path;
use std::sync::Arc;
@@ -57,6 +59,7 @@ pub mod canvas;
pub mod render_system;
pub mod compu_system;
pub mod event_system;
pub mod parser;
#[derive(Default)]
pub struct PersistentState {
@@ -72,61 +75,26 @@ struct TrSprite {
}
use std::fs;
use nom::sequence::{preceded, tuple};
use nom::bytes::complete::{take_while1, tag, take_while_m_n};
use nom::character::complete::line_ending;
use nom::error::ErrorKind;
use nom::combinator::map_res;
use nom::IResult;
use crate::parser::parser::{Color, hex_color, parse_script};
use logos::Logos;
#[derive(Logos, Debug, PartialEq)]
enum Token {
// Tokens can be literal strings, of any length.
#[token("fast")]
Fast,
#[token(".")]
Period,
// Or regular expressions.
#[regex("[a-zA-Z]+")]
Text,
// Logos requires one token variant to handle errors,
// it can be named anything you wish.
#[error]
// We can also use this variant to define whitespace,
// or any other matches we wish to skip.
#[regex(r"[ \t\n\f]+", logos::skip)]
Error,
}
pub fn main() {
//https://dylanede.github.io/cassowary-rs/cassowary/index.html
let mut lex = Token::lexer("Create ridiculously fast Lexers.");
assert_eq!(lex.next(), Some(Token::Text));
assert_eq!(lex.span(), 0..6);
assert_eq!(lex.slice(), "Create");
assert_eq!(lex.next(), Some(Token::Text));
assert_eq!(lex.span(), 7..19);
assert_eq!(lex.slice(), "ridiculously");
assert_eq!(lex.next(), Some(Token::Fast));
assert_eq!(lex.span(), 20..24);
assert_eq!(lex.slice(), "fast");
assert_eq!(lex.next(), Some(Token::Text));
assert_eq!(lex.span(), 25..31);
assert_eq!(lex.slice(), "Lexers");
assert_eq!(lex.next(), Some(Token::Period));
assert_eq!(lex.span(), 31..32);
assert_eq!(lex.slice(), ".");
assert_eq!(lex.next(), None);
let input_string = fs::read_to_string("./resources/scripts/scratch").unwrap();
parse_script::<(&str, ErrorKind)>(&input_string);
return;
//hprof::start_frame();
//let g = hprof::enter("vulkan preload");