OILS / cpp / frontend_match.h View on Github | oilshell.org

90 lines, 46 significant
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
14namespace 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.
20typedef int Id_t;
21
22using types_asdl::lex_mode_t;
23
24// The big lexer
25Tuple2<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
28typedef void (*MatchFunc)(const unsigned char* line, int line_len,
29 int start_pos, int* id, int* end_pos);
30
31class 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
58SimpleLexer* BraceRangeLexer(BigStr* s);
59SimpleLexer* GlobLexer(BigStr* s);
60SimpleLexer* EchoLexer(BigStr* s);
61
62List<Tuple2<Id_t, BigStr*>*>* HistoryTokens(BigStr* s);
63List<Tuple2<Id_t, BigStr*>*>* Ps1Tokens(BigStr* s);
64
65Id_t BracketUnary(BigStr* s);
66Id_t BracketBinary(BigStr* s);
67Id_t BracketOther(BigStr* s);
68
69Tuple2<Id_t, int> MatchJ8Token(BigStr* s, int pos);
70Tuple2<Id_t, int> MatchJ8LinesToken(BigStr* s, int pos);
71Tuple2<Id_t, int> MatchJ8StrToken(BigStr* s, int pos);
72Tuple2<Id_t, int> MatchJsonStrToken(BigStr* s, int pos);
73
74//
75// Other Matching Functions
76//
77
78bool IsValidVarName(BigStr* s);
79bool ShouldHijack(BigStr* s);
80bool CanOmitQuotes(BigStr* s);
81bool LooksLikeFloat(BigStr* s);
82bool LooksLikeInteger(BigStr* s);
83
84// StringToInt
85
86int MatchOption(BigStr* s);
87
88} // namespace match
89
90#endif // MATCH_H