tree-sitter/src/rules.rs

192 lines
4.5 KiB
Rust
Raw Normal View History

2018-12-05 12:50:12 -08:00
use std::rc::Rc;
2018-12-08 13:44:11 -08:00
use std::char;
2018-12-05 12:50:12 -08:00
use std::collections::HashMap;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
2018-12-06 22:11:52 -08:00
pub(crate) enum SymbolType {
2018-12-05 12:50:12 -08:00
External,
Terminal,
NonTerminal,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
2018-12-06 22:11:52 -08:00
pub(crate) enum Associativity {
2018-12-05 12:50:12 -08:00
Left,
Right
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
2018-12-06 22:11:52 -08:00
pub(crate) struct Alias {
pub value: String,
pub is_named: bool,
2018-12-05 12:50:12 -08:00
}
2018-12-06 22:11:52 -08:00
pub(crate) type AliasMap = HashMap<Symbol, Alias>;
2018-12-05 12:50:12 -08:00
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
2018-12-06 22:11:52 -08:00
pub(crate) struct MetadataParams {
pub precedence: Option<i32>,
pub dynamic_precedence: i32,
pub associativity: Option<Associativity>,
pub is_token: bool,
pub is_string: bool,
pub is_active: bool,
pub is_main_token: bool,
pub is_excluded: bool,
pub alias: Option<Alias>,
2018-12-05 12:50:12 -08:00
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
2018-12-06 22:11:52 -08:00
pub(crate) struct Symbol {
pub kind: SymbolType,
pub index: usize,
2018-12-05 12:50:12 -08:00
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
2018-12-06 22:11:52 -08:00
pub(crate) enum Rule {
2018-12-05 12:50:12 -08:00
Blank,
String(String),
Pattern(String),
NamedSymbol(String),
Symbol(Symbol),
2018-12-08 23:35:48 -08:00
Choice(Vec<Rule>),
2018-12-05 12:50:12 -08:00
Metadata {
params: MetadataParams,
2018-12-08 23:35:48 -08:00
rule: Box<Rule>,
2018-12-05 12:50:12 -08:00
},
2018-12-08 23:35:48 -08:00
Repeat(Box<Rule>),
Seq(Vec<Rule>),
2018-12-05 12:50:12 -08:00
}
impl Rule {
pub fn token(content: Rule) -> Self {
add_metadata(content, |params| {
params.is_token = true;
})
}
pub fn immediate_token(content: Rule) -> Self {
add_metadata(content, |params| {
params.is_token = true;
params.is_main_token = true;
})
}
pub fn prec(value: i32, content: Rule) -> Self {
add_metadata(content, |params| {
params.precedence = Some(value);
})
}
pub fn prec_left(value: i32, content: Rule) -> Self {
add_metadata(content, |params| {
params.associativity = Some(Associativity::Left);
params.precedence = Some(value);
})
}
pub fn prec_right(value: i32, content: Rule) -> Self {
add_metadata(content, |params| {
params.associativity = Some(Associativity::Right);
params.precedence = Some(value);
})
}
pub fn repeat(rule: Rule) -> Self {
2018-12-08 23:35:48 -08:00
Rule::Repeat(Box::new(rule))
2018-12-05 12:50:12 -08:00
}
pub fn choice(rules: Vec<Rule>) -> Self {
let mut elements = Vec::with_capacity(rules.len());
for rule in rules {
choice_helper(&mut elements, rule);
}
2018-12-08 23:35:48 -08:00
Rule::Choice(elements)
2018-12-05 12:50:12 -08:00
}
pub fn seq(rules: Vec<Rule>) -> Self {
2018-12-08 23:35:48 -08:00
Rule::Seq(rules)
2018-12-05 12:50:12 -08:00
}
pub fn terminal(index: usize) -> Self {
Rule::Symbol(Symbol::terminal(index))
}
pub fn non_terminal(index: usize) -> Self {
Rule::Symbol(Symbol::non_terminal(index))
}
pub fn external(index: usize) -> Self {
Rule::Symbol(Symbol::external(index))
}
pub fn named(name: &'static str) -> Self {
Rule::NamedSymbol(name.to_string())
}
pub fn string(value: &'static str) -> Self {
Rule::String(value.to_string())
}
2018-12-06 22:11:52 -08:00
pub fn pattern(value: &'static str) -> Self {
Rule::Pattern(value.to_string())
}
2018-12-05 12:50:12 -08:00
}
impl Symbol {
2018-12-06 22:11:52 -08:00
pub fn is_non_terminal(&self) -> bool {
return self.kind == SymbolType::NonTerminal
}
pub fn is_external(&self) -> bool {
return self.kind == SymbolType::External
}
2018-12-05 12:50:12 -08:00
pub fn non_terminal(index: usize) -> Self {
Symbol { kind: SymbolType::NonTerminal, index }
}
pub fn terminal(index: usize) -> Self {
Symbol { kind: SymbolType::Terminal, index }
}
pub fn external(index: usize) -> Self {
Symbol { kind: SymbolType::External, index }
}
}
impl From<Symbol> for Rule {
fn from(symbol: Symbol) -> Self {
Rule::Symbol(symbol)
}
}
fn add_metadata<T: Fn(&mut MetadataParams)>(input: Rule, f: T) -> Rule {
match input {
Rule::Metadata { rule, mut params } => {
f(&mut params);
Rule::Metadata { rule, params }
},
_ => {
let mut params = MetadataParams::default();
f(&mut params);
2018-12-08 23:35:48 -08:00
Rule::Metadata { rule: Box::new(input), params }
2018-12-05 12:50:12 -08:00
}
}
}
fn choice_helper(result: &mut Vec<Rule>, rule: Rule) {
match rule {
2018-12-08 23:35:48 -08:00
Rule::Choice(elements) => {
2018-12-05 12:50:12 -08:00
for element in elements {
choice_helper(result, element);
}
},
_ => {
if !result.contains(&rule) {
result.push(rule);
}
}
}
}