initial commit

This commit is contained in:
soruh 2024-03-25 23:16:07 +01:00
commit 8f31c35bc9
62 changed files with 1348332 additions and 0 deletions

4
.gitattributes vendored Normal file
View File

@ -0,0 +1,4 @@
/src/** linguist-vendored
/script/** text eol=lf
/src/parser.c -diff
/src/grammar.json -diff

35
.github/workflows/ci.yml vendored Normal file
View File

@ -0,0 +1,35 @@
name: CI
on:
workflow_dispatch:
push:
branches:
- master
pull_request:
branches:
- "**" # Don't trust forks because miners
jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: true
matrix:
os: [macos-14, ubuntu-latest]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 18
- run: npm install
- run: npm test
test-windows:
runs-on: windows-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 18
- run: npm install
- run: npm run-script test-windows

33
.github/workflows/publish_crate.yml vendored Normal file
View File

@ -0,0 +1,33 @@
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 }}

13
.gitignore vendored Normal file
View File

@ -0,0 +1,13 @@
Cargo.lock
node_modules
build
npm-debug.log
package-lock.json
parser.exp
parser.lib
parser.obj
examples
!examples/.gitkeep
scanner.obj
/target/
.build/

6
.npmignore Normal file
View File

@ -0,0 +1,6 @@
examples
corpus
build
.travis.yml
appveyor.yml
target

36
Cargo.toml Normal file
View File

@ -0,0 +1,36 @@
[package]
name = "tree-sitter-c-sharp"
description = "C# grammar for the tree-sitter parsing library"
version = "0.20.0"
authors = [
"Damien Guard <damieng@gmail.com>",
"Max Brunsfeld <maxbrunsfeld@gmail.com>",
"Martin Midtgaard <martin.midtgaard@gmail.com>",
"Sjoerd Langkemper <sjoerd-github@linuxonly.nl>",
"Patrick Thomson <patrickt@github.com>",
"Noelle Caldwell <noelle.caldwell@microsoft.com>",
"Douglas Creager <dcreager@dcreager.net>",
]
license = "MIT"
readme = "bindings/rust/README.md"
keywords = ["incremental", "parsing", "c-sharp"]
categories = ["parsing", "text-editors"]
repository = "https://github.com/tree-sitter/tree-sitter-c-sharp"
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"

21
LICENSE Normal file
View File

@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2014-2023 Max Brunsfeld, Damien Guard, and contributors.
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.

38
Package.swift Normal file
View File

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

45
README.md Normal file
View File

@ -0,0 +1,45 @@
# tree-sitter-c-sharp
[![CI][ci]](https://github.com/tree-sitter/tree-sitter-c-sharp/actions/workflows/ci.yml)
[![discord][discord]](https://discord.gg/w7nTvsVJhm)
[![matrix][matrix]](https://matrix.to/#/#tree-sitter-chat:matrix.org)
[![crates][crates]](https://crates.io/crates/tree-sitter-c-sharp)
[![npm][npm]](https://www.npmjs.com/package/tree-sitter-c-sharp)
C# grammar for [tree-sitter](https://github.com/tree-sitter/tree-sitter) based upon the Roslyn grammar with changes in order to:
- Deal with differences between the parsing technologies
- Work around some bugs in that grammar
- Handle `#if`, `#else`, `#elif`, `#endif` blocks
- Support syntax highlighting/parsing of fragments
- Simplify the output tree
### Status
Comprehensive supports C# 1 through 11.0 with the following exceptions:
- [ ] `async`, `var` and `await` cannot be used as identifiers everywhere they are valid
- [ ] Raw string literals (C# 11)
#### C# 12.0 (under development)
- [ ] Alias any type
- [ ] Collection expressions
- [ ] Default lambda parameters
- [x] Experimental attribute
- [ ] Inline arrays
- [x] Interceptors
- [ ] Primary constructors
- [ ] Ref readonly parameters
### References
- [Official C# 6 Language Spec](https://github.com/dotnet/csharplang/blob/master/spec/) provides chapters that formally define the language grammar.
- [Roslyn C# language grammar export](https://github.com/dotnet/roslyn/blob/master/src/Compilers/CSharp/Portable/Generated/CSharp.Generated.g4)
- [SharpLab](https://sharplab.io) (web-based syntax tree playground based on Roslyn)
[ci]: https://img.shields.io/github/actions/workflow/status/tree-sitter/tree-sitter-c-sharp/ci.yml?logo=github&label=CI
[discord]: https://img.shields.io/discord/1063097320771698699?logo=discord&label=discord
[matrix]: https://img.shields.io/matrix/tree-sitter-chat%3Amatrix.org?logo=matrix&label=matrix
[npm]: https://img.shields.io/npm/v/tree-sitter-c-sharp?logo=npm
[crates]: https://img.shields.io/crates/v/tree-sitter-c-sharp?logo=rust

19
binding.gyp Normal file
View File

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

28
bindings/node/binding.cc Normal file
View File

@ -0,0 +1,28 @@
#include "tree_sitter/parser.h"
#include <node.h>
#include "nan.h"
using namespace v8;
extern "C" TSLanguage * tree_sitter_c_sharp();
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_c_sharp());
Nan::Set(instance, Nan::New("name").ToLocalChecked(), Nan::New("c_sharp").ToLocalChecked());
Nan::Set(module, Nan::New("exports").ToLocalChecked(), instance);
}
NODE_MODULE(tree_sitter_c_sharp_binding, Init)
} // namespace

19
bindings/node/index.js Normal file
View File

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

37
bindings/rust/README.md Normal file
View File

@ -0,0 +1,37 @@
# tree-sitter-c-sharp
This crate provides a C# 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-c-sharp = "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) => x * 2;
}
"#;
let mut parser = Parser::new();
parser.set_language(tree_sitter_c_sharp::language()).expect("Error loading C# 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-c-sharp/*/tree_sitter_c_sharp/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

19
bindings/rust/build.rs Normal file
View File

@ -0,0 +1,19 @@
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);
let scanner_path = src_dir.join("scanner.c");
c_config.file(&scanner_path);
println!("cargo:rerun-if-changed={}", parser_path.to_str().unwrap());
c_config.compile("parser-scanner");
}

69
bindings/rust/lib.rs Normal file
View File

@ -0,0 +1,69 @@
// -*- coding: utf-8 -*-
// ------------------------------------------------------------------------------------------------
// Copyright © 2020, tree-sitter-c-sharp authors.
// See the LICENSE file in this repo for license details.
// ------------------------------------------------------------------------------------------------
//! This crate provides a C# 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) => x * 2;
//! }
//! "#;
//! let mut parser = Parser::new();
//! parser.set_language(tree_sitter_c_sharp::language()).expect("Error loading C# 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_c_sharp() -> 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_c_sharp() }
}
/// The source of the C# tree-sitter grammar description.
pub const GRAMMAR: &'static str = include_str!("../../grammar.js");
/// 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: &'static str = include_str!("../../src/node-types.json");
/// The symbol tagging query for this language.
pub const TAGGING_QUERY: &'static str = include_str!("../../queries/tags.scm");
/// The syntax highlighting query for this language.
pub const HIGHLIGHT_QUERY: &'static str = include_str!("../../queries/highlights.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 C# grammar");
}
}

View File

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

481
corpus/attributes.txt Normal file
View File

@ -0,0 +1,481 @@
================================================================================
Global attributes
================================================================================
[assembly: Single]
[module: A, C()]
--------------------------------------------------------------------------------
(compilation_unit
(global_attribute_list
(attribute
name: (identifier)))
(global_attribute_list
(attribute
name: (identifier))
(attribute
name: (identifier)
(attribute_argument_list))))
================================================================================
Attributes with arguments
================================================================================
[A(B.C)]
class D {}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
(attribute_list
(attribute
name: (identifier)
(attribute_argument_list
(attribute_argument
(member_access_expression
expression: (identifier)
name: (identifier))))))
name: (identifier)
body: (declaration_list)))
================================================================================
Attributes with qualified name
================================================================================
[NS.A(B.C)]
class D {}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
(attribute_list
(attribute
name: (qualified_name
qualifier: (identifier)
name: (identifier))
(attribute_argument_list
(attribute_argument
(member_access_expression
expression: (identifier)
name: (identifier))))))
name: (identifier)
body: (declaration_list)))
================================================================================
Attributes on classes
================================================================================
[Single]
class A { }
[One][Two]
[Three]
class A { }
[One]
[Two,Three()]
class A { }
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
(attribute_list
(attribute
name: (identifier)))
name: (identifier)
body: (declaration_list))
(class_declaration
(attribute_list
(attribute
name: (identifier)))
(attribute_list
(attribute
name: (identifier)))
(attribute_list
(attribute
name: (identifier)))
name: (identifier)
body: (declaration_list))
(class_declaration
(attribute_list
(attribute
name: (identifier)))
(attribute_list
(attribute
name: (identifier))
(attribute
name: (identifier)
(attribute_argument_list)))
name: (identifier)
body: (declaration_list)))
================================================================================
Attributes on structs
================================================================================
[A,B()][C]
struct A { }
--------------------------------------------------------------------------------
(compilation_unit
(struct_declaration
(attribute_list
(attribute
name: (identifier))
(attribute
name: (identifier)
(attribute_argument_list)))
(attribute_list
(attribute
name: (identifier)))
name: (identifier)
body: (declaration_list)))
================================================================================
Attributes on fields
================================================================================
class Zzz {
[A,B()][C]
public int Z;
}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
name: (identifier)
body: (declaration_list
(field_declaration
(attribute_list
(attribute
name: (identifier))
(attribute
name: (identifier)
(attribute_argument_list)))
(attribute_list
(attribute
name: (identifier)))
(modifier)
(variable_declaration
type: (predefined_type)
(variable_declarator
name: (identifier)))))))
================================================================================
Attributes on methods
================================================================================
class Methods {
[ValidatedContract]
int Method1() { return 0; }
[method: ValidatedContract]
int Method2() { return 0; }
[return: ValidatedContract]
int Method3() { return 0; }
}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
name: (identifier)
body: (declaration_list
(method_declaration
(attribute_list
(attribute
name: (identifier)))
type: (predefined_type)
name: (identifier)
parameters: (parameter_list)
body: (block
(return_statement
(integer_literal))))
(method_declaration
(attribute_list
(attribute_target_specifier)
(attribute
name: (identifier)))
type: (predefined_type)
name: (identifier)
parameters: (parameter_list)
body: (block
(return_statement
(integer_literal))))
(method_declaration
(attribute_list
(attribute_target_specifier)
(attribute
name: (identifier)))
type: (predefined_type)
name: (identifier)
parameters: (parameter_list)
body: (block
(return_statement
(integer_literal)))))))
================================================================================
Attributes on enums
================================================================================
[Single]
enum A { B, C }
[One][Two]
[Three]
enum A { B, C }
[One]
[Two,Three()]
enum A { B, C }
--------------------------------------------------------------------------------
(compilation_unit
(enum_declaration
(attribute_list
(attribute
name: (identifier)))
name: (identifier)
body: (enum_member_declaration_list
(enum_member_declaration
name: (identifier))
(enum_member_declaration
name: (identifier))))
(enum_declaration
(attribute_list
(attribute
name: (identifier)))
(attribute_list
(attribute
name: (identifier)))
(attribute_list
(attribute
name: (identifier)))
name: (identifier)
body: (enum_member_declaration_list
(enum_member_declaration
name: (identifier))
(enum_member_declaration
name: (identifier))))
(enum_declaration
(attribute_list
(attribute
name: (identifier)))
(attribute_list
(attribute
name: (identifier))
(attribute
name: (identifier)
(attribute_argument_list)))
name: (identifier)
body: (enum_member_declaration_list
(enum_member_declaration
name: (identifier))
(enum_member_declaration
name: (identifier)))))
================================================================================
Attributes on events
================================================================================
class Zzz {
[A,B()][C]
public event EventHandler SomeEvent { add { } remove { } }
}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
name: (identifier)
body: (declaration_list
(event_declaration
(attribute_list
(attribute
name: (identifier))
(attribute
name: (identifier)
(attribute_argument_list)))
(attribute_list
(attribute
name: (identifier)))
(modifier)
type: (identifier)
name: (identifier)
accessors: (accessor_list
(accessor_declaration
body: (block))
(accessor_declaration
body: (block)))))))
================================================================================
Attributes on type parameters
================================================================================
class Class<[A, B][C()]T1> {
void Method<[E] [F, G(1)] T2>() {
}
}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
name: (identifier)
type_parameters: (type_parameter_list
(type_parameter
(attribute_list
(attribute
name: (identifier))
(attribute
name: (identifier)))
(attribute_list
(attribute
name: (identifier)
(attribute_argument_list)))
name: (identifier)))
body: (declaration_list
(method_declaration
type: (predefined_type)
name: (identifier)
type_parameters: (type_parameter_list
(type_parameter
(attribute_list
(attribute
name: (identifier)))
(attribute_list
(attribute
name: (identifier))
(attribute
name: (identifier)
(attribute_argument_list
(attribute_argument
(integer_literal)))))
name: (identifier)))
parameters: (parameter_list)
body: (block)))))
================================================================================
Attributes on event accessors
================================================================================
class Zzz {
public event EventHandler SomeEvent {
[A,B()][C] add { }
[A,B()][C] remove { }
}
}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
name: (identifier)
body: (declaration_list
(event_declaration
(modifier)
type: (identifier)
name: (identifier)
accessors: (accessor_list
(accessor_declaration
(attribute_list
(attribute
name: (identifier))
(attribute
name: (identifier)
(attribute_argument_list)))
(attribute_list
(attribute
name: (identifier)))
body: (block))
(accessor_declaration
(attribute_list
(attribute
name: (identifier))
(attribute
name: (identifier)
(attribute_argument_list)))
(attribute_list
(attribute
name: (identifier)))
body: (block)))))))
================================================================================
Attributes with trailing comma
================================================================================
[Theory,]
void A() { }
--------------------------------------------------------------------------------
(compilation_unit
(global_statement
(local_function_statement
(attribute_list
(attribute
name: (identifier)))
type: (predefined_type)
name: (identifier)
parameters: (parameter_list)
body: (block))))
================================================================================
Generic attribute
================================================================================
[Theory<About,Life>]
void A() { }
--------------------------------------------------------------------------------
(compilation_unit
(global_statement
(local_function_statement
(attribute_list
(attribute
name: (generic_name
name: (identifier)
type_arguments: (type_argument_list
(identifier)
(identifier)))))
type: (predefined_type)
name: (identifier)
parameters: (parameter_list)
body: (block))))
================================================================================
Lambda with attribute
================================================================================
var greeting = [Hello] () => Console.WriteLine("hello");
--------------------------------------------------------------------------------
(compilation_unit
(global_statement
(local_declaration_statement
(variable_declaration
type: (implicit_type)
(variable_declarator
name: (identifier)
(equals_value_clause
(lambda_expression
(attribute_list
(attribute
name: (identifier)))
parameters: (parameter_list)
body: (invocation_expression
function: (member_access_expression
expression: (identifier)
name: (identifier))
arguments: (argument_list
(argument
(string_literal
(string_literal_fragment))))))))))))

663
corpus/classes.txt Normal file
View File

@ -0,0 +1,663 @@
================================================================================
Global empty class
================================================================================
public class F {}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
(modifier)
name: (identifier)
body: (declaration_list)))
================================================================================
Class base is dynamic
================================================================================
public class F : dynamic { }
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
(modifier)
name: (identifier)
bases: (base_list
(identifier))
body: (declaration_list)))
================================================================================
Class base is object with interfaces
================================================================================
public class F : object, IAlpha, IOmega { }
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
(modifier)
name: (identifier)
bases: (base_list
(predefined_type)
(identifier)
(identifier))
body: (declaration_list)))
================================================================================
Partial class
================================================================================
public partial class F {}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
(modifier)
(modifier)
name: (identifier)
body: (declaration_list)))
================================================================================
Class with a single type parameter
================================================================================
class F<T> {}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
name: (identifier)
type_parameters: (type_parameter_list
(type_parameter
name: (identifier)))
body: (declaration_list)))
================================================================================
Class with multiple type parameters
================================================================================
internal class F<T1, T2> {}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
(modifier)
name: (identifier)
type_parameters: (type_parameter_list
(type_parameter
name: (identifier))
(type_parameter
name: (identifier)))
body: (declaration_list)))
================================================================================
Class with co-variant and contra-variant type parameters
================================================================================
internal class F<in T1, out T2> {}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
(modifier)
name: (identifier)
type_parameters: (type_parameter_list
(type_parameter
name: (identifier))
(type_parameter
name: (identifier)))
body: (declaration_list)))
================================================================================
Class with a type parameter struct constraint
================================================================================
public class F<T> where T:struct {}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
(modifier)
name: (identifier)
type_parameters: (type_parameter_list
(type_parameter
name: (identifier)))
(type_parameter_constraints_clause
target: (identifier)
constraints: (type_parameter_constraint))
body: (declaration_list)))
================================================================================
Class with a type parameter unmanaged constraint
================================================================================
public class F<T> where T:unmanaged {}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
(modifier)
name: (identifier)
type_parameters: (type_parameter_list
(type_parameter
name: (identifier)))
(type_parameter_constraints_clause
target: (identifier)
constraints: (type_parameter_constraint))
body: (declaration_list)))
================================================================================
Class with a type parameter class constraint
================================================================================
public class F<T> where T:class {}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
(modifier)
name: (identifier)
type_parameters: (type_parameter_list
(type_parameter
name: (identifier)))
(type_parameter_constraints_clause
target: (identifier)
constraints: (type_parameter_constraint))
body: (declaration_list)))
================================================================================
Class with a type parameter and nullable constraints
================================================================================
public class F<T> where T:class?, notnull, Mine? {}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
(modifier)
name: (identifier)
type_parameters: (type_parameter_list
(type_parameter
name: (identifier)))
(type_parameter_constraints_clause
target: (identifier)
constraints: (type_parameter_constraint)
constraints: (type_parameter_constraint)
constraints: (type_parameter_constraint
(type_constraint
type: (nullable_type
type: (identifier)))))
body: (declaration_list)))
================================================================================
Class with type parameter new constraint
================================================================================
public class F<T> where T: new() {}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
(modifier)
name: (identifier)
type_parameters: (type_parameter_list
(type_parameter
name: (identifier)))
(type_parameter_constraints_clause
target: (identifier)
constraints: (type_parameter_constraint
(constructor_constraint)))
body: (declaration_list)))
================================================================================
Class with type parameter identifier constraint
================================================================================
public class F<T> where T: I {}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
(modifier)
name: (identifier)
type_parameters: (type_parameter_list
(type_parameter
name: (identifier)))
(type_parameter_constraints_clause
target: (identifier)
constraints: (type_parameter_constraint
(type_constraint
type: (identifier))))
body: (declaration_list)))
================================================================================
Class with type parameter identifier and new constraints
================================================================================
public class F<T> where T: I, new() {}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
(modifier)
name: (identifier)
type_parameters: (type_parameter_list
(type_parameter
name: (identifier)))
(type_parameter_constraints_clause
target: (identifier)
constraints: (type_parameter_constraint
(type_constraint
type: (identifier)))
constraints: (type_parameter_constraint
(constructor_constraint)))
body: (declaration_list)))
================================================================================
Class with multiple type parameter constraints
================================================================================
private class F<T1,T2> where T1 : I1, I2, new() where T2 : I2 { }
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
(modifier)
name: (identifier)
type_parameters: (type_parameter_list
(type_parameter
name: (identifier))
(type_parameter
name: (identifier)))
(type_parameter_constraints_clause
target: (identifier)
constraints: (type_parameter_constraint
(type_constraint
type: (identifier)))
constraints: (type_parameter_constraint
(type_constraint
type: (identifier)))
constraints: (type_parameter_constraint
(constructor_constraint)))
(type_parameter_constraints_clause
target: (identifier)
constraints: (type_parameter_constraint
(type_constraint
type: (identifier))))
body: (declaration_list)))
================================================================================
Class with public constructor
================================================================================
class Foo {
public Foo() {}
}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
name: (identifier)
body: (declaration_list
(constructor_declaration
(modifier)
name: (identifier)
parameters: (parameter_list)
body: (block)))))
================================================================================
Class with expression bodied constructor
================================================================================
class Foo {
public Foo(string name) => Name = name;
}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
name: (identifier)
body: (declaration_list
(constructor_declaration
(modifier)
name: (identifier)
parameters: (parameter_list
(parameter
type: (predefined_type)
name: (identifier)))
body: (arrow_expression_clause
(assignment_expression
left: (identifier)
(assignment_operator)
right: (identifier)))))))
================================================================================
Class with static constructor
================================================================================
class Foo {
static Foo() {}
static extern Foo() {}
extern static Foo() {}
}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
name: (identifier)
body: (declaration_list
(constructor_declaration
(modifier)
name: (identifier)
parameters: (parameter_list)
body: (block))
(constructor_declaration
(modifier)
(modifier)
name: (identifier)
parameters: (parameter_list)
body: (block))
(constructor_declaration
(modifier)
(modifier)
name: (identifier)
parameters: (parameter_list)
body: (block)))))
================================================================================
Class with extern destructor
================================================================================
class Foo {
extern ~Foo() {}
}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
name: (identifier)
body: (declaration_list
(destructor_declaration
name: (identifier)
parameters: (parameter_list)
body: (block)))))
================================================================================
Class with expression bodied destructor
================================================================================
class Foo {
~Foo() => DoSomething();
}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
name: (identifier)
body: (declaration_list
(destructor_declaration
name: (identifier)
parameters: (parameter_list)
body: (arrow_expression_clause
(invocation_expression
function: (identifier)
arguments: (argument_list)))))))
================================================================================
Class with constants
================================================================================
class Foo {
private const int a = 1;
const string b = $"hello";
}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
name: (identifier)
body: (declaration_list
(field_declaration
(modifier)
(modifier)
(variable_declaration
type: (predefined_type)
(variable_declarator
name: (identifier)
(equals_value_clause
(integer_literal)))))
(field_declaration
(modifier)
(variable_declaration
type: (predefined_type)
(variable_declarator
name: (identifier)
(equals_value_clause
(interpolated_string_expression
(interpolated_string_text)))))))))
================================================================================
Class with indexer
================================================================================
class Foo {
public bool this[int index] {
get { return a; }
set { a = value; }
}
}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
name: (identifier)
body: (declaration_list
(indexer_declaration
(modifier)
type: (predefined_type)
parameters: (bracketed_parameter_list
(parameter
type: (predefined_type)
name: (identifier)))
accessors: (accessor_list
(accessor_declaration
body: (block
(return_statement
(identifier))))
(accessor_declaration
body: (block
(expression_statement
(assignment_expression
left: (identifier)
(assignment_operator)
right: (identifier))))))))))
================================================================================
Class with expression bodied indexer
================================================================================
class Foo {
public bool this[int index] => a[index];
}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
name: (identifier)
body: (declaration_list
(indexer_declaration
(modifier)
type: (predefined_type)
parameters: (bracketed_parameter_list
(parameter
type: (predefined_type)
name: (identifier)))
value: (arrow_expression_clause
(element_access_expression
expression: (identifier)
subscript: (bracketed_argument_list
(argument
(identifier)))))))))
================================================================================
Class with expression bodied indexer accessors
================================================================================
class Foo {
public string this[int index]
{
get => a[index];
set => a[index] = value;
}
}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
name: (identifier)
body: (declaration_list
(indexer_declaration
(modifier)
type: (predefined_type)
parameters: (bracketed_parameter_list
(parameter
type: (predefined_type)
name: (identifier)))
accessors: (accessor_list
(accessor_declaration
body: (arrow_expression_clause
(element_access_expression
expression: (identifier)
subscript: (bracketed_argument_list
(argument
(identifier))))))
(accessor_declaration
body: (arrow_expression_clause
(assignment_expression
left: (element_access_expression
expression: (identifier)
subscript: (bracketed_argument_list
(argument
(identifier))))
(assignment_operator)
right: (identifier)))))))))
================================================================================
Class with varargs indexer
================================================================================
class A {
public int this[params string[] arguments] {
get { return 1; }
}
}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
name: (identifier)
body: (declaration_list
(indexer_declaration
(modifier)
type: (predefined_type)
parameters: (bracketed_parameter_list
type: (array_type
type: (predefined_type)
rank: (array_rank_specifier))
name: (identifier))
accessors: (accessor_list
(accessor_declaration
body: (block
(return_statement
(integer_literal)))))))))
================================================================================
Method with qualified return type
================================================================================
class A {
B.C d() {
return null;
}
}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
name: (identifier)
body: (declaration_list
(method_declaration
type: (qualified_name
qualifier: (identifier)
name: (identifier))
name: (identifier)
parameters: (parameter_list)
body: (block
(return_statement
(null_literal)))))))
================================================================================
Class and methods with Unicode identifiers
================================================================================
class Ωµ {
B.C d() {
return null;
}
}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
name: (identifier)
body: (declaration_list
(method_declaration
type: (qualified_name
qualifier: (identifier)
name: (identifier))
name: (identifier)
parameters: (parameter_list)
body: (block
(return_statement
(null_literal)))))))
================================================================================
File scoped class
================================================================================
file class A {}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
(modifier)
name: (identifier)
body: (declaration_list)))

View File

@ -0,0 +1,614 @@
================================================================================
From keyword can be a variable
================================================================================
var a = Assert.Range(from, to);
--------------------------------------------------------------------------------
(compilation_unit
(global_statement
(local_declaration_statement
(variable_declaration
type: (implicit_type)
(variable_declarator
name: (identifier)
(equals_value_clause
(invocation_expression
function: (member_access_expression
expression: (identifier)
name: (identifier))
arguments: (argument_list
(argument
(identifier))
(argument
(identifier))))))))))
================================================================================
File keyword in invocation
================================================================================
file.Method(1, 2);
--------------------------------------------------------------------------------
(compilation_unit
(global_statement
(expression_statement
(invocation_expression
function: (member_access_expression
expression: (identifier)
name: (identifier))
arguments: (argument_list
(argument
(integer_literal))
(argument
(integer_literal)))))))
================================================================================
File contextual keyword
================================================================================
void file() { }
void m(file p) { }
void m(int file) { }
void m()
{
file v = null;
int file = file;
file();
m(file);
var x = file + 1;
}
file class file { }
--------------------------------------------------------------------------------
(compilation_unit
(global_statement
(local_function_statement
type: (predefined_type)
name: (identifier)
parameters: (parameter_list)
body: (block)))
(global_statement
(local_function_statement
type: (predefined_type)
name: (identifier)
parameters: (parameter_list
(parameter
type: (identifier)
name: (identifier)))
body: (block)))
(global_statement
(local_function_statement
type: (predefined_type)
name: (identifier)
parameters: (parameter_list
(parameter
type: (predefined_type)
name: (identifier)))
body: (block)))
(global_statement
(local_function_statement
type: (predefined_type)
name: (identifier)
parameters: (parameter_list)
body: (block
(local_declaration_statement
(variable_declaration
type: (identifier)
(variable_declarator
name: (identifier)
(equals_value_clause
(null_literal)))))
(local_declaration_statement
(variable_declaration
type: (predefined_type)
(variable_declarator
name: (identifier)
(equals_value_clause
(identifier)))))
(expression_statement
(invocation_expression
function: (identifier)
arguments: (argument_list)))
(expression_statement
(invocation_expression
function: (identifier)
arguments: (argument_list
(argument
(identifier)))))
(local_declaration_statement
(variable_declaration
type: (implicit_type)
(variable_declarator
name: (identifier)
(equals_value_clause
(binary_expression
left: (identifier)
right: (integer_literal)))))))))
(class_declaration
(modifier)
name: (identifier)
body: (declaration_list)))
================================================================================
Scoped contextual keyword
================================================================================
void scoped() { }
void m(scoped p) { }
void m(scoped ref int p) { }
void m(scoped ref scoped p) { }
void m(int scoped) { }
void m()
{
scoped v = null;
scoped ref int v = null;
scoped ref scoped v = null;
int scoped = null;
scoped();
m(scoped);
var x = scoped + 1;
var l = scoped => null;
var l = (scoped i) => null;
var l = (scoped, i) => null;
var l = scoped (int i, int j) => null;
}
class scoped { }
--------------------------------------------------------------------------------
(compilation_unit
(global_statement
(local_function_statement
type: (predefined_type)
name: (identifier)
parameters: (parameter_list)
body: (block)))
(global_statement
(local_function_statement
type: (predefined_type)
name: (identifier)
parameters: (parameter_list
(parameter
type: (identifier)
name: (identifier)))
body: (block)))
(global_statement
(local_function_statement
type: (predefined_type)
name: (identifier)
parameters: (parameter_list
(parameter
(parameter_modifier)
(parameter_modifier)
type: (predefined_type)
name: (identifier)))
body: (block)))
(global_statement
(local_function_statement
type: (predefined_type)
name: (identifier)
parameters: (parameter_list
(parameter
(parameter_modifier)
(parameter_modifier)
type: (identifier)
name: (identifier)))
body: (block)))
(global_statement
(local_function_statement
type: (predefined_type)
name: (identifier)
parameters: (parameter_list
(parameter
type: (predefined_type)
name: (identifier)))
body: (block)))
(global_statement
(local_function_statement
type: (predefined_type)
name: (identifier)
parameters: (parameter_list)
body: (block
(local_declaration_statement
(variable_declaration
type: (identifier)
(variable_declarator
name: (identifier)
(equals_value_clause
(null_literal)))))
(local_declaration_statement
(variable_declaration
type: (scoped_type
type: (ref_type
type: (predefined_type)))
(variable_declarator
name: (identifier)
(equals_value_clause
(null_literal)))))
(local_declaration_statement
(variable_declaration
type: (scoped_type
type: (ref_type
type: (identifier)))
(variable_declarator
name: (identifier)
(equals_value_clause
(null_literal)))))
(local_declaration_statement
(variable_declaration
type: (predefined_type)
(variable_declarator
name: (identifier)
(equals_value_clause
(null_literal)))))
(expression_statement
(invocation_expression
function: (identifier)
arguments: (argument_list)))
(expression_statement
(invocation_expression
function: (identifier)
arguments: (argument_list
(argument
(identifier)))))
(local_declaration_statement
(variable_declaration
type: (implicit_type)
(variable_declarator
name: (identifier)
(equals_value_clause
(binary_expression
left: (identifier)
right: (integer_literal))))))
(local_declaration_statement
(variable_declaration
type: (implicit_type)
(variable_declarator
name: (identifier)
(equals_value_clause
(lambda_expression
parameters: (implicit_parameter_list
(parameter
name: (identifier)))
body: (null_literal))))))
(local_declaration_statement
(variable_declaration
type: (implicit_type)
(variable_declarator
name: (identifier)
(equals_value_clause
(lambda_expression
parameters: (parameter_list
(parameter
type: (identifier)
name: (identifier)))
body: (null_literal))))))
(local_declaration_statement
(variable_declaration
type: (implicit_type)
(variable_declarator
name: (identifier)
(equals_value_clause
(lambda_expression
parameters: (parameter_list
(parameter
name: (identifier))
(parameter
name: (identifier)))
body: (null_literal))))))
(local_declaration_statement
(variable_declaration
type: (implicit_type)
(variable_declarator
name: (identifier)
(equals_value_clause
(lambda_expression
type: (identifier)
parameters: (parameter_list
(parameter
type: (predefined_type)
name: (identifier))
(parameter
type: (predefined_type)
name: (identifier)))
body: (null_literal)))))))))
(class_declaration
name: (identifier)
body: (declaration_list)))
================================================================================
Set contextual keyword
================================================================================
void set() { }
void m(set p) { }
void m(int set) { }
void m()
{
set v = null;
int set = set;
set();
m(set);
var x = set + 1;
}
class set { }
--------------------------------------------------------------------------------
(compilation_unit
(global_statement
(local_function_statement
type: (predefined_type)
name: (identifier)
parameters: (parameter_list)
body: (block)))
(global_statement
(local_function_statement
type: (predefined_type)
name: (identifier)
parameters: (parameter_list
(parameter
type: (identifier)
name: (identifier)))
body: (block)))
(global_statement
(local_function_statement
type: (predefined_type)
name: (identifier)
parameters: (parameter_list
(parameter
type: (predefined_type)
name: (identifier)))
body: (block)))
(global_statement
(local_function_statement
type: (predefined_type)
name: (identifier)
parameters: (parameter_list)
body: (block
(local_declaration_statement
(variable_declaration
type: (identifier)
(variable_declarator
name: (identifier)
(equals_value_clause
(null_literal)))))
(local_declaration_statement
(variable_declaration
type: (predefined_type)
(variable_declarator
name: (identifier)
(equals_value_clause
(identifier)))))
(expression_statement
(invocation_expression
function: (identifier)
arguments: (argument_list)))
(expression_statement
(invocation_expression
function: (identifier)
arguments: (argument_list
(argument
(identifier)))))
(local_declaration_statement
(variable_declaration
type: (implicit_type)
(variable_declarator
name: (identifier)
(equals_value_clause
(binary_expression
left: (identifier)
right: (integer_literal)))))))))
(class_declaration
name: (identifier)
body: (declaration_list)))
================================================================================
Var contextual keyword
================================================================================
void var() { }
void m(var p) { }
void m(int var) { }
void m()
{
var v = null;
int var = var;
var var = 1;
var();
m(var);
var x = var + 1;
}
class var { }
--------------------------------------------------------------------------------
(compilation_unit
(global_statement
(local_function_statement
type: (predefined_type)
name: (identifier)
parameters: (parameter_list)
body: (block)))
(global_statement
(local_function_statement
type: (predefined_type)
name: (identifier)
parameters: (parameter_list
(parameter
type: (implicit_type)
name: (identifier)))
body: (block)))
(global_statement
(local_function_statement
type: (predefined_type)
name: (identifier)
parameters: (parameter_list
(parameter
type: (predefined_type)
name: (identifier)))
body: (block)))
(global_statement
(local_function_statement
type: (predefined_type)
name: (identifier)
parameters: (parameter_list)
body: (block
(local_declaration_statement
(variable_declaration
type: (implicit_type)
(variable_declarator
name: (identifier)
(equals_value_clause
(null_literal)))))
(local_declaration_statement
(variable_declaration
type: (predefined_type)
(variable_declarator
name: (identifier)
(equals_value_clause
(identifier)))))
(local_declaration_statement
(variable_declaration
type: (implicit_type)
(variable_declarator
name: (identifier)
(equals_value_clause
(integer_literal)))))
(expression_statement
(invocation_expression
function: (identifier)
arguments: (argument_list)))
(expression_statement
(invocation_expression
function: (identifier)
arguments: (argument_list
(argument
(identifier)))))
(local_declaration_statement
(variable_declaration
type: (implicit_type)
(variable_declarator
name: (identifier)
(equals_value_clause
(binary_expression
left: (identifier)
right: (integer_literal)))))))))
(class_declaration
name: (identifier)
body: (declaration_list)))
================================================================================
Nameof contextual keyword
================================================================================
void nameof() { }
void m(nameof p) { }
void m(int nameof) { }
void m()
{
nameof v = null;
int nameof = nameof;
nameof();
nameof(a, b);
m(nameof);
var x = nameof + 1;
}
class nameof { }
--------------------------------------------------------------------------------
(compilation_unit
(global_statement
(local_function_statement
type: (predefined_type)
name: (identifier)
parameters: (parameter_list)
body: (block)))
(global_statement
(local_function_statement
type: (predefined_type)
name: (identifier)
parameters: (parameter_list
(parameter
type: (identifier)
name: (identifier)))
body: (block)))
(global_statement
(local_function_statement
type: (predefined_type)
name: (identifier)
parameters: (parameter_list
(parameter
type: (predefined_type)
name: (identifier)))
body: (block)))
(global_statement
(local_function_statement
type: (predefined_type)
name: (identifier)
parameters: (parameter_list)
body: (block
(local_declaration_statement
(variable_declaration
type: (identifier)
(variable_declarator
name: (identifier)
(equals_value_clause
(null_literal)))))
(local_declaration_statement
(variable_declaration
type: (predefined_type)
(variable_declarator
name: (identifier)
(equals_value_clause
(identifier)))))
(expression_statement
(invocation_expression
function: (identifier)
arguments: (argument_list)))
(expression_statement
(invocation_expression
function: (identifier)
arguments: (argument_list
(argument
(identifier))
(argument
(identifier)))))
(expression_statement
(invocation_expression
function: (identifier)
arguments: (argument_list
(argument
(identifier)))))
(local_declaration_statement
(variable_declaration
type: (implicit_type)
(variable_declarator
name: (identifier)
(equals_value_clause
(binary_expression
left: (identifier)
right: (integer_literal)))))))))
(class_declaration
name: (identifier)
body: (declaration_list)))

59
corpus/enums.txt Normal file
View File

@ -0,0 +1,59 @@
================================================================================
global enum with one option
================================================================================
enum A { One }
--------------------------------------------------------------------------------
(compilation_unit
(enum_declaration
name: (identifier)
body: (enum_member_declaration_list
(enum_member_declaration
name: (identifier)))))
================================================================================
enum with integer values
================================================================================
enum B { Ten = 10, Twenty = 20 }
--------------------------------------------------------------------------------
(compilation_unit
(enum_declaration
name: (identifier)
body: (enum_member_declaration_list
(enum_member_declaration
name: (identifier)
value: (integer_literal))
(enum_member_declaration
name: (identifier)
value: (integer_literal)))))
================================================================================
enum with byte base
================================================================================
namespace A {
enum B : byte { Five = 0x05, Fifteen = 0x0F }
}
--------------------------------------------------------------------------------
(compilation_unit
(namespace_declaration
name: (identifier)
body: (declaration_list
(enum_declaration
name: (identifier)
bases: (base_list
(predefined_type))
body: (enum_member_declaration_list
(enum_member_declaration
name: (identifier)
value: (integer_literal))
(enum_member_declaration
name: (identifier)
value: (integer_literal)))))))

3457
corpus/expressions.txt Normal file

File diff suppressed because it is too large Load Diff

134
corpus/identifiers.txt Normal file
View File

@ -0,0 +1,134 @@
================================================================================
basic indentifiers
================================================================================
int x = y;
--------------------------------------------------------------------------------
(compilation_unit
(global_statement
(local_declaration_statement
(variable_declaration
type: (predefined_type)
(variable_declarator
name: (identifier)
(equals_value_clause
(identifier)))))))
================================================================================
indentifiers with keyword names
================================================================================
int @var = @const;
--------------------------------------------------------------------------------
(compilation_unit
(global_statement
(local_declaration_statement
(variable_declaration
type: (predefined_type)
(variable_declarator
name: (identifier)
(equals_value_clause
(identifier)))))))
================================================================================
identifiers with contextual keyword names
================================================================================
int nint = 0;
int nuint = 0;
--------------------------------------------------------------------------------
(compilation_unit
(global_statement
(local_declaration_statement
(variable_declaration
type: (predefined_type)
(variable_declarator
name: (identifier)
(equals_value_clause
(integer_literal))))))
(global_statement
(local_declaration_statement
(variable_declaration
type: (predefined_type)
(variable_declarator
name: (identifier)
(equals_value_clause
(integer_literal)))))))
================================================================================
unicode identifiers
================================================================================
var under_score = 0;
var with1number = 0;
var varæble = 0;
var Переменная = 0;
var first‿letter = 0;
var ග්‍රහලෝකය = 0;
var _كوكبxxx = 0;
--------------------------------------------------------------------------------
(compilation_unit
(global_statement
(local_declaration_statement
(variable_declaration
type: (implicit_type)
(variable_declarator
name: (identifier)
(equals_value_clause
(integer_literal))))))
(global_statement
(local_declaration_statement
(variable_declaration
type: (implicit_type)
(variable_declarator
name: (identifier)
(equals_value_clause
(integer_literal))))))
(global_statement
(local_declaration_statement
(variable_declaration
type: (implicit_type)
(variable_declarator
name: (identifier)
(equals_value_clause
(integer_literal))))))
(global_statement
(local_declaration_statement
(variable_declaration
type: (implicit_type)
(variable_declarator
name: (identifier)
(equals_value_clause
(integer_literal))))))
(global_statement
(local_declaration_statement
(variable_declaration
type: (implicit_type)
(variable_declarator
name: (identifier)
(equals_value_clause
(integer_literal))))))
(global_statement
(local_declaration_statement
(variable_declaration
type: (implicit_type)
(variable_declarator
name: (identifier)
(equals_value_clause
(integer_literal))))))
(global_statement
(local_declaration_statement
(variable_declaration
type: (implicit_type)
(variable_declarator
name: (identifier)
(equals_value_clause
(integer_literal)))))))

335
corpus/interfaces.txt Normal file
View File

@ -0,0 +1,335 @@
================================================================================
Global empty interface
================================================================================
public interface IOne {};
--------------------------------------------------------------------------------
(compilation_unit
(interface_declaration
(modifier)
name: (identifier)
body: (declaration_list)))
================================================================================
Interface with properties
================================================================================
interface IOne {
byte Get { get; }
char Set { set; }
uint GetSet { get; set; }
long SetGet { set; get; }
};
--------------------------------------------------------------------------------
(compilation_unit
(interface_declaration
name: (identifier)
body: (declaration_list
(property_declaration
type: (predefined_type)
name: (identifier)
accessors: (accessor_list
(accessor_declaration)))
(property_declaration
type: (predefined_type)
name: (identifier)
accessors: (accessor_list
(accessor_declaration)))
(property_declaration
type: (predefined_type)
name: (identifier)
accessors: (accessor_list
(accessor_declaration)
(accessor_declaration)))
(property_declaration
type: (predefined_type)
name: (identifier)
accessors: (accessor_list
(accessor_declaration)
(accessor_declaration))))))
================================================================================
Interface with methods
================================================================================
interface IOne {
void Nothing();
int Output();
void Input(string a);
int InputOutput(string a);
};
--------------------------------------------------------------------------------
(compilation_unit
(interface_declaration
name: (identifier)
body: (declaration_list
(method_declaration
type: (predefined_type)
name: (identifier)
parameters: (parameter_list))
(method_declaration
type: (predefined_type)
name: (identifier)
parameters: (parameter_list))
(method_declaration
type: (predefined_type)
name: (identifier)
parameters: (parameter_list
(parameter
type: (predefined_type)
name: (identifier))))
(method_declaration
type: (predefined_type)
name: (identifier)
parameters: (parameter_list
(parameter
type: (predefined_type)
name: (identifier)))))))
================================================================================
Interface base single
================================================================================
private interface IOne : ITwo { }
--------------------------------------------------------------------------------
(compilation_unit
(interface_declaration
(modifier)
name: (identifier)
bases: (base_list
(identifier))
body: (declaration_list)))
================================================================================
Interface base multiple
================================================================================
private interface IOne : ITwo, IThree { }
--------------------------------------------------------------------------------
(compilation_unit
(interface_declaration
(modifier)
name: (identifier)
bases: (base_list
(identifier)
(identifier))
body: (declaration_list)))
================================================================================
Interface generic
================================================================================
private interface IOne<T1> : ITwo { }
--------------------------------------------------------------------------------
(compilation_unit
(interface_declaration
(modifier)
name: (identifier)
type_parameters: (type_parameter_list
(type_parameter
name: (identifier)))
bases: (base_list
(identifier))
body: (declaration_list)))
================================================================================
Interface generic single constraint
================================================================================
private interface IOne<T1> : ITwo where T1:T2 { }
--------------------------------------------------------------------------------
(compilation_unit
(interface_declaration
(modifier)
name: (identifier)
type_parameters: (type_parameter_list
(type_parameter
name: (identifier)))
bases: (base_list
(identifier))
(type_parameter_constraints_clause
target: (identifier)
constraints: (type_parameter_constraint
(type_constraint
type: (identifier))))
body: (declaration_list)))
================================================================================
Interface generic multiple constraints
================================================================================
private interface IOne<T1, T3> : ITwo where T1:T2 where T3:new() { }
--------------------------------------------------------------------------------
(compilation_unit
(interface_declaration
(modifier)
name: (identifier)
type_parameters: (type_parameter_list
(type_parameter
name: (identifier))
(type_parameter
name: (identifier)))
bases: (base_list
(identifier))
(type_parameter_constraints_clause
target: (identifier)
constraints: (type_parameter_constraint
(type_constraint
type: (identifier))))
(type_parameter_constraints_clause
target: (identifier)
constraints: (type_parameter_constraint
(constructor_constraint)))
body: (declaration_list)))
================================================================================
Interface in namespace
================================================================================
namespace A {
interface IOne : ITwo { }
}
--------------------------------------------------------------------------------
(compilation_unit
(namespace_declaration
name: (identifier)
body: (declaration_list
(interface_declaration
name: (identifier)
bases: (base_list
(identifier))
body: (declaration_list)))))
================================================================================
Interface event declarations
================================================================================
interface A {
event EventHandler<T> SomeEvent;
}
--------------------------------------------------------------------------------
(compilation_unit
(interface_declaration
name: (identifier)
body: (declaration_list
(event_field_declaration
(variable_declaration
type: (generic_name
name: (identifier)
type_arguments: (type_argument_list
(identifier)))
(variable_declarator
name: (identifier)))))))
================================================================================
Interface with indexer
================================================================================
interface A {
bool this[int index] { get; set; }
}
--------------------------------------------------------------------------------
(compilation_unit
(interface_declaration
name: (identifier)
body: (declaration_list
(indexer_declaration
type: (predefined_type)
parameters: (bracketed_parameter_list
(parameter
type: (predefined_type)
name: (identifier)))
accessors: (accessor_list
(accessor_declaration)
(accessor_declaration))))))
================================================================================
Interface with default method
================================================================================
interface MyDefault {
void Log(string message) {
Console.WriteLine(message);
}
}
--------------------------------------------------------------------------------
(compilation_unit
(interface_declaration
name: (identifier)
body: (declaration_list
(method_declaration
type: (predefined_type)
name: (identifier)
parameters: (parameter_list
(parameter
type: (predefined_type)
name: (identifier)))
body: (block
(expression_statement
(invocation_expression
function: (member_access_expression
expression: (identifier)
name: (identifier))
arguments: (argument_list
(argument
(identifier))))))))))
================================================================================
Static abstract members
================================================================================
public interface IGetNext<T> where T : IGetNext<T>
{
static abstract T operator ++(T other);
}
--------------------------------------------------------------------------------
(compilation_unit
(interface_declaration
(modifier)
name: (identifier)
type_parameters: (type_parameter_list
(type_parameter
name: (identifier)))
(type_parameter_constraints_clause
target: (identifier)
constraints: (type_parameter_constraint
(type_constraint
type: (generic_name
name: (identifier)
type_arguments: (type_argument_list
(identifier))))))
body: (declaration_list
(operator_declaration
(modifier)
(modifier)
type: (identifier)
parameters: (parameter_list
(parameter
type: (identifier)
name: (identifier)))))))

1263
corpus/literals.txt Normal file

File diff suppressed because it is too large Load Diff

307
corpus/preprocessor.txt Normal file
View File

@ -0,0 +1,307 @@
================================================================================
If, elif and else directives
================================================================================
#if WIN32
string os = "Win32";
#elif MACOS
string os = "MacOS";
#else
string os = "Unknown";
#endif
--------------------------------------------------------------------------------
(compilation_unit
(if_directive
(identifier))
(global_statement
(local_declaration_statement
(variable_declaration
type: (predefined_type)
(variable_declarator
name: (identifier)
(equals_value_clause
(string_literal
(string_literal_fragment)))))))
(elif_directive
(identifier))
(global_statement
(local_declaration_statement
(variable_declaration
type: (predefined_type)
(variable_declarator
name: (identifier)
(equals_value_clause
(string_literal
(string_literal_fragment)))))))
(else_directive)
(global_statement
(local_declaration_statement
(variable_declaration
type: (predefined_type)
(variable_declarator
name: (identifier)
(equals_value_clause
(string_literal
(string_literal_fragment)))))))
(endif_directive))
================================================================================
Complex if conditions
================================================================================
#if !MACOS
#if WIN32==true
#if !MACOS!=false
#if A && B || C
#if (A)
#if (A || B)
#if (A && B) || C
--------------------------------------------------------------------------------
(compilation_unit
(if_directive
(prefix_unary_expression
argument: (identifier)))
(if_directive
(binary_expression
left: (identifier)
right: (boolean_literal)))
(if_directive
(binary_expression
left: (prefix_unary_expression
argument: (identifier))
right: (boolean_literal)))
(if_directive
(binary_expression
left: (binary_expression
left: (identifier)
right: (identifier))
right: (identifier)))
(if_directive
(parenthesized_expression
(identifier)))
(if_directive
(parenthesized_expression
(binary_expression
left: (identifier)
right: (identifier))))
(if_directive
(binary_expression
left: (parenthesized_expression
(binary_expression
left: (identifier)
right: (identifier)))
right: (identifier))))
================================================================================
Region directives
================================================================================
#region Here, there, everywhere
// something fast
#endregion
--------------------------------------------------------------------------------
(compilation_unit
(region_directive
(preproc_message))
(comment)
(endregion_directive))
================================================================================
Define and undefine directives
================================================================================
#define SOMETHING
#undef BAD
--------------------------------------------------------------------------------
(compilation_unit
(define_directive
(identifier))
(undef_directive
(identifier)))
================================================================================
Warning and error directives
================================================================================
class Of1879 {
#warning This class is bad.
#error Okay, just stop.
}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
name: (identifier)
body: (declaration_list
(warning_directive
(preproc_message))
(error_directive
(preproc_message)))))
================================================================================
Line directives
================================================================================
class Of1879 {
void AMethod() {
#line 2001 "A Space" // Comment
#line hidden
#line default
#line (1, 1) - (1, 3) 1 "a.cs"
#line (2, 1) - (2, 3) "a.cs"
}
}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
name: (identifier)
body: (declaration_list
(method_declaration
type: (predefined_type)
name: (identifier)
parameters: (parameter_list)
body: (block
(line_directive
(preproc_integer_literal)
(preproc_string_literal))
(comment)
(line_directive)
(line_directive)
(line_directive
(preproc_integer_literal)
(preproc_integer_literal)
(preproc_integer_literal)
(preproc_integer_literal)
(preproc_integer_literal)
(preproc_string_literal))
(line_directive
(preproc_integer_literal)
(preproc_integer_literal)
(preproc_integer_literal)
(preproc_integer_literal)
(preproc_string_literal)))))))
================================================================================
Spaces in directives
================================================================================
class Of1879 {
void AMethod() {
# line 2001 "A Space"
# line hidden
# line default
}
}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
name: (identifier)
body: (declaration_list
(method_declaration
type: (predefined_type)
name: (identifier)
parameters: (parameter_list)
body: (block
(line_directive
(preproc_integer_literal)
(preproc_string_literal))
(line_directive)
(line_directive))))))
================================================================================
Pragmas
================================================================================
#pragma warning disable 660,661,nullable
--------------------------------------------------------------------------------
(compilation_unit
(pragma_directive
(integer_literal)
(integer_literal)
(identifier)))
================================================================================
Directives not in strings or comments
================================================================================
class Of1879 {
void AMethod() {
var s = @"Only a string
#if NOPE
";
/* Only a comment
#if NOPE
*/
}
}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
name: (identifier)
body: (declaration_list
(method_declaration
type: (predefined_type)
name: (identifier)
parameters: (parameter_list)
body: (block
(local_declaration_statement
(variable_declaration
type: (implicit_type)
(variable_declarator
name: (identifier)
(equals_value_clause
(verbatim_string_literal)))))
(comment))))))
================================================================================
Reference (r) directive
================================================================================
#r "Microsoft.WindowsAzure.Storage"
--------------------------------------------------------------------------------
(compilation_unit
(reference_directive
(preproc_string_literal)))
================================================================================
Load directive
================================================================================
#load "mylogger.csx"
--------------------------------------------------------------------------------
(compilation_unit
(load_directive
(preproc_string_literal)))
================================================================================
Shebang directive
================================================================================
#!/usr/bin/env scriptcs
--------------------------------------------------------------------------------
(compilation_unit
(shebang_directive))

438
corpus/query-syntax.txt Normal file
View File

@ -0,0 +1,438 @@
================================================================================
Query from select
================================================================================
var x = from a in source select a.B;
--------------------------------------------------------------------------------
(compilation_unit
(global_statement
(local_declaration_statement
(variable_declaration
type: (implicit_type)
(variable_declarator
name: (identifier)
(equals_value_clause
(query_expression
(from_clause
name: (identifier)
(identifier))
(select_clause
(member_access_expression
expression: (identifier)
name: (identifier))))))))))
================================================================================
Query from select with operator
================================================================================
var x = from a in source select a * 2;
--------------------------------------------------------------------------------
(compilation_unit
(global_statement
(local_declaration_statement
(variable_declaration
type: (implicit_type)
(variable_declarator
name: (identifier)
(equals_value_clause
(query_expression
(from_clause
name: (identifier)
(identifier))
(select_clause
(binary_expression
left: (identifier)
right: (integer_literal))))))))))
================================================================================
Query from select with method call
================================================================================
var x = from a in source select a.B();
--------------------------------------------------------------------------------
(compilation_unit
(global_statement
(local_declaration_statement
(variable_declaration
type: (implicit_type)
(variable_declarator
name: (identifier)
(equals_value_clause
(query_expression
(from_clause
name: (identifier)
(identifier))
(select_clause
(invocation_expression
function: (member_access_expression
expression: (identifier)
name: (identifier))
arguments: (argument_list))))))))))
================================================================================
Query from select with conditional operator
================================================================================
var x = from a in source select a ? 0 : 1;
--------------------------------------------------------------------------------
(compilation_unit
(global_statement
(local_declaration_statement
(variable_declaration
type: (implicit_type)
(variable_declarator
name: (identifier)
(equals_value_clause
(query_expression
(from_clause
name: (identifier)
(identifier))
(select_clause
(conditional_expression
condition: (identifier)
consequence: (integer_literal)
alternative: (integer_literal))))))))))
================================================================================
Query from select with assignment
================================================================================
var x = from a in source select somevar = a;
--------------------------------------------------------------------------------
(compilation_unit
(global_statement
(local_declaration_statement
(variable_declaration
type: (implicit_type)
(variable_declarator
name: (identifier)
(equals_value_clause
(query_expression
(from_clause
name: (identifier)
(identifier))
(select_clause
(assignment_expression
left: (identifier)
(assignment_operator)
right: (identifier))))))))))
================================================================================
Query from select projection
================================================================================
var x = from a in source select new { Name = a.B };
--------------------------------------------------------------------------------
(compilation_unit
(global_statement
(local_declaration_statement
(variable_declaration
type: (implicit_type)
(variable_declarator
name: (identifier)
(equals_value_clause
(query_expression
(from_clause
name: (identifier)
(identifier))
(select_clause
(anonymous_object_creation_expression
(name_equals
(identifier))
(member_access_expression
expression: (identifier)
name: (identifier)))))))))))
================================================================================
Query from select with where
================================================================================
var x = from a in source
where a.B == "A"
select a;
--------------------------------------------------------------------------------
(compilation_unit
(global_statement
(local_declaration_statement
(variable_declaration
type: (implicit_type)
(variable_declarator
name: (identifier)
(equals_value_clause
(query_expression
(from_clause
name: (identifier)
(identifier))
(where_clause
(binary_expression
left: (member_access_expression
expression: (identifier)
name: (identifier))
right: (string_literal
(string_literal_fragment))))
(select_clause
(identifier)))))))))
================================================================================
Query from select with where and projection
================================================================================
var x = from a in source
where a.B == "A" && a.C == "D"
select new { Name = a.B };
--------------------------------------------------------------------------------
(compilation_unit
(global_statement
(local_declaration_statement
(variable_declaration
type: (implicit_type)
(variable_declarator
name: (identifier)
(equals_value_clause
(query_expression
(from_clause
name: (identifier)
(identifier))
(where_clause
(binary_expression
left: (binary_expression
left: (member_access_expression
expression: (identifier)
name: (identifier))
right: (string_literal
(string_literal_fragment)))
right: (binary_expression
left: (member_access_expression
expression: (identifier)
name: (identifier))
right: (string_literal
(string_literal_fragment)))))
(select_clause
(anonymous_object_creation_expression
(name_equals
(identifier))
(member_access_expression
expression: (identifier)
name: (identifier)))))))))))
================================================================================
Query from select with orderby
================================================================================
var x = from a in source
orderby a.A descending
orderby a.C ascending
orderby 1
select a;
--------------------------------------------------------------------------------
(compilation_unit
(global_statement
(local_declaration_statement
(variable_declaration
type: (implicit_type)
(variable_declarator
name: (identifier)
(equals_value_clause
(query_expression
(from_clause
name: (identifier)
(identifier))
(order_by_clause
(member_access_expression
expression: (identifier)
name: (identifier)))
(order_by_clause
(member_access_expression
expression: (identifier)
name: (identifier)))
(order_by_clause
(integer_literal))
(select_clause
(identifier)))))))))
================================================================================
Query from select with let
================================================================================
var x = from a in source
let z = new { a.A, a.B }
select z;
--------------------------------------------------------------------------------
(compilation_unit
(global_statement
(local_declaration_statement
(variable_declaration
type: (implicit_type)
(variable_declarator
name: (identifier)
(equals_value_clause
(query_expression
(from_clause
name: (identifier)
(identifier))
(let_clause
(identifier)
(anonymous_object_creation_expression
(member_access_expression
expression: (identifier)
name: (identifier))
(member_access_expression
expression: (identifier)
name: (identifier))))
(select_clause
(identifier)))))))))
================================================================================
Query from select with join
================================================================================
var x = from a in sourceA
join b in sourceB on a.FK equals b.PK
select new { A.A, B.B };
--------------------------------------------------------------------------------
(compilation_unit
(global_statement
(local_declaration_statement
(variable_declaration
type: (implicit_type)
(variable_declarator
name: (identifier)
(equals_value_clause
(query_expression
(from_clause
name: (identifier)
(identifier))
(join_clause
name: (identifier)
(identifier)
(member_access_expression
expression: (identifier)
name: (identifier))
(member_access_expression
expression: (identifier)
name: (identifier)))
(select_clause
(anonymous_object_creation_expression
(member_access_expression
expression: (identifier)
name: (identifier))
(member_access_expression
expression: (identifier)
name: (identifier)))))))))))
================================================================================
Query from select with multiple from
================================================================================
var x = from a in sourceA
from b in sourceB
where a.FK == b.FK
select new { A.A, B.B };
--------------------------------------------------------------------------------
(compilation_unit
(global_statement
(local_declaration_statement
(variable_declaration
type: (implicit_type)
(variable_declarator
name: (identifier)
(equals_value_clause
(query_expression
(from_clause
name: (identifier)
(identifier))
(from_clause
name: (identifier)
(identifier))
(where_clause
(binary_expression
left: (member_access_expression
expression: (identifier)
name: (identifier))
right: (member_access_expression
expression: (identifier)
name: (identifier))))
(select_clause
(anonymous_object_creation_expression
(member_access_expression
expression: (identifier)
name: (identifier))
(member_access_expression
expression: (identifier)
name: (identifier)))))))))))
================================================================================
Query from select with group by & continuation
================================================================================
var x = from a in sourceA
group a by a.Country into g
select new { Country = g.Key, Population = g.Sum(p => p.Population) };
--------------------------------------------------------------------------------
(compilation_unit
(global_statement
(local_declaration_statement
(variable_declaration
type: (implicit_type)
(variable_declarator
name: (identifier)
(equals_value_clause
(query_expression
(from_clause
name: (identifier)
(identifier))
(group_clause
(identifier)
(member_access_expression
expression: (identifier)
name: (identifier)))
(query_continuation
name: (identifier)
(select_clause
(anonymous_object_creation_expression
(name_equals
(identifier))
(member_access_expression
expression: (identifier)
name: (identifier))
(name_equals
(identifier))
(invocation_expression
function: (member_access_expression
expression: (identifier)
name: (identifier))
arguments: (argument_list
(argument
(lambda_expression
parameters: (implicit_parameter_list
(parameter
name: (identifier)))
body: (member_access_expression
expression: (identifier)
name: (identifier))))))))))))))))

527
corpus/records.txt Normal file
View File

@ -0,0 +1,527 @@
================================================================================
Basic record declaration
================================================================================
record F {
int Age { get; init; }
}
--------------------------------------------------------------------------------
(compilation_unit
(record_declaration
name: (identifier)
body: (declaration_list
(property_declaration
type: (predefined_type)
name: (identifier)
accessors: (accessor_list
(accessor_declaration)
(accessor_declaration))))))
================================================================================
Basic record struct declaration
================================================================================
record struct F {
int Age { get; init; }
}
--------------------------------------------------------------------------------
(compilation_unit
(record_struct_declaration
name: (identifier)
body: (declaration_list
(property_declaration
type: (predefined_type)
name: (identifier)
accessors: (accessor_list
(accessor_declaration)
(accessor_declaration))))))
================================================================================
Record class with optional `class` specification
================================================================================
record class F {
int Age { get; init; }
}
--------------------------------------------------------------------------------
(compilation_unit
(record_declaration
name: (identifier)
body: (declaration_list
(property_declaration
type: (predefined_type)
name: (identifier)
accessors: (accessor_list
(accessor_declaration)
(accessor_declaration))))))
================================================================================
Record with a type parameter struct constraint
================================================================================
public record F<T> where T:struct {}
--------------------------------------------------------------------------------
(compilation_unit
(record_declaration
(modifier)
name: (identifier)
type_parameters: (type_parameter_list
(type_parameter
name: (identifier)))
(type_parameter_constraints_clause
target: (identifier)
constraints: (type_parameter_constraint))
body: (declaration_list)))
================================================================================
Record with a type parameter class constraint
================================================================================
public record F<T> where T:class {}
--------------------------------------------------------------------------------
(compilation_unit
(record_declaration
(modifier)
name: (identifier)
type_parameters: (type_parameter_list
(type_parameter
name: (identifier)))
(type_parameter_constraints_clause
target: (identifier)
constraints: (type_parameter_constraint))
body: (declaration_list)))
================================================================================
Record with type parameter new constraint
================================================================================
public record F<T> where T: new() {}
--------------------------------------------------------------------------------
(compilation_unit
(record_declaration
(modifier)
name: (identifier)
type_parameters: (type_parameter_list
(type_parameter
name: (identifier)))
(type_parameter_constraints_clause
target: (identifier)
constraints: (type_parameter_constraint
(constructor_constraint)))
body: (declaration_list)))
================================================================================
Record with interface
================================================================================
public record A : ISomething { }
--------------------------------------------------------------------------------
(compilation_unit
(record_declaration
(modifier)
name: (identifier)
bases: (base_list
(identifier))
body: (declaration_list)))
================================================================================
Record with multiple type parameter constraints
================================================================================
[Nice]
private record F<T1,T2> where T1 : I1, I2, new() where T2 : I2 { }
--------------------------------------------------------------------------------
(compilation_unit
(record_declaration
(attribute_list
(attribute
name: (identifier)))
(modifier)
name: (identifier)
type_parameters: (type_parameter_list
(type_parameter
name: (identifier))
(type_parameter
name: (identifier)))
(type_parameter_constraints_clause
target: (identifier)
constraints: (type_parameter_constraint
(type_constraint
type: (identifier)))
constraints: (type_parameter_constraint
(type_constraint
type: (identifier)))
constraints: (type_parameter_constraint
(constructor_constraint)))
(type_parameter_constraints_clause
target: (identifier)
constraints: (type_parameter_constraint
(type_constraint
type: (identifier))))
body: (declaration_list)))
================================================================================
Record with constructor
================================================================================
record Person(string FirstName, string LastName);
--------------------------------------------------------------------------------
(compilation_unit
(record_declaration
name: (identifier)
parameters: (parameter_list
(parameter
type: (predefined_type)
name: (identifier))
(parameter
type: (predefined_type)
name: (identifier)))))
================================================================================
Record inheritance with constructor overload
================================================================================
record Teacher(string FirstName, string LastName, string Subject) : Person(FirstName, LastName);
--------------------------------------------------------------------------------
(compilation_unit
(record_declaration
name: (identifier)
parameters: (parameter_list
(parameter
type: (predefined_type)
name: (identifier))
(parameter
type: (predefined_type)
name: (identifier))
(parameter
type: (predefined_type)
name: (identifier)))
bases: (base_list
(primary_constructor_base_type
type: (identifier)
(argument_list
(argument
(identifier))
(argument
(identifier)))))))
================================================================================
Record inheritance with constructor overload and interfaces
================================================================================
record Teacher(string FirstName, string LastName, string Subject) : Person(FirstName, LastName), Ns.I1, I2;
--------------------------------------------------------------------------------
(compilation_unit
(record_declaration
name: (identifier)
parameters: (parameter_list
(parameter
type: (predefined_type)
name: (identifier))
(parameter
type: (predefined_type)
name: (identifier))
(parameter
type: (predefined_type)
name: (identifier)))
bases: (base_list
(primary_constructor_base_type
type: (identifier)
(argument_list
(argument
(identifier))
(argument
(identifier))))
(qualified_name
qualifier: (identifier)
name: (identifier))
(identifier))))
================================================================================
Record inheritance with generic base
================================================================================
record Teacher() : Entity<Person>(), I1;
record A : System.IEquatable<A>;
--------------------------------------------------------------------------------
(compilation_unit
(record_declaration
name: (identifier)
parameters: (parameter_list)
bases: (base_list
(primary_constructor_base_type
type: (generic_name
name: (identifier)
type_arguments: (type_argument_list
(identifier)))
(argument_list))
(identifier)))
(record_declaration
name: (identifier)
bases: (base_list
(qualified_name
qualifier: (identifier)
name: (generic_name
name: (identifier)
type_arguments: (type_argument_list
(identifier)))))))
================================================================================
Record types can end with a semicolon
================================================================================
public record Person { };
public record struct Person2 { };
--------------------------------------------------------------------------------
(compilation_unit
(record_declaration
(modifier)
name: (identifier)
body: (declaration_list))
(record_struct_declaration
(modifier)
name: (identifier)
body: (declaration_list)))
================================================================================
Record types can seal ToString()
================================================================================
record A {
public sealed override string ToString(){
return "";
}
}
--------------------------------------------------------------------------------
(compilation_unit
(record_declaration
name: (identifier)
body: (declaration_list
(method_declaration
(modifier)
(modifier)
(modifier)
type: (predefined_type)
name: (identifier)
parameters: (parameter_list)
body: (block
(return_statement
(string_literal)))))))
================================================================================
With expression typical basic form
================================================================================
void A() {
var newFriend = friend with { LastName = "Edwards" };
}
--------------------------------------------------------------------------------
(compilation_unit
(global_statement
(local_function_statement
type: (predefined_type)
name: (identifier)
parameters: (parameter_list)
body: (block
(local_declaration_statement
(variable_declaration
type: (implicit_type)
(variable_declarator
name: (identifier)
(equals_value_clause
(with_expression
(identifier)
(with_initializer_expression
(simple_assignment_expression
(identifier)
(string_literal
(string_literal_fragment)))))))))))))
================================================================================
With expression using expressions
================================================================================
void A() {
var friend = GetAFriend() with {
ForeName = RandomFirstName(),
LastName = RandomLastName()
};
}
--------------------------------------------------------------------------------
(compilation_unit
(global_statement
(local_function_statement
type: (predefined_type)
name: (identifier)
parameters: (parameter_list)
body: (block
(local_declaration_statement
(variable_declaration
type: (implicit_type)
(variable_declarator
name: (identifier)
(equals_value_clause
(with_expression
(invocation_expression
function: (identifier)
arguments: (argument_list))
(with_initializer_expression
(simple_assignment_expression
(identifier)
(invocation_expression
function: (identifier)
arguments: (argument_list)))
(simple_assignment_expression
(identifier)
(invocation_expression
function: (identifier)
arguments: (argument_list)))))))))))))
================================================================================
Precedence between with and cast
================================================================================
var x = (Point) p1 with {X = 3};
--------------------------------------------------------------------------------
(compilation_unit
(global_statement
(local_declaration_statement
(variable_declaration
type: (implicit_type)
(variable_declarator
name: (identifier)
(equals_value_clause
(with_expression
(cast_expression
type: (identifier)
value: (identifier))
(with_initializer_expression
(simple_assignment_expression
(identifier)
(integer_literal))))))))))
================================================================================
Precedence between with and switch
================================================================================
var x = p1 with {X = 3} switch { _ => 3 };
--------------------------------------------------------------------------------
(compilation_unit
(global_statement
(local_declaration_statement
(variable_declaration
type: (implicit_type)
(variable_declarator
name: (identifier)
(equals_value_clause
(switch_expression
(with_expression
(identifier)
(with_initializer_expression
(simple_assignment_expression
(identifier)
(integer_literal))))
(switch_expression_arm
(discard)
(integer_literal)))))))))
================================================================================
Precedence between with and equals
================================================================================
var x = p1 with {X = 3} == p1 with {X = 4};
--------------------------------------------------------------------------------
(compilation_unit
(global_statement
(local_declaration_statement
(variable_declaration
type: (implicit_type)
(variable_declarator
name: (identifier)
(equals_value_clause
(binary_expression
left: (with_expression
(identifier)
(with_initializer_expression
(simple_assignment_expression
(identifier)
(integer_literal))))
right: (with_expression
(identifier)
(with_initializer_expression
(simple_assignment_expression
(identifier)
(integer_literal)))))))))))
================================================================================
Associativity of with expression
================================================================================
var x = p1 with {X = 3} with {X = 4} with {X = 5};
--------------------------------------------------------------------------------
(compilation_unit
(global_statement
(local_declaration_statement
(variable_declaration
type: (implicit_type)
(variable_declarator
name: (identifier)
(equals_value_clause
(with_expression
(with_expression
(with_expression
(identifier)
(with_initializer_expression
(simple_assignment_expression
(identifier)
(integer_literal))))
(with_initializer_expression
(simple_assignment_expression
(identifier)
(integer_literal))))
(with_initializer_expression
(simple_assignment_expression
(identifier)
(integer_literal))))))))))

View File

@ -0,0 +1,273 @@
================================================================================
Using directives
================================================================================
using A;
using B.C;
using global::E.F;
using G = H.I;
using static J.K;
--------------------------------------------------------------------------------
(compilation_unit
(using_directive
(identifier))
(using_directive
(qualified_name
(identifier)
(identifier)))
(using_directive
(qualified_name
(alias_qualified_name
(identifier)
(identifier))
(identifier)))
(using_directive
(name_equals
(identifier))
(qualified_name
(identifier)
(identifier)))
(using_directive
(qualified_name
(identifier)
(identifier))))
================================================================================
Nested using directives
================================================================================
namespace Foo {
using A;
}
--------------------------------------------------------------------------------
(compilation_unit
(namespace_declaration
name: (identifier)
body: (declaration_list
(using_directive
name: (identifier)))))
================================================================================
Global using directives
================================================================================
global using A;
global using static A.B;
--------------------------------------------------------------------------------
(compilation_unit
(using_directive
(identifier))
(using_directive
(qualified_name
(identifier)
(identifier))))
================================================================================
Comments
================================================================================
// I'm a single-line comment
/*
* I'm a block comment: a * b / c
*/
--------------------------------------------------------------------------------
(compilation_unit
(comment)
(comment))
================================================================================
Comment with double asterisk
================================================================================
/** test **/
--------------------------------------------------------------------------------
(compilation_unit
(comment))
================================================================================
Namespaces
================================================================================
namespace A {
namespace B.C.D {
}
namespace E.F {
}
}
--------------------------------------------------------------------------------
(compilation_unit
(namespace_declaration
name: (identifier)
body: (declaration_list
(namespace_declaration
name: (qualified_name
qualifier: (qualified_name
qualifier: (identifier)
name: (identifier))
name: (identifier))
body: (declaration_list))
(namespace_declaration
name: (qualified_name
qualifier: (identifier)
name: (identifier))
body: (declaration_list)))))
================================================================================
File scoped namespaces
================================================================================
namespace A;
class B {
}
--------------------------------------------------------------------------------
(compilation_unit
(file_scoped_namespace_declaration
name: (identifier)
(class_declaration
name: (identifier)
body: (declaration_list))))
================================================================================
Interfaces
================================================================================
public interface IFoo {
}
--------------------------------------------------------------------------------
(compilation_unit
(interface_declaration
(modifier)
name: (identifier)
body: (declaration_list)))
================================================================================
Externs
================================================================================
extern alias A;
--------------------------------------------------------------------------------
(compilation_unit
(extern_alias_directive
(identifier)))
================================================================================
Delegates
================================================================================
public delegate int Global(ref char a = '\n');
public delegate ref int Global(ref char a = '\n');
public delegate ref readonly int Global(ref char a = '\n');
delegate void A<T>() where T:class;
delegate void A(params int [] test);
class Z {
delegate void Zed();
}
--------------------------------------------------------------------------------
(compilation_unit
(delegate_declaration
(modifier)
type: (predefined_type)
name: (identifier)
parameters: (parameter_list
(parameter
(parameter_modifier)
type: (predefined_type)
name: (identifier)
(equals_value_clause
(character_literal
(escape_sequence))))))
(delegate_declaration
(modifier)
type: (ref_type
type: (predefined_type))
name: (identifier)
parameters: (parameter_list
(parameter
(parameter_modifier)
type: (predefined_type)
name: (identifier)
(equals_value_clause
(character_literal
(escape_sequence))))))
(delegate_declaration
(modifier)
type: (ref_type
type: (predefined_type))
name: (identifier)
parameters: (parameter_list
(parameter
(parameter_modifier)
type: (predefined_type)
name: (identifier)
(equals_value_clause
(character_literal
(escape_sequence))))))
(delegate_declaration
type: (predefined_type)
name: (identifier)
type_parameters: (type_parameter_list
(type_parameter
name: (identifier)))
parameters: (parameter_list)
(type_parameter_constraints_clause
target: (identifier)
constraints: (type_parameter_constraint)))
(delegate_declaration
type: (predefined_type)
name: (identifier)
parameters: (parameter_list
type: (array_type
type: (predefined_type)
rank: (array_rank_specifier))
name: (identifier)))
(class_declaration
name: (identifier)
body: (declaration_list
(delegate_declaration
type: (predefined_type)
name: (identifier)
parameters: (parameter_list)))))
================================================================================
Var declared equal to integer literal
================================================================================
var a = 1;
--------------------------------------------------------------------------------
(compilation_unit
(global_statement
(local_declaration_statement
(variable_declaration
type: (implicit_type)
(variable_declarator
name: (identifier)
(equals_value_clause
(integer_literal)))))))

2039
corpus/statements.txt Normal file

File diff suppressed because it is too large Load Diff

140
corpus/structs.txt Normal file
View File

@ -0,0 +1,140 @@
================================================================================
Struct with a type parameter struct constraint
================================================================================
public struct F<T> where T:struct {}
--------------------------------------------------------------------------------
(compilation_unit
(struct_declaration
(modifier)
name: (identifier)
type_parameters: (type_parameter_list
(type_parameter
name: (identifier)))
(type_parameter_constraints_clause
target: (identifier)
constraints: (type_parameter_constraint))
body: (declaration_list)))
================================================================================
Struct with a type parameter class constraint
================================================================================
public struct F<T> where T:class {}
--------------------------------------------------------------------------------
(compilation_unit
(struct_declaration
(modifier)
name: (identifier)
type_parameters: (type_parameter_list
(type_parameter
name: (identifier)))
(type_parameter_constraints_clause
target: (identifier)
constraints: (type_parameter_constraint))
body: (declaration_list)))
================================================================================
Struct with type parameter new constraint
================================================================================
public struct F<T> where T: new() {}
--------------------------------------------------------------------------------
(compilation_unit
(struct_declaration
(modifier)
name: (identifier)
type_parameters: (type_parameter_list
(type_parameter
name: (identifier)))
(type_parameter_constraints_clause
target: (identifier)
constraints: (type_parameter_constraint
(constructor_constraint)))
body: (declaration_list)))
================================================================================
Struct with interface
================================================================================
public struct A : ISomething { }
--------------------------------------------------------------------------------
(compilation_unit
(struct_declaration
(modifier)
name: (identifier)
bases: (base_list
(identifier))
body: (declaration_list)))
================================================================================
Struct with multiple type parameter constraints
================================================================================
private struct F<T1,T2> where T1 : I1, I2, new() where T2 : I2 { }
--------------------------------------------------------------------------------
(compilation_unit
(struct_declaration
(modifier)
name: (identifier)
type_parameters: (type_parameter_list
(type_parameter
name: (identifier))
(type_parameter
name: (identifier)))
(type_parameter_constraints_clause
target: (identifier)
constraints: (type_parameter_constraint
(type_constraint
type: (identifier)))
constraints: (type_parameter_constraint
(type_constraint
type: (identifier)))
constraints: (type_parameter_constraint
(constructor_constraint)))
(type_parameter_constraints_clause
target: (identifier)
constraints: (type_parameter_constraint
(type_constraint
type: (identifier))))
body: (declaration_list)))
================================================================================
Struct with readonly modifier
================================================================================
readonly struct Test {
}
--------------------------------------------------------------------------------
(compilation_unit
(struct_declaration
(modifier)
name: (identifier)
body: (declaration_list)))
================================================================================
Struct with ref modifier
================================================================================
ref struct Test {
}
--------------------------------------------------------------------------------
(compilation_unit
(struct_declaration
(modifier)
name: (identifier)
body: (declaration_list)))

83
corpus/type-events.txt Normal file
View File

@ -0,0 +1,83 @@
================================================================================
Class event declarations
================================================================================
class A {
public event EventHandler<T> SomeEvent { add { } remove { } }
}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
name: (identifier)
body: (declaration_list
(event_declaration
(modifier)
type: (generic_name
name: (identifier)
type_arguments: (type_argument_list
(identifier)))
name: (identifier)
accessors: (accessor_list
(accessor_declaration
body: (block))
(accessor_declaration
body: (block)))))))
================================================================================
Struct event declarations
================================================================================
struct A {
public event EventHandler<T> SomeEvent { add { } remove { } }
}
--------------------------------------------------------------------------------
(compilation_unit
(struct_declaration
name: (identifier)
body: (declaration_list
(event_declaration
(modifier)
type: (generic_name
name: (identifier)
type_arguments: (type_argument_list
(identifier)))
name: (identifier)
accessors: (accessor_list
(accessor_declaration
body: (block))
(accessor_declaration
body: (block)))))))
================================================================================
Class event declarations with expression bodies
================================================================================
class A {
public event EventHandler SomeEvent { add => addSomething(); remove => removeSomething(); }
}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
name: (identifier)
body: (declaration_list
(event_declaration
(modifier)
type: (identifier)
name: (identifier)
accessors: (accessor_list
(accessor_declaration
body: (arrow_expression_clause
(invocation_expression
function: (identifier)
arguments: (argument_list))))
(accessor_declaration
body: (arrow_expression_clause
(invocation_expression
function: (identifier)
arguments: (argument_list)))))))))

382
corpus/type-fields.txt Normal file
View File

@ -0,0 +1,382 @@
================================================================================
Class field declarations
================================================================================
class A {
public readonly int _B;
Int64 D_e_f, g;
Tuple<char, Nullable<int>> z;
}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
name: (identifier)
body: (declaration_list
(field_declaration
(modifier)
(modifier)
(variable_declaration
type: (predefined_type)
(variable_declarator
name: (identifier))))
(field_declaration
(variable_declaration
type: (identifier)
(variable_declarator
name: (identifier))
(variable_declarator
name: (identifier))))
(field_declaration
(variable_declaration
type: (generic_name
name: (identifier)
type_arguments: (type_argument_list
(predefined_type)
(generic_name
name: (identifier)
type_arguments: (type_argument_list
(predefined_type)))))
(variable_declarator
name: (identifier)))))))
================================================================================
Struct field declarations
================================================================================
struct A {
private readonly int c_;
}
--------------------------------------------------------------------------------
(compilation_unit
(struct_declaration
name: (identifier)
body: (declaration_list
(field_declaration
(modifier)
(modifier)
(variable_declaration
type: (predefined_type)
(variable_declarator
name: (identifier)))))))
================================================================================
Class field nullable type
================================================================================
class A {
public readonly int? i;
private Byte? b;
}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
name: (identifier)
body: (declaration_list
(field_declaration
(modifier)
(modifier)
(variable_declaration
type: (nullable_type
type: (predefined_type))
(variable_declarator
name: (identifier))))
(field_declaration
(modifier)
(variable_declaration
type: (nullable_type
type: (identifier))
(variable_declarator
name: (identifier)))))))
================================================================================
Class field pointer type
================================================================================
class A {
public readonly int* i;
private Byte* b;
private void* c;
}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
name: (identifier)
body: (declaration_list
(field_declaration
(modifier)
(modifier)
(variable_declaration
type: (pointer_type
type: (predefined_type))
(variable_declarator
name: (identifier))))
(field_declaration
(modifier)
(variable_declaration
type: (pointer_type
type: (identifier))
(variable_declarator
name: (identifier))))
(field_declaration
(modifier)
(variable_declaration
type: (pointer_type
type: (predefined_type))
(variable_declarator
name: (identifier)))))))
================================================================================
Function pointer type
================================================================================
class A {
// Function pointer equivalent without calling convention
delegate*<string, int> a;
delegate*<delegate*<in string, int>, delegate*<ref string, ref readonly int>> b;
// Function pointer equivalent with calling convention
delegate* managed<string, int> c;
delegate*<delegate* unmanaged[MyCallConv, YourCallConv]<string, int>, delegate*<string, int>> d;
}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
name: (identifier)
body: (declaration_list
(comment)
(field_declaration
(variable_declaration
type: (function_pointer_type
(function_pointer_parameter
type: (predefined_type))
(function_pointer_parameter
type: (predefined_type)))
(variable_declarator
name: (identifier))))
(field_declaration
(variable_declaration
type: (function_pointer_type
(function_pointer_parameter
type: (function_pointer_type
(function_pointer_parameter
(parameter_modifier)
type: (predefined_type))
(function_pointer_parameter
type: (predefined_type))))
(function_pointer_parameter
type: (function_pointer_type
(function_pointer_parameter
(parameter_modifier)
type: (predefined_type))
(function_pointer_parameter
type: (ref_type
type: (predefined_type))))))
(variable_declarator
name: (identifier))))
(comment)
(field_declaration
(variable_declaration
type: (function_pointer_type
(function_pointer_calling_convention)
(function_pointer_parameter
type: (predefined_type))
(function_pointer_parameter
type: (predefined_type)))
(variable_declarator
name: (identifier))))
(field_declaration
(variable_declaration
type: (function_pointer_type
(function_pointer_parameter
type: (function_pointer_type
(function_pointer_calling_convention
(function_pointer_unmanaged_calling_convention_list
(function_pointer_unmanaged_calling_convention
(identifier))
(function_pointer_unmanaged_calling_convention
(identifier))))
(function_pointer_parameter
type: (predefined_type))
(function_pointer_parameter
type: (predefined_type))))
(function_pointer_parameter
type: (function_pointer_type
(function_pointer_parameter
type: (predefined_type))
(function_pointer_parameter
type: (predefined_type)))))
(variable_declarator
name: (identifier)))))))
================================================================================
Ref readonly
================================================================================
class A {
ref readonly Point Origin => ref origin;
ref readonly Point* Origin;
ref readonly Point[] Origin;
ref readonly Point? Origin;
}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
name: (identifier)
body: (declaration_list
(property_declaration
type: (ref_type
type: (identifier))
name: (identifier)
value: (arrow_expression_clause
(ref_expression
(identifier))))
(field_declaration
(variable_declaration
type: (ref_type
type: (pointer_type
type: (identifier)))
(variable_declarator
name: (identifier))))
(field_declaration
(variable_declaration
type: (ref_type
type: (array_type
type: (identifier)
rank: (array_rank_specifier)))
(variable_declarator
name: (identifier))))
(field_declaration
(variable_declaration
type: (ref_type
type: (nullable_type
type: (identifier)))
(variable_declarator
name: (identifier)))))))
================================================================================
Nullable reference types
================================================================================
class A {
string? a;
A? a;
}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
name: (identifier)
body: (declaration_list
(field_declaration
(variable_declaration
type: (nullable_type
type: (predefined_type))
(variable_declarator
name: (identifier))))
(field_declaration
(variable_declaration
type: (nullable_type
type: (identifier))
(variable_declarator
name: (identifier)))))))
================================================================================
Tuple types
================================================================================
class A {
(int, string str) a;
(B b, C c, D d) a;
}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
name: (identifier)
body: (declaration_list
(field_declaration
(variable_declaration
type: (tuple_type
(tuple_element
type: (predefined_type))
(tuple_element
type: (predefined_type)
name: (identifier)))
(variable_declarator
name: (identifier))))
(field_declaration
(variable_declaration
type: (tuple_type
(tuple_element
type: (identifier)
name: (identifier))
(tuple_element
type: (identifier)
name: (identifier))
(tuple_element
type: (identifier)
name: (identifier)))
(variable_declarator
name: (identifier)))))))
================================================================================
Native integer types
================================================================================
class A {
nint a;
nuint b;
}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
name: (identifier)
body: (declaration_list
(field_declaration
(variable_declaration
type: (predefined_type)
(variable_declarator
name: (identifier))))
(field_declaration
(variable_declaration
type: (predefined_type)
(variable_declarator
name: (identifier)))))))
================================================================================
Required fields
================================================================================
class A {
public required int B;
}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
name: (identifier)
body: (declaration_list
(field_declaration
(modifier)
(modifier)
(variable_declaration
type: (predefined_type)
(variable_declarator
name: (identifier)))))))

494
corpus/type-methods.txt Normal file
View File

@ -0,0 +1,494 @@
================================================================================
Class method with single parameter
================================================================================
class A {
private int GetBack(int b) {
return b;
}
}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
name: (identifier)
body: (declaration_list
(method_declaration
(modifier)
type: (predefined_type)
name: (identifier)
parameters: (parameter_list
(parameter
type: (predefined_type)
name: (identifier)))
body: (block
(return_statement
(identifier)))))))
================================================================================
Class method with multiple parameters
================================================================================
class A {
void DoSomething(A a, B b) {
}
}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
name: (identifier)
body: (declaration_list
(method_declaration
type: (predefined_type)
name: (identifier)
parameters: (parameter_list
(parameter
type: (identifier)
name: (identifier))
(parameter
type: (identifier)
name: (identifier)))
body: (block)))))
================================================================================
Class generic method
================================================================================
class A {
void Accept<T>(T accept) {
}
}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
name: (identifier)
body: (declaration_list
(method_declaration
type: (predefined_type)
name: (identifier)
type_parameters: (type_parameter_list
(type_parameter
name: (identifier)))
parameters: (parameter_list
(parameter
type: (identifier)
name: (identifier)))
body: (block)))))
================================================================================
Class generic method with new type constraint
================================================================================
class A {
void Accept<T>(T accept) where T: new() {
}
}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
name: (identifier)
body: (declaration_list
(method_declaration
type: (predefined_type)
name: (identifier)
type_parameters: (type_parameter_list
(type_parameter
name: (identifier)))
parameters: (parameter_list
(parameter
type: (identifier)
name: (identifier)))
(type_parameter_constraints_clause
target: (identifier)
constraints: (type_parameter_constraint
(constructor_constraint)))
body: (block)))))
================================================================================
Class generic method with multiple type constraints
================================================================================
class A {
void Accept<T1, T2>(T1 accept, T2 from)
where T1: new()
where T2: T1, new() {
}
}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
name: (identifier)
body: (declaration_list
(method_declaration
type: (predefined_type)
name: (identifier)
type_parameters: (type_parameter_list
(type_parameter
name: (identifier))
(type_parameter
name: (identifier)))
parameters: (parameter_list
(parameter
type: (identifier)
name: (identifier))
(parameter
type: (identifier)
name: (identifier)))
(type_parameter_constraints_clause
target: (identifier)
constraints: (type_parameter_constraint
(constructor_constraint)))
(type_parameter_constraints_clause
target: (identifier)
constraints: (type_parameter_constraint
(type_constraint
type: (identifier)))
constraints: (type_parameter_constraint
(constructor_constraint)))
body: (block)))))
================================================================================
Class method with out parameter
================================================================================
class A {
void HasAnOut(out int a) {
}
}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
name: (identifier)
body: (declaration_list
(method_declaration
type: (predefined_type)
name: (identifier)
parameters: (parameter_list
(parameter
(parameter_modifier)
type: (predefined_type)
name: (identifier)))
body: (block)))))
================================================================================
Class method with in parameter
================================================================================
class A {
void HasAnOut(in int a) {
}
}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
name: (identifier)
body: (declaration_list
(method_declaration
type: (predefined_type)
name: (identifier)
parameters: (parameter_list
(parameter
(parameter_modifier)
type: (predefined_type)
name: (identifier)))
body: (block)))))
================================================================================
Class method with ref parameter
================================================================================
class A {
void HasAnOut(ref int a) {
}
}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
name: (identifier)
body: (declaration_list
(method_declaration
type: (predefined_type)
name: (identifier)
parameters: (parameter_list
(parameter
(parameter_modifier)
type: (predefined_type)
name: (identifier)))
body: (block)))))
================================================================================
Class method with extension ref parameter
================================================================================
class A {
void M(this ref int a) { }
void M(this scoped ref int a) { }
}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
name: (identifier)
body: (declaration_list
(method_declaration
type: (predefined_type)
name: (identifier)
parameters: (parameter_list
(parameter
(parameter_modifier)
(parameter_modifier)
type: (predefined_type)
name: (identifier)))
body: (block))
(method_declaration
type: (predefined_type)
name: (identifier)
parameters: (parameter_list
(parameter
(parameter_modifier)
(parameter_modifier)
(parameter_modifier)
type: (predefined_type)
name: (identifier)))
body: (block)))))
================================================================================
Class method with contextually-reserved keyword named parameters
================================================================================
class A {
void HasAnOut(int from, string partial) {
}
}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
name: (identifier)
body: (declaration_list
(method_declaration
type: (predefined_type)
name: (identifier)
parameters: (parameter_list
(parameter
type: (predefined_type)
name: (identifier))
(parameter
type: (predefined_type)
name: (identifier)))
body: (block)))))
================================================================================
Class method with default parameter
================================================================================
class A {
void HasAnOut(int a = 5) {
}
}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
name: (identifier)
body: (declaration_list
(method_declaration
type: (predefined_type)
name: (identifier)
parameters: (parameter_list
(parameter
type: (predefined_type)
name: (identifier)
(equals_value_clause
(integer_literal))))
body: (block)))))
================================================================================
Class static method with single parameter
================================================================================
class A {
static int GetBack(int b) {
return b;
}
}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
name: (identifier)
body: (declaration_list
(method_declaration
(modifier)
type: (predefined_type)
name: (identifier)
parameters: (parameter_list
(parameter
type: (predefined_type)
name: (identifier)))
body: (block
(return_statement
(identifier)))))))
================================================================================
Class method with explicit interface specifier
================================================================================
class A : ISomething {
int ISomething.GetBack(int b) {
return b;
}
}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
name: (identifier)
bases: (base_list
(identifier))
body: (declaration_list
(method_declaration
type: (predefined_type)
(explicit_interface_specifier
name: (identifier))
name: (identifier)
parameters: (parameter_list
(parameter
type: (predefined_type)
name: (identifier)))
body: (block
(return_statement
(identifier)))))))
================================================================================
Class method with readonly method
================================================================================
class A {
public readonly double Add => x + y;
}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
name: (identifier)
body: (declaration_list
(property_declaration
(modifier)
(modifier)
type: (predefined_type)
name: (identifier)
value: (arrow_expression_clause
(binary_expression
left: (identifier)
right: (identifier)))))))
================================================================================
Class method with nullable parameter list
================================================================================
class A {
public int Zero(params int[]? ints) => 0;
}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
name: (identifier)
body: (declaration_list
(method_declaration
(modifier)
type: (predefined_type)
name: (identifier)
parameters: (parameter_list
type: (nullable_type
type: (array_type
type: (predefined_type)
rank: (array_rank_specifier)))
name: (identifier))
body: (arrow_expression_clause
(integer_literal))))))
================================================================================
Method with scoped parameter and scoped local variable
================================================================================
ref struct S {
void M(scoped ref System.Span<int> p) {
scoped ref System.Span<int> i = ref p;
scoped System.Span<int> j = p;
}
}
--------------------------------------------------------------------------------
(compilation_unit
(struct_declaration
(modifier)
name: (identifier)
body: (declaration_list
(method_declaration
type: (predefined_type)
name: (identifier)
parameters: (parameter_list
(parameter
(parameter_modifier)
(parameter_modifier)
type: (qualified_name
qualifier: (identifier)
name: (generic_name
name: (identifier)
type_arguments: (type_argument_list
(predefined_type))))
name: (identifier)))
body: (block
(local_declaration_statement
(variable_declaration
type: (scoped_type
type: (ref_type
type: (qualified_name
qualifier: (identifier)
name: (generic_name
name: (identifier)
type_arguments: (type_argument_list
(predefined_type))))))
(variable_declarator
name: (identifier)
(equals_value_clause
(ref_expression
(identifier))))))
(local_declaration_statement
(variable_declaration
type: (scoped_type
type: (qualified_name
qualifier: (identifier)
name: (generic_name
name: (identifier)
type_arguments: (type_argument_list
(predefined_type)))))
(variable_declarator
name: (identifier)
(equals_value_clause
(identifier))))))))))

901
corpus/type-operators.txt Normal file
View File

@ -0,0 +1,901 @@
================================================================================
Operator declarations
================================================================================
class A
{
[SomeAttribute]
public static int operator +(A a) { return 0; }
public static int operator +(A a, A b) { return 0; }
int operator -(A a) { return 0; }
}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
name: (identifier)
body: (declaration_list
(operator_declaration
(attribute_list
(attribute
name: (identifier)))
(modifier)
(modifier)
type: (predefined_type)
parameters: (parameter_list
(parameter
type: (identifier)
name: (identifier)))
body: (block
(return_statement
(integer_literal))))
(operator_declaration
(modifier)
(modifier)
type: (predefined_type)
parameters: (parameter_list
(parameter
type: (identifier)
name: (identifier))
(parameter
type: (identifier)
name: (identifier)))
body: (block
(return_statement
(integer_literal))))
(operator_declaration
type: (predefined_type)
parameters: (parameter_list
(parameter
type: (identifier)
name: (identifier)))
body: (block
(return_statement
(integer_literal)))))))
================================================================================
boolean operator declarations
================================================================================
class A
{
public static bool operator true(A a) { return true; }
bool operator false(A a) { return false; }
}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
name: (identifier)
body: (declaration_list
(operator_declaration
(modifier)
(modifier)
type: (predefined_type)
parameters: (parameter_list
(parameter
type: (identifier)
name: (identifier)))
body: (block
(return_statement
(boolean_literal))))
(operator_declaration
type: (predefined_type)
parameters: (parameter_list
(parameter
type: (identifier)
name: (identifier)))
body: (block
(return_statement
(boolean_literal)))))))
================================================================================
conversion operator declaration
================================================================================
class A
{
public static implicit operator int (A a) { return 0; }
explicit operator int (A a) { return 0; }
}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
name: (identifier)
body: (declaration_list
(conversion_operator_declaration
(modifier)
(modifier)
type: (predefined_type)
parameters: (parameter_list
(parameter
type: (identifier)
name: (identifier)))
body: (block
(return_statement
(integer_literal))))
(conversion_operator_declaration
type: (predefined_type)
parameters: (parameter_list
(parameter
type: (identifier)
name: (identifier)))
body: (block
(return_statement
(integer_literal)))))))
================================================================================
conversion operator with expression body
================================================================================
class A
{
public static implicit operator int (A a) => 0;
}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
name: (identifier)
body: (declaration_list
(conversion_operator_declaration
(modifier)
(modifier)
type: (predefined_type)
parameters: (parameter_list
(parameter
type: (identifier)
name: (identifier)))
body: (arrow_expression_clause
(integer_literal))))))
================================================================================
extern operators
================================================================================
class A
{
public static extern int operator + (A a);
public static extern bool operator <(A a, A b);
public static explicit operator int (A a);
}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
name: (identifier)
body: (declaration_list
(operator_declaration
(modifier)
(modifier)
(modifier)
type: (predefined_type)
parameters: (parameter_list
(parameter
type: (identifier)
name: (identifier))))
(operator_declaration
(modifier)
(modifier)
(modifier)
type: (predefined_type)
parameters: (parameter_list
(parameter
type: (identifier)
name: (identifier))
(parameter
type: (identifier)
name: (identifier))))
(conversion_operator_declaration
(modifier)
(modifier)
type: (predefined_type)
parameters: (parameter_list
(parameter
type: (identifier)
name: (identifier)))))))
================================================================================
Class conversion operators with expression body
================================================================================
class A
{
public static extern int operator + (A a) => 0;
public static extern bool operator <(A a, A b) => true;
public static explicit operator int (A a) => 0;
}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
name: (identifier)
body: (declaration_list
(operator_declaration
(modifier)
(modifier)
(modifier)
type: (predefined_type)
parameters: (parameter_list
(parameter
type: (identifier)
name: (identifier)))
body: (arrow_expression_clause
(integer_literal)))
(operator_declaration
(modifier)
(modifier)
(modifier)
type: (predefined_type)
parameters: (parameter_list
(parameter
type: (identifier)
name: (identifier))
(parameter
type: (identifier)
name: (identifier)))
body: (arrow_expression_clause
(boolean_literal)))
(conversion_operator_declaration
(modifier)
(modifier)
type: (predefined_type)
parameters: (parameter_list
(parameter
type: (identifier)
name: (identifier)))
body: (arrow_expression_clause
(integer_literal))))))
================================================================================
Unary operator overloads
================================================================================
class A
{
public static A operator +(A a) { return a; }
public static A operator -(A a) { return a; }
public static A operator !(A a) { return a; }
public static A operator ~(A a) { return a; }
public static A operator ++(A a) { return a; }
public static A operator --(A a) { return a; }
public static bool operator true (A a) { return true; }
public static bool operator false(A a) { return true; }
}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
name: (identifier)
body: (declaration_list
(operator_declaration
(modifier)
(modifier)
type: (identifier)
parameters: (parameter_list
(parameter
type: (identifier)
name: (identifier)))
body: (block
(return_statement
(identifier))))
(operator_declaration
(modifier)
(modifier)
type: (identifier)
parameters: (parameter_list
(parameter
type: (identifier)
name: (identifier)))
body: (block
(return_statement
(identifier))))
(operator_declaration
(modifier)
(modifier)
type: (identifier)
parameters: (parameter_list
(parameter
type: (identifier)
name: (identifier)))
body: (block
(return_statement
(identifier))))
(operator_declaration
(modifier)
(modifier)
type: (identifier)
parameters: (parameter_list
(parameter
type: (identifier)
name: (identifier)))
body: (block
(return_statement
(identifier))))
(operator_declaration
(modifier)
(modifier)
type: (identifier)
parameters: (parameter_list
(parameter
type: (identifier)
name: (identifier)))
body: (block
(return_statement
(identifier))))
(operator_declaration
(modifier)
(modifier)
type: (identifier)
parameters: (parameter_list
(parameter
type: (identifier)
name: (identifier)))
body: (block
(return_statement
(identifier))))
(operator_declaration
(modifier)
(modifier)
type: (predefined_type)
parameters: (parameter_list
(parameter
type: (identifier)
name: (identifier)))
body: (block
(return_statement
(boolean_literal))))
(operator_declaration
(modifier)
(modifier)
type: (predefined_type)
parameters: (parameter_list
(parameter
type: (identifier)
name: (identifier)))
body: (block
(return_statement
(boolean_literal)))))))
================================================================================
Binary unpaired operator overloads
================================================================================
class A
{
public static A operator +(A a, A b) { return a == b; }
public static A operator -(A a, A b) { return a != b; }
public static A operator *(A a, A b) { return a < b; }
public static A operator /(A a, A b) { return a <= b; }
public static A operator %(A a, A b) { return a > b; }
public static A operator &(A a, A b) { return a >= b; }
public static A operator |(A a, A b) { return a >= b; }
public static A operator ^(A a, A b) { return a >= b; }
public static A operator <<(A a, A b) { return a >= b; }
public static A operator >>(A a, A b) { return a >= b; }
public static A operator >>>(A a, A b) { return a >= b; }
}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
name: (identifier)
body: (declaration_list
(operator_declaration
(modifier)
(modifier)
type: (identifier)
parameters: (parameter_list
(parameter
type: (identifier)
name: (identifier))
(parameter
type: (identifier)
name: (identifier)))
body: (block
(return_statement
(binary_expression
left: (identifier)
right: (identifier)))))
(operator_declaration
(modifier)
(modifier)
type: (identifier)
parameters: (parameter_list
(parameter
type: (identifier)
name: (identifier))
(parameter
type: (identifier)
name: (identifier)))
body: (block
(return_statement
(binary_expression
left: (identifier)
right: (identifier)))))
(operator_declaration
(modifier)
(modifier)
type: (identifier)
parameters: (parameter_list
(parameter
type: (identifier)
name: (identifier))
(parameter
type: (identifier)
name: (identifier)))
body: (block
(return_statement
(binary_expression
left: (identifier)
right: (identifier)))))
(operator_declaration
(modifier)
(modifier)
type: (identifier)
parameters: (parameter_list
(parameter
type: (identifier)
name: (identifier))
(parameter
type: (identifier)
name: (identifier)))
body: (block
(return_statement
(binary_expression
left: (identifier)
right: (identifier)))))
(operator_declaration
(modifier)
(modifier)
type: (identifier)
parameters: (parameter_list
(parameter
type: (identifier)
name: (identifier))
(parameter
type: (identifier)
name: (identifier)))
body: (block
(return_statement
(binary_expression
left: (identifier)
right: (identifier)))))
(operator_declaration
(modifier)
(modifier)
type: (identifier)
parameters: (parameter_list
(parameter
type: (identifier)
name: (identifier))
(parameter
type: (identifier)
name: (identifier)))
body: (block
(return_statement
(binary_expression
left: (identifier)
right: (identifier)))))
(operator_declaration
(modifier)
(modifier)
type: (identifier)
parameters: (parameter_list
(parameter
type: (identifier)
name: (identifier))
(parameter
type: (identifier)
name: (identifier)))
body: (block
(return_statement
(binary_expression
left: (identifier)
right: (identifier)))))
(operator_declaration
(modifier)
(modifier)
type: (identifier)
parameters: (parameter_list
(parameter
type: (identifier)
name: (identifier))
(parameter
type: (identifier)
name: (identifier)))
body: (block
(return_statement
(binary_expression
left: (identifier)
right: (identifier)))))
(operator_declaration
(modifier)
(modifier)
type: (identifier)
parameters: (parameter_list
(parameter
type: (identifier)
name: (identifier))
(parameter
type: (identifier)
name: (identifier)))
body: (block
(return_statement
(binary_expression
left: (identifier)
right: (identifier)))))
(operator_declaration
(modifier)
(modifier)
type: (identifier)
parameters: (parameter_list
(parameter
type: (identifier)
name: (identifier))
(parameter
type: (identifier)
name: (identifier)))
body: (block
(return_statement
(binary_expression
left: (identifier)
right: (identifier)))))
(operator_declaration
(modifier)
(modifier)
type: (identifier)
parameters: (parameter_list
(parameter
type: (identifier)
name: (identifier))
(parameter
type: (identifier)
name: (identifier)))
body: (block
(return_statement
(binary_expression
left: (identifier)
right: (identifier))))))))
================================================================================
Binary paired operator overloads
================================================================================
class A
{
public static A operator ==(A a, A b) { return a == b; }
public static A operator !=(A a, A b) { return a != b; }
public static A operator <(A a, A b) { return a < b; }
public static A operator <=(A a, A b) { return a <= b; }
public static A operator >(A a, A b) { return a > b; }
public static A operator >=(A a, A b) { return a >= b; }
}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
name: (identifier)
body: (declaration_list
(operator_declaration
(modifier)
(modifier)
type: (identifier)
parameters: (parameter_list
(parameter
type: (identifier)
name: (identifier))
(parameter
type: (identifier)
name: (identifier)))
body: (block
(return_statement
(binary_expression
left: (identifier)
right: (identifier)))))
(operator_declaration
(modifier)
(modifier)
type: (identifier)
parameters: (parameter_list
(parameter
type: (identifier)
name: (identifier))
(parameter
type: (identifier)
name: (identifier)))
body: (block
(return_statement
(binary_expression
left: (identifier)
right: (identifier)))))
(operator_declaration
(modifier)
(modifier)
type: (identifier)
parameters: (parameter_list
(parameter
type: (identifier)
name: (identifier))
(parameter
type: (identifier)
name: (identifier)))
body: (block
(return_statement
(binary_expression
left: (identifier)
right: (identifier)))))
(operator_declaration
(modifier)
(modifier)
type: (identifier)
parameters: (parameter_list
(parameter
type: (identifier)
name: (identifier))
(parameter
type: (identifier)
name: (identifier)))
body: (block
(return_statement
(binary_expression
left: (identifier)
right: (identifier)))))
(operator_declaration
(modifier)
(modifier)
type: (identifier)
parameters: (parameter_list
(parameter
type: (identifier)
name: (identifier))
(parameter
type: (identifier)
name: (identifier)))
body: (block
(return_statement
(binary_expression
left: (identifier)
right: (identifier)))))
(operator_declaration
(modifier)
(modifier)
type: (identifier)
parameters: (parameter_list
(parameter
type: (identifier)
name: (identifier))
(parameter
type: (identifier)
name: (identifier)))
body: (block
(return_statement
(binary_expression
left: (identifier)
right: (identifier))))))))
================================================================================
Explicit operator overloads
================================================================================
interface I
{
static abstract int operator +(I i, I j);
}
public class C : I
{
static int I.operator +(I i, I j) { return 1; }
}
--------------------------------------------------------------------------------
(compilation_unit
(interface_declaration
name: (identifier)
body: (declaration_list
(operator_declaration
(modifier)
(modifier)
type: (predefined_type)
parameters: (parameter_list
(parameter
type: (identifier)
name: (identifier))
(parameter
type: (identifier)
name: (identifier))))))
(class_declaration
(modifier)
name: (identifier)
bases: (base_list
(identifier))
body: (declaration_list
(operator_declaration
(modifier)
type: (predefined_type)
(explicit_interface_specifier
name: (identifier))
parameters: (parameter_list
(parameter
type: (identifier)
name: (identifier))
(parameter
type: (identifier)
name: (identifier)))
body: (block
(return_statement
(integer_literal)))))))
================================================================================
Checked operators
================================================================================
public class C
{
public static int operator checked +(C i, C j) => throw null;
public static int operator +(C i, C j) => throw null;
public static explicit operator checked int(C c) => throw null;
public static explicit operator int(C c) => throw null;
}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
(modifier)
name: (identifier)
body: (declaration_list
(operator_declaration
(modifier)
(modifier)
type: (predefined_type)
parameters: (parameter_list
(parameter
type: (identifier)
name: (identifier))
(parameter
type: (identifier)
name: (identifier)))
body: (arrow_expression_clause
(throw_expression
(null_literal))))
(operator_declaration
(modifier)
(modifier)
type: (predefined_type)
parameters: (parameter_list
(parameter
type: (identifier)
name: (identifier))
(parameter
type: (identifier)
name: (identifier)))
body: (arrow_expression_clause
(throw_expression
(null_literal))))
(conversion_operator_declaration
(modifier)
(modifier)
type: (predefined_type)
parameters: (parameter_list
(parameter
type: (identifier)
name: (identifier)))
body: (arrow_expression_clause
(throw_expression
(null_literal))))
(conversion_operator_declaration
(modifier)
(modifier)
type: (predefined_type)
parameters: (parameter_list
(parameter
type: (identifier)
name: (identifier)))
body: (arrow_expression_clause
(throw_expression
(null_literal)))))))
================================================================================
Explicit conversion operator overloads
================================================================================
public interface I<T> where T : I<T>
{
static abstract explicit operator T(C c);
static abstract explicit operator checked T(C c);
}
public class C : I<C>
{
static explicit I<C>.operator C(C c) => throw null;
static explicit I<C>.operator checked C(C c) => throw null;
}
--------------------------------------------------------------------------------
(compilation_unit
(interface_declaration
(modifier)
name: (identifier)
type_parameters: (type_parameter_list
(type_parameter
name: (identifier)))
(type_parameter_constraints_clause
target: (identifier)
constraints: (type_parameter_constraint
(type_constraint
type: (generic_name
name: (identifier)
type_arguments: (type_argument_list
(identifier))))))
body: (declaration_list
(conversion_operator_declaration
(modifier)
(modifier)
type: (identifier)
parameters: (parameter_list
(parameter
type: (identifier)
name: (identifier))))
(conversion_operator_declaration
(modifier)
(modifier)
type: (identifier)
parameters: (parameter_list
(parameter
type: (identifier)
name: (identifier))))))
(class_declaration
(modifier)
name: (identifier)
bases: (base_list
(generic_name
name: (identifier)
type_arguments: (type_argument_list
(identifier))))
body: (declaration_list
(conversion_operator_declaration
(modifier)
(explicit_interface_specifier
name: (generic_name
name: (identifier)
type_arguments: (type_argument_list
(identifier))))
type: (identifier)
parameters: (parameter_list
(parameter
type: (identifier)
name: (identifier)))
body: (arrow_expression_clause
(throw_expression
(null_literal))))
(conversion_operator_declaration
(modifier)
(explicit_interface_specifier
name: (generic_name
name: (identifier)
type_arguments: (type_argument_list
(identifier))))
type: (identifier)
parameters: (parameter_list
(parameter
type: (identifier)
name: (identifier)))
body: (arrow_expression_clause
(throw_expression
(null_literal)))))))

233
corpus/type-properties.txt Normal file
View File

@ -0,0 +1,233 @@
================================================================================
Class with bodyless properties
================================================================================
class Foo {
byte Get { get; }
char Set { set; }
uint GetSet { get; set; }
long SetGet { set; get; }
}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
name: (identifier)
body: (declaration_list
(property_declaration
type: (predefined_type)
name: (identifier)
accessors: (accessor_list
(accessor_declaration)))
(property_declaration
type: (predefined_type)
name: (identifier)
accessors: (accessor_list
(accessor_declaration)))
(property_declaration
type: (predefined_type)
name: (identifier)
accessors: (accessor_list
(accessor_declaration)
(accessor_declaration)))
(property_declaration
type: (predefined_type)
name: (identifier)
accessors: (accessor_list
(accessor_declaration)
(accessor_declaration))))))
================================================================================
Class with init properties
================================================================================
class Person
{
public string FirstName { get; init; }
}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
name: (identifier)
body: (declaration_list
(property_declaration
(modifier)
type: (predefined_type)
name: (identifier)
accessors: (accessor_list
(accessor_declaration)
(accessor_declaration))))))
================================================================================
Class with single-accessor property bodies
================================================================================
class Foo {
byte Get { get { return 0xFF; } }
char Set { set { x = value; } }
}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
name: (identifier)
body: (declaration_list
(property_declaration
type: (predefined_type)
name: (identifier)
accessors: (accessor_list
(accessor_declaration
body: (block
(return_statement
(integer_literal))))))
(property_declaration
type: (predefined_type)
name: (identifier)
accessors: (accessor_list
(accessor_declaration
body: (block
(expression_statement
(assignment_expression
left: (identifier)
(assignment_operator)
right: (identifier))))))))))
================================================================================
Class with double-accessor property bodies
================================================================================
class Foo {
uint GetSet {
get { return x; }
set { x = value; }
}
long SetGet {
set { x = value; }
get { return x; }
}
}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
name: (identifier)
body: (declaration_list
(property_declaration
type: (predefined_type)
name: (identifier)
accessors: (accessor_list
(accessor_declaration
body: (block
(return_statement
(identifier))))
(accessor_declaration
body: (block
(expression_statement
(assignment_expression
left: (identifier)
(assignment_operator)
right: (identifier)))))))
(property_declaration
type: (predefined_type)
name: (identifier)
accessors: (accessor_list
(accessor_declaration
body: (block
(expression_statement
(assignment_expression
left: (identifier)
(assignment_operator)
right: (identifier)))))
(accessor_declaration
body: (block
(return_statement
(identifier)))))))))
================================================================================
Class with bodyless properties and initializers
================================================================================
class Foo {
byte Get { get; } = 0x00;
uint GetSet { get; set; } = 1;
long SetGet { set; get; } = 2;
}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
name: (identifier)
body: (declaration_list
(property_declaration
type: (predefined_type)
name: (identifier)
accessors: (accessor_list
(accessor_declaration))
value: (integer_literal))
(property_declaration
type: (predefined_type)
name: (identifier)
accessors: (accessor_list
(accessor_declaration)
(accessor_declaration))
value: (integer_literal))
(property_declaration
type: (predefined_type)
name: (identifier)
accessors: (accessor_list
(accessor_declaration)
(accessor_declaration))
value: (integer_literal)))))
================================================================================
Class with explicit interface properties
================================================================================
class Foo: IFoo {
byte IFoo.Get { get; }
}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
name: (identifier)
bases: (base_list
(identifier))
body: (declaration_list
(property_declaration
type: (predefined_type)
(explicit_interface_specifier
name: (identifier))
name: (identifier)
accessors: (accessor_list
(accessor_declaration))))))
================================================================================
Required properties
================================================================================
class A {
public required int B { get; set; }
}
--------------------------------------------------------------------------------
(compilation_unit
(class_declaration
name: (identifier)
body: (declaration_list
(property_declaration
(modifier)
(modifier)
type: (predefined_type)
name: (identifier)
accessors: (accessor_list
(accessor_declaration)
(accessor_declaration))))))

2004
grammar.js Normal file

File diff suppressed because it is too large Load Diff

35
package.json Normal file
View File

@ -0,0 +1,35 @@
{
"name": "tree-sitter-c-sharp",
"version": "0.20.0",
"description": "C# grammar for tree-sitter",
"main": "bindings/node",
"keywords": [
"parser",
"tree-sitter",
"lexer"
],
"repository": {
"type": "git",
"url": "https://github.com/tree-sitter/tree-sitter-c-sharp.git"
},
"author": "The Tree-Sitter C# Team",
"license": "MIT",
"dependencies": {
"nan": "^2.14.0"
},
"devDependencies": {
"tree-sitter-cli": "^0.20.0"
},
"scripts": {
"test": "tree-sitter test && script/update-file-sizes && script/parse-examples",
"test-windows": "tree-sitter test"
},
"tree-sitter": [
{
"scope": "source.cs",
"file-types": [
"cs"
]
}
]
}

254
queries/highlights.scm Normal file
View File

@ -0,0 +1,254 @@
;; Methods
(method_declaration name: (identifier) @function)
(local_function_statement name: (identifier) @function)
;; Types
(interface_declaration name: (identifier) @type)
(class_declaration name: (identifier) @type)
(enum_declaration name: (identifier) @type)
(struct_declaration (identifier) @type)
(record_declaration (identifier) @type)
(record_struct_declaration (identifier) @type)
(namespace_declaration name: (identifier) @module)
(constructor_declaration name: (identifier) @constructor)
(destructor_declaration name: (identifier) @constructor)
[
(implicit_type)
(predefined_type)
] @type.builtin
(_ type: (identifier) @type)
;; Enum
(enum_member_declaration (identifier) @property.definition)
;; Literals
[
(real_literal)
(integer_literal)
] @number
[
(character_literal)
(string_literal)
(verbatim_string_literal)
(interpolated_string_text)
(interpolated_verbatim_string_text)
"\""
"$\""
"@$\""
"$@\""
] @string
[
(boolean_literal)
(null_literal)
] @constant.builtin
;; Comments
(comment) @comment
;; Tokens
[
";"
"."
","
] @punctuation.delimiter
[
"--"
"-"
"-="
"&"
"&="
"&&"
"+"
"++"
"+="
"<"
"<="
"<<"
"<<="
"="
"=="
"!"
"!="
"=>"
">"
">="
">>"
">>="
">>>"
">>>="
"|"
"|="
"||"
"?"
"??"
"??="
"^"
"^="
"~"
"*"
"*="
"/"
"/="
"%"
"%="
":"
] @operator
[
"("
")"
"["
"]"
"{"
"}"
] @punctuation.bracket
;; Keywords
(modifier) @keyword
(this_expression) @keyword
(escape_sequence) @keyword
[
"add"
"alias"
"as"
"base"
"break"
"case"
"catch"
"checked"
"class"
"continue"
"default"
"delegate"
"do"
"else"
"enum"
"event"
"explicit"
"extern"
"finally"
"for"
"foreach"
"global"
"goto"
"if"
"implicit"
"interface"
"is"
"lock"
"namespace"
"notnull"
"operator"
"params"
"return"
"remove"
"sizeof"
"stackalloc"
"static"
"struct"
"switch"
"throw"
"try"
"typeof"
"unchecked"
"using"
"while"
"new"
"await"
"in"
"yield"
"get"
"set"
"when"
"out"
"ref"
"from"
"where"
"select"
"record"
"init"
"with"
"let"
] @keyword
;; Linq
(from_clause (identifier) @variable)
(group_clause (identifier) @variable)
(order_by_clause (identifier) @variable)
(join_clause (identifier) @variable)
(select_clause (identifier) @variable)
(query_continuation (identifier) @variable) @keyword
;; Record
(with_expression
(with_initializer_expression
(simple_assignment_expression
(identifier) @variable)))
;; Exprs
(binary_expression (identifier) @variable (identifier) @variable)
(binary_expression (identifier)* @variable)
(conditional_expression (identifier) @variable)
(prefix_unary_expression (identifier) @variable)
(postfix_unary_expression (identifier)* @variable)
(assignment_expression (identifier) @variable)
(cast_expression (_) (identifier) @variable)
;; Class
(base_list (identifier) @type) ;; applies to record_base too
(property_declaration (generic_name))
(property_declaration
name: (identifier) @variable)
(property_declaration
name: (identifier) @variable)
(property_declaration
name: (identifier) @variable)
;; Lambda
(lambda_expression) @variable
;; Attribute
(attribute) @attribute
;; Parameter
(parameter
name: (identifier) @variable.parameter)
(parameter (identifier) @variable.parameter)
(parameter_modifier) @keyword
;; Variable declarations
(variable_declarator (identifier) @variable)
(for_each_statement left: (identifier) @variable)
(catch_declaration (_) (identifier) @variable)
;; Return
(return_statement (identifier) @variable)
(yield_statement (identifier) @variable)
;; Type
(generic_name (identifier) @type)
(type_parameter (identifier) @property.definition)
(type_argument_list (identifier) @type)
(as_expression right: (identifier) @type)
(is_expression right: (identifier) @type)
;; Type constraints
(type_parameter_constraints_clause (identifier) @property.definition)
;; Switch
(switch_statement (identifier) @variable)
(switch_expression (identifier) @variable)
;; Lock statement
(lock_statement (identifier) @variable)
;; Method calls
(invocation_expression (member_access_expression name: (identifier) @function))

23
queries/tags.scm Normal file
View File

@ -0,0 +1,23 @@
(class_declaration name: (identifier) @name) @definition.class
(class_declaration bases: (base_list (_) @name)) @reference.class
(interface_declaration name: (identifier) @name) @definition.interface
(interface_declaration bases: (base_list (_) @name)) @reference.interface
(method_declaration name: (identifier) @name) @definition.method
(object_creation_expression type: (identifier) @name) @reference.class
(type_parameter_constraints_clause target: (identifier) @name) @reference.class
(type_constraint type: (identifier) @name) @reference.class
(variable_declaration type: (identifier) @name) @reference.class
(invocation_expression function: (member_access_expression name: (identifier) @name)) @reference.send
(namespace_declaration name: (identifier) @name) @definition.module
(namespace_declaration name: (identifier) @name) @module

5
script/file_sizes.txt Normal file
View File

@ -0,0 +1,5 @@
src/grammar.json 0.2MB 11156
src/node-types.json 0.1MB 7916
src/parser.c 41.8MB 1311934
src/scanner.c 0.0MB 37
total 42.2MB 1331043

View File

@ -0,0 +1,3 @@
examples/Newtonsoft.Json/Src/Newtonsoft.Json.Tests/Serialization/SerializationErrorHandlingTests.cs
examples/Newtonsoft.Json/Src/Newtonsoft.Json.Tests/TestFixtureBase.cs
examples/Newtonsoft.Json/Src/Newtonsoft.Json/JsonReader.cs

45
script/parse-examples Executable file
View File

@ -0,0 +1,45 @@
#!/bin/bash
set -eu
cd "$(dirname "$0")/.."
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
actual_sha=$(git rev-parse HEAD)
if [ "$actual_sha" != "$sha" ]; then
echo "Updating $owner/$name to $sha"
git fetch
git reset --hard $sha
fi
popd > /dev/null
}
clone_repo JamesNK Newtonsoft.Json 7c3d7f8da7e35dde8fa74188b0decff70f8f10e3
clone_repo nunit nunit ad49f27294bd0f2677d8699756c6ccb10df600f8
clone_repo OrchardCMS orchard 0a82721968232b07354edcaac63a9ccea02220c6
known_failures="$(cat script/known_failures.txt)"
tree-sitter parse -q \
'examples/**/*.cs' \
$(for failure in $known_failures; do echo "!${failure}"; done)
example_count=$(find examples -name '*.cs' | 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

3
script/update-file-sizes Executable file
View File

@ -0,0 +1,3 @@
#!/bin/bash
wc -l -m src/*.* | awk '{printf("%-20s\t%0.1fMB\t%10s\n", $3, $2/1048576, $1)}' > script/file_sizes.txt

7
script/update-known-failures Executable file
View File

@ -0,0 +1,7 @@
#!/bin/bash
set -eu
find examples -name '*.cs' -print0 | \
xargs -0 -n 1000 tree-sitter parse -q | \
tee >(cut -d' ' -f1 | sort > script/known-failures.txt)

11155
src/grammar.json vendored Normal file

File diff suppressed because it is too large Load Diff

7917
src/node-types.json vendored Normal file

File diff suppressed because it is too large Load Diff

1311896
src/parser.c vendored Normal file

File diff suppressed because it is too large Load Diff

37
src/scanner.c vendored Normal file
View File

@ -0,0 +1,37 @@
#include <tree_sitter/parser.h>
#include <wctype.h>
enum TokenType {
PREPROC_DIRECTIVE_END,
OPT_SEMI,
};
void *tree_sitter_c_sharp_external_scanner_create() { return NULL; }
void tree_sitter_c_sharp_external_scanner_destroy(void *p) {}
void tree_sitter_c_sharp_external_scanner_reset(void *p) {}
unsigned tree_sitter_c_sharp_external_scanner_serialize(void *p, char *buffer) { return 0; }
void tree_sitter_c_sharp_external_scanner_deserialize(void *p, const char *b, unsigned n) {}
bool tree_sitter_c_sharp_external_scanner_scan(
void *payload,
TSLexer *lexer,
const bool *valid_symbols
) {
if (valid_symbols[OPT_SEMI]) {
lexer->result_symbol = OPT_SEMI;
if (lexer->lookahead == ';') {
lexer->advance(lexer, true);
}
return true;
}
// Detect either a newline or EOF. Currently, external scanners
// are the only way to match against EOF.
if (!valid_symbols[PREPROC_DIRECTIVE_END]) return false;
lexer->result_symbol = PREPROC_DIRECTIVE_END;
for (;;) {
if (lexer->lookahead == 0) return true;
if (lexer->lookahead == '\n') return true;
if (!iswspace(lexer->lookahead)) return false;
lexer->advance(lexer, true);
}
}

230
src/tree_sitter/parser.h vendored Normal file
View File

@ -0,0 +1,230 @@
#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
#ifndef TREE_SITTER_API_H_
typedef uint16_t TSStateId;
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
*/
#ifdef _MSC_VER
#define UNUSED __pragma(warning(suppress : 4101))
#else
#define UNUSED __attribute__((unused))
#endif
#define START_LEXER() \
bool result = false; \
bool skip = false; \
UNUSED \
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_

3
test.js Normal file
View File

@ -0,0 +1,3 @@
console.log("Trying to require index.js in JavaScript");
require('.')
console.log("Require successfull");

986
test/highlight/baseline.cs Normal file
View File

@ -0,0 +1,986 @@
extern alias A;
// ^ punctuation.delimiter
using System;
// <- keyword
// ^ punctuation.delimiter
using global::System.Collections.Generic;
// <- keyword
// ^ punctuation.delimiter
// ^ punctuation.delimiter
// ^ punctuation.delimiter
using static System.Console;
// <- keyword
// ^ punctuation.delimiter
// ^ punctuation.delimiter
using X = System.Console;
// <- keyword
// ^ operator
// ^ punctuation.delimiter
// ^ punctuation.delimiter
global using A;
// <- keyword
// ^ keyword
// ^ punctuation.delimiter
global using static A.B;
// <- keyword
// ^ keyword
// ^ punctuation.delimiter
// ^ punctuation.delimiter
namespace Namespace
// <- keyword
// ^ module
{
// <- punctuation.bracket
using A;
// <- keyword
// ^ punctuation.delimiter
internal delegate void A(params int[] test);
// <- keyword
// ^ keyword
// ^ type.builtin
// ^ punctuation.bracket
// ^ keyword
// ^ type.builtin
// ^ punctuation.bracket
// ^ punctuation.bracket
public struct F<T> where T : struct { }
// <- keyword
// ^ keyword
// ^ type
// ^ operator
// ^ property.definition
// ^ operator
// ^ keyword
// ^ property.definition
// ^ operator
// ^ keyword
// ^ punctuation.bracket
// ^ punctuation.bracket
record struct F
// <- keyword
// ^ keyword
// ^ type
{
// <- punctuation.bracket
int Age { get; init; }
// <- type.builtin
// ^ variable
// ^ punctuation.bracket
// ^ keyword
// ^ punctuation.delimiter
// ^ keyword
// ^ punctuation.delimiter
// ^ punctuation.bracket
}
// <- punctuation.bracket
[Nice]
// <- punctuation.bracket
// <- attribute
// ^ punctuation.bracket
private record F<T1, T2> where T1 : I1, I2, new() where T2 : I2 { }
// <- keyword
// ^ keyword
// ^ type
// ^ operator
// ^ property.definition
// ^ punctuation.delimiter
// ^ property.definition
// ^ operator
// ^ keyword
// ^ property.definition
// ^ operator
// ^ type
// ^ punctuation.delimiter
// ^ type
// ^ punctuation.delimiter
// ^ keyword
// ^ punctuation.bracket
// ^ keyword
// ^ property.definition
// ^ operator
// ^ type
// ^ punctuation.bracket
// ^ punctuation.bracket
record Teacher(string FirstName, string LastName, string Subject) : Person(FirstName, LastName);
// <- keyword
// ^ type
// ^ punctuation.bracket
// ^ type.builtin
// ^ variable.parameter
// ^ punctuation.delimiter
// ^ type.builtin
// ^ variable.parameter
// ^ punctuation.delimiter
// ^ type.builtin
// ^ variable.parameter
// ^ punctuation.bracket
// ^ operator
// ^ punctuation.bracket
// ^ punctuation.delimiter
// ^ punctuation.bracket
enum B { Ten = 10, Twenty = 20 }
// <- keyword
// ^ type
// ^ punctuation.bracket
// ^ property.definition
// ^ operator
// ^ number
// ^ punctuation.delimiter
// ^ property.definition
// ^ operator
// ^ number
// ^ punctuation.bracket
public class F : object, IAlpha, IOmega { }
// <- keyword
// ^ keyword
// ^ type
// ^ operator
// ^ type.builtin
// ^ punctuation.delimiter
// ^ type
// ^ punctuation.delimiter
// ^ type
// ^ punctuation.bracket
// ^ punctuation.bracket
public partial class Class<in TParam> where TParam : class?, notnull, F?
// <- keyword
// ^ keyword
// ^ keyword
// ^ type
// ^ operator
// ^ keyword
// ^ property.definition
// ^ operator
// ^ keyword
// ^ property.definition
// ^ operator
// ^ keyword
// ^ operator
// ^ keyword
// ^ punctuation.delimiter
// ^ type
// ^ operator
{
// <- punctuation.bracket
public event EventHandler<T> SomeEvent { add { } remove { } }
// <- keyword
// ^ keyword
// ^ type
// ^ operator
// ^ type
// ^ operator
// ^ punctuation.bracket
// ^ keyword
// ^ punctuation.bracket
// ^ punctuation.bracket
// ^ keyword
// ^ punctuation.bracket
// ^ punctuation.bracket
// ^ punctuation.bracket
public readonly int _B;
// <- keyword
// ^ keyword
// ^ type.builtin
// ^ variable
// ^ variable
// ^ punctuation.delimiter
Int64 D_e_f, g;
// <- type
// ^ variable
// ^ variable
// ^ variable
// ^ variable
// ^ variable
// ^ punctuation.delimiter
// ^ variable
// ^ punctuation.delimiter
Tuple<char, Nullable<int>> z;
// <- type
// ^ operator
// ^ type.builtin
// ^ punctuation.delimiter
// ^ type
// ^ operator
// ^ type.builtin
// ^ operator
// ^ variable
// ^ punctuation.delimiter
[SomeAttribute]
// <- punctuation.bracket
// <- attribute
// ^ punctuation.bracket
public static int operator +(A a) { return 0; }
// <- keyword
// ^ keyword
// ^ type.builtin
// ^ keyword
// ^ operator
// ^ type
// ^ variable.parameter
// ^ punctuation.bracket
// ^ punctuation.bracket
// ^ keyword
// ^ number
// ^ punctuation.delimiter
// ^ punctuation.bracket
uint GetSet { get; set; }
// <- type.builtin
// ^ variable
// ^ punctuation.bracket
// ^ keyword
// ^ punctuation.delimiter
// ^ keyword
// ^ punctuation.delimiter
// ^ punctuation.bracket
static extern Foo() { }
// <- keyword
// ^ keyword
// ^ constructor
// ^ punctuation.bracket
// ^ punctuation.bracket
// ^ punctuation.bracket
extern ~Class() { }
// ^ operator
// ^ constructor
// ^ punctuation.bracket
// ^ punctuation.bracket
// ^ punctuation.bracket
public void Method()
// <- keyword
// ^ type.builtin
// ^ function
// ^ punctuation.bracket
{
// <- punctuation.bracket
const int dec = 1_2;
// <- keyword
// ^ type.builtin
// ^ variable
// ^ operator
// ^ number
// ^ number
// ^ number
// ^ punctuation.delimiter
const long hex = 0xf_1l;
// <- keyword
// ^ type.builtin
// ^ variable
// ^ operator
// ^ number
// ^ number
// ^ number
// ^ punctuation.delimiter
const long hex2 = 0Xffff;
// <- keyword
// ^ type.builtin
// ^ variable
// ^ operator
// ^ number
// ^ punctuation.delimiter
const long hex3 = 0x_0_f;
// <- keyword
// ^ type.builtin
// ^ variable
// ^ operator
// ^ number
// ^ number
// ^ number
// ^ number
// ^ number
// ^ punctuation.delimiter
const UInt64 dec = 1uL;
// <- keyword
// ^ type
// ^ variable
// ^ operator
// ^ number
// ^ punctuation.delimiter
const UInt16 bin = 0b0100_100;
// <- keyword
// ^ type
// ^ variable
// ^ operator
// ^ number
// ^ number
// ^ number
// ^ punctuation.delimiter
const UInt16 bin2 = 0B01010__10;
// <- keyword
// ^ type
// ^ variable
// ^ operator
// ^ number
// ^ number
// ^ number
// ^ punctuation.delimiter
const long bin3 = 0b_0_10;
// <- keyword
// ^ type.builtin
// ^ variable
// ^ operator
// ^ number
// ^ number
// ^ number
// ^ number
// ^ number
// ^ punctuation.delimiter
const bool t = true, u = false;
// <- keyword
// ^ type.builtin
// ^ variable
// ^ operator
// ^ constant.builtin
// ^ punctuation.delimiter
// ^ variable
// ^ operator
// ^ constant.builtin
// ^ punctuation.delimiter
const char c = 'a';
// <- keyword
// ^ type.builtin
// ^ variable
// ^ operator
// ^ string
// ^ string
// ^ string
const char esc = '\n';
// <- keyword
// ^ type.builtin
// ^ variable
// ^ operator
// ^ string
// ^ keyword
// ^ string
const char hex = '\xf09a';
// <- keyword
// ^ type.builtin
// ^ variable
// ^ operator
// ^ string
// ^ keyword
// ^ string
const char uni16 = '\ua0bf';
// <- keyword
// ^ type.builtin
// ^ variable
// ^ operator
// ^ string
// ^ keyword
// ^ string
const char uni32 = '\UA0BFf9ca';
// <- keyword
// ^ type.builtin
// ^ variable
// ^ operator
// ^ string
// ^ keyword
// ^ string
const float s = 012.23F;
// <- keyword
// ^ type.builtin
// ^ variable
// ^ operator
// ^ number
// ^ number
// ^ number
// ^ punctuation.delimiter
const float e = 1e6f;
// <- keyword
// ^ type.builtin
// ^ variable
// ^ operator
// ^ number
// ^ punctuation.delimiter
const Single en = 0e-1f;
// <- keyword
// ^ type
// ^ variable
// ^ operator
// ^ number
// ^ number
// ^ number
// ^ punctuation.delimiter
const Single ep = 1_1e+12f;
// <- keyword
// ^ type
// ^ variable
// ^ operator
// ^ number
// ^ number
// ^ number
// ^ number
// ^ number
// ^ punctuation.delimiter
const double d = 0.9_9d;
// <- keyword
// ^ type.builtin
// ^ variable
// ^ operator
// ^ number
// ^ number
// ^ number
// ^ number
// ^ number
// ^ punctuation.delimiter
const double e = .4_9d;
// <- keyword
// ^ type.builtin
// ^ variable
// ^ operator
// ^ number
// ^ number
// ^ number
// ^ number
// ^ punctuation.delimiter
const decimal m = 0_1_2.9m;
// <- keyword
// ^ type.builtin
// ^ variable
// ^ operator
// ^ number
// ^ number
// ^ number
// ^ number
// ^ number
// ^ number
// ^ number
// ^ punctuation.delimiter
const Decimal m2 = 102.349M;
// <- keyword
// ^ type
// ^ variable
// ^ operator
// ^ number
// ^ number
// ^ number
// ^ punctuation.delimiter
const string x = null;
// <- keyword
// ^ type.builtin
// ^ variable
// ^ operator
// ^ constant.builtin
// ^ punctuation.delimiter
String e = "";
// <- type
// ^ variable
// ^ operator
// ^ string
string s = "a";
// <- type.builtin
// ^ variable
// ^ operator
// ^ string
// ^ string
// ^ string
string m = "abc";
// <- type.builtin
// ^ variable
// ^ operator
// ^ string
// ^ string
// ^ string
string esc = "ab\"\t";
// <- type.builtin
// ^ variable
// ^ operator
// ^ string
// ^ string
// ^ keyword
// ^ keyword
// ^ string
string hex = "ab\x22r";
// <- type.builtin
// ^ variable
// ^ operator
// ^ string
// ^ string
// ^ keyword
// ^ keyword
// ^ string
int @var = @const;
// <- type.builtin
// ^ variable
// ^ variable
// ^ operator
// ^ punctuation.delimiter
var x = $"""The point {X}, {Y} is {Math.Sqrt(X * X + Y * Y)} from the origin""";
// <- type.builtin
// ^ variable
// ^ operator
// ^ punctuation.bracket
// ^ punctuation.bracket
// ^ punctuation.bracket
// ^ punctuation.bracket
// ^ punctuation.bracket
// ^ punctuation.delimiter
// ^ punctuation.bracket
// ^ variable
// ^ operator
// ^ variable
// ^ operator
// ^ variable
// ^ operator
// ^ variable
// ^ punctuation.bracket
List<int> numbers = new() { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
// <- type
// ^ operator
// ^ type.builtin
// ^ operator
// ^ variable
// ^ operator
// ^ keyword
// ^ punctuation.bracket
// ^ punctuation.bracket
// ^ number
// ^ punctuation.delimiter
// ^ number
// ^ punctuation.delimiter
// ^ number
// ^ punctuation.delimiter
// ^ number
// ^ punctuation.delimiter
// ^ number
// ^ punctuation.delimiter
// ^ number
// ^ punctuation.delimiter
// ^ number
// ^ punctuation.delimiter
// ^ number
// ^ punctuation.delimiter
// ^ number
// ^ punctuation.delimiter
// ^ number
// ^ punctuation.bracket
var query =
// <- type.builtin
// ^ variable
// ^ operator
from num in numbers
// <- keyword
// ^ variable
// ^ keyword
// ^ variable
where num < 3 || num > 7
// <- keyword
// ^ variable
// ^ operator
// ^ number
// ^ operator
// ^ variable
// ^ operator
// ^ number
orderby num ascending
select num;
// <- keyword
// ^ variable
// ^ punctuation.delimiter
var u = x is int?
// <- type.builtin
// ^ variable
// ^ operator
// ^ keyword
// ^ type.builtin
// ^ operator
? a
// <- operator
//^ variable
: b;
// <- operator
//^ variable
// ^ punctuation.delimiter
a = (B)c + (C)d;
// <- variable
//^ operator
// ^ punctuation.bracket
// ^ type
// ^ punctuation.bracket
// ^ variable
// ^ operator
// ^ punctuation.bracket
// ^ type
// ^ punctuation.bracket
// ^ variable
// ^ punctuation.delimiter
b = (float)a[0];
// <- variable
//^ operator
// ^ punctuation.bracket
// ^ type.builtin
// ^ punctuation.bracket
// ^ punctuation.bracket
// ^ number
// ^ punctuation.bracket
var x = new
// <- type.builtin
// ^ variable
// ^ operator
// ^ keyword
{
// <- punctuation.bracket
};
// <- punctuation.bracket
var three = checked(1 + 2);
// <- type.builtin
// ^ variable
// ^ operator
// ^ keyword
// ^ punctuation.bracket
// ^ number
// ^ operator
// ^ number
// ^ punctuation.bracket
var d = delegate (int a)
// <- type.builtin
// ^ variable
// ^ operator
// ^ keyword
// ^ punctuation.bracket
// ^ type.builtin
// ^ variable.parameter
// ^ punctuation.bracket
{
// <- punctuation.bracket
return a;
// <- keyword
// ^ variable
// ^ punctuation.delimiter
};
// <- punctuation.bracket
var l = (A a, B b) => { return a.c(b); };
// <- type.builtin
// ^ variable
// ^ operator
// ^ punctuation.bracket
// ^ type
// ^ variable.parameter
// ^ punctuation.delimiter
// ^ type
// ^ variable.parameter
// ^ punctuation.bracket
// ^ operator
// ^ punctuation.bracket
// ^ keyword
// ^ variable
// ^ punctuation.delimiter
// ^ function
// ^ punctuation.bracket
// ^ variable
// ^ punctuation.bracket
// ^ punctuation.bracket
int Add(int left, int right) => a + b;
// <- type.builtin
// ^ function
// ^ type.builtin
// ^ variable.parameter
// ^ type.builtin
// ^ variable.parameter
// ^ operator
// ^ operator
Do(async () => { });
//^ punctuation.bracket
// ^ keyword
// ^ punctuation.bracket
// ^ operator
// ^ punctuation.bracket
// ^ punctuation.bracket
var gp = __makeref(g);
// <- type.builtin
// ^ variable
// ^ operator
// ^ punctuation.bracket
// ^ punctuation.bracket
var z = typeof(List<string>.Enumerator);
// <- type.builtin
// ^ variable
// ^ operator
// ^ keyword
// ^ punctuation.bracket
// ^ type
// ^ operator
// ^ type.builtin
// ^ operator
// ^ punctuation.bracket
ref VeryLargeStruct reflocal = ref veryLargeStruct;
// <- keyword
// ^ variable
// ^ operator
// ^ keyword
// ^ punctuation.delimiter
ref var elementRef = ref arr[0];
// <- keyword
// ^ type.builtin
// ^ variable
// ^ operator
// ^ keyword
// ^ punctuation.bracket
// ^ number
// ^ punctuation.bracket
var x = name is (var a);
// <- type.builtin
// ^ variable
// ^ operator
// ^ keyword
// ^ punctuation.bracket
// ^ punctuation.bracket
var x = c is < '0' or >= 'A' and <= 'Z';
// <- type.builtin
// ^ variable
// ^ operator
// ^ keyword
// ^ operator
// ^ string
// ^ string
// ^ string
// ^ string
// ^ string
// ^ string
// ^ string
// ^ string
// ^ string
var x = !this.Call();
// <- type.builtin
// ^ variable
// ^ operator
// ^ operator
// ^ keyword
// ^ punctuation.delimiter
// ^ punctuation.bracket
}
// <- punctuation.bracket
void Sample()
// <- type.builtin
// ^ punctuation.bracket
{
// <- punctuation.bracket
while (true) break;
// <- keyword
// ^ punctuation.bracket
// ^ constant.builtin
// ^ punctuation.bracket
// ^ keyword
// ^ punctuation.delimiter
throw ex;
// <- keyword
// ^ punctuation.delimiter
do { } while (a);
// <- keyword
// ^ punctuation.bracket
// ^ punctuation.bracket
// ^ keyword
// ^ punctuation.bracket
// ^ punctuation.bracket
goto end;
// <- keyword
// ^ punctuation.delimiter
end:
// ^ operator
return;
// <- keyword
// ^ punctuation.delimiter
if (true) return 1;
// <- keyword
// ^ punctuation.bracket
// ^ constant.builtin
// ^ punctuation.bracket
// ^ keyword
// ^ number
// ^ punctuation.delimiter
else return 0;
// <- keyword
// ^ keyword
// ^ number
// ^ punctuation.delimiter
(string a, bool b) c = default;
// <- punctuation.bracket
// <- type.builtin
// ^ punctuation.delimiter
// ^ type.builtin
// ^ punctuation.bracket
// ^ variable
// ^ operator
// ^ keyword
// ^ punctuation.delimiter
switch (a, a)
// <- keyword
// ^ punctuation.bracket
// ^ punctuation.delimiter
// ^ punctuation.bracket
{
// <- punctuation.bracket
case (1, 1):
// <- keyword
// ^ punctuation.bracket
// ^ number
// ^ punctuation.delimiter
// ^ number
// ^ punctuation.bracket
return 1;
// <- keyword
// ^ number
// ^ punctuation.delimiter
default:
// <- keyword
// ^ operator
return 0;
// <- keyword
// ^ number
// ^ punctuation.delimiter
}
// <- punctuation.bracket
lock (this)
// <- keyword
// ^ punctuation.bracket
// ^ keyword
// ^ punctuation.bracket
{
// <- punctuation.bracket
return;
// <- keyword
// ^ punctuation.delimiter
}
// <- punctuation.bracket
yield return 1;
// <- keyword
// ^ keyword
// ^ number
// ^ punctuation.delimiter
using (Stream a = File.OpenRead("a"), b = new BinaryReader(a))
// <- keyword
// ^ punctuation.bracket
// ^ type
// ^ variable
// ^ operator
// ^ punctuation.delimiter
// ^ punctuation.bracket
// ^ string
// ^ string
// ^ variable
// ^ operator
// ^ keyword
// ^ punctuation.bracket
// ^ punctuation.bracket
{
// <- punctuation.bracket
return;
// <- keyword
// ^ punctuation.delimiter
}
// <- punctuation.bracket
foreach (var (x, y) in z)
// <- keyword
// ^ punctuation.bracket
// ^ type.builtin
// ^ punctuation.bracket
// ^ punctuation.delimiter
// ^ punctuation.bracket
// ^ keyword
// ^ punctuation.bracket
q += x;
// <- variable
//^ operator
// ^ variable
// ^ punctuation.delimiter
for (int x = 0; x < 100; x++)
// <- keyword
// ^ punctuation.bracket
// ^ type.builtin
// ^ variable
// ^ operator
// ^ number
// ^ punctuation.delimiter
// ^ variable
// ^ operator
// ^ number
// ^ punctuation.delimiter
// ^ variable
// ^ operator
{
// <- punctuation.bracket
z += x;
// <- variable
//^ operator
// ^ variable
// ^ punctuation.delimiter
}
// <- punctuation.bracket
dynamic dyn = "";
// <- type
// ^ variable
// ^ operator
// ^ string
}
// <- punctuation.bracket
string b(Object operation) =>
// <- type.builtin
// ^ punctuation.bracket
// ^ type
// ^ variable.parameter
// ^ punctuation.bracket
// ^ operator
operation switch
// <- variable
// ^ keyword
{
// <- punctuation.bracket
1 => "one",
// <- number
//^ operator
// ^ string
// ^ string
// ^ string
_ => "more",
//^ operator
// ^ string
// ^ string
// ^ string
};
// <- punctuation.bracket
}
// <- punctuation.bracket
}
// <- punctuation.bracket

View File

@ -0,0 +1,95 @@
using Namespace;
class C
{
void M()
{
// unary
a = +a;
// ^ operator
a = -a;
// ^ operator
a = !a;
// ^ operator
a = ~a;
// ^ operator
a = ++a;
// ^ operator
a = --a;
// ^ operator
a = a++;
// ^ operator
a = a--;
// ^ operator
a = a!;
// ^ operator
a = a++;
// ^ operator
a = a--;
// ^ operator
// binary
a = a + a;
// ^ operator
a = a - a;
// ^ operator
a = a * a;
// ^ operator
a = a / a;
// ^ operator
a = a % a;
// ^ operator
a = a & a;
// ^ operator
a = a | a;
// ^ operator
a = a ^ a;
// ^ operator
a = a >> a;
// ^ operator
a = a << a;
// ^ operator
a = a >>> a;
// ^ operator
a = a == b;
// ^ operator
a = a != b;
// ^ operator
a = a < b;
// ^ operator
a = a <= b;
// ^ operator
a = a > b;
// ^ operator
a = a >= b;
// ^ operator
// assignment binary
a += a;
//^ operator
a -= a;
//^ operator
a *= a;
//^ operator
a /= a;
//^ operator
a %= a;
//^ operator
a <<= a;
//^ operator
a >>= a;
//^ operator
a >>>= a;
//^ operator
// ternary
string y = x ? "foo" : "bar";
// ^ operator
// ^ operator
// misc
var l = (int i) => i;
// ^ operator
}
}

67
test/highlight/types.cs Normal file
View File

@ -0,0 +1,67 @@
class A : B, C
// ^ type
// ^ type
// ^ type
{
public void M()
{
int a;
// <- type.builtin
var a;
// <- type.builtin
int? a;
// <- type.builtin
// ^ operator
A? a;
// <- type
// <- operator
int* a;
// <- type.builtin
// ^ operator
A* a;
// <- type
// <- operator
ref A* a;
// <- keyword
// ^ type
// ^ operator
var a = x is int;
// ^ type.builtin
var a = x is A;
// ^
var a = x as int;
// ^ type.builtin
var a = x as A;
// ^ type
var a = (int)x;
// ^ type.builtin
var a = (A)x;
// ^ type
A<int, A> a = new A<int, A>();
// <- type
//^ type.builtin
// ^ type
// ^ type
// ^ type.builtin
// ^ type
}
}
record A(int a, B b) : B(), I;
// ^ type
// ^ type.builtin
// ^ type
// ^ type
// ^ type
record A : B, I;
// ^ type
// ^ type
// ^ type

10
test/highlight/var.cs Normal file
View File

@ -0,0 +1,10 @@
class var
// ^ type
{
void M()
{
var var = new var();
// <- type.builtin
// ^ variable
}
}

View File

@ -0,0 +1,25 @@
class A
{
public void M()
{
foreach (int i in new[] { 1 })
// ^ variable
{
int j = i;
// ^ variable
}
var x = from a in sourceA
// ^ variable
// ^ variable
join b in sourceB on a.FK equals b.PK
// ^ variable
// ^ variable
group a by a.X into g
// ^ variable
// ^ variable
orderby g ascending
// ^ variable
select new { A.A, B.B };
}
}

View File

@ -0,0 +1,14 @@
namespace World
{
class Hello {
static void Main(string []args)
{
Hello x = new Hello();
System.Console.WriteLine("Hello, world.");
}
}
interface Blah {
}
}

View File

@ -0,0 +1,2 @@
bin/
obj/

View File

@ -0,0 +1,26 @@
{
"version": "0.2.0",
"configurations": [
{
// Use IntelliSense to find out which attributes exist for C# debugging
// Use hover for the description of the existing attributes
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/bin/Debug/net6.0/Generator.dll",
"args": [],
"cwd": "${workspaceFolder}",
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
"console": "internalConsole",
"stopAtEntry": false
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach"
}
]
}

View File

@ -0,0 +1,41 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/Generator.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/Generator.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"--project",
"${workspaceFolder}/Generator.csproj"
],
"problemMatcher": "$msCompile"
}
]
}

View File

@ -0,0 +1,177 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
if (args.Length != 1)
{
Console.WriteLine("Add the path to the file as an argument. The path needs to be fully qualified and point to an existing file in [REPO ROOT]/test/highlight.");
return;
}
var filePath = args[0];
// Some basic tests on the path, so that we have a chance:
if (!filePath.Contains("/test/highlight/") ||
!filePath.EndsWith(".cs") ||
!File.Exists(filePath) ||
!Path.IsPathFullyQualified(filePath))
{
Console.WriteLine("The file needs to exist in [REPO ROOT]/test/highlight, and the path needs to be fully qualified.");
return;
}
// Random variable name prefix, so that we don't accidentally replace something in the file:
var idPrefix = "a" + new Random(filePath.GetHashCode()).NextInt64(10000000) + "_";
var originalLines = File.ReadAllLines(filePath);
/// <summary>
/// Adds tree-sitter highlighting comments to the input file.
/// Comments start with either `// <-` or `// ^`, depending on the position of the hiughlighted token.
/// For highlight category, a unique random identifier is used.
/// </summary>
void AddCommentsToFile()
{
var newLines = new List<string>();
var index = 0;
foreach (var line in originalLines)
{
newLines.Add(line);
var leadingWhitespaces = line[..^line.TrimStart().Length];
var first = true;
var position = leadingWhitespaces.Length;
while (position < line.Length)
{
var ch = line[position];
bool HandleToken(Func<char, bool> isOfType)
{
if (!isOfType(ch))
{
return false;
}
var variable = $"{idPrefix}{index++}";
if (first)
{
newLines.Add($"{leadingWhitespaces}// <- {variable}");
first = false;
}
else
{
var spacesLength = position - leadingWhitespaces.Length - 2;
if (spacesLength < 0)
{
// Handle case when the first two characters need different highlight categories:
// Shift // by one space to the right.
newLines.Add($"{leadingWhitespaces} // <- {variable}");
}
else
{
var spaces = new string(' ', position - leadingWhitespaces.Length - 2);
newLines.Add($"{leadingWhitespaces}//{spaces}^ {variable}");
}
}
while (position < line.Length && isOfType(line[position]))
{
position++;
}
return true;
}
// The below char methods are not exactly what we need for token parsing, but good enough.
// For example
// - `_abc` is an identifier, but has both letter and punctuation characters.
// - string literals are parsed pretty badly, considering they can have all sorts of characters, even spaces, on which we split.
if (!HandleToken(char.IsLetterOrDigit) &&
!HandleToken(c => char.IsPunctuation(c) || char.IsSymbol(c)))
{
position++;
}
}
}
File.WriteAllLines(filePath, newLines.ToArray());
}
string GetHighlighterOutput()
{
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "tree-sitter",
Arguments = $"test --filter skip-all-corpus-tests",
UseShellExecute = false,
RedirectStandardOutput = true,
WorkingDirectory = Path.GetFullPath(Path.Combine(filePath, "..", "..", "..")),
}
};
process.Start();
var output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
return output;
}
var regexWithHighlight = new Regex($@"Failure - row: \d+, column: \d+, expected highlight '{idPrefix}(\d+)', actual highlights: '(.*)'", RegexOptions.Compiled);
var regexWithNone = new Regex($@"Failure - row: \d+, column: \d+, expected highlight '{idPrefix}(\d+)', actual highlights: none.", RegexOptions.Compiled);
/// <summary>
/// Runs the tree-sitter test command, and tries to find a single highlighting failure.
/// If a failure is found, the category is extracted from the output, and the corresponding variable is replaced with the category.
/// </summary>
bool FindAndFixHighlightFailure()
{
Console.Write(".");
var output = GetHighlighterOutput();
if (output.IndexOf("✗") != output.LastIndexOf("✗"))
{
Console.WriteLine("\nThe tree-sitter test execution identified multiple files with failed highlighting. Aborting.");
File.WriteAllLines(filePath, originalLines);
Environment.Exit(1);
}
var match = regexWithHighlight.Match(output);
if (match.Success && match.Groups.Count == 3)
{
// Highlight found for position, so replace with expected category.
var variableCat = $"{idPrefix}{match.Groups[1].Captures[0].Value}";
var category = match.Groups[2].Captures[0].Value;
File.WriteAllText(filePath, File.ReadAllText(filePath).Replace(variableCat + "\n", category + "\n"));
return true;
}
match = regexWithNone.Match(output);
if (!match.Success || match.Groups.Count != 2)
{
// Couldn't match any of the expected patterns.
return false;
}
// No highlight found for position, so remove entire line.
var variableNone = $"{idPrefix}{match.Groups[1].Captures[0].Value}";
var lines = File.ReadAllLines(filePath).Where(line => !line.EndsWith(variableNone)).ToArray();
File.WriteAllLines(filePath, lines);
return true;
}
AddCommentsToFile();
Console.WriteLine("Calling tree-sitter highlighter several times. This might take a while.");
while (FindAndFixHighlightFailure())
{ }
Console.WriteLine("");
Console.WriteLine("Done modifying the input file. It may require some manual cleanup.");

View File

@ -0,0 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
</Project>

View File

@ -0,0 +1,5 @@
#!/bin/bash
ROOT="$(git rev-parse --show-toplevel)"
echo $ROOT/tools/highlight-test-generator/Generator.csproj
dotnet run --project $ROOT/tools/highlight-test-generator/Generator.csproj $1