Parse unknown switches as positional for region highlighters

This commit is contained in:
Maxime Coste 2018-06-29 08:36:06 +10:00
parent 40211d1e3b
commit 96c9718144
5 changed files with 23 additions and 10 deletions

View File

@ -17,7 +17,7 @@ add-highlighter shared/haskell/string region (?<!'\\)(?<!')"
add-highlighter shared/haskell/macro region ^\h*?\K# (?<!\\)\n '' fill meta
add-highlighter shared/haskell/pragma region \{-# '#-\}' \{- fill meta
add-highlighter shared/haskell/comment region \{- -\} \{- fill comment
add-highlighter shared/haskell/line_comment region -- --(?:[^!#$%&*+./<>?@\\\^|~=]|$) $ '' fill comment
add-highlighter shared/haskell/line_comment region --(?:[^!#$%&*+./<>?@\\\^|~=]|$) $ '' fill comment
add-highlighter shared/haskell/code/ regex (?<!')\b0x+[A-Fa-f0-9]+ 0:value
add-highlighter shared/haskell/code/ regex (?<!')\b\d+([.]\d+)? 0:value

View File

@ -15,9 +15,9 @@ add-highlighter shared/lua regions
add-highlighter shared/lua/code default-region group
add-highlighter shared/lua/double_string region '"' (?<!\\)(?:\\\\)*" '' fill string
add-highlighter shared/lua/single_string region "'" (?<!\\)(?:\\\\)*' '' fill string
add-highlighter shared/lua/comment region -- '--' $ '' fill comment
add-highlighter shared/lua/comment region '--' $ '' fill comment
add-highlighter shared/lua/raw_string region -match-capture '\[(=*)\[' '\](=*)\]' '' fill string
add-highlighter shared/lua/raw_comment region -match-capture -- '--\[(=*)\[' '\](=*)\]' '' fill comment
add-highlighter shared/lua/raw_comment region -match-capture '--\[(=*)\[' '\](=*)\]' '' fill comment
add-highlighter shared/lua/code/ regex \b(and|break|do|else|elseif|end|false|for|function|goto|if|in|local|nil|not|or|repeat|return|then|true|until|while)\b 0:keyword

View File

@ -1860,7 +1860,8 @@ public:
static const ParameterDesc param_desc{
{ { "match-capture", { false, "" } } },
ParameterDesc::Flags::SwitchesOnlyAtStart, 4
ParameterDesc::Flags::SwitchesOnlyAtStart | ParameterDesc::Flags::IgnoreUnknownSwitches,
4
};
ParametersParser parser{params, param_desc};

View File

@ -25,22 +25,33 @@ String generate_switches_doc(const SwitchMap& switches)
return res;
}
ParametersParser::ParametersParser(ParameterList params,
const ParameterDesc& desc)
ParametersParser::ParametersParser(ParameterList params, const ParameterDesc& desc)
: m_params(params),
m_desc(desc)
{
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;
Vector<bool> switch_seen(desc.switches.size(), false);
for (size_t i = 0; i < params.size(); ++i)
{
if (not only_pos and params[i] == "--")
if (not only_pos and not ignore_unknown_switches and params[i] == "--")
only_pos = true;
else if (not only_pos and not params[i].empty() and params[i][0_byte] == '-')
{
auto it = m_desc.switches.find(params[i].substr(1_byte));
if (it == m_desc.switches.end())
{
if (ignore_unknown_switches)
{
m_positional_indices.push_back(i);
if (switches_only_at_start)
only_pos = true;
continue;
}
throw unknown_option(params[i]);
}
auto switch_index = it - m_desc.switches.begin();
if (switch_seen[switch_index])
@ -52,7 +63,7 @@ ParametersParser::ParametersParser(ParameterList params,
}
else // positional
{
if (desc.flags & ParameterDesc::Flags::SwitchesOnlyAtStart)
if (switches_only_at_start)
only_pos = true;
m_positional_indices.push_back(i);
}

View File

@ -52,8 +52,9 @@ struct ParameterDesc
enum class Flags
{
None = 0,
SwitchesOnlyAtStart = 1,
SwitchesAsPositional = 2,
SwitchesOnlyAtStart = 0b0001,
SwitchesAsPositional = 0b0010,
IgnoreUnknownSwitches = 0b0100
};
friend constexpr bool with_bit_ops(Meta::Type<Flags>) { return true; }