Use tree-sitter-java from crates.io

pull/795/head
Wilfred Hughes 2024-12-20 08:18:17 +07:00
parent 80f0100c29
commit d2ed2f6123
56 changed files with 18 additions and 86120 deletions

@ -11,7 +11,8 @@ with YAML.
Improved language detection when one argument is a named pipe.
Updated to the latest tree-sitter parser for C, C++, C#, Haskell,
JavaScript, Objective-C, OCaml, Python, Ruby, Scala and TypeScript.
Java, JavaScript, Objective-C, OCaml, Python, Ruby, Scala and
TypeScript.
### Syntax Highlighting

11
Cargo.lock generated

@ -253,6 +253,7 @@ dependencies = [
"tree-sitter-c-sharp",
"tree-sitter-cpp",
"tree-sitter-haskell",
"tree-sitter-java",
"tree-sitter-javascript",
"tree-sitter-language",
"tree-sitter-objc",
@ -1052,6 +1053,16 @@ dependencies = [
"tree-sitter-language",
]
[[package]]
name = "tree-sitter-java"
version = "0.23.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6f9b24494d5f386acb2c086e0541c1b145692e0e012101ddb8257737b83c2803"
dependencies = [
"cc",
"tree-sitter-language",
]
[[package]]
name = "tree-sitter-javascript"
version = "0.23.1"

@ -89,6 +89,7 @@ tree-sitter-cpp = "0.23.4"
tree-sitter-c = "0.23.4"
tree-sitter-javascript = "0.23.1"
tree-sitter-typescript = "0.23.2"
tree-sitter-java = "0.23.4"
[dev-dependencies]
# assert_cmd 2.0.10 requires predicates 3.

@ -174,11 +174,6 @@ fn main() {
src_dir: "vendored_parsers/tree-sitter-janet-simple-src",
extra_files: vec!["scanner.c"],
},
TreeSitterParser {
name: "tree-sitter-java",
src_dir: "vendored_parsers/tree-sitter-java-src",
extra_files: vec![],
},
TreeSitterParser {
name: "tree-sitter-json",
src_dir: "vendored_parsers/tree-sitter-json-src",

@ -83,7 +83,6 @@ extern "C" {
fn tree_sitter_hcl() -> ts::Language;
fn tree_sitter_html() -> ts::Language;
fn tree_sitter_janet_simple() -> ts::Language;
fn tree_sitter_java() -> ts::Language;
fn tree_sitter_json() -> ts::Language;
fn tree_sitter_julia() -> ts::Language;
fn tree_sitter_kotlin() -> ts::Language;
@ -573,7 +572,8 @@ pub(crate) fn from_language(language: guess::Language) -> TreeSitterConfig {
}
}
Java => {
let language = unsafe { tree_sitter_java() };
let language_fn = tree_sitter_java::LANGUAGE;
let language = tree_sitter::Language::new(language_fn);
TreeSitterConfig {
language: language.clone(),
atom_nodes: vec![
@ -594,11 +594,8 @@ pub(crate) fn from_language(language: guess::Language) -> TreeSitterConfig {
.into_iter()
.collect(),
delimiter_tokens: vec![("(", ")"), ("{", "}"), ("[", "]")],
highlight_query: ts::Query::new(
&language,
include_str!("../../vendored_parsers/highlights/java.scm"),
)
.unwrap(),
highlight_query: ts::Query::new(&language, tree_sitter_java::HIGHLIGHTS_QUERY)
.unwrap(),
sub_languages: vec![],
}
}

@ -1 +0,0 @@
../tree-sitter-java/queries/highlights.scm

@ -1 +0,0 @@
tree-sitter-java/src

@ -1,2 +0,0 @@
/src/** linguist-vendored
/examples/* linguist-vendored

@ -1,9 +0,0 @@
Checklist:
- [ ] All tests pass in CI.
- [ ] There are sufficient tests for the new fix/feature.
- [ ] Grammar rules have not been renamed unless absolutely necessary.
- [ ] The conflicts section hasn't grown too much.
- [ ] The parser size hasn't grown too much (check the value of STATE_COUNT in src/parser.c).

@ -1,33 +0,0 @@
name: CI
on:
push:
branches:
- master
pull_request:
branches:
- "**"
jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: true
matrix:
os: [macos-latest, ubuntu-latest]
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 18
- run: npm install
- run: npm test
test_windows:
runs-on: windows-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 18
- run: npm install
- run: npm run-script test-windows

@ -1,33 +0,0 @@
name: Publish on crates.io
on:
push:
tags:
- v*
env:
CARGO_TERM_COLOR: always
CARGO_INCREMENTAL: 0
jobs:
publish:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Install Rust stable
run: |
rustup toolchain install stable --profile minimal --no-self-update
- name: Verify publish crate
uses: katyo/publish-crates@v1
with:
dry-run: true
- name: Publish crate
uses: katyo/publish-crates@v1
with:
registry-token: ${{ secrets.CARGO_REGISTRY_TOKEN }}

@ -1,18 +0,0 @@
Cargo.lock
node_modules
build
*.log
package-lock.json
script/javaparser-test/target
test.java
/examples/elasticsearch
/examples/guava
/examples/RxJava
/target/
*.a
*.dylib
*.so
*.o
bindings/c/*.h
bindings/c/tree-sitter-*.pc
.build/

@ -1,6 +0,0 @@
corpus
build
script
examples
target
Cargo.lock

@ -1,18 +0,0 @@
cmake_minimum_required(VERSION 3.8)
project(tree-sitter-java)
# Enable C++11
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
# Set source files
set(SOURCE_FILES
src/parser.c
)
# Build tree-sitter-java as a shared library
add_library(tree-sitter-java SHARED ${SOURCE_FILES})
# Set the include directories
target_include_directories(tree-sitter-java PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/src")

@ -1,68 +0,0 @@
# Introduction
These guidelines are intended to help developers contribute to the grammar and preserve consistency across the project while doing so.
### Contributions needed
The main objective of this project is to support all versions of the Java language. This means addressing issues with the "todo" and "known error" labels, assigning yourself to a problem that you're working on so that work isn't duplicated. We're also open to documentation improvements and feature enhancements and recommend filing an issue.
# Java Grammar Development Guide
#### General grammar structure
Language constructs are grouped into the top-level categories denoting declarations, statements within methods and expressions. All granular constructs feed into those through the defined grammar hierarchy.
```
rules: {
program: $ => repeat($._statement),
_statement: $ => prec(1, choice(
$._expression_statement,
$._declaration,
$._method_statement
)),
```
#### Deviating from the language spec
The `grammar.js` file follows the BNF grammar outlined in the [Java Language Specification](https://docs.oracle.com/javase/specs/jls/se9/html/jls-19.html).
There are situations where we've deviated from the spec:
- **Prefered naming:** if common developer parlance prefers a naming convention other than the spec, we tend to deviate. An example of this is for `generic_type` as the outer wrapper for `type_arguments`, since generics are a familiar Java programming concept.
- **Simplicity:** The spec is convoluted and not conducive to compact, readable code. In this situation, we've preferred structuring things in a way that are more reusable throughout the grammar and also read clearly. An example of this is our preference to use `binary_` and `unary_` expressions to model relationships between operators, as opposed to supporting the spec's [`ConditionalExpression`](https://docs.oracle.com/javase/specs/jls/se9/html/jls-15.html#jls-ConditionalExpression) hierarchy.
#### When it's okay to parse invalid Java
There are situations in which we parse invalid code to support end-user experiences. For example, it's important to ensure syntax-highlighting doesn't break down for a snippet of Java code in a markdown file. For this reason, we currently allow expressions to be parsed outside of methods, even though that is not valid Java.
To know what is "valid enough", consider what good documentation would look like:
- ✅ `int x = (1 + 2);` = This is invalid since it is not within a method, but still comprehensible. Parse this.
- ❌ `int x = (1 + ) =;` This is not only invalid Java, but it is invalid logic. It wouldn't make sense in documentation. Don't parse this.
Running your code using something like [JavaRepl](https://github.com/albertlatacz/java-repl) is also a good way to verify the correctness of the input program.
#### Adding unit tests
The recommendation is to be comprehensive in adding tests. If it's a visible node, add it to a `/corpus/` test file. It's typically a good idea to test as many permutations of a particular language construct as possible. This increases test coverage, but doubly acquaints readers with a way to examine expected outputs and understand the "edges" of a language.
#### Testing on external repos
Three of the "most popular" Java repositories have been cloned into the project under the `/examples` directory (where popularity is defined by repositories that are most starred and have highest number of active contributers within the last month). Parsing these repos allows us to gauge how well our grammar performs at parsing "real world" Java.
_To test:_
- `./script/parse-examples` runs the tests and outputs them to `known-errors.txt`, representing the files that have any errors or `MISSING ;` flags.
- The goal is to drive down the errors in `known-errors.txt` to 0.
- `known-errors.txt` allows you to find erroring files and parse them individually to diagnose and debug errors.
#### Testing with other parsers
It's worth consulting other LR Java parsers (such as [JavaParser](https://github.com/javaparser/javaparser)) to guide your own grammar development. Comparing tree structure and naming can provide valuable insight into what is usable.
#### References
- [JavaParser](https://github.com/javaparser/javaparser)
- [Java Language Specification](https://docs.oracle.com/javase/specs/jls/se9/html/jls-19.html)
- [Tree-sitter documentation](http://tree-sitter.github.io/tree-sitter/creating-parsers.html)
- [JavaRepl](https://github.com/albertlatacz/java-repl)

@ -1,31 +0,0 @@
[package]
name = "tree-sitter-java"
description = "Java grammar for the tree-sitter parsing library"
version = "0.20.0"
authors = [
"Douglas Creager <dcreager@dcreager.net>",
"Ayman Nadeem <aymannadeem@github.com>",
]
license = "MIT"
readme = "bindings/rust/README.md"
keywords = ["incremental", "parsing", "java"]
categories = ["parsing", "text-editors"]
repository = "https://github.com/tree-sitter/tree-sitter-java"
edition = "2018"
build = "bindings/rust/build.rs"
include = [
"bindings/rust/*",
"grammar.js",
"queries/*",
"src/*",
]
[lib]
path = "bindings/rust/lib.rs"
[dependencies]
tree-sitter = ">= 0.19, < 0.21"
[build-dependencies]
cc = "1.0"

@ -1,21 +0,0 @@
MIT License
Copyright (c) 2017 Ayman Nadeem
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

@ -1,114 +0,0 @@
VERSION := 0.19.1
# Repository
SRC_DIR := src
PARSER_REPO_URL := $(shell git -C $(SRC_DIR) remote get-url origin )
ifeq (, $(PARSER_NAME))
PARSER_NAME := $(shell basename $(PARSER_REPO_URL))
PARSER_NAME := $(subst tree-sitter-,,$(PARSER_NAME))
PARSER_NAME := $(subst .git,,$(PARSER_NAME))
endif
ifeq (, $(PARSER_URL))
PARSER_URL := $(subst :,/,$(PARSER_REPO_URL))
PARSER_URL := $(subst git@,https://,$(PARSER_URL))
PARSER_URL := $(subst .git,,$(PARSER_URL))
endif
UPPER_PARSER_NAME := $(shell echo $(PARSER_NAME) | tr a-z A-Z )
# install directory layout
PREFIX ?= /usr/local
INCLUDEDIR ?= $(PREFIX)/include
LIBDIR ?= $(PREFIX)/lib
PCLIBDIR ?= $(LIBDIR)/pkgconfig
# collect C++ sources, and link if necessary
CPPSRC := $(wildcard $(SRC_DIR)/*.cc)
ifeq (, $(CPPSRC))
ADDITIONALLIBS :=
else
ADDITIONALLIBS := -lc++
endif
# collect sources
SRC := $(wildcard $(SRC_DIR)/*.c)
SRC += $(CPPSRC)
OBJ := $(addsuffix .o,$(basename $(SRC)))
# ABI versioning
SONAME_MAJOR := 0
SONAME_MINOR := 0
CFLAGS ?= -O3 -Wall -Wextra -I$(SRC_DIR)
CXXFLAGS ?= -O3 -Wall -Wextra -I$(SRC_DIR)
override CFLAGS += -std=gnu99 -fPIC
override CXXFLAGS += -fPIC
# OS-specific bits
ifeq ($(shell uname),Darwin)
SOEXT = dylib
SOEXTVER_MAJOR = $(SONAME_MAJOR).dylib
SOEXTVER = $(SONAME_MAJOR).$(SONAME_MINOR).dylib
LINKSHARED := $(LINKSHARED)-dynamiclib -Wl,
ifneq ($(ADDITIONALLIBS),)
LINKSHARED := $(LINKSHARED)$(ADDITIONALLIBS),
endif
LINKSHARED := $(LINKSHARED)-install_name,$(LIBDIR)/libtree-sitter-$(PARSER_NAME).$(SONAME_MAJOR).dylib,-rpath,@executable_path/../Frameworks
else
SOEXT = so
SOEXTVER_MAJOR = so.$(SONAME_MAJOR)
SOEXTVER = so.$(SONAME_MAJOR).$(SONAME_MINOR)
LINKSHARED := $(LINKSHARED)-shared -Wl,
ifneq ($(ADDITIONALLIBS),)
LINKSHARED := $(LINKSHARED)$(ADDITIONALLIBS)
endif
LINKSHARED := $(LINKSHARED)-soname,libtree-sitter-$(PARSER_NAME).so.$(SONAME_MAJOR)
endif
ifneq (,$(filter $(shell uname),FreeBSD NetBSD DragonFly))
PCLIBDIR := $(PREFIX)/libdata/pkgconfig
endif
all: libtree-sitter-$(PARSER_NAME).a libtree-sitter-$(PARSER_NAME).$(SOEXTVER) bindings/c/$(PARSER_NAME).h bindings/c/tree-sitter-$(PARSER_NAME).pc
libtree-sitter-$(PARSER_NAME).a: $(OBJ)
$(AR) rcs $@ $^
libtree-sitter-$(PARSER_NAME).$(SOEXTVER): $(OBJ)
$(CC) $(LDFLAGS) $(LINKSHARED) $^ $(LDLIBS) -o $@
ln -sf $@ libtree-sitter-$(PARSER_NAME).$(SOEXT)
ln -sf $@ libtree-sitter-$(PARSER_NAME).$(SOEXTVER_MAJOR)
bindings/c/$(PARSER_NAME).h:
sed -e 's|@UPPER_PARSERNAME@|$(UPPER_PARSER_NAME)|' \
-e 's|@PARSERNAME@|$(PARSER_NAME)|' \
bindings/c/tree-sitter.h.in > $@
bindings/c/tree-sitter-$(PARSER_NAME).pc:
sed -e 's|@LIBDIR@|$(LIBDIR)|;s|@INCLUDEDIR@|$(INCLUDEDIR)|;s|@VERSION@|$(VERSION)|' \
-e 's|=$(PREFIX)|=$${prefix}|' \
-e 's|@PREFIX@|$(PREFIX)|' \
-e 's|@ADDITIONALLIBS@|$(ADDITIONALLIBS)|' \
-e 's|@PARSERNAME@|$(PARSER_NAME)|' \
-e 's|@PARSERURL@|$(PARSER_URL)|' \
bindings/c/tree-sitter.pc.in > $@
install: all
install -d '$(DESTDIR)$(LIBDIR)'
install -m755 libtree-sitter-$(PARSER_NAME).a '$(DESTDIR)$(LIBDIR)'/libtree-sitter-$(PARSER_NAME).a
install -m755 libtree-sitter-$(PARSER_NAME).$(SOEXTVER) '$(DESTDIR)$(LIBDIR)'/libtree-sitter-$(PARSER_NAME).$(SOEXTVER)
ln -sf libtree-sitter-$(PARSER_NAME).$(SOEXTVER) '$(DESTDIR)$(LIBDIR)'/libtree-sitter-$(PARSER_NAME).$(SOEXTVER_MAJOR)
ln -sf libtree-sitter-$(PARSER_NAME).$(SOEXTVER) '$(DESTDIR)$(LIBDIR)'/libtree-sitter-$(PARSER_NAME).$(SOEXT)
install -d '$(DESTDIR)$(INCLUDEDIR)'/tree_sitter
install -m644 bindings/c/$(PARSER_NAME).h '$(DESTDIR)$(INCLUDEDIR)'/tree_sitter/
install -d '$(DESTDIR)$(PCLIBDIR)'
install -m644 bindings/c/tree-sitter-$(PARSER_NAME).pc '$(DESTDIR)$(PCLIBDIR)'/
clean:
rm -f $(OBJ) libtree-sitter-$(PARSER_NAME).a libtree-sitter-$(PARSER_NAME).$(SOEXT) libtree-sitter-$(PARSER_NAME).$(SOEXTVER_MAJOR) libtree-sitter-$(PARSER_NAME).$(SOEXTVER)
rm -f bindings/c/$(PARSER_NAME).h bindings/c/tree-sitter-$(PARSER_NAME).pc
.PHONY: all install clean

@ -1,36 +0,0 @@
// swift-tools-version:5.3
import PackageDescription
let package = Package(
name: "TreeSitterJava",
products: [
.library(name: "TreeSitterJava", targets: ["TreeSitterJava"]),
],
dependencies: [],
targets: [
.target(name: "TreeSitterJava",
path: ".",
exclude: [
"binding.gyp",
"bindings",
"Cargo.toml",
"corpus",
"grammar.js",
"LICENSE",
"Makefile",
"package.json",
"README.md",
"script",
"src/grammar.json",
"src/node-types.json",
],
sources: [
"src/parser.c",
],
resources: [
.copy("queries")
],
publicHeadersPath: "bindings/swift",
cSettings: [.headerSearchPath("src")])
]
)

@ -1,7 +0,0 @@
tree-sitter-java
================
[![CI](https://github.com/tree-sitter/tree-sitter-java/actions/workflows/ci.yml/badge.svg)](https://github.com/tree-sitter/tree-sitter-java/actions/workflows/ci.yml)
[![Discord](https://img.shields.io/discord/1063097320771698699?logo=discord)](https://discord.gg/w7nTvsVJhm)
Java grammar for [tree-sitter](https://github.com/tree-sitter/tree-sitter).

@ -1,18 +0,0 @@
{
"targets": [
{
"target_name": "tree_sitter_java_binding",
"include_dirs": [
"<!(node -e \"require('nan')\")",
"src"
],
"sources": [
"src/parser.c",
"bindings/node/binding.cc"
],
"cflags_c": [
"-std=c99",
]
}
]
}

@ -1,16 +0,0 @@
#ifndef TREE_SITTER_@UPPER_PARSERNAME@_H_
#define TREE_SITTER_@UPPER_PARSERNAME@_H_
#include <tree_sitter/parser.h>
#ifdef __cplusplus
extern "C" {
#endif
extern TSLanguage *tree_sitter_@PARSERNAME@();
#ifdef __cplusplus
}
#endif
#endif // TREE_SITTER_@UPPER_PARSERNAME@_H_

@ -1,11 +0,0 @@
prefix=@PREFIX@
libdir=@LIBDIR@
includedir=@INCLUDEDIR@
additionallibs=@ADDITIONALLIBS@
Name: tree-sitter-@PARSERNAME@
Description: A tree-sitter grammar for the @PARSERNAME@ programming language.
URL: @PARSERURL@
Version: @VERSION@
Libs: -L${libdir} ${additionallibs} -ltree-sitter-@PARSERNAME@
Cflags: -I${includedir}

@ -1,28 +0,0 @@
#include "tree_sitter/parser.h"
#include <node.h>
#include "nan.h"
using namespace v8;
extern "C" TSLanguage * tree_sitter_java();
namespace {
NAN_METHOD(New) {}
void Init(Local<Object> exports, Local<Object> module) {
Local<FunctionTemplate> tpl = Nan::New<FunctionTemplate>(New);
tpl->SetClassName(Nan::New("Language").ToLocalChecked());
tpl->InstanceTemplate()->SetInternalFieldCount(1);
Local<Function> constructor = Nan::GetFunction(tpl).ToLocalChecked();
Local<Object> instance = constructor->NewInstance(Nan::GetCurrentContext()).ToLocalChecked();
Nan::SetInternalFieldPointer(instance, 0, tree_sitter_java());
Nan::Set(instance, Nan::New("name").ToLocalChecked(), Nan::New("java").ToLocalChecked());
Nan::Set(module, Nan::New("exports").ToLocalChecked(), instance);
}
NODE_MODULE(tree_sitter_java_binding, Init)
} // namespace

@ -1,19 +0,0 @@
try {
module.exports = require("../../build/Release/tree_sitter_java_binding");
} catch (error1) {
if (error1.code !== 'MODULE_NOT_FOUND') {
throw error1;
}
try {
module.exports = require("../../build/Debug/tree_sitter_java_binding");
} catch (error2) {
if (error2.code !== 'MODULE_NOT_FOUND') {
throw error2;
}
throw error1
}
}
try {
module.exports.nodeTypeInfo = require("../../src/node-types.json");
} catch (_) {}

@ -1,39 +0,0 @@
# tree-sitter-java
This crate provides a Java grammar for the [tree-sitter][] parsing library. To
use this crate, add it to the `[dependencies]` section of your `Cargo.toml`
file. (Note that you will probably also need to depend on the
[`tree-sitter`][tree-sitter crate] crate to use the parsed result in any useful
way.)
``` toml
[dependencies]
tree-sitter = "0.17"
tree-sitter-java = "0.16"
```
Typically, you will use the [language][language func] function to add this
grammar to a tree-sitter [Parser][], and then use the parser to parse some code:
``` rust
let code = r#"
class Test {
int double(int x) {
return x * 2;
}
}
"#;
let mut parser = Parser::new();
parser.set_language(tree_sitter_java::language()).expect("Error loading Java grammar");
let parsed = parser.parse(code, None);
```
If you have any questions, please reach out to us in the [tree-sitter
discussions] page.
[Language]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Language.html
[language func]: https://docs.rs/tree-sitter-java/*/tree_sitter_java/fn.language.html
[Parser]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Parser.html
[tree-sitter]: https://tree-sitter.github.io/
[tree-sitter crate]: https://crates.io/crates/tree-sitter
[tree-sitter discussions]: https://github.com/tree-sitter/tree-sitter/discussions

@ -1,17 +0,0 @@
use std::path::Path;
extern crate cc;
fn main() {
let src_dir = Path::new("src");
let mut c_config = cc::Build::new();
c_config.include(src_dir);
c_config
.flag_if_supported("-Wno-unused-parameter")
.flag_if_supported("-Wno-unused-but-set-variable")
.flag_if_supported("-Wno-trigraphs");
let parser_path = src_dir.join("parser.c");
c_config.file(&parser_path);
println!("cargo:rerun-if-changed={}", parser_path.to_str().unwrap());
c_config.compile("parser");
}

@ -1,71 +0,0 @@
// -*- coding: utf-8 -*-
// ------------------------------------------------------------------------------------------------
// Copyright © 2020, tree-sitter-java authors.
// See the LICENSE file in this repo for license details.
// ------------------------------------------------------------------------------------------------
//! This crate provides a Java grammar for the [tree-sitter][] parsing library.
//!
//! Typically, you will use the [language][language func] function to add this grammar to a
//! tree-sitter [Parser][], and then use the parser to parse some code:
//!
//! ```
//! use tree_sitter::Parser;
//!
//! let code = r#"
//! class Test {
//! int double(int x) {
//! return x * 2;
//! }
//! }
//! "#;
//! let mut parser = Parser::new();
//! parser.set_language(tree_sitter_java::language()).expect("Error loading Java grammar");
//! let parsed = parser.parse(code, None);
//! # let parsed = parsed.unwrap();
//! # let root = parsed.root_node();
//! # assert!(!root.has_error());
//! ```
//!
//! [Language]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Language.html
//! [language func]: fn.language.html
//! [Parser]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Parser.html
//! [tree-sitter]: https://tree-sitter.github.io/
use tree_sitter::Language;
extern "C" {
fn tree_sitter_java() -> Language;
}
/// Returns the tree-sitter [Language][] for this grammar.
///
/// [Language]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Language.html
pub fn language() -> Language {
unsafe { tree_sitter_java() }
}
/// The source of the Java tree-sitter grammar description.
pub const GRAMMAR: &str = include_str!("../../grammar.js");
/// The syntax highlighting query for this language.
pub const HIGHLIGHT_QUERY: &str = include_str!("../../queries/highlights.scm");
/// The content of the [`node-types.json`][] file for this grammar.
///
/// [`node-types.json`]: https://tree-sitter.github.io/tree-sitter/using-parsers#static-node-types
pub const NODE_TYPES: &str = include_str!("../../src/node-types.json");
/// The symbol tagging query for this language.
pub const TAGGING_QUERY: &str = include_str!("../../queries/tags.scm");
#[cfg(test)]
mod tests {
#[test]
fn can_load_grammar() {
let mut parser = tree_sitter::Parser::new();
parser
.set_language(super::language())
.expect("Error loading Java grammar");
}
}

@ -1,16 +0,0 @@
#ifndef TREE_SITTER_JAVA_H_
#define TREE_SITTER_JAVA_H_
typedef struct TSLanguage TSLanguage;
#ifdef __cplusplus
extern "C" {
#endif
extern TSLanguage *tree_sitter_java();
#ifdef __cplusplus
}
#endif
#endif // TREE_SITTER_JAVA_H_

File diff suppressed because it is too large Load Diff

@ -1,5 +0,0 @@
### Problem description
### Expected parse tree output (optional)
### Actual parse tree output (optional)

@ -1,40 +0,0 @@
{
"name": "tree-sitter-java",
"version": "0.19.1",
"description": "Java grammar for tree-sitter",
"main": "bindings/node",
"keywords": [
"parser",
"java"
],
"author": "Ayman Nadeem",
"license": "MIT",
"bugs": {
"url": "https://github.com/tree-sitter/tree-sitter-java/issues"
},
"homepage": "https://github.com/tree-sitter/tree-sitter-java#readme",
"dependencies": {
"nan": "^2.14.1"
},
"devDependencies": {
"tree-sitter-cli": "^0.20.6"
},
"scripts": {
"build": "tree-sitter generate && node-gyp rebuild",
"test": "tree-sitter test && script/parse-examples",
"test-windows": "tree-sitter test",
"build-test": "tree-sitter generate && node-gyp build && tree-sitter test"
},
"repository": {
"type": "git",
"url": "git+https://github.com/tree-sitter/tree-sitter-java.git"
},
"tree-sitter": [
{
"scope": "source.java",
"file-types": [
"java"
]
}
]
}

@ -1,141 +0,0 @@
; Methods
(method_declaration
name: (identifier) @function.method)
(method_invocation
name: (identifier) @function.method)
(super) @function.builtin
; Annotations
(annotation
name: (identifier) @attribute)
(marker_annotation
name: (identifier) @attribute)
"@" @operator
; Types
(type_identifier) @type
(interface_declaration
name: (identifier) @type)
(class_declaration
name: (identifier) @type)
(enum_declaration
name: (identifier) @type)
((field_access
object: (identifier) @type)
(#match? @type "^[A-Z]"))
((scoped_identifier
scope: (identifier) @type)
(#match? @type "^[A-Z]"))
((method_invocation
object: (identifier) @type)
(#match? @type "^[A-Z]"))
((method_reference
. (identifier) @type)
(#match? @type "^[A-Z]"))
(constructor_declaration
name: (identifier) @type)
[
(boolean_type)
(integral_type)
(floating_point_type)
(floating_point_type)
(void_type)
] @type.builtin
; Variables
((identifier) @constant
(#match? @constant "^_*[A-Z][A-Z\\d_]+$"))
(identifier) @variable
(this) @variable.builtin
; Literals
[
(hex_integer_literal)
(decimal_integer_literal)
(octal_integer_literal)
(decimal_floating_point_literal)
(hex_floating_point_literal)
] @number
[
(character_literal)
(string_literal)
] @string
(escape_sequence) @string.escape
[
(true)
(false)
(null_literal)
] @constant.builtin
[
(line_comment)
(block_comment)
] @comment
; Keywords
[
"abstract"
"assert"
"break"
"case"
"catch"
"class"
"continue"
"default"
"do"
"else"
"enum"
"exports"
"extends"
"final"
"finally"
"for"
"if"
"implements"
"import"
"instanceof"
"interface"
"module"
"native"
"new"
"non-sealed"
"open"
"opens"
"package"
"private"
"protected"
"provides"
"public"
"requires"
"return"
"sealed"
"static"
"strictfp"
"switch"
"synchronized"
"throw"
"throws"
"to"
"transient"
"transitive"
"try"
"uses"
"volatile"
"while"
"with"
] @keyword

@ -1,20 +0,0 @@
(class_declaration
name: (identifier) @name) @definition.class
(method_declaration
name: (identifier) @name) @definition.method
(method_invocation
name: (identifier) @name
arguments: (argument_list) @reference.call)
(interface_declaration
name: (identifier) @name) @definition.interface
(type_list
(type_identifier) @name) @reference.implementation
(object_creation_expression
type: (type_identifier) @name) @reference.class
(superclass (type_identifier) @name) @reference.class

@ -1,3 +0,0 @@
examples/flink/flink-quickstart/flink-quickstart-java/src/main/resources/archetype-resources/src/main/java/DataStreamJob.java
examples/flink/flink-walkthroughs/flink-walkthrough-datastream-java/src/main/resources/archetype-resources/src/main/java/FraudDetectionJob.java
examples/flink/flink-walkthroughs/flink-walkthrough-datastream-java/src/main/resources/archetype-resources/src/main/java/FraudDetector.java

@ -1,48 +0,0 @@
#!/bin/bash
set -e
cd "$(dirname "$0")/.."
# clone_repo OWNER NAME SHA
function clone_repo {
owner=$1
name=$2
sha=$3
path=examples/$name
if [ ! -d "$path" ]; then
echo "Cloning $owner/$name"
git clone "https://github.com/$owner/$name" "$path"
fi
pushd "$path" > /dev/null
if [ "$(git rev-parse HEAD)" != "$sha" ]; then
echo "Updating $owner/$name to $sha"
git fetch
git reset --hard $sha
fi
popd > /dev/null
}
clone_repo elastic elasticsearch 4d62640bf116af7e825d89c7319a39c3f2f325b4
clone_repo google guava v31.1
clone_repo ReactiveX RxJava v3.1.6
clone_repo apache flink release-1.16.0
clone_repo apache logging-log4j2 rel/2.19.0
clone_repo apache cassandra cassandra-4.1.0
known_failures="$(cat script/known-failures.txt)"
tree-sitter parse -q \
'examples/**/*.java' \
$(for file in $known_failures; do echo "!${file}"; done)
example_count=$(find examples -name '*.java' | wc -l)
failure_count=$(wc -w <<< "$known_failures")
success_count=$(( $example_count - $failure_count ))
success_percent=$(bc -l <<< "100*${success_count}/${example_count}")
printf \
"Successfully parsed %d of %d example files (%.1f%%)\n" \
$success_count $example_count $success_percent

@ -1,19 +0,0 @@
# Run-Javaparser
This is a command-line program for running the [`Javaparser`](https://javaparser.org/) library on a Java source file.
In developing this grammar, it is helpful to compare the syntax trees to those produced by `Javaparser`, the most popular Java-parsing library in the Java ecosystem. Unlike the Java language spec, `Javaparser` is designed to produce syntax trees that are useful for code analysis.
## Usage
On macOS, compile the parser with this script:
```sh
./bootstrap
```
Then run it on a java file like this
```sh
./run path/to/the/file.java
```

@ -1,5 +0,0 @@
#!/bin/bash
brew install maven
brew install jq
mvn install

@ -1,24 +0,0 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.tree-sitter</groupId>
<artifactId>run-javaparser</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Run Javaparser</name>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.github.javaparser</groupId>
<artifactId>javaparser-core</artifactId>
<version>3.5.9</version>
</dependency>
</dependencies>
</project>

@ -1,12 +0,0 @@
#!/bin/bash
filename=$1
if [[ ! "$filename" = /* ]]; then
filename=${PWD}/$filename
fi
cd $(dirname $0)
java -cp \
~/.m2/repository/com/github/javaparser/javaparser-core/3.5.9/javaparser-core-3.5.9.jar:target/run-javaparser-1.0-SNAPSHOT.jar \
RunJavaParser \
$filename | jq .

@ -1,26 +0,0 @@
import com.github.javaparser.*;
import com.github.javaparser.ast.*;
import com.github.javaparser.printer.*;
import java.nio.file.*;
import java.nio.charset.*;
public class RunJavaParser
{
public static void main(String[] args) {
String sourceCode = readFile(args[0]);
CompilationUnit compilationUnit = JavaParser.parse(sourceCode);
JsonPrinter printer = new JsonPrinter(true);
System.out.println(printer.output(compilationUnit));
}
static String readFile(String path)
{
try {
byte[] encoded = Files.readAllBytes(Paths.get(path));
return new String(encoded, StandardCharsets.UTF_8);
} catch (Exception error) {
System.err.println(error);
return "";
}
}
}

@ -1,5 +0,0 @@
#Generated by Maven
#Thu May 28 10:27:28 PDT 2020
groupId=com.github.tree-sitter
artifactId=run-javaparser
version=1.0-SNAPSHOT

@ -1 +0,0 @@
/Users/maxbrunsfeld/github/tree-sitter-java/script/run-javaparser/src/main/java/com/github/tree-sitter/DemoApp.java

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

@ -1,224 +0,0 @@
#ifndef TREE_SITTER_PARSER_H_
#define TREE_SITTER_PARSER_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#define ts_builtin_sym_error ((TSSymbol)-1)
#define ts_builtin_sym_end 0
#define TREE_SITTER_SERIALIZATION_BUFFER_SIZE 1024
typedef uint16_t TSStateId;
#ifndef TREE_SITTER_API_H_
typedef uint16_t TSSymbol;
typedef uint16_t TSFieldId;
typedef struct TSLanguage TSLanguage;
#endif
typedef struct {
TSFieldId field_id;
uint8_t child_index;
bool inherited;
} TSFieldMapEntry;
typedef struct {
uint16_t index;
uint16_t length;
} TSFieldMapSlice;
typedef struct {
bool visible;
bool named;
bool supertype;
} TSSymbolMetadata;
typedef struct TSLexer TSLexer;
struct TSLexer {
int32_t lookahead;
TSSymbol result_symbol;
void (*advance)(TSLexer *, bool);
void (*mark_end)(TSLexer *);
uint32_t (*get_column)(TSLexer *);
bool (*is_at_included_range_start)(const TSLexer *);
bool (*eof)(const TSLexer *);
};
typedef enum {
TSParseActionTypeShift,
TSParseActionTypeReduce,
TSParseActionTypeAccept,
TSParseActionTypeRecover,
} TSParseActionType;
typedef union {
struct {
uint8_t type;
TSStateId state;
bool extra;
bool repetition;
} shift;
struct {
uint8_t type;
uint8_t child_count;
TSSymbol symbol;
int16_t dynamic_precedence;
uint16_t production_id;
} reduce;
uint8_t type;
} TSParseAction;
typedef struct {
uint16_t lex_state;
uint16_t external_lex_state;
} TSLexMode;
typedef union {
TSParseAction action;
struct {
uint8_t count;
bool reusable;
} entry;
} TSParseActionEntry;
struct TSLanguage {
uint32_t version;
uint32_t symbol_count;
uint32_t alias_count;
uint32_t token_count;
uint32_t external_token_count;
uint32_t state_count;
uint32_t large_state_count;
uint32_t production_id_count;
uint32_t field_count;
uint16_t max_alias_sequence_length;
const uint16_t *parse_table;
const uint16_t *small_parse_table;
const uint32_t *small_parse_table_map;
const TSParseActionEntry *parse_actions;
const char * const *symbol_names;
const char * const *field_names;
const TSFieldMapSlice *field_map_slices;
const TSFieldMapEntry *field_map_entries;
const TSSymbolMetadata *symbol_metadata;
const TSSymbol *public_symbol_map;
const uint16_t *alias_map;
const TSSymbol *alias_sequences;
const TSLexMode *lex_modes;
bool (*lex_fn)(TSLexer *, TSStateId);
bool (*keyword_lex_fn)(TSLexer *, TSStateId);
TSSymbol keyword_capture_token;
struct {
const bool *states;
const TSSymbol *symbol_map;
void *(*create)(void);
void (*destroy)(void *);
bool (*scan)(void *, TSLexer *, const bool *symbol_whitelist);
unsigned (*serialize)(void *, char *);
void (*deserialize)(void *, const char *, unsigned);
} external_scanner;
const TSStateId *primary_state_ids;
};
/*
* Lexer Macros
*/
#define START_LEXER() \
bool result = false; \
bool skip = false; \
bool eof = false; \
int32_t lookahead; \
goto start; \
next_state: \
lexer->advance(lexer, skip); \
start: \
skip = false; \
lookahead = lexer->lookahead;
#define ADVANCE(state_value) \
{ \
state = state_value; \
goto next_state; \
}
#define SKIP(state_value) \
{ \
skip = true; \
state = state_value; \
goto next_state; \
}
#define ACCEPT_TOKEN(symbol_value) \
result = true; \
lexer->result_symbol = symbol_value; \
lexer->mark_end(lexer);
#define END_STATE() return result;
/*
* Parse Table Macros
*/
#define SMALL_STATE(id) id - LARGE_STATE_COUNT
#define STATE(id) id
#define ACTIONS(id) id
#define SHIFT(state_value) \
{{ \
.shift = { \
.type = TSParseActionTypeShift, \
.state = state_value \
} \
}}
#define SHIFT_REPEAT(state_value) \
{{ \
.shift = { \
.type = TSParseActionTypeShift, \
.state = state_value, \
.repetition = true \
} \
}}
#define SHIFT_EXTRA() \
{{ \
.shift = { \
.type = TSParseActionTypeShift, \
.extra = true \
} \
}}
#define REDUCE(symbol_val, child_count_val, ...) \
{{ \
.reduce = { \
.type = TSParseActionTypeReduce, \
.symbol = symbol_val, \
.child_count = child_count_val, \
__VA_ARGS__ \
}, \
}}
#define RECOVER() \
{{ \
.type = TSParseActionTypeRecover \
}}
#define ACCEPT_INPUT() \
{{ \
.type = TSParseActionTypeAccept \
}}
#ifdef __cplusplus
}
#endif
#endif // TREE_SITTER_PARSER_H_

@ -1,37 +0,0 @@
====
comment
====
// This is a comment
/* This is also a comment */
/* this comment /* // /** ends here: */
/**/
a /* dkjfhsdf + */ + b; /* ***** */
---
(program
(line_comment)
(block_comment)
(block_comment)
(block_comment)
(expression_statement
(binary_expression
(identifier)
(block_comment)
(identifier)))
(block_comment))
======================
comments and literals
======================
123;
// comment
---
(program
(expression_statement (decimal_integer_literal))
(line_comment))

@ -1,850 +0,0 @@
===============
local variable
===============
class A {
public int b() {
int c = 5;
}
}
---
(program
(class_declaration
name: (identifier)
body: (class_body
(method_declaration
(modifiers)
type: (integral_type)
name: (identifier)
parameters: (formal_parameters)
body: (block
(local_variable_declaration
type: (integral_type)
declarator: (variable_declarator
name: (identifier)
value: (decimal_integer_literal))))))))
=====================
local array variable
=====================
String[] nodeNames = internalCluster().getNodeNames();
Integer[][] inputArrays = new Integer[0x100][];
---
(program
(local_variable_declaration
type: (array_type
element: (type_identifier)
dimensions: (dimensions))
declarator: (variable_declarator
name: (identifier)
value: (method_invocation
object: (method_invocation
name: (identifier)
arguments: (argument_list))
name: (identifier)
arguments: (argument_list))))
(local_variable_declaration
type: (array_type
element: (type_identifier)
dimensions: (dimensions))
declarator: (variable_declarator
name: (identifier)
value: (array_creation_expression
type: (type_identifier)
dimensions: (dimensions_expr (hex_integer_literal))
dimensions: (dimensions)))))
==========
module
==========
module com.foo { }
open module com.foo { }
---
(program
(module_declaration
name: (scoped_identifier
scope: (identifier)
name: (identifier))
body: (module_body))
(module_declaration
name: (scoped_identifier
scope: (identifier)
name: (identifier))
body: (module_body)))
==============================
module with normal annotation
==============================
@RequestForEnhancement(
id = 2868724,
synopsis = "Provide time-travel functionality",
engineer = "Mr. Peabody",
date = "4/1/2004"
)
module com.foo { }
---
(program
(module_declaration
(annotation
(identifier)
(annotation_argument_list
(element_value_pair (identifier) (decimal_integer_literal))
(element_value_pair (identifier) (string_literal (string_fragment)))
(element_value_pair (identifier) (string_literal (string_fragment)))
(element_value_pair (identifier) (string_literal (string_fragment)))))
(scoped_identifier (identifier) (identifier))
(module_body)))
==============================
module with marker annotation
==============================
@Preliminary module com.foo { }
@Preliminary open module com.foo { }
---
(program
(module_declaration
(marker_annotation (identifier))
(scoped_identifier (identifier) (identifier))
(module_body))
(module_declaration
(marker_annotation (identifier))
(scoped_identifier (identifier) (identifier))
(module_body)))
======================================
module with single element annotation
======================================
@Copyright("a")
module com.foo {}
---
(program
(module_declaration
(annotation
(identifier)
(annotation_argument_list (string_literal (string_fragment))))
(scoped_identifier
(identifier)
(identifier))
(module_body)))
====================
package_declaration
====================
package myVector;
---
(program (package_declaration (identifier)))
=================
module directive
=================
module com.example.foo {
requires com.example.foo.http;
}
---
(program
(module_declaration
name: (scoped_identifier
scope: (scoped_identifier
scope: (identifier)
name: (identifier))
name: (identifier))
body: (module_body
(requires_module_directive
module: (scoped_identifier
scope: (scoped_identifier
scope: (scoped_identifier
scope: (identifier)
name: (identifier))
name: (identifier))
name: (identifier))))))
==================================================================
module directive with requires, exports, opens, uses and provides
==================================================================
module com.example.foo {
requires com.example.http;
requires java.logging;
requires transitive com.example.network;
exports com.example.bar;
exports com.example.internal to com.example.probe;
opens com.example.quux;
opens com.example.internal to com.example.network, com.example.probe;
uses com.example.Intf;
provides com.example.Intf with com.example.Impl;
}
---
(program
(module_declaration
(scoped_identifier (scoped_identifier (identifier) (identifier)) (identifier))
(module_body
(requires_module_directive
(scoped_identifier (scoped_identifier (identifier) (identifier)) (identifier)))
(requires_module_directive
(scoped_identifier (identifier) (identifier)))
(requires_module_directive
(requires_modifier)
(scoped_identifier (scoped_identifier (identifier) (identifier)) (identifier)))
(exports_module_directive
(scoped_identifier (scoped_identifier (identifier) (identifier)) (identifier)))
(exports_module_directive
(scoped_identifier (scoped_identifier (identifier) (identifier)) (identifier))
(scoped_identifier (scoped_identifier (identifier) (identifier)) (identifier)))
(opens_module_directive
(scoped_identifier (scoped_identifier (identifier) (identifier)) (identifier)))
(opens_module_directive
(scoped_identifier (scoped_identifier (identifier) (identifier)) (identifier))
(scoped_identifier (scoped_identifier (identifier) (identifier)) (identifier))
(scoped_identifier (scoped_identifier (identifier) (identifier)) (identifier)))
(uses_module_directive
(scoped_identifier (scoped_identifier (identifier) (identifier)) (identifier)))
(provides_module_directive
(scoped_identifier (scoped_identifier (identifier) (identifier)) (identifier))
(scoped_identifier (scoped_identifier (identifier) (identifier)) (identifier))))))
===============================
single type import declaration
===============================
import java.util.Vector;
---
(program (import_declaration
(scoped_identifier (scoped_identifier (identifier) (identifier)) (identifier))))
===========================
type_import_on_declaraction
===========================
import java.util.*;
---
(program (import_declaration
(scoped_identifier (identifier) (identifier)) (asterisk)))
=================================
single static import declaration
=================================
import static java.util.Vector;
---
(program (import_declaration
(scoped_identifier
(scoped_identifier (identifier) (identifier))
(identifier))))
===================================
static import on demand declaration
===================================
import static java.util.*;
---
(program (import_declaration
(scoped_identifier (identifier) (identifier))
(asterisk)))
=================
class declaration
=================
class Point {
}
---
(program
(class_declaration
(identifier)
(class_body)))
=====================================================================
class declaration involving public, private, abstract and superclass
=====================================================================
public class Point {
}
private class Point {
}
abstract class ColoredPoint extends Point {
}
---
(program
(class_declaration (modifiers) (identifier) (class_body))
(class_declaration (modifiers) (identifier) (class_body))
(class_declaration (modifiers) (identifier) (superclass (type_identifier)) (class_body)))
==================================
class declaration with implements
==================================
public class Dog implements ISpeak {
}
---
(program
(class_declaration
(modifiers) (identifier)
(super_interfaces (type_list (type_identifier))) (class_body)))
============================
class declaration with body
============================
class Point {
int x;
void bar() {
x = 2;
}
}
---
(program
(class_declaration
(identifier)
(class_body
(field_declaration
(integral_type)
(variable_declarator (identifier)))
(method_declaration
(void_type)
(identifier)
(formal_parameters)
(block
(expression_statement
(assignment_expression (identifier) (decimal_integer_literal))))))))
======================
interface declaration
======================
interface Top {
}
---
(program
(interface_declaration
(identifier)
(interface_body)))
===================================
interface declaration with extends
===================================
interface Left extends Top {
}
interface Bottom extends Left, Right {}
---
(program
(interface_declaration
(identifier)
(extends_interfaces (type_list (type_identifier)))
(interface_body))
(interface_declaration
(identifier)
(extends_interfaces (type_list (type_identifier) (type_identifier))) (interface_body)))
===========================================
interface with annotation type declaration
===========================================
@interface SelfRef {}
---
(program
(annotation_type_declaration (identifier) (annotation_type_body)))
===================
method declaration
===================
class Beyonce {
void calculateAnswer(double wingSpan, int numberOfEngines,
double length, double grossTons) {
//do the calculation here
}
}
---
(program
(class_declaration
(identifier)
(class_body
(method_declaration
(void_type)
(identifier)
(formal_parameters
(formal_parameter (floating_point_type) (identifier))
(formal_parameter (integral_type) (identifier))
(formal_parameter (floating_point_type) (identifier))
(formal_parameter (floating_point_type) (identifier)))
(block (line_comment))))))
========================
constructor declaration
========================
class Point {
int x, y;
Point(int x, int y) {
this.x = x;
this.y = y;
}
Point() {
this(0, 0);
}
}
---
(program
(class_declaration
name: (identifier)
body: (class_body
(field_declaration
type: (integral_type)
declarator: (variable_declarator name: (identifier))
declarator: (variable_declarator name: (identifier)))
(constructor_declaration
name: (identifier)
parameters: (formal_parameters
(formal_parameter
type: (integral_type)
name: (identifier))
(formal_parameter
type: (integral_type)
name: (identifier)))
body: (constructor_body
(expression_statement (assignment_expression
left: (field_access
object: (this)
field: (identifier))
right: (identifier)))
(expression_statement (assignment_expression
left: (field_access
object: (this)
field: (identifier))
right: (identifier)))))
(constructor_declaration
name: (identifier)
parameters: (formal_parameters)
body: (constructor_body
(explicit_constructor_invocation
constructor: (this)
arguments: (argument_list
(decimal_integer_literal)
(decimal_integer_literal))))))))
=======
throws
=======
class Beyonce {
BufferedReader newReader() throws FileNotFoundException {
new BufferedReader(new InputStreamReader(new FileInputStream(file), charset));
}
}
---
(program
(class_declaration
(identifier)
(class_body
(method_declaration
(type_identifier)
(identifier)
(formal_parameters)
(throws (type_identifier))
(block
(expression_statement
(object_creation_expression
(type_identifier)
(argument_list
(object_creation_expression
(type_identifier)
(argument_list
(object_creation_expression
(type_identifier)
(argument_list (identifier)))
(identifier)))))))))))
======================
object instantiation
======================
class Point {
public double Foo() {
new BufferedWriter();
Foo.new BufferedWriter();
}
}
---
(program
(class_declaration
(identifier)
(class_body
(method_declaration
(modifiers)
(floating_point_type)
(identifier)
(formal_parameters)
(block
(expression_statement
(object_creation_expression
(type_identifier)
(argument_list)))
(expression_statement
(object_creation_expression
(identifier)
(type_identifier)
(argument_list))))))))
=====================
variable declaration
=====================
class JayZ {
public void Beyonce() {
int blue_ivy_carter;
};
}
---
(program
(class_declaration
(identifier)
(class_body
(method_declaration
(modifiers)
(void_type)
(identifier)
(formal_parameters)
(block
(local_variable_declaration
(integral_type)
(variable_declarator
(identifier))))))))
=================
enum declaration
=================
enum HandSign {
SCISSOR, PAPER, STONE
}
---
(program
(enum_declaration
name: (identifier)
body: (enum_body
(enum_constant name: (identifier))
(enum_constant name: (identifier))
(enum_constant name: (identifier)))))
=================
enum declaration inside an interface
=================
public @interface Foo {
enum HandSign {
SCISSOR, PAPER, STONE
}
}
---
(program
(annotation_type_declaration
(modifiers)
name: (identifier)
body: (annotation_type_body
(enum_declaration
name: (identifier)
body: (enum_body
(enum_constant
name: (identifier))
(enum_constant
name: (identifier))
(enum_constant
name: (identifier)))))))
=================
record declaration
==================
public record Foo(int bar) {
}
---
(program
(record_declaration
(modifiers)
name: (identifier)
parameters: (formal_parameters
(formal_parameter
type: (integral_type)
name: (identifier)))
body: (class_body)))
================================
record declaration with generics
================================
public record Foo<T>(T bar) {
}
---
(program
(record_declaration
(modifiers)
name: (identifier)
type_parameters: (type_parameters
(type_parameter
(type_identifier)))
parameters: (formal_parameters
(formal_parameter
type: (type_identifier)
name: (identifier)))
body: (class_body)))
=================================
record declaration inside a class
=================================
public class Usecase {
public static record Commande(@NotNull String param) {
public Commande foo() {
return new Commande("");
}
}
}
---
(program
(class_declaration
(modifiers)
name: (identifier)
body: (class_body
(record_declaration
(modifiers)
name: (identifier)
parameters: (formal_parameters
(formal_parameter
(modifiers
(marker_annotation
name: (identifier)))
type: (type_identifier)
name: (identifier)))
body: (class_body
(method_declaration
(modifiers)
type: (type_identifier)
name: (identifier)
parameters: (formal_parameters)
body: (block
(return_statement
(object_creation_expression
type: (type_identifier)
arguments: (argument_list
(string_literal)))))))))))
======================================
record declaration inside an interface
======================================
interface I { record R(int a) {} }
---
(program
(interface_declaration
(identifier)
(interface_body
(record_declaration
(identifier)
(formal_parameters
(formal_parameter
(integral_type)
(identifier)))
(class_body)))))
===========================================
record declaration with compact constructor
===========================================
record Person(int age) {
public Person {
if (age < 0) throw new IllegalArgumentException("invalid age");
}
}
---
(program
(record_declaration
(identifier)
(formal_parameters
(formal_parameter
(integral_type)
(identifier)))
(class_body
(compact_constructor_declaration
(modifiers)
(identifier)
(block
(if_statement
(condition
(binary_expression
(identifier)
(decimal_integer_literal)))
(throw_statement
(object_creation_expression
(type_identifier)
(argument_list
(string_literal
(string_fragment)))))))))))
============================================
record declaration that implements interface
============================================
record R() implements I {}
---
(program
(record_declaration
(identifier)
(formal_parameters)
(super_interfaces
(type_list
(type_identifier)))
(class_body)))
==============================================
class declaration with dollar-sign identifiers
==============================================
class A$B {
void func() {
$object.$property;
$hello();
}
}
---
(program
(class_declaration
(identifier)
(class_body
(method_declaration
(void_type)
(identifier)
(formal_parameters)
(block
(expression_statement
(field_access
(identifier)
(identifier)))
(expression_statement
(method_invocation
(identifier)
(argument_list))))))))
================
Sealed classes
================
sealed interface A permits B, C {
}
final class B implements A {}
non-sealed interface C extends A {}
---
(program
(interface_declaration
(modifiers)
(identifier)
(permits
(type_list
(type_identifier)
(type_identifier)))
(interface_body))
(class_declaration
(modifiers)
(identifier)
(super_interfaces
(type_list
(type_identifier)))
(class_body))
(interface_declaration
(modifiers)
(identifier)
(extends_interfaces
(type_list
(type_identifier)))
(interface_body)))

File diff suppressed because it is too large Load Diff

@ -1,401 +0,0 @@
========================
decimal integer literals
========================
123;
4l;
50L;
---
(program
(expression_statement (decimal_integer_literal))
(expression_statement (decimal_integer_literal))
(expression_statement (decimal_integer_literal)))
====================
hex integer literals
====================
0xa_bcd_ef0;
0Xa_bcd_ef0;
0X8000L;
---
(program
(expression_statement (hex_integer_literal))
(expression_statement (hex_integer_literal))
(expression_statement (hex_integer_literal)))
======================
octal integer literals
======================
0o123;
0O123;
0123;
---
(program
(expression_statement (octal_integer_literal))
(expression_statement (octal_integer_literal))
(expression_statement (octal_integer_literal)))
=======================
binary integer literal
=======================
0b001;
0B001;
0b10;
0B1000;
---
(program
(expression_statement (binary_integer_literal))
(expression_statement (binary_integer_literal))
(expression_statement (binary_integer_literal))
(expression_statement (binary_integer_literal)))
===============================
floating point literals
===============================
4.23e9;
4.23e-9;
4.23e+9;
40.3e6;
40.3e-6;
1.234;
0.123456;
.12345;
1e4;
0.2e-2;
0.0e-4;
.2e-2;
0x5.4;
0x5.4e-10;
---
(program
(expression_statement (decimal_floating_point_literal))
(expression_statement (decimal_floating_point_literal))
(expression_statement (decimal_floating_point_literal))
(expression_statement (decimal_floating_point_literal))
(expression_statement (decimal_floating_point_literal))
(expression_statement (decimal_floating_point_literal))
(expression_statement (decimal_floating_point_literal))
(expression_statement (decimal_floating_point_literal))
(expression_statement (decimal_floating_point_literal))
(expression_statement (decimal_floating_point_literal))
(expression_statement (decimal_floating_point_literal))
(expression_statement (decimal_floating_point_literal))
(expression_statement (hex_floating_point_literal))
(expression_statement (hex_floating_point_literal)))
================
boolean literals
================
true;
false;
---
(program
(expression_statement (true))
(expression_statement (false)))
===================
character literals
===================
'a';
'%';
'\t';
'\\';
'\'';
'\u03a9';
'\uFFFF';
'\177';
'™';
---
(program
(expression_statement (character_literal))
(expression_statement (character_literal))
(expression_statement (character_literal))
(expression_statement (character_literal))
(expression_statement (character_literal))
(expression_statement (character_literal))
(expression_statement (character_literal))
(expression_statement (character_literal))
(expression_statement (character_literal)))
===============
string literals
===============
"";
"\"";
"This is a string";
"'";
---
(program
(expression_statement
(string_literal))
(expression_statement
(string_literal
(escape_sequence)))
(expression_statement
(string_literal
(string_fragment)))
(expression_statement
(string_literal
(string_fragment))))
===============
text block
===============
"""
""";
"""
Closing token
at the same line""";
"""
Spaces after
opening token""";
"""
Closing token
at new line
""";
"""
{
"foo": 4
}
""";
"""
"this is single double quotes"
""this is double quotes""
"" """;
"""
"hi
""";
"""
\\
""";
---
(program
(expression_statement
(string_literal
(multiline_string_fragment)))
(expression_statement
(string_literal
(multiline_string_fragment)))
(expression_statement
(string_literal
(multiline_string_fragment)))
(expression_statement
(string_literal
(multiline_string_fragment)))
(expression_statement
(string_literal
(multiline_string_fragment)
(multiline_string_fragment)
(multiline_string_fragment)))
(expression_statement
(string_literal
(multiline_string_fragment)
(multiline_string_fragment)
(multiline_string_fragment)
(multiline_string_fragment)
(multiline_string_fragment)
(multiline_string_fragment)
(multiline_string_fragment)
(multiline_string_fragment)
(multiline_string_fragment)))
(expression_statement
(string_literal
(multiline_string_fragment)
(multiline_string_fragment)))
(expression_statement
(string_literal
(multiline_string_fragment))))
===============
escape sequences
===============
"\n\a\b\fhi im a piece of text\t\v and im some more text \\\'\"\?";
---
(program
(expression_statement
(string_literal
(escape_sequence)
(escape_sequence)
(escape_sequence)
(escape_sequence)
(string_fragment)
(escape_sequence)
(escape_sequence)
(string_fragment)
(escape_sequence)
(escape_sequence)
(escape_sequence)
(escape_sequence))))
=============
null literals
=============
null;
---
(program
(expression_statement (null_literal)))
=========================================
char escapes
=========================================
'\b';
'\t';
'\n';
'\f';
'\r';
'\"';
'\'';
'\\' + 'a';
---
(program
(expression_statement (character_literal))
(expression_statement (character_literal))
(expression_statement (character_literal))
(expression_statement (character_literal))
(expression_statement (character_literal))
(expression_statement (character_literal))
(expression_statement (character_literal))
(expression_statement (binary_expression (character_literal) (character_literal))))
=========================================
octal escapes
=========================================
'\7';
---
(program
(expression_statement (character_literal)))
=========================================
unicode escapes
=========================================
'\\u2122=\u2122';
---
(program
(expression_statement (character_literal)))
=================
ascii escapes
=================
'\NUL';
'\SOH';
'\STX';
'\ETX';
'\EOT';
'\ENQ';
'\ACK';
'\BEL';
'\BS';
'\TAB';
'\LF';
'\VT';
'\FF';
'\CR';
'\SO';
'\SI';
'\DLE';
'\DC1';
'\DC2';
'\DC3';
'\DC4';
'\NAK';
'\SYN';
'\ETB';
'\CAN';
'\EM';
'\SUB';
'\ESC';
'\FS';
'\GS';
'\RS';
'\US';
---
(program
(expression_statement (character_literal))
(expression_statement (character_literal))
(expression_statement (character_literal))
(expression_statement (character_literal))
(expression_statement (character_literal))
(expression_statement (character_literal))
(expression_statement (character_literal))
(expression_statement (character_literal))
(expression_statement (character_literal))
(expression_statement (character_literal))
(expression_statement (character_literal))
(expression_statement (character_literal))
(expression_statement (character_literal))
(expression_statement (character_literal))
(expression_statement (character_literal))
(expression_statement (character_literal))
(expression_statement (character_literal))
(expression_statement (character_literal))
(expression_statement (character_literal))
(expression_statement (character_literal))
(expression_statement (character_literal))
(expression_statement (character_literal))
(expression_statement (character_literal))
(expression_statement (character_literal))
(expression_statement (character_literal))
(expression_statement (character_literal))
(expression_statement (character_literal))
(expression_statement (character_literal))
(expression_statement (character_literal))
(expression_statement (character_literal))
(expression_statement (character_literal))
(expression_statement (character_literal)))
========================
class literals
========================
String.class;
---
(program
(expression_statement
(class_literal (type_identifier))))

@ -1,58 +0,0 @@
==========
Precedence
==========
class A {
public void b() {
int a = 1;
int b = 2;
bool c = (1*1) + a > 5;
bool d = a+b == a+b;
}
}
---
(program
(class_declaration
(identifier)
(class_body
(method_declaration
(modifiers)
(void_type)
(identifier)
(formal_parameters)
(block
(local_variable_declaration
(integral_type)
(variable_declarator
(identifier)
(decimal_integer_literal)))
(local_variable_declaration
(integral_type)
(variable_declarator
(identifier)
(decimal_integer_literal)))
(local_variable_declaration
(type_identifier)
(variable_declarator
(identifier)
(binary_expression
(binary_expression
(parenthesized_expression
(binary_expression
(decimal_integer_literal)
(decimal_integer_literal)))
(identifier))
(decimal_integer_literal))))
(local_variable_declaration
(type_identifier)
(variable_declarator
(identifier)
(binary_expression
(binary_expression
(identifier)
(identifier))
(binary_expression
(identifier)
(identifier))))))))))

@ -1,69 +0,0 @@
===============
integral types
===============
class Beyonce {
int formation() {
int x;
byte x;
short x;
long x;
char x;
}
}
---
(program
(class_declaration
(identifier)
(class_body
(method_declaration
(integral_type)
(identifier)
(formal_parameters)
(block
(local_variable_declaration
(integral_type)
(variable_declarator (identifier)))
(local_variable_declaration
(integral_type)
(variable_declarator (identifier)))
(local_variable_declaration
(integral_type)
(variable_declarator (identifier)))
(local_variable_declaration
(integral_type)
(variable_declarator (identifier)))
(local_variable_declaration
(integral_type)
(variable_declarator (identifier))))))))
=====================
floating point types
=====================
class Beyonce {
int formation() {
float x;
double x;
}
}
---
(program
(class_declaration
(identifier)
(class_body
(method_declaration
(integral_type)
(identifier)
(formal_parameters)
(block
(local_variable_declaration
(floating_point_type)
(variable_declarator (identifier)))
(local_variable_declaration
(floating_point_type)
(variable_declarator (identifier))))))))

@ -1,36 +0,0 @@
enum Material {
// ^ type
DENIM,
// ^ constant
CANVAS,
// ^ constant
SPANDEX_3_PERCENT
// ^ constant
}
class Person {
// ^ type
Person(string name) {
// <- type
// ^ type
this.name = name;
// ^ variable
this.pants = new Pants<Pocket>();
// ^ type
// ^ type
}
string getName() {
// <- type
// ^ function.method
a = this.name;
b = new one.two.Three();
// ^ type
// ^ type
// ^ type
c = Material.DENIM;
// ^ type
// ^ constant
}
}