Skip to content

Commit

Permalink
replace regex with simple code; bump 0.1.8
Browse files Browse the repository at this point in the history
  • Loading branch information
tiye committed Oct 2, 2021
1 parent 181ca9e commit 924671b
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 65 deletions.
42 changes: 1 addition & 41 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cirru_parser"
version = "0.1.7"
version = "0.1.8"
authors = ["jiyinyiyong <[email protected]>"]
edition = "2018"
license = "MIT"
Expand All @@ -19,8 +19,6 @@ exclude = [
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
regex = "1.5.4"
lazy_static = "1.4.0"

[dev-dependencies]
serde_json = "1.0.68"
Expand Down
3 changes: 0 additions & 3 deletions examples/demo.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// #[macro_use]
// extern crate lazy_static;

use cirru_parser::{format, parse, CirruWriterOptions};
// use std::collections::hash_map::DefaultHasher;
use std::fs;
Expand Down
3 changes: 0 additions & 3 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ parses to:
find more on <http://text.cirru.org/> .
*/

#[macro_use]
extern crate lazy_static;

mod primes;
mod s_expr;
mod tree;
Expand Down
20 changes: 13 additions & 7 deletions src/primes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use std::str;
// use std::marker::Copy;

use crate::s_expr;
use regex::Regex;

/// Cirru uses nested Vecters and Strings as data structure
#[derive(Clone)]
Expand Down Expand Up @@ -35,15 +34,11 @@ pub enum CirruLexItem {

pub type CirruLexItemList = Vec<CirruLexItem>;

lazy_static! {
static ref RE_SIMPLE_TOKEN: Regex = Regex::new(r"^[\w\d\-\?!\+\*\$@!#%&_=|:\.<>]+$").unwrap();
}

impl fmt::Display for Cirru {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Cirru::Leaf(a) => {
if RE_SIMPLE_TOKEN.is_match(a) {
if is_normal_str(a) {
write!(f, "{}", a)
} else {
write!(f, "{}", escape_cirru_leaf(a))
Expand All @@ -63,6 +58,17 @@ impl fmt::Display for Cirru {
}
}

fn is_normal_str(tok: &str) -> bool {
for s in tok.chars() {
if !matches!(s, 'A'..='Z' | 'a'..='z'|'0'..='9' | '-' | '?' |'!'|'+'|'*'|'$'|'@'|'#'|'%'|'&'|'_'|'='|'|'|':'|'.'|'<'|'>')
{
return false;
}
}

true
}

/// common API for turning Cirru leaf with strings escaped
/// ```rust
/// use cirru_parser::escape_cirru_leaf;
Expand All @@ -71,7 +77,7 @@ impl fmt::Display for Cirru {
/// ```
pub fn escape_cirru_leaf(s: &str) -> String {
let mut chunk = String::from("\"");
if RE_SIMPLE_TOKEN.is_match(s) {
if is_normal_str(s) {
chunk.push_str(s);
} else {
chunk.push_str(&s.escape_default().to_string());
Expand Down
21 changes: 13 additions & 8 deletions src/s_expr.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
use crate::primes::Cirru;

use lazy_static::lazy_static;
use regex::Regex;

lazy_static! {
static ref ENDS_WITH_NEWLINE: Regex = Regex::new("\\n\\s+$").unwrap();
}

/// format to Cirru to WAT
pub fn format_to_lisp(xs: Vec<Cirru>) -> Result<String, String> {
let mut content: String = String::from("\n");
Expand Down Expand Up @@ -43,7 +36,7 @@ pub fn format_expr(node: &Cirru, indent: usize) -> Result<String, String> {
chunk = format!("{}{}", chunk, next);
}
// TODO dirty way, but intuitive for now
if idx < xs.len() - 1 && !ENDS_WITH_NEWLINE.is_match(&chunk) {
if idx < xs.len() - 1 && !ends_with_newline(&chunk) {
chunk = format!("{} ", chunk);
}
}
Expand All @@ -67,6 +60,18 @@ pub fn format_expr(node: &Cirru, indent: usize) -> Result<String, String> {
}
}

pub fn ends_with_newline(s: &str) -> bool {
for c in s.chars().rev() {
if c == 'c' {
continue;
}
if c == '\n' {
return true;
}
}
false
}

pub fn gen_newline(n: usize) -> String {
let mut chunk: String = String::from("\n");
for _ in 0..n {
Expand Down

0 comments on commit 924671b

Please sign in to comment.