Search in sources :

Example 1 with SourceUnit

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);
    }
}
Also used : SyntaxErrorMessage(org.codehaus.groovy.control.messages.SyntaxErrorMessage) SyntaxException(org.codehaus.groovy.syntax.SyntaxException) SourceUnit(org.codehaus.groovy.control.SourceUnit)

Example 2 with SourceUnit

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;
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) ParserException(org.codehaus.groovy.syntax.ParserException) IncorrectTypeHintException(groovy.transform.stc.IncorrectTypeHintException) AtomicReference(java.util.concurrent.atomic.AtomicReference) SourceUnit(org.codehaus.groovy.control.SourceUnit) TokenStreamException(antlr.TokenStreamException) Reduction(org.codehaus.groovy.syntax.Reduction) AntlrParserPlugin(org.codehaus.groovy.antlr.AntlrParserPlugin) GroovyLexer(org.codehaus.groovy.antlr.parser.GroovyLexer) StringReader(java.io.StringReader) GenericsType(org.codehaus.groovy.ast.GenericsType) GroovyRecognizer(org.codehaus.groovy.antlr.parser.GroovyRecognizer) RecognitionException(antlr.RecognitionException)

Example 3 with SourceUnit

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();
}
Also used : SourceUnit(org.codehaus.groovy.control.SourceUnit)

Example 4 with SourceUnit

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("");
    }
}
Also used : SourceUnit(org.codehaus.groovy.control.SourceUnit)

Example 5 with SourceUnit

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;
    }
}
Also used : Expression(org.codehaus.groovy.ast.expr.Expression) Statement(org.codehaus.groovy.ast.stmt.Statement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) SourceUnit(org.codehaus.groovy.control.SourceUnit) ExpressionInfo(org.spockframework.runtime.model.ExpressionInfo)

Aggregations

SourceUnit (org.codehaus.groovy.control.SourceUnit)26 CompilationUnit (org.codehaus.groovy.control.CompilationUnit)10 ClassNode (org.codehaus.groovy.ast.ClassNode)8 SyntaxException (org.codehaus.groovy.syntax.SyntaxException)8 SyntaxErrorMessage (org.codehaus.groovy.control.messages.SyntaxErrorMessage)6 CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)5 GeneratorContext (org.codehaus.groovy.classgen.GeneratorContext)4 RecognitionException (antlr.RecognitionException)2 TokenStreamException (antlr.TokenStreamException)2 GroovyObject (groovy.lang.GroovyObject)2 IncorrectTypeHintException (groovy.transform.stc.IncorrectTypeHintException)2 StringReader (java.io.StringReader)2 ArrayList (java.util.ArrayList)2 LinkedHashSet (java.util.LinkedHashSet)2 List (java.util.List)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 AntlrParserPlugin (org.codehaus.groovy.antlr.AntlrParserPlugin)2 GroovyLexer (org.codehaus.groovy.antlr.parser.GroovyLexer)2 GroovyRecognizer (org.codehaus.groovy.antlr.parser.GroovyRecognizer)2 GenericsType (org.codehaus.groovy.ast.GenericsType)2