Mercurial > repos > tabletprog
comparison modules/parser.tp @ 227:8c16ef123aee
Implement list literals in grammar
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Sun, 29 Dec 2013 17:09:21 -0800 |
parents | 6055f56d0e45 |
children | decdf28a8517 |
comparison
equal
deleted
inserted
replaced
226:6055f56d0e45 | 227:8c16ef123aee |
---|---|
452 } yield: { | 452 } yield: { |
453 #{ | 453 #{ |
454 left <- Left | 454 left <- Left |
455 op <- "|" | 455 op <- "|" |
456 right <- Right | 456 right <- Right |
457 string <- { | |
458 (string: left) . " " . op . " " . right | |
459 } | |
457 } | 460 } |
458 } | 461 } |
459 addsub <- binaryOps: ["+" "-" "."] withHigherPrec: muldiv | 462 addsub <- binaryOps: ["+" "-" "."] withHigherPrec: muldiv |
460 muldiv <- binaryOps: ["*" "/" "%"] withHigherPrec: primlitsym | 463 muldiv <- binaryOps: ["*" "/" "%"] withHigherPrec: primlitsym |
461 | 464 |
701 } | 704 } |
702 } | 705 } |
703 } | 706 } |
704 } | 707 } |
705 | 708 |
709 unarymeth <- match: Receiver . hws . Method where: { | |
710 Receiver <- match: opexpr | |
711 Method <- match: symexpr | |
712 } yield: { | |
713 #{ | |
714 receiver <- Receiver | |
715 name <- Method name | |
716 args <- [] | |
717 } | |
718 } | |
719 | |
706 methcall <- match: Receiver . hws . Rest where: { | 720 methcall <- match: Receiver . hws . Rest where: { |
707 Receiver <- match: opexpr | 721 Receiver <- match: opexpr |
708 Rest <- matchOne: [funcall ( | 722 Rest <- match: funcall |
709 match: Name where: { Name <- match: symexpr } yield: { | |
710 #{ | |
711 name <- Name name | |
712 args <- [] | |
713 } | |
714 } | |
715 )] | |
716 } yield: { | 723 } yield: { |
717 #{ | 724 #{ |
718 receiver <- Receiver | 725 receiver <- Receiver |
719 name <- Rest name | 726 name <- Rest name |
720 args <- Rest args | 727 args <- Rest args |
752 | 759 |
753 expr <- match: (hws . Expr . ws) where: { | 760 expr <- match: (hws . Expr . ws) where: { |
754 Expr <- matchOne: [ | 761 Expr <- matchOne: [ |
755 funcall | 762 funcall |
756 methcall | 763 methcall |
764 unarymeth | |
765 opexpr | |
766 ] | |
767 } yield: { | |
768 Expr | |
769 } | |
770 | |
771 lexpr <- match: (hws . Expr . ws) where: { | |
772 Expr <- matchOne: [ | |
773 funcall | |
774 methcall | |
757 opexpr | 775 opexpr |
758 ] | 776 ] |
759 } yield: { | 777 } yield: { |
760 Expr | 778 Expr |
761 } | 779 } |
780 messages <- Messages | 798 messages <- Messages |
781 string <- { | 799 string <- { |
782 "#{\n\t". ((messages map: :el { | 800 "#{\n\t". ((messages map: :el { |
783 string: el | 801 string: el |
784 }) join: "\n\t") . "\n}" | 802 }) join: "\n\t") . "\n}" |
803 } | |
804 } | |
805 } | |
806 | |
807 listlit <- match: "[" . ws . Els . "]" where: { | |
808 Els <- zeroPlus: lexpr | |
809 } yield: { | |
810 //Handle limitation of zeroPlus macro | |
811 if: (Els length) = 0 { | |
812 Els <- [] | |
813 } | |
814 #{ | |
815 litval <- Els | |
816 string <- { | |
817 "[\n\t". ((litval map: :el { | |
818 string: el | |
819 }) join: "\n\t") . "\n]" | |
785 } | 820 } |
786 } | 821 } |
787 } | 822 } |
788 | 823 |
789 primlitsym <- match: hws . Lit where: { | 824 primlitsym <- match: hws . Lit where: { |
791 hexlit | 826 hexlit |
792 binary | 827 binary |
793 decimal | 828 decimal |
794 symexpr | 829 symexpr |
795 object | 830 object |
831 listlit | |
796 ] | 832 ] |
797 } yield: { | 833 } yield: { |
798 Lit | 834 Lit |
799 } | 835 } |
800 | 836 |
845 testmatchintlit: "-567" :s {decimal: s} | 881 testmatchintlit: "-567" :s {decimal: s} |
846 testmatchintlit: "123u16" :s {decimal: s} | 882 testmatchintlit: "123u16" :s {decimal: s} |
847 testmatchintlit: "0x20" :s {hexlit: s} | 883 testmatchintlit: "0x20" :s {hexlit: s} |
848 testmatchintlit: "0x42u64" :s {hexlit: s} | 884 testmatchintlit: "0x42u64" :s {hexlit: s} |
849 testmatchintlit: "0b10101" :s {binary: s} | 885 testmatchintlit: "0b10101" :s {binary: s} |
850 code <- "#{ foo <- 123 > 0x42 && 42 < 104\n bar <- 0xABC + 0b1010101\n baz <- 0b1010 * 5\n qux <- fo: 38 shizzle: bam\n}" | 886 code <- "#{ foo <- 123 > 0x42 && 42 < 104\n bar <- 0xABC + 0b1010101\n baz <- 0b1010 * 5\n qux <- fo: 38 shizzle: bam\n quine <- 123 | [4 5 6 fiddle sticks]\n}" |
851 codem <- expr: code | 887 codem <- expr: code |
852 if: (codem matched?) { | 888 if: (codem matched?) { |
853 print: code . "\nmatched with yield:\n" . (codem yield) . "\n" | 889 print: code . "\nmatched with yield:\n" . (codem yield) . "\n" |
854 } else: { | 890 } else: { |
855 print: code . "\ndid not match\n" | 891 print: code . "\ndid not match\n" |