yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

Lauro OyenMove c++ parsing code from slang-cpp-extractor to static library (#5675)eaa8dcfcc

master
4.3 KiB153 linesraw
1#include "options.h"
2
3#include "diagnostics.h"
4
5namespace CppParse
6{
7
8SlangResult OptionsParser::_parseArgFlag(const char* option, bool& outFlag)
9{
10    SLANG_ASSERT(UnownedStringSlice(m_args[m_index]) == option);
11    SLANG_ASSERT(m_index < m_argCount);
12
13    m_index++;
14    outFlag = true;
15    return SLANG_OK;
16}
17
18SlangResult OptionsParser::_parseArgWithValue(const char* option, String& ioValue)
19{
20    SLANG_ASSERT(UnownedStringSlice(m_args[m_index]) == option);
21    if (m_index + 1 < m_argCount)
22    {
23        // Next parameter is the output path, there can only be one
24        if (ioValue.getLength())
25        {
26            // There already is output
27            m_sink->diagnose(SourceLoc(), CPPDiagnostics::optionAlreadyDefined, option, ioValue);
28            return SLANG_FAIL;
29        }
30    }
31    else
32    {
33        m_sink->diagnose(SourceLoc(), CPPDiagnostics::requireValueAfterOption, option);
34        return SLANG_FAIL;
35    }
36
37    ioValue = m_args[m_index + 1];
38    m_index += 2;
39    return SLANG_OK;
40}
41
42SlangResult OptionsParser::_parseArgReplaceValue(const char* option, String& ioValue)
43{
44    SLANG_ASSERT(UnownedStringSlice(m_args[m_index]) == option);
45    if (m_index + 1 >= m_argCount)
46    {
47        m_sink->diagnose(SourceLoc(), CPPDiagnostics::requireValueAfterOption, option);
48        return SLANG_FAIL;
49    }
50
51    ioValue = m_args[m_index + 1];
52    m_index += 2;
53    return SLANG_OK;
54}
55
56SlangResult OptionsParser::parse(
57    int argc,
58    const char* const* argv,
59    DiagnosticSink* sink,
60    Options& outOptions)
61{
62    outOptions.reset();
63
64    m_index = 0;
65    m_argCount = argc;
66    m_args = argv;
67    m_sink = sink;
68
69    outOptions.reset();
70
71    while (m_index < m_argCount)
72    {
73        const UnownedStringSlice arg = UnownedStringSlice(argv[m_index]);
74
75        if (arg.getLength() > 0 && arg[0] == '-')
76        {
77            if (arg == "-d")
78            {
79                SLANG_RETURN_ON_FAIL(_parseArgWithValue("-d", outOptions.m_inputDirectory));
80                continue;
81            }
82            else if (arg == "-o")
83            {
84                SLANG_RETURN_ON_FAIL(_parseArgWithValue("-o", outOptions.m_outputPath));
85                continue;
86            }
87            else if (arg == "-dump")
88            {
89                SLANG_RETURN_ON_FAIL(_parseArgFlag("-dump", outOptions.m_dump));
90                continue;
91            }
92            else if (arg == "-mark-prefix")
93            {
94                SLANG_RETURN_ON_FAIL(
95                    _parseArgReplaceValue("-mark-prefix", outOptions.m_markPrefix));
96                continue;
97            }
98            else if (arg == "-mark-suffix")
99            {
100                SLANG_RETURN_ON_FAIL(
101                    _parseArgReplaceValue("-mark-suffix", outOptions.m_markSuffix));
102                continue;
103            }
104            else if (arg == "-defs")
105            {
106                SLANG_RETURN_ON_FAIL(_parseArgFlag("-defs", outOptions.m_defs));
107                continue;
108            }
109            else if (arg == "-output-fields")
110            {
111                SLANG_RETURN_ON_FAIL(_parseArgFlag("-output-fields", outOptions.m_outputFields));
112                continue;
113            }
114            else if (arg == "-strip-prefix")
115            {
116                SLANG_RETURN_ON_FAIL(
117                    _parseArgWithValue("-strip-prefix", outOptions.m_stripFilePrefix));
118                continue;
119            }
120            else if (arg == "-unit-test")
121            {
122                SLANG_RETURN_ON_FAIL(_parseArgFlag("-unit-test", outOptions.m_runUnitTests));
123                continue;
124            }
125            else if (arg == "-unmarked")
126            {
127                bool unmarked;
128                SLANG_RETURN_ON_FAIL(_parseArgFlag("-unmarked", unmarked));
129                outOptions.m_requireMark = !unmarked;
130                continue;
131            }
132
133            m_sink->diagnose(SourceLoc(), CPPDiagnostics::unknownOption, arg);
134            return SLANG_FAIL;
135        }
136        else
137        {
138            // If it doesn't start with - then it's assumed to be an input path
139            outOptions.m_inputPaths.add(arg);
140            m_index++;
141        }
142    }
143
144    if (outOptions.m_inputPaths.getCount() < 0)
145    {
146        m_sink->diagnose(SourceLoc(), CPPDiagnostics::noInputPathsSpecified);
147        return SLANG_FAIL;
148    }
149
150    return SLANG_OK;
151}
152
153} // namespace CppParse