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 the line that the character S[Pos] is in.
24 static StringRef getLine(StringRef S, size_t Pos) {
25 size_t Begin = S.rfind('\n', Pos);
26 size_t End = S.find('\n', Pos);
27 Begin = (Begin == StringRef::npos) ? 0 : Begin + 1;
28 if (End == StringRef::npos)
30 // rtrim for DOS-style newlines.
31 return S.substr(Begin, End - Begin).rtrim();
34 void ScriptParserBase::printErrorPos() {
35 StringRef Tok = Tokens[Pos == 0 ? 0 : Pos - 1];
36 StringRef Line = getLine(Input, Tok.data() - Input.data());
37 size_t Col = Tok.data() - Line.data();
39 error(std::string(Col, ' ') + "^");
42 // We don't want to record cascading errors. Keep only the first one.
43 void ScriptParserBase::setError(const Twine &Msg) {
46 if (Input.empty() || Tokens.empty()) {
49 error("line " + Twine(getPos()) + ": " + Msg);
55 // Split S into linker script tokens.
56 std::vector<StringRef> ScriptParserBase::tokenize(StringRef S) {
57 std::vector<StringRef> Ret;
64 if (S.startswith("\"")) {
65 size_t E = S.find("\"", 1);
66 if (E == StringRef::npos) {
67 error("unclosed quote");
70 Ret.push_back(S.substr(1, E - 1));
76 size_t Pos = S.find_first_not_of(
77 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
78 "0123456789_.$/\\~=+[]*?-:!<>");
79 // A character that cannot start a word (which is usually a
80 // punctuation) forms a single character token.
83 Ret.push_back(S.substr(0, Pos));
88 // Skip leading whitespace characters or comments.
89 StringRef ScriptParserBase::skipSpace(StringRef S) {
91 if (S.startswith("/*")) {
92 size_t E = S.find("*/", 2);
93 if (E == StringRef::npos) {
94 error("unclosed comment in a linker script");
100 if (S.startswith("#")) {
101 size_t E = S.find('\n', 1);
102 if (E == StringRef::npos)
107 size_t Size = S.size();
109 if (S.size() == Size)
114 // An erroneous token is handled as if it were the last token before EOF.
115 bool ScriptParserBase::atEOF() { return Error || Tokens.size() == Pos; }
117 StringRef ScriptParserBase::next() {
121 setError("unexpected EOF");
124 return Tokens[Pos++];
127 StringRef ScriptParserBase::peek() {
128 StringRef Tok = next();
135 bool ScriptParserBase::skip(StringRef Tok) {
139 setError("unexpected EOF");
142 if (Tokens[Pos] != Tok)
148 void ScriptParserBase::expect(StringRef Expect) {
151 StringRef Tok = next();
153 setError(Expect + " expected, but got " + Tok);
156 // Returns the current line number.
157 size_t ScriptParserBase::getPos() {
160 const char *Begin = Input.data();
161 const char *Tok = Tokens[Pos - 1].data();
162 return StringRef(Begin, Tok - Begin).count('\n') + 1;