Almad / drafter

Snow Crash parser harness

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

logo

Drafter Circle CI Build status

Snowcrash parser harness

API Blueprint Parser

Drafter is complex builder of API Blueprint. Internally it uses Snowcrash library, reference API Blueprint parser.

API Blueprint is Web API documentation language. You can find API Blueprint documentation on the API Blueprint site.

Additionally Drafter provide set of Wrappers for serialization, of parsing result, via SOS library into JSON and YAML format.

Drafter also provides the user ability to select the type of the output. There are two possible values:

By default, Drafter assumes the Refract AST.

Both the types of AST are available in two different serialization formats, YAML and JSON. YAML is the default for the CLI.

Status

Install

OS X using Homebrew:

$ brew install --HEAD \
  https://raw.github.com/apiaryio/drafter/master/tools/homebrew/drafter.rb

AUR package for Arch Linux.

Other systems refer to build notes.

Use

C++ library

#include "drafter.h"      // Blueprint Parser
#include "SerializeAST.h" // Blueprint Result Wrapper for serialization
#include "sosJSON.h"      // Serializer

mdp::ByteBuffer blueprint = R"(
# My API
## GET /message
+ Response 200 (text/plain)

        Hello World!
)";

// Blueprint parsing
snowcrash::ParseResult<snowcrash::Blueprint> ast;
drafter::ParseBlueprint(blueprint, 0, ast);

std::cout << "API Name: " << ast.node.name << std::endl;

// Serialization to JSON format
sos::SerializeJSON serializer;
serializer.process(drafter::WrapBlueprint(ast.node, drafter::RefractASTType), std::cout);

C-interface

For purpose of bindings to other languages Drafter provides very simple C-interface.

#include "cdrafter.h"

const char* source = "# My API\n## GET /message\n + Response 200 (text/plain)\n\n        Hello World\n";
char *result = NULL;
int ret = drafter_c_parse(source, 0, DRAFTER_REFRACT_AST_TYPE, &result);

printf("Result: %s\n", ret == 0 ? "OK" : "ERROR");
printf("Serialized JSON result:\n%s\n", result);

free(result); /* we MUST release allocted memory for result */

Refer to Blueprint.h for the details about the Snow Crash AST and BlueprintSourcemap.h for details about Source Maps tree. See Drafter bindings for using the library in other languages.

Command line tool

$ cat << 'EOF' > blueprint.apib
# My API
## GET /message
+ Response 200 (text/plain)

        Hello World!
EOF

$ drafter blueprint.apib 
element: "category"
meta:
  classes:
    - "api"
  title: "My API"
 ...

See parse feature for the details on using the drafter command line tool.

Build

  1. Clone the repo + fetch the submodules:

    $ git clone --recursive git://github.com/apiaryio/drafter.git
    $ cd drafter
  2. Build & test Drafter:

    $ ./configure
    $ make test

    To include integration tests (using Cucumber) use the --include-integration-tests flag:

    $ ./configure --include-integration-tests
    $ make test

We love Windows too! Please refer to Building on Windows.

Drafter command line tool

  1. Build drafter:

    $ make drafter
  2. Install & use drafter:

    $ sudo make install
    $ drafter --help

Bindings

Drafter bindings in other languages:

CLI Wrapper

Contribute

Fork & Pull Request

If you want to create a binding for Drafter please refer to the Writing a Binding article.

License

MIT License. See the LICENSE file.

About

Snow Crash parser harness

License:MIT License


Languages

Language:C++ 91.1%Language:Batchfile 2.6%Language:Gherkin 1.9%Language:Python 1.8%Language:API Blueprint 0.8%Language:C 0.7%Language:Ruby 0.6%Language:Makefile 0.5%