Mercurial Hosting > luan
comparison src/luan/interp/LuaParser.java @ 5:9ef0fd711101
start LuaParser
git-svn-id: https://luan-java.googlecode.com/svn/trunk@6 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Mon, 19 Nov 2012 09:49:06 +0000 |
parents | |
children | a315783c9524 |
comparison
equal
deleted
inserted
replaced
4:24fd6381caca | 5:9ef0fd711101 |
---|---|
1 package luan.interp; | |
2 | |
3 import java.util.Scanner; | |
4 import org.parboiled.BaseParser; | |
5 import org.parboiled.Parboiled; | |
6 import org.parboiled.Rule; | |
7 import org.parboiled.parserunners.ReportingParseRunner; | |
8 import org.parboiled.support.ParsingResult; | |
9 import org.parboiled.errors.ErrorUtils; | |
10 import luan.Lua; | |
11 import luan.LuaState; | |
12 | |
13 | |
14 public class LuaParser extends BaseParser<Object> { | |
15 | |
16 public Rule Target() { | |
17 return Sequence(ConstExpr(), EOI); | |
18 } | |
19 | |
20 public Rule ConstExpr() { | |
21 return Sequence( | |
22 Const(), | |
23 push(new ConstExpr(pop())) | |
24 ); | |
25 } | |
26 | |
27 public Rule Const() { | |
28 return FirstOf( | |
29 NilConst(), | |
30 BinaryConst(), | |
31 NumberConst() | |
32 ); | |
33 } | |
34 | |
35 public Rule NilConst() { | |
36 return Sequence( | |
37 String("nil"), | |
38 push(null) | |
39 ); | |
40 } | |
41 | |
42 public Rule BinaryConst() { | |
43 return FirstOf( | |
44 TrueConst(), | |
45 FalseConst() | |
46 ); | |
47 } | |
48 | |
49 public Rule TrueConst() { | |
50 return Sequence( | |
51 String("true"), | |
52 push(true) | |
53 ); | |
54 } | |
55 | |
56 public Rule FalseConst() { | |
57 return Sequence( | |
58 String("false"), | |
59 push(false) | |
60 ); | |
61 } | |
62 | |
63 public Rule NumberConst() { | |
64 return Sequence( | |
65 Number(), | |
66 push(Double.parseDouble(match())) | |
67 ); | |
68 } | |
69 | |
70 public Rule Number() { | |
71 return FirstOf( | |
72 Sequence( | |
73 Int(), | |
74 Optional( | |
75 Ch('.'), | |
76 Optional(Int()) | |
77 ), | |
78 NumberExp() | |
79 ), | |
80 Sequence( | |
81 Ch('.'), | |
82 Int(), | |
83 NumberExp() | |
84 ) | |
85 ); | |
86 } | |
87 | |
88 public Rule NumberExp() { | |
89 return Optional( | |
90 IgnoreCase('e'), | |
91 Optional(AnyOf("+-")), | |
92 Int() | |
93 ); | |
94 } | |
95 | |
96 Rule Int() { | |
97 return OneOrMore(Digit()); | |
98 } | |
99 | |
100 Rule Digit() { | |
101 return CharRange('0', '9'); | |
102 } | |
103 | |
104 // for testing | |
105 public static void main(String[] args) throws Exception { | |
106 LuaParser parser = Parboiled.createParser(LuaParser.class); | |
107 while( true ) { | |
108 String input = new Scanner(System.in).nextLine(); | |
109 ParsingResult<?> result = new ReportingParseRunner(parser.Target()).run(input); | |
110 if( result.hasErrors() ) { | |
111 System.out.println("Parse Errors:\n" + ErrorUtils.printParseErrors(result)); | |
112 } else { | |
113 Expr expr = (Expr)result.resultValue; | |
114 LuaState lua = new LuaState(); | |
115 Object val = expr.eval(lua); | |
116 System.out.println("Result: "+Lua.toString(val)); | |
117 } | |
118 } | |
119 } | |
120 } |