comparison src/luan/interp/LuanParser.java @ 82:8ea2e94f3318

add -> operator git-svn-id: https://luan-java.googlecode.com/svn/trunk@83 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Mon, 18 Feb 2013 06:29:50 +0000
parents 1ff53a88579a
children b84f66704026
comparison
equal deleted inserted replaced
81:9df729fa4419 82:8ea2e94f3318
725 } 725 }
726 726
727 @Cached 727 @Cached
728 Rule Var(boolean inParens) { 728 Rule Var(boolean inParens) {
729 Var<Integer> start = new Var<Integer>(); 729 Var<Integer> start = new Var<Integer>();
730 return Sequence( 730 Var<ExpList.Builder> builder = new Var<ExpList.Builder>();
731 start.set(currentIndex()), 731 return Sequence(
732 FirstOf( 732 start.set(currentIndex()),
733 Sequence( 733 VarStart(inParens),
734 '(', Spaces(true), Expr(true), ')', Spaces(inParens),
735 push(expr(pop())),
736 push(null) // marker
737 ),
738 Sequence(
739 push(null), // marker
740 Name(inParens)
741 )
742 ),
743 ZeroOrMore( 734 ZeroOrMore(
744 makeVarExp(start.get()), 735 makeVarExp(start.get()),
745 FirstOf( 736 FirstOf(
746 SubExpr(inParens), 737 VarExt(inParens),
747 Sequence( '.', Spaces(inParens), NameExpr(inParens) ),
748 Sequence( 738 Sequence(
749 Args(inParens,start), 739 builder.set(new ExpList.Builder()),
750 push(null) // marker 740 Args(inParens,start,builder)
741 ),
742 Sequence(
743 "->", Spaces(inParens),
744 builder.set(new ExpList.Builder()),
745 addToExpList(builder.get()),
746 VarExpB(inParens),
747 Args(inParens,start,builder)
751 ) 748 )
752 ) 749 )
750 )
751 );
752 }
753
754 @Cached
755 Rule VarExpB(boolean inParens) {
756 Var<Integer> start = new Var<Integer>();
757 return Sequence(
758 start.set(currentIndex()),
759 VarStart(inParens),
760 ZeroOrMore(
761 makeVarExp(start.get()),
762 VarExt(inParens)
763 ),
764 makeVarExp(start.get())
765 );
766 }
767
768 @Cached
769 Rule VarExt(boolean inParens) {
770 return FirstOf(
771 SubExpr(inParens),
772 Sequence( '.', Spaces(inParens), NameExpr(inParens) )
773 );
774 }
775
776 @Cached
777 Rule VarStart(boolean inParens) {
778 return FirstOf(
779 Sequence(
780 '(', Spaces(true), Expr(true), ')', Spaces(inParens),
781 push(expr(pop())),
782 push(null) // marker
783 ),
784 Sequence(
785 push(null), // marker
786 Name(inParens)
753 ) 787 )
754 ); 788 );
755 } 789 }
756 790
757 Expr env() { 791 Expr env() {
780 return push( new GetUpVar(se(start),index) ); 814 return push( new GetUpVar(se(start),index) );
781 return push( new IndexExpr( se(start), env(), new ConstExpr(name) ) ); 815 return push( new IndexExpr( se(start), env(), new ConstExpr(name) ) );
782 } 816 }
783 817
784 // function should be on top of the stack 818 // function should be on top of the stack
785 Rule Args(boolean inParens,Var<Integer> start) { 819 Rule Args(boolean inParens,Var<Integer> start,Var<ExpList.Builder> builder) {
786 return Sequence( 820 return Sequence(
787 FirstOf( 821 FirstOf(
788 Sequence( 822 Sequence(
789 '(', Spaces(true), Expressions(true), ')', Spaces(inParens) 823 '(', Spaces(true), Optional(ExpList(true,builder)), ')', Spaces(inParens)
790 ), 824 ),
791 Sequence( 825 Sequence(
792 TableExpr(inParens), 826 TableExpr(inParens),
793 push( new ExpList.SingleExpList(expr(pop())) ) 827 addToExpList(builder.get())
794 ), 828 ),
795 Sequence( 829 Sequence(
796 StringLiteral(inParens), 830 StringLiteral(inParens),
797 push( new ExpList.SingleExpList(new ConstExpr(pop())) ) 831 addToExpList(builder.get())
798 ) 832 )
799 ), 833 ),
800 push( new FnCall( se(start.get()), expr(pop(1)), (Expressions)pop() ) ) 834 push( new FnCall( se(start.get()), expr(pop()), builder.get().build() ) ),
835 push(null) // marker
801 ); 836 );
802 } 837 }
803 838
804 @Cached 839 @Cached
805 Rule Expressions(boolean inParens) { 840 Rule Expressions(boolean inParens) {
811 846
812 @Cached 847 @Cached
813 Rule ExpList(boolean inParens) { 848 Rule ExpList(boolean inParens) {
814 Var<ExpList.Builder> builder = new Var<ExpList.Builder>(new ExpList.Builder()); 849 Var<ExpList.Builder> builder = new Var<ExpList.Builder>(new ExpList.Builder());
815 return Sequence( 850 return Sequence(
851 ExpList(inParens,builder),
852 push( builder.get().build() )
853 );
854 }
855
856 Rule ExpList(boolean inParens,Var<ExpList.Builder> builder) {
857 return Sequence(
816 Expr(inParens), 858 Expr(inParens),
817 addToExpList(builder.get()), 859 addToExpList(builder.get()),
818 ZeroOrMore( 860 ZeroOrMore(
819 ',', Spaces(inParens), Expr(inParens), 861 ',', Spaces(inParens), Expr(inParens),
820 addToExpList(builder.get()) 862 addToExpList(builder.get())
821 ), 863 )
822 push( builder.get().build() )
823 ); 864 );
824 } 865 }
825 866
826 boolean addToExpList(ExpList.Builder bld) { 867 boolean addToExpList(ExpList.Builder bld) {
827 Object obj = pop(); 868 Object obj = pop();