Skip to content

Commit

Permalink
Add WithCoord flag to ParameterDesc
Browse files Browse the repository at this point in the history
Adding the `WithCoord` flag will make the `ParametersParser` try to
parse a special `+<line>:<column>` VIM-style switch.
  • Loading branch information
pjungkamp committed Jun 16, 2024
1 parent 7e9c4b7 commit 0cdb088
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
20 changes: 20 additions & 0 deletions src/parameters_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ ParametersParser::ParametersParser(ParameterList params, const ParameterDesc& de
const bool switches_only_at_start = desc.flags & ParameterDesc::Flags::SwitchesOnlyAtStart;
const bool ignore_unknown_switches = desc.flags & ParameterDesc::Flags::IgnoreUnknownSwitches;
bool only_pos = desc.flags & ParameterDesc::Flags::SwitchesAsPositional;
bool with_coord = desc.flags & ParameterDesc::Flags::WithCoord;

Vector<bool> switch_seen(desc.switches.size(), false);
for (size_t i = 0; i < params.size(); ++i)
Expand All @@ -40,6 +41,25 @@ ParametersParser::ParametersParser(ParameterList params, const ParameterDesc& de
m_state = State::Switch;
only_pos = true;
}
else if (not only_pos and with_coord and not params[i].empty() and params[i][0_byte] == '+')
{
m_state = State::Switch;
with_coord = false;
const auto coord_str = params[i].substr(1_byte);
const auto colon = find(coord_str, ':');

const auto line_str = StringView{coord_str.begin(), colon};
const LineCount line = line_str.empty() ? INT_MAX : std::max(1, str_to_int(line_str)) - 1;

ByteCount column = 0;
if (colon != coord_str.end())
{
const auto column_str = StringView{colon + 1, coord_str.end()};
column = column_str.empty() ? INT_MAX : std::max(1, str_to_int(column_str)) - 1;
}

m_coord = BufferCoord{line, column};
}
else if (not only_pos and not params[i].empty() and params[i][0_byte] == '-')
{
StringView switch_name = params[i].substr(1_byte);
Expand Down
6 changes: 5 additions & 1 deletion src/parameters_parser.hh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "hash_map.hh"
#include "meta.hh"
#include "array_view.hh"
#include "coord.hh"
#include "optional.hh"
#include "flags.hh"
#include "string.hh"
Expand Down Expand Up @@ -62,7 +63,8 @@ struct ParameterDesc
None = 0,
SwitchesOnlyAtStart = 0b0001,
SwitchesAsPositional = 0b0010,
IgnoreUnknownSwitches = 0b0100
IgnoreUnknownSwitches = 0b0100,
WithCoord = 0b1000,
};
friend constexpr bool with_bit_ops(Meta::Type<Flags>) { return true; }

Expand Down Expand Up @@ -142,12 +144,14 @@ struct ParametersParser
iterator end() const { return iterator(*this, m_positional_indices.size()); }

State state() const { return *m_state; }
Optional<BufferCoord> get_coord() const { return m_coord; }

private:
ParameterList m_params;
Vector<size_t, MemoryDomain::Commands> m_positional_indices;
HashMap<String, StringView> m_switches;
Optional<State> m_state;
Optional<BufferCoord> m_coord;
};

}
Expand Down

0 comments on commit 0cdb088

Please sign in to comment.