kotlin parser

A kotlin file code parser

License

License

Categories

Categories

Kotlin Languages
GroupId

GroupId

com.github.sarahbuisson
ArtifactId

ArtifactId

kotlin-parser
Last Version

Last Version

1.5
Release Date

Release Date

Type

Type

xml
Description

Description

kotlin parser
A kotlin file code parser
Project URL

Project URL

https://github.com/sarahBuisson/kotlin-parser
Source Code Management

Source Code Management

https://github.com/sarahBuisson/kotlin-parser.git

Download kotlin-parser

Dependencies

compile (1)

Group / Artifact Type Version
org.antlr : antlr4 jar 4.7

test (1)

Group / Artifact Type Version
junit : junit jar 4.12

Project Modules

There are no modules declared in this project.

##Kotlin parser

This is a sample of a kotlin code parser lib !

how to use:

To use it, build in local then add to your pom.xml the dependencies

  <dependency>
            <groupId>com.github.sarahbuisson</groupId>
            <artifactId>kotlin-parser</artifactId>
            <version>1.0-SNAPSHOT</version>
  </dependency>

create a listener ( a class who well react each time a statement of the kotlin file is parsed, IE:)



class ClassCounterListener extends KotlinParserBaseListener {

    int numberOfClassModifier;
    int numberOfDataClass;
    int numberOfClass;


    @Override
    public void enterClassModifier(KotlinParser.ClassModifierContext ctx) {
        numberOfClassModifier++;
        if (ctx.DATA() != null) {
            numberOfDataClass++;
        }
    }

    @Override
    public void enterClassDeclaration(KotlinParser.ClassDeclarationContext ctx) {
        if (ctx.CLASS() != null) {
            numberOfClass++;
        }
    }

    /**
     * Getter for property 'numberOfClassModifier'.
     *
     * @return Value for property 'numberOfClassModifier'.
     */
    public int getNumberOfClassModifier() {
        return numberOfClassModifier;
    }

    /**
     * Getter for property 'numberOfDataClass'.
     *
     * @return Value for property 'numberOfDataClass'.
     */
    public int getNumberOfDataClass() {
        return numberOfDataClass;
    }

    /**
     * Getter for property 'numberOfClass'.
     *
     * @return Value for property 'numberOfClass'.
     */
    public int getNumberOfClass() {
        return numberOfClass;
    }
}

Then add to your code:

    KotlinLexer KotlinLexer = new KotlinLexer(CharStreams.fromStream(getClass().getResourceAsStream("./sampleKotlin.kt")));

    CommonTokenStream commonTokenStream = new CommonTokenStream(KotlinLexer);
    KotlinParser kotlinParser = new KotlinParser(commonTokenStream);

    ParseTree tree = kotlinParser.kotlinFile();
    ParseTreeWalker walker = new ParseTreeWalker();

    ClassCounterListener listener = new ClassCounterListener();
    walker.walk(listener, tree);

Versions

Version
1.5
1.0