comparison src/luan/interp/LuaParser.java @ 7:bca8fc5d928b

work on expressions git-svn-id: https://luan-java.googlecode.com/svn/trunk@8 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Tue, 20 Nov 2012 10:06:27 +0000
parents a315783c9524
children 8896068e0a4b
comparison
equal deleted inserted replaced
6:a315783c9524 7:bca8fc5d928b
13 13
14 14
15 public class LuaParser extends BaseParser<Object> { 15 public class LuaParser extends BaseParser<Object> {
16 16
17 Rule Target() { 17 Rule Target() {
18 return Sequence(ConstExpr(), EOI); 18 return Sequence(Spaces(), Expr(), EOI);
19 } 19 }
20 20
21 Rule ConstExpr() { 21 Rule Expr() {
22 return Sequence( 22 return OrExpr();
23 Const(), 23 }
24
25 Rule OrExpr() {
26 return Sequence(
27 AndExpr(),
28 ZeroOrMore( "or", Spaces(), AndExpr(), push( new OrExpr((Expr)pop(1),(Expr)pop()) ) )
29 );
30 }
31
32 Rule AndExpr() {
33 return Sequence(
34 RelExpr(),
35 ZeroOrMore( "and", Spaces(), RelExpr(), push( new AndExpr((Expr)pop(1),(Expr)pop()) ) )
36 );
37 }
38
39 Rule RelExpr() {
40 return Sequence(
41 SumExpr(),
42 ZeroOrMore(
43 FirstOf(
44 Sequence( "==", Spaces(), SumExpr(), push( new EqExpr((Expr)pop(1),(Expr)pop()) ) ),
45 Sequence( "~=", Spaces(), SumExpr(), push( new NotExpr(new EqExpr((Expr)pop(1),(Expr)pop())) ) )
46 )
47 )
48 );
49 }
50
51 Rule SumExpr() {
52 return Sequence(
53 TermExpr(),
54 ZeroOrMore(
55 FirstOf(
56 Sequence( '+', Spaces(), TermExpr(), push( new AddExpr((Expr)pop(1),(Expr)pop()) ) ),
57 Sequence( '-', Spaces(), TermExpr(), push( new SubExpr((Expr)pop(1),(Expr)pop()) ) )
58 )
59 )
60 );
61 }
62
63 Rule TermExpr() {
64 return Sequence(
65 UnaryExpr(),
66 ZeroOrMore(
67 FirstOf(
68 Sequence( '*', Spaces(), UnaryExpr(), push( new MulExpr((Expr)pop(1),(Expr)pop()) ) ),
69 Sequence( '/', Spaces(), UnaryExpr(), push( new DivExpr((Expr)pop(1),(Expr)pop()) ) ),
70 Sequence( '%', Spaces(), UnaryExpr(), push( new ModExpr((Expr)pop(1),(Expr)pop()) ) )
71 )
72 )
73 );
74 }
75
76 Rule UnaryExpr() {
77 return FirstOf(
78 Sequence( '#', Spaces(), SingleExpr(), push( new LenExpr((Expr)pop()) ) ),
79 Sequence( '-', Spaces(), SingleExpr(), push( new UnmExpr((Expr)pop()) ) ),
80 Sequence( "not", Spaces(), SingleExpr(), push( new NotExpr((Expr)pop()) ) ),
81 SingleExpr()
82 );
83 }
84
85 Rule SingleExpr() {
86 return FirstOf(
87 Sequence( '(', Spaces(), Expr(), ')', Spaces() ),
88 LiteralExpr()
89 );
90 }
91
92 Rule LiteralExpr() {
93 return Sequence(
94 Literal(), Spaces(),
24 push(new ConstExpr(pop())) 95 push(new ConstExpr(pop()))
25 ); 96 );
26 } 97 }
27 98
28 Rule Const() { 99 Rule Literal() {
29 return FirstOf( 100 return FirstOf(
30 NilConst(), 101 NilLiteral(),
31 BooleanConst(), 102 BooleanLiteral(),
32 NumberConst(), 103 NumberLiteral(),
33 StringConst() 104 StringLiteral()
34 ); 105 );
35 } 106 }
36 107
37 Rule NilConst() { 108 Rule NilLiteral() {
38 return Sequence( 109 return Sequence( "nil", push(null) );
39 String("nil"), 110 }
40 push(null) 111
41 ); 112 Rule BooleanLiteral() {
42 } 113 return FirstOf(
43 114 Sequence( "true", push(true) ),
44 Rule BooleanConst() { 115 Sequence( "false", push(false) )
45 return FirstOf( 116 );
46 Sequence( 117 }
47 String("true"), 118
48 push(true) 119 Rule NumberLiteral() {
49 ),
50 Sequence(
51 String("false"),
52 push(false)
53 )
54 );
55 }
56
57 Rule NumberConst() {
58 return Sequence( 120 return Sequence(
59 Number(), 121 Number(),
60 push(new LuaNumber((Double)pop())) 122 push(new LuaNumber((Double)pop()))
61 ); 123 );
62 } 124 }
77 139
78 Rule DecNumber() { 140 Rule DecNumber() {
79 return FirstOf( 141 return FirstOf(
80 Sequence( 142 Sequence(
81 Int(), 143 Int(),
82 Optional( 144 Optional( '.', Optional(Int()) ),
83 Ch('.'),
84 Optional(Int())
85 ),
86 NumberExp() 145 NumberExp()
87 ), 146 ),
88 Sequence( 147 Sequence( '.', Int(), NumberExp() )
89 Ch('.'),
90 Int(),
91 NumberExp()
92 )
93 ); 148 );
94 } 149 }
95 150
96 Rule NumberExp() { 151 Rule NumberExp() {
97 return Optional( 152 return Optional(
114 169
115 Rule Digit() { 170 Rule Digit() {
116 return CharRange('0', '9'); 171 return CharRange('0', '9');
117 } 172 }
118 173
119 Rule StringConst() { 174 Rule StringLiteral() {
120 return FirstOf( 175 return FirstOf(
121 QuotedString('"'), 176 QuotedString('"'),
122 QuotedString('\'') 177 QuotedString('\'')
123 ); 178 );
124 } 179 }
125 180
126 Rule QuotedString(char quote) { 181 Rule QuotedString(char quote) {
127 return Sequence( 182 return Sequence(
128 Ch(quote), 183 quote,
129 push(new StringBuffer()), 184 push(new StringBuffer()),
130 ZeroOrMore( 185 ZeroOrMore(
131 FirstOf( 186 FirstOf(
132 Sequence( 187 Sequence(
133 NoneOf("\\\n"+quote), 188 NoneOf("\\\n"+quote),
134 append(matchedChar()) 189 append(matchedChar())
135 ), 190 ),
136 EscSeq() 191 EscSeq()
137 ) 192 )
138 ), 193 ),
139 Ch(quote), 194 quote,
140 push(((StringBuffer)pop()).toString()) 195 push(((StringBuffer)pop()).toString())
141 ); 196 );
142 } 197 }
143 198
144 Rule EscSeq() { 199 Rule EscSeq() {
145 return Sequence( 200 return Sequence(
146 Ch('\\'), 201 '\\',
147 FirstOf( 202 FirstOf(
148 Sequence( Ch('b'), append('\b') ), 203 Sequence( 'a', append('\u0007') ),
149 Sequence( Ch('f'), append('\f') ), 204 Sequence( 'b', append('\b') ),
150 Sequence( Ch('n'), append('\n') ), 205 Sequence( 'f', append('\f') ),
151 Sequence( Ch('r'), append('\r') ), 206 Sequence( 'n', append('\n') ),
152 Sequence( Ch('t'), append('\t') ), 207 Sequence( 'r', append('\r') ),
153 Sequence( Ch('\\'), append('\\') ), 208 Sequence( 't', append('\t') ),
154 Sequence( Ch('"'), append('"') ), 209 Sequence( 'v', append('\u000b') ),
155 Sequence( Ch('\''), append('\'') ) 210 Sequence( '\\', append('\\') ),
211 Sequence( '"', append('"') ),
212 Sequence( '\'', append('\'') ),
213 Sequence(
214 'x',
215 Sequence( HexDigit(), HexDigit() ),
216 append( (char)Integer.parseInt(match(),16) )
217 ),
218 Sequence(
219 Sequence(
220 Digit(),
221 Optional(
222 Digit(),
223 Optional(
224 Digit()
225 )
226 )
227 ),
228 append( (char)Integer.parseInt(match()) )
229 )
156 ) 230 )
157 ); 231 );
158 } 232 }
159 233
160 boolean append(char ch) { 234 boolean append(char ch) {
161 StringBuffer sb = (StringBuffer)peek(); 235 StringBuffer sb = (StringBuffer)peek();
162 sb.append(ch); 236 sb.append(ch);
163 return true; 237 return true;
238 }
239
240 public Rule Spaces() {
241 return ZeroOrMore(AnyOf(" \t"));
164 } 242 }
165 243
166 // for testing 244 // for testing
167 public static void main(String[] args) throws Exception { 245 public static void main(String[] args) throws Exception {
168 LuaParser parser = Parboiled.createParser(LuaParser.class); 246 LuaParser parser = Parboiled.createParser(LuaParser.class);