Class: Parser::Parser
- Inherits:
-
Object
- Object
- Parser::Parser
- Defined in:
- lib/sdx/compiler/parser.rb
Class Method Summary collapse
- .expect(tokens, toktype) ⇒ Object
- .lookahead(tokens, toktype, n) ⇒ Object
- .parse(tokens, path, lines) ⇒ Object
- .parse_assign(tokens) ⇒ Object
- .parse_block(tokens) ⇒ Object
- .parse_bool(tokens) ⇒ Object
- .parse_call(tokens) ⇒ Object
- .parse_expr(tokens) ⇒ Object
- .parse_factor(tokens) ⇒ Object
- .parse_float(tokens) ⇒ Object
- .parse_fn(tokens) ⇒ Object
- .parse_for(tokens) ⇒ Object
- .parse_if(tokens) ⇒ Object
- .parse_list(tokens) ⇒ Object
- .parse_literal(tokens) ⇒ Object
- .parse_name(tokens) ⇒ Object
- .parse_new(tokens) ⇒ Object
- .parse_nil(tokens) ⇒ Object
- .parse_number(tokens) ⇒ Object
- .parse_object(tokens) ⇒ Object
- .parse_op(tokens) ⇒ Object
- .parse_parens(tokens) ⇒ Object
- .parse_require(tokens) ⇒ Object
- .parse_return(tokens) ⇒ Object
- .parse_string(tokens) ⇒ Object
- .parse_term(tokens) ⇒ Object
- .parse_while(tokens) ⇒ Object
Class Method Details
.expect(tokens, toktype) ⇒ Object
141 142 143 |
# File 'lib/sdx/compiler/parser.rb', line 141 def self.expect(tokens, toktype) self.lookahead(tokens, toktype, 0) end |
.lookahead(tokens, toktype, n) ⇒ Object
134 135 136 137 138 139 |
# File 'lib/sdx/compiler/parser.rb', line 134 def self.lookahead(tokens, toktype, n) if n >= tokens.size return false end tokens[n][1] == toktype end |
.parse(tokens, path, lines) ⇒ Object
706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 |
# File 'lib/sdx/compiler/parser.rb', line 706 def self.parse(tokens, path, lines) parsed = [] while State::state == :ok and tokens.size > 0 e = self.parse_expr tokens if e if e[0].nodetype == :require code = nil path.each do |search| begin code = File.read "#{File.join(search, e[0].value)}.sdx" rescue nil end end unless code error "Cannot find file #{e[0].value}.sdx anywhere in path" State::state = :error return nil end tokens = tokens[e[1]..-1] lexed, _ = Lexer.lex code tokens = [*lexed, *tokens] else parsed << e[0] tokens = tokens[e[1]..-1] end else error %{ Unexpected token #{tokens[0][1]} at #{tokens[0][2]}:#{tokens[0][3]} #{" " * tokens[0][2].to_s.size} | #{tokens[0][2]} | #{lines[tokens[0][2]].rstrip} #{" " * tokens[0][2].to_s.size} | #{" " * tokens[0][3]}^ here} State::state = :error end end parsed end |
.parse_assign(tokens) ⇒ Object
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 |
# File 'lib/sdx/compiler/parser.rb', line 546 def self.parse_assign(tokens) total = 0 unless self.expect tokens, :name return nil end name = tokens[0][0] total += 1 tokens = tokens[1..-1] unless self.expect tokens, :eq return nil end eq = tokens[0][0] total += 1 tokens = tokens[1..-1] res = self.parse_expr tokens unless res return nil end rhs, part = res total += part [ (Node.new :assign, eq, [name, rhs]), total] end |
.parse_block(tokens) ⇒ Object
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/sdx/compiler/parser.rb', line 245 def self.parse_block(tokens) unless (self.expect tokens, :lbrace) return nil end tokens = tokens[1..-1] children = [] total = 1 while true if self.expect tokens, :rbrace total += 1 return [ (Node.new :block, "", children), total ] end e = self.parse_expr tokens if e children << e[0] total += e[1] tokens = tokens[e[1]..-1] else puts "Syntax error at token ", tokens[0] Kernel.exit 1 end end total += 1 [ (Node.new :block, "", children), total ] end |
.parse_bool(tokens) ⇒ Object
169 170 171 172 173 174 175 |
# File 'lib/sdx/compiler/parser.rb', line 169 def self.parse_bool(tokens) if self.expect tokens, :bool [ (Node.new :bool, tokens[0][0], []), 1 ] else nil end end |
.parse_call(tokens) ⇒ Object
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
# File 'lib/sdx/compiler/parser.rb', line 283 def self.parse_call(tokens) res = (self.parse_literal tokens) unless res return nil end callee = res total = callee[1] tokens = tokens[total..-1] callee = callee[0] if self.expect tokens, :lpar args = [] tokens = tokens[1..-1] total += 1 if self.expect tokens, :rpar return [ (Node.new :call, callee, args), total + 1 ] end while true res = (self.parse_expr tokens) unless res return nil end arg, part = res total += part tokens = tokens[part..-1] args << arg if self.expect tokens, :rpar tokens = tokens[1..-1] total += 1 break end unless (self.expect tokens, :comma) return nil end total += 1 tokens = tokens[1..-1] end return [ (Node.new :call, callee, args), total ] else nil end end |
.parse_expr(tokens) ⇒ Object
691 692 693 694 695 696 697 698 699 700 701 702 703 704 |
# File 'lib/sdx/compiler/parser.rb', line 691 def self.parse_expr(tokens) (self.parse_op tokens) || (self.parse_call tokens) || (self.parse_require tokens) || (self.parse_return tokens) || (self.parse_new tokens) || (self.parse_object tokens) || (self.parse_fn tokens) || (self.parse_assign tokens) || (self.parse_literal tokens) || (self.parse_if tokens) || (self.parse_while tokens) || (self.parse_for tokens) end |
.parse_factor(tokens) ⇒ Object
453 454 455 456 457 458 459 460 461 462 463 464 |
# File 'lib/sdx/compiler/parser.rb', line 453 def self.parse_factor(tokens) (self.parse_call tokens) || (self.parse_require tokens) || (self.parse_new tokens) || (self.parse_object tokens) || (self.parse_fn tokens) || (self.parse_assign tokens) || (self.parse_literal tokens) || (self.parse_if tokens) || (self.parse_while tokens) || (self.parse_for tokens) end |
.parse_float(tokens) ⇒ Object
177 178 179 180 181 182 183 |
# File 'lib/sdx/compiler/parser.rb', line 177 def self.parse_float(tokens) if self.expect tokens, :float [ (Node.new :float, tokens[0][0], []), 1 ] else nil end end |
.parse_fn(tokens) ⇒ Object
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 |
# File 'lib/sdx/compiler/parser.rb', line 569 def self.parse_fn(tokens) unless self.expect tokens, :fn return nil end total = 1 tokens = tokens[1..-1] unless self.expect tokens, :name return nil end name = tokens[0][0] total += 1 tokens = tokens[1..-1] unless self.expect tokens, :lpar return nil end total += 1 tokens = tokens[1..-1] args = [] while true if self.expect tokens, :rpar total += 1 tokens = tokens[1..-1] break end unless self.expect tokens, :name return nil end args << tokens[0][0] total += 1 tokens = tokens[1..-1] if self.expect tokens, :rpar total += 1 tokens = tokens[1..-1] break end unless self.expect tokens, :comma return nil end total += 1 tokens = tokens[1..-1] end res = self.parse_expr tokens unless res return nil end body, part = res total += part [ (Node.new :fn, name, [args, body]), total ] end |
.parse_for(tokens) ⇒ Object
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 |
# File 'lib/sdx/compiler/parser.rb', line 425 def self.parse_for(tokens) unless self.expect tokens, :for return nil end total = 1 tokens = tokens[1..-1] name = nil if self.expect tokens, :name and self.lookahead tokens, :in, 1 name = tokens[0][0] total += 2 tokens = tokens[2..-1] end res = self.parse_expr tokens unless res return nil end e, part = res total += part tokens = tokens[part..-1] res = self.parse_expr tokens unless res return nil end block, part = res total += part [ (Node.new :for, e, [name, block]), total ] end |
.parse_if(tokens) ⇒ Object
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 |
# File 'lib/sdx/compiler/parser.rb', line 369 def self.parse_if(tokens) unless self.expect tokens, :if return nil end total = 1 tokens = tokens[1..-1] res = self.parse_expr tokens unless res return nil end e, part = res total += part tokens = tokens[part..-1] res = self.parse_expr tokens unless res return nil end block, part = res total += part tokens = tokens[part..-1] el = nil if self.expect tokens, :else total += 1 tokens = tokens[1..-1] res = self.parse_expr tokens unless res return nil end el, part = res total += part end [ (Node.new :if, e, [block, el]), total ] end |
.parse_list(tokens) ⇒ Object
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/sdx/compiler/parser.rb', line 212 def self.parse_list(tokens) unless (self.expect tokens, :lbrack) return nil end tokens = tokens[1..-1] children = [] total = 1 while true if self.expect tokens, :rbrack total += 1 break end res = self.parse_expr tokens unless res return nil end e, part = res children << e total += part tokens = tokens[part..-1] if self.expect tokens, :rbrack total += 1 break end unless (self.expect tokens, :comma) return nil end total += 1 tokens = tokens[1..-1] end [ (Node.new :list, "", children), total ] end |
.parse_literal(tokens) ⇒ Object
271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/sdx/compiler/parser.rb', line 271 def self.parse_literal(tokens) (self.parse_block tokens) || (self.parse_bool tokens) || (self.parse_float tokens) || (self.parse_name tokens) || (self.parse_number tokens) || (self.parse_list tokens) || (self.parse_string tokens) || (self.parse_nil tokens) || (self.parse_parens tokens) end |
.parse_name(tokens) ⇒ Object
145 146 147 148 149 150 151 |
# File 'lib/sdx/compiler/parser.rb', line 145 def self.parse_name(tokens) if self.expect tokens, :name [ (Node.new :name, tokens[0][0], []), 1 ] else nil end end |
.parse_new(tokens) ⇒ Object
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 |
# File 'lib/sdx/compiler/parser.rb', line 325 def self.parse_new(tokens) unless self.expect tokens, :new return nil end total = 1 tokens = tokens[1..-1] if self.lookahead tokens, :lpar, 1 res = (self.parse_literal tokens) unless res return nil end callee = res callee = callee[0] args = [] tokens = tokens[2..-1] total += 2 if self.expect tokens, :rpar return [ (Node.new :call, callee, args), total + 1 ] end while true res = (self.parse_expr tokens) unless res return nil end arg, part = res total += part tokens = tokens[part..-1] args << arg total += 1 if self.expect tokens, :rpar tokens = tokens[1..-1] break end unless (self.expect tokens, :comma) return nil end tokens = tokens[1..-1] end return [ (Node.new :new, callee, args), total ] else nil end end |
.parse_nil(tokens) ⇒ Object
153 154 155 156 157 158 159 |
# File 'lib/sdx/compiler/parser.rb', line 153 def self.parse_nil(tokens) if self.expect tokens, :nil [ (Node.new :nil, tokens[0][0], []), 1 ] else nil end end |
.parse_number(tokens) ⇒ Object
161 162 163 164 165 166 167 |
# File 'lib/sdx/compiler/parser.rb', line 161 def self.parse_number(tokens) if self.expect tokens, :number [ (Node.new :number, tokens[0][0], []), 1 ] else nil end end |
.parse_object(tokens) ⇒ Object
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 |
# File 'lib/sdx/compiler/parser.rb', line 619 def self.parse_object(tokens) unless self.expect tokens, :object return nil end total = 1 tokens = tokens[1..-1] unless self.expect tokens, :name return nil end name = tokens[0][0] total += 1 tokens = tokens[1..-1] args = [] if self.expect tokens, :lpar total += 1 tokens = tokens[1..-1] while true if self.expect tokens, :rpar total += 1 tokens = tokens[1..-1] break end unless self.expect tokens, :name return nil end args << tokens[0][0] total += 1 tokens = tokens[1..-1] if self.expect tokens, :rpar total += 1 tokens = tokens[1..-1] break end unless self.expect tokens, :comma return nil end total += 1 tokens = tokens[1..-1] end end res = self.parse_expr tokens unless res return nil end body, part = res total += part [ (Node.new :object, name, [args, body]), total ] end |
.parse_op(tokens) ⇒ Object
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 |
# File 'lib/sdx/compiler/parser.rb', line 506 def self.parse_op(tokens) total = 0 res = self.parse_term tokens unless res return nil end lhs, part = res total += part tokens = tokens[part..-1] unless self.expect tokens, :l1op return [lhs, part] end op = tokens[0][0] total += 1 tokens = tokens[1..-1] res = self.parse_term tokens unless res return nil end rhs, part = res total += part tokens = tokens[part..-1] out = (Node.new :op, op, [lhs]) while self.expect tokens, :l1op op = tokens[0][0] total += 1 tokens = tokens[1..-1] res = self.parse_term tokens unless res return nil end rhs2, part = res total += part tokens = tokens[part..-1] rhs = Node.new :op, op, [rhs, rhs2] end out.children << rhs [out, total] end |
.parse_parens(tokens) ⇒ Object
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/sdx/compiler/parser.rb', line 193 def self.parse_parens(tokens) if self.expect tokens, :lpar tokens = tokens[1..-1] res = self.parse_expr tokens unless res return nil end e, part = self.parse_expr tokens tokens = tokens[part..-1] res = self.expect tokens, :rpar unless res return nil end return [e, part + 2] else return nil end end |
.parse_require(tokens) ⇒ Object
668 669 670 671 672 673 674 675 676 677 |
# File 'lib/sdx/compiler/parser.rb', line 668 def self.parse_require(tokens) unless self.expect tokens, :require return nil end tokens = tokens[1..-1] unless self.expect tokens, :string return nil end [ (Node.new :require, tokens[0][0][1..-2], []), 2 ] end |
.parse_return(tokens) ⇒ Object
679 680 681 682 683 684 685 686 687 688 689 |
# File 'lib/sdx/compiler/parser.rb', line 679 def self.parse_return(tokens) unless self.expect tokens, :return return nil end tokens = tokens[1..-1] res = self.parse_expr tokens unless res return nil end [ (Node.new :return, res[0], []), res[1] ] end |
.parse_string(tokens) ⇒ Object
185 186 187 188 189 190 191 |
# File 'lib/sdx/compiler/parser.rb', line 185 def self.parse_string(tokens) if self.expect tokens, :string [ (Node.new :string, tokens[0][0][1..-2], []), 1 ] else nil end end |
.parse_term(tokens) ⇒ Object
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 |
# File 'lib/sdx/compiler/parser.rb', line 466 def self.parse_term(tokens) total = 0 res = self.parse_factor tokens unless res return nil end lhs, part = res total += part tokens = tokens[part..-1] unless self.expect tokens, :l2op return [lhs, part] end op = tokens[0][0] total += 1 tokens = tokens[1..-1] res = self.parse_factor tokens unless res return nil end rhs, part = res total += part tokens = tokens[part..-1] out = (Node.new :op, op, [lhs]) while self.expect tokens, :l2op op = tokens[0][0] total += 1 tokens = tokens[1..-1] res = self.parse_term tokens unless res return nil end rhs2, part = res total += part tokens = tokens[part..-1] rhs = Node.new :op, op, [rhs, rhs2] end out.children << rhs [out, total] end |
.parse_while(tokens) ⇒ Object
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 |
# File 'lib/sdx/compiler/parser.rb', line 403 def self.parse_while(tokens) unless self.expect tokens, :while return nil end total = 1 tokens = tokens[1..-1] res = self.parse_expr tokens unless res return nil end e, part = res total += part tokens = tokens[part..-1] res = self.parse_expr tokens unless res return nil end block, part = res total += part [ (Node.new :while, e, [block]), total ] end |