Search code examples
javaeclipsextextxtend

How to fix error occuring while executing EValidator


i want to create a parser of obj files with xtext. I already wrote a part of the grammar, now i was writing the validator, but while testing my grammar and validator an error occured. Can anyone helps me?

Here is my grammar code:

grammar ce.xtend.projects.mobj.MObj with org.eclipse.xtext.common.Terminals //hidden()

generate mObj "http://www.xtend.ce/projects/mobj/MObj"

ObjMdodel:
    {ObjMdodel}('Start' 
          (subElements+= SubElement)+ 
    'End')
          ;

SubElement:
 Part|Group
;
Part:
'Part' name=ID  "{"  content = PartContent   "}"  
;
Group:
    'Group' name=ID  "{"  content = GroupContent  "}"  ;
PartContent:

        ( 'Type' ":" partType =PartType ';' ) 
        ('CsType' ':' a_csType= A_CsType 
            '{'    '}'';'
        )?
        ('Degree' ':' degValue=INT ';')? 
        ('Step' ':' step=INT ';')?
        ('Matrix'  matrix=Matrix)?

        (vertList += Vertices+)
;
GroupContent:
     {GroupContent} (parts +=Part+ )
;   
Vertices:
    'Vertices' name = ID "{" 
          verticesContent=VerticesContent 
         "}" 
; 
VerticesContent:
    ('Type' ':' vertType = VertType ';' ) 
          (vertices+=Vertice+ )
      ('Order' ':' '[' orderContent +=(VERTICE_ORDER_CONTENT)+ ']' ';' )
; 


Vertice:
    ('x' | 'X') '=' x=FLOAT ',' ('y' | 'Y') '=' y=FLOAT ',' ('z' | 'Z') '=' z=FLOAT (',' ('w' | 'W') '=' w=FLOAT )?';' 

;

terminal VERTICE_ORDER_CONTENT:
    (WS* INT WS*)/*|(INT WS* ';' WS* INT)(';')?(WS* INT WS*)?*/
;
terminal FLOAT: 
 '-'?(( INT? '.' INT (('E'|'e') '-'? INT)?))|INT;

enum VertType:
    Geometric='Geo'|Normal='Norm'|Textuture='Text'
;
enum PartType:
    Face='Face' |Curve ='Curve' | Line='Line' | Surface='Surface'|Point='Point'
;


enum A_CsType:
    Bezier='Bezier' |Bmatrix ='Bmatrix' | Bspline='Bspline' | Cardinal='Cardinal' | Taylor = 'Taylor'
;

Matrix:
    name=ID '{'
    ('Type' ':' type = MatrixType ';')
    (lines +=MatrixLine+)
    '}';

MatrixLine:
    {MatrixLine} line+=(FLOAT)* ((INT)*)';'
;

enum MatrixType:
    uType = 'U'|vType='V'|u_lType ='u'|v_lType='v'
;

here is my validator in xtend

class MObjValidator extends AbstractMObjValidator {

    public static val InvalidVertListSize = 'ce.xtend.projects.mobj.InvalidVertListSize';
    public static val InvalidVerticesContent = 'ce.xtend.projects.mobj.InvalidVerticesContent';
    public static val InvalidVerticeTypeNumber = 'ce.xtend.projects.mobj.InvalidVerticeTypeNumber';
    public static val InvalidVerticeOrder = 'ce.xtend.projects.mobj.InvalidOrder';


    @Check
    def checkVerticesType(Part part) {

        //var part = subElt as Part;
        if (part !== null) {
            //var content = part.content;
            //var vertList = part.content.vertList;

            var normDefined = false;
            var geoDefined = false;
            var textDefined = false;

            for (vert : part.content.vertList) {

                if (vert.verticesContent.vertType === VertType.GEOMETRIC) {

                    if (geoDefined) {
                        error("A vertice list of type geometric already defined",vert,
                                MObjPackage.Literals.VERTICES_CONTENT__VERT_TYPE, InvalidVerticeTypeNumber,vert.toString());

                    } else {
                        geoDefined = true;
                    }
                } else {
                    if (vert.verticesContent.vertType === VertType.NORMAL) {
                        if (normDefined) {
                            error("A vertice list of type normal already defined",
                                MObjPackage.Literals.VERTICES_CONTENT__VERT_TYPE, InvalidVerticeTypeNumber,vert.toString());
                                //System.out.println("Error normal")
                        } else {
                            normDefined = true;
                        }
                    } else {
                        if (vert.verticesContent.vertType === VertType.TEXTUTURE) {
                            if (textDefined) {
                            error("A vertice list of type texture already defined",vert,
                                        MObjPackage.Literals.VERTICES_CONTENT__VERT_TYPE, InvalidVerticeTypeNumber,vert.toString());
                                System.out.println("Error texture")
                            } else {
                                textDefined = true;
                            }
                        }
                    }

                }
            }
        }
    }


}

Here is my file Test File


Start
Part t 
{
    Type: Surface;
    Vertices tt 
    {
        Type:Text;

        x=1.0,y=2.0, Z=3.0;
        x=1.0 ,y=2.0, Z=3.0;
        x=5.0,y=2.0, Z= -3.0;
         Order : [1 5];

    }
        Vertices tt 
    {
        Type:Text;

        x=1.0,y=2.0, Z=3.0;
        x=1.0 ,y=2.0, Z=3.0;
        x=5.0,y=2.0, Z= -3.0;
         Order : [1 5];

    }

}
End


here the error message

java.lang.IllegalArgumentException: The sources EClass 'Vertices' does not expose the feature 'VerticesContent.vertType'
    at org.eclipse.xtext.validation.FeatureBasedDiagnostic.<init>(FeatureBasedDiagnostic.java:33)
    at org.eclipse.xtext.validation.AbstractDeclarativeValidator.createDiagnostic(AbstractDeclarativeValidator.java:623)
    at org.eclipse.xtext.validation.AbstractDeclarativeValidator.acceptError(AbstractDeclarativeValidator.java:563)
    at org.eclipse.xtext.validation.AbstractDeclarativeValidator.error(AbstractDeclarativeValidator.java:441)
    at ce.xtend.projects.mobj.validation.MObjValidator.checkVerticesType(MObjValidator.java:79)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.eclipse.xtext.validation.AbstractDeclarativeValidator$MethodWrapper.invoke(AbstractDeclarativeValidator.java:127)
    at org.eclipse.xtext.validation.AbstractDeclarativeValidator.internalValidate(AbstractDeclarativeValidator.java:318)
    at org.eclipse.xtext.validation.AbstractInjectableValidator.validate(AbstractInjectableValidator.java:71)
    at org.eclipse.xtext.validation.CompositeEValidator.validate(CompositeEValidator.java:150)
    at org.eclipse.emf.ecore.util.Diagnostician.doValidate(Diagnostician.java:257)
    at org.eclipse.emf.ecore.util.Diagnostician.validate(Diagnostician.java:244)
    at org.eclipse.xtext.validation.CancelableDiagnostician.validate(CancelableDiagnostician.java:40)
    at org.eclipse.emf.ecore.util.Diagnostician.validate(Diagnostician.java:201)
    at org.eclipse.emf.ecore.util.Diagnostician.validate(Diagnostician.java:143)
    at org.eclipse.xtext.validation.ResourceValidatorImpl.validate(ResourceValidatorImpl.java:146)
    at org.eclipse.xtext.validation.ResourceValidatorImpl.validate(ResourceValidatorImpl.java:124)
    at org.eclipse.xtext.validation.ResourceValidatorImpl.validate(ResourceValidatorImpl.java:90)
    at org.eclipse.xtext.ui.editor.validation.ValidationJob$1.exec(ValidationJob.java:91)
    at org.eclipse.xtext.ui.editor.validation.ValidationJob$1.exec(ValidationJob.java:1)
    at org.eclipse.xtext.util.concurrent.CancelableUnitOfWork.exec(CancelableUnitOfWork.java:26)
    at org.eclipse.xtext.resource.OutdatedStateManager.exec(OutdatedStateManager.java:91)
    at org.eclipse.xtext.ui.editor.model.XtextDocument$XtextDocumentLocker.internalReadOnly(XtextDocument.java:527)
    at org.eclipse.xtext.ui.editor.model.XtextDocument$XtextDocumentLocker.readOnly(XtextDocument.java:499)
    at org.eclipse.xtext.ui.editor.model.XtextDocument.readOnly(XtextDocument.java:138)
    at org.eclipse.xtext.ui.editor.validation.ValidationJob.createIssues(ValidationJob.java:86)
    at org.eclipse.xtext.ui.editor.validation.ValidationJob.run(ValidationJob.java:67)
    at org.eclipse.core.internal.jobs.Worker.run(Worker.java:63)

i was expecting that it prints my error message : "A vertice list of type texture already defined"


Solution

  • you call the error method with the wrong parameters

    it should be

    error("Message", object, feature, issueCode, issueData)
    

    in your case object and feature dont match. e.g. here

    error("A vertice list of type texture already defined",vert,MObjPackage.Literals.VERTICES_CONTENT__VERT_TYPE, InvalidVerticeTypeNumber,vert.toString());
    

    which should actually be

    error("A vertice list of type texture already defined",vert,MObjPackage.Literals.VERTICES__NAME, InvalidVerticeTypeNumber,vert.toString());
    

    or

    error("A vertice list of type texture already defined",vert.verticesContent,MObjPackage.Literals.VERTICES_CONTENT__VERT_TYPE, InvalidVerticeTypeNumber,vert.toString());
    

    depending on what you actually want to underline