|
Re: control values by antlr [message #638617 is a reply to message #638567] |
Thu, 11 November 2010 18:52 |
Henrik Lindberg Messages: 2509 Registered: July 2009 |
Senior Member |
|
|
Xtext does not generate a type checker for your grammar. It does not
know what '+' and '>' etc. means. You have to implement the type
checking. It is not a good idea to have the parser perform the type
checking as the grammar would be complicated and would signal type
problems as syntax errors with no chance of providing quick fixes etc.
(i.e. the input would be seen as invalid as opposed to having the wrong
type).
There are several ways of implementing type checking, ranging from quite
simple to very advanced.
If your operators always operate on literals (like 2 3 "hello" etc) then
it is simply a matter of implementing validation of the corresponding
operator - a validator for 'add' could check that the left and right
sides have the same type for instance.
If you have full expressions in your language, you need to have a
mechanism to associate a type with every expression. It is also a good
idea to cache the type if types are inferred.
A basic implementation can use a polymorphic dispatcher and simply
return the type for different expressions. If you want a cache, one can
be implemented using a model adapter.
As an example - type(LiteralExpression o) would return the getClass()
for the value of the literal expression o. A type(BinaryOpExpression o)
would return the return type of the function that implements the binary
op - which means that it must calculate the left and right parameter
types, then find the function and then get its return type. This gets
tricky if you want to support integer, double, float, and other number
types as you probably want the result to have the more general result
(i.e. int + double => double). To make the example a bit more complete,
a type(VariableExpression o) would look at what the VariableExpression
is referencing (i.e. the cross reference), and get the type from there
(assuming that variables are declared with a type like "Integer a").
Yet more advanced is to infer types - e.g.
var a = "hello"; // variable a is a string
var a = { 1, 2, 3.14 }; // a is list of Number
Once you have a mechanism to return type for any expression, you can
implement validation.
I don't know what is available in the Xbase language - which could be a
starting point. The b3 language I am working on has a quite competent
type inference mechanism (implemented as described above) - perhaps
something to take a look at if you do not find information elsewhere
(look at the class B3BackendTypeProvider as a starting point).
I hope that gives an idea about what is required.
Regards
- henrik
On 11/11/10 5:14 PM, Maria wrote:
> Hi,
> In my Xtext editor i would like to write expressions like this:
>
> 3 > 4 // we should compare two integers
> string1 != string2 // we should compare two strings
> Here I must be sure that I don't write for example string > 4 or 3 !=
> string.
>
> So is it possible to avoid this control by using Xtext and using in its
> place the parser antlr ?
> (the grammar Xtext permit to write (Any type of data 'comparaison
> operator' Any type of data) and ANTLR verify that "Any type of data is
> integer or string and prohibit us to write boolean for example ar string
> compared with an Integer)
> If yes, How can I proceed ?
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03098 seconds