2017-02-14 04:47:05 +00:00
|
|
|
//===- ScriptLexer.h --------------------------------------------*- C++ -*-===//
|
2016-04-06 20:59:11 +00:00
|
|
|
//
|
2019-01-19 08:50:56 +00:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2016-04-06 20:59:11 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-02-14 04:47:05 +00:00
|
|
|
#ifndef LLD_ELF_SCRIPT_LEXER_H
|
|
|
|
#define LLD_ELF_SCRIPT_LEXER_H
|
2016-04-06 20:59:11 +00:00
|
|
|
|
2017-10-02 21:00:41 +00:00
|
|
|
#include "lld/Common/LLVM.h"
|
2024-07-27 17:25:13 -07:00
|
|
|
#include "llvm/ADT/DenseSet.h"
|
2024-07-26 14:26:38 -07:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2016-04-06 20:59:11 +00:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2022-02-07 21:53:34 -08:00
|
|
|
#include "llvm/Support/MemoryBufferRef.h"
|
2016-04-06 21:33:18 +00:00
|
|
|
#include <vector>
|
2016-04-06 20:59:11 +00:00
|
|
|
|
2022-08-10 15:31:58 -04:00
|
|
|
namespace lld::elf {
|
2024-09-21 11:06:06 -07:00
|
|
|
struct Ctx;
|
2016-04-06 20:59:11 +00:00
|
|
|
|
2017-02-14 04:47:05 +00:00
|
|
|
class ScriptLexer {
|
2024-07-26 14:26:38 -07:00
|
|
|
protected:
|
|
|
|
struct Buffer {
|
|
|
|
// The remaining content to parse and the filename.
|
|
|
|
StringRef s, filename;
|
|
|
|
const char *begin = nullptr;
|
2024-07-27 14:46:41 -07:00
|
|
|
size_t lineNumber = 1;
|
2024-07-28 11:43:27 -07:00
|
|
|
// True if the script is opened as an absolute path under the --sysroot
|
|
|
|
// directory.
|
|
|
|
bool isUnderSysroot = false;
|
|
|
|
|
2024-07-26 14:26:38 -07:00
|
|
|
Buffer() = default;
|
2024-09-21 11:06:06 -07:00
|
|
|
Buffer(Ctx &ctx, MemoryBufferRef mb);
|
2024-07-26 14:26:38 -07:00
|
|
|
};
|
2024-11-14 22:17:10 -08:00
|
|
|
Ctx &ctx;
|
2024-07-26 14:26:38 -07:00
|
|
|
// The current buffer and parent buffers due to INCLUDE.
|
|
|
|
Buffer curBuf;
|
|
|
|
SmallVector<Buffer, 0> buffers;
|
|
|
|
|
2024-07-27 17:25:13 -07:00
|
|
|
// Used to detect INCLUDE() cycles.
|
|
|
|
llvm::DenseSet<StringRef> activeFilenames;
|
|
|
|
|
2024-07-26 17:13:37 -07:00
|
|
|
struct Token {
|
|
|
|
StringRef str;
|
|
|
|
explicit operator bool() const { return !str.empty(); }
|
|
|
|
operator StringRef() const { return str; }
|
|
|
|
};
|
|
|
|
|
2024-07-26 14:26:38 -07:00
|
|
|
// The token before the last next().
|
|
|
|
StringRef prevTok;
|
|
|
|
// Rules for what is a token are different when we are in an expression.
|
|
|
|
// curTok holds the cached return value of peek() and is invalid when the
|
|
|
|
// expression state changes.
|
|
|
|
StringRef curTok;
|
2024-07-27 14:46:41 -07:00
|
|
|
size_t prevTokLine = 1;
|
2024-07-26 14:26:38 -07:00
|
|
|
// The inExpr state when curTok is cached.
|
|
|
|
bool curTokState = false;
|
|
|
|
bool eof = false;
|
|
|
|
|
2016-04-06 20:59:11 +00:00
|
|
|
public:
|
2024-09-21 11:06:06 -07:00
|
|
|
explicit ScriptLexer(Ctx &ctx, MemoryBufferRef mb);
|
2016-04-06 20:59:11 +00:00
|
|
|
|
|
|
|
void setError(const Twine &msg);
|
2024-07-26 14:26:38 -07:00
|
|
|
void lex();
|
2020-07-22 12:48:16 +03:00
|
|
|
StringRef skipSpace(StringRef s);
|
2016-04-06 20:59:11 +00:00
|
|
|
bool atEOF();
|
|
|
|
StringRef next();
|
2017-03-09 19:23:00 +00:00
|
|
|
StringRef peek();
|
2016-10-17 06:21:13 +00:00
|
|
|
void skip();
|
2016-10-17 16:01:53 +00:00
|
|
|
bool consume(StringRef tok);
|
2016-04-06 20:59:11 +00:00
|
|
|
void expect(StringRef expect);
|
2024-07-26 17:13:37 -07:00
|
|
|
Token till(StringRef tok);
|
2016-12-01 04:36:49 +00:00
|
|
|
std::string getCurrentLocation();
|
2024-01-22 09:09:46 -08:00
|
|
|
MemoryBufferRef getCurrentMB();
|
2016-04-06 20:59:11 +00:00
|
|
|
|
2016-11-21 15:49:56 +00:00
|
|
|
std::vector<MemoryBufferRef> mbs;
|
2017-02-15 19:58:17 +00:00
|
|
|
bool inExpr = false;
|
2016-11-21 15:49:56 +00:00
|
|
|
|
|
|
|
private:
|
2016-12-01 04:36:49 +00:00
|
|
|
StringRef getLine();
|
|
|
|
size_t getColumnNumber();
|
2016-04-06 20:59:11 +00:00
|
|
|
};
|
|
|
|
|
2022-08-10 15:31:58 -04:00
|
|
|
} // namespace lld::elf
|
2016-04-06 20:59:11 +00:00
|
|
|
|
|
|
|
#endif
|