如何修复执行 EValidator 时发生的错误
How to fix error occuring while executing EValidator
我想用 xtext 创建一个 obj 文件的解析器。我已经写了一部分语法,现在我正在编写验证器,但是在测试我的语法和验证器时发生了错误。谁能帮帮我?
这是我的语法代码:
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'
;
这是我在 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;
}
}
}
}
}
}
}
}
这是我的测试文件
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
这里是错误信息
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.exec(ValidationJob.java:91)
at org.eclipse.xtext.ui.editor.validation.ValidationJob.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)
我期待它打印我的错误消息:"A vertice list of type texture already defined"
你用错误的参数调用错误方法
应该是
error("Message", object, feature, issueCode, issueData)
在您的案例中,对象和特征不匹配。
例如这里
error("A vertice list of type texture already defined",vert,MObjPackage.Literals.VERTICES_CONTENT__VERT_TYPE, InvalidVerticeTypeNumber,vert.toString());
实际上应该是
error("A vertice list of type texture already defined",vert,MObjPackage.Literals.VERTICES__NAME, InvalidVerticeTypeNumber,vert.toString());
或
error("A vertice list of type texture already defined",vert.verticesContent,MObjPackage.Literals.VERTICES_CONTENT__VERT_TYPE, InvalidVerticeTypeNumber,vert.toString());
取决于您实际想要强调的内容
我想用 xtext 创建一个 obj 文件的解析器。我已经写了一部分语法,现在我正在编写验证器,但是在测试我的语法和验证器时发生了错误。谁能帮帮我?
这是我的语法代码:
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'
;
这是我在 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;
}
}
}
}
}
}
}
}
这是我的测试文件
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
这里是错误信息
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.exec(ValidationJob.java:91)
at org.eclipse.xtext.ui.editor.validation.ValidationJob.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)
我期待它打印我的错误消息:"A vertice list of type texture already defined"
你用错误的参数调用错误方法
应该是
error("Message", object, feature, issueCode, issueData)
在您的案例中,对象和特征不匹配。 例如这里
error("A vertice list of type texture already defined",vert,MObjPackage.Literals.VERTICES_CONTENT__VERT_TYPE, InvalidVerticeTypeNumber,vert.toString());
实际上应该是
error("A vertice list of type texture already defined",vert,MObjPackage.Literals.VERTICES__NAME, InvalidVerticeTypeNumber,vert.toString());
或
error("A vertice list of type texture already defined",vert.verticesContent,MObjPackage.Literals.VERTICES_CONTENT__VERT_TYPE, InvalidVerticeTypeNumber,vert.toString());
取决于您实际想要强调的内容