comparison src/luan/interp/LuaParser.java @ 6:a315783c9524

more parsing git-svn-id: https://luan-java.googlecode.com/svn/trunk@7 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Mon, 19 Nov 2012 13:08:30 +0000
parents 9ef0fd711101
children bca8fc5d928b
comparison
equal deleted inserted replaced
5:9ef0fd711101 6:a315783c9524
6 import org.parboiled.Rule; 6 import org.parboiled.Rule;
7 import org.parboiled.parserunners.ReportingParseRunner; 7 import org.parboiled.parserunners.ReportingParseRunner;
8 import org.parboiled.support.ParsingResult; 8 import org.parboiled.support.ParsingResult;
9 import org.parboiled.errors.ErrorUtils; 9 import org.parboiled.errors.ErrorUtils;
10 import luan.Lua; 10 import luan.Lua;
11 import luan.LuaNumber;
11 import luan.LuaState; 12 import luan.LuaState;
12 13
13 14
14 public class LuaParser extends BaseParser<Object> { 15 public class LuaParser extends BaseParser<Object> {
15 16
16 public Rule Target() { 17 Rule Target() {
17 return Sequence(ConstExpr(), EOI); 18 return Sequence(ConstExpr(), EOI);
18 } 19 }
19 20
20 public Rule ConstExpr() { 21 Rule ConstExpr() {
21 return Sequence( 22 return Sequence(
22 Const(), 23 Const(),
23 push(new ConstExpr(pop())) 24 push(new ConstExpr(pop()))
24 ); 25 );
25 } 26 }
26 27
27 public Rule Const() { 28 Rule Const() {
28 return FirstOf( 29 return FirstOf(
29 NilConst(), 30 NilConst(),
30 BinaryConst(), 31 BooleanConst(),
31 NumberConst() 32 NumberConst(),
33 StringConst()
32 ); 34 );
33 } 35 }
34 36
35 public Rule NilConst() { 37 Rule NilConst() {
36 return Sequence( 38 return Sequence(
37 String("nil"), 39 String("nil"),
38 push(null) 40 push(null)
39 ); 41 );
40 } 42 }
41 43
42 public Rule BinaryConst() { 44 Rule BooleanConst() {
43 return FirstOf( 45 return FirstOf(
44 TrueConst(), 46 Sequence(
45 FalseConst() 47 String("true"),
48 push(true)
49 ),
50 Sequence(
51 String("false"),
52 push(false)
53 )
46 ); 54 );
47 } 55 }
48 56
49 public Rule TrueConst() { 57 Rule NumberConst() {
50 return Sequence( 58 return Sequence(
51 String("true"), 59 Number(),
52 push(true) 60 push(new LuaNumber((Double)pop()))
53 ); 61 );
54 } 62 }
55 63
56 public Rule FalseConst() { 64 Rule Number() {
57 return Sequence( 65 return FirstOf(
58 String("false"), 66 Sequence(
59 push(false) 67 IgnoreCase("0x"),
68 OneOrMore(HexDigit()),
69 push((double)Long.parseLong(match(),16))
70 ),
71 Sequence(
72 DecNumber(),
73 push(Double.parseDouble(match()))
74 )
60 ); 75 );
61 } 76 }
62 77
63 public Rule NumberConst() { 78 Rule DecNumber() {
64 return Sequence(
65 Number(),
66 push(Double.parseDouble(match()))
67 );
68 }
69
70 public Rule Number() {
71 return FirstOf( 79 return FirstOf(
72 Sequence( 80 Sequence(
73 Int(), 81 Int(),
74 Optional( 82 Optional(
75 Ch('.'), 83 Ch('.'),
83 NumberExp() 91 NumberExp()
84 ) 92 )
85 ); 93 );
86 } 94 }
87 95
88 public Rule NumberExp() { 96 Rule NumberExp() {
89 return Optional( 97 return Optional(
90 IgnoreCase('e'), 98 IgnoreCase('e'),
91 Optional(AnyOf("+-")), 99 Optional(AnyOf("+-")),
92 Int() 100 Int()
93 ); 101 );
94 } 102 }
95 103
96 Rule Int() { 104 Rule Int() {
97 return OneOrMore(Digit()); 105 return OneOrMore(Digit());
98 } 106 }
99 107
100 Rule Digit() { 108 Rule HexDigit() {
101 return CharRange('0', '9'); 109 return FirstOf(
102 } 110 Digit(),
111 AnyOf("abcdefABCDEF")
112 );
113 }
114
115 Rule Digit() {
116 return CharRange('0', '9');
117 }
118
119 Rule StringConst() {
120 return FirstOf(
121 QuotedString('"'),
122 QuotedString('\'')
123 );
124 }
125
126 Rule QuotedString(char quote) {
127 return Sequence(
128 Ch(quote),
129 push(new StringBuffer()),
130 ZeroOrMore(
131 FirstOf(
132 Sequence(
133 NoneOf("\\\n"+quote),
134 append(matchedChar())
135 ),
136 EscSeq()
137 )
138 ),
139 Ch(quote),
140 push(((StringBuffer)pop()).toString())
141 );
142 }
143
144 Rule EscSeq() {
145 return Sequence(
146 Ch('\\'),
147 FirstOf(
148 Sequence( Ch('b'), append('\b') ),
149 Sequence( Ch('f'), append('\f') ),
150 Sequence( Ch('n'), append('\n') ),
151 Sequence( Ch('r'), append('\r') ),
152 Sequence( Ch('t'), append('\t') ),
153 Sequence( Ch('\\'), append('\\') ),
154 Sequence( Ch('"'), append('"') ),
155 Sequence( Ch('\''), append('\'') )
156 )
157 );
158 }
159
160 boolean append(char ch) {
161 StringBuffer sb = (StringBuffer)peek();
162 sb.append(ch);
163 return true;
164 }
103 165
104 // for testing 166 // for testing
105 public static void main(String[] args) throws Exception { 167 public static void main(String[] args) throws Exception {
106 LuaParser parser = Parboiled.createParser(LuaParser.class); 168 LuaParser parser = Parboiled.createParser(LuaParser.class);
107 while( true ) { 169 while( true ) {
108 String input = new Scanner(System.in).nextLine(); 170 String input = new Scanner(System.in).nextLine();
109 ParsingResult<?> result = new ReportingParseRunner(parser.Target()).run(input); 171 ParsingResult<?> result = new ReportingParseRunner(parser.Target()).run(input);
110 if( result.hasErrors() ) { 172 if( result.hasErrors() ) {
111 System.out.println("Parse Errors:\n" + ErrorUtils.printParseErrors(result)); 173 System.out.println("Parse Errors:\n" + ErrorUtils.printParseErrors(result));
112 } else { 174 } else {
113 Expr expr = (Expr)result.resultValue; 175 Expr expr = (Expr)result.resultValue;