Boost.Parser

repository·develop·Indexed 41 days ago

https://github.com/boostorg/parser

A header-only C++ parser combinator library introduced in Boost 1.87.0. It allows developers to build complex parsers from simple ones using operator overloading, featuring high performance, Unicode friendliness, and diagnostics similar to GCC and Clang.

Tokens
687
Snippets
1
Records
3
Agent score
2%

What's inside Boost.Parser

  1. Overview of boost::parser features

    develop

    The boost::parser library provides several key capabilities for building robust parsers:

    • Parser Combinators: Build complex parsers by combining simpler ones using operator overloading.
    • Data Extraction: Multiple methods for retrieving parsed data, including support for callbacks.
    • Unicode Support: Designed to be highly Unicode-friendly.
    • Error Reporting: Provides high-quality diagnostics similar to those produced by GCC and Clang.
    • Debugging: Includes trace support to help debug parser logic.
    • Compatibility: Sentinel- and range-friendly, allowing it to work with various input types.
    • Developer Experience: Includes optional features to make compile-time errors easier to interpret.
  2. Install and use boost::parser

    develop
    The boost::parser library is a header-only C++ parser combinator library. It has no Boost dependencies by default, making it easy to integrate into existing projects. It first appeared in Boost 1.87.0.
  3. Quickstart: Parse a list of doubles using boost::parser

    develop

    You can use boost::parser::parse to execute a parser against an input string. The library uses operator overloading (like >> for sequencing and * for repetition) to build complex parsers from simple ones.

    In the example below, the parser bp::double_ >> *(',' >> bp::double_) looks for an initial double, followed by zero or more sequences of a comma and another double. The bp::ws argument is used to handle whitespace.

    If the parse is successful, the result can be treated as a container of the parsed values.

    #include <boost/parser/parser.hpp>
    #include <iostream>
    #include <string>
    
    namespace bp = boost::parser;
    
    int main()
    {
        std::cout << "Enter a list of doubles, separated by commas.  No pressure. ";
        std::string input;
        std::getline(std::cin, input);
    
        auto const result = bp::parse(
            input, bp::double_ >> *(',' >> bp::double_), bp::ws);
    
        if (result) {
            std::cout << "Great! It looks like you entered:\n";
            for (double x : *result) {
                std::cout << x << "\n";
            }
        } else {
            std::cout
                << "Good job!  Please proceed to the recovery annex for cake.\n";
        }
    }