grammar.capnp 7.08 KB
Newer Older
Kenton Varda's avatar
Kenton Varda committed
1 2
# Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
# Licensed under the MIT License:
Kenton Varda's avatar
Kenton Varda committed
3
#
Kenton Varda's avatar
Kenton Varda committed
4 5 6 7 8 9
# 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:
Kenton Varda's avatar
Kenton Varda committed
10
#
Kenton Varda's avatar
Kenton Varda committed
11 12
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
Kenton Varda's avatar
Kenton Varda committed
13
#
Kenton Varda's avatar
Kenton Varda committed
14 15 16 17 18 19 20
# 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.
Kenton Varda's avatar
Kenton Varda committed
21 22

@0xc56be168dcbbc3c6;
23 24 25
# The structures in this file correspond to the AST of the Cap'n Proto schema language.
#
# This file is intended to be used internally by capnpc.  Mostly, it is useful because it is more
26
# convenient than defining data classes in C++, particularly where variant types (unions) are
27
# needed.  Over time, this file may change in backwards-incompatible ways.
Kenton Varda's avatar
Kenton Varda committed
28 29 30 31 32

using Cxx = import "/capnp/c++.capnp";

$Cxx.namespace("capnp::compiler");

33 34
# TODO(someday):  Here's a case where parameterized types might be nice, but note that it would
#   need to support primitive parameters...
Kenton Varda's avatar
Kenton Varda committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
struct LocatedText {
  value @0 :Text;
  startByte @1 :UInt32;
  endByte @2 :UInt32;
}

struct LocatedInteger {
  value @0 :UInt64;
  startByte @1 :UInt32;
  endByte @2 :UInt32;
}

struct LocatedFloat {
  value @0 :Float64;
  startByte @1 :UInt32;
  endByte @2 :UInt32;
}

Kenton Varda's avatar
Kenton Varda committed
53 54 55
struct Expression {
  # An expression. May evaluate to a type, a value, or a declaration (i.e. some named thing which
  # is neither a type nor a value, like an annotation declaration).
Kenton Varda's avatar
Kenton Varda committed
56

57 58 59 60 61 62
  union {
    unknown @0 :Void;  # e.g. parse error; downstream should ignore
    positiveInt @1 :UInt64;
    negativeInt @2 :UInt64;
    float @3 :Float64;
    string @4 :Text;
Jason Choy's avatar
Jason Choy committed
63
    binary @10 :Data;
Kenton Varda's avatar
Kenton Varda committed
64 65 66 67 68 69 70 71 72 73

    relativeName @5 :LocatedText;
    # Just an identifier.

    absoluteName @15 :LocatedText;
    # An identifier with leading '.'.

    import @16 :LocatedText;
    # An import directive.

74 75 76
    embed @17 :LocatedText;
    # An embed directive.

Kenton Varda's avatar
Kenton Varda committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
    list @6 :List(Expression);
    # Bracketed list; members are never named.

    tuple @7 :List(Param);
    # Parenthesized list, possibly with named members.
    #
    # Note that a parenthesized list with one unnamed member is just a parenthesized expression,
    # not a tuple, and so will never be represented as a tuple.

    application :group {
      # Application of a function to some parameters, e.g. "foo(bar, baz)".

      function @11 :Expression;
      params @12 :List(Param);
    }

    member :group {
      # A named member of an aggregate, e.g. "foo.bar".

      parent @13 :Expression;
      name @14 :LocatedText;
    }

    # TODO(someday): Basic arithmetic?
Kenton Varda's avatar
Kenton Varda committed
101 102
  }

Kenton Varda's avatar
Kenton Varda committed
103 104 105 106 107 108
  struct Param {
    union {
      unnamed @0 :Void;          # Just a value.
      named @1 :LocatedText;     # "name = value"
    }
    value @2 :Expression;
Kenton Varda's avatar
Kenton Varda committed
109 110
  }

111 112
  startByte @8 :UInt32;
  endByte @9 :UInt32;
Kenton Varda's avatar
Kenton Varda committed
113 114 115 116 117 118 119
}

struct Declaration {
  # A declaration statement.

  name @0 :LocatedText;

120 121 122 123
  id :union {
    unspecified @1 :Void;
    uid @2 :LocatedInteger;
    ordinal @3 :LocatedInteger;  # limited to 16 bits
Kenton Varda's avatar
Kenton Varda committed
124 125
  }

126
  parameters @57 :List(BrandParameter);
Kenton Varda's avatar
Kenton Varda committed
127 128
  # If this node is parameterized (generic), the list of parameters. Empty for non-generic types.

129
  struct BrandParameter {
Kenton Varda's avatar
Kenton Varda committed
130 131 132 133 134
    name @0 :Text;
    startByte @1 :UInt32;
    endByte @2 :UInt32;
  }

135
  nestedDecls @4 :List(Declaration);
Kenton Varda's avatar
Kenton Varda committed
136 137 138

  annotations @5 :List(AnnotationApplication);
  struct AnnotationApplication {
Kenton Varda's avatar
Kenton Varda committed
139
    name @0 :Expression;
140

141 142
    value :union {
      none @1 :Void;   # None specified; implies void value.
Kenton Varda's avatar
Kenton Varda committed
143
      expression @2 :Expression;
144
    }
Kenton Varda's avatar
Kenton Varda committed
145 146
  }

147 148
  startByte @6 :UInt32;
  endByte @7 :UInt32;
Kenton Varda's avatar
Kenton Varda committed
149

150
  docComment @8 :Text;
Kenton Varda's avatar
Kenton Varda committed
151

152 153
  union {
    file @9 :Void;
Kenton Varda's avatar
Kenton Varda committed
154

155
    using :group {
Kenton Varda's avatar
Kenton Varda committed
156
      target @10 :Expression;
Kenton Varda's avatar
Kenton Varda committed
157 158
    }

159
    const :group {
Kenton Varda's avatar
Kenton Varda committed
160 161
      type @11 :Expression;
      value @12 :Expression;
162
    }
163

164 165
    enum @13 :Void;
    enumerant @14 :Void;
Kenton Varda's avatar
Kenton Varda committed
166

167 168
    struct @15 :Void;
    field :group {
Kenton Varda's avatar
Kenton Varda committed
169
      type @16 :Expression;
170 171
      defaultValue :union {
        none @17 :Void;
Kenton Varda's avatar
Kenton Varda committed
172
        value @18 :Expression;
173 174 175 176 177
      }
    }
    union @19 :Void;
    group @20 :Void;

178
    interface :group {
179
      superclasses @21 :List(Expression);
180
    }
181
    method :group {
182 183
      params @22 :ParamList;
      results :union {
184
        none @23 :Void;
185
        explicit @24 :ParamList;
Kenton Varda's avatar
Kenton Varda committed
186 187
      }
    }
188

189
    annotation :group {
Kenton Varda's avatar
Kenton Varda committed
190
      type @25 :Expression;
191 192 193 194 195 196 197 198 199 200 201 202 203

      targetsFile @26 :Bool;
      targetsConst @27 :Bool;
      targetsEnum @28 :Bool;
      targetsEnumerant @29 :Bool;
      targetsStruct @30 :Bool;
      targetsField @31 :Bool;
      targetsUnion @32 :Bool;
      targetsGroup @33 :Bool;
      targetsInterface @34 :Bool;
      targetsMethod @35 :Bool;
      targetsParam @36 :Bool;
      targetsAnnotation @37 :Bool;
204
    }
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228

    nakedId @38 :LocatedInteger;
    nakedAnnotation @39 :AnnotationApplication;
    # A floating UID or annotation (allowed at the file top level).

    # The following declaration types are not produced by the parser, but are declared here
    # so that the compiler can handle symbol name lookups more uniformly.
    #
    # New union members added here will magically become visible in the global scope.
    # E.g. "builtinFoo" becomes visible as "Foo".
    builtinVoid @40 :Void;
    builtinBool @41 :Void;
    builtinInt8 @42 :Void;
    builtinInt16 @43 :Void;
    builtinInt32 @44 :Void;
    builtinInt64 @45 :Void;
    builtinUInt8 @46 :Void;
    builtinUInt16 @47 :Void;
    builtinUInt32 @48 :Void;
    builtinUInt64 @49 :Void;
    builtinFloat32 @50 :Void;
    builtinFloat64 @51 :Void;
    builtinText @52 :Void;
    builtinData @53 :Void;
Kenton Varda's avatar
Kenton Varda committed
229
    builtinList @54 :Void $builtinParams([(name = "Element")]);
230 231
    builtinObject @55 :Void;  # only for "renamed to AnyPointer" error message
    builtinAnyPointer @56 :Void;
232 233 234
    builtinAnyStruct @58 :Void;
    builtinAnyList @59 :Void;
    builtinCapability @60 :Void;
Kenton Varda's avatar
Kenton Varda committed
235 236
  }

237
  annotation builtinParams @0x94099c3f9eb32d6b (field) :List(BrandParameter);
Kenton Varda's avatar
Kenton Varda committed
238

239 240 241 242 243 244
  struct ParamList {
    # A list of method parameters or method returns.

    union {
      namedList @0 :List(Param);

Kenton Varda's avatar
Kenton Varda committed
245
      type @1 :Expression;
246 247 248 249 250 251
      # Specified some other struct type instead of a named list.
    }

    startByte @2 :UInt32;
    endByte @3 :UInt32;
  }
252 253
  struct Param {
    name @0 :LocatedText;  # If null, param failed to parse.
Kenton Varda's avatar
Kenton Varda committed
254
    type @1 :Expression;
255
    annotations @2 :List(AnnotationApplication);
256 257
    defaultValue :union {
      none @3 :Void;
Kenton Varda's avatar
Kenton Varda committed
258
      value @4 :Expression;
259
    }
260 261 262

    startByte @5 :UInt32;
    endByte @6 :UInt32;
Kenton Varda's avatar
Kenton Varda committed
263 264 265 266
  }
}

struct ParsedFile {
267
  root @0 :Declaration;
Kenton Varda's avatar
Kenton Varda committed
268
}