| 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 | Tuple2<Id_t, int> OneToken(lex_mode_t lex_mode, BigStr* line, int start_pos) {
|
| 17 | int id;
|
| 18 | int end_pos;
|
| 19 |
|
| 20 | // TODO: get rid of these casts
|
| 21 | MatchOshToken(static_cast<int>(lex_mode),
|
| 22 | reinterpret_cast<const unsigned char*>(line->data_), len(line),
|
| 23 | start_pos, &id, &end_pos);
|
| 24 | return Tuple2<Id_t, int>(static_cast<Id_t>(id), end_pos);
|
| 25 | }
|
| 26 |
|
| 27 | Tuple2<Id_t, BigStr*> SimpleLexer::Next() {
|
| 28 | int id;
|
| 29 | int end_pos;
|
| 30 | match_func_(reinterpret_cast<const unsigned char*>(s_->data_), len(s_), pos_,
|
| 31 | &id, &end_pos);
|
| 32 |
|
| 33 | int len = end_pos - pos_;
|
| 34 | BigStr* val = NewStr(len);
|
| 35 | memcpy(val->data_, s_->data_ + pos_, len); // copy the list item
|
| 36 | val->data_[len] = '\0';
|
| 37 |
|
| 38 | pos_ = end_pos;
|
| 39 | return Tuple2<Id_t, BigStr*>(static_cast<Id_t>(id), val);
|
| 40 | }
|
| 41 |
|
| 42 | List<Tuple2<Id_t, BigStr*>*>* SimpleLexer::Tokens() {
|
| 43 | auto tokens = NewList<Tuple2<Id_t, BigStr*>*>();
|
| 44 | while (true) {
|
| 45 | auto tup2 = Next();
|
| 46 | if (tup2.at0() == Id::Eol_Tok) {
|
| 47 | break;
|
| 48 | }
|
| 49 | // it's annoying that we have to put it on the heap
|
| 50 | tokens->append(Alloc<Tuple2<Id_t, BigStr*>>(tup2.at0(), tup2.at1()));
|
| 51 | }
|
| 52 | return tokens;
|
| 53 | }
|
| 54 |
|
| 55 | SimpleLexer* BraceRangeLexer(BigStr* s) {
|
| 56 | return Alloc<SimpleLexer>(&MatchBraceRangeToken, s);
|
| 57 | }
|
| 58 |
|
| 59 | SimpleLexer* GlobLexer(BigStr* s) {
|
| 60 | return Alloc<SimpleLexer>(&MatchGlobToken, s);
|
| 61 | }
|
| 62 |
|
| 63 | SimpleLexer* EchoLexer(BigStr* s) {
|
| 64 | return Alloc<SimpleLexer>(&MatchEchoToken, s);
|
| 65 | }
|
| 66 |
|
| 67 | SimpleLexer* PrintfBLexer(BigStr* s) {
|
| 68 | return Alloc<SimpleLexer>(&MatchPrintfBToken, s);
|
| 69 | }
|
| 70 |
|
| 71 | List<Tuple2<Id_t, BigStr*>*>* HistoryTokens(BigStr* s) {
|
| 72 | SimpleLexer lexer(&MatchHistoryToken, s);
|
| 73 | return lexer.Tokens();
|
| 74 | }
|
| 75 |
|
| 76 | List<Tuple2<Id_t, BigStr*>*>* Ps1Tokens(BigStr* s) {
|
| 77 | SimpleLexer lexer(&MatchPS1Token, s);
|
| 78 | return lexer.Tokens();
|
| 79 | }
|
| 80 |
|
| 81 | Id_t BracketUnary(BigStr* s) {
|
| 82 | return ::BracketUnary(reinterpret_cast<const unsigned char*>(s->data_),
|
| 83 | len(s));
|
| 84 | }
|
| 85 | Id_t BracketBinary(BigStr* s) {
|
| 86 | return ::BracketBinary(reinterpret_cast<const unsigned char*>(s->data_),
|
| 87 | len(s));
|
| 88 | }
|
| 89 | Id_t BracketOther(BigStr* s) {
|
| 90 | return ::BracketOther(reinterpret_cast<const unsigned char*>(s->data_),
|
| 91 | len(s));
|
| 92 | }
|
| 93 |
|
| 94 | Tuple2<Id_t, int> MatchJ8Token(BigStr* s, int pos) {
|
| 95 | int id;
|
| 96 | int end_pos;
|
| 97 | ::MatchJ8Token(reinterpret_cast<const unsigned char*>(s->data_), len(s), pos,
|
| 98 | &id, &end_pos);
|
| 99 | return Tuple2<Id_t, int>(static_cast<Id_t>(id), end_pos);
|
| 100 | }
|
| 101 |
|
| 102 | Tuple2<Id_t, int> MatchJ8LinesToken(BigStr* s, int pos) {
|
| 103 | int id;
|
| 104 | int end_pos;
|
| 105 | ::MatchJ8LinesToken(reinterpret_cast<const unsigned char*>(s->data_), len(s),
|
| 106 | pos, &id, &end_pos);
|
| 107 | return Tuple2<Id_t, int>(static_cast<Id_t>(id), end_pos);
|
| 108 | }
|
| 109 |
|
| 110 | Tuple2<Id_t, int> MatchJ8StrToken(BigStr* s, int pos) {
|
| 111 | int id;
|
| 112 | int end_pos;
|
| 113 | ::MatchJ8StrToken(reinterpret_cast<const unsigned char*>(s->data_), len(s),
|
| 114 | pos, &id, &end_pos);
|
| 115 | return Tuple2<Id_t, int>(static_cast<Id_t>(id), end_pos);
|
| 116 | }
|
| 117 |
|
| 118 | Tuple2<Id_t, int> MatchJsonStrToken(BigStr* s, int pos) {
|
| 119 | int id;
|
| 120 | int end_pos;
|
| 121 | ::MatchJsonStrToken(reinterpret_cast<const unsigned char*>(s->data_), len(s),
|
| 122 | pos, &id, &end_pos);
|
| 123 | return Tuple2<Id_t, int>(static_cast<Id_t>(id), end_pos);
|
| 124 | }
|
| 125 |
|
| 126 | Tuple2<Id_t, int> MatchShNumberToken(BigStr* s, int pos) {
|
| 127 | int id;
|
| 128 | int end_pos;
|
| 129 | ::MatchShNumberToken(reinterpret_cast<const unsigned char*>(s->data_), len(s),
|
| 130 | pos, &id, &end_pos);
|
| 131 | return Tuple2<Id_t, int>(static_cast<Id_t>(id), end_pos);
|
| 132 | }
|
| 133 |
|
| 134 | bool IsUtf8Codeset(BigStr* s) {
|
| 135 | return ::IsUtf8Codeset(reinterpret_cast<const unsigned char*>(s->data_),
|
| 136 | len(s));
|
| 137 | }
|
| 138 |
|
| 139 | bool IsValidVarName(BigStr* s) {
|
| 140 | return ::IsValidVarName(reinterpret_cast<const unsigned char*>(s->data_),
|
| 141 | len(s));
|
| 142 | }
|
| 143 |
|
| 144 | bool ShouldHijack(BigStr* s) {
|
| 145 | return ::ShouldHijack(reinterpret_cast<const unsigned char*>(s->data_),
|
| 146 | len(s));
|
| 147 | }
|
| 148 |
|
| 149 | bool LooksLikeInteger(BigStr* s) {
|
| 150 | return ::LooksLikeInteger(reinterpret_cast<const unsigned char*>(s->data_),
|
| 151 | len(s));
|
| 152 | }
|
| 153 |
|
| 154 | bool LooksLikeYshInt(BigStr* s) {
|
| 155 | return ::LooksLikeYshInt(reinterpret_cast<const unsigned char*>(s->data_),
|
| 156 | len(s));
|
| 157 | }
|
| 158 |
|
| 159 | bool LooksLikeYshFloat(BigStr* s) {
|
| 160 | return ::LooksLikeYshFloat(reinterpret_cast<const unsigned char*>(s->data_),
|
| 161 | len(s));
|
| 162 | }
|
| 163 |
|
| 164 | } // namespace match
|