Chilon C++0x Library

The Chilon Library is a C++0x utility library. The most advanced parts are a command-line argument parser and a parser generator library based on parsing expression grammars with built in AST generation abilities.

chilon::parser

chilon::parser is a C++0x template library that allows parsers based on PEG grammars to be created using templates. chilon::parser is built using the TBPEG variant of PEGs which allows an appropriate AST type to be built by the grammar in addition to the parser. Parsers built with chilon::parser are capable of populating the AST using the same grammar used to create the parsing algorithm. The TBPEG variant of PEG notation is discussed in this paper.

To get a feel for how expressive chilon::parser is, here is an example parser that stores a subset of javascript (variable declarations and infinitely nested function declarations) in a very neat AST containing a custom class in less than 25 lines of source code:

#include <chilon/parser.hpp>
using namespace chilon::parser;
using namespace chilon::parser::ascii;

typedef lexeme<
    choice< char_range<a,z, A,Z>, char_<'_'> >,
    many< choice< char_range<a,z, A,Z, '0', '9'>, char_<'_'> > > >
identifier;

typedef sequence<char_<v,a,r>, identifier> variable;

struct function : node<function> {
    typedef sequence<char_<f,u,n,c,t,i,o,n>, identifier>  name;
    typedef joined<char_<','>, identifier>                arguments;
    typedef many< choice< node<function>, variable > >    elements;

    stored<arguments>::type  arguments_;
    stored<elements>::type   elements_;
    range                    name_;

    typedef sequence<
        CHILON_NODE_MEMBER(name),
        char_<'('>, CHILON_NODE_MEMBER(arguments), char_<')'>,
        char_<'{'>, CHILON_NODE_MEMBER(elements), char_<'}'>
    > type;

    // This type will be stored in the AST so put your own methods here:
};

typedef many< choice< variable, function > >  simple_javascript_parser;

This parser is used in tutorial 6 to produce an AST from the following input file:

var _one

function command1(parameter, friend) {
    var v1

    function nested1(p1) {
        var n1v1
    }

    function nested2(p2) {
        function nested2nested1(p21) {}

        var n2v2

        function nested2nested2(p22a, p22b) {
            var n2n2v1
        }
    }
}

var _two_two_

In tutorial 6 the AST is pretty printed using chilon::print to produce the following output:

[
    "_one"
    function command1(
        arguments: [
            "parameter"
            "friend"
        ]
        elements: [
            "v1"
            function nested1(
                arguments: [ "p1" ]
                elements: [ "n1v1" ]
            )
            function nested2(
                arguments: [ "p2" ]
                elements: [
                    function nested2nested1(
                        arguments: [ "p21" ]
                        elements: []
                    )
                    "n2v2"
                    function nested2nested2(
                        arguments: [
                            "p22a"
                            "p22b"
                        ]
                        elements: [ "n2n2v1" ]
                    )
                ]
            )
        ]
    )
    "_two_two_"
]

Less than 20 extra lines of code are needed to hook the `function' class into chilon::print and create the file stream from argv to do all of this. You can find this code in the tutorial.

previous | next