forked from vrotaru/btcticker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPathQuery.ragel
102 lines (87 loc) · 2.54 KB
/
PathQuery.ragel
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Generated using:
// ragel -J -o src/com/appspot/btcticker/fsm/PathQuery.java PathQuery.ragel
//
package com.appspot.btcticker.fsm;
import com.appspot.btcticker.enums.Currency;
import com.appspot.btcticker.enums.Rate;
public class PathQuery {
public Currency from = Currency.BITCOIN;
public Currency to = Currency.DOLLAR;
public Rate rate = Rate.TICKER;
public double amount = -9;
%%{
machine query;
write data;
action clear { i = 0; f = 0.0; scale = 1.0; }
action ipart { i = i * 10 + (int)data[p] - zero; }
action fpart { scale = scale * 0.1; f = f + scale * ((int)data[p] - zero); }
number = digit+ >clear $ipart ('.' digit+ $fpart)?
;
btc = "btc"
( '2'
( "usd" > { }
| "eur" > { to = Currency.EURO; }
| "gbp" > { to = Currency.POUND; }
| "dkk" > { to = Currency.DKK; }
)
)?
;
btc4 = "btc"
( '4'
( "usd" > { }
| "eur" > { to = Currency.EURO; }
| "gbp" > { to = Currency.POUND; }
| "dkk" > { to = Currency.DKK; }
)
)?
;
usd = "usd" % { from = Currency.DOLLAR; to = Currency.BITCOIN; }
"2btc"?
;
eur = "eur" % { from = Currency.EURO; to = Currency.BITCOIN; }
"2btc"?
;
gbp = "gbp" % { from = Currency.POUND; to = Currency.BITCOIN; }
"2btc"?
;
dkk = "dkk" % { from = Currency.DKK; to = Currency.BITCOIN; }
;
ticker = "/" number % { amount = i + f; }
( btc | usd | eur | gbp | dkk ) ?
".png" ?
;
buy = "/buy" % { rate = Rate.BUY; } number % { amount = i + f; }
btc4 ?
".png" ?
;
sell = "/sell" % { rate = Rate.SELL; } number % { amount = i + f; }
btc4 ?
".png" ?
;
main := ticker | buy | sell
;
}%%
public boolean parse(String urlPart) {
char[] data = urlPart.toCharArray();
// Used by Ragel
int cs;
int p = 0;
int pe = data.length;
int eof = data.length;
// Used to store extracted data
final int zero = (int) '0';
int i = 0;
double f = 0.0;
double scale = 1.0;
// reset state
from = Currency.BITCOIN;
to = Currency.DOLLAR;
amount = -1;
%% write init;
%% write exec;
if (cs < query_first_final) {
return false;
}
return true;
}
}