1 #ifndef SYNTAX_SYNTAX_H
2 #define SYNTAX_SYNTAX_H
3 
4 #include <stdbool.h>
5 #include <stddef.h>
6 #include <stdint.h>
7 #include "bitset.h"
8 #include "color.h"
9 #include "../util/hashset.h"
10 #include "../util/ptr-array.h"
11 
12 typedef enum {
13     COND_BUFIS,
14     COND_CHAR,
15     COND_CHAR_BUFFER,
16     COND_CHAR1,
17     COND_INLIST,
18     COND_RECOLOR,
19     COND_RECOLOR_BUFFER,
20     COND_STR,
21     COND_STR2,
22     COND_STR_ICASE,
23     COND_HEREDOCEND,
24 } ConditionType;
25 
26 typedef struct {
27     struct State *destination;
28 
29     // If condition has no emit name this is set to destination state's
30     // emit name or list name (COND_INLIST).
31     char *emit_name;
32 
33     // Set after all colors have been added (config loaded).
34     HlColor *emit_color;
35 } Action;
36 
37 typedef struct {
38     char *name;
39     HashSet strings;
40     bool used;
41     bool defined;
42 } StringList;
43 
44 typedef struct {
45     union {
46         struct {
47             uint8_t len;
48             bool icase;
49             char str[30];
50         } cond_bufis;
51         struct {
52             BitSet bitset;
53         } cond_char;
54         struct {
55             unsigned char ch;
56         } cond_single_char;
57         struct {
58             StringList *list;
59         } cond_inlist;
60         struct {
61             size_t len;
62         } cond_recolor;
63         struct {
64             uint8_t len;
65             char str[31];
66         } cond_str;
67         struct {
68             size_t len;
69             char *str;
70         } cond_heredocend;
71     } u;
72     Action a;
73     ConditionType type;
74 } Condition;
75 
76 typedef struct {
77     char *name;
78     PointerArray states;
79     PointerArray string_lists;
80     PointerArray default_colors;
81     bool heredoc;
82     bool used;
83 } Syntax;
84 
85 typedef struct State {
86     char *name;
87     char *emit_name;
88     PointerArray conds;
89 
90     bool defined;
91     bool visited;
92     bool copied;
93 
94     enum {
95         STATE_INVALID = -1,
96         STATE_EAT,
97         STATE_NOEAT,
98         STATE_NOEAT_BUFFER,
99         STATE_HEREDOCBEGIN,
100     } type;
101     Action a;
102 
103     struct {
104         Syntax *subsyntax;
105         PointerArray states;
106     } heredoc;
107 } State;
108 
109 typedef struct {
110     State *state;
111     char *delim;
112     size_t len;
113 } HeredocState;
114 
115 typedef struct {
116     Syntax *subsyn;
117     State *return_state;
118     const char *delim;
119     size_t delim_len;
120 } SyntaxMerge;
121 
is_subsyntax(const Syntax * syn)122 static inline bool is_subsyntax(const Syntax *syn)
123 {
124     return syn->name[0] == '.';
125 }
126 
127 StringList *find_string_list(const Syntax *syn, const char *name);
128 State *find_state(const Syntax *syn, const char *name);
129 State *merge_syntax(Syntax *syn, SyntaxMerge *m);
130 void finalize_syntax(Syntax *syn, unsigned int saved_nr_errors);
131 
132 Syntax *find_any_syntax(const char *name);
133 Syntax *find_syntax(const char *name);
134 void update_syntax_colors(Syntax *syn);
135 void update_all_syntax_colors(void);
136 void find_unused_subsyntaxes(void);
137 
138 #endif
139