我想用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$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)
我期望它会显示我的错误消息:“已经定义了纹理类型的顶点列表”
答案 0 :(得分:0)
您使用错误的参数调用错误方法
应该是
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());
取决于您实际要强调的内容