1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
use lib_ruby_parser_nodes::{Message, Node};

/// A struct with codegen options
pub struct Options {
    /// Identifier that is passed to wrapping `#ifndef`
    ///
    /// Default: `LIB_RUBY_PARSER_BINDINGS_H`
    pub ifndef_name: String,

    /// Pre-code
    ///
    /// You can put here things like `#include` or
    /// `#ifdef __cplusplus \n extern "C" ...`
    ///
    /// Default: `"// pre-code"`
    pub pre_code: String,

    /// Post-code
    ///
    /// Can be used to close `#ifdef __cplusplus`
    ///
    /// Default: `"// post-code"`
    pub post_code: String,

    /// Name of what you want to be used
    /// **as a blob** of the `Ptr<T>` struct in `lib-ruby-parser`
    pub ptr_blob_name: String,

    /// Name of what you want to be used
    /// **as a blob** of the `MaybePtr<T>` struct in `lib-ruby-parser`
    pub maybe_ptr_blob_name: String,

    /// Name of what you want to be used
    /// **as a blob** of the `StringPtr` struct in `lib-ruby-parser`
    pub string_ptr_blob_name: String,

    /// Name of what you want to be used
    /// **as a blob** of the `MaybeStringPtr` struct in `lib-ruby-parser`
    pub maybe_string_ptr_blob_name: String,

    /// Name of what you want to be used
    /// **as a blob** of the `SharedByteList` struct in `lib-ruby-parser`
    pub shared_byte_list_blob_name: String,

    /// Name of what you want to be used
    /// **as a blob** of the `u8` struct in `lib-ruby-parser`
    ///
    /// In most cases if you build low-level bindings it will be u8
    /// (without converting it to/from blob)
    pub byte_blob_name: String,

    /// Name of what you want to be used
    /// **as a blob** of the `Token` struct in `lib-ruby-parser`
    pub token_blob_name: String,

    /// Name of what you want to be used
    /// **as a blob** of the `Node` struct in `lib-ruby-parser`
    pub node_blob_name: String,

    /// Name of what you want to be used
    /// **as a blob** of the `Diagnostic` struct in `lib-ruby-parser`
    pub diagnostic_blob_name: String,

    /// Name of what you want to be used
    /// **as a blob** of the `CommentType` struct in `lib-ruby-parser`
    pub comment_type_blob_name: String,

    /// Name of what you want to be used
    /// **as a blob** of the `Comment` struct in `lib-ruby-parser`
    pub comment_blob_name: String,

    /// Name of what you want to be used
    /// **as a blob** of the `MagicCommentKind` struct in `lib-ruby-parser`
    pub magic_comment_kind_blob_name: String,

    /// Name of what you want to be used
    /// **as a blob** of the `MagicComment` struct in `lib-ruby-parser`
    pub magic_comment_blob_name: String,

    /// Name of what you want to be used
    /// **as a blob** of the `Bytes` struct in `lib-ruby-parser`
    pub bytes_blob_name: String,

    /// Name of what you want to be used
    /// **as a blob** of the `Loc` struct in `lib-ruby-parser`
    pub loc_blob_name: String,

    /// Name of what you want to be used
    /// **as a blob** of the `MaybeLoc` struct in `lib-ruby-parser`
    pub maybe_loc_blob_name: String,

    /// Name of what you want to be used
    /// **as a blob** of the `ErrorLevel` struct in `lib-ruby-parser`
    pub error_level_blob_name: String,

    /// Name of what you want to be used
    /// **as a blob** of the `Diagnostic` struct in `lib-ruby-parser`
    pub diagnostic_message_blob_name: String,

    /// Name of what you want to be used
    /// **as a blob** of the `SourceLine` struct in `lib-ruby-parser`
    pub source_line_blob_name: String,

    /// Name of what you want to be used
    /// **as a blob** of the `List<Byte>` struct in `lib-ruby-parser`
    pub byte_list_blob_name: String,

    /// Name of what you want to be used
    /// **as a blob** of the `List<Token>` struct in `lib-ruby-parser`
    pub token_list_blob_name: String,

    /// Name of what you want to be used
    /// **as a blob** of the `List<Node>` struct in `lib-ruby-parser`
    pub node_list_blob_name: String,

    /// Name of what you want to be used
    /// **as a blob** of the `List<Diagnostic>` struct in `lib-ruby-parser`
    pub diagnostic_list_blob_name: String,

    /// Name of what you want to be used
    /// **as a blob** of the `List<Comment>` struct in `lib-ruby-parser`
    pub comment_list_blob_name: String,

    /// Name of what you want to be used
    /// **as a blob** of the `List<MagicComment>` struct in `lib-ruby-parser`
    pub magic_comment_list_blob_name: String,

    /// Name of what you want to be used
    /// **as a blob** of the `List<SourceLine>` struct in `lib-ruby-parser`
    pub source_line_list_blob_name: String,

    /// Function that takes a `Message` from `lib-ruby-parser-nodes`
    /// and returns a name of what you want to be used
    /// **as a blob** of its representation
    /// in in `lib-ruby-parser`
    ///
    /// For example, for `UnexpectedToken` message you may want to
    /// call your blob class/struct in C++ as `UnexpectedToken_BLOB`
    ///
    /// Default: fn that returns `<message.camelcase_name>_BLOB`
    pub message_variant_blob_name_fn: Box<dyn Fn(&Message) -> String>,

    /// Function that takes a `Node` from `lib-ruby-parser-nodes`
    /// and returns a name of what you want to be used
    /// **as a blob** of its representation
    /// in in `lib-ruby-parser`
    ///
    /// For example, for `Arg` message you may want to
    /// call your blob class/struct in C++ as `Arg_BLOB`
    ///
    /// Default: fn that returns `<node.camelcase_name>_BLOB`
    pub node_variant_blob_name_fn: Box<dyn Fn(&Node) -> String>,
}

impl Default for Options {
    fn default() -> Self {
        Self {
            ifndef_name: String::from("LIB_RUBY_PARSER_BINDINGS_H"),
            pre_code: String::from("// pre-code"),
            post_code: String::from("// post-code"),

            // blob names
            ptr_blob_name: String::from("Ptr_BLOB"),
            maybe_ptr_blob_name: String::from("MaybePtr_BLOB"),
            string_ptr_blob_name: String::from("StringPtr_BLOB"),
            maybe_string_ptr_blob_name: String::from("MaybeStringPtr_BLOB"),
            shared_byte_list_blob_name: String::from("SharedByteList_BLOB"),
            byte_blob_name: String::from("Byte_BLOB"),
            token_blob_name: String::from("Token_BLOB"),
            node_blob_name: String::from("Node_BLOB"),
            diagnostic_blob_name: String::from("Diagnostic_BLOB"),
            comment_type_blob_name: String::from("CommentType_BLOB"),
            comment_blob_name: String::from("Comment_BLOB"),
            magic_comment_kind_blob_name: String::from("MagicCommentKind_BLOB"),
            magic_comment_blob_name: String::from("MagicComment_BLOB"),
            source_line_blob_name: String::from("SourceLine_BLOB"),
            bytes_blob_name: String::from("Bytes_BLOB"),
            loc_blob_name: String::from("Loc_BLOB"),
            maybe_loc_blob_name: String::from("MaybeLoc_BLOB"),
            error_level_blob_name: String::from("ErrorLevel_BLOB"),
            diagnostic_message_blob_name: String::from("DiagnosticMessage_BLOB"),

            // list blob names
            byte_list_blob_name: String::from("ByteList_BLOB"),
            token_list_blob_name: String::from("TokenList_BLOB"),
            node_list_blob_name: String::from("NodeList_BLOB"),
            diagnostic_list_blob_name: String::from("DiagnosticList_BLOB"),
            comment_list_blob_name: String::from("CommentList_BLOB"),
            magic_comment_list_blob_name: String::from("MagicCommentList_BLOB"),
            source_line_list_blob_name: String::from("SourceLineList_BLOB"),

            // messages
            message_variant_blob_name_fn: Box::new(&|message: &Message| {
                format!("{}_BLOB", message.camelcase_name())
            }),

            // nodes
            node_variant_blob_name_fn: Box::new(&|node: &Node| {
                format!("{}_BLOB", node.camelcase_name)
            }),
        }
    }
}

impl Options {
    /// Applies `self.message_variant_blob_name_fn` on a given `message`
    ///
    /// Utility function
    pub fn message_variant_blob_name(&self, message: &Message) -> String {
        (&*self.message_variant_blob_name_fn)(message)
    }

    /// Applies `self.node_variant_blob_name` on a given `node`
    ///
    /// Utility function
    pub fn node_variant_blob_name(&self, node: &Node) -> String {
        (&*self.node_variant_blob_name_fn)(node)
    }
}