language-thrift-0.3.0.0: Parser for the Thrift IDL format.

Copyright(c) Abhinav Gupta 2015
LicenseBSD3
MaintainerAbhinav Gupta <mail@abhinavg.net>
Stabilityexperimental
Safe HaskellNone
LanguageHaskell2010

Language.Thrift.Parser

Contents

Description

Provides a parser for Thrift IDLs.

In addition to parsing the IDLs, the parser also keeps track of Javadoc-style docstrings on defined items and makes their values available. For example,

/**
 * Fetches an item.
 */
Item getItem()

Note that the parser does not validate the Thrift file for correctness, so, for example, you could define a string value for an int constant.

Synopsis

Documentation

thriftIDL :: (MonadPlus p, TokenParsing p) => p n -> p (Program n) Source

Get a top level parser that is able to parse full Thrift documents.

Entities defined in the IDL are annotated with n values (determined by executing p n before the parser for the entity is executed).

Usage with Trifecta to get entities tagged with location information (see also, thriftIDL):

Trifecta.parseFromFile (thriftIDL Trifecta.position) "service.thrift"

Usage with Attoparsec without any annotations:

Attoparsec.parse (thriftIDL (return ())) document

Parser type

data ThriftParser p n a Source

The ThriftParser wraps another parser p with some extra state. It also allows injecting a configurable action (p n) which produces annotations that will be attached to entities in the Thrift file. See thriftIDLParser for an example.

runThriftParser Source

Arguments

:: (MonadPlus p, TokenParsing p) 
=> p n

How to get annotations from the underlying parser. If this is not something you need to use, make it return () and generated types will be annotated with ().

-> ThriftParser p n a 
-> p a 

Get an exeecutable parser from the given ThriftParser.

Parser components

program :: (TokenParsing p, MonadPlus p) => ThriftParser p n (Program n) Source

Top-level parser to parse complete Thrift documents.

header :: (TokenParsing p, MonadPlus p) => ThriftParser p n Header Source

Headers defined for the IDL.

definition :: (TokenParsing p, MonadPlus p) => ThriftParser p n (Definition n) Source

A constant, type, or service definition.

typeDefinition :: (TokenParsing p, MonadPlus p) => ThriftParser p n (Definition n) Source

A type definition.

typedef :: (TokenParsing p, MonadPlus p) => ThriftParser p n (Type n) Source

A typedef is just an alias for another type.

typedef common.Foo Bar

enum :: (TokenParsing p, MonadPlus p) => ThriftParser p n (Type n) Source

Enums are sets of named integer values.

enum Role {
    User = 1, Admin
}

enumDef :: (TokenParsing p, MonadPlus p) => ThriftParser p n (EnumDef n) Source

A value defined inside an enum.

senum :: (TokenParsing p, MonadPlus p) => ThriftParser p n (Type n) Source

An string-only enum. These are a deprecated feature of Thrift and shouldn't be used.

struct :: (TokenParsing p, MonadPlus p) => ThriftParser p n (Type n) Source

A struct.

struct User {
    1: string name
    2: Role role = Role.User;
}

union :: (TokenParsing p, MonadPlus p) => ThriftParser p n (Type n) Source

A union of types.

union Value {
    1: string stringValue;
    2: i32 intValue;
}

exception :: (TokenParsing p, MonadPlus p) => ThriftParser p n (Type n) Source

An exception that can be raised by service methods.

exception UserDoesNotExist {
    1: optional string message
    2: required string username
}

fieldRequiredness :: (TokenParsing p, MonadPlus p) => ThriftParser p n FieldRequiredness Source

Whether a field is required or optional.

fieldType :: (TokenParsing p, MonadPlus p) => ThriftParser p n FieldType Source

A reference to a built-in or defined field.

field :: (TokenParsing p, MonadPlus p) => ThriftParser p n (Field n) Source

A struct field.

constant :: (TokenParsing p, MonadPlus p) => ThriftParser p n (Definition n) Source

A const definition.

const i32 code = 1;

constantValue :: (TokenParsing p, MonadPlus p) => ThriftParser p n ConstValue Source

A constant value literal.

service :: (TokenParsing p, MonadPlus p) => ThriftParser p n (Definition n) Source

A service.

service MyService {
    // ...
}

function :: (TokenParsing p, MonadPlus p) => ThriftParser p n (Function n) Source

A function defined inside a service.

Foo getFoo() throws (1: FooDoesNotExist doesNotExist);
oneway void putBar(1: Bar bar);

typeAnnotations :: (TokenParsing p, MonadPlus p) => ThriftParser p n [TypeAnnotation] Source

Type annotations on entitites.

("foo" = "bar", "baz" = "qux")

These do not usually affect code generation but allow for custom logic if writing your own code generator.