/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 | | #ifdef DUMB_ALLOC |
13 | | #include "dumb_alloc.h" |
14 | | #define malloc dumb_malloc |
15 | | #define free dumb_free |
16 | | #endif |
17 | | |
18 | | namespace match { |
19 | | |
20 | 0 | Tuple2<Id_t, int> OneToken(lex_mode_t lex_mode, Str* line, int start_pos) { |
21 | 0 | int id; |
22 | 0 | int end_pos; |
23 | | |
24 | | // TODO: get rid of these casts |
25 | 0 | MatchOshToken(static_cast<int>(lex_mode), |
26 | 0 | reinterpret_cast<const unsigned char*>(line->data_), len(line), |
27 | 0 | start_pos, &id, &end_pos); |
28 | 0 | return Tuple2<Id_t, int>(static_cast<Id_t>(id), end_pos); |
29 | 0 | } |
30 | | |
31 | 7 | Tuple2<Id_t, Str*> SimpleLexer::Next() { |
32 | 7 | int id; |
33 | 7 | int end_pos; |
34 | 7 | match_func_(reinterpret_cast<const unsigned char*>(s_->data_), len(s_), pos_, |
35 | 7 | &id, &end_pos); |
36 | | |
37 | 7 | int len = end_pos - pos_; |
38 | 7 | char* buf = static_cast<char*>(malloc(len + 1)); |
39 | 7 | memcpy(buf, s_->data_ + pos_, len); // copy the list item |
40 | 7 | buf[len] = '\0'; |
41 | 7 | Str* val = CopyBufferIntoNewStr(buf, len); |
42 | | |
43 | 7 | pos_ = end_pos; |
44 | 7 | return Tuple2<Id_t, Str*>(static_cast<Id_t>(id), val); |
45 | 7 | } |
46 | | |
47 | | namespace Id = id_kind_asdl::Id; |
48 | | |
49 | 0 | List<Tuple2<Id_t, Str*>*>* SimpleLexer::Tokens() { |
50 | 0 | auto tokens = NewList<Tuple2<Id_t, Str*>*>(); |
51 | 0 | while (true) { |
52 | 0 | auto tup2 = Next(); |
53 | 0 | if (tup2.at0() == Id::Eol_Tok) { |
54 | 0 | break; |
55 | 0 | } |
56 | | // it's annoying that we have to put it on the heap |
57 | 0 | tokens->append(Alloc<Tuple2<Id_t, Str*>>(tup2.at0(), tup2.at1())); |
58 | 0 | } |
59 | 0 | return tokens; |
60 | 0 | } |
61 | | |
62 | 2 | SimpleLexer* BraceRangeLexer(Str* s) { |
63 | 2 | return Alloc<SimpleLexer>(&MatchBraceRangeToken, s); |
64 | 2 | } |
65 | | |
66 | 0 | SimpleLexer* GlobLexer(Str* s) { |
67 | 0 | return Alloc<SimpleLexer>(&MatchGlobToken, s); |
68 | 0 | } |
69 | | |
70 | 0 | SimpleLexer* EchoLexer(Str* s) { |
71 | 0 | return Alloc<SimpleLexer>(&MatchEchoToken, s); |
72 | 0 | } |
73 | | |
74 | 0 | List<Tuple2<Id_t, Str*>*>* HistoryTokens(Str* s) { |
75 | 0 | SimpleLexer lexer(&MatchHistoryToken, s); |
76 | 0 | return lexer.Tokens(); |
77 | 0 | } |
78 | | |
79 | 0 | List<Tuple2<Id_t, Str*>*>* Ps1Tokens(Str* s) { |
80 | 0 | SimpleLexer lexer(&MatchPS1Token, s); |
81 | 0 | return lexer.Tokens(); |
82 | 0 | } |
83 | | |
84 | 3 | Id_t BracketUnary(Str* s) { |
85 | 3 | return ::BracketUnary(reinterpret_cast<const unsigned char*>(s->data_), |
86 | 3 | len(s)); |
87 | 3 | } |
88 | 3 | Id_t BracketBinary(Str* s) { |
89 | 3 | return ::BracketBinary(reinterpret_cast<const unsigned char*>(s->data_), |
90 | 3 | len(s)); |
91 | 3 | } |
92 | 0 | Id_t BracketOther(Str* s) { |
93 | 0 | return ::BracketOther(reinterpret_cast<const unsigned char*>(s->data_), |
94 | 0 | len(s)); |
95 | 0 | } |
96 | | |
97 | 0 | bool IsValidVarName(Str* s) { |
98 | 0 | return ::IsValidVarName(reinterpret_cast<const unsigned char*>(s->data_), |
99 | 0 | len(s)); |
100 | 0 | } |
101 | | |
102 | 0 | bool ShouldHijack(Str* s) { |
103 | 0 | return ::ShouldHijack(reinterpret_cast<const unsigned char*>(s->data_), |
104 | 0 | len(s)); |
105 | 0 | } |
106 | | |
107 | | } // namespace match |