Struct Parser

Source
pub struct Parser<'a> { /* private fields */ }
Expand description

Parser that decodes a SISE file into a sequence of ParsedItem.

§Example

let data = "(test (1 2 3))";
let mut parser = sise::Parser::new(data);
assert_eq!(parser.next_item().unwrap(), sise::ParsedItem::ListStart(0));
assert_eq!(
    parser.next_item().unwrap(),
    sise::ParsedItem::Atom("test", 1),
);
assert_eq!(parser.next_item().unwrap(), sise::ParsedItem::ListStart(6));
assert_eq!(parser.next_item().unwrap(), sise::ParsedItem::Atom("1", 7));
assert_eq!(parser.next_item().unwrap(), sise::ParsedItem::Atom("2", 9));
assert_eq!(parser.next_item().unwrap(), sise::ParsedItem::Atom("3", 11));
assert_eq!(parser.next_item().unwrap(), sise::ParsedItem::ListEnd(12));
assert_eq!(parser.next_item().unwrap(), sise::ParsedItem::ListEnd(13));
parser.finish().unwrap();

Implementations§

Source§

impl<'a> Parser<'a>

Source

pub fn new(data: &'a str) -> Self

Examples found in repository?
examples/parse_sise.rs (line 20)
11fn main() {
12    let args: Vec<_> = std::env::args_os().collect();
13    if args.len() != 2 {
14        eprintln!("Usage: {} [input-file]", args[0].to_string_lossy());
15        std::process::exit(1);
16    }
17
18    let file_data = std::fs::read(&args[1]).unwrap();
19    let file_data = String::from_utf8(file_data).unwrap();
20    let mut parser = sise::Parser::new(&file_data);
21    let parsed = sise::parse_tree(&mut parser).unwrap();
22    parser.finish().unwrap();
23
24    println!("{:#?}", parsed);
25}
More examples
Hide additional examples
examples/reserialize_sise.rs (line 37)
11fn main() {
12    let args: Vec<_> = std::env::args_os().collect();
13    if args.len() != 3 {
14        eprintln!(
15            "Usage: {} [compact|spaced] [input-file]",
16            args[0].to_string_lossy()
17        );
18        std::process::exit(1);
19    }
20
21    enum SerializeStyle {
22        Compact,
23        Spaced,
24    }
25
26    let serialize_style = match &*args[1].to_string_lossy() {
27        "compact" => SerializeStyle::Compact,
28        "spaced" => SerializeStyle::Spaced,
29        _ => {
30            eprintln!("Unknown style {:?}.", args[1]);
31            std::process::exit(1);
32        }
33    };
34
35    let file_data = std::fs::read(&args[2]).unwrap();
36    let file_data = String::from_utf8(file_data).unwrap();
37    let mut parser = sise::Parser::new(&file_data);
38    let parsed = sise::parse_tree(&mut parser).unwrap();
39    parser.finish().unwrap();
40
41    let break_line_at = match serialize_style {
42        SerializeStyle::Compact => usize::MAX,
43        SerializeStyle::Spaced => 0,
44    };
45    let mut reserialized = String::new();
46    let mut serializer = sise::Serializer::new(
47        sise::SerializerStyle {
48            line_break: "\n",
49            indentation: "  ",
50        },
51        &mut reserialized,
52    );
53    sise::serialize_tree(&mut serializer, &parsed, break_line_at);
54    serializer.finish(false);
55    println!("{}", reserialized);
56}
Source

pub fn next_item(&mut self) -> Result<ParsedItem<'a>, ParseError>

Source

pub fn finish(self) -> Result<(), ParseError>

Examples found in repository?
examples/parse_sise.rs (line 22)
11fn main() {
12    let args: Vec<_> = std::env::args_os().collect();
13    if args.len() != 2 {
14        eprintln!("Usage: {} [input-file]", args[0].to_string_lossy());
15        std::process::exit(1);
16    }
17
18    let file_data = std::fs::read(&args[1]).unwrap();
19    let file_data = String::from_utf8(file_data).unwrap();
20    let mut parser = sise::Parser::new(&file_data);
21    let parsed = sise::parse_tree(&mut parser).unwrap();
22    parser.finish().unwrap();
23
24    println!("{:#?}", parsed);
25}
More examples
Hide additional examples
examples/reserialize_sise.rs (line 39)
11fn main() {
12    let args: Vec<_> = std::env::args_os().collect();
13    if args.len() != 3 {
14        eprintln!(
15            "Usage: {} [compact|spaced] [input-file]",
16            args[0].to_string_lossy()
17        );
18        std::process::exit(1);
19    }
20
21    enum SerializeStyle {
22        Compact,
23        Spaced,
24    }
25
26    let serialize_style = match &*args[1].to_string_lossy() {
27        "compact" => SerializeStyle::Compact,
28        "spaced" => SerializeStyle::Spaced,
29        _ => {
30            eprintln!("Unknown style {:?}.", args[1]);
31            std::process::exit(1);
32        }
33    };
34
35    let file_data = std::fs::read(&args[2]).unwrap();
36    let file_data = String::from_utf8(file_data).unwrap();
37    let mut parser = sise::Parser::new(&file_data);
38    let parsed = sise::parse_tree(&mut parser).unwrap();
39    parser.finish().unwrap();
40
41    let break_line_at = match serialize_style {
42        SerializeStyle::Compact => usize::MAX,
43        SerializeStyle::Spaced => 0,
44    };
45    let mut reserialized = String::new();
46    let mut serializer = sise::Serializer::new(
47        sise::SerializerStyle {
48            line_break: "\n",
49            indentation: "  ",
50        },
51        &mut reserialized,
52    );
53    sise::serialize_tree(&mut serializer, &parsed, break_line_at);
54    serializer.finish(false);
55    println!("{}", reserialized);
56}

Auto Trait Implementations§

§

impl<'a> Freeze for Parser<'a>

§

impl<'a> RefUnwindSafe for Parser<'a>

§

impl<'a> Send for Parser<'a>

§

impl<'a> Sync for Parser<'a>

§

impl<'a> Unpin for Parser<'a>

§

impl<'a> UnwindSafe for Parser<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.