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

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