1 //===- ScriptParser.cpp ---------------------------------------------------===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the base parser class for linker script and dynamic
13 //===----------------------------------------------------------------------===//
15 #include "ScriptParser.h"
17 #include "llvm/ADT/Twine.h"
21 using namespace lld::elf;
23 // Returns a whole line containing the current token.
24 StringRef ScriptParserBase::getLine() {
25 StringRef S = getCurrentMB().getBuffer();
26 StringRef Tok = Tokens[Pos - 1];
28 size_t Pos = S.rfind('\n', Tok.data() - S.data());
29 if (Pos != StringRef::npos)
30 S = S.substr(Pos + 1);
31 return S.substr(0, S.find_first_of("\r\n"));
34 // Returns 1-based line number of the current token.
35 size_t ScriptParserBase::getLineNumber() {
36 StringRef S = getCurrentMB().getBuffer();
37 StringRef Tok = Tokens[Pos - 1];
38 return S.substr(0, Tok.data() - S.data()).count('\n') + 1;
41 // Returns 0-based column number of the current token.
42 size_t ScriptParserBase::getColumnNumber() {
43 StringRef Tok = Tokens[Pos - 1];
44 return Tok.data() - getLine().data();
47 std::string ScriptParserBase::getCurrentLocation() {
48 std::string Filename = getCurrentMB().getBufferIdentifier();
51 return (Filename + ":" + Twine(getLineNumber())).str();
54 ScriptParserBase::ScriptParserBase(MemoryBufferRef MB) { tokenize(MB); }
56 // We don't want to record cascading errors. Keep only the first one.
57 void ScriptParserBase::setError(const Twine &Msg) {
63 error(getCurrentLocation() + ": " + Msg);
67 std::string S = getCurrentLocation() + ": ";
70 error(S + std::string(getColumnNumber(), ' ') + "^");
73 // Split S into linker script tokens.
74 void ScriptParserBase::tokenize(MemoryBufferRef MB) {
75 std::vector<StringRef> Vec;
77 StringRef S = MB.getBuffer();
85 // Quoted token. Note that double-quote characters are parts of a token
86 // because, in a glob match context, only unquoted tokens are interpreted
87 // as glob patterns. Double-quoted tokens are literal patterns in that
89 if (S.startswith("\"")) {
90 size_t E = S.find("\"", 1);
91 if (E == StringRef::npos) {
92 StringRef Filename = MB.getBufferIdentifier();
93 size_t Lineno = Begin.substr(0, S.data() - Begin.data()).count('\n');
94 error(Filename + ":" + Twine(Lineno + 1) + ": unclosed quote");
98 Vec.push_back(S.take_front(E + 1));
103 // Unquoted token. This is more relaxed than tokens in C-like language,
104 // so that you can write "file-name.cpp" as one bare token, for example.
105 size_t Pos = S.find_first_not_of(
106 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
107 "0123456789_.$/\\~=+[]*?-:!<>^");
109 // A character that cannot start a word (which is usually a
110 // punctuation) forms a single character token.
113 Vec.push_back(S.substr(0, Pos));
117 Tokens.insert(Tokens.begin() + Pos, Vec.begin(), Vec.end());
120 // Skip leading whitespace characters or comments.
121 StringRef ScriptParserBase::skipSpace(StringRef S) {
123 if (S.startswith("/*")) {
124 size_t E = S.find("*/", 2);
125 if (E == StringRef::npos) {
126 error("unclosed comment in a linker script");
132 if (S.startswith("#")) {
133 size_t E = S.find('\n', 1);
134 if (E == StringRef::npos)
139 size_t Size = S.size();
141 if (S.size() == Size)
146 // An erroneous token is handled as if it were the last token before EOF.
147 bool ScriptParserBase::atEOF() { return Error || Tokens.size() == Pos; }
149 StringRef ScriptParserBase::next() {
153 setError("unexpected EOF");
156 return Tokens[Pos++];
159 StringRef ScriptParserBase::peek() {
160 StringRef Tok = next();
167 bool ScriptParserBase::consume(StringRef Tok) {
175 void ScriptParserBase::skip() { (void)next(); }
177 void ScriptParserBase::expect(StringRef Expect) {
180 StringRef Tok = next();
182 setError(Expect + " expected, but got " + Tok);
185 // Returns true if S encloses T.
186 static bool encloses(StringRef S, StringRef T) {
187 return S.bytes_begin() <= T.bytes_begin() && T.bytes_end() <= S.bytes_end();
190 MemoryBufferRef ScriptParserBase::getCurrentMB() {
191 // Find input buffer containing the current token.
192 assert(!MBs.empty());
196 for (MemoryBufferRef MB : MBs)
197 if (encloses(MB.getBuffer(), Tokens[Pos - 1]))
199 llvm_unreachable("getCurrentMB: failed to find a token");