summaryrefslogtreecommitdiff
path: root/source/compiler-core/slang-command-line-args.h
diff options
context:
space:
mode:
authorjsmall-nvidia <jsmall@nvidia.com>2021-05-19 17:53:24 -0400
committerGitHub <noreply@github.com>2021-05-19 14:53:24 -0700
commitc4c90f5a6da45229405533372215ba40de91df37 (patch)
treeb9fdf847656199c5f9b34f081d37ff7f466b7a6d /source/compiler-core/slang-command-line-args.h
parent61e9154cb797cffe19cfbf3205b4a5a614e8b552 (diff)
SourceLoc use in command line processing (#1848)
* #include an absolute path didn't work - because paths were taken to always be relative. * Added SourceLoc handling for command line parsing. * Fix typo in debug. * Fix issue around the DiagnosticSink used in options parsing not having a writer available - by having DiagnosticSink parenting. * Small rename for clarity. Co-authored-by: T. Foley <tfoleyNV@users.noreply.github.com>
Diffstat (limited to 'source/compiler-core/slang-command-line-args.h')
-rw-r--r--source/compiler-core/slang-command-line-args.h95
1 files changed, 95 insertions, 0 deletions
diff --git a/source/compiler-core/slang-command-line-args.h b/source/compiler-core/slang-command-line-args.h
new file mode 100644
index 000000000..6dea7408c
--- /dev/null
+++ b/source/compiler-core/slang-command-line-args.h
@@ -0,0 +1,95 @@
+#ifndef SLANG_COMMAND_LINE_ARGS_H
+#define SLANG_COMMAND_LINE_ARGS_H
+
+// This file defines the `Name` type, used to represent
+// the name of types, variables, etc. in the AST.
+
+#include "../core/slang-basic.h"
+
+#include "slang-source-loc.h"
+#include "slang-diagnostic-sink.h"
+
+namespace Slang {
+
+struct CommandLineArg
+{
+ String value; ///< The value of the arg
+ SourceLoc loc; ///< The location of the arg
+};
+
+struct CommandLineArgs
+{
+ typedef CommandLineArg Arg;
+
+ SLANG_FORCE_INLINE Index getArgCount() const { return m_args.getCount(); }
+ const Arg& operator[](Index i) const { return m_args[i]; }
+
+ const Arg* begin() const { return m_args.begin(); }
+ const Arg* end() const { return m_args.end(); }
+
+ /// NOTE! Should NOT include the executable name
+ void setArgs(const char*const* args, size_t argCount);
+
+ /// Ctor with a source manager
+ CommandLineArgs(SourceManager* manager):
+ m_sourceManager(manager),
+ m_sourceView(nullptr)
+ {
+ }
+
+ String m_executablePath; ///< Can be optionally be set
+
+ List<Arg> m_args; ///< The args
+ SourceManager* m_sourceManager; ///< The source manager and associated diagnostics sink
+ SourceView* m_sourceView; ///< contains the command line as source
+};
+
+struct CommandLineReader
+{
+ /// Peek the current location
+ SourceLoc peekLoc() const { return m_index < m_args->getArgCount() ? (*m_args)[m_index].loc : SourceLoc(); }
+ /// Peek the current arg
+ const CommandLineArg& peekArg() const { SLANG_ASSERT(hasArg()); return (*m_args)[m_index]; }
+
+ /// Peek the string value at that position
+ const String& peekValue() const { SLANG_ASSERT(hasArg()); return (*m_args)[m_index].value; }
+
+ /// Get the arg and advance
+ CommandLineArg getArgAndAdvance() { CommandLineArg arg(peekArg()); advance(); return arg; }
+
+ const String& getValueAndAdvance() { const String& value = peekValue(); advance(); return value; }
+
+ /// True if at end
+ bool atEnd() const { return m_index >= m_args->getArgCount(); }
+ /// True if has a current arg
+ bool hasArg() const { return !atEnd(); }
+
+ /// Advance to next arg
+ void advance() { SLANG_ASSERT(m_index < m_args->getArgCount()); m_index++; }
+ /// Removes arg at current position
+ void removeArg() { SLANG_ASSERT(hasArg()); m_args->m_args.removeAt(m_index); }
+
+ /// Get the value from the arg previous to the current position. Will assert if there isn't one.
+ String getPreviousValue() const;
+
+ /// If there is an arg outArg is set and advanced
+ /// Note, this *assumes* the previous arg is the option that initated this
+ SlangResult expectArg(String& outArg);
+ SlangResult expectArg(CommandLineArg& outArg);
+
+ /// Set up reader with args
+ CommandLineReader(CommandLineArgs* args, DiagnosticSink* sink):
+ m_args(args),
+ m_index(0),
+ m_sink(sink)
+ {
+ }
+
+ DiagnosticSink* m_sink;
+ CommandLineArgs* m_args;
+ Index m_index;
+};
+
+} // namespace Slang
+
+#endif