Skip to content

Instantly share code, notes, and snippets.

@m4rw3r m4rw3r/expanded_parser.rs
Last active Aug 29, 2015

Embed
What would you like to do?
Macros expanded in the parser.
Compiling my_own v0.1.0 (file:///Users/m4rw3r/Desktop/attoparsec/examples/my_own)
#![feature(no_std)]
#![no_std]
#[prelude_import]
use std::prelude::v1::*;
#[macro_use]
extern crate std as std;
#[macro_use]
extern crate parser;
use parser::*;
use std::fs::File;
use std::env;
struct Request<'a> {
method: &'a [u8],
uri: &'a [u8],
version: &'a [u8],
}
#[automatically_derived]
impl <'a> ::std::fmt::Debug for Request<'a> {
fn fmt(&self, __arg_0: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
match *self {
Request {
method: ref __self_0_0,
uri: ref __self_0_1,
version: ref __self_0_2 } =>
__arg_0.debug_struct("Request").field("method",
&&(*__self_0_0)).field("uri",
&&(*__self_0_1)).field("version",
&&(*__self_0_2)).finish(),
}
}
}
struct Header<'a> {
name: &'a [u8],
value: Vec<&'a [u8]>,
}
#[automatically_derived]
impl <'a> ::std::fmt::Debug for Header<'a> {
fn fmt(&self, __arg_0: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
match *self {
Header { name: ref __self_0_0, value: ref __self_0_1 } =>
__arg_0.debug_struct("Header").field("name",
&&(*__self_0_0)).field("value",
&&(*__self_0_1)).finish(),
}
}
}
fn is_token(c: u8) -> bool {
c < 128 && c > 31 &&
b"()<>@,;:\\\"/[]?={} \t".iter().position(|&i| i == c).is_none()
}
fn is_horizontal_space(c: u8) -> bool { c == b' ' || c == b'\t' }
fn is_space(c: u8) -> bool { c == b' ' }
fn is_not_space(c: u8) -> bool { c != b' ' }
fn is_end_of_line(c: u8) -> bool { c == b'\r' || c == b'\n' }
fn is_http_version_prefix(c: u8) -> bool {
b"HTP/".iter().position(|&i| i == c).is_some()
}
fn is_http_version(c: u8) -> bool { c >= b'0' && c <= b'9' || c == b'.' }
fn end_of_line<'a>(p: Empty<'a, u8>) -> Parser<'a, u8, u8, Error<u8>> {
or(
bind(char(p, b'\r'),
|p, _|
bind(char(p, b'\n'),
|p, _| ret::<_, u8, Error<u8>>(p, b'\r'))),
|p| char(p, b'\n'))
}
fn http_version<'a>(p: Empty<'a, u8>) -> Parser<'a, u8, &'a [u8], Error<u8>> {
bind(take_while1(p, is_http_version_prefix),
|p, _|
bind(take_while1(p, is_http_version),
|p, version| ret(p, version)))
}
fn request_line<'a>(p: Empty<'a, u8>)
-> Parser<'a, u8, Request<'a>, Error<u8>> {
bind(take_while1(p, is_token),
|p, method|
bind(take_while1(p, is_space),
|p, _|
bind(take_while1(p, is_not_space),
|p, uri|
bind(take_while1(p, is_space),
|p, _|
bind(http_version(p),
|p, version|
ret(p,
Request{method: method,
uri: uri,
version:
version,}))))))
}
fn message_header_line<'a>(p: Empty<'a, u8>)
-> Parser<'a, u8, &'a [u8], Error<u8>> {
bind(take_while1(p, is_horizontal_space),
|p, _|
bind(take_till(p, is_end_of_line),
|p, line| bind(end_of_line(p), |p, _| ret(p, line))))
}
fn message_header<'a>(p: Empty<'a, u8>) -> Parser<'a, u8, Header, Error<u8>> {
bind(take_while1(p, is_token),
|p, name|
bind(char(p, b':'),
|p, _|
bind(many1(p, message_header_line),
|p, lines|
ret(p, Header{name: name, value: lines,}))))
}
fn request<'a>(p: Empty<'a, u8>)
-> Parser<'a, u8, (Request, Vec<Header>), Error<u8>> {
bind(request_line(p),
|p, r|
bind(end_of_line(p),
|p, _|
bind(many(p, message_header),
|p, h|
bind(end_of_line(p), |p, _| ret(p, (r, h))))))
}
fn main() {
let mut contents: Vec<u8> = Vec::new();
{
use std::io::Read;
let mut file =
File::open(env::args().nth(1).expect("File to read")).ok().expect("Failed to open file");
let _ = file.read_to_end(&mut contents).unwrap();
}
let i = parser::Iter::new(&contents, request);
::std::io::_print(::std::fmt::Arguments::new_v1({
static __STATIC_FMTSTR:
&'static [&'static str]
=
&["num: ", "\n"];
__STATIC_FMTSTR
},
&match (&i.count(),) {
(__arg0,) =>
[::std::fmt::ArgumentV1::new(__arg0,
::std::fmt::Display::fmt)],
}));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.