Commit de44bb01 authored by Lubomir Bulej's avatar Lubomir Bulej

DislClassParser: cleaned up.

parent c90b7751
......@@ -11,87 +11,108 @@ import org.objectweb.asm.tree.AnnotationNode;
import org.objectweb.asm.tree.ClassNode;
import ch.usi.dag.disl.annotation.ArgumentProcessor;
import ch.usi.dag.disl.exception.ArgProcessorParserException;
import ch.usi.dag.disl.exception.GuardException;
import ch.usi.dag.disl.exception.MarkerException;
import ch.usi.dag.disl.exception.ParserException;
import ch.usi.dag.disl.exception.ReflectionException;
import ch.usi.dag.disl.exception.ScopeParserException;
import ch.usi.dag.disl.exception.SnippetParserException;
import ch.usi.dag.disl.exception.StaticContextGenException;
import ch.usi.dag.disl.localvar.LocalVars;
import ch.usi.dag.disl.processor.ArgProcessor;
import ch.usi.dag.disl.snippet.Snippet;
public class DislClassParser {
SnippetParser snippetParser = new SnippetParser();
ArgProcessorParser processorParser = new ArgProcessorParser();
/**
* Parser for DiSL classes containing either snippets or method argument
* processors.
*/
public class DislClassParser {
public void parse(InputStream is) throws ParserException,
SnippetParserException, ReflectionException, ScopeParserException,
StaticContextGenException, ArgProcessorParserException,
MarkerException, GuardException {
private final SnippetParser __snippetParser = new SnippetParser ();
// prepare class node
ClassReader cr;
try {
cr = new ClassReader(is);
} catch (IOException e) {
// rethrow exception as DiSL exception
throw new ParserException(e);
}
ClassNode classNode = new ClassNode();
private final ArgProcessorParser __argProcParser = new ArgProcessorParser ();
cr.accept(classNode, ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);
//
// decide according to processor annotation if it is a snippet or
// processor
public void parse (final InputStream classBytes)
throws ParserException, ReflectionException, StaticContextGenException,
MarkerException, GuardException {
// *** snippet ***
if (classNode.invisibleAnnotations == null) {
snippetParser.parse(classNode);
return;
//
// Get an ASM representation of the DiSL class first, then parse it
// as a snippet or an argument processor depending on the annotations
// associated with the class.
//
final ClassNode classNode = __createClassNode (classBytes);
if (__isArgumentProcessor (classNode)) {
__argProcParser.parse (classNode);
} else {
__snippetParser.parse (classNode);
}
}
// *** processor ***
// check for one annotation
if (classNode.invisibleAnnotations.size() > 1) {
throw new ParserException("Class " + classNode.name
+ " may have only one anotation");
private boolean __isArgumentProcessor (final ClassNode classNode) {
//
// An argument processor must have an @ArgumentProcessor annotation
// associated with the class. DiSL instrumentation classes may have
// an @Instrumentation annotation. DiSL classes without annotations
// are by default considered to be instrumentation classes.
//
if (classNode.invisibleAnnotations != null) {
final Type apType = Type.getType (ArgumentProcessor.class);
for (final AnnotationNode annotation : classNode.invisibleAnnotations) {
final Type annotationType = Type.getType (annotation.desc);
if (apType.equals (annotationType)) {
return true;
}
}
}
AnnotationNode annotation =
(AnnotationNode) classNode.invisibleAnnotations.get(0);
// default: not an argument processor
return false;
}
Type annotationType = Type.getType(annotation.desc);
// check for processor annotation
if (! annotationType.equals(Type.getType(ArgumentProcessor.class))) {
throw new ParserException("Class " + classNode.name
+ " may have only ArgumentProcessor anotation");
}
private ClassNode __createClassNode (final InputStream is)
throws ParserException {
//
// Parse input stream into a class node. Include debug information so
// that we can report line numbers in case of problems in DiSL classes.
// Re-throw any exceptions as DiSL exceptions.
//
try {
final ClassReader classReader = new ClassReader (is);
final ClassNode classNode = new ClassNode ();
classReader.accept (classNode, ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);
return classNode;
processorParser.parse(classNode);
} catch (final IOException ioe) {
throw new ParserException (ioe);
}
}
public LocalVars getAllLocalVars() {
LocalVars merged = new LocalVars();
//
// merge local variables from snippets and processors
merged.putAll(snippetParser.getAllLocalVars());
merged.putAll(processorParser.getAllLocalVars());
public LocalVars getAllLocalVars () {
//
// Merge all local variables from snippets and argument processors.
//
final LocalVars result = new LocalVars ();
result.putAll (__snippetParser.getAllLocalVars ());
result.putAll (__argProcParser.getAllLocalVars ());
return merged;
return result;
}
public List<Snippet> getSnippets() {
return snippetParser.getSnippets();
public List <Snippet> getSnippets () {
return __snippetParser.getSnippets ();
}
public Map<Type, ArgProcessor> getProcessors() {
return processorParser.getProcessors();
public Map <Type, ArgProcessor> getProcessors () {
return __argProcParser.getProcessors ();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment