17#include "llvm/ADT/StringMap.h"
18#include "llvm/ADT/StringRef.h"
19#include "llvm/Support/Errc.h"
20#include "llvm/Support/Error.h"
27using namespace transformer;
38template <
typename... Ts>
using RangeSelectorOp =
RangeSelector (*)(Ts...);
45 StringRef OriginalInput;
50template <
typename ResultType>
struct ParseProgress {
57template <
typename T>
using ParseFunction = ExpectedProgress<T> (*)(ParseState);
59class ParseError :
public llvm::ErrorInfo<ParseError> {
64 ParseError(
size_t Pos, std::string ErrorMsg, std::string InputExcerpt)
65 : Pos(Pos), ErrorMsg(
std::move(ErrorMsg)),
66 Excerpt(
std::move(InputExcerpt)) {}
68 void log(llvm::raw_ostream &OS)
const override {
69 OS <<
"parse error at position (" << Pos <<
"): " << ErrorMsg
73 std::error_code convertToErrorCode()
const override {
74 return llvm::inconvertibleErrorCode();
87static const llvm::StringMap<RangeSelectorOp<std::string>> &
89 static const llvm::StringMap<RangeSelectorOp<std::string>> M = {
101static const llvm::StringMap<RangeSelectorOp<RangeSelector>> &
103 static const llvm::StringMap<RangeSelectorOp<RangeSelector>> M = {
108static const llvm::StringMap<RangeSelectorOp<std::string, std::string>> &
110 static const llvm::StringMap<RangeSelectorOp<std::string, std::string>> M = {
115static const llvm::StringMap<RangeSelectorOp<RangeSelector, RangeSelector>> &
117 static const llvm::StringMap<RangeSelectorOp<RangeSelector, RangeSelector>>
122template <
typename Element>
123std::optional<Element>
findOptional(
const llvm::StringMap<Element> &Map,
124 llvm::StringRef Key) {
125 auto it = Map.find(Key);
131template <
typename ResultType>
134 return ParseProgress<ResultType>{State, std::move(Result)};
138 size_t Pos = S.OriginalInput.size() - S.Input.size();
139 return llvm::make_error<ParseError>(Pos, std::move(ErrorMsg),
140 S.OriginalInput.substr(Pos, 20).str());
144static ParseState
advance(ParseState S,
size_t N) {
145 S.Input = S.Input.drop_front(N);
155static ExpectedProgress<std::nullopt_t>
parseChar(
char c, ParseState State) {
157 if (State.Input.empty() || State.Input.front() !=
c)
159 (
"expected char not found: " + llvm::Twine(
c)).str());
164static ExpectedProgress<std::string>
parseId(ParseState State) {
166 auto Id = State.Input.take_while(
177 if (State.Input.empty())
179 if (!State.Input.consume_front(
"\""))
182 "expecting string, but encountered other character or end of input");
184 StringRef
Id = State.Input.take_until([](
char c) {
return c ==
'"'; });
185 if (State.Input.size() ==
Id.size())
194ExpectedProgress<RangeSelector>
parseSingle(ParseFunction<T> ParseElement,
195 RangeSelectorOp<T> Op,
199 return P.takeError();
201 auto E = ParseElement(
P->State);
203 return E.takeError();
207 return P.takeError();
215ExpectedProgress<RangeSelector>
parsePair(ParseFunction<T> ParseElement,
216 RangeSelectorOp<T, T> Op,
220 return P.takeError();
222 auto Left = ParseElement(
P->State);
224 return Left.takeError();
228 return P.takeError();
230 auto Right = ParseElement(
P->State);
232 return Right.takeError();
236 return P.takeError();
239 Op(std::move(Left->Value), std::move(Right->Value)));
245static ExpectedProgress<RangeSelector>
249 return Id.takeError();
251 std::string OpName = std::move(
Id->Value);
268 ParseState State = {Input, Input};
271 return Result.takeError();
272 State = Result->State;
275 if (State.Input.empty())
276 return Result->Value;
static ExpectedProgress< std::string > parseStringId(ParseState State)
static ExpectedProgress< std::nullopt_t > parseChar(char c, ParseState State)
ParseProgress< ResultType > makeParseProgress(ParseState State, ResultType Result)
static StringRef consumeWhitespace(StringRef S)
ExpectedProgress< RangeSelector > parseSingle(ParseFunction< T > ParseElement, RangeSelectorOp< T > Op, ParseState State)
ExpectedProgress< RangeSelector > parsePair(ParseFunction< T > ParseElement, RangeSelectorOp< T, T > Op, ParseState State)
std::optional< Element > findOptional(const llvm::StringMap< Element > &Map, llvm::StringRef Key)
static llvm::Error makeParseError(const ParseState &S, std::string ErrorMsg)
static const llvm::StringMap< RangeSelectorOp< RangeSelector > > & getUnaryRangeSelectors()
static ExpectedProgress< RangeSelector > parseRangeSelectorImpl(ParseState State)
static ExpectedProgress< std::string > parseId(ParseState State)
static ParseState advance(ParseState S, size_t N)
static const llvm::StringMap< RangeSelectorOp< RangeSelector, RangeSelector > > & getBinaryRangeSelectors()
static const llvm::StringMap< RangeSelectorOp< std::string, std::string > > & getBinaryStringSelectors()
static const llvm::StringMap< RangeSelectorOp< std::string > > & getUnaryStringSelectors()
Defines parsing functions for Transformer types.
Defines a combinator library supporting the definition of selectors, which select source ranges based...
Defines the clang::SourceLocation class and associated facilities.
__device__ __2f16 float c
The JSON file list parser is used to communicate input to InstallAPI.
LLVM_READNONE bool isASCII(char c)
Returns true if a byte is an ASCII character.
LLVM_READONLY bool isAsciiIdentifierContinue(unsigned char c)
LLVM_READONLY bool isWhitespace(unsigned char c)
Return true if this character is horizontal or vertical ASCII whitespace: ' ', '\t',...