| 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 | // Disabled, see hack below
|
| 9 | // using id_kind_asdl::Id_t;
|
| 10 |
|
| 11 | #include "_gen/frontend/syntax.asdl.h"
|
| 12 | #include "_gen/frontend/types.asdl.h"
|
| 13 |
|
| 14 | namespace match {
|
| 15 |
|
| 16 | // Horrible hack that is OK because we know there are less than 2^16 Token IDs
|
| 17 | // uint16_t and int are interchangeable.
|
| 18 | // These functions will return an int, not uint16_t. And then
|
| 19 | // syntax_asdl::Token() stores a uint16_t.
|
| 20 | typedef int Id_t;
|
| 21 |
|
| 22 | using types_asdl::lex_mode_t;
|
| 23 |
|
| 24 | // The big lexer
|
| 25 | Tuple2<Id_t, int> OneToken(lex_mode_t lex_mode, BigStr* line, int start_pos);
|
| 26 |
|
| 27 | // There are 5 secondary lexers with matchers of this type
|
| 28 | typedef void (*MatchFunc)(const unsigned char* line, int line_len,
|
| 29 | int start_pos, int* id, int* end_pos);
|
| 30 |
|
| 31 | class SimpleLexer {
|
| 32 | public:
|
| 33 | SimpleLexer(MatchFunc match_func, BigStr* s)
|
| 34 | : match_func_(match_func), s_(s), pos_(0) {
|
| 35 | }
|
| 36 |
|
| 37 | Tuple2<Id_t, BigStr*> Next();
|
| 38 | List<Tuple2<Id_t, BigStr*>*>* Tokens();
|
| 39 |
|
| 40 | static constexpr ObjHeader obj_header() {
|
| 41 | return ObjHeader::ClassFixed(field_mask(), sizeof(SimpleLexer));
|
| 42 | }
|
| 43 |
|
| 44 | static constexpr uint32_t field_mask() {
|
| 45 | return maskbit(offsetof(SimpleLexer, s_));
|
| 46 | }
|
| 47 |
|
| 48 | private:
|
| 49 | MatchFunc match_func_;
|
| 50 | BigStr* s_;
|
| 51 | int pos_;
|
| 52 | };
|
| 53 |
|
| 54 | //
|
| 55 | // Secondary Lexers
|
| 56 | //
|
| 57 |
|
| 58 | SimpleLexer* BraceRangeLexer(BigStr* s);
|
| 59 | SimpleLexer* GlobLexer(BigStr* s);
|
| 60 | SimpleLexer* EchoLexer(BigStr* s);
|
| 61 |
|
| 62 | List<Tuple2<Id_t, BigStr*>*>* HistoryTokens(BigStr* s);
|
| 63 | List<Tuple2<Id_t, BigStr*>*>* Ps1Tokens(BigStr* s);
|
| 64 |
|
| 65 | Id_t BracketUnary(BigStr* s);
|
| 66 | Id_t BracketBinary(BigStr* s);
|
| 67 | Id_t BracketOther(BigStr* s);
|
| 68 |
|
| 69 | Tuple2<Id_t, int> MatchJ8Token(BigStr* s, int pos);
|
| 70 | Tuple2<Id_t, int> MatchJ8LinesToken(BigStr* s, int pos);
|
| 71 | Tuple2<Id_t, int> MatchJ8StrToken(BigStr* s, int pos);
|
| 72 | Tuple2<Id_t, int> MatchJsonStrToken(BigStr* s, int pos);
|
| 73 |
|
| 74 | //
|
| 75 | // Other Matching Functions
|
| 76 | //
|
| 77 |
|
| 78 | bool IsValidVarName(BigStr* s);
|
| 79 | bool ShouldHijack(BigStr* s);
|
| 80 | bool CanOmitQuotes(BigStr* s);
|
| 81 | bool LooksLikeFloat(BigStr* s);
|
| 82 | bool LooksLikeInteger(BigStr* s);
|
| 83 |
|
| 84 | // StringToInt
|
| 85 |
|
| 86 | int MatchOption(BigStr* s);
|
| 87 |
|
| 88 | } // namespace match
|
| 89 |
|
| 90 | #endif // MATCH_H
|