Mercurial Hosting > luan
comparison core/src/luan/impl/LuanParser.java @ 535:289908469e02
parse numeric constants to ints or longs if possible
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 31 May 2015 00:00:17 -0600 |
parents | 115cae4ac8fd |
children | 0742ac78fa69 |
comparison
equal
deleted
inserted
replaced
534:115cae4ac8fd | 535:289908469e02 |
---|---|
1144 return parser.success(n); | 1144 return parser.success(n); |
1145 } | 1145 } |
1146 | 1146 |
1147 private Number DecNumber() { | 1147 private Number DecNumber() { |
1148 int start = parser.begin(); | 1148 int start = parser.begin(); |
1149 boolean isInt = true; | |
1149 if( Int() ) { | 1150 if( Int() ) { |
1150 if( parser.match('.') ) | 1151 if( parser.match('.') ) { |
1152 isInt = false; | |
1151 Int(); // optional | 1153 Int(); // optional |
1154 } | |
1152 } else if( parser.match('.') && Int() ) { | 1155 } else if( parser.match('.') && Int() ) { |
1153 // ok | 1156 // ok |
1157 isInt = false; | |
1154 } else | 1158 } else |
1155 return parser.failure(null); | 1159 return parser.failure(null); |
1156 Exponent(); // optional | 1160 if( Exponent() ) // optional |
1157 return parser.success(Double.valueOf(parser.textFrom(start))); | 1161 isInt = false; |
1162 String s = parser.textFrom(start); | |
1163 if( isInt ) { | |
1164 try { | |
1165 return parser.success(Integer.valueOf(s)); | |
1166 } catch(NumberFormatException e) {} | |
1167 try { | |
1168 return parser.success(Long.valueOf(s)); | |
1169 } catch(NumberFormatException e) {} | |
1170 } | |
1171 return parser.success(Double.valueOf(s)); | |
1158 } | 1172 } |
1159 | 1173 |
1160 private boolean Exponent() { | 1174 private boolean Exponent() { |
1161 parser.begin(); | 1175 parser.begin(); |
1162 if( !parser.matchIgnoreCase("e") ) | 1176 if( !parser.matchIgnoreCase("e") ) |
1178 return parser.inCharRange('0', '9'); | 1192 return parser.inCharRange('0', '9'); |
1179 } | 1193 } |
1180 | 1194 |
1181 private Number HexNumber() { | 1195 private Number HexNumber() { |
1182 int start = parser.begin(); | 1196 int start = parser.begin(); |
1197 long nLong = 0; | |
1183 double n; | 1198 double n; |
1184 if( HexInt() ) { | 1199 if( HexInt() ) { |
1185 n = (double)Long.parseLong(parser.textFrom(start),16); | 1200 nLong = Long.parseLong(parser.textFrom(start),16); |
1201 n = (double)nLong; | |
1186 if( parser.match('.') ) { | 1202 if( parser.match('.') ) { |
1187 start = parser.currentIndex(); | 1203 start = parser.currentIndex(); |
1188 if( HexInt() ) { | 1204 if( HexInt() ) { |
1189 String dec = parser.textFrom(start); | 1205 String dec = parser.textFrom(start); |
1190 n += (double)Long.parseLong(dec,16) / Math.pow(16,dec.length()); | 1206 n += (double)Long.parseLong(dec,16) / Math.pow(16,dec.length()); |
1200 parser.anyOf("+-"); // optional | 1216 parser.anyOf("+-"); // optional |
1201 start = parser.currentIndex(); | 1217 start = parser.currentIndex(); |
1202 if( !HexInt() ) | 1218 if( !HexInt() ) |
1203 return parser.failure(null); | 1219 return parser.failure(null); |
1204 n *= Math.pow(2,(double)Long.parseLong(parser.textFrom(start))); | 1220 n *= Math.pow(2,(double)Long.parseLong(parser.textFrom(start))); |
1221 } | |
1222 if( nLong == n ) { | |
1223 int nInt = (int)nLong; | |
1224 if( nInt == nLong ) | |
1225 return parser.success(Integer.valueOf(nInt)); | |
1226 return parser.success(Long.valueOf(nLong)); | |
1205 } | 1227 } |
1206 return parser.success(Double.valueOf(n)); | 1228 return parser.success(Double.valueOf(n)); |
1207 } | 1229 } |
1208 | 1230 |
1209 private boolean HexInt() { | 1231 private boolean HexInt() { |