Commit 7b8bbc87 authored by Kenton Varda's avatar Kenton Varda

Refactor grammar.capnp to use unnamed unions.

parent 4288dd27
......@@ -67,11 +67,11 @@ public:
Node(const Node& parent, const Declaration::Reader& declaration);
// Create a child node.
Node(kj::StringPtr name, Declaration::Body::Which kind);
Node(kj::StringPtr name, Declaration::Which kind);
// Create a dummy node representing a built-in declaration, like "Int32" or "true".
uint64_t getId() const { return id; }
Declaration::Body::Which getKind() const { return kind; }
Declaration::Which getKind() const { return kind; }
kj::Maybe<const Node&> lookupMember(kj::StringPtr name) const;
// Find a direct member of this node with the given name.
......@@ -115,7 +115,7 @@ private:
// Fully-qualified display name for this node. For files, this is just the file name, otherwise
// it is "filename:Path.To.Decl".
Declaration::Body::Which kind;
Declaration::Which kind;
// Kind of node.
bool isBuiltin;
......@@ -337,7 +337,7 @@ Compiler::Node::Node(CompiledModule& module)
declaration(module.getParsedFile().getRoot()),
id(generateId(0, declaration.getName().getValue(), declaration.getId())),
displayName(module.getSourceName()),
kind(declaration.getBody().which()),
kind(declaration.which()),
isBuiltin(false) {
auto name = declaration.getName();
if (name.getValue().size() > 0) {
......@@ -358,7 +358,7 @@ Compiler::Node::Node(const Node& parent, const Declaration::Reader& declaration)
id(generateId(parent.id, declaration.getName().getValue(), declaration.getId())),
displayName(joinDisplayName(parent.module->getCompiler().getNodeArena(),
parent, declaration.getName().getValue())),
kind(declaration.getBody().which()),
kind(declaration.which()),
isBuiltin(false) {
auto name = declaration.getName();
if (name.getValue().size() > 0) {
......@@ -372,7 +372,7 @@ Compiler::Node::Node(const Node& parent, const Declaration::Reader& declaration)
id = module->getCompiler().addNode(id, *this);
}
Compiler::Node::Node(kj::StringPtr name, Declaration::Body::Which kind)
Compiler::Node::Node(kj::StringPtr name, Declaration::Which kind)
: module(nullptr),
parent(nullptr),
id(0),
......@@ -421,13 +421,13 @@ const Compiler::Node::Content& Compiler::Node::getContent(Content::State minimum
auto& arena = module->getCompiler().getNodeArena();
for (auto nestedDecl: declaration.getNestedDecls()) {
switch (nestedDecl.getBody().which()) {
case Declaration::Body::FILE_DECL:
case Declaration::Body::CONST_DECL:
case Declaration::Body::ANNOTATION_DECL:
case Declaration::Body::ENUM_DECL:
case Declaration::Body::STRUCT_DECL:
case Declaration::Body::INTERFACE_DECL: {
switch (nestedDecl.which()) {
case Declaration::FILE:
case Declaration::CONST:
case Declaration::ANNOTATION:
case Declaration::ENUM:
case Declaration::STRUCT:
case Declaration::INTERFACE: {
kj::Own<Node> subNode = arena.allocateOwn<Node>(*this, nestedDecl);
kj::StringPtr name = nestedDecl.getName().getValue();
locked->orderedNestedNodes.add(subNode);
......@@ -435,20 +435,20 @@ const Compiler::Node::Content& Compiler::Node::getContent(Content::State minimum
break;
}
case Declaration::Body::USING_DECL: {
case Declaration::USING: {
kj::Own<Alias> alias = arena.allocateOwn<Alias>(
*this, nestedDecl.getBody().getUsingDecl().getTarget());
*this, nestedDecl.getUsing().getTarget());
kj::StringPtr name = nestedDecl.getName().getValue();
locked->aliases.insert(std::make_pair(name, kj::mv(alias)));
break;
}
case Declaration::Body::ENUMERANT_DECL:
case Declaration::Body::FIELD_DECL:
case Declaration::Body::UNION_DECL:
case Declaration::Body::GROUP_DECL:
case Declaration::Body::METHOD_DECL:
case Declaration::Body::NAKED_ID:
case Declaration::Body::NAKED_ANNOTATION:
case Declaration::ENUMERANT:
case Declaration::FIELD:
case Declaration::UNION:
case Declaration::GROUP:
case Declaration::METHOD:
case Declaration::NAKED_ID:
case Declaration::NAKED_ANNOTATION:
// Not a node. Skip.
break;
default:
......@@ -841,18 +841,18 @@ static void findImports(TypeExpression::Reader type, std::set<kj::StringPtr>& ou
}
static void findImports(Declaration::Reader decl, std::set<kj::StringPtr>& output) {
switch (decl.getBody().which()) {
case Declaration::Body::USING_DECL:
findImports(decl.getBody().getUsingDecl().getTarget(), output);
switch (decl.which()) {
case Declaration::USING:
findImports(decl.getUsing().getTarget(), output);
break;
case Declaration::Body::CONST_DECL:
findImports(decl.getBody().getConstDecl().getType(), output);
case Declaration::CONST:
findImports(decl.getConst().getType(), output);
break;
case Declaration::Body::FIELD_DECL:
findImports(decl.getBody().getFieldDecl().getType(), output);
case Declaration::FIELD:
findImports(decl.getField().getType(), output);
break;
case Declaration::Body::METHOD_DECL: {
auto method = decl.getBody().getMethodDecl();
case Declaration::METHOD: {
auto method = decl.getMethod();
for (auto param: method.getParams()) {
findImports(param.getType(), output);
for (auto ann: param.getAnnotations()) {
......@@ -905,22 +905,22 @@ Compiler::Impl::Impl(AnnotationFlag annotationFlag)
// defines a builtin declaration visible in the global scope.
#warning "temporary hack for schema transition"
builtinDecls["Void"] = nodeArena.allocateOwn<Node>("Void", Declaration::Body::BUILTIN_VOID);
builtinDecls["Bool"] = nodeArena.allocateOwn<Node>("Bool", Declaration::Body::BUILTIN_BOOL);
builtinDecls["Int8"] = nodeArena.allocateOwn<Node>("Int8", Declaration::Body::BUILTIN_INT8);
builtinDecls["Int16"] = nodeArena.allocateOwn<Node>("Int16", Declaration::Body::BUILTIN_INT16);
builtinDecls["Int32"] = nodeArena.allocateOwn<Node>("Int32", Declaration::Body::BUILTIN_INT32);
builtinDecls["Int64"] = nodeArena.allocateOwn<Node>("Int64", Declaration::Body::BUILTIN_INT64);
builtinDecls["UInt8"] = nodeArena.allocateOwn<Node>("UInt8", Declaration::Body::BUILTIN_U_INT8);
builtinDecls["UInt16"] = nodeArena.allocateOwn<Node>("UInt16", Declaration::Body::BUILTIN_U_INT16);
builtinDecls["UInt32"] = nodeArena.allocateOwn<Node>("UInt32", Declaration::Body::BUILTIN_U_INT32);
builtinDecls["UInt64"] = nodeArena.allocateOwn<Node>("UInt64", Declaration::Body::BUILTIN_U_INT64);
builtinDecls["Float32"] = nodeArena.allocateOwn<Node>("Float32", Declaration::Body::BUILTIN_FLOAT32);
builtinDecls["Float64"] = nodeArena.allocateOwn<Node>("Float64", Declaration::Body::BUILTIN_FLOAT64);
builtinDecls["Text"] = nodeArena.allocateOwn<Node>("Text", Declaration::Body::BUILTIN_TEXT);
builtinDecls["Data"] = nodeArena.allocateOwn<Node>("Data", Declaration::Body::BUILTIN_DATA);
builtinDecls["List"] = nodeArena.allocateOwn<Node>("List", Declaration::Body::BUILTIN_LIST);
builtinDecls["Object"] = nodeArena.allocateOwn<Node>("Object", Declaration::Body::BUILTIN_OBJECT);
builtinDecls["Void"] = nodeArena.allocateOwn<Node>("Void", Declaration::BUILTIN_VOID);
builtinDecls["Bool"] = nodeArena.allocateOwn<Node>("Bool", Declaration::BUILTIN_BOOL);
builtinDecls["Int8"] = nodeArena.allocateOwn<Node>("Int8", Declaration::BUILTIN_INT8);
builtinDecls["Int16"] = nodeArena.allocateOwn<Node>("Int16", Declaration::BUILTIN_INT16);
builtinDecls["Int32"] = nodeArena.allocateOwn<Node>("Int32", Declaration::BUILTIN_INT32);
builtinDecls["Int64"] = nodeArena.allocateOwn<Node>("Int64", Declaration::BUILTIN_INT64);
builtinDecls["UInt8"] = nodeArena.allocateOwn<Node>("UInt8", Declaration::BUILTIN_U_INT8);
builtinDecls["UInt16"] = nodeArena.allocateOwn<Node>("UInt16", Declaration::BUILTIN_U_INT16);
builtinDecls["UInt32"] = nodeArena.allocateOwn<Node>("UInt32", Declaration::BUILTIN_U_INT32);
builtinDecls["UInt64"] = nodeArena.allocateOwn<Node>("UInt64", Declaration::BUILTIN_U_INT64);
builtinDecls["Float32"] = nodeArena.allocateOwn<Node>("Float32", Declaration::BUILTIN_FLOAT32);
builtinDecls["Float64"] = nodeArena.allocateOwn<Node>("Float64", Declaration::BUILTIN_FLOAT64);
builtinDecls["Text"] = nodeArena.allocateOwn<Node>("Text", Declaration::BUILTIN_TEXT);
builtinDecls["Data"] = nodeArena.allocateOwn<Node>("Data", Declaration::BUILTIN_DATA);
builtinDecls["List"] = nodeArena.allocateOwn<Node>("List", Declaration::BUILTIN_LIST);
builtinDecls["Object"] = nodeArena.allocateOwn<Node>("Object", Declaration::BUILTIN_OBJECT);
#if 0
StructSchema::Union declBodySchema =
......@@ -930,7 +930,7 @@ Compiler::Impl::Impl(AnnotationFlag annotationFlag)
if (name.startsWith("builtin")) {
kj::StringPtr symbolName = name.slice(strlen("builtin"));
builtinDecls[symbolName] = nodeArena.allocateOwn<Node>(
symbolName, static_cast<Declaration::Body::Which>(member.getIndex()));
symbolName, static_cast<Declaration::Which>(member.getIndex()));
}
}
#endif
......
......@@ -59,19 +59,19 @@ struct DeclName {
# * `.absolute.path.to.SomeType`
# * `import "foo.capnp"`
base @0 union {
base :union {
# The first element of the name.
absoluteName @1 :LocatedText; # A symbol at the global scope.
relativeName @2 :LocatedText; # A symbol that should be looked up lexically.
importName @3 :LocatedText; # A file name to import.
absoluteName @0 :LocatedText; # A symbol at the global scope.
relativeName @1 :LocatedText; # A symbol that should be looked up lexically.
importName @2 :LocatedText; # A file name to import.
}
memberPath @4 :List(LocatedText);
memberPath @3 :List(LocatedText);
# List of `.member` suffixes.
startByte @5 :UInt32;
endByte @6 :UInt32;
startByte @4 :UInt32;
endByte @5 :UInt32;
}
struct TypeExpression {
......@@ -92,28 +92,24 @@ struct TypeExpression {
struct ValueExpression {
# An expression evaluating to a value.
body @0 union {
unknown @1 :Void; # e.g. parse error; downstream should ignore
positiveInt @2 :UInt64;
negativeInt @3 :UInt64;
float @4 :Float64;
string @5 :Text;
name @6 :DeclName;
list @7 :List(ValueExpression);
structValue @8 :List(FieldAssignment);
union {
unknown @0 :Void; # e.g. parse error; downstream should ignore
positiveInt @1 :UInt64;
negativeInt @2 :UInt64;
float @3 :Float64;
string @4 :Text;
name @5 :DeclName;
list @6 :List(ValueExpression);
struct @7 :List(FieldAssignment);
}
struct FieldAssignment {
fieldName @0 :LocatedText;
union {
notUnion @1 :Void;
union @2 :LocatedText; # Name of union member being assigned.
}
value @3 :ValueExpression;
value @1 :ValueExpression;
}
startByte @9 :UInt32;
endByte @10 :UInt32;
startByte @8 :UInt32;
endByte @9 :UInt32;
}
struct Declaration {
......@@ -121,133 +117,116 @@ struct Declaration {
name @0 :LocatedText;
id @1 union {
unspecified @2 :Void;
uid @3 :LocatedInteger;
ordinal @4 :LocatedInteger; # limited to 16 bits
id :union {
unspecified @1 :Void;
uid @2 :LocatedInteger;
ordinal @3 :LocatedInteger; # limited to 16 bits
}
nestedDecls @17 :List(Declaration);
nestedDecls @4 :List(Declaration);
annotations @5 :List(AnnotationApplication);
struct AnnotationApplication {
name @0 :DeclName;
value @1 union {
none @2 :Void; # None specified; implies void value.
expression @3 :ValueExpression;
value :union {
none @1 :Void; # None specified; implies void value.
expression @2 :ValueExpression;
}
}
startByte @18 :UInt32;
endByte @19 :UInt32;
docComment @20 :Text;
body @6 union {
fileDecl @24 :File;
usingDecl @7 :Using;
constDecl @8 :Const;
enumDecl @9 :Enum;
enumerantDecl @10 :Enumerant;
structDecl @11 :Struct;
fieldDecl @12 :Field;
unionDecl @13 :Union;
groupDecl @23 :Group;
interfaceDecl @14 :Interface;
methodDecl @15 :Method;
annotationDecl @16 :Annotation;
nakedId @21 :LocatedInteger;
nakedAnnotation @22 :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 @25 :Void;
builtinBool @26 :Void;
builtinInt8 @27 :Void;
builtinInt16 @28 :Void;
builtinInt32 @29 :Void;
builtinInt64 @30 :Void;
builtinUInt8 @31 :Void;
builtinUInt16 @32 :Void;
builtinUInt32 @33 :Void;
builtinUInt64 @34 :Void;
builtinFloat32 @35 :Void;
builtinFloat64 @36 :Void;
builtinText @37 :Void;
builtinData @38 :Void;
builtinList @39 :Void;
builtinObject @40 :Void;
}
struct File {}
struct Using {
target @0 :DeclName;
}
struct Const {
type @0 :TypeExpression;
value @1 :ValueExpression;
}
startByte @6 :UInt32;
endByte @7 :UInt32;
struct Enum {}
docComment @8 :Text;
struct Enumerant {}
union {
file @9 :Void;
struct Struct {}
struct Field {
type @0 :TypeExpression;
defaultValue @1 union {
none @2 :Void;
value @3 :ValueExpression;
using :group {
target @10 :DeclName;
}
}
struct Union {}
struct Group {}
const :group {
type @11 :TypeExpression;
value @12 :ValueExpression;
}
struct Interface {}
enum @13 :Void;
enumerant @14 :Void;
struct Method {
params @0 :List(Param);
struct Param {
name @0 :LocatedText; # If null, param failed to parse.
type @1 :TypeExpression;
annotations @2 :List(AnnotationApplication);
defaultValue @3 union {
none @4 :Void;
value @5 :ValueExpression;
struct @15 :Void;
field :group {
type @16 :TypeExpression;
defaultValue :union {
none @17 :Void;
value @18 :ValueExpression;
}
}
union @19 :Void;
group @20 :Void;
interface @21 :Void;
method :group {
params @22 :List(Param);
returnType :union {
none @23 :Void;
expression @24 :TypeExpression;
}
}
returnType @1 union {
none @2 :Void; # No return type specified; implied Void.
expression @3 :TypeExpression;
annotation :group {
type @25 :TypeExpression;
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;
}
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;
builtinList @54 :Void;
builtinObject @55 :Void;
}
struct Annotation {
type @0 :TypeExpression;
targetsFile @1 :Bool;
targetsConst @2 :Bool;
targetsEnum @3 :Bool;
targetsEnumerant @4 :Bool;
targetsStruct @5 :Bool;
targetsField @6 :Bool;
targetsUnion @7 :Bool;
targetsInterface @8 :Bool;
targetsMethod @9 :Bool;
targetsParam @10 :Bool;
targetsAnnotation @11 :Bool;
struct Param {
name @0 :LocatedText; # If null, param failed to parse.
type @1 :TypeExpression;
annotations @2 :List(AnnotationApplication);
defaultValue @3 union {
none @4 :Void;
value @5 :ValueExpression;
}
}
}
......
This source diff could not be displayed because it is too large. You can view the blob instead.
This diff is collapsed.
This diff is collapsed.
......@@ -46,7 +46,7 @@ public:
public:
struct ResolvedName {
uint64_t id;
Declaration::Body::Which kind;
Declaration::Which kind;
};
virtual kj::Maybe<ResolvedName> resolve(const DeclName::Reader& name) const = 0;
......@@ -143,11 +143,11 @@ private:
class StructLayout;
class StructTranslator;
void compileEnum(Declaration::Enum::Reader decl, List<Declaration>::Reader members,
void compileEnum(Void decl, List<Declaration>::Reader members,
schema2::Node::Builder builder);
void compileStruct(Declaration::Struct::Reader decl, List<Declaration>::Reader members,
void compileStruct(Void decl, List<Declaration>::Reader members,
schema2::Node::Builder builder);
void compileInterface(Declaration::Interface::Reader decl, List<Declaration>::Reader members,
void compileInterface(Void decl, List<Declaration>::Reader members,
schema2::Node::Builder builder);
// The `members` arrays contain only members with ordinal numbers, in code order. Other members
// are handled elsewhere.
......
This diff is collapsed.
......@@ -117,7 +117,7 @@ public:
Parser<Orphan<Declaration::AnnotationApplication>> annotation;
Parser<Orphan<LocatedInteger>> uid;
Parser<Orphan<LocatedInteger>> ordinal;
Parser<Orphan<Declaration::Method::Param>> param;
Parser<Orphan<Declaration::Param>> param;
DeclParser usingDecl;
DeclParser constDecl;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment