comparison modules/parser.tp @ 247:b76f683d076e

Finish moving ast object definitions to a separate ast module
author Michael Pavone <pavone@retrodev.com>
date Wed, 08 Jan 2014 19:27:19 -0800
parents 8c81afd6d2d3
children 004946743678
comparison
equal deleted inserted replaced
246:8c81afd6d2d3 247:b76f683d076e
696 } 696 }
697 697
698 symexpr <- match: Name where: { 698 symexpr <- match: Name where: {
699 Name <- match: (onePlus: (charClass: "a-zA-Z_@!?")) . (zeroPlus: ((matchOne: [":" ""]) . (charClass: "a-zA-Z_@!?0-9"))) 699 Name <- match: (onePlus: (charClass: "a-zA-Z_@!?")) . (zeroPlus: ((matchOne: [":" ""]) . (charClass: "a-zA-Z_@!?0-9")))
700 } yield: { 700 } yield: {
701 #{ 701 ast symbol: Name
702 name <- Name
703 string <- {
704 name
705 }
706 }
707 } 702 }
708 703
709 namepart <- match: hws . Symbol . ":" where: { 704 namepart <- match: hws . Symbol . ":" where: {
710 Symbol <- match: symexpr 705 Symbol <- match: symexpr
711 } yield: { 706 } yield: {
732 Parts <- zeroPlus: argpart 727 Parts <- zeroPlus: argpart
733 } yield: { 728 } yield: {
734 if: (Parts length) = 0 { 729 if: (Parts length) = 0 {
735 Parts <- [] 730 Parts <- []
736 } 731 }
737 Initial | Parts foldr: #{ 732 combined <- Initial | Parts foldr: #{
738 name <- "" 733 name <- ""
739 args <- [] 734 args <- []
740 } with: :acc el { 735 } with: :acc el {
741 nextName <- acc name 736 nextName <- acc name
742 nextArgs <- acc args 737 nextArgs <- acc args
743 if: (el isNamePart?) { 738 if: (el isNamePart?) {
744 nextName <- if: ((acc name) length) > 0 { (el val) . ":" . (acc name) } else: { el val } 739 nextName <- if: ((acc name) length) > 0 { (el val) . ":" . (acc name) } else: { el val }
745 } else: { 740 } else: {
746 nextArgs <- (el val) | nextArgs 741 nextArgs <- (el val) | nextArgs
747 } 742 }
748
749 #{ 743 #{
750 name <- nextName 744 name <- nextName
751 args <- nextArgs 745 args <- nextArgs
752 string <- { 746 }
753 str <- "" 747 }
754 curArgs <- args 748 ast funcall: (ast symbol: (combined name)) withArgs: (combined args) hasReceiver?: false
755 nameParts <- name splitOn: ":"
756 foreach: nameParts :idx part {
757 str <- str . part . ":"
758 if: (not: (curArgs empty?)) {
759 str <- str . " " . (curArgs value)
760 curArgs <- curArgs tail
761 }
762 }
763 while: { not: (curArgs empty?) } do: {
764 str <- str . " " . (curArgs value)
765 curArgs <- curArgs tail
766 }
767 str
768 }
769 }
770 }
771 } 749 }
772 750
773 unarymeth <- match: Receiver . hws . Method where: { 751 unarymeth <- match: Receiver . hws . Method where: {
774 Receiver <- match: opexpr 752 Receiver <- match: opexpr
775 Method <- match: symexpr 753 Method <- match: symexpr
776 } yield: { 754 } yield: {
777 #{ 755 ast funcall: Method withArgs: [Receiver] hasReceiver?: true
778 receiver <- Receiver
779 name <- Method name
780 args <- []
781 string <- {
782 (string: receiver) . " " . name
783 }
784 }
785 } 756 }
786 757
787 methcall <- match: Receiver . hws . Rest where: { 758 methcall <- match: Receiver . hws . Rest where: {
788 Receiver <- match: opexpr 759 Receiver <- match: opexpr
789 Rest <- match: funcall 760 Rest <- match: funcall
790 } yield: { 761 } yield: {
791 #{ 762 ast funcall: (Rest tocall) withArgs: Receiver | (Rest args) hasReceiver?: true
792 receiver <- Receiver
793 name <- Rest name
794 args <- Rest args
795 string <- {
796 nameParts <- name splitOn: ":"
797 curArgs <- args
798 str <- (string: receiver) . " "
799 foreach: nameParts :part {
800 str <- str . part . ":"
801 if: (not: (curArgs empty?)) {
802 str <- str . " " . (curArgs value)
803 curArgs <- curArgs tail
804 }
805 }
806 while: { not: (curArgs empty?) } do: {
807 str <- str . " " . (curArgs value)
808 curArgs <- curArgs tail
809 }
810 str
811 }
812 }
813 } 763 }
814 _processOpPieces <- :Left Pieces { 764 _processOpPieces <- :Left Pieces {
815 if: (Pieces length) > 0 { 765 if: (Pieces length) > 0 {
816 Pieces fold: Left with: :acc piece { 766 Pieces fold: Left with: :acc piece {
817 #{ 767 ast binaryOp: (piece op) withArgs: acc (piece right)
818 left <- acc
819 op <- piece op
820 right <- piece right
821 string <- {
822 (string: left) . " " . op . " " . right
823 }
824 }
825 } 768 }
826 } else: { 769 } else: {
827 Left 770 Left
828 } 771 }
829 } 772 }
866 symexpr 809 symexpr
867 opsym 810 opsym
868 ] 811 ]
869 Expr <- match: expr 812 Expr <- match: expr
870 } yield: { 813 } yield: {
871 #{ 814 ast assign: Expr to: Symbol
872 assign <- Expr
873 to <- Symbol
874 string <- {
875 (string: to) . " <- " . assign
876 }
877 }
878 } 815 }
879 816
880 object <- match: "#{" . ws . Messages . "}" where: { 817 object <- match: "#{" . ws . Messages . "}" where: {
881 Messages <- zeroPlus: (match: ws . El where: { 818 Messages <- zeroPlus: (match: ws . El where: {
882 El <- matchOne: [ 819 El <- matchOne: [
886 } yield: { El }) 823 } yield: { El })
887 } yield: { 824 } yield: {
888 if: (Messages length) = 0 { 825 if: (Messages length) = 0 {
889 Messages <- [] 826 Messages <- []
890 } 827 }
891 #{ 828 ast object: Messages
892 messages <- Messages
893 string <- {
894 "#{\n\t". ((messages map: :el {
895 string: el
896 }) join: "\n\t") . "\n}"
897 }
898 }
899 } 829 }
900 830
901 listlit <- match: "[" . ws . Els . "]" where: { 831 listlit <- match: "[" . ws . Els . "]" where: {
902 Els <- zeroPlus: lexpr 832 Els <- zeroPlus: lexpr
903 } yield: { 833 } yield: {
904 //Handle limitation of zeroPlus macro 834 //Handle limitation of zeroPlus macro
905 if: (Els length) = 0 { 835 if: (Els length) = 0 {
906 Els <- [] 836 Els <- []
907 } 837 }
908 #{ 838 ast seqLit: Els array?: false
909 litval <- Els
910 string <- {
911 "[\n\t". ((litval map: :el {
912 string: el
913 }) join: "\n\t") . "\n]"
914 }
915 }
916 } 839 }
917 840
918 arraylit <- match: "#[" . ws . Els . "]" where: { 841 arraylit <- match: "#[" . ws . Els . "]" where: {
919 Els <- zeroPlus: lexpr 842 Els <- zeroPlus: lexpr
920 } yield: { 843 } yield: {
921 //Handle limitation of zeroPlus macro 844 //Handle limitation of zeroPlus macro
922 if: (Els length) = 0 { 845 if: (Els length) = 0 {
923 Els <- [] 846 Els <- []
924 } 847 }
925 #{ 848 ast seqLit: Els array?: true
926 litval <- Els
927 string <- {
928 "#[\n\t". ((litval map: :el {
929 string: el
930 }) join: "\n\t") . "\n]"
931 }
932 }
933 } 849 }
934 850
935 argname <- match: hws . Pre . Initial . Rest where: { 851 argname <- match: hws . Pre . Initial . Rest where: {
936 Pre <- matchOne: [":" ""] 852 Pre <- matchOne: [":" ""]
937 Initial <- onePlus: (charClass: "a-zA-Z_!?@") 853 Initial <- onePlus: (charClass: "a-zA-Z_!?@")
956 Exprs <- zeroPlus: expr 872 Exprs <- zeroPlus: expr
957 } yield: { 873 } yield: {
958 if: (Exprs length) = 0 { 874 if: (Exprs length) = 0 {
959 Exprs <- [] 875 Exprs <- []
960 } 876 }
961 #{ 877 ast lambda: Exprs withArgs: Arglist
962 args <- Arglist
963 expressions <- Exprs
964 string <- {
965 (args join: " ") . "{\n\t" .
966 ((expressions map: :el { string: el }) join: "\n\t") .
967 "}"
968 }
969 }
970 } 878 }
971 879
972 parenexp <- match: "(" . ws . Expr . ws . ")" where: { 880 parenexp <- match: "(" . ws . Expr . ws . ")" where: {
973 Expr <- match: expr 881 Expr <- match: expr
974 } yield: { 882 } yield: {