/home/andy/git/oilshell/oil/cpp/frontend_match.cc
Line | Count | Source (jump to first uncovered line) |
1 | | // frontend_match.cc: manual port of frontend/match |
2 | | |
3 | | #include "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 | | namespace match { |
13 | | |
14 | | using id_kind_asdl::Id; |
15 | | |
16 | 0 | Tuple2<Id_t, int> OneToken(lex_mode_t lex_mode, BigStr* line, int start_pos) { |
17 | 0 | int id; |
18 | 0 | int end_pos; |
19 | | |
20 | | // TODO: get rid of these casts |
21 | 0 | MatchOshToken(static_cast<int>(lex_mode), |
22 | 0 | reinterpret_cast<const unsigned char*>(line->data_), len(line), |
23 | 0 | start_pos, &id, &end_pos); |
24 | 0 | return Tuple2<Id_t, int>(static_cast<Id_t>(id), end_pos); |
25 | 0 | } |
26 | | |
27 | 9 | Tuple2<Id_t, BigStr*> SimpleLexer::Next() { |
28 | 9 | int id; |
29 | 9 | int end_pos; |
30 | 9 | match_func_(reinterpret_cast<const unsigned char*>(s_->data_), len(s_), pos_, |
31 | 9 | &id, &end_pos); |
32 | | |
33 | 9 | int len = end_pos - pos_; |
34 | 9 | BigStr* val = NewStr(len); |
35 | 9 | memcpy(val->data_, s_->data_ + pos_, len); // copy the list item |
36 | 9 | val->data_[len] = '\0'; |
37 | | |
38 | 9 | pos_ = end_pos; |
39 | 9 | return Tuple2<Id_t, BigStr*>(static_cast<Id_t>(id), val); |
40 | 9 | } |
41 | | |
42 | 3 | List<Tuple2<Id_t, BigStr*>*>* SimpleLexer::Tokens() { |
43 | 3 | auto tokens = NewList<Tuple2<Id_t, BigStr*>*>(); |
44 | 8 | while (true) { |
45 | 8 | auto tup2 = Next(); |
46 | 8 | if (tup2.at0() == Id::Eol_Tok) { |
47 | 3 | break; |
48 | 3 | } |
49 | | // it's annoying that we have to put it on the heap |
50 | 5 | tokens->append(Alloc<Tuple2<Id_t, BigStr*>>(tup2.at0(), tup2.at1())); |
51 | 5 | } |
52 | 3 | return tokens; |
53 | 3 | } |
54 | | |
55 | 2 | SimpleLexer* BraceRangeLexer(BigStr* s) { |
56 | 2 | return Alloc<SimpleLexer>(&MatchBraceRangeToken, s); |
57 | 2 | } |
58 | | |
59 | 1 | SimpleLexer* GlobLexer(BigStr* s) { |
60 | 1 | return Alloc<SimpleLexer>(&MatchGlobToken, s); |
61 | 1 | } |
62 | | |
63 | 1 | SimpleLexer* EchoLexer(BigStr* s) { |
64 | 1 | return Alloc<SimpleLexer>(&MatchEchoToken, s); |
65 | 1 | } |
66 | | |
67 | 1 | List<Tuple2<Id_t, BigStr*>*>* HistoryTokens(BigStr* s) { |
68 | 1 | SimpleLexer lexer(&MatchHistoryToken, s); |
69 | 1 | return lexer.Tokens(); |
70 | 1 | } |
71 | | |
72 | 1 | List<Tuple2<Id_t, BigStr*>*>* Ps1Tokens(BigStr* s) { |
73 | 1 | SimpleLexer lexer(&MatchPS1Token, s); |
74 | 1 | return lexer.Tokens(); |
75 | 1 | } |
76 | | |
77 | 3 | Id_t BracketUnary(BigStr* s) { |
78 | 3 | return ::BracketUnary(reinterpret_cast<const unsigned char*>(s->data_), |
79 | 3 | len(s)); |
80 | 3 | } |
81 | 3 | Id_t BracketBinary(BigStr* s) { |
82 | 3 | return ::BracketBinary(reinterpret_cast<const unsigned char*>(s->data_), |
83 | 3 | len(s)); |
84 | 3 | } |
85 | 1 | Id_t BracketOther(BigStr* s) { |
86 | 1 | return ::BracketOther(reinterpret_cast<const unsigned char*>(s->data_), |
87 | 1 | len(s)); |
88 | 1 | } |
89 | | |
90 | 0 | Tuple2<Id_t, int> MatchJ8Token(BigStr* s, int pos) { |
91 | 0 | int id; |
92 | 0 | int end_pos; |
93 | 0 | ::MatchJ8Token(reinterpret_cast<const unsigned char*>(s->data_), len(s), pos, |
94 | 0 | &id, &end_pos); |
95 | 0 | return Tuple2<Id_t, int>(static_cast<Id_t>(id), end_pos); |
96 | 0 | } |
97 | | |
98 | 0 | Tuple2<Id_t, int> MatchJ8StrToken(BigStr* s, int pos) { |
99 | 0 | int id; |
100 | 0 | int end_pos; |
101 | 0 | ::MatchJ8StrToken(reinterpret_cast<const unsigned char*>(s->data_), len(s), |
102 | 0 | pos, &id, &end_pos); |
103 | 0 | return Tuple2<Id_t, int>(static_cast<Id_t>(id), end_pos); |
104 | 0 | } |
105 | | |
106 | 0 | Tuple2<Id_t, int> MatchJsonStrToken(BigStr* s, int pos) { |
107 | 0 | int id; |
108 | 0 | int end_pos; |
109 | 0 | ::MatchJsonStrToken(reinterpret_cast<const unsigned char*>(s->data_), len(s), |
110 | 0 | pos, &id, &end_pos); |
111 | 0 | return Tuple2<Id_t, int>(static_cast<Id_t>(id), end_pos); |
112 | 0 | } |
113 | | |
114 | 4 | bool IsValidVarName(BigStr* s) { |
115 | 4 | return ::IsValidVarName(reinterpret_cast<const unsigned char*>(s->data_), |
116 | 4 | len(s)); |
117 | 4 | } |
118 | | |
119 | 2 | bool ShouldHijack(BigStr* s) { |
120 | 2 | return ::ShouldHijack(reinterpret_cast<const unsigned char*>(s->data_), |
121 | 2 | len(s)); |
122 | 2 | } |
123 | | |
124 | 0 | bool LooksLikeFloat(BigStr* s) { |
125 | 0 | return ::LooksLikeFloat(reinterpret_cast<const unsigned char*>(s->data_), |
126 | 0 | len(s)); |
127 | 0 | } |
128 | | |
129 | 0 | bool LooksLikeInteger(BigStr* s) { |
130 | 0 | return ::LooksLikeInteger(reinterpret_cast<const unsigned char*>(s->data_), |
131 | 0 | len(s)); |
132 | 0 | } |
133 | | |
134 | | } // namespace match |