capnproto

FORK: Cap'n Proto serialization/RPC system - core tools and C++ library
git clone https://git.neptards.moe/neptards/capnproto.git
Log | Files | Refs | README | LICENSE

lexer.h (3806B)


      1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
      2 // Licensed under the MIT License:
      3 //
      4 // Permission is hereby granted, free of charge, to any person obtaining a copy
      5 // of this software and associated documentation files (the "Software"), to deal
      6 // in the Software without restriction, including without limitation the rights
      7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
      8 // copies of the Software, and to permit persons to whom the Software is
      9 // furnished to do so, subject to the following conditions:
     10 //
     11 // The above copyright notice and this permission notice shall be included in
     12 // all copies or substantial portions of the Software.
     13 //
     14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
     20 // THE SOFTWARE.
     21 
     22 #pragma once
     23 
     24 #include <capnp/compiler/lexer.capnp.h>
     25 #include <kj/parse/common.h>
     26 #include <kj/arena.h>
     27 #include "error-reporter.h"
     28 
     29 CAPNP_BEGIN_HEADER
     30 
     31 namespace capnp {
     32 namespace compiler {
     33 
     34 bool lex(kj::ArrayPtr<const char> input, LexedStatements::Builder result,
     35          ErrorReporter& errorReporter);
     36 bool lex(kj::ArrayPtr<const char> input, LexedTokens::Builder result, ErrorReporter& errorReporter);
     37 // Lex the given source code, placing the results in `result`.  Returns true if there
     38 // were no errors, false if there were.  Even when errors are present, the file may have partial
     39 // content which can be fed into later stages of parsing in order to find more errors.
     40 //
     41 // There are two versions, one that parses a list of statements, and one which just parses tokens
     42 // that might form a part of one statement.  In other words, in the later case, the input should
     43 // not contain semicolons or curly braces, unless they are in string literals of course.
     44 
     45 class Lexer {
     46   // Advanced lexer interface.  This interface exposes the inner parsers so that you can embed them
     47   // into your own parsers.
     48 
     49 public:
     50   Lexer(Orphanage orphanage, ErrorReporter& errorReporter);
     51   // `orphanage` is used to allocate Cap'n Proto message objects in the result.  `inputStart` is
     52   // a pointer to the beginning of the input, used to compute byte offsets.
     53 
     54   ~Lexer() noexcept(false);
     55 
     56   class ParserInput: public kj::parse::IteratorInput<char, const char*> {
     57     // Like IteratorInput<char, const char*> except that positions are measured as byte offsets
     58     // rather than pointers.
     59 
     60   public:
     61     ParserInput(const char* begin, const char* end)
     62       : IteratorInput<char, const char*>(begin, end), begin(begin) {}
     63     explicit ParserInput(ParserInput& parent)
     64       : IteratorInput<char, const char*>(parent), begin(parent.begin) {}
     65 
     66     inline uint32_t getBest() {
     67       return IteratorInput<char, const char*>::getBest() - begin;
     68     }
     69     inline uint32_t getPosition() {
     70       return IteratorInput<char, const char*>::getPosition() - begin;
     71     }
     72 
     73   private:
     74     const char* begin;
     75   };
     76 
     77   template <typename Output>
     78   using Parser = kj::parse::ParserRef<ParserInput, Output>;
     79 
     80   struct Parsers {
     81     Parser<kj::Tuple<>> emptySpace;
     82     Parser<Orphan<Token>> token;
     83     Parser<kj::Array<Orphan<Token>>> tokenSequence;
     84     Parser<Orphan<Statement>> statement;
     85     Parser<kj::Array<Orphan<Statement>>> statementSequence;
     86   };
     87 
     88   const Parsers& getParsers() { return parsers; }
     89 
     90 private:
     91   Orphanage orphanage;
     92   kj::Arena arena;
     93   Parsers parsers;
     94 };
     95 
     96 }  // namespace compiler
     97 }  // namespace capnp
     98 
     99 CAPNP_END_HEADER