cpp

Coverage Report

Created: 2023-03-07 20:24

/home/andy/git/oilshell/oil/cpp/frontend_match.h
Line
Count
Source
1
// Replacement for frontend/match
2
3
#ifndef MATCH_H
4
#define MATCH_H
5
6
#include "_gen/frontend/id_kind.asdl.h"  // syntax.asdl depends on this
7
#include "mycpp/runtime.h"
8
using id_kind_asdl::Id_t;  // TODO: proper ASDL modules
9
10
#include "_gen/frontend/syntax.asdl.h"
11
#include "_gen/frontend/types.asdl.h"
12
13
namespace match {
14
15
using types_asdl::lex_mode_t;
16
17
// The big lexer
18
Tuple2<Id_t, int> OneToken(lex_mode_t lex_mode, Str* line, int start_pos);
19
20
// There are 5 secondary lexers with matchers of this type
21
typedef void (*MatchFunc)(const unsigned char* line, int line_len,
22
                          int start_pos, int* id, int* end_pos);
23
24
class SimpleLexer {
25
 public:
26
  SimpleLexer(MatchFunc match_func, Str* s)
27
6
      : header_(obj_header()), match_func_(match_func), s_(s), pos_(0) {
28
6
  }
29
30
  Tuple2<Id_t, Str*> Next();
31
  List<Tuple2<Id_t, Str*>*>* Tokens();
32
33
6
  static constexpr ObjHeader obj_header() {
34
6
    return ObjHeader::ClassFixed(field_mask(), sizeof(SimpleLexer));
35
6
  }
36
37
6
  static constexpr uint16_t field_mask() {
38
6
    return maskbit(offsetof(SimpleLexer, s_));
39
6
  }
40
41
 private:
42
  GC_OBJ(header_);
43
  MatchFunc match_func_;
44
  Str* s_;
45
  int pos_;
46
};
47
48
//
49
// Secondary Lexers
50
//
51
52
SimpleLexer* BraceRangeLexer(Str* s);
53
SimpleLexer* GlobLexer(Str* s);
54
SimpleLexer* EchoLexer(Str* s);
55
56
List<Tuple2<Id_t, Str*>*>* HistoryTokens(Str* s);
57
List<Tuple2<Id_t, Str*>*>* Ps1Tokens(Str* s);
58
59
Id_t BracketUnary(Str* s);
60
Id_t BracketBinary(Str* s);
61
Id_t BracketOther(Str* s);
62
63
//
64
// Other Matching Functions
65
//
66
67
bool IsValidVarName(Str* s);
68
bool ShouldHijack(Str* s);
69
70
// StringToInt
71
72
int MatchOption(Str* s);
73
74
}  // namespace match
75
76
#endif  // MATCH_H