Skip to content

Commit

Permalink
Add Apache Thrift grammar with examples
Browse files Browse the repository at this point in the history
Add Cap'n Proto grammar with examples
Add Google FlatBuffers grammar with examples
  • Loading branch information
gidl committed Mar 9, 2018
1 parent 6610e82 commit 90b24bd
Show file tree
Hide file tree
Showing 59 changed files with 6,245 additions and 0 deletions.
147 changes: 147 additions & 0 deletions capnproto/CapnProto.g4
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@

grammar CapnProto;

// parser rules

document :
file_identifier using_import* namespace? document_content* EOF ;

file_identifier :
FILE_ID ';' ;

using_import :
'using' ( NAME '=' )? 'import' TEXT ( '.' NAME )? ';' ;

namespace :
'$' NAME '.namespace' '(' TEXT ')' ';' ;

document_content :
struct_def | interface_def | function_def | annotation_def | const_def | enum_def ;

struct_def :
'struct' type annotation_reference? '{' struct_content* '}' ;

struct_content :
field_def | enum_def | named_union_def
| unnamed_union_def | interface_def | annotation_def
| struct_def | group_def | const_def | inner_using ;

interface_def :
'interface' type ( 'extends' '(' type ')' )? '{' interface_content* '}' ;

interface_content :
field_def | enum_def | named_union_def
| unnamed_union_def | interface_def
| struct_def | function_def ;

field_def :
NAME LOCATOR ':' type ( '=' const_value )? ';' ;

type :
NAME
inner_type?
( '.' type )? ;

inner_type :
'(' type inner_type? ( ',' type inner_type? )* ')' ;

enum_def :
'enum' NAME annotation_reference? '{' enum_content* '}' ;

annotation_reference :
'$' type '.ann'? '(' TEXT ')' ;

enum_content :
NAME LOCATOR annotation_reference? ';' ;

named_union_def :
NAME LOCATOR? ':union' '{' union_content* '}' ;

unnamed_union_def :
'union' '{' union_content* '}' ;

union_content :
field_def | group_def | unnamed_union_def | named_union_def ;

group_def :
NAME ':group' '{' group_content* '}' ;

group_content :
field_def | unnamed_union_def | named_union_def ;

function_def :
NAME LOCATOR? generic_type_parameters? ( function_parameters | type )
( '->' ( function_parameters | type ) )?
';' ;

generic_type_parameters :
'['
NAME ( ',' NAME )*
']' ;

function_parameters :
'('
( NAME ':' type ( '=' const_value )?
( ',' NAME ':' type ( '=' const_value )? )*
)?
')' ;

annotation_def :
'annotation' type annotation_parameters? ':' type ';' ;

annotation_parameters :
'(' 'struct' ')' ;

const_def :
'const' NAME ':' type '=' const_value ';' ;

const_value :
'-'? '.'? NAME ( '.' NAME )? | INTEGER | FLOAT
| TEXT | BOOLEAN | HEXADECIMAL | VOID
| literal_list | literal_union | literal_bytes ;

literal_union :
'(' NAME '=' union_mapping ( ',' NAME '=' union_mapping )* ')' ;

literal_list :
'[' const_value ( ',' const_value )* ']' ;

literal_bytes :
'0x' TEXT ;

union_mapping :
'(' NAME '=' const_value ')' | const_value ;

inner_using :
'using' NAME ( '.' NAME )*
( '=' type )?
';' ;


// lexer rules

fragment DIGIT : [0-9] ;

fragment HEX_DIGIT : DIGIT | 'A'..'F' | 'a'..'f' ;

LOCATOR : '@' DIGIT+ '!'? ;

TEXT : '"' ~["]*? '"' ;
INTEGER : '-'? DIGIT+ ;
FLOAT : '-'? DIGIT+ ( '.' DIGIT+ )? ( 'e' '-'? DIGIT+ )? ;
HEXADECIMAL : '-'? '0x' HEX_DIGIT+ ;
FILE_ID : '@' HEXADECIMAL ;
BOOLEAN : 'true' | 'false' ;
VOID : 'void' ;
NAME : [a-zA-Z] [a-zA-Z0-9]* ;
COMMENT : '#' ~[\n]* -> channel(HIDDEN) ;
WHITESPACE : [ \t\r\n] -> skip ;
1 change: 1 addition & 0 deletions capnproto/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ANTLR v4 grammar for the Cap'n Proto schema language: https://capnproto.org/language.html
55 changes: 55 additions & 0 deletions capnproto/examples/addressbook.capnp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
# Licensed under the MIT License:
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

@0x9eb32e19f86ee174;

using Cxx = import "/capnp/c++.capnp";
$Cxx.namespace("addressbook");

struct Person {
id @0 :UInt32;
name @1 :Text;
email @2 :Text;
phones @3 :List(PhoneNumber);

struct PhoneNumber {
number @0 :Text;
type @1 :Type;

enum Type {
mobile @0;
home @1;
work @2;
}
}

employment :union {
unemployed @4 :Void;
employer @5 :Text;
school @6 :Text;
selfEmployed @7 :Void;
# We assume that a person is only one of these.
}
}

struct AddressBook {
people @0 :List(Person);
}
118 changes: 118 additions & 0 deletions capnproto/examples/calculator.capnp
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
# Licensed under the MIT License:
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

@0x85150b117366d14b;

interface Calculator {
# A "simple" mathematical calculator, callable via RPC.
#
# But, to show off Cap'n Proto, we add some twists:
#
# - You can use the result from one call as the input to the next
# without a network round trip. To accomplish this, evaluate()
# returns a `Value` object wrapping the actual numeric value.
# This object may be used in a subsequent expression. With
# promise pipelining, the Value can actually be used before
# the evaluate() call that creates it returns!
#
# - You can define new functions, and then call them. This again
# shows off pipelining, but it also gives the client the
# opportunity to define a function on the client side and have
# the server call back to it.
#
# - The basic arithmetic operators are exposed as Functions, and
# you have to call getOperator() to obtain them from the server.
# This again demonstrates pipelining -- using getOperator() to
# get each operator and then using them in evaluate() still
# only takes one network round trip.

evaluate @0 (expression :Expression) -> (value :Value);
# Evaluate the given expression and return the result. The
# result is returned wrapped in a Value interface so that you
# may pass it back to the server in a pipelined request. To
# actually get the numeric value, you must call read() on the
# Value -- but again, this can be pipelined so that it incurs
# no additional latency.

struct Expression {
# A numeric expression.

union {
literal @0 :Float64;
# A literal numeric value.

previousResult @1 :Value;
# A value that was (or, will be) returned by a previous
# evaluate().

parameter @2 :UInt32;
# A parameter to the function (only valid in function bodies;
# see defFunction).

call :group {
# Call a function on a list of parameters.
function @3 :Function;
params @4 :List(Expression);
}
}
}

interface Value {
# Wraps a numeric value in an RPC object. This allows the value
# to be used in subsequent evaluate() requests without the client
# waiting for the evaluate() that returns the Value to finish.

read @0 () -> (value :Float64);
# Read back the raw numeric value.
}

defFunction @1 (paramCount :Int32, body :Expression)
-> (func :Function);
# Define a function that takes `paramCount` parameters and returns the
# evaluation of `body` after substituting these parameters.

interface Function {
# An algebraic function. Can be called directly, or can be used inside
# an Expression.
#
# A client can create a Function that runs on the server side using
# `defFunction()` or `getOperator()`. Alternatively, a client can
# implement a Function on the client side and the server will call back
# to it. However, a function defined on the client side will require a
# network round trip whenever the server needs to call it, whereas
# functions defined on the server and then passed back to it are called
# locally.

call @0 (params :List(Float64)) -> (value :Float64);
# Call the function on the given parameters.
}

getOperator @2 (op :Operator) -> (func :Function);
# Get a Function representing an arithmetic operator, which can then be
# used in Expressions.

enum Operator {
add @0;
subtract @1;
multiply @2;
divide @3;
}
}
Loading

0 comments on commit 90b24bd

Please sign in to comment.