parser.h 5.84 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 23 24

#ifndef CAPNP_COMPILER_PARSER_H_
#define CAPNP_COMPILER_PARSER_H_

25 26 27 28
#if defined(__GNUC__) && !CAPNP_HEADER_WARNINGS
#pragma GCC system_header
#endif

29 30
#include <capnp/compiler/grammar.capnp.h>
#include <capnp/compiler/lexer.capnp.h>
Kenton Varda's avatar
Kenton Varda committed
31 32
#include <kj/parse/common.h>
#include <kj/arena.h>
33
#include "error-reporter.h"
Kenton Varda's avatar
Kenton Varda committed
34 35 36 37

namespace capnp {
namespace compiler {

38
void parseFile(List<Statement>::Reader statements, ParsedFile::Builder result,
39
               ErrorReporter& errorReporter);
Kenton Varda's avatar
Kenton Varda committed
40
// Parse a list of statements to build a ParsedFile.
41 42 43
//
// If any errors are reported, then the output is not usable.  However, it may be passed on through
// later stages of compilation in order to detect additional errors.
Kenton Varda's avatar
Kenton Varda committed
44

45 46 47
uint64_t generateRandomId();
// Generate a new random unique ID.  This lives here mostly for lack of a better location.

Kenton Varda's avatar
Kenton Varda committed
48 49
uint64_t generateChildId(uint64_t parentId, kj::StringPtr childName);
// Generate the ID for a child node given its parent ID and name.
50 51 52

uint64_t generateGroupId(uint64_t parentId, uint16_t groupIndex);
// Generate the ID for a group within a struct.
53 54 55

uint64_t generateMethodParamsId(uint64_t parentId, uint16_t methodOrdinal, bool isResults);
// Generate the ID for a struct representing method params / results.
Kenton Varda's avatar
Kenton Varda committed
56
//
57
// TODO(cleanup):  Move generate*Id() somewhere more sensible.
Kenton Varda's avatar
Kenton Varda committed
58

Kenton Varda's avatar
Kenton Varda committed
59 60 61 62 63
class CapnpParser {
  // Advanced parser interface.  This interface exposes the inner parsers so that you can embed
  // them into your own parsers.

public:
64
  CapnpParser(Orphanage orphanage, ErrorReporter& errorReporter);
Kenton Varda's avatar
Kenton Varda committed
65 66 67
  // `orphanage` is used to allocate Cap'n Proto message objects in the result.  `inputStart` is
  // a pointer to the beginning of the input, used to compute byte offsets.

68
  ~CapnpParser() noexcept(false);
Kenton Varda's avatar
Kenton Varda committed
69

70 71
  KJ_DISALLOW_COPY(CapnpParser);

Kenton Varda's avatar
Kenton Varda committed
72 73 74 75 76 77
  using ParserInput = kj::parse::IteratorInput<Token::Reader, List<Token>::Reader::Iterator>;
  struct DeclParserResult;
  template <typename Output>
  using Parser = kj::parse::ParserRef<ParserInput, Output>;
  using DeclParser = Parser<DeclParserResult>;

78 79
  kj::Maybe<Orphan<Declaration>> parseStatement(
      Statement::Reader statement, const DeclParser& parser);
Kenton Varda's avatar
Kenton Varda committed
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
  // Parse a statement using the given parser.  In addition to parsing the token sequence itself,
  // this takes care of parsing the block (if any) and copying over the doc comment (if any).

  struct DeclParserResult {
    // DeclParser parses a sequence of tokens representing just the "line" part of the statement --
    // i.e. everything up to the semicolon or opening curly brace.
    //
    // Use `parseStatement()` to avoid having to deal with this struct.

    Orphan<Declaration> decl;
    // The decl parsed so far.  The decl's `docComment` and `nestedDecls` are both empty at this
    // point.

    kj::Maybe<DeclParser> memberParser;
    // If null, the statement should not have a block.  If non-null, the statement should have a
    // block containing statements parseable by this parser.

    DeclParserResult(Orphan<Declaration>&& decl, const DeclParser& memberParser)
        : decl(kj::mv(decl)), memberParser(memberParser) {}
    explicit DeclParserResult(Orphan<Declaration>&& decl)
        : decl(kj::mv(decl)), memberParser(nullptr) {}
  };

  struct Parsers {
    DeclParser genericDecl;
    // Parser that matches any declaration type except those that have ordinals (since they are
    // context-dependent).

    DeclParser fileLevelDecl;
    DeclParser enumLevelDecl;
    DeclParser structLevelDecl;
    DeclParser interfaceLevelDecl;
    // Parsers that match genericDecl *and* the ordinal-based declaration types valid in the given
    // contexts.  Note that these may match declarations that are not actually allowed in the given
    // contexts, as long as the grammar is unambiguous.  E.g. nested types are not allowed in
    // enums, but they'll be accepted by enumLevelDecl.  A later stage of compilation should report
    // these as errors.

Kenton Varda's avatar
Kenton Varda committed
118
    Parser<Orphan<Expression>> expression;
119 120 121
    Parser<Orphan<Declaration::AnnotationApplication>> annotation;
    Parser<Orphan<LocatedInteger>> uid;
    Parser<Orphan<LocatedInteger>> ordinal;
122
    Parser<Orphan<Declaration::Param>> param;
Kenton Varda's avatar
Kenton Varda committed
123 124 125 126 127 128 129 130

    DeclParser usingDecl;
    DeclParser constDecl;
    DeclParser enumDecl;
    DeclParser enumerantDecl;
    DeclParser structDecl;
    DeclParser fieldDecl;
    DeclParser unionDecl;
131
    DeclParser groupDecl;
Kenton Varda's avatar
Kenton Varda committed
132 133 134 135 136 137 138 139 140 141 142
    DeclParser interfaceDecl;
    DeclParser methodDecl;
    DeclParser paramDecl;
    DeclParser annotationDecl;
    // Parsers for individual declaration types.
  };

  const Parsers& getParsers() { return parsers; }

private:
  Orphanage orphanage;
143
  ErrorReporter& errorReporter;
Kenton Varda's avatar
Kenton Varda committed
144 145 146 147 148 149 150 151
  kj::Arena arena;
  Parsers parsers;
};

}  // namespace compiler
}  // namespace capnp

#endif  // CAPNP_COMPILER_PARSER_H_