Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » numeric representation too big for Integer.
numeric representation too big for Integer. [message #1300376] Thu, 17 April 2014 14:26 Go to next message
Christophe Bouhier is currently offline Christophe Bouhier
Messages: 937
Registered: July 2009
Senior Member
Hi,

I need to process a numeric string which is too big to fit in an int.
A value which fails:

4294967295

This is actually the exact max size of an unsigned int, but java int is
32bit signed, so this won't fit. So I guess I need a unsigned int here.

I tried changing the return type to EBigDecimal and EBigInteger but I
still get errors/markers in the editor.

terminal INT returns ecore::EInt:
'-'? ('0'..'9')+;

Do I need to implement an IValueConverter or is there a way to handle
this in the grammar directly.?

Thank You,
Christophe
Re: numeric representation too big for Integer. [message #1300401 is a reply to message #1300376] Thu, 17 April 2014 14:49 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6388
Registered: July 2009
Senior Member
Hi,

yes you have to

BIG_INT returns ecore::EBigInteger:
	INT
;	


public class MyDslConverters extends DefaultTerminalConverters {
	
	@Inject
	private BigIntegerValueConverter bigIntegerValueConverter;
	
	@ValueConverter(rule = "BIG_INT")
	public IValueConverter<BigInteger> BIG_INT() {
		return bigIntegerValueConverter;
	}
	
	private static class BigIntegerValueConverter extends AbstractLexerBasedConverter<BigInteger> {

		@Override
		public BigInteger toValue(String string, INode node)
				throws ValueConverterException {
			try {				
				return new BigInteger(string);
			} catch (NumberFormatException e) {
				throw new ValueConverterException("Kein BigInteger", node, e);
			}
		}
		
	}

}
Re: numeric representation too big for Integer. [message #1300402 is a reply to message #1300376] Thu, 17 April 2014 14:49 Go to previous messageGo to next message
Ed Willink is currently offline Ed Willink
Messages: 4097
Registered: July 2009
Senior Member
Hi

INT is bound to the IntValueConverter that uses Integer.parseInt, but it
is injectable so you might manage to replace it. However you're liable
to find other places whether the special behavior of INT is assumed so
much better to introduce a new terminal.

Regards

Ed Willink

On 17/04/2014 15:26, Christophe Bouhier wrote:
> Hi,
>
> I need to process a numeric string which is too big to fit in an int.
> A value which fails:
>
> 4294967295
>
> This is actually the exact max size of an unsigned int, but java int
> is 32bit signed, so this won't fit. So I guess I need a unsigned int
> here.
>
> I tried changing the return type to EBigDecimal and EBigInteger but I
> still get errors/markers in the editor.
>
> terminal INT returns ecore::EInt:
> '-'? ('0'..'9')+;
>
> Do I need to implement an IValueConverter or is there a way to handle
> this in the grammar directly.?
>
> Thank You,
> Christophe
Re: numeric representation too big for Integer. [message #1300737 is a reply to message #1300401] Thu, 17 April 2014 19:46 Go to previous message
Christophe Bouhier is currently offline Christophe Bouhier
Messages: 937
Registered: July 2009
Senior Member
On 17-04-14 16:49, Christian Dietrich wrote:
> Hi,
>
> yes you have to
>
> BIG_INT returns ecore::EBigInteger:
> INT
> ;


Thanks!

>
> public class MyDslConverters extends DefaultTerminalConverters {
>
> @Inject
> private BigIntegerValueConverter bigIntegerValueConverter;
>
> @ValueConverter(rule = "BIG_INT")
> public IValueConverter<BigInteger> BIG_INT() {
> return bigIntegerValueConverter;
> }
>
> private static class BigIntegerValueConverter extends
> AbstractLexerBasedConverter<BigInteger> {
>
> @Override
> public BigInteger toValue(String string, INode node)
> throws ValueConverterException {
> try {
> return new BigInteger(string);
> } catch (NumberFormatException e) {
> throw new ValueConverterException("Kein BigInteger",
> node, e);
> }
> }
>
> }
>
> }
>
Previous Topic:Xtext' editor embedded in a Java Application
Next Topic:Literal string conflict
Goto Forum:
  


Current Time: Sat Oct 25 01:38:59 GMT 2014

Powered by FUDForum. Page generated in 0.02299 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software