All Projects → c2nes → Javalang

c2nes / Javalang

Licence: mit
Pure Python Java parser and tools

Programming Languages

python
139335 projects - #7 most used programming language
java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Javalang

Typin
Declarative framework for interactive CLI applications
Stars: ✭ 126 (-69.12%)
Mutual labels:  parser, library
Sdk
Library for using Grafana' structures in Go programs and client for Grafana REST API.
Stars: ✭ 193 (-52.7%)
Mutual labels:  parser, library
Phplrt
PHP Language Recognition Tool
Stars: ✭ 127 (-68.87%)
Mutual labels:  parser, library
Argumentum
C++ command line parsing library
Stars: ✭ 92 (-77.45%)
Mutual labels:  parser, library
Regex
The Hoa\Regex library.
Stars: ✭ 308 (-24.51%)
Mutual labels:  parser, library
Simplepie
A simple Atom/RSS parsing library for PHP.
Stars: ✭ 1,389 (+240.44%)
Mutual labels:  parser, library
Isobmff
C++ Library for ISO/IEC 14496-12 - ISO Base Media File Format (QuickTime, MPEG-4, HEIF, etc)
Stars: ✭ 157 (-61.52%)
Mutual labels:  parser, library
Anglesharp.js
👼 Extends AngleSharp with a .NET-based JavaScript engine.
Stars: ✭ 68 (-83.33%)
Mutual labels:  parser, library
Length.js
📏 JavaScript library for length units conversion.
Stars: ✭ 292 (-28.43%)
Mutual labels:  parser, library
Php Svg
Vector graphics (SVG) library for PHP
Stars: ✭ 256 (-37.25%)
Mutual labels:  parser, library
Rst
PHP library to parse reStructuredText documents
Stars: ✭ 90 (-77.94%)
Mutual labels:  parser, library
Anglesharp
👼 The ultimate angle brackets parser library parsing HTML5, MathML, SVG and CSS to construct a DOM based on the official W3C specifications.
Stars: ✭ 4,018 (+884.8%)
Mutual labels:  parser, library
Internettools
XPath/XQuery 3.1 interpreter for Pascal with compatibility modes for XPath 2.0/XQuery 1.0/3.0, custom and JSONiq extensions, XML/HTML parsers and classes for HTTP/S requests
Stars: ✭ 82 (-79.9%)
Mutual labels:  parser, library
Commonmark Java
Java library for parsing and rendering CommonMark (Markdown)
Stars: ✭ 1,675 (+310.54%)
Mutual labels:  parser, library
Cat
Plain C library for parsing AT commands for use in host devices.
Stars: ✭ 77 (-81.13%)
Mutual labels:  parser, library
Libnmea
Lightweight C library for parsing NMEA 0183 sentences
Stars: ✭ 146 (-64.22%)
Mutual labels:  parser, library
Substitution Schedule Parser
Java library for parsing schools' substitution schedules. Supports multiple different systems mainly used in the German-speaking countries, including Untis, svPlan, and DAVINCI
Stars: ✭ 33 (-91.91%)
Mutual labels:  parser, library
Sharpmath
A small .NET math library.
Stars: ✭ 36 (-91.18%)
Mutual labels:  parser, library
Vmime
VMime Mail Library
Stars: ✭ 218 (-46.57%)
Mutual labels:  parser, library
Shortcode
Advanced shortcode (BBCode) parser and engine for PHP
Stars: ✭ 331 (-18.87%)
Mutual labels:  parser, library

======== javalang

.. image:: https://travis-ci.org/c2nes/javalang.svg?branch=master :target: https://travis-ci.org/c2nes/javalang

.. image:: https://badge.fury.io/py/javalang.svg :target: https://badge.fury.io/py/javalang

javalang is a pure Python library for working with Java source code. javalang provides a lexer and parser targeting Java 8. The implementation is based on the Java language spec available at http://docs.oracle.com/javase/specs/jls/se8/html/.

The following gives a very brief introduction to using javalang.


Getting Started

.. code-block:: python

>>> import javalang
>>> tree = javalang.parse.parse("package javalang.brewtab.com; class Test {}")

This will return a CompilationUnit instance. This object is the root of a tree which may be traversed to extract different information about the compilation unit,

.. code-block:: python

>>> tree.package.name
u'javalang.brewtab.com'
>>> tree.types[0]
ClassDeclaration
>>> tree.types[0].name
u'Test'

The string passed to javalang.parse.parse() must represent a complete unit which simply means it should represent a complete, valid Java source file. Other methods in the javalang.parse module allow for some smaller code snippets to be parsed without providing an entire compilation unit.

Working with the syntax tree ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

CompilationUnit is a subclass of javalang.ast.Node, as are its descendants in the tree. The javalang.tree module defines the different types of Node subclasses, each of which represent the different syntaxual elements you will find in Java code. For more detail on what node types are available, see the javalang/tree.py source file until the documentation is complete.

Node instances support iteration,

.. code-block:: python

>>> for path, node in tree:
...     print path, node
... 
() CompilationUnit
(CompilationUnit,) PackageDeclaration
(CompilationUnit, [ClassDeclaration]) ClassDeclaration

This iteration can also be filtered by type,

.. code-block:: python

>>> for path, node in tree.filter(javalang.tree.ClassDeclaration):
...     print path, node
... 
(CompilationUnit, [ClassDeclaration]) ClassDeclaration

Component Usage

Internally, the javalang.parse.parse method is a simple method which creates a token stream for the input, initializes a new javalang.parser.Parser instance with the given token stream, and then invokes the parser's parse() method, returning the resulting CompilationUnit. These components may be also be used individually.

Tokenizer ^^^^^^^^^

The tokenizer/lexer may be invoked directly be calling javalang.tokenizer.tokenize,

.. code-block:: python

>>> javalang.tokenizer.tokenize('System.out.println("Hello " + "world");')
<generator object tokenize at 0x1ce5190>

This returns a generator which provides a stream of JavaToken objects. Each token carries position (line, column) and value information,

.. code-block:: python

>>> tokens = list(javalang.tokenizer.tokenize('System.out.println("Hello " + "world");'))
>>> tokens[6].value
u'"Hello "'
>>> tokens[6].position
(1, 19)

The tokens are not directly instances of JavaToken, but are instead instances of subclasses which identify their general type,

.. code-block:: python

>>> type(tokens[6])
<class 'javalang.tokenizer.String'>
>>> type(tokens[7])
<class 'javalang.tokenizer.Operator'>

NOTE: The shift operators >> and >>> are represented by multiple > tokens. This is because multiple > may appear in a row when closing nested generic parameter/arguments lists. This abiguity is instead resolved by the parser.

Parser ^^^^^^

To parse snippets of code, a parser may be used directly,

.. code-block:: python

>>> tokens = javalang.tokenizer.tokenize('System.out.println("Hello " + "world");')
>>> parser = javalang.parser.Parser(tokens)
>>> parser.parse_expression()
MethodInvocation

The parse methods are designed for incremental parsing so they will not restart at the beginning of the token stream. Attempting to call a parse method more than once will result in a JavaSyntaxError exception.

Invoking the incorrect parse method will also result in a JavaSyntaxError exception,

.. code-block:: python

>>> tokens = javalang.tokenizer.tokenize('System.out.println("Hello " + "world");')
>>> parser = javalang.parser.Parser(tokens)
>>> parser.parse_type_declaration()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "javalang/parser.py", line 336, in parse_type_declaration
    return self.parse_class_or_interface_declaration()
  File "javalang/parser.py", line 353, in parse_class_or_interface_declaration
    self.illegal("Expected type declaration")
  File "javalang/parser.py", line 122, in illegal
    raise JavaSyntaxError(description, at)
javalang.parser.JavaSyntaxError

The javalang.parse module also provides convenience methods for parsing more common types of code snippets.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].