Generic parser for competitive programming

Related tags

Parsing testcase-rs
Overview

Generic parser for competitive programming

This is a generic parser for competitive programming, it can be used to read structured data line-by-line or though derive macro in a higher level fashion.

Functional API

You can take any BufRead input and parse each lines into a structured output. Output can be strings, numerics or any combination of tuple, sized array or Vec.

().unwrap(), (1, 2) ); ">
use testcase::prelude::*;

let input = "1 2\n3 4\n";

assert_eq!(
    input.as_bytes().parse_line::<(i32, i32)>().unwrap(),
    (1, 2)
);

Derive API

You can define complex layouts using the TestCase derive in combination of parse_testcase, each #[testcase(line)] attribute symbolizes a newline in the input.

, } let input = "2\n1 2\n3 4\n"; let testcase: Input = input.as_bytes() .parse_testcase() .unwrap(); assert_eq!( testcase.table, vec![ [1, 2], [3, 4] ], ); ">
use testcase::prelude::*;
use testcase_derive::TestCase;

#[derive(Debug, TestCase)]
struct Input {
    #[testcase(line)]
    n: usize,
    #[testcase(lines = "n")]
    table: Vec<[u32; 2]>,
}

let input = "2\n1 2\n3 4\n";

let testcase: Input = input.as_bytes()
    .parse_testcase()
    .unwrap();

assert_eq!(
    testcase.table,
    vec![
        [1, 2],
        [3, 4]
    ],
);

Domain-specific APIs

Facebook Hackercup

The #[hackercup] will generate your main function from the solution for a single case, it will automatically iterate over all cases and parse inputs using the TestCase trait usually implemented through its derive.

u64 { solve_problem(&input) } ">
use testcase_derive::{hackercup, TestCase};

#[derive(TestCase)]
struct Input {
    #[testcase(line)]
    _k: usize,
    #[testcase(line)]
    text: String,
}

fn solve_problem(input: &Input) -> u64 {
    todo!()
}

// Tests can automatically be generated if you specify sample input and output:
// #[hackercup(input = "test_sample.in", output = "test_sample.out")]
#[hackercup]
fn solve(input: Input) -> u64 {
    solve_problem(&input)
}
You might also like...
A typed parser generator embedded in Rust code for Parsing Expression Grammars

Oak Compiled on the nightly channel of Rust. Use rustup for managing compiler channels. You can download and set up the exact same version of the comp

Rust query string parser with nesting support

What is Queryst? This is a fork of the original, with serde and serde_json updated to 0.9 A query string parsing library for Rust inspired by https://

A fast, extensible, command-line arguments parser

parkour A fast, extensible, command-line arguments parser. Introduction 📚 The most popular argument parser, clap, allows you list all the possible ar

Soon to be AsciiDoc parser implemented in rust!

pagliascii "But ASCII Doc, I am Pagliascii" Soon to be AsciiDoc parser implemented in rust! This project is the current implementation of the requeste

An LR parser generator, implemented as a proc macro

parsegen parsegen is an LR parser generator, similar to happy, ocamlyacc, and lalrpop. It currently generates canonical LR(1) parsers, but LALR(1) and

A rusty, dual-wielding Quake and Half-Life texture WAD parser.

Ogre   A rusty, dual-wielding Quake and Half-Life texture WAD parser ogre is a rust representation and nom parser for Quake and Half-Life WAD files. I

A modern dialogue executor and tree parser using YAML.

A modern dialogue executor and tree parser using YAML. This crate is for building(ex), importing/exporting(ex), and walking(ex) dialogue trees. convo

A friendly parser combinator crate

Chumsky A friendly parser combinator crate that makes writing LL-1 parsers with error recovery easy. Example Here follows a Brainfuck parser. See exam

Minimalist pedantic command line parser

Lexopt Lexopt is an argument parser for Rust. It tries to have the simplest possible design that's still correct. It's so simple that it's a bit tedio

Owner
Rémi Dupré
Rémi Dupré
Website for Microformats Rust parser (using 'microformats-parser'/'mf2')

Website for Microformats Rust parser (using 'microformats-parser'/'mf2')

Microformats 5 Jul 19, 2022
A fault-tolerant, recursive-descent parser for Ara Programming Language 🌲

Ara Parser A fault-tolerant, recursive-descent parser for Ara Programming Language ?? Note: This project is a hard-fork of php-rust-tools/parser Speci

Ara Programming Language 18 Jan 23, 2023
A native Rust port of Google's robots.txt parser and matcher C++ library.

robotstxt A native Rust port of Google's robots.txt parser and matcher C++ library. Native Rust port, no third-part crate dependency Zero unsafe code

Folyd 72 Dec 11, 2022
Rust parser combinator framework

nom, eating data byte by byte nom is a parser combinators library written in Rust. Its goal is to provide tools to build safe parsers without compromi

Geoffroy Couprie 7.6k Jan 7, 2023
url parameter parser for rest filter inquiry

inquerest Inquerest can parse complex url query into a SQL abstract syntax tree. Example this url: /person?age=lt.42&(student=eq.true|gender=eq.'M')&

Jovansonlee Cesar 25 Nov 2, 2020
Parsing Expression Grammar (PEG) parser generator for Rust

Parsing Expression Grammars in Rust Documentation | Release Notes rust-peg is a simple yet flexible parser generator that makes it easy to write robus

Kevin Mehall 1.2k Dec 30, 2022
A fast monadic-style parser combinator designed to work on stable Rust.

Chomp Chomp is a fast monadic-style parser combinator library designed to work on stable Rust. It was written as the culmination of the experiments de

Martin Wernstål 228 Oct 31, 2022
A parser combinator library for Rust

combine An implementation of parser combinators for Rust, inspired by the Haskell library Parsec. As in Parsec the parsers are LL(1) by default but th

Markus Westerlind 1.1k Dec 28, 2022
LR(1) parser generator for Rust

LALRPOP LALRPOP is a Rust parser generator framework with usability as its primary goal. You should be able to write compact, DRY, readable grammars.

null 2.4k Jan 7, 2023
The Elegant Parser

pest. The Elegant Parser pest is a general purpose parser written in Rust with a focus on accessibility, correctness, and performance. It uses parsing

pest 3.5k Jan 8, 2023