cpp

Coverage Report

Created: 2023-09-13 01:07

/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
      : 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
4
  static constexpr ObjHeader obj_header() {
34
4
    return ObjHeader::ClassFixed(field_mask(), sizeof(SimpleLexer));
35
4
  }
36
37
4
  static constexpr uint32_t field_mask() {
38
4
    return maskbit(offsetof(SimpleLexer, s_));
39
4
  }
40
41
 private:
42
  MatchFunc match_func_;
43
  Str* s_;
44
  int pos_;
45
};
46
47
//
48
// Secondary Lexers
49
//
50
51
SimpleLexer* BraceRangeLexer(Str* s);
52
SimpleLexer* GlobLexer(Str* s);
53
SimpleLexer* EchoLexer(Str* s);
54
55
List<Tuple2<Id_t, Str*>*>* HistoryTokens(Str* s);
56
List<Tuple2<Id_t, Str*>*>* Ps1Tokens(Str* s);
57
58
Id_t BracketUnary(Str* s);
59
Id_t BracketBinary(Str* s);
60
Id_t BracketOther(Str* s);
61
62
//
63
// Other Matching Functions
64
//
65
66
bool IsValidVarName(Str* s);
67
bool ShouldHijack(Str* s);
68
bool LooksLikeFloat(Str* s);
69
bool LooksLikeInteger(Str* s);
70
71
// StringToInt
72
73
int MatchOption(Str* s);
74
75
}  // namespace match
76
77
#endif  // MATCH_H