• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

antonvw / wex / 18993700065

01 Nov 2025 07:56AM UTC coverage: 64.309% (+0.08%) from 64.227%
18993700065

push

github

antonvw
added separate files

18406 of 31387 branches covered (58.64%)

Branch coverage included in aggregate %.

14638 of 19996 relevant lines covered (73.2%)

1530.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

86.76
/src/ex/command-parser.cpp
1
////////////////////////////////////////////////////////////////////////////////
2
// Name:      command-parser.cpp
3
// Purpose:   Implementation of class wex::command_parser
4
// Author:    Anton van Wezenbeek
5
// Copyright: (c) 2021-2025 Anton van Wezenbeek
6
////////////////////////////////////////////////////////////////////////////////
7

8
#include <boost/algorithm/string.hpp>
9
#include <wex/core/log.h>
10
#include <wex/ex/addressrange.h>
11
#include <wex/ex/command-parser.h>
12
#include <wex/ex/ex.h>
13
#include <wex/factory/control.h>
14
#include <wex/syntax/stc.h>
15

16
wex::command_parser::command_parser(
2,265✔
17
  ex*                ex,
18
  const std::string& text,
19
  parse_t            type)
2,265✔
20
  : command_parser_data(text)
21
  , m_ex(ex)
2,265✔
22
{
23
  m_is_ok = m_text.empty() ? false : parse(type);
2,265✔
24
}
2,265✔
25

26
bool wex::command_parser::parse(parse_t type)
2,264✔
27
{
28
  if (m_text.starts_with(ex_command::selection_range()))
2,264✔
29
  {
30
    if (!parse_selection())
14✔
31
    {
32
      return false;
2✔
33
    }
34
  }
35
  else if (!parse_other())
2,250!
36
  {
37
    return false;
×
38
  }
39

40
  if (type == parse_t::CHECK)
2,262✔
41
  {
42
    return true;
3✔
43
  }
44

45
  try
46
  {
47
    switch (m_type)
2,259!
48
    {
49
      case address_t::NO_ADDR:
85✔
50
      {
51
        m_range.clear();
85✔
52
        const auto line(address(m_ex, m_text).get_line());
85✔
53
        return m_ex->get_stc()->inject(data::control().line(line));
85✔
54
      }
55

56
      case address_t::ONE_ADDR:
1,735✔
57
        return address(m_ex).parse(*this);
1,735✔
58

59
      case address_t::TWO_ADDR:
439✔
60
        if (info_message_t im; !addressrange(m_ex, m_range).parse(*this, im))
439✔
61
        {
62
          return false;
12✔
63
        }
64
        else if (im != info_message_t::NONE)
427✔
65
        {
66
          m_ex->info_message(m_ex->register_text(), im);
95✔
67
        }
68
        break;
427✔
69

70
      default:
×
71
        assert(0);
×
72
    }
73
  }
74
  catch (std::exception& e)
×
75
  {
76
    log(e) << m_cmd;
×
77
    return false;
×
78
  }
×
79

80
  m_ex->m_command_parsed_data = *this;
427✔
81

82
  return true;
427✔
83
}
84

85
bool wex::command_parser::parse_other()
2,250✔
86
{
87
  marker_and_register_expansion(m_ex, m_text);
2,250✔
88

89
  // Addressing in ex.
90
  // See also address::get_line
91
  const std::string addr(
92
    // (1) . (2) $ (3) decimal number, + or - (7)
93
    "[\\.\\$0-9\\+\\-]+|"
94
    // (4) marker
95
    "'[a-z]|"
96
    // (5) (6) regex find, non-greedy!
97
    "[-+]?[0-9]*[\\?/].*?[\\?/][-+]?[0-9]*");
2,250✔
98

99
  const auto& cmds_1addr(address(m_ex).regex_commands());
2,250✔
100
  const auto& cmds_2addr(addressrange(m_ex).regex_commands());
2,250✔
101

102
  if (regex v({// 2addr % range
2,250✔
103
               {"^%" + cmds_2addr,
×
104
                [&](const regex::match_t& m)
×
105
                {
106
                  m_type  = address_t::TWO_ADDR;
40✔
107
                  m_range = "%";
40✔
108
                  m_cmd   = m[0];
40✔
109
                  m_text  = m[1];
40✔
110
                }},
40✔
111
               // 1addr (or none)
112
               {"^(" + addr + ")?" + cmds_1addr,
4,500✔
113
                [&](const regex::match_t& m)
×
114
                {
115
                  m_type  = address_t::ONE_ADDR;
1,735✔
116
                  m_range = m[0];
1,735✔
117
                  m_cmd   = m[1];
1,735✔
118
                  m_text  = boost::algorithm::trim_left_copy(m[2]);
1,735✔
119
                  log::trace("ex 1addr") << m_range;
1,735✔
120
                }},
1,735✔
121
               // 2addr
122
               {"^(" + addr + "),(" + addr + ")" + cmds_2addr,
4,500✔
123
                [&](const regex::match_t& m)
×
124
                {
125
                  m_type  = address_t::TWO_ADDR;
122✔
126
                  m_range = m[0] + "," + m[1];
122✔
127
                  m_cmd   = m[2];
122✔
128
                  m_text  = m[3];
122✔
129
                  log::trace("ex 2addr") << m_range;
122✔
130
                }},
122✔
131
               // 2addr
132
               {"^(" + addr + ")?" + cmds_2addr,
4,500✔
133
                [&](const regex::match_t& m)
2,250✔
134
                {
135
                  m_type  = address_t::TWO_ADDR;
267✔
136
                  m_range = m[0];
267✔
137
                  m_cmd   = m[1];
267✔
138
                  m_text  = m[2];
267✔
139
                  log::trace("ex 2addr") << m_range;
267✔
140
                }}});
13,767!
141
      v.match(m_text) <= 1)
2,250✔
142
  {
143
    m_type = address_t::NO_ADDR;
86✔
144
  }
2,250✔
145

146
  if (m_range.empty() && m_cmd != "!")
2,250✔
147
  {
148
    m_range =
149
      (m_cmd.starts_with("g") || m_cmd == "v" || m_cmd == "w" ? "%" : ".");
1,840✔
150
  }
151

152
  return true;
2,250✔
153
}
11,250!
154

155
bool wex::command_parser::parse_selection()
14✔
156
{
157
  if (m_ex->get_stc()->get_selected_text().empty())
14✔
158
  {
159
    return false;
1✔
160
  }
161

162
  const auto& cmds_2addr(addressrange(m_ex).regex_commands());
13✔
163

164
  if (regex r(
13✔
165
        {{ex_command::selection_range() + cmds_2addr,
26✔
166
          [&](const regex::match_t& m)
13✔
167
          {
168
            m_type  = address_t::TWO_ADDR;
12✔
169
            m_range = ex_command::selection_range();
12✔
170
            m_cmd   = m[0];
12✔
171
            m_text  = m[1];
12✔
172
            log::trace("ex selection") << m_range;
12✔
173
          }}});
51!
174
      r.match(m_text) == 2)
13✔
175
  {
176
    return true;
12✔
177
  }
13✔
178

179
  return false;
1✔
180
}
39!
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc