blob: da262e9bb58498139aad974f5503fc72fc2c8382 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
#include "slang-command-line-args.h"
#include "../core/slang-process-util.h"
#include "../core/slang-string-escape-util.h"
#include "slang-core-diagnostics.h"
namespace Slang {
void CommandLineArgs::setArgs(const char*const* args, size_t argCount)
{
m_args.clear();
const SourceLoc startLoc = m_sourceManager->getNextRangeStart();
StringBuilder buf;
auto escapeHandler = ProcessUtil::getEscapeHandler();
for (size_t i = 0; i < argCount; ++i)
{
const Index offset = buf.getLength();
const char* srcArg = args[i];
Arg dstArg;
dstArg.loc = startLoc + offset;
dstArg.value = srcArg;
m_args.add(dstArg);
// Write the string escaped if necessary
StringEscapeUtil::appendMaybeQuoted(escapeHandler, dstArg.value.getUnownedSlice(), buf);
// Put a space between the args
buf << " ";
}
SourceFile* sourceFile = m_sourceManager->createSourceFileWithString(PathInfo::makeUnknown(), buf.ProduceString());
m_sourceView = m_sourceManager->createSourceView(sourceFile, nullptr, SourceLoc::fromRaw(0));
SLANG_ASSERT(m_sourceView->getRange().begin == startLoc);
}
/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
CommandLineReader
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
String CommandLineReader::getPreviousValue() const
{
SLANG_ASSERT(m_index > 0);
if (m_index > 0)
{
const auto& prevArg = (*m_args)[m_index - 1];
return prevArg.value;
}
else
{
return String();
}
}
SlangResult CommandLineReader::expectArg(String& outArg)
{
if (hasArg())
{
outArg = m_args->m_args[m_index++].value;
return SLANG_OK;
}
else
{
m_sink->diagnose(peekLoc(), MiscDiagnostics::expectedArgumentForOption, getPreviousValue());
return SLANG_FAIL;
}
}
SlangResult CommandLineReader::expectArg(CommandLineArg& outArg)
{
if (hasArg())
{
outArg = peekArg();
advance();
return SLANG_OK;
}
else
{
m_sink->diagnose(peekLoc(), MiscDiagnostics::expectedArgumentForOption, getPreviousValue());
return SLANG_FAIL;
}
}
} // namespace Slang
|