Search in sources :

Example 16 with SourceUnit

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;
}
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 17 with SourceUnit

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

Example 18 with SourceUnit

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

Example 19 with SourceUnit

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);
}
Also used : CompilationUnit(org.codehaus.groovy.control.CompilationUnit) ClassNode(org.codehaus.groovy.ast.ClassNode) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) SourceUnit(org.codehaus.groovy.control.SourceUnit) GeneratorContext(org.codehaus.groovy.classgen.GeneratorContext)

Example 20 with SourceUnit

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

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