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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
use crate::*; use std::fmt::{Debug, Error, Formatter}; use std::sync::Arc; #[derive(Debug)] pub struct ParseErrorContext { pub node: NodeId, pub span: Span, } impl ParseErrorContext { pub fn new(node: NodeId, span: Span) -> Self { Self { node, span } } } #[derive(Debug)] pub struct ParseError { pub problem: Box<dyn Problem + 'static>, pub span: Span, pub context: Vec<ParseErrorContext>, } impl ParseError { pub fn new( problem: Box<dyn Problem + 'static>, span: Span, context: Vec<ParseErrorContext>, ) -> Self { Self { problem, span, context, } } } pub struct State { pub input: Span, pub nodes: Vec<Node>, extras: Vec<Option<Arc<dyn Parser>>>, parsing_extra: bool, pub(crate) errors: Vec<ParseError>, pub panic: bool, } impl Debug for State { fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> { self.input.fmt(f)?; self.nodes.fmt(f)?; Ok(()) } } impl<'a> From<&'a str> for State { fn from(input: &'a str) -> State { let input: Span = input.into(); Self { input: input.clone(), nodes: vec![Node::root(input)], extras: vec![], errors: vec![], parsing_extra: false, panic: false, } } } impl State { pub fn parse(input: &str, parser: impl Parser) -> Parsed { let mut state = Self::from(input); state.add(parser); let nodes = state.nodes.pop().expect("At least root").children; Parsed { input: input.into(), rest: state.input, nodes, errors: state.errors, } } pub fn add_node(&mut self, node: Node) { if !self.parsing_extra && !self.panic { self.add_extra(); } self.add_node_inner(node); if !self.parsing_extra && !self.panic { self.add_extra(); } } pub fn add(&mut self, parser: impl Parser) { if !self.parsing_extra && !self.panic { self.add_extra(); } let node = parser.parse(self); self.add_node_inner(node); if !self.parsing_extra && !self.panic { self.add_extra(); } } } impl State { pub fn push_extra(&mut self, extra: std::sync::Arc<dyn Parser>) { self.extras.push(Some(extra)); } pub fn push_atomic(&mut self) { self.extras.push(None); } pub fn pop_extra(&mut self) { self.extras.pop(); } pub(crate) fn add_node_inner(&mut self, node: Node) { let parent = self.node().expect("At least root"); if node.is(NodeId::VIRTUAL) { for mut child in node.children { if !child.is(NodeId::EXTRA) { child.add_aliases(node.alias.as_slice()); } parent.children.push(child); } return; } parent.children.push(node); } pub(crate) fn last_error(&mut self) -> Option<&mut Node> { self.node() .and_then(|root| root.children.last_mut()) .and_then(|node| { if node.is(NodeId::ERROR) { Some(node) } else { None } }) } pub(crate) fn pop_node(&mut self) -> Option<Node> { self.node().and_then(|root| root.children.pop()) } } impl State { fn node(&mut self) -> Option<&mut Node> { self.nodes.last_mut() } fn add_extra(&mut self) { if let Some(Some(extra)) = self.extras.last() { self.parsing_extra = true; let extra = extra.clone(); let mut extra_node = extra.parse(self); extra_node.add_alias(NodeId::EXTRA); self.add_node_inner(extra_node); self.parsing_extra = false; } } }