/home/uke/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 "_devbuild/gen/osh-types.h" |
8 | | #include "_devbuild/gen/id.h" |
9 | | #include "_devbuild/gen/osh-lex.h" |
10 | | // clang-format on |
11 | | |
12 | | #ifdef DUMB_ALLOC |
13 | | #include "leaky_dumb_alloc.h" |
14 | 6 | #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 | | // cstring-TODO: input isn't NUL-terminated |
24 | | |
25 | | // TODO: get rid of these casts |
26 | 0 | MatchOshToken(static_cast<int>(lex_mode), |
27 | 0 | reinterpret_cast<const unsigned char*>(line->data_), line->len_, |
28 | 0 | start_pos, &id, &end_pos); |
29 | 0 | return Tuple2<Id_t, int>(static_cast<Id_t>(id), end_pos); |
30 | 0 | } |
31 | | |
32 | 6 | Tuple2<Id_t, Str*> SimpleLexer::Next() { |
33 | 6 | int id; |
34 | 6 | int end_pos; |
35 | | // cstring-TODO: input isn't NUL-terminated |
36 | 6 | match_func_(reinterpret_cast<const unsigned char*>(s_->data_), s_->len_, pos_, |
37 | 6 | &id, &end_pos); |
38 | | // cstring-TODO: Don't allocate? |
39 | | // Str* val = new Str(s->data_ + pos_, end_pos - pos_); |
40 | | |
41 | 6 | int len = end_pos - pos_; |
42 | 6 | char* buf = static_cast<char*>(malloc(len + 1)); |
43 | 6 | memcpy(buf, s_->data_ + pos_, len); // copy the list item |
44 | 6 | buf[len] = '\0'; |
45 | 6 | Str* val = new Str(buf, len); |
46 | | |
47 | 6 | pos_ = end_pos; |
48 | 6 | return Tuple2<Id_t, Str*>(static_cast<Id_t>(id), val); |
49 | 6 | } |
50 | | |
51 | | namespace Id = id_kind_asdl::Id; |
52 | | |
53 | 0 | List<Tuple2<Id_t, Str*>*>* SimpleLexer::Tokens() { |
54 | 0 | auto tokens = new List<Tuple2<Id_t, Str*>*>(); |
55 | 0 | while (true) { |
56 | 0 | auto tup2 = Next(); |
57 | 0 | if (tup2.at0() == Id::Eol_Tok) { |
58 | 0 | break; |
59 | 0 | } |
60 | | // it's annoying that we have to put it on the heap |
61 | 0 | tokens->append(new Tuple2<Id_t, Str*>(tup2.at0(), tup2.at1())); |
62 | 0 | } |
63 | 0 | return tokens; |
64 | 0 | } |
65 | | |
66 | 1 | SimpleLexer* BraceRangeLexer(Str* s) { |
67 | 1 | return new SimpleLexer(&MatchBraceRangeToken, s); |
68 | 1 | } |
69 | | |
70 | 0 | SimpleLexer* GlobLexer(Str* s) { |
71 | 0 | return new SimpleLexer(&MatchGlobToken, s); |
72 | 0 | } |
73 | | |
74 | 0 | SimpleLexer* EchoLexer(Str* s) { |
75 | 0 | return new SimpleLexer(&MatchEchoToken, s); |
76 | 0 | } |
77 | | |
78 | 0 | List<Tuple2<Id_t, Str*>*>* HistoryTokens(Str* s) { |
79 | 0 | SimpleLexer lexer(&MatchHistoryToken, s); |
80 | 0 | return lexer.Tokens(); |
81 | 0 | } |
82 | | |
83 | 0 | List<Tuple2<Id_t, Str*>*>* Ps1Tokens(Str* s) { |
84 | 0 | SimpleLexer lexer(&MatchPS1Token, s); |
85 | 0 | return lexer.Tokens(); |
86 | 0 | } |
87 | | |
88 | 3 | Id_t BracketUnary(Str* s) { |
89 | | // cstring-TODO: input isn't NUL-terminated |
90 | 3 | return ::BracketUnary(reinterpret_cast<const unsigned char*>(s->data_), |
91 | 3 | s->len_); |
92 | 3 | } |
93 | 3 | Id_t BracketBinary(Str* s) { |
94 | | // cstring-TODO: input isn't NUL-terminated |
95 | 3 | return ::BracketBinary(reinterpret_cast<const unsigned char*>(s->data_), |
96 | 3 | s->len_); |
97 | 3 | } |
98 | 0 | Id_t BracketOther(Str* s) { |
99 | | // cstring-TODO: input isn't NUL-terminated |
100 | 0 | return ::BracketOther(reinterpret_cast<const unsigned char*>(s->data_), |
101 | 0 | s->len_); |
102 | 0 | } |
103 | | |
104 | 0 | bool IsValidVarName(Str* s) { |
105 | | // cstring-TODO: input isn't NUL-terminated |
106 | |
|
107 | 0 | return ::IsValidVarName(reinterpret_cast<const unsigned char*>(s->data_), |
108 | 0 | s->len_); |
109 | 0 | } |
110 | | |
111 | 0 | bool ShouldHijack(Str* s) { |
112 | | // cstring-TODO: input isn't NUL-terminated |
113 | |
|
114 | 0 | return ::ShouldHijack(reinterpret_cast<const unsigned char*>(s->data_), |
115 | 0 | s->len_); |
116 | 0 | } |
117 | | |
118 | | } // namespace match |