| 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 <type_traits> |
| 19 |
#include <utility> |
| 20 |
|
| 21 |
#include "frontend/Parser.h" |
| 22 |
#include "js/ColumnNumber.h" // JS::LimitedColumnNumberOneOrigin |
| 23 |
|
| 24 |
namespace js::frontend { |
| 25 |
|
| 26 |
class EitherParser final { |
| 27 |
// Leave this as a variant, to promote good form until 8-bit parser |
| 28 |
// integration. |
| 29 |
mozilla::Variant<Parser<FullParseHandler, char16_t>* const, |
| 30 |
Parser<FullParseHandler, mozilla::Utf8Unit>* const> |
| 31 |
parser; |
| 32 |
|
| 33 |
public: |
| 34 |
template <class Parser> |
| 35 |
explicit EitherParser(Parser* parser) : parser(parser) {} |
| 36 |
|
| 37 |
const ErrorReporter& errorReporter() const { |
| 38 |
return parser.match([](auto* parser) -> const frontend::ErrorReporter& { |
| 39 |
return parser->tokenStream; |
| 40 |
}); |
| 41 |
} |
| 42 |
|
| 43 |
void computeLineAndColumn(uint32_t offset, uint32_t* line, |
| 44 |
JS::LimitedColumnNumberOneOrigin* column) const { |
| 45 |
return parser.match([offset, line, column](auto* parser) -> void { |
| 46 |
parser->tokenStream.computeLineAndColumn(offset, line, column); |
| 47 |
}); |
| 48 |
} |
| 49 |
|
| 50 |
ParserAtomsTable& parserAtoms() { |
| 51 |
auto& base = parser.match( |
| 52 |
[](auto* parser) -> frontend::ParserSharedBase& { return *parser; }); |
| 53 |
return base.parserAtoms(); |
| 54 |
} |
| 55 |
}; |
| 56 |
|
| 57 |
} /* namespace js::frontend */ |
| 58 |
|
| 59 |
#endif /* frontend_EitherParser_h */ |