use of org.codehaus.groovy.control.SourceUnit in project groovy-core by groovy.
the class GenericsUtils method parseClassNodesFromString.
public static ClassNode[] parseClassNodesFromString(final String option, final SourceUnit sourceUnit, final CompilationUnit compilationUnit, final MethodNode mn, final ASTNode usage) {
GroovyLexer lexer = new GroovyLexer(new StringReader("DummyNode<" + option + ">"));
final GroovyRecognizer rn = GroovyRecognizer.make(lexer);
try {
rn.classOrInterfaceType(true);
final AtomicReference<ClassNode> ref = new AtomicReference<ClassNode>();
AntlrParserPlugin plugin = new AntlrParserPlugin() {
@Override
public ModuleNode buildAST(final SourceUnit sourceUnit, final ClassLoader classLoader, final Reduction cst) throws ParserException {
ref.set(makeTypeWithArguments(rn.getAST()));
return null;
}
};
plugin.buildAST(null, null, null);
ClassNode parsedNode = ref.get();
// the returned node is DummyNode<Param1, Param2, Param3, ...)
GenericsType[] parsedNodeGenericsTypes = parsedNode.getGenericsTypes();
if (parsedNodeGenericsTypes == null) {
return null;
}
ClassNode[] signature = new ClassNode[parsedNodeGenericsTypes.length];
for (int i = 0; i < parsedNodeGenericsTypes.length; i++) {
final GenericsType genericsType = parsedNodeGenericsTypes[i];
signature[i] = resolveClassNode(sourceUnit, compilationUnit, mn, usage, genericsType.getType());
}
return signature;
} catch (RecognitionException e) {
sourceUnit.addError(new IncorrectTypeHintException(mn, e, usage.getLineNumber(), usage.getColumnNumber()));
} catch (TokenStreamException e) {
sourceUnit.addError(new IncorrectTypeHintException(mn, e, usage.getLineNumber(), usage.getColumnNumber()));
} catch (ParserException e) {
sourceUnit.addError(new IncorrectTypeHintException(mn, e, usage.getLineNumber(), usage.getColumnNumber()));
}
return null;
}
use of org.codehaus.groovy.control.SourceUnit in project groovy-core by groovy.
the class ClassCodeVisitorSupport method addError.
protected void addError(String msg, ASTNode expr) {
SourceUnit source = getSourceUnit();
source.getErrorCollector().addErrorAndContinue(new SyntaxErrorMessage(new SyntaxException(msg + '\n', expr.getLineNumber(), expr.getColumnNumber(), expr.getLastLineNumber(), expr.getLastColumnNumber()), source));
}
use of org.codehaus.groovy.control.SourceUnit in project groovy-core by groovy.
the class Compiler method compile.
/**
* Compiles a string of code.
*/
public void compile(String name, String code) throws CompilationFailedException {
CompilationUnit unit = new CompilationUnit(configuration);
unit.addSource(new SourceUnit(name, code, configuration, unit.getClassLoader(), unit.getErrorCollector()));
unit.compile();
}
use of org.codehaus.groovy.control.SourceUnit in project groovy by apache.
the class DependencyTest method setUp.
@Override
protected void setUp() throws Exception {
super.setUp();
cu = new CompilationUnit();
cache = new StringSetMap();
cu.addPhaseOperation(new CompilationUnit.PrimaryClassNodeOperation() {
@Override
public void call(final SourceUnit source, GeneratorContext context, ClassNode classNode) throws CompilationFailedException {
DependencyTracker dt = new DependencyTracker(source, cache);
dt.visitClass(classNode);
}
}, Phases.CLASS_GENERATION);
}
use of org.codehaus.groovy.control.SourceUnit in project groovy by apache.
the class SyntaxErrorMessageTest method testSetsTheSourceLocatorOfItsSyntaxExceptionAsTheNameOfTheCorrespondingSourceUnitWhenInstantiated.
public void testSetsTheSourceLocatorOfItsSyntaxExceptionAsTheNameOfTheCorrespondingSourceUnitWhenInstantiated() {
SyntaxException syntaxException = new SyntaxException(someString(), -1, -1);
assertEquals("source locator", null, syntaxException.getSourceLocator());
String sourceUnitName = someString();
SourceUnit sourceUnit = SourceUnit.create(sourceUnitName, someString());
new SyntaxErrorMessage(syntaxException, sourceUnit);
assertEquals("source locator", sourceUnitName, syntaxException.getSourceLocator());
}
Aggregations