cpp

Coverage Report

Created: 2024-03-13 14:13

/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, BigStr* 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, BigStr* s)
27
6
      : match_func_(match_func), s_(s), pos_(0) {
28
6
  }
29
30
  Tuple2<Id_t, BigStr*> Next();
31
  List<Tuple2<Id_t, BigStr*>*>* 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
  BigStr* s_;
44
  int pos_;
45
};
46
47
//
48
// Secondary Lexers
49
//
50
51
SimpleLexer* BraceRangeLexer(BigStr* s);
52
SimpleLexer* GlobLexer(BigStr* s);
53
SimpleLexer* EchoLexer(BigStr* s);
54
55
List<Tuple2<Id_t, BigStr*>*>* HistoryTokens(BigStr* s);
56
List<Tuple2<Id_t, BigStr*>*>* Ps1Tokens(BigStr* s);
57
58
Id_t BracketUnary(BigStr* s);
59
Id_t BracketBinary(BigStr* s);
60
Id_t BracketOther(BigStr* s);
61
62
Tuple2<Id_t, int> MatchJ8Token(BigStr* s, int pos);
63
Tuple2<Id_t, int> MatchJ8StrToken(BigStr* s, int pos);
64
Tuple2<Id_t, int> MatchJsonStrToken(BigStr* s, int pos);
65
66
//
67
// Other Matching Functions
68
//
69
70
bool IsValidVarName(BigStr* s);
71
bool ShouldHijack(BigStr* s);
72
bool CanOmitQuotes(BigStr* s);
73
bool LooksLikeFloat(BigStr* s);
74
bool LooksLikeInteger(BigStr* s);
75
76
// StringToInt
77
78
int MatchOption(BigStr* s);
79
80
}  // namespace match
81
82
#endif  // MATCH_H