Search in sources :

Example 6 with GroovyLexer

use of org.codehaus.groovy.antlr.parser.GroovyLexer in project groovy-core by groovy.

the class Main method parseFile.

// Here's where we do the real work...
public static void parseFile(String f, GroovyLexer l, SourceBuffer sourceBuffer) throws Exception {
    try {
        // Create a parser that reads from the scanner
        GroovyRecognizer parser = GroovyRecognizer.make(l);
        parser.setSourceBuffer(sourceBuffer);
        parser.setFilename(f);
        if (whitespaceIncluded) {
            GroovyLexer lexer = parser.getLexer();
            lexer.setWhitespaceIncluded(true);
            while (true) {
                Token t = lexer.nextToken();
                System.out.println(t);
                if (t == null || t.getType() == Token.EOF_TYPE)
                    break;
            }
            return;
        }
        // start parsing at the compilationUnit rule
        parser.compilationUnit();
        System.out.println("parseFile " + f + " => " + parser.getAST());
        // do something with the tree
        doTreeAction(f, parser.getAST(), parser.getTokenNames());
    } catch (Exception e) {
        System.err.println("parser exception: " + e);
        // so we can get stack trace        
        e.printStackTrace();
    }
}
Also used : GroovyLexer(org.codehaus.groovy.antlr.parser.GroovyLexer) Token(antlr.Token) GroovyRecognizer(org.codehaus.groovy.antlr.parser.GroovyRecognizer)

Example 7 with GroovyLexer

use of org.codehaus.groovy.antlr.parser.GroovyLexer in project groovy-core by groovy.

the class AntlrParserPlugin method transformCSTIntoAST.

protected void transformCSTIntoAST(SourceUnit sourceUnit, Reader reader, SourceBuffer sourceBuffer) throws CompilationFailedException {
    ast = null;
    setController(sourceUnit);
    // TODO find a way to inject any GroovyLexer/GroovyRecognizer
    UnicodeEscapingReader unicodeReader = new UnicodeEscapingReader(reader, sourceBuffer);
    UnicodeLexerSharedInputState inputState = new UnicodeLexerSharedInputState(unicodeReader);
    GroovyLexer lexer = new GroovyLexer(inputState);
    unicodeReader.setLexer(lexer);
    GroovyRecognizer parser = GroovyRecognizer.make(lexer);
    parser.setSourceBuffer(sourceBuffer);
    tokenNames = parser.getTokenNames();
    parser.setFilename(sourceUnit.getName());
    // start parsing at the compilationUnit rule
    try {
        parser.compilationUnit();
    } catch (TokenStreamRecognitionException tsre) {
        RecognitionException e = tsre.recog;
        SyntaxException se = new SyntaxException(e.getMessage(), e, e.getLine(), e.getColumn());
        se.setFatal(true);
        sourceUnit.addError(se);
    } catch (RecognitionException e) {
        SyntaxException se = new SyntaxException(e.getMessage(), e, e.getLine(), e.getColumn());
        se.setFatal(true);
        sourceUnit.addError(se);
    } catch (TokenStreamException e) {
        sourceUnit.addException(e);
    }
    ast = parser.getAST();
}
Also used : TokenStreamException(antlr.TokenStreamException) TokenStreamRecognitionException(antlr.TokenStreamRecognitionException) GroovyLexer(org.codehaus.groovy.antlr.parser.GroovyLexer) GroovyRecognizer(org.codehaus.groovy.antlr.parser.GroovyRecognizer) TokenStreamRecognitionException(antlr.TokenStreamRecognitionException) RecognitionException(antlr.RecognitionException)

Example 8 with GroovyLexer

use of org.codehaus.groovy.antlr.parser.GroovyLexer in project groovy-core by groovy.

the class SourceParserTest method parse.

protected void parse(String name, Reader reader) {
    SourceBuffer sourceBuffer = new SourceBuffer();
    UnicodeEscapingReader unicodeReader = new UnicodeEscapingReader(reader, sourceBuffer);
    GroovyLexer lexer = new GroovyLexer(unicodeReader);
    unicodeReader.setLexer(lexer);
    GroovyRecognizer parser = GroovyRecognizer.make(lexer);
    parser.setSourceBuffer(sourceBuffer);
    parser.setFilename(name);
    // start parsing at the compilationUnit rule
    try {
        parser.compilationUnit();
    } catch (Exception ex) {
        StringWriter out = new StringWriter();
        out.write(ex.getMessage());
        out.write("\n");
        ex.printStackTrace(new PrintWriter(out));
        fail(out.toString());
    }
}
Also used : StringWriter(java.io.StringWriter) GroovyLexer(org.codehaus.groovy.antlr.parser.GroovyLexer) GroovyRecognizer(org.codehaus.groovy.antlr.parser.GroovyRecognizer) PrintWriter(java.io.PrintWriter)

Example 9 with GroovyLexer

use of org.codehaus.groovy.antlr.parser.GroovyLexer in project groovy-core by groovy.

the class CompositeVisitorTest method assertCompositeTransparency.

private void assertCompositeTransparency(String input) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GroovyRecognizer parser;
    SourceBuffer sourceBuffer = new SourceBuffer();
    UnicodeEscapingReader unicodeReader = new UnicodeEscapingReader(new StringReader(input), sourceBuffer);
    GroovyLexer lexer = new GroovyLexer(unicodeReader);
    unicodeReader.setLexer(lexer);
    parser = GroovyRecognizer.make(lexer);
    parser.setSourceBuffer(sourceBuffer);
    String[] tokenNames = parser.getTokenNames();
    parser.compilationUnit();
    AST ast = parser.getAST();
    // determine direct result
    Visitor directVisitor = new SourcePrinter(new PrintStream(baos), tokenNames, false);
    AntlrASTProcessor traverser = new SourceCodeTraversal(directVisitor);
    traverser.process(ast);
    String directResult = new String(baos.toByteArray());
    // determine composite result
    baos.reset();
    List wrappedVisitors = new ArrayList();
    wrappedVisitors.add(directVisitor);
    Visitor compositeVisitor = new CompositeVisitor(wrappedVisitors);
    traverser = new SourceCodeTraversal(compositeVisitor);
    traverser.process(ast);
    String compositeResult = new String(baos.toByteArray());
    assertEquals(directResult, compositeResult);
}
Also used : PrintStream(java.io.PrintStream) AST(antlr.collections.AST) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SourceBuffer(org.codehaus.groovy.antlr.SourceBuffer) AntlrASTProcessor(org.codehaus.groovy.antlr.AntlrASTProcessor) UnicodeEscapingReader(org.codehaus.groovy.antlr.UnicodeEscapingReader) GroovyLexer(org.codehaus.groovy.antlr.parser.GroovyLexer) StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) List(java.util.List) GroovyRecognizer(org.codehaus.groovy.antlr.parser.GroovyRecognizer)

Example 10 with GroovyLexer

use of org.codehaus.groovy.antlr.parser.GroovyLexer in project groovy-core by groovy.

the class TraversalTestHelper method traverse.

// todo - the visitor doesn't always take PrintStreams as constructor params!  Could be a more reusable implementation than this...
public String traverse(String input, Class visitorClass, Boolean extraParam) throws Exception {
    if (!Visitor.class.isAssignableFrom(visitorClass)) {
        throw new RuntimeException("Invalid class for traversal: " + visitorClass.getName());
    }
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GroovyRecognizer parser;
    SourceBuffer sourceBuffer = new SourceBuffer();
    UnicodeEscapingReader unicodeReader = new UnicodeEscapingReader(new StringReader(input), sourceBuffer);
    GroovyLexer lexer = new GroovyLexer(unicodeReader);
    unicodeReader.setLexer(lexer);
    parser = GroovyRecognizer.make(lexer);
    parser.setSourceBuffer(sourceBuffer);
    String[] tokenNames = parser.getTokenNames();
    parser.compilationUnit();
    AST ast = parser.getAST();
    Class[] paramTypes;
    Object[] params;
    if (extraParam == null) {
        paramTypes = new Class[] { PrintStream.class, String[].class };
        params = new Object[] { new PrintStream(baos), tokenNames };
    } else {
        paramTypes = new Class[] { PrintStream.class, String[].class, Boolean.TYPE };
        params = new Object[] { new PrintStream(baos), tokenNames, extraParam };
    }
    Constructor constructor = visitorClass.getConstructor(paramTypes);
    Visitor visitor = (Visitor) constructor.newInstance(params);
    AntlrASTProcessor traverser = new SourceCodeTraversal(visitor);
    traverser.process(ast);
    return new String(baos.toByteArray());
}
Also used : PrintStream(java.io.PrintStream) AST(antlr.collections.AST) Constructor(java.lang.reflect.Constructor) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SourceBuffer(org.codehaus.groovy.antlr.SourceBuffer) AntlrASTProcessor(org.codehaus.groovy.antlr.AntlrASTProcessor) UnicodeEscapingReader(org.codehaus.groovy.antlr.UnicodeEscapingReader) GroovyLexer(org.codehaus.groovy.antlr.parser.GroovyLexer) StringReader(java.io.StringReader) GroovyRecognizer(org.codehaus.groovy.antlr.parser.GroovyRecognizer)

Aggregations

GroovyLexer (org.codehaus.groovy.antlr.parser.GroovyLexer)18 GroovyRecognizer (org.codehaus.groovy.antlr.parser.GroovyRecognizer)18 StringReader (java.io.StringReader)12 UnicodeEscapingReader (org.codehaus.groovy.antlr.UnicodeEscapingReader)10 SourceBuffer (org.codehaus.groovy.antlr.SourceBuffer)8 AST (antlr.collections.AST)6 AntlrASTProcessor (org.codehaus.groovy.antlr.AntlrASTProcessor)6 RecognitionException (antlr.RecognitionException)4 TokenStreamException (antlr.TokenStreamException)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 PrintStream (java.io.PrintStream)4 Token (antlr.Token)2 TokenStreamRecognitionException (antlr.TokenStreamRecognitionException)2 IncorrectTypeHintException (groovy.transform.stc.IncorrectTypeHintException)2 PrintWriter (java.io.PrintWriter)2 StringWriter (java.io.StringWriter)2 Constructor (java.lang.reflect.Constructor)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2