| 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 |
#include <algorithm> |
| 10 |
#include <cstdarg> |
| 11 |
#include <cstdint> |
| 12 |
#include <cstdio> |
| 13 |
#include <cstdlib> |
| 14 |
#include <exception> |
| 15 |
#include <functional> |
| 16 |
#include <limits> |
| 17 |
#include <list> |
| 18 |
|
| 19 |
#include "../asserts.hpp" |
| 20 |
#include "../strings.hpp" |
| 21 |
#include "Floats.hpp" |
| 22 |
#include "Parser.hpp" |
| 23 |
|
| 24 |
namespace iga { |
| 25 |
////////////////////////////////////////////////////////////////////// |
| 26 |
// DEBUGGING |
| 27 |
// |
| 28 |
void Parser::ShowCurrentLexicalContext(const Loc &loc, std::ostream &os) const { |
| 29 |
WriteTokenContext(m_lexer.GetSource(), loc, os); |
| 30 |
} |
| 31 |
|
| 32 |
////////////////////////////////////////////////////////////////////// |
| 33 |
// WARNINGS and ERRORS |
| 34 |
// |
| 35 |
void Parser::WarningS(const Loc &loc, const std::string &msg) { |
| 36 |
m_errorHandler.reportWarning(loc, msg); |
| 37 |
} |
| 38 |
void Parser::ErrorAtS(const Loc &loc, const std::string &msg) { |
| 39 |
m_errorHandler.reportError(loc, msg); |
| 40 |
} |
| 41 |
|
| 42 |
void Parser::FailS(const Loc &loc, const std::string &msg) { |
| 43 |
// DumpLookahead(); |
| 44 |
throw SyntaxError(loc, msg); |
| 45 |
} |
| 46 |
|
| 47 |
void Parser::FailAfterPrev(const char *msg) { |
| 48 |
Token pv = Next(-1); |
| 49 |
if (pv.loc.extent == 0) { |
| 50 |
// previous location is not valid => use the other path |
| 51 |
FailS(NextLoc(), msg); |
| 52 |
} else { |
| 53 |
// step over the token contents manually |
| 54 |
Loc loc = pv.loc.endOfToken(); |
| 55 |
FailS(loc, msg); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
////////////////////////////////////////////////////////////////////// |
| 60 |
// BASIC and GENERAL FUNCTIONS |
| 61 |
uint32_t Parser::ExtentToPrevEnd(const Loc &start) const { |
| 62 |
return ExtentTo(start, NextLoc(-1)); |
| 63 |
} |
| 64 |
|
| 65 |
uint32_t Parser::ExtentTo(const Loc &start, const Loc &end) const { |
| 66 |
return end.offset - start.offset + end.extent; |
| 67 |
} |
| 68 |
|
| 69 |
std::string Parser::GetTokenAsString(const Token &token) const { |
| 70 |
return m_lexer.GetSource().substr(token.loc.offset, token.loc.extent); |
| 71 |
} |
| 72 |
|
| 73 |
////////////////////////////////////////////////////////////////////// |
| 74 |
// QUERYING (non-destructive lookahead) |
| 75 |
bool Parser::LookingAtFrom(int k, Lexeme lxm) const { |
| 76 |
return m_lexer.LookingAtFrom(k, lxm); |
| 77 |
} |
| 78 |
|
| 79 |
bool Parser::LookingAtSeq(std::initializer_list<Lexeme> lxms) const { |
| 80 |
int i = 0; |
| 81 |
for (Lexeme lxm : lxms) { |
| 82 |
if (!LookingAtFrom(i++, lxm)) { |
| 83 |
return false; |
| 84 |
} |
| 85 |
} |
| 86 |
return true; |
| 87 |
} |
| 88 |
|
| 89 |
bool Parser::LookingAtAnyOf(std::initializer_list<Lexeme> lxms) const { |
| 90 |
return LookingAtAnyOfFrom(0, lxms); |
| 91 |
} |
| 92 |
|
| 93 |
bool Parser::LookingAtAnyOfFrom(int i, |
| 94 |
std::initializer_list<Lexeme> lxms) const { |
| 95 |
int off = 0; |
| 96 |
for (Lexeme lxm : lxms) { |
| 97 |
if (LookingAtFrom(off + i, lxm)) { |
| 98 |
return true; |
| 99 |
} |
| 100 |
} |
| 101 |
return false; |
| 102 |
} |
| 103 |
bool Parser::LookingAtPrefix(const char *pfx) const { |
| 104 |
return PrefixAtEq(NextLoc().offset, pfx); |
| 105 |
} |
| 106 |
|
| 107 |
////////////////////////////////////////////////////////////////////// |
| 108 |
// CONSUMPTION (destructive lookahead) |
| 109 |
void Parser::ConsumeOrFail(Lexeme lxm, const char *msg) { |
| 110 |
if (!Consume(lxm)) { |
| 111 |
FailS(NextLoc(), msg); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
void Parser::ConsumeOrFailAfterPrev(Lexeme lxm, const char *msg) { |
| 116 |
if (!Consume(lxm)) { |
| 117 |
FailAfterPrev(msg); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
////////////////////////////////////////////////////////////////////// |
| 122 |
// IDENTIFIER and RAW STRING MANIPULATION |
| 123 |
bool Parser::PrefixAtEq(size_t off, const char *pfx) const { |
| 124 |
size_t slen = stringLength(pfx); |
| 125 |
if (off + slen > m_lexer.GetSource().size()) |
| 126 |
return false; |
| 127 |
return strncmp(pfx, &m_lexer.GetSource()[off], slen) == 0; |
| 128 |
} |
| 129 |
bool Parser::LookingAtIdentEq(const char *eq) const { |
| 130 |
return LookingAtIdentEq(0, eq); |
| 131 |
} |
| 132 |
bool Parser::LookingAtIdentEq(int k, const char *eq) const { |
| 133 |
const Token &t = Next(k); |
| 134 |
if (t.lexeme != Lexeme::IDENT) |
| 135 |
return false; |
| 136 |
return TokenEq(Next(k), eq); |
| 137 |
} |
| 138 |
bool Parser::LookingAtIdentEq(const Token &tk, const char *eq) const { |
| 139 |
if (tk.lexeme != Lexeme::IDENT) |
| 140 |
return false; |
| 141 |
return TokenEq(tk, eq); |
| 142 |
} |
| 143 |
|
| 144 |
bool Parser::ConsumeIdentEq(const char *eq) { |
| 145 |
if (LookingAtIdentEq(eq)) { |
| 146 |
Skip(); |
| 147 |
return true; |
| 148 |
} |
| 149 |
return false; |
| 150 |
} |
| 151 |
std::string Parser::ConsumeIdentOrFail(const char *what) { |
| 152 |
const Token &t = Next(0); |
| 153 |
if (t.lexeme != Lexeme::IDENT) { |
| 154 |
if (what == nullptr) { |
| 155 |
FailT("expected identifier"); |
| 156 |
} else { |
| 157 |
FailT("expected ", what); |
| 158 |
} |
| 159 |
} |
| 160 |
std::string id = GetTokenAsString(t); |
| 161 |
Skip(); |
| 162 |
return id; |
| 163 |
} |
| 164 |
bool Parser::TokenEq(const Token &tk, const char *eq) const { |
| 165 |
if (!eq) |
| 166 |
return false; |
| 167 |
size_t slen = strlen(eq); |
| 168 |
if (slen != tk.loc.extent || |
| 169 |
tk.loc.offset + slen > m_lexer.GetSource().size()) |
| 170 |
return false; |
| 171 |
const char *str = &m_lexer.GetSource()[tk.loc.offset]; |
| 172 |
return strncmp(eq, str, slen) == 0; |
| 173 |
// return strncmp(eq,&m_input[t.loc.offset],slen) == 0; |
| 174 |
} |
| 175 |
|
| 176 |
/////////////////////////////////////////////////////////////////////////// |
| 177 |
// NUMBERS |
| 178 |
|
| 179 |
// Examples: |
| 180 |
// 3.141 |
| 181 |
// .451 |
| 182 |
// 3.1e7 |
| 183 |
// 3e9 |
| 184 |
// 3e9.5 |
| 185 |
void Parser::ParseFltFrom(const Loc loc, double &value) { |
| 186 |
auto str = GetTokenAsString(); |
| 187 |
double x = 0.0; |
| 188 |
if (!ParseFLTLIT(str, x)) { |
| 189 |
// NOTE: this is an internal error since it indicates an |
| 190 |
// inconsistency between the lexical specification and the parser |
| 191 |
FailS(loc, "INTERNAL ERROR: parsing float literal (busted lexer?)"); |
| 192 |
} |
| 193 |
value = x; |
| 194 |
} |
| 195 |
} // namespace iga |