artichoke_backend/
parser.rs1use std::borrow::Cow;
2
3use spinoso_exception::ScriptError;
4
5use crate::Artichoke;
6use crate::core::{ClassRegistry, IncrementLinenoError, Parser, TryConvertMut};
7use crate::error::{Error, RubyException};
8use crate::ffi::InterpreterExtractError;
9use crate::state::parser::Context;
10use crate::sys;
11
12impl Parser for Artichoke {
13 type Context = Context;
14 type Error = Error;
15
16 fn reset_parser(&mut self) -> Result<(), Self::Error> {
17 let mrb = unsafe { self.mrb.as_mut() };
18 let state = self.state.as_deref_mut().ok_or_else(InterpreterExtractError::new)?;
19 let parser = state.parser.as_mut().ok_or_else(InterpreterExtractError::new)?;
20 parser.reset(mrb);
21 Ok(())
22 }
23
24 fn fetch_lineno(&self) -> Result<usize, Self::Error> {
25 let state = self.state.as_deref().ok_or_else(InterpreterExtractError::new)?;
26 let parser = state.parser.as_ref().ok_or_else(InterpreterExtractError::new)?;
27 let lineno = parser.fetch_lineno();
28 Ok(lineno)
29 }
30
31 fn add_fetch_lineno(&mut self, val: usize) -> Result<usize, Self::Error> {
32 let state = self.state.as_deref_mut().ok_or_else(InterpreterExtractError::new)?;
33 let parser = state.parser.as_mut().ok_or_else(InterpreterExtractError::new)?;
34 let lineno = parser.add_fetch_lineno(val)?;
35 Ok(lineno)
36 }
37
38 fn push_context(&mut self, context: Self::Context) -> Result<(), Self::Error> {
39 let mrb = unsafe { self.mrb.as_mut() };
40 let state = self.state.as_deref_mut().ok_or_else(InterpreterExtractError::new)?;
41 let parser = state.parser.as_mut().ok_or_else(InterpreterExtractError::new)?;
42 parser.push_context(mrb, context);
43 Ok(())
44 }
45
46 fn pop_context(&mut self) -> Result<Option<Self::Context>, Self::Error> {
47 let mrb = unsafe { self.mrb.as_mut() };
48 let state = self.state.as_deref_mut().ok_or_else(InterpreterExtractError::new)?;
49 let parser = state.parser.as_mut().ok_or_else(InterpreterExtractError::new)?;
50 let context = parser.pop_context(mrb);
51 Ok(context)
52 }
53
54 fn peek_context(&self) -> Result<Option<&Self::Context>, Self::Error> {
55 let state = self.state.as_deref().ok_or_else(InterpreterExtractError::new)?;
56 let parser = state.parser.as_ref().ok_or_else(InterpreterExtractError::new)?;
57 let context = parser.peek_context();
58 Ok(context)
59 }
60}
61
62impl RubyException for IncrementLinenoError {
63 fn message(&self) -> Cow<'_, [u8]> {
64 Cow::Borrowed(b"parser exceeded maximum line count")
65 }
66
67 fn name(&self) -> Cow<'_, str> {
68 "ScriptError".into()
69 }
70
71 fn vm_backtrace(&self, interp: &mut Artichoke) -> Option<Vec<Vec<u8>>> {
72 let _ = interp;
73 None
74 }
75
76 fn as_mrb_value(&self, interp: &mut Artichoke) -> Option<sys::mrb_value> {
77 let message = interp.try_convert_mut(self.message()).ok()?;
78 let value = interp.new_instance::<ScriptError>(&[message]).ok().flatten()?;
79 Some(value.inner())
80 }
81}
82
83impl From<IncrementLinenoError> for Error {
84 fn from(exception: IncrementLinenoError) -> Self {
85 let err: Box<dyn RubyException> = Box::new(exception);
86 Self::from(err)
87 }
88}