|
| 1 | +pub(crate) struct TypedDTs<'a> { |
| 2 | + nodes: &'a [lib_ruby_parser_nodes::Node], |
| 3 | + messages: &'a [lib_ruby_parser_nodes::Message], |
| 4 | +} |
| 5 | + |
| 6 | +impl<'a> TypedDTs<'a> { |
| 7 | + pub(crate) fn new( |
| 8 | + nodes: &'a [lib_ruby_parser_nodes::Node], |
| 9 | + messages: &'a [lib_ruby_parser_nodes::Message], |
| 10 | + ) -> Self { |
| 11 | + Self { nodes, messages } |
| 12 | + } |
| 13 | + |
| 14 | + pub(crate) fn write(&self) { |
| 15 | + std::fs::write("../types.d.ts", self.contents()).unwrap() |
| 16 | + } |
| 17 | + |
| 18 | + fn contents(&self) -> String { |
| 19 | + format!( |
| 20 | + "// lib-ruby-parser |
| 21 | +
|
| 22 | +export class Loc {{ |
| 23 | + begin: number |
| 24 | + end: number |
| 25 | +
|
| 26 | + constructor(begin: number, end: number); |
| 27 | + source(input: Input): Uint8Array; |
| 28 | +}} |
| 29 | +
|
| 30 | +export class Token {{ |
| 31 | + name: string |
| 32 | + value: Uint8Array |
| 33 | + loc: Loc |
| 34 | +}} |
| 35 | +
|
| 36 | +export type DiagnosticLevel = |
| 37 | + | \"error\" |
| 38 | + | \"warning\"; |
| 39 | +
|
| 40 | +export class Diagnostic {{ |
| 41 | + level: DiagnosticLevel |
| 42 | + message: Message |
| 43 | + rendered: string |
| 44 | + loc: Loc |
| 45 | +}} |
| 46 | +
|
| 47 | +export type CommentKind = |
| 48 | + | \"inline\" |
| 49 | + | \"document\" |
| 50 | + | \"unknown\"; |
| 51 | +
|
| 52 | +export class Comment {{ |
| 53 | + kind: CommentKind |
| 54 | + location: Loc |
| 55 | +}} |
| 56 | +
|
| 57 | +export type MagicCommentKind = |
| 58 | + | \"encoding\" |
| 59 | + | \"frozen-string-literal\" |
| 60 | + | \"warn-indent\" |
| 61 | + | \"shareable-constant-value\"; |
| 62 | +
|
| 63 | +export class MagicComment {{ |
| 64 | + kind: MagicCommentKind |
| 65 | + key_l: Loc |
| 66 | + value_l: Loc |
| 67 | +}} |
| 68 | +
|
| 69 | +export class Input {{}} |
| 70 | +
|
| 71 | +export class ParserResult {{ |
| 72 | + ast: Node | null |
| 73 | + tokens: Token[] |
| 74 | + diagnostics: Diagnostic[] |
| 75 | + comments: Comment[] |
| 76 | + magic_comments: MagicComment[] |
| 77 | + input: Input |
| 78 | +}} |
| 79 | +
|
| 80 | +export interface ParserOptions {{}} |
| 81 | +
|
| 82 | +// Namespace with all kinds of AST nodes |
| 83 | +{node_ifaces} |
| 84 | +
|
| 85 | +// Namespace with all kinds of AST diagnostic messages |
| 86 | +{message_ifaces} |
| 87 | +
|
| 88 | +export type Node = |
| 89 | + {nodes_sum}; |
| 90 | +
|
| 91 | +export type Message = |
| 92 | + {messages_sum}; |
| 93 | +
|
| 94 | +export function bytes_to_utf8_lossy(bytes: Uint8Array): string; |
| 95 | +export function parse(input: Uint8Array, options: ParserOptions): ParserResult; |
| 96 | +", |
| 97 | + node_ifaces = self.node_ifaces().join("\n\n"), |
| 98 | + message_ifaces = self.message_ifaces().join("\n\n"), |
| 99 | + nodes_sum = self.nodes_sum().join("\n "), |
| 100 | + messages_sum = self.messages_sum().join("\n ") |
| 101 | + ) |
| 102 | + } |
| 103 | + |
| 104 | + fn node_ifaces(&self) -> Vec<String> { |
| 105 | + self.nodes.iter().map(node_iface).collect() |
| 106 | + } |
| 107 | + fn message_ifaces(&self) -> Vec<String> { |
| 108 | + self.messages.iter().map(message_iface).collect() |
| 109 | + } |
| 110 | + fn nodes_sum(&self) -> Vec<String> { |
| 111 | + self.nodes.iter().map(nodes_sum_item).collect() |
| 112 | + } |
| 113 | + fn messages_sum(&self) -> Vec<String> { |
| 114 | + self.messages.iter().map(messages_sum_item).collect() |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +fn node_iface(node: &lib_ruby_parser_nodes::Node) -> String { |
| 119 | + let fields = node |
| 120 | + .fields |
| 121 | + .iter() |
| 122 | + .map(|f| { |
| 123 | + let field_type = match f.field_type { |
| 124 | + lib_ruby_parser_nodes::FieldType::Node => "Node", |
| 125 | + lib_ruby_parser_nodes::FieldType::MaybeNode => "Node | null", |
| 126 | + lib_ruby_parser_nodes::FieldType::Nodes => "Node[]", |
| 127 | + lib_ruby_parser_nodes::FieldType::Loc => "Loc", |
| 128 | + lib_ruby_parser_nodes::FieldType::MaybeLoc => "Loc | null", |
| 129 | + lib_ruby_parser_nodes::FieldType::Str => "string", |
| 130 | + lib_ruby_parser_nodes::FieldType::MaybeStr => "string | null", |
| 131 | + lib_ruby_parser_nodes::FieldType::Chars => "string | null", |
| 132 | + lib_ruby_parser_nodes::FieldType::StringValue => "Uint8Array", |
| 133 | + lib_ruby_parser_nodes::FieldType::U8 => "number", |
| 134 | + lib_ruby_parser_nodes::FieldType::Usize => "number", |
| 135 | + lib_ruby_parser_nodes::FieldType::RawString => "string", |
| 136 | + lib_ruby_parser_nodes::FieldType::RegexOptions => "Node", |
| 137 | + }; |
| 138 | + format!( |
| 139 | + "{comment} |
| 140 | + {field_name}: {field_type};", |
| 141 | + comment = comment(&f.comment, 4).join("\n"), |
| 142 | + field_name = f.field_name, |
| 143 | + field_type = field_type |
| 144 | + ) |
| 145 | + }) |
| 146 | + .collect::<Vec<_>>(); |
| 147 | + |
| 148 | + format!( |
| 149 | + "{comment} |
| 150 | +export class {name} {{ |
| 151 | +{fields} |
| 152 | +}}", |
| 153 | + comment = comment(&node.comment, 0).join("\n"), |
| 154 | + name = node.struct_name, |
| 155 | + fields = fields.join("\n\n") |
| 156 | + ) |
| 157 | +} |
| 158 | +fn message_iface(message: &lib_ruby_parser_nodes::Message) -> String { |
| 159 | + let fields = message |
| 160 | + .fields |
| 161 | + .iter() |
| 162 | + .map(|f| { |
| 163 | + let field_type = match f.field_type { |
| 164 | + lib_ruby_parser_nodes::MessageFieldType::Str => "string", |
| 165 | + lib_ruby_parser_nodes::MessageFieldType::Byte => "string", |
| 166 | + }; |
| 167 | + format!( |
| 168 | + "{comment} |
| 169 | + {field_name}: {field_type}", |
| 170 | + comment = comment(&f.comment, 4).join("\n"), |
| 171 | + field_name = f.name, |
| 172 | + field_type = field_type |
| 173 | + ) |
| 174 | + }) |
| 175 | + .collect::<Vec<_>>(); |
| 176 | + |
| 177 | + format!( |
| 178 | + "{comment} |
| 179 | +export class {name} {{ |
| 180 | +{fields} |
| 181 | +}}", |
| 182 | + comment = comment(&message.comment, 0).join("\n"), |
| 183 | + name = message.name, |
| 184 | + fields = fields.join("\n\n") |
| 185 | + ) |
| 186 | +} |
| 187 | +fn nodes_sum_item(node: &lib_ruby_parser_nodes::Node) -> String { |
| 188 | + format!("| {}", node.struct_name) |
| 189 | +} |
| 190 | +fn messages_sum_item(message: &lib_ruby_parser_nodes::Message) -> String { |
| 191 | + format!("| {}", message.name) |
| 192 | +} |
| 193 | + |
| 194 | +fn comment(s: &str, spaces: usize) -> Vec<String> { |
| 195 | + s.lines() |
| 196 | + .map(|l| { |
| 197 | + format!("{}// {}", " ".repeat(spaces), l) |
| 198 | + .trim_end() |
| 199 | + .to_owned() |
| 200 | + }) |
| 201 | + .collect() |
| 202 | +} |
0 commit comments