1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
use std::clone::Clone;
use std::cmp::Ordering;
use std::fmt;
use std::hash::{Hash, Hasher};
use std::str;
// use std::marker::Copy;

use regex::Regex;

/// Cirru uses nested Vecters and Strings as data structure
#[derive(Clone)]
pub enum CirruNode {
  CirruLeaf(String),
  CirruList(Vec<CirruNode>),
}

#[derive(fmt::Debug, PartialEq)]
pub enum CirruLexState {
  LexStateSpace,
  LexStateToken,
  LexStateEscape,
  LexStateIndent,
  LexStateString,
}

/// internal control item during lexing
#[derive(fmt::Debug, PartialEq, PartialOrd, Ord, Eq, Clone)]
pub enum CirruLexItem {
  LexItemOpen,
  LexItemClose,
  LexItemIndent(usize),
  LexItemString(String),
}

pub type CirruLexItemList = Vec<CirruLexItem>;

use CirruNode::{CirruLeaf, CirruList};

impl fmt::Display for CirruNode {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    match self {
      Self::CirruLeaf(a) => {
        let re = Regex::new(r"^[\w\d\-\?!]+$").unwrap();
        if re.is_match(a) {
          write!(f, "{}", a)
        } else {
          write!(f, "\"{}\"", str::escape_debug(a))
        }
      }
      Self::CirruList(xs) => {
        write!(f, "(")?;
        for (idx, x) in xs.iter().enumerate() {
          if idx > 0 {
            write!(f, " ")?;
          }
          write!(f, "{}", x)?;
        }
        write!(f, ")")?;
        Ok(())
      }
    }
  }
}

impl fmt::Debug for CirruNode {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    // just use fn from Display
    write!(f, "{}", format!("{}", self))?;
    Ok(())
  }
}

impl Eq for CirruNode {}

impl PartialEq for CirruNode {
  fn eq(&self, other: &Self) -> bool {
    match (self, other) {
      (Self::CirruLeaf(a), Self::CirruLeaf(b)) => a == b,
      (Self::CirruList(a), Self::CirruList(b)) => a == b,
      (_, _) => false,
    }
  }
}

impl Ord for CirruNode {
  fn cmp(&self, other: &Self) -> Ordering {
    match (self, other) {
      (CirruLeaf(a), CirruLeaf(b)) => a.cmp(b),
      (CirruLeaf(_), CirruList(_)) => Ordering::Less,
      (CirruList(_), CirruLeaf(_)) => Ordering::Greater,
      (CirruList(a), CirruList(b)) => a.cmp(b),
    }
  }
}

impl PartialOrd for CirruNode {
  fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
    Some(self.cmp(other))
  }
}

impl Hash for CirruNode {
  fn hash<H: Hasher>(&self, state: &mut H) {
    match self {
      CirruLeaf(s) => {
        s.hash(state);
      }
      CirruList(xs) => {
        xs.hash(state);
      }
    }
  }
}

impl CirruNode {
  pub fn len(&self) -> usize {
    match self {
      CirruLeaf(s) => s.len(),
      CirruList(xs) => xs.len(),
    }
  }

  pub fn is_empty(&self) -> bool {
    match self {
      CirruLeaf(s) => s.len() == 0,
      CirruList(xs) => xs.len() == 0,
    }
  }
}