| 1 | package Yote::YapiServer::YapiDef; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | #====================================================================== | ||
| 7 | # Public API | ||
| 8 | #====================================================================== | ||
| 9 | |||
| 10 | # Parse a .ydef file and return an arrayref of definition hashrefs, | ||
| 11 | # each matching the structure that YAML::LoadFile would produce. | ||
| 12 | sub parse_file { | ||
| 13 | my ($filename) = @_; | ||
| 14 | open my $fh, '<', $filename or die "Cannot open $filename: $!\n"; | ||
| 15 | my $text = do { local $/; <$fh> }; | ||
| 16 | close $fh; | ||
| 17 | return parse_string($text, $filename); | ||
| 18 | } | ||
| 19 | |||
| 20 | sub parse_string { | ||
| 21 | my ($text, $source) = @_; | ||
| 22 | $source //= '<string>'; | ||
| 23 | |||
| 24 | my @lines = split /\n/, $text; | ||
| 25 | my $pos = 0; | ||
| 26 | my @defs; | ||
| 27 | my $current_app; # when inside an app block, nested objects attach here | ||
| 28 | |||
| 29 | while ($pos <= $#lines) { | ||
| 30 | my $line = $lines[$pos]; | ||
| 31 | |||
| 32 | # Skip blank lines and comments | ||
| 33 | if ($line =~ /^\s*$/ || $line =~ /^\s*#/) { | ||
| 34 | $pos++; | ||
| 35 | next; | ||
| 36 | } | ||
| 37 | |||
| 38 | # app PACKAGE { | ||
| 39 | if ($line =~ /^\s*app\s+(\S+)\s*\{/) { | ||
| 40 | my $pkg = $1; | ||
| 41 | my ($block, $new_pos) = parse_block(\@lines, $pos + 1); | ||
| 42 | my $def = process_app_block($block, $pkg); | ||
| 43 | push @defs, $def; | ||
| 44 | $current_app = $def; | ||
| 45 | $pos = $new_pos; | ||
| 46 | next; | ||
| 47 | } | ||
| 48 | |||
| 49 | # object NAME { ... } — standalone or after app | ||
| 50 | if ($line =~ /^\s*object\s+(\S+)\s*\{/) { | ||
| 51 | my $name = $1; | ||
| 52 | my ($block, $new_pos) = parse_block(\@lines, $pos + 1); | ||
| 53 | if ($current_app && $name !~ /::/) { | ||
| 54 | # Nested object — attach to the current app | ||
| 55 | my $obj_def = process_object_block($block); | ||
| 56 | $current_app->{objects}{$name} = $obj_def; | ||
| 57 | } else { | ||
| 58 | # Standalone object | ||
| 59 | my $def = process_object_block($block); | ||
| 60 | $def->{type} = 'object'; | ||
| 61 | $def->{package} = $name; | ||
| 62 | push @defs, $def; | ||
| 63 | } | ||
| 64 | $pos = $new_pos; | ||
| 65 | next; | ||
| 66 | } | ||
| 67 | |||
| 68 | # server PACKAGE { | ||
| 69 | if ($line =~ /^\s*server\s+(\S+)\s*\{/) { | ||
| 70 | my $pkg = $1; | ||
| 71 | my ($block, $new_pos) = parse_block(\@lines, $pos + 1); | ||
| 72 | my $def = process_server_block($block, $pkg); | ||
| 73 | push @defs, $def; | ||
| 74 | $current_app = undef; | ||
| 75 | $pos = $new_pos; | ||
| 76 | next; | ||
| 77 | } | ||
| 78 | |||
| 79 | # Anything else at top level is unexpected | ||
| 80 | die "Parse error in $source at line " . ($pos + 1) . ": unexpected '$line'\n"; | ||
| 81 | } | ||
| 82 | |||
| 83 | return \@defs; | ||
| 84 | } | ||
| 85 | |||
| 86 | #====================================================================== | ||
| 87 | # Block parsing — read lines until matching closing brace | ||
| 88 | #====================================================================== | ||
| 89 | |||
| 90 | # Returns (arrayref of lines inside the block, next position after closing brace) | ||
| 91 | sub parse_block { | ||
| 92 | my ($lines, $pos) = @_; | ||
| 93 | my @block; | ||
| 94 | my $depth = 1; | ||
| 95 | |||
| 96 | while ($pos <= $#$lines && $depth > 0) { | ||
| 97 | my $line = $lines->[$pos]; | ||
| 98 | my $delta = brace_delta($line); | ||
| 99 | $depth += $delta; | ||
| 100 | |||
| 101 | if ($depth <= 0) { | ||
| 102 | # The closing brace line — capture any content before the } | ||
| 103 | my $before = $line; | ||
| 104 | $before =~ s/\}\s*$//; | ||
| 105 | push @block, $before if $before =~ /\S/; | ||
| 106 | $pos++; | ||
| 107 | last; | ||
| 108 | } | ||
| 109 | |||
| 110 | push @block, $line; | ||
| 111 | $pos++; | ||
| 112 | } | ||
| 113 | |||
| 114 | return (\@block, $pos); | ||
| 115 | } | ||
| 116 | |||
| 117 | # Count net brace depth change on a line, skipping braces in strings and comments | ||
| 118 | sub brace_delta { | ||
| 119 | my ($line) = @_; | ||
| 120 | my $delta = 0; | ||
| 121 | my ($in_sq, $in_dq) = (0, 0); | ||
| 122 | |||
| 123 | for my $i (0 .. length($line) - 1) { | ||
| 124 | my $ch = substr($line, $i, 1); | ||
| 125 | my $prev = $i > 0 ? substr($line, $i - 1, 1) : ''; | ||
| 126 | |||
| 127 | # Comment — stop counting (but $# is Perl's array-last-index, not a comment) | ||
| 128 | last if $ch eq '#' && !$in_sq && !$in_dq && $prev ne '$'; | ||
| 129 | |||
| 130 | if ($ch eq "'" && !$in_dq && $prev ne '\\') { $in_sq = !$in_sq } | ||
| 131 | elsif ($ch eq '"' && !$in_sq && $prev ne '\\') { $in_dq = !$in_dq } | ||
| 132 | elsif (!$in_sq && !$in_dq) { | ||
| 133 | $delta++ if $ch eq '{'; | ||
| 134 | $delta-- if $ch eq '}'; | ||
| 135 | } | ||
| 136 | } | ||
| 137 | |||
| 138 | return $delta; | ||
| 139 | } | ||
| 140 | |||
| 141 | #====================================================================== | ||
| 142 | # Process top-level block types | ||
| 143 | #====================================================================== | ||
| 144 | |||
| 145 | sub process_app_block { | ||
| 146 | my ($block_lines, $pkg) = @_; | ||
| 147 | my $def = { | ||
| 148 | type => 'app', | ||
| 149 | package => $pkg, | ||
| 150 | }; | ||
| 151 | parse_block_content($block_lines, $def); | ||
| 152 | return $def; | ||
| 153 | } | ||
| 154 | |||
| 155 | sub process_object_block { | ||
| 156 | my ($block_lines) = @_; | ||
| 157 | my $def = {}; | ||
| 158 | parse_block_content($block_lines, $def); | ||
| 159 | return $def; | ||
| 160 | } | ||
| 161 | |||
| 162 | sub process_server_block { | ||
| 163 | my ($block_lines, $pkg) = @_; | ||
| 164 | my $def = { | ||
| 165 | type => 'server', | ||
| 166 | package => $pkg, | ||
| 167 | }; | ||
| 168 | parse_server_content($block_lines, $def); | ||
| 169 | return $def; | ||
| 170 | } | ||
| 171 | |||
| 172 | #====================================================================== | ||
| 173 | # Parse block content (app or object interior) | ||
| 174 | #====================================================================== | ||
| 175 | |||
| 176 | sub parse_block_content { | ||
| 177 | my ($lines, $def) = @_; | ||
| 178 | my $pos = 0; | ||
| 179 | |||
| 180 | while ($pos <= $#$lines) { | ||
| 181 | my $line = $lines->[$pos]; | ||
| 182 | |||
| 183 | # Skip blank lines and comments | ||
| 184 | if ($line =~ /^\s*$/ || $line =~ /^\s*#/) { | ||
| 185 | $pos++; | ||
| 186 | next; | ||
| 187 | } | ||
| 188 | |||
| 189 | # cols { ... } | ||
| 190 | if ($line =~ /^\s*cols\s*\{/) { | ||
| 191 | my ($inner, $new_pos) = parse_block($lines, $pos + 1); | ||
| 192 | $def->{cols} = parse_key_value_lines($inner); | ||
| 193 | $pos = $new_pos; | ||
| 194 | next; | ||
| 195 | } | ||
| 196 | |||
| 197 | # field_access { ... } | ||
| 198 | if ($line =~ /^\s*field_access\s*\{/) { | ||
| 199 | my ($inner, $new_pos) = parse_block($lines, $pos + 1); | ||
| 200 | $def->{field_access} = parse_key_value_lines($inner); | ||
| 201 | $pos = $new_pos; | ||
| 202 | next; | ||
| 203 | } | ||
| 204 | |||
| 205 | # values { ... } → public_vars | ||
| 206 | if ($line =~ /^\s*values\s*\{/) { | ||
| 207 | my ($inner, $new_pos) = parse_block($lines, $pos + 1); | ||
| 208 | $def->{public_vars} = parse_values_block($inner); | ||
| 209 | $pos = $new_pos; | ||
| 210 | next; | ||
| 211 | } | ||
| 212 | |||
| 213 | # vars { ... } | ||
| 214 | if ($line =~ /^\s*vars\s*\{/) { | ||
| 215 | my ($inner, $new_pos) = parse_block($lines, $pos + 1); | ||
| 216 | $def->{vars} = parse_values_block($inner); | ||
| 217 | $pos = $new_pos; | ||
| 218 | next; | ||
| 219 | } | ||
| 220 | |||
| 221 | # uses { ... } | ||
| 222 | if ($line =~ /^\s*uses\s*\{/) { | ||
| 223 | my ($inner, $new_pos) = parse_block($lines, $pos + 1); | ||
| 224 | $def->{uses} = parse_uses_block($inner); | ||
| 225 | $pos = $new_pos; | ||
| 226 | next; | ||
| 227 | } | ||
| 228 | |||
| 229 | # method ACCESS NAME { CODE } | ||
| 230 | if ($line =~ /^\s*method\s+(\S+)\s+(\w+)\s*\{/) { | ||
| 231 | my ($access_str, $name) = ($1, $2); | ||
| 232 | my ($code_lines, $new_pos) = parse_code_block($lines, $pos); | ||
| 233 | my $code = join("\n", @$code_lines); | ||
| 234 | |||
| 235 | my $access = parse_access($access_str); | ||
| 236 | |||
| 237 | $def->{methods}{$name} = { | ||
| 238 | access => $access, | ||
| 239 | code => $code, | ||
| 240 | }; | ||
| 241 | $pos = $new_pos; | ||
| 242 | next; | ||
| 243 | } | ||
| 244 | |||
| 245 | # sub NAME { CODE } | ||
| 246 | if ($line =~ /^\s*sub\s+(\w+)\s*\{/) { | ||
| 247 | my $name = $1; | ||
| 248 | my ($code_lines, $new_pos) = parse_code_block($lines, $pos); | ||
| 249 | my $code = join("\n", @$code_lines); | ||
| 250 | $def->{subs}{$name} = $code; | ||
| 251 | $pos = $new_pos; | ||
| 252 | next; | ||
| 253 | } | ||
| 254 | |||
| 255 | # base PACKAGE (single line, no braces) | ||
| 256 | if ($line =~ /^\s*base\s+(\S+)\s*$/) { | ||
| 257 | $def->{base} = $1; | ||
| 258 | $pos++; | ||
| 259 | next; | ||
| 260 | } | ||
| 261 | |||
| 262 | # Fallthrough — skip unrecognized content | ||
| 263 | $pos++; | ||
| 264 | } | ||
| 265 | } | ||
| 266 | |||
| 267 | #====================================================================== | ||
| 268 | # Parse server content | ||
| 269 | #====================================================================== | ||
| 270 | |||
| 271 | sub parse_server_content { | ||
| 272 | my ($lines, $def) = @_; | ||
| 273 | my $pos = 0; | ||
| 274 | |||
| 275 | while ($pos <= $#$lines) { | ||
| 276 | my $line = $lines->[$pos]; | ||
| 277 | |||
| 278 | if ($line =~ /^\s*$/ || $line =~ /^\s*#/) { | ||
| 279 | $pos++; | ||
| 280 | next; | ||
| 281 | } | ||
| 282 | |||
| 283 | # cols { ... } | ||
| 284 | if ($line =~ /^\s*cols\s*\{/) { | ||
| 285 | my ($inner, $new_pos) = parse_block($lines, $pos + 1); | ||
| 286 | $def->{cols} = parse_key_value_lines($inner); | ||
| 287 | $pos = $new_pos; | ||
| 288 | next; | ||
| 289 | } | ||
| 290 | |||
| 291 | # apps { ... } | ||
| 292 | if ($line =~ /^\s*apps\s*\{/) { | ||
| 293 | my ($inner, $new_pos) = parse_block($lines, $pos + 1); | ||
| 294 | $def->{apps} = parse_key_value_lines($inner); | ||
| 295 | $pos = $new_pos; | ||
| 296 | next; | ||
| 297 | } | ||
| 298 | |||
| 299 | # uses { ... } | ||
| 300 | if ($line =~ /^\s*uses\s*\{/) { | ||
| 301 | my ($inner, $new_pos) = parse_block($lines, $pos + 1); | ||
| 302 | $def->{uses} = parse_uses_block($inner); | ||
| 303 | $pos = $new_pos; | ||
| 304 | next; | ||
| 305 | } | ||
| 306 | |||
| 307 | # base PACKAGE | ||
| 308 | if ($line =~ /^\s*base\s+(\S+)\s*$/) { | ||
| 309 | $def->{base} = $1; | ||
| 310 | $pos++; | ||
| 311 | next; | ||
| 312 | } | ||
| 313 | |||
| 314 | $pos++; | ||
| 315 | } | ||
| 316 | } | ||
| 317 | |||
| 318 | #====================================================================== | ||
| 319 | # Parse inner block formats | ||
| 320 | #====================================================================== | ||
| 321 | |||
| 322 | # Simple key-value: "key rest_of_line" | ||
| 323 | sub parse_key_value_lines { | ||
| 324 | my ($lines) = @_; | ||
| 325 | my %kv; | ||
| 326 | for my $line (@$lines) { | ||
| 327 | next if $line =~ /^\s*$/ || $line =~ /^\s*#/; | ||
| 328 | if ($line =~ /^\s*(\S+)\s+(.+?)\s*$/) { | ||
| 329 | $kv{$1} = $2; | ||
| 330 | } | ||
| 331 | } | ||
| 332 | return \%kv; | ||
| 333 | } | ||
| 334 | |||
| 335 | # Values block: key value, with support for multi-line quoted strings | ||
| 336 | sub parse_values_block { | ||
| 337 | my ($lines) = @_; | ||
| 338 | my %kv; | ||
| 339 | my $pos = 0; | ||
| 340 | |||
| 341 | while ($pos <= $#$lines) { | ||
| 342 | my $line = $lines->[$pos]; | ||
| 343 | |||
| 344 | if ($line =~ /^\s*$/ || $line =~ /^\s*#/) { | ||
| 345 | $pos++; | ||
| 346 | next; | ||
| 347 | } | ||
| 348 | |||
| 349 | # key "multi-line value..." | ||
| 350 | if ($line =~ /^\s*(\S+)\s+"(.*?)"\s*$/) { | ||
| 351 | # Single-line quoted value (complete on one line) | ||
| 352 | $kv{$1} = $2; | ||
| 353 | $pos++; | ||
| 354 | next; | ||
| 355 | } | ||
| 356 | |||
| 357 | if ($line =~ /^\s*(\S+)\s+"(.*)$/) { | ||
| 358 | # Multi-line quoted value — opening quote without closing | ||
| 359 | my $key = $1; | ||
| 360 | my $val = $2; | ||
| 361 | $pos++; | ||
| 362 | while ($pos <= $#$lines) { | ||
| 363 | my $next = $lines->[$pos]; | ||
| 364 | if ($next =~ /^(.*?)"\s*$/) { | ||
| 365 | $val .= "\n" . $1; | ||
| 366 | $pos++; | ||
| 367 | last; | ||
| 368 | } else { | ||
| 369 | $val .= "\n" . $next; | ||
| 370 | $pos++; | ||
| 371 | } | ||
| 372 | } | ||
| 373 | $kv{$key} = $val; | ||
| 374 | next; | ||
| 375 | } | ||
| 376 | |||
| 377 | # Unquoted: key rest_of_line | ||
| 378 | if ($line =~ /^\s*(\S+)\s+(.+?)\s*$/) { | ||
| 379 | $kv{$1} = $2; | ||
| 380 | } | ||
| 381 | |||
| 382 | $pos++; | ||
| 383 | } | ||
| 384 | |||
| 385 | return \%kv; | ||
| 386 | } | ||
| 387 | |||
| 388 | # Uses block: one module per line | ||
| 389 | sub parse_uses_block { | ||
| 390 | my ($lines) = @_; | ||
| 391 | my @mods; | ||
| 392 | for my $line (@$lines) { | ||
| 393 | next if $line =~ /^\s*$/ || $line =~ /^\s*#/; | ||
| 394 | if ($line =~ /^\s*(\S+)/) { | ||
| 395 | push @mods, $1; | ||
| 396 | } | ||
| 397 | } | ||
| 398 | return \@mods; | ||
| 399 | } | ||
| 400 | |||
| 401 | #====================================================================== | ||
| 402 | # Parse code blocks (method/sub bodies) with brace counting | ||
| 403 | #====================================================================== | ||
| 404 | |||
| 405 | # Given lines array and position of the opening line (e.g. "method public foo {"), | ||
| 406 | # return (code_lines_arrayref, next_position) | ||
| 407 | sub parse_code_block { | ||
| 408 | my ($lines, $start_pos) = @_; | ||
| 409 | |||
| 410 | my $opening_line = $lines->[$start_pos]; | ||
| 411 | my @code; | ||
| 412 | my $depth = brace_delta($opening_line); # Usually +1 from the opening { | ||
| 413 | |||
| 414 | # Capture any code on the opening line after the { | ||
| 415 | if ($opening_line =~ /\{\s*(.+)$/) { | ||
| 416 | my $rest = $1; | ||
| 417 | # Check if this rest doesn't just close the brace | ||
| 418 | if ($rest =~ /\S/ && $rest !~ /^\s*\}\s*$/) { | ||
| 419 | push @code, $rest; | ||
| 420 | } | ||
| 421 | } | ||
| 422 | |||
| 423 | my $pos = $start_pos + 1; | ||
| 424 | |||
| 425 | while ($pos <= $#$lines && $depth > 0) { | ||
| 426 | my $line = $lines->[$pos]; | ||
| 427 | my $delta = brace_delta($line); | ||
| 428 | $depth += $delta; | ||
| 429 | |||
| 430 | if ($depth <= 0) { | ||
| 431 | # Closing brace line — capture content before the } | ||
| 432 | my $before = $line; | ||
| 433 | $before =~ s/\}\s*$//; | ||
| 434 | push @code, $before if $before =~ /\S/; | ||
| 435 | $pos++; | ||
| 436 | last; | ||
| 437 | } | ||
| 438 | |||
| 439 | push @code, $line; | ||
| 440 | $pos++; | ||
| 441 | } | ||
| 442 | |||
| 443 | # Strip common leading whitespace from code lines | ||
| 444 | my @trimmed = strip_indent(\@code); | ||
| 445 | |||
| 446 | return (\@trimmed, $pos); | ||
| 447 | } | ||
| 448 | |||
| 449 | # Remove common leading whitespace | ||
| 450 | sub strip_indent { | ||
| 451 | my ($lines) = @_; | ||
| 452 | return () unless @$lines; | ||
| 453 | |||
| 454 | # Find minimum indentation (ignoring blank lines) | ||
| 455 | my $min_indent; | ||
| 456 | for my $line (@$lines) { | ||
| 457 | next unless $line =~ /\S/; | ||
| 458 | my ($spaces) = $line =~ /^(\s*)/; | ||
| 459 | my $len = length($spaces); | ||
| 460 | $min_indent = $len if !defined $min_indent || $len < $min_indent; | ||
| 461 | } | ||
| 462 | $min_indent //= 0; | ||
| 463 | |||
| 464 | return map { | ||
| 465 | my $l = $_; | ||
| 466 | if ($l =~ /\S/) { | ||
| 467 | substr($l, 0, $min_indent) = '' if $min_indent > 0; | ||
| 468 | } | ||
| 469 | $l; | ||
| 470 | } @$lines; | ||
| 471 | } | ||
| 472 | |||
| 473 | #====================================================================== | ||
| 474 | # Parse access level string | ||
| 475 | #====================================================================== | ||
| 476 | |||
| 477 | sub parse_access { | ||
| 478 | my ($str) = @_; | ||
| 479 | if ($str =~ /,/) { | ||
| 480 | # Compound: "auth,owner_only" → { auth => 1, owner_only => 1 } | ||
| 481 | my %access; | ||
| 482 | for my $part (split /,/, $str) { | ||
| 483 | $access{$part} = 1; | ||
| 484 | } | ||
| 485 | return \%access; | ||
| 486 | } | ||
| 487 | # Simple: "public", "auth", "admin_only" | ||
| 488 | return $str; | ||
| 489 | } | ||
| 490 | |||
| 491 | 1; |