Git Product home page Git Product logo

kopyt's Introduction

Kopyt

Kopyt is a Kotlin programming language parser in pure Python, inspired by javalang library.

Installation

pip install -U kopyt

Features

  • Supports Kotlin 1.5
  • Zero dependency

Requirements

  • Python 3.7+

Usage

from kopyt import Parser, node

code = """\
package main

import a.b
import x.y.z.*

fun main() {
    println("Hello, world!")
}
"""

parser = Parser(code)
result = parser.parse()

assert result.package is not None
assert result.package.name == "main"

assert len(result.imports) == 2
assert result.imports[0].name == "a.b"
assert result.imports[1].name == "x.y.z"
assert result.imports[1].wildcard

assert len(result.declarations) == 1
assert isinstance(result.declarations[0], node.FunctionDeclaration)
assert result.declarations[0].name == "main"
assert result.declarations[0].position.line == 6

print(result) # this will output a string similar to the original code

All nodes and its members can be found on kopyt/node.py.

Partial Parsing

It is possible to parse partial Kotlin code, for example you want to parse a function declaration:

from kopyt import Parser, node

code = "fun plusOne(x: Int) = x + 1"
parser = Parser(code)
result = parser.parse_function_declaration()

assert result.name == "plusOne"
assert len(result.parameters) == 1
assert result.parameters[0].name == "x"

All parse functions can be found on kopyt/parser.py.

kopyt's People

Contributors

mvisat avatar

Stargazers

İbrahim Süren avatar wybosys avatar  avatar Jared Dillard avatar farrr avatar Joss Moffatt avatar Georgi Boiko avatar circleous avatar Fariskhi Vidyan avatar

Watchers

James Cloos avatar  avatar circleous avatar Fariskhi Vidyan avatar

kopyt's Issues

parse kotlin code error ‘ (() -> Unit) ’

The following code will raise a ParserException,hope it can be fixed.

package com.test


class TestHandler {

    fun test() {
        val func = object :(() -> Unit) {
            override fun invoke() {
            }
        }
    }

}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.