added generation of token constants

This commit is contained in:
sefftinge 2008-05-14 11:53:09 +00:00 committed by sefftinge
parent d4a9dcd55e
commit f15680dd81
3 changed files with 53 additions and 15 deletions

View file

@ -0,0 +1,26 @@
package org.eclipse.xtext;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.xtext.core.parser.IParseErrorHandler;
import org.eclipse.xtext.generator.tests.AbstractGeneratorTest;
public class ParseErrorHandlingTest extends AbstractGeneratorTest {
@Override
protected Class<?> getTheClass() {
return XtextGrammarTest.class;
}
public void testLexError() throws Exception {
final List<String> errors = new ArrayList<String>();
parse("import 'holla' % as foo", new IParseErrorHandler() {
public void handleParserError(int offset, int length, String text, String message) {
System.out.println(message + ":'" + text + "' (" + offset + "," + length + ")");
errors.add(text);
}
});
//TODO assertEquals("%",errors.get(0));
assertEquals(1, errors.size());
}
}

View file

@ -59,7 +59,7 @@ public class XtextGrammarTest extends AbstractGeneratorTest {
public void testInstantiateXtextGrammar() throws Exception {
InputStream bootGrammar = getClass().getClassLoader().getResourceAsStream(getClass().getName().replace('.','/')+".xtext");
EObject grammar = (EObject) parse(bootGrammar , new XtextGrammarTestASTFactory());
EObject grammar = (EObject) parse(bootGrammar , new XtextGrammarTestASTFactory(),null);
assertWithXtend("true","parserRules.select(e|e.name=='AbstractToken').first()!=null",grammar);
assertWithXtend("'AbstractElement'","parserRules.select(e|e.name=='AbstractToken').first().type.name",grammar);
// Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put(

View file

@ -16,6 +16,7 @@ import junit.framework.TestCase;
import org.apache.tools.ant.filters.StringInputStream;
import org.eclipse.xtext.core.parser.IElementFactory;
import org.eclipse.xtext.core.parser.IParseErrorHandler;
import org.openarchitectureware.xtend.XtendFacade;
/**
@ -24,27 +25,38 @@ import org.openarchitectureware.xtend.XtendFacade;
*/
public abstract class AbstractGeneratorTest extends TestCase {
public Object parse(String model, IElementFactory factory) throws Exception {
String name = getClass().getPackage().getName() + ".parser." + getClass().getSimpleName();
String parser = name + "Parser";
Class<?> parserClass = getClass().getClassLoader().loadClass(parser);
Object parserInstance = parserClass.newInstance();
return parserClass.getMethod("parse", InputStream.class, IElementFactory.class).invoke(parserInstance,
new StringInputStream(model), factory);
protected Class<?> getTheClass() {
return getClass();
}
public Object parse(InputStream model, IElementFactory factory) throws Exception {
String name = getClass().getPackage().getName() + ".parser." + getClass().getSimpleName();
public Object parse(String model, IElementFactory factory) throws Exception {
return parse(model, factory, null);
}
public Object parse(String model, IElementFactory factory, IParseErrorHandler errorHandler) throws Exception {
return parse(new StringInputStream(model),factory,errorHandler);
}
public Object parse(InputStream model, IElementFactory factory, IParseErrorHandler errorHandler) throws Exception {
String name = getTheClass().getPackage().getName() + ".parser." + getTheClass().getSimpleName();
String parser = name + "Parser";
Class<?> parserClass = getClass().getClassLoader().loadClass(parser);
Class<?> parserClass = getTheClass().getClassLoader().loadClass(parser);
Object parserInstance = parserClass.newInstance();
return parserClass.getMethod("parse", InputStream.class, IElementFactory.class).invoke(parserInstance, model,
factory);
if (errorHandler != null) {
return parserClass.getMethod("parse", InputStream.class, IElementFactory.class, IParseErrorHandler.class)
.invoke(parserInstance, model, factory, errorHandler);
} else {
return parserClass.getMethod("parse", InputStream.class, IElementFactory.class).invoke(parserInstance,
model, factory);
}
}
public List<Invocation> parse(String model) throws Exception {
return parse(model, (IParseErrorHandler) null);
}
public List<Invocation> parse(String model, IParseErrorHandler errorHandler) throws Exception {
final List<Invocation> calls = new ArrayList<Invocation>();
parse(model, new IElementFactory() {
@ -60,7 +72,7 @@ public abstract class AbstractGeneratorTest extends TestCase {
public void set(Object _this, String feature, Object value) {
calls.add(new Invocation("set", feature, value));
}
});
}, errorHandler);
return calls;
}