cpp

Coverage Report

Created: 2022-11-10 11:34

/home/andy/git/oilshell/oil/cpp/leaky_frontend_match.cc
Line
Count
Source (jump to first uncovered line)
1
// frontend_match.cc: manual port of frontend/match
2
3
#include "leaky_frontend_match.h"
4
5
// This order is required to get it to compile, despite clang-format
6
// clang-format off
7
#include "_gen/frontend/types.asdl_c.h"
8
#include "_gen/frontend/id_kind.asdl_c.h"
9
#include "_gen/frontend/match.re2c.h"
10
// clang-format on
11
12
namespace match {
13
14
0
Tuple2<Id_t, int> OneToken(lex_mode_t lex_mode, Str* line, int start_pos) {
15
0
  int id;
16
0
  int end_pos;
17
18
  // TODO: get rid of these casts
19
0
  MatchOshToken(static_cast<int>(lex_mode),
20
0
                reinterpret_cast<const unsigned char*>(line->data_), len(line),
21
0
                start_pos, &id, &end_pos);
22
0
  return Tuple2<Id_t, int>(static_cast<Id_t>(id), end_pos);
23
0
}
24
25
7
Tuple2<Id_t, Str*> SimpleLexer::Next() {
26
7
  int id;
27
7
  int end_pos;
28
7
  match_func_(reinterpret_cast<const unsigned char*>(s_->data_), len(s_), pos_,
29
7
              &id, &end_pos);
30
31
7
  int len = end_pos - pos_;
32
7
  Str* val = NewStr(len);                     // TODO: return value rooting
33
7
  memcpy(val->data_, s_->data_ + pos_, len);  // copy the list item
34
7
  val->data_[len] = '\0';
35
36
7
  pos_ = end_pos;
37
7
  return Tuple2<Id_t, Str*>(static_cast<Id_t>(id), val);
38
7
}
39
40
namespace Id = id_kind_asdl::Id;
41
42
0
List<Tuple2<Id_t, Str*>*>* SimpleLexer::Tokens() {
43
0
  auto tokens = NewList<Tuple2<Id_t, Str*>*>();
44
0
  while (true) {
45
0
    auto tup2 = Next();
46
0
    if (tup2.at0() == Id::Eol_Tok) {
47
0
      break;
48
0
    }
49
    // it's annoying that we have to put it on the heap
50
0
    tokens->append(Alloc<Tuple2<Id_t, Str*>>(tup2.at0(), tup2.at1()));
51
0
  }
52
0
  return tokens;
53
0
}
54
55
2
SimpleLexer* BraceRangeLexer(Str* s) {
56
2
  return Alloc<SimpleLexer>(&MatchBraceRangeToken, s);
57
2
}
58
59
0
SimpleLexer* GlobLexer(Str* s) {
60
0
  return Alloc<SimpleLexer>(&MatchGlobToken, s);
61
0
}
62
63
0
SimpleLexer* EchoLexer(Str* s) {
64
0
  return Alloc<SimpleLexer>(&MatchEchoToken, s);
65
0
}
66
67
0
List<Tuple2<Id_t, Str*>*>* HistoryTokens(Str* s) {
68
0
  SimpleLexer lexer(&MatchHistoryToken, s);
69
0
  return lexer.Tokens();
70
0
}
71
72
0
List<Tuple2<Id_t, Str*>*>* Ps1Tokens(Str* s) {
73
0
  SimpleLexer lexer(&MatchPS1Token, s);
74
0
  return lexer.Tokens();
75
0
}
76
77
3
Id_t BracketUnary(Str* s) {
78
3
  return ::BracketUnary(reinterpret_cast<const unsigned char*>(s->data_),
79
3
                        len(s));
80
3
}
81
3
Id_t BracketBinary(Str* s) {
82
3
  return ::BracketBinary(reinterpret_cast<const unsigned char*>(s->data_),
83
3
                         len(s));
84
3
}
85
0
Id_t BracketOther(Str* s) {
86
0
  return ::BracketOther(reinterpret_cast<const unsigned char*>(s->data_),
87
0
                        len(s));
88
0
}
89
90
0
bool IsValidVarName(Str* s) {
91
0
  return ::IsValidVarName(reinterpret_cast<const unsigned char*>(s->data_),
92
0
                          len(s));
93
0
}
94
95
0
bool ShouldHijack(Str* s) {
96
0
  return ::ShouldHijack(reinterpret_cast<const unsigned char*>(s->data_),
97
0
                        len(s));
98
0
}
99
100
}  // namespace match