Skip to content

Instantly share code, notes, and snippets.

trait Source<'a> {
fn get(&mut self) -> Option<u8>;
fn mark(&self) -> &'a [u8];
fn restore(&mut self, &'a [u8]);
}
struct Slice<'a>(&'a [u8]);
impl<'a> Source<'a> for Slice<'a> {
fn get(&mut self) -> Option<u8> {
mod parser {
use std::fmt::Debug;
#[derive(Debug)]
pub enum Error<T> {
DidNotSatisfy(T)
}
#[derive(Debug)]
enum State<T, E> {
@m4rw3r
m4rw3r / parser_attoparsec_example.rs
Last active August 29, 2015 14:27
Version of the attoparsec benchmark for parsing HTTP header dumps writtien in rust using my experimental parser combinator.
extern crate parser;
use parser::{Parser, Error};
use std::fs::File;
use std::env;
#[derive(Debug)]
struct Request<'a> {
method: &'a [u8],
@m4rw3r
m4rw3r / parser_attoparsec_example_nom.rs
Last active August 29, 2015 14:27
Version of the attoparsec benchmark for parsing HTTP header dumps writtien in rust for the nom parser library.
#[macro_use]
extern crate nom;
use nom::IResult;
use std::env;
use std::fs::File;
#[derive(Debug)]
struct Request<'a> {
method: &'a [u8],
@m4rw3r
m4rw3r / parser_attoparsec_example2.rs
Last active August 29, 2015 14:27
Second version of the attoparsec benchmark for parsing HTTP header dumps writtien in rust using my experimental parser combinator.
//! http parser comparable to the http-parser found in attoparsec's examples.
//!
//! Reads data in the following format:
//!
//! ```text
//! GET /robot.txt HTTP/1.1
//! Host: localhost
//! Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
//!
//! ```
@m4rw3r
m4rw3r / expanded_parser.rs
Last active August 29, 2015 14:27
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;
@m4rw3r
m4rw3r / http_parser.rs.diff
Created August 18, 2015 15:43
Difference between manually threading state and boxed closures in a piece of code using a parser-combinator.
diff --git a/examples/http_parser.rs b/examples/http_parser.rs
index 5c31b35..196763e 100644
--- a/examples/http_parser.rs
+++ b/examples/http_parser.rs
@@ -41,17 +41,17 @@ 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(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(mdo!{p,
@m4rw3r
m4rw3r / combine_http_parser.rs
Created August 20, 2015 15:19
Version of the attoparsec example using the parser combinator Combine.
extern crate combine;
use combine::*;
use std::fs::File;
use std::env;
// Seems like combine does not support any kind of zero-copy parsing
#[derive(Debug, Clone)]
struct Request {
@m4rw3r
m4rw3r / combine_http_parser-pre.rs
Last active August 29, 2015 14:27
Version of the attoparsec http-header example for pre-release version of Combine.
extern crate combine;
use combine::*;
use combine::combinator::{take_while1, range};
use combine::primitives::Error;
use std::fs::File;
use std::env;

Requirements

  • Easy chaining of monadic actions.

    Ie. separating two monadic actions should chain them together with bind(a, |_| b).

  • Simple bind of a value to an ident.

    This should probably also allow any monadic action and not just calls to functions, eg. something like: