use std::collections::{HashMap, HashSet};
use std::sync::Arc;
use crate::grammar::{ClassItem, Span, ValueBuilder};
use crate::tree::{FieldCount, FieldId, Kind, KindId, Names};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub(crate) struct RuleId(pub u32);
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub(crate) struct FlagId(pub u16);
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub(crate) struct CaptureId(pub u16);
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub(crate) struct LabelId(pub u32);
#[derive(Debug)]
pub(crate) struct Grammar {
pub rules: Vec<Rule>,
pub entry: HashMap<String, Expr>,
pub names: Arc<Names>,
pub kind_display: Vec<String>,
pub skip: Option<Expr>,
pub word: CharSet,
pub reserved: Reserved,
pub tab_width: u32,
pub labels: Vec<Label>,
pub error_alternatives: u32,
pub error_ranks: Vec<u32>,
pub recoveries: Vec<Option<Expr>>,
pub kind_fields: Vec<Vec<(FieldId, FieldCount)>>,
pub kind_builders: Vec<Option<ValueBuilder>>,
pub kind_tokens: Vec<bool>,
pub kind_made: Vec<bool>,
pub single_item: HashMap<String, bool>,
}
impl Grammar {
pub fn kinds(&self) -> Vec<Kind> {
(self.names.kinds.iter().enumerate())
.map(|(id, name)| Kind {
name: name.clone(),
display: self.kind_display.get(id).cloned().unwrap_or_default(),
fields: self.kind_fields.get(id).cloned().unwrap_or_default(),
builder: self.kind_builders.get(id).cloned().flatten(),
token: self.kind_tokens.get(id).copied().unwrap_or(false),
made: self.kind_made.get(id).copied().unwrap_or(false),
})
.collect()
}
}
#[derive(Debug)]
pub(crate) struct Rule {
pub name: String,
pub span: Span,
pub body: Expr,
pub memo: MemoMark,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum MemoMark {
None,
Memo,
NoMemo,
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub(crate) enum Label {
Message(String),
Named(String),
}
impl Label {
pub fn text(&self) -> &str {
match self {
Label::Message(text) | Label::Named(text) => text,
}
}
}
#[derive(Debug, Default)]
pub(crate) struct Reserved {
pub exact: HashSet<String>,
pub folded: HashSet<String>,
}
impl Reserved {
pub fn contains(&self, text: &str) -> bool {
self.exact.contains(text) || self.folded.contains(&text.to_ascii_lowercase())
}
}
#[derive(Clone, Debug, PartialEq)]
pub(crate) struct CharSet {
pub negated: bool,
pub items: Vec<ClassItem>,
}
impl CharSet {
pub fn contains(&self, c: char) -> bool {
let listed = self.items.iter().any(|item| match *item {
ClassItem::Range(first, last) => first <= c && c <= last,
ClassItem::Property { property, negated } => property.contains(c) != negated,
});
listed != self.negated
}
}
#[derive(Clone, Debug)]
pub(crate) enum Expr {
Empty,
Literal {
text: Box<str>,
case_insensitive: bool,
},
Class(Box<CharSet>),
Any,
Call(RuleId),
Seq(Vec<Expr>),
Choice(Vec<Expr>),
Repeat {
expr: Box<Expr>,
min: u32,
max: Option<u32>,
},
And(Box<Expr>),
Not(Box<Expr>),
Cut,
Skip,
Token {
kind: KindId,
hidden: bool,
expr: Box<Expr>,
},
Node {
kind: KindId,
expr: Box<Expr>,
},
Transparent {
kind: KindId,
expr: Box<Expr>,
},
Field {
field: FieldId,
expr: Box<Expr>,
},
WordEnd,
Unreserved(Box<Expr>),
FlagOn {
flag: FlagId,
expr: Box<Expr>,
},
FlagOff {
flag: FlagId,
expr: Box<Expr>,
},
FlagTest {
flag: FlagId,
negated: bool,
},
Indent {
expr: Box<Expr>,
skip: bool,
},
Same {
skip: bool,
},
End {
skip: bool,
},
Def {
capture: CaptureId,
expr: Box<Expr>,
skip: bool,
},
Ref {
capture: CaptureId,
},
Label {
label: LabelId,
expr: Box<Expr>,
skip: bool,
},
ErrorAlternative {
expr: Box<Expr>,
message: String,
index: u32,
skip: bool,
},
Shared(Arc<Expr>),
}
impl Expr {
pub fn children(&self) -> &[Expr] {
match self {
Expr::Seq(items) | Expr::Choice(items) => items,
Expr::Repeat { expr, .. }
| Expr::And(expr)
| Expr::Not(expr)
| Expr::Token { expr, .. }
| Expr::Node { expr, .. }
| Expr::Transparent { expr, .. }
| Expr::Field { expr, .. }
| Expr::Unreserved(expr)
| Expr::FlagOn { expr, .. }
| Expr::FlagOff { expr, .. }
| Expr::Indent { expr, .. }
| Expr::Def { expr, .. }
| Expr::Label { expr, .. }
| Expr::ErrorAlternative { expr, .. } => std::slice::from_ref(&**expr),
Expr::Shared(expr) => std::slice::from_ref(&**expr),
Expr::Empty
| Expr::Literal { .. }
| Expr::Class(_)
| Expr::Any
| Expr::Call(_)
| Expr::Cut
| Expr::Skip
| Expr::WordEnd
| Expr::FlagTest { .. }
| Expr::Same { .. }
| Expr::End { .. }
| Expr::Ref { .. } => &[],
}
}
}