| 1 |
/*========================== begin_copyright_notice ============================ |
| 2 |
|
| 3 |
Copyright (C) 2017-2021 Intel Corporation |
| 4 |
|
| 5 |
SPDX-License-Identifier: MIT |
| 6 |
|
| 7 |
============================= end_copyright_notice ===========================*/ |
| 8 |
|
| 9 |
#ifndef IGA_FRONTEND_PARSER_HPP |
| 10 |
#define IGA_FRONTEND_PARSER_HPP |
| 11 |
|
| 12 |
#include "../ErrorHandler.hpp" |
| 13 |
#include "../IR/Loc.hpp" |
| 14 |
#include "../Models/Models.hpp" |
| 15 |
#include "BufferedLexer.hpp" |
| 16 |
|
| 17 |
#include <cstdarg> |
| 18 |
#include <initializer_list> |
| 19 |
#include <ostream> |
| 20 |
#include <sstream> |
| 21 |
#include <stdexcept> |
| 22 |
#include <string> |
| 23 |
#include <utility> |
| 24 |
#include <vector> |
| 25 |
|
| 26 |
namespace iga { |
| 27 |
template <typename T> |
| 28 |
using IdentMap = std::initializer_list<std::pair<std::string, T>>; |
| 29 |
template <typename T> |
| 30 |
static inline T Lookup(std::string sym, const IdentMap<T> &M, T orElse) { |
| 31 |
for (const auto &e : M) { |
| 32 |
if (e.first == sym) |
| 33 |
return e.second; |
| 34 |
} |
| 35 |
return orElse; |
| 36 |
} |
| 37 |
|
| 38 |
// this type is used to bail out of the parsing algorithm upon syntax error |
| 39 |
struct SyntaxError : std::runtime_error { |
| 40 |
const Loc loc; |
| 41 |
std::string message; |
| 42 |
|
| 43 |
SyntaxError(const struct Loc &l, const std::string &m) throw() |
| 44 |
: std::runtime_error(m), loc(l), message(m) {} |
| 45 |
~SyntaxError() {} |
| 46 |
}; |
| 47 |
|
| 48 |
/////////////////////////////////////////////////////////////////////////// |
| 49 |
// Recursive descent parser. |
| 50 |
// The nomaclaure for method names is roughly: |
| 51 |
// Looking**** peeks at the token, doesn't consume |
| 52 |
// Looking**From peeks relative to the lexer's current offset |
| 53 |
// Consume**** consume next token if some criteria is true |
| 54 |
// Parse****** generally corresponds to a non-terminal or some |
| 55 |
// complicated lexemes |
| 56 |
// |
| 57 |
// |
| 58 |
class Parser { |
| 59 |
protected: |
| 60 |
BufferedLexer m_lexer; |
| 61 |
ErrorHandler &m_errorHandler; |
| 62 |
|
| 63 |
public: |
| 64 |
Parser(const std::string &inp, ErrorHandler &errHandler) |
| 65 |
: m_lexer(inp), m_errorHandler(errHandler) {} |
| 66 |
|
| 67 |
////////////////////////////////////////////////////////////////////// |
| 68 |
// DEBUGGING |
| 69 |
// void DumpLookaheads(int n = 1) const {m_lexer.DumpLookaheads(n); } |
| 70 |
void ShowCurrentLexicalContext(std::ostream &os) const { |
| 71 |
ShowCurrentLexicalContext(NextLoc(), os); |
| 72 |
} |
| 73 |
void ShowCurrentLexicalContext(const Loc &loc, std::ostream &os) const; |
| 74 |
|
| 75 |
////////////////////////////////////////////////////////////////////// |
| 76 |
// WARNINGS and ERRORS |
| 77 |
template <typename... Ts> void WarningT(Ts... ts) { |
| 78 |
WarningS(NextLoc(), iga::format(std::move(ts)...)); |
| 79 |
} |
| 80 |
template <typename... Ts> void WarningAtT(const Loc &loc, Ts... ts) { |
| 81 |
WarningS(loc, iga::format(std::move(ts)...)); |
| 82 |
} |
| 83 |
void WarningS(const Loc &loc, const std::string &msg); |
| 84 |
|
| 85 |
template <typename... Ts> void ErrorT(Ts... ts) { |
| 86 |
ErrorAtS(NextLoc(), iga::format(std::move(ts)...)); |
| 87 |
} |
| 88 |
template <typename... Ts> void ErrorAtT(const Loc &loc, Ts... ts) { |
| 89 |
ErrorAtS(loc, iga::format(std::move(ts)...)); |
| 90 |
} |
| 91 |
void ErrorAtS(const Loc &loc, const std::string &smsg); |
| 92 |
|
| 93 |
template <typename... Ts> void FailT(Ts... ts) { |
| 94 |
FailS(NextLoc(), iga::format(std::move(ts)...)); |
| 95 |
} |
| 96 |
template <typename... Ts> void FailAtT(const Loc &loc, Ts... ts) { |
| 97 |
FailS(loc, iga::format(std::move(ts)...)); |
| 98 |
} |
| 99 |
void FailS(const Loc &loc, const std::string &msg); |
| 100 |
void FailAfterPrev(const char *msg); |
| 101 |
|
| 102 |
////////////////////////////////////////////////////////////////////// |
| 103 |
// BASIC and GENERAL FUNCTIONS |
| 104 |
const Token &Next(int i = 0) const { return m_lexer.Next(i); } |
| 105 |
|
| 106 |
Loc NextLoc(int i = 0) const { return Next(i).loc; } |
| 107 |
|
| 108 |
uint32_t ExtentToPrevEnd(const Loc &start) const; |
| 109 |
|
| 110 |
uint32_t ExtentTo(const Loc &start, const Loc &end) const; |
| 111 |
|
| 112 |
bool EndOfFile() const { return m_lexer.EndOfFile(); } |
| 113 |
|
| 114 |
bool Skip(int k = 1) { return m_lexer.Skip(k); } |
| 115 |
|
| 116 |
std::string GetTokenAsString(const Token &token) const; |
| 117 |
std::string GetTokenAsString() const { return GetTokenAsString(Next()); } |
| 118 |
|
| 119 |
////////////////////////////////////////////////////////////////////// |
| 120 |
// QUERYING (non-destructive lookahead) |
| 121 |
bool LookingAt(Lexeme lxm) const { return LookingAtFrom(0, lxm); } |
| 122 |
bool LookingAtFrom(int k, Lexeme lxm) const; |
| 123 |
|
| 124 |
bool LookingAtSeq(Lexeme lxm0, Lexeme lxm1) const { |
| 125 |
return LookingAtSeq({lxm0, lxm1}); |
| 126 |
} |
| 127 |
bool LookingAtSeq(Lexeme lxm0, Lexeme lxm1, Lexeme lxm2) const { |
| 128 |
return LookingAtSeq({lxm0, lxm1, lxm2}); |
| 129 |
} |
| 130 |
bool LookingAtSeq(std::initializer_list<Lexeme> lxms) const; |
| 131 |
|
| 132 |
bool LookingAtAnyOf(Lexeme lxm0, Lexeme lxm1) const { |
| 133 |
return LookingAtAnyOf({lxm0, lxm1}); |
| 134 |
} |
| 135 |
bool LookingAtAnyOf(Lexeme lxm0, Lexeme lxm1, Lexeme lxm2) const { |
| 136 |
return LookingAtAnyOf({lxm0, lxm1, lxm2}); |
| 137 |
} |
| 138 |
bool LookingAtAnyOf(std::initializer_list<Lexeme> lxms) const; |
| 139 |
bool LookingAtAnyOfFrom(int i, std::initializer_list<Lexeme> lxms) const; |
| 140 |
|
| 141 |
bool LookingAtPrefix(const char *pfx) const; |
| 142 |
|
| 143 |
////////////////////////////////////////////////////////////////////// |
| 144 |
// CONSUMPTION (destructive lookahead) |
| 145 |
bool Consume(Lexeme lxm) { return m_lexer.Consume(lxm); } |
| 146 |
void ConsumeOrFail(Lexeme lxm, const char *msg); |
| 147 |
// same as above, but the error location chosen is the end of the |
| 148 |
// previous token; i.e. the suffix is screwed up |
| 149 |
void ConsumeOrFailAfterPrev(Lexeme lxm, const char *msg); |
| 150 |
bool Consume(Lexeme lxm0, Lexeme lxm1) { |
| 151 |
// first block doesn't require a label |
| 152 |
if (LookingAtSeq(lxm0, lxm1)) { |
| 153 |
return Skip(2); |
| 154 |
} |
| 155 |
return false; |
| 156 |
} |
| 157 |
|
| 158 |
////////////////////////////////////////////////////////////////////// |
| 159 |
// IDENTIFIER and RAW STRING MANIPULATION |
| 160 |
bool PrefixAtEq(size_t off, const char *pfx) const; |
| 161 |
|
| 162 |
bool LookingAtIdentEq(const char *eq) const; |
| 163 |
bool LookingAtIdentEq(int k, const char *eq) const; |
| 164 |
bool LookingAtIdentEq(const Token &tk, const char *eq) const; |
| 165 |
bool ConsumeIdentEq(const char *eq); |
| 166 |
std::string ConsumeIdentOrFail( |
| 167 |
const char *what = |
| 168 |
nullptr); // can tell what type of ident optionally; e.g. "op name" |
| 169 |
|
| 170 |
bool TokenEq(const Token &tk, const char *eq) const; |
| 171 |
|
| 172 |
template <typename T> |
| 173 |
bool IdentLookupFrom(int k, const IdentMap<T> &map, T &value) const { |
| 174 |
if (!LookingAtFrom(k, IDENT)) { |
| 175 |
return false; |
| 176 |
} |
| 177 |
for (const auto &p : map) { |
| 178 |
if (TokenEq(Next(k), p.first.c_str())) { |
| 179 |
value = p.second; |
| 180 |
return true; |
| 181 |
} |
| 182 |
} |
| 183 |
return false; |
| 184 |
} |
| 185 |
|
| 186 |
template <typename T> |
| 187 |
void ConsumeIdentOneOfOrFail(const IdentMap<T> &map, T &value, |
| 188 |
const char *errExpecting, |
| 189 |
const char *errInvalid) { |
| 190 |
if (!LookingAt(IDENT)) { |
| 191 |
FailT(errExpecting); |
| 192 |
} |
| 193 |
if (!IdentLookupFrom(0, map, value)) { |
| 194 |
FailT(errInvalid); |
| 195 |
} |
| 196 |
Skip(); |
| 197 |
} |
| 198 |
|
| 199 |
template <typename T> |
| 200 |
bool ConsumeIdentOneOf(const IdentMap<T> &map, T &value) { |
| 201 |
if (LookingAt(IDENT) && IdentLookupFrom(0, map, value)) { |
| 202 |
Skip(); |
| 203 |
return true; |
| 204 |
} |
| 205 |
return false; |
| 206 |
} |
| 207 |
|
| 208 |
/////////////////////////////////////////////////////////////////////////// |
| 209 |
// NUMBERS |
| 210 |
// |
| 211 |
template <typename T> bool ConsumeIntLit(T &value) { |
| 212 |
if (LookingAtAnyOf({INTLIT02, INTLIT10, INTLIT16})) { |
| 213 |
ParseIntFrom(NextLoc(), value); |
| 214 |
Skip(); |
| 215 |
return true; |
| 216 |
} |
| 217 |
return false; |
| 218 |
} |
| 219 |
|
| 220 |
template <typename T> void ConsumeIntLitOrFail(T &value, const char *err) { |
| 221 |
if (!ConsumeIntLit(value)) { |
| 222 |
FailT(err); |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
// Examples: |
| 227 |
// 3.141 |
| 228 |
// .451 |
| 229 |
// 3.1e7 |
| 230 |
// 3e9 |
| 231 |
// 3e9.5 |
| 232 |
void ParseFltFrom(const Loc loc, double &value); |
| 233 |
|
| 234 |
template <typename T> void ParseIntFrom(const Loc &loc, T &value) { |
| 235 |
ParseIntFrom(loc.offset, loc.extent, value); |
| 236 |
} |
| 237 |
|
| 238 |
template <typename T> void ParseIntFrom(size_t off, size_t len, T &value) { |
| 239 |
const std::string &src = m_lexer.GetSource(); |
| 240 |
value = 0; |
| 241 |
if (len > 2 && src[off] == '0' && |
| 242 |
(src[off + 1] == 'b' || src[off + 1] == 'B')) { |
| 243 |
for (size_t i = 2; i < len; i++) { |
| 244 |
char chr = src[off + i]; |
| 245 |
T next_value = 2 * value + chr - '0'; |
| 246 |
if (next_value < value) { |
| 247 |
FailS(-1, "integer literal too large"); |
| 248 |
} |
| 249 |
value = next_value; |
| 250 |
} |
| 251 |
} else if (len > 2 && src[off] == '0' && |
| 252 |
(src[off + 1] == 'x' || src[off + 1] == 'X')) { |
| 253 |
for (size_t i = 2; i < len; i++) { |
| 254 |
char chr = src[off + i]; |
| 255 |
char dig = 0; |
| 256 |
if (chr >= '0' && chr <= '9') |
| 257 |
dig = chr - '0'; |
| 258 |
else if (chr >= 'A' && chr <= 'F') |
| 259 |
dig = chr - 'A' + 10; |
| 260 |
else if (chr >= 'a' && chr <= 'f') |
| 261 |
dig = chr - 'a' + 10; |
| 262 |
T next_value = 16 * value + dig; |
| 263 |
if (next_value < value) { |
| 264 |
FailS(-1, "integer literal too large"); |
| 265 |
} |
| 266 |
value = next_value; |
| 267 |
} |
| 268 |
} else { |
| 269 |
for (size_t i = 0; i < len; i++) { |
| 270 |
char chr = src[off + i]; |
| 271 |
T next_value = 10 * value + chr - '0'; |
| 272 |
if (next_value < value) { |
| 273 |
FailS(-1, "integer literal too large"); |
| 274 |
} |
| 275 |
value = next_value; |
| 276 |
} |
| 277 |
} |
| 278 |
} |
| 279 |
}; // Parser |
| 280 |
} // namespace iga |
| 281 |
|
| 282 |
#endif // IGA_FRONTEND_PARSER_HPP |