tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

EitherParser.h (1768B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
      2 * vim: set ts=8 sts=2 et sw=2 tw=80:
      3 * This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 /*
      8 * A variant-like class abstracting operations on a Parser with a given
      9 * ParseHandler but unspecified character type.
     10 */
     11 
     12 #ifndef frontend_EitherParser_h
     13 #define frontend_EitherParser_h
     14 
     15 #include "mozilla/Utf8.h"
     16 #include "mozilla/Variant.h"
     17 
     18 #include "frontend/Parser.h"
     19 #include "js/ColumnNumber.h"  // JS::LimitedColumnNumberOneOrigin
     20 
     21 namespace js::frontend {
     22 
     23 class EitherParser final {
     24  // Leave this as a variant, to promote good form until 8-bit parser
     25  // integration.
     26  mozilla::Variant<Parser<FullParseHandler, char16_t>* const,
     27                   Parser<FullParseHandler, mozilla::Utf8Unit>* const>
     28      parser;
     29 
     30 public:
     31  template <class Parser>
     32  explicit EitherParser(Parser* parser) : parser(parser) {}
     33 
     34  const ErrorReporter& errorReporter() const {
     35    return parser.match([](auto* parser) -> const frontend::ErrorReporter& {
     36      return parser->tokenStream;
     37    });
     38  }
     39 
     40  void computeLineAndColumn(uint32_t offset, uint32_t* line,
     41                            JS::LimitedColumnNumberOneOrigin* column) const {
     42    return parser.match([offset, line, column](auto* parser) -> void {
     43      parser->tokenStream.computeLineAndColumn(offset, line, column);
     44    });
     45  }
     46 
     47  ParserAtomsTable& parserAtoms() {
     48    auto& base = parser.match(
     49        [](auto* parser) -> frontend::ParserSharedBase& { return *parser; });
     50    return base.parserAtoms();
     51  }
     52 };
     53 
     54 } /* namespace js::frontend */
     55 
     56 #endif /* frontend_EitherParser_h */