grass/src/common.rs
ConnorSkees 969726eb30 Revert string interning
There existed issues related to multithreaded tests that are difficult
to resolve. In the future interning may be reimplemented but in a more
limited capacity.

The motivation behind interning *values* was that it appeared checking
for named colors was responsible for much of the time lost when parsing
unquoted strings. If interning were to be reimplemented, it may make
sense to limit it solely to identifiers and style properties.
2020-05-22 14:20:31 -04:00

122 lines
2.9 KiB
Rust

use std::fmt::{self, Display, Write};
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum Op {
Equal,
NotEqual,
GreaterThan,
GreaterThanEqual,
LessThan,
LessThanEqual,
Plus,
Minus,
Mul,
Div,
Rem,
And,
Or,
Not,
}
impl Display for Op {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Equal => write!(f, "=="),
Self::NotEqual => write!(f, "!="),
Self::GreaterThanEqual => write!(f, ">="),
Self::LessThanEqual => write!(f, "<="),
Self::GreaterThan => write!(f, ">"),
Self::LessThan => write!(f, "<"),
Self::Plus => write!(f, "+"),
Self::Minus => write!(f, "-"),
Self::Mul => write!(f, "*"),
Self::Div => write!(f, "/"),
Self::Rem => write!(f, "%"),
Self::And => write!(f, "and"),
Self::Or => write!(f, "or"),
Self::Not => write!(f, "not"),
}
}
}
impl Op {
/// Get order of precedence for an operator
///
/// Higher numbers are evaluated first.
/// Do not rely on the number itself, but rather the size relative to other numbers
///
/// If precedence is equal, the leftmost operation is evaluated first
pub fn precedence(self) -> usize {
match self {
Self::And | Self::Or | Self::Not => 0,
Self::Equal
| Self::NotEqual
| Self::GreaterThan
| Self::GreaterThanEqual
| Self::LessThan
| Self::LessThanEqual => 1,
Self::Plus | Self::Minus => 2,
Self::Mul | Self::Div | Self::Rem => 3,
}
}
}
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub(crate) enum QuoteKind {
Quoted,
None,
}
impl Display for QuoteKind {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Quoted => f.write_char('"'),
Self::None => Ok(()),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Brackets {
None,
Bracketed,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum ListSeparator {
Space,
Comma,
}
impl ListSeparator {
pub fn as_str(self) -> &'static str {
match self {
Self::Space => " ",
Self::Comma => ", ",
}
}
pub fn name(self) -> &'static str {
match self {
Self::Space => "space",
Self::Comma => "comma",
}
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) struct QualifiedName {
pub ident: String,
pub namespace: Option<String>,
}
impl Display for QualifiedName {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(namespace) = &self.namespace {
write!(f, "{}|", namespace)?;
}
f.write_str(&self.ident)
}
}