use of org.codehaus.groovy.control.SourceUnit in project groovy by apache.
the class CompileUnit method addClass.
/**
* Adds a class to the unit.
*/
public void addClass(ClassNode node) {
node = node.redirect();
String name = node.getName();
ClassNode stored = classes.get(name);
if (stored != null && stored != node) {
// we have a duplicate class!
// One possibility for this is, that we declared a script and a
// class in the same file and named the class like the file
SourceUnit nodeSource = node.getModule().getContext();
SourceUnit storedSource = stored.getModule().getContext();
String txt = "Invalid duplicate class definition of class " + node.getName() + " : ";
if (nodeSource == storedSource) {
// same class in same source
txt += "The source " + nodeSource.getName() + " contains at least two definitions of the class " + node.getName() + ".\n";
if (node.isScriptBody() || stored.isScriptBody()) {
txt += "One of the classes is an explicit generated class using the class statement, the other is a class generated from" + " the script body based on the file name. Solutions are to change the file name or to change the class name.\n";
}
} else {
txt += "The sources " + nodeSource.getName() + " and " + storedSource.getName() + " each contain a class with the name " + node.getName() + ".\n";
}
nodeSource.getErrorCollector().addErrorAndContinue(new SyntaxErrorMessage(new SyntaxException(txt, node.getLineNumber(), node.getColumnNumber(), node.getLastLineNumber(), node.getLastColumnNumber()), nodeSource));
}
classes.put(name, node);
if (classesToCompile.containsKey(name)) {
ClassNode cn = classesToCompile.get(name);
cn.setRedirect(node);
classesToCompile.remove(name);
}
}
use of org.codehaus.groovy.control.SourceUnit in project groovy by apache.
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 by apache.
the class TestParserSupport method parse.
public ModuleNode parse(String text, String description) throws Exception {
SourceUnit unit = SourceUnit.create(description, text);
unit.parse();
unit.convert();
return unit.getAST();
}
use of org.codehaus.groovy.control.SourceUnit in project groovy by apache.
the class LocatedMessage method write.
public void write(PrintWriter writer, Janitor janitor) {
if (owner instanceof SourceUnit) {
SourceUnit source = (SourceUnit) owner;
String name = source.getName();
int line = context.getStartLine();
int column = context.getStartColumn();
String sample = source.getSample(line, column, janitor);
if (sample != null) {
writer.println(source.getSample(line, column, janitor));
}
writer.println(name + ": " + line + ": " + this.message);
writer.println("");
} else {
writer.println("<No Relevant Source>: " + this.message);
writer.println("");
}
}
use of org.codehaus.groovy.control.SourceUnit in project spock by spockframework.
the class ExpressionInfoBuilder method build.
public ExpressionInfo build() {
try {
SourceUnit unit = SourceUnit.create("Spec expression", adjustedText);
unit.parse();
unit.completePhase();
unit.convert();
BlockStatement blockStat = unit.getAST().getStatementBlock();
Assert.that(blockStat != null && blockStat.getStatements().size() == 1);
Statement stat = blockStat.getStatements().get(0);
Assert.that(stat instanceof ExpressionStatement);
Expression expr = ((ExpressionStatement) stat).getExpression();
ExpressionInfo exprInfo = new ExpressionInfoConverter(lines).convert(expr);
// IDEA: rest of this method could be moved to ExpressionInfoConverter (but: might make EIC less testable)
// IDEA: could make ExpressionInfo immutable
List<ExpressionInfo> inPostfixOrder = exprInfo.inPostfixOrder(false);
for (int variableNumber = 0; variableNumber < inPostfixOrder.size(); variableNumber++) {
ExpressionInfo info = inPostfixOrder.get(variableNumber);
info.setText(findText(info.getRegion()));
if (notRecordedVarNumberBecauseOfException != null && variableNumber == notRecordedVarNumberBecauseOfException) {
info.setValue(exception);
} else if (values.size() > variableNumber) {
//we have this value
info.setValue(values.get(variableNumber));
} else {
info.setValue(ExpressionInfo.VALUE_NOT_AVAILABLE);
}
if (startPos.getLineIndex() > 0)
info.shiftVertically(startPos.getLineIndex());
}
return exprInfo;
} catch (Throwable t) {
final ExpressionInfo expressionInfo = new ExpressionInfo(TextRegion.create(TextPosition.create(1, 1), TextPosition.create(1, 1)), TextPosition.create(1, 1), null);
expressionInfo.setText(text);
expressionInfo.setValue(lastOrNull(values));
return expressionInfo;
}
}
Aggregations