groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Rahul Somasunderam <rsomasunde...@transcendinsights.com>
Subject GroovyLexer and newlines in comments
Date Sun, 22 May 2016 18:52:48 GMT
Hi!

I've got this code 

```
@Grab('org.fusesource.jansi:jansi:1.12')

import static org.fusesource.jansi.Ansi.Color.RED
import static org.fusesource.jansi.Ansi.ansi

import groovyjarjarantlr.Token
import org.codehaus.groovy.antlr.parser.GroovyLexer
import org.fusesource.jansi.AnsiConsole

AnsiConsole.systemInstall()

/**
 * Some class
 */
class Foo {
    /**
     *
     * @param a
     * @param b
     * @param c
     * @return
     */
    String doSomething(String a, int b, double c) {
        ''
    }
}
def lexer = new GroovyLexer(new File('gpa.groovy').newInputStream())
lexer.setWhitespaceIncluded(true)
def lastLine = 0

while (true) {
    def t = lexer.nextToken()
    if (!t || t.type == Token.EOF_TYPE) {
        break
    }
    if (lastLine != t.line) {
        println ''
        def lineNum = t.line.toString().padLeft(4)
        print ansi().fg(RED).a(lineNum).a('| ').reset()
        lastLine = t.line
    }
    print t.text
}
println ''```

When I run it, it parses itself, and prints itself out with line numbers.
There are 2 weird things I see.

1. The comments and javadoc get squished into one line. Is there a way to
get them unmodified?
2. The last statement doesn't retain the last token which is a constant of
an empty string when the file ends on the same line. This problem goes away
if i add a newline at the end of the file. Is there some way to convince the
lexer to not do that?

Thanks!

R,
rahul



--
View this message in context: http://groovy.329449.n5.nabble.com/GroovyLexer-and-newlines-in-comments-tp5732960.html
Sent from the Groovy Users mailing list archive at Nabble.com.

Mime
View raw message