Add 'vendor/tree-sitter-typescript/' from commit '83816f563c8d9d2f1b9c921206a7944d0c5904ad'

git-subtree-dir: vendor/tree-sitter-typescript
git-subtree-mainline: e96a724176
git-subtree-split: 83816f563c
ida_star
Wilfred Hughes 2021-09-05 19:05:06 +07:00
commit 008f55203e
50 changed files with 701163 additions and 0 deletions

@ -0,0 +1,5 @@
parser.c linguist-vendored
parser.h linguist-vendored
grammar.json linguist-vendored
tsx/examples/* linguist-vendored
typescript/examples/* linguist-vendored

@ -0,0 +1,31 @@
---
name: Bug report
about: Report unexpected parsing results
title: ''
labels: 'bug'
assignees: ''
---
The following piece of code is valid but it is parsed incorrectly:
```typescript
```
Here's a link to the TypeScript Playground showing that the snippet above is valid JavaScript or TypeScript:
<!-- Please check your code at https://www.typescriptlang.org/play
and paste the URL below. -->
<!-- Please run `tree-sitter parse YOUR_FILE` and show us the output. -->
The output of `tree-sitter parse` is the following:
```
```
<!-- If there is no `ERROR` or `MISSING` node in the output above,
explain what you were expecting: -->
<!-- Name of the broken/missing feature, link to official
documentation, and any other relevant info is appreciated: -->

@ -0,0 +1,13 @@
---
name: Feature request
about: Suggest an idea for this project
title: ''
labels: ''
assignees: ''
---
<!--
The tree-sitter-typescript project is a TypeScript and TSX parser only.
How can we improve it?
-->

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

@ -0,0 +1,29 @@
name: build
on:
push:
pull_request:
jobs:
build:
name: build
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Setup Node
uses: actions/setup-node@v2-beta
with:
node-version: '12'
- name: Display Node versions
run: |
node --version
npm --version
- name: Install dependencies
run: npm install
- name: Test corpus & parse examples
run: npm test

@ -0,0 +1,13 @@
Cargo.lock
node_modules
.node-version
target
build
*.log
Cargo.lock
package-lock.json
/test.ts
examples/desktop
examples/redux
examples/vscode
log.html

@ -0,0 +1,9 @@
corpus
examples
.travis.yml
appveyor.yml
.gitattributes
build
script
target
test.ts

@ -0,0 +1,31 @@
[package]
name = "tree-sitter-typescript"
description = "Typescript grammar for the tree-sitter parsing library"
version = "0.19.0"
authors = ["Max Brunsfeld <maxbrunsfeld@gmail.com>"]
license = "MIT"
readme = "README.md"
keywords = ["incremental", "parsing", "typescript", "tsx"]
categories = ["parsing", "text-editors"]
repository = "https://github.com/tree-sitter/tree-sitter-typescript"
edition = "2018"
build = "bindings/rust/build.rs"
include = [
"common",
"bindings/rust",
"typescript/grammar.js",
"typescript/src",
"tsx/grammar.js",
"tsx/src",
"queries"
]
[lib]
path = "bindings/rust/lib.rs"
[dependencies]
tree-sitter = "0.19"
[build-dependencies]
cc = "1.0"

@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2017 GitHub
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.

@ -0,0 +1,20 @@
tree-sitter-typescript
===========================
[![Build Status](https://github.com/tree-sitter/tree-sitter-typescript/workflows/build/badge.svg)](https://github.com/tree-sitter/tree-sitter-typescript/actions?query=workflow%3Abuild)
[![Build status](https://ci.appveyor.com/api/projects/status/rn11gs5y3tm7tuy0/branch/master?svg=true)](https://ci.appveyor.com/project/maxbrunsfeld/tree-sitter-typescript/branch/master)
TypeScript and TSX grammars for [tree-sitter][].
Because TSX and TypeScript are actually two different dialects, this module defines two grammars. Require them as follows:
```js
require('tree-sitter-typescript').typescript; // TypeScript grammar
require('tree-sitter-typescript').tsx; // TSX grammar
```
[tree-sitter]: https://github.com/tree-sitter/tree-sitter
References
* [TypeScript Language Spec](https://github.com/microsoft/TypeScript/blob/main/doc/spec-ARCHIVED.md)

@ -0,0 +1,22 @@
image: Visual Studio 2015
environment:
nodejs_version: "8"
platform:
- x64
install:
- ps: Install-Product node $env:nodejs_version
- node --version
- npm --version
- npm install
test_script:
- npm run test-windows
build: off
branches:
only:
- master

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

@ -0,0 +1,37 @@
#include "tree_sitter/parser.h"
#include <node.h>
#include "nan.h"
using namespace v8;
extern "C" TSLanguage * tree_sitter_typescript();
extern "C" TSLanguage * tree_sitter_tsx();
namespace {
NAN_METHOD(New) {}
void Init(Local<Object> exports, Local<Object> module) {
Local<FunctionTemplate> ts_tpl = Nan::New<FunctionTemplate>(New);
ts_tpl->SetClassName(Nan::New("Language").ToLocalChecked());
ts_tpl->InstanceTemplate()->SetInternalFieldCount(1);
Local<Function> ts_constructor = Nan::GetFunction(ts_tpl).ToLocalChecked();
Local<Object> ts_instance = ts_constructor->NewInstance(Nan::GetCurrentContext()).ToLocalChecked();
Nan::SetInternalFieldPointer(ts_instance, 0, tree_sitter_typescript());
Nan::Set(ts_instance, Nan::New("name").ToLocalChecked(), Nan::New("typescript").ToLocalChecked());
Local<FunctionTemplate> tsx_tpl = Nan::New<FunctionTemplate>(New);
tsx_tpl->SetClassName(Nan::New("Language").ToLocalChecked());
tsx_tpl->InstanceTemplate()->SetInternalFieldCount(1);
Local<Function> tsx_constructor = Nan::GetFunction(tsx_tpl).ToLocalChecked();
Local<Object> tsx_instance = tsx_constructor->NewInstance(Nan::GetCurrentContext()).ToLocalChecked();
Nan::SetInternalFieldPointer(tsx_instance, 0, tree_sitter_tsx());
Nan::Set(tsx_instance, Nan::New("name").ToLocalChecked(), Nan::New("tsx").ToLocalChecked());
Nan::Set(exports, Nan::New("typescript").ToLocalChecked(), ts_instance);
Nan::Set(exports, Nan::New("tsx").ToLocalChecked(), tsx_instance);
}
NODE_MODULE(tree_sitter_typescript_binding, Init)
} // namespace

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

@ -0,0 +1 @@
module.exports = require('./index').tsx;

@ -0,0 +1 @@
module.exports = require('./index').typescript;

@ -0,0 +1,28 @@
fn main() {
let root_dir = std::path::Path::new(".");
let typescript_dir = root_dir.join("typescript").join("src");
let tsx_dir = root_dir.join("tsx").join("src");
let mut config = cc::Build::new();
config.include(&typescript_dir);
config
.flag_if_supported("-Wno-unused-parameter")
.flag_if_supported("-Wno-unused-but-set-variable");
for path in &[
typescript_dir.join("parser.c"),
typescript_dir.join("scanner.c"),
tsx_dir.join("parser.c"),
tsx_dir.join("scanner.c"),
] {
config.file(&path);
println!("cargo:rerun-if-changed={}", path.to_str().unwrap());
}
println!(
"cargo:rerun-if-changed={}",
root_dir.join("common").join("scanner.h").to_str().unwrap()
);
config.compile("parser-scanner");
}

@ -0,0 +1,62 @@
//! This crate provides Typescript and TSX grammars for the [tree-sitter][] parsing library.
//!
//! Typically, you will use the [language_typescript][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#"
//! function double(x: number): number {
//! return x * 2;
//! }
//! "#;
//! let mut parser = Parser::new();
//! parser
//! .set_language(tree_sitter_typescript::language_typescript())
//! .expect("Error loading typescript grammar");
//! let parsed = parser.parse(code, None).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_typescript.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_typescript() -> Language;
fn tree_sitter_tsx() -> Language;
}
/// Returns the tree-sitter [Language][] for this Typescript.
///
/// [Language]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Language.html
pub fn language_typescript() -> Language {
unsafe { tree_sitter_typescript() }
}
/// Returns the tree-sitter [Language][] for TSX.
///
/// [Language]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Language.html
pub fn language_tsx() -> Language {
unsafe { tree_sitter_tsx() }
}
/// The syntax highlighting query for this language.
pub const HIGHLIGHT_QUERY: &str = include_str!("../../queries/highlights.scm");
/// The local-variable syntax highlighting query for this language.
pub const LOCALS_QUERY: &str = include_str!("../../queries/locals.scm");
/// The symbol tagging query for this language.
pub const TAGGING_QUERY: &str = include_str!("../../queries/tags.scm");
/// The content of the [`node-types.json`][] file for this grammar.
///
/// [`node-types.json`]: https://tree-sitter.github.io/tree-sitter/using-parsers#static-node-types
pub const TYPESCRIPT_NODE_TYPES: &str = include_str!("../../typescript/src/node-types.json");
pub const TSX_NODE_TYPES: &str = include_str!("../../tsx/src/node-types.json");

@ -0,0 +1,879 @@
==================================
Ambient declarations
==================================
declare class Error {
constructor: Function
}
declare var foo: number;
declare const bar = "baz";
declare function greet(greeting: string): void;
declare namespace myLib {
function makeGreeting(s: string): string;
let numberOfGreetings: number;
interface LogOptions {
verbose?: boolean;
}
interface AlertOptions {
modal: boolean;
title?: string;
color?: string;
}
}
declare class Greeter {
constructor(greeting: string);
greeting: string;
showGreeting(): void;
}
declare module Foo.Bar { export var foo; };
declare module Foo {
break;
continue;
debugger;
do { } while (true);
for (x in null) { }
for (;;) { }
if (true) { } else { }
1;
return;
switch (x) {
case 1:
break;
default:
break;
}
throw "hello";
try { }
catch (e) { }
finally { }
}
---
(program
(ambient_declaration
(class_declaration
name: (type_identifier)
body: (class_body
(public_field_definition
name: (property_identifier)
type: (type_annotation (type_identifier))))))
(ambient_declaration
(variable_declaration
(variable_declarator
name: (identifier)
type: (type_annotation (predefined_type)))))
(ambient_declaration
(lexical_declaration
(variable_declarator
name: (identifier)
value: (string (string_fragment)))))
(ambient_declaration
(function_signature
name: (identifier)
parameters: (formal_parameters (required_parameter (identifier) (type_annotation (predefined_type))))
return_type: (type_annotation (predefined_type))))
(ambient_declaration
(internal_module
name: (identifier)
body: (statement_block
(function_signature
name: (identifier)
parameters: (formal_parameters (required_parameter (identifier) (type_annotation (predefined_type))))
return_type: (type_annotation (predefined_type)))
(lexical_declaration (variable_declarator
name: (identifier)
type: (type_annotation (predefined_type))))
(interface_declaration
name: (type_identifier)
body: (object_type
(property_signature
name: (property_identifier)
type: (type_annotation (predefined_type)))))
(interface_declaration
name: (type_identifier)
body: (object_type
(property_signature name: (property_identifier) type: (type_annotation (predefined_type)))
(property_signature name: (property_identifier) type: (type_annotation (predefined_type)))
(property_signature name: (property_identifier) type: (type_annotation (predefined_type))))))))
(ambient_declaration
(class_declaration
name: (type_identifier)
body: (class_body
(method_signature
name: (property_identifier)
parameters: (formal_parameters
(required_parameter (identifier) (type_annotation (predefined_type)))))
(public_field_definition
name: (property_identifier)
type: (type_annotation (predefined_type)))
(method_signature
name: (property_identifier)
parameters: (formal_parameters)
return_type: (type_annotation (predefined_type))))))
(ambient_declaration
(module
name: (nested_identifier (identifier) (identifier))
body: (statement_block
(export_statement
declaration: (variable_declaration (variable_declarator name: (identifier)))))))
(empty_statement)
(ambient_declaration
(module
name: (identifier)
body: (statement_block
(break_statement)
(continue_statement)
(debugger_statement)
(do_statement
body: (statement_block)
condition: (parenthesized_expression (true)))
(for_in_statement
left: (identifier)
right: (null)
body: (statement_block))
(for_statement
initializer: (empty_statement)
condition: (empty_statement)
body: (statement_block))
(if_statement
condition: (parenthesized_expression (true))
consequence: (statement_block)
alternative: (else_clause (statement_block)))
(expression_statement (number))
(return_statement)
(switch_statement
value: (parenthesized_expression (identifier))
body: (switch_body
(switch_case value: (number) (break_statement))
(switch_default (break_statement))))
(throw_statement (string (string_fragment)))
(try_statement
body: (statement_block)
handler: (catch_clause
parameter: (identifier)
body: (statement_block))
finalizer: (finally_clause body: (statement_block)))
)
)
)
)
==================================================
Exception handling
==================================================
try {}
catch (e: unknown) {}
finally {}
---
(program
(try_statement
body: (statement_block)
handler: (catch_clause
parameter: (identifier)
type: (type_annotation (type_identifier))
body: (statement_block)
)
finalizer: (finally_clause body: (statement_block))
)
)
==================================================
Flow-style ambient class declarations with commas
==================================================
declare interface IFoo {
bar(): number,
baz(): IBaz,
}
declare class Foo {
bar(): number,
baz(): Baz,
}
---
(program
(ambient_declaration
(interface_declaration (type_identifier) (object_type
(method_signature
(property_identifier)
(formal_parameters)
(type_annotation (predefined_type)))
(method_signature
(property_identifier)
(formal_parameters)
(type_annotation (type_identifier))))))
(ambient_declaration
(class_declaration (type_identifier) (class_body
(method_signature
(property_identifier)
(formal_parameters)
(type_annotation (predefined_type)))
(method_signature
(property_identifier)
(formal_parameters)
(type_annotation (type_identifier)))))))
==================================
Flow module.exports declarations
==================================
declare module.exports: {
foo: string;
}
---
(program
(ambient_declaration (property_identifier) (object_type
(property_signature (property_identifier) (type_annotation (predefined_type))))))
==================================
Ambient exports
==================================
export default function point(x: number, y: number) {
return { x, y };
}
// a comment
export default class A {}
export async function readFile(filename: string): Promise<Buffer>
---
(program
(export_statement
(function_declaration
(identifier)
(formal_parameters
(required_parameter (identifier) (type_annotation (predefined_type)))
(required_parameter (identifier) (type_annotation (predefined_type))))
(statement_block
(return_statement (object (shorthand_property_identifier) (shorthand_property_identifier))))))
(comment)
(export_statement (class_declaration (type_identifier) (class_body)))
(export_statement (function_signature
(identifier)
(formal_parameters (required_parameter (identifier) (type_annotation (predefined_type))))
(type_annotation (generic_type (type_identifier) (type_arguments (type_identifier)))))))
==================================
Typeof types
==================================
declare class Linter {
static findConfiguration: typeof findConfiguration;
}
---
(program
(ambient_declaration
(class_declaration
(type_identifier)
(class_body
(public_field_definition
(property_identifier)
(type_annotation (type_query (identifier))))))))
==================================
Export assignments
==================================
export = Linter;
---
(program (export_statement (identifier)))
==================================
Import aliases
==================================
import r = X.N;
---
(program
(import_alias (identifier) (nested_identifier (identifier) (identifier))))
==================================
Import aliases in modules
==================================
module C {
import r = X.N;
}
---
(program
(module (identifier) (statement_block
(import_alias (identifier) (nested_identifier (identifier) (identifier))))))
==================================
Export import aliases
==================================
module M {
export module N {
}
export import X = N;
}
---
(program
(module (identifier) (statement_block
(export_statement (module (identifier) (statement_block)))
(export_statement (import_alias (identifier) (identifier))))))
==================================
Property signatures with accessibility modifiers
==================================
export interface IAppState {
public readonly users: ReadonlyArray<User>
}
export class CloningRepository {
public readonly id = CloningRepositoryID++
}
---
(program
(export_statement
(interface_declaration
(type_identifier)
(object_type
(property_signature
(accessibility_modifier)
(property_identifier)
(type_annotation (generic_type (type_identifier) (type_arguments (type_identifier))))))))
(export_statement
(class_declaration
(type_identifier)
(class_body
(public_field_definition
(accessibility_modifier)
(property_identifier) (update_expression (identifier)))))))
==================================
Ambient type declarations
==================================
declare type IndexableType = string | number | Date | Array<string | number | Date>;
---
(program
(ambient_declaration
(type_alias_declaration
(type_identifier)
(union_type
(union_type
(union_type (predefined_type) (predefined_type))
(type_identifier))
(generic_type
(type_identifier)
(type_arguments (union_type (union_type (predefined_type) (predefined_type)) (type_identifier))))))))
==================================
Ambient module declarations
==================================
module Promise {
var on: {}
export function resolve<R>(value?: Thenable<R>): Promise<R>;
}
declare module "example"
declare module "example" { }
---
(program
(module (identifier) (statement_block
(variable_declaration (variable_declarator
(identifier)
(type_annotation (object_type))))
(export_statement (function_signature
(identifier)
(type_parameters (type_parameter (type_identifier)))
(formal_parameters
(optional_parameter
(identifier)
(type_annotation (generic_type (type_identifier) (type_arguments (type_identifier))))))
(type_annotation (generic_type (type_identifier) (type_arguments (type_identifier))))))))
(ambient_declaration (module (string (string_fragment))))
(ambient_declaration (module (string (string_fragment)) (statement_block))))
=================================
Accessibility modifiers as pair keywords
=================================
x = { name, description, private: private_ }
---
(program (expression_statement (assignment_expression
(identifier)
(object
(shorthand_property_identifier)
(shorthand_property_identifier)
(pair (property_identifier) (identifier))))))
=================================
Type casts
=================================
foo as any as Array<number>
---
(program
(expression_statement
(as_expression
(as_expression (identifier) (predefined_type))
(generic_type (type_identifier) (type_arguments (predefined_type))))))
=================================
Ambient export function declarations
=================================
export interface Foo {
export function OrderedMap<K, V>(iter: Iterable.Keyed<K, V>): OrderedMap<K, V>;
}
---
(program
(export_statement
(interface_declaration
(type_identifier)
(object_type
(export_statement
(function_signature
(identifier)
(type_parameters (type_parameter (type_identifier)) (type_parameter (type_identifier)))
(formal_parameters
(required_parameter
(identifier)
(type_annotation
(generic_type
(nested_type_identifier (identifier) (type_identifier))
(type_arguments (type_identifier) (type_identifier))))))
(type_annotation (generic_type (type_identifier) (type_arguments (type_identifier) (type_identifier))))))))))
=================================
Ambient type alias declarations in namespaces
=================================
declare namespace moment {
type formatFunction = () => string;
export var x: string;
export class foo {
}
export function utc(): Moment;
export const enum Blah { Blaz, Bloz, Bleez }
}
---
(program
(ambient_declaration
(internal_module (identifier) (statement_block
(type_alias_declaration (type_identifier) (function_type (formal_parameters) (predefined_type)))
(export_statement (variable_declaration (variable_declarator (identifier) (type_annotation (predefined_type)))))
(export_statement (class_declaration (type_identifier) (class_body)))
(export_statement
(function_signature
(identifier)
(formal_parameters)
(type_annotation (type_identifier))))
(export_statement (enum_declaration (identifier) (enum_body
(property_identifier)
(property_identifier)
(property_identifier))))))))
=================================
Export interfaces in namespaces
=================================
declare namespace Foo {
export interface Bar {
}
}
---
(program
(ambient_declaration
(internal_module
(identifier)
(statement_block (export_statement (interface_declaration (type_identifier) (object_type)))))))
=================================
Namespaces as internal modules
=================================
namespace Foo {
}
namespace Bar {
var x;
}
---
(program
(expression_statement
(internal_module (identifier) (statement_block)))
(expression_statement
(internal_module (identifier) (statement_block
(variable_declaration (variable_declarator (identifier)))))))
===========================================
Method declarations with keywords as names
===========================================
class Foo {
private async() {};
get(): Result {};
private set(plugin) {};
}
---
(program
(class_declaration
(type_identifier)
(class_body
(method_definition
(accessibility_modifier)
(property_identifier)
(formal_parameters)
(statement_block))
(method_definition
(property_identifier)
(formal_parameters)
(type_annotation (type_identifier))
(statement_block))
(method_definition
(accessibility_modifier)
(property_identifier)
(formal_parameters (required_parameter (identifier)))
(statement_block)))))
=======================================
Classes with method signatures
=======================================
class Foo {
public async waitFor<T>(func: () => T | Promise<T | undefined>, accept?: (result: T) => boolean | Promise<boolean>, timeoutMessage?: string, retryCount?: number): Promise<T>;
public readonly async bar?<T>();
private static bar();
private static async bar(): T;
}
---
(program
(class_declaration
(type_identifier)
(class_body
(method_signature
(accessibility_modifier)
(property_identifier)
(type_parameters (type_parameter (type_identifier)))
(formal_parameters
(required_parameter
(identifier)
(type_annotation (function_type
(formal_parameters)
(union_type (type_identifier) (generic_type (type_identifier) (type_arguments (union_type (type_identifier) (type_identifier))))))))
(optional_parameter
(identifier)
(type_annotation (function_type
(formal_parameters (required_parameter (identifier) (type_annotation (type_identifier))))
(union_type (predefined_type) (generic_type (type_identifier) (type_arguments (predefined_type)))))))
(optional_parameter
(identifier)
(type_annotation (predefined_type)))
(optional_parameter
(identifier)
(type_annotation (predefined_type))))
(type_annotation (generic_type (type_identifier) (type_arguments (type_identifier)))))
(method_signature
(accessibility_modifier)
(property_identifier)
(type_parameters (type_parameter (type_identifier)))
(formal_parameters))
(method_signature
(accessibility_modifier)
(property_identifier)
(formal_parameters))
(method_signature
(accessibility_modifier)
(property_identifier)
(formal_parameters)
(type_annotation (type_identifier))))))
=======================================
Classes with property names as strings or numbers
=======================================
class Foo {
static 2: string;
public static 2: string = 'string';
public static readonly 'hello'?: int = 'string';
static readonly 'hello'?: int = 'string';
readonly 'hello'?: int = 'string';
}
---
(program
(class_declaration
(type_identifier)
(class_body
(public_field_definition (number) (type_annotation (predefined_type)))
(public_field_definition (accessibility_modifier) (number) (type_annotation (predefined_type)) (string (string_fragment)))
(public_field_definition (accessibility_modifier) (string (string_fragment)) (type_annotation (type_identifier)) (string (string_fragment)))
(public_field_definition (string (string_fragment)) (type_annotation (type_identifier)) (string (string_fragment)))
(public_field_definition (string (string_fragment)) (type_annotation (type_identifier)) (string (string_fragment))))))
=======================================
Classes with decorators
=======================================
@baz @bam class Foo {
@foo static 2: string;
@bar.buzz(grue) public static 2: string = 'string';
@readonly readonly 'hello'?: int = 'string';
@readonly fooBar(@required param: any, @optional param2?: any) {
}
}
---
(program
(class_declaration
(decorator (identifier))
(decorator (identifier))
(type_identifier)
(class_body
(decorator (identifier))
(public_field_definition (number) (type_annotation (predefined_type)))
(decorator (call_expression (member_expression (identifier) (property_identifier)) (arguments (identifier))))
(public_field_definition (accessibility_modifier) (number) (type_annotation (predefined_type)) (string (string_fragment)))
(decorator (identifier))
(public_field_definition (string (string_fragment)) (type_annotation (type_identifier)) (string (string_fragment)))
(decorator (identifier))
(method_definition
(property_identifier)
(formal_parameters
(required_parameter
(decorator (identifier))
(identifier)
(type_annotation (predefined_type)))
(optional_parameter
(decorator (identifier))
(identifier)
(type_annotation (predefined_type))))
(statement_block)))))
=======================================
Classes with methods with and without trailing semicolons on one line
=======================================
class Foo<Bar> extends Baz { bar = 5; static one(a) { return a; }; two(b) { return b; } three(c) { return c; } }
---
(program
(class_declaration
(type_identifier)
(type_parameters (type_parameter (type_identifier)))
(class_heritage (extends_clause (type_identifier)))
(class_body
(public_field_definition (property_identifier) (number))
(method_definition (property_identifier)
(formal_parameters (required_parameter (identifier)))
(statement_block (return_statement (identifier))))
(method_definition
(property_identifier)
(formal_parameters (required_parameter (identifier)))
(statement_block (return_statement (identifier))))
(method_definition
(property_identifier)
(formal_parameters (required_parameter (identifier)))
(statement_block (return_statement (identifier)))))))
=======================================
Global namespace declarations
=======================================
declare global {
}
---
(program (ambient_declaration (statement_block)))
=======================================
Abstract classes
=======================================
abstract class Foo {
}
abstract class Animal {
readonly abstract prop: string;
requiredProp!: string;
abstract makeSound(): void;
abstract get readonlyProp(): string;
protected abstract readonlyProp?(): string;
move(): void {
console.log("roaming the earth...");
}
}
@bar
abstract class Foo {
}
---
(program
(abstract_class_declaration
name: (type_identifier)
body: (class_body))
(abstract_class_declaration
name: (type_identifier)
body: (class_body
(public_field_definition
name: (property_identifier)
type: (type_annotation (predefined_type)))
(public_field_definition
name: (property_identifier)
type: (type_annotation (predefined_type)))
(abstract_method_signature
name: (property_identifier)
parameters: (formal_parameters)
return_type: (type_annotation (predefined_type)))
(abstract_method_signature
name: (property_identifier)
parameters: (formal_parameters)
return_type: (type_annotation (predefined_type)))
(abstract_method_signature
(accessibility_modifier)
name: (property_identifier)
parameters: (formal_parameters)
return_type: (type_annotation (predefined_type)))
(method_definition
name: (property_identifier)
parameters: (formal_parameters)
return_type: (type_annotation (predefined_type))
body: (statement_block
(expression_statement (call_expression
function: (member_expression
object: (identifier)
property: (property_identifier))
arguments: (arguments (string (string_fragment)))))))))
(abstract_class_declaration
decorator: (decorator (identifier))
name: (type_identifier)
body: (class_body)))
==================================
Index type queries
==================================
export type Extracted = keyof Pick<Base, "id">
---
(program
(export_statement
declaration: (type_alias_declaration
name: (type_identifier)
value: (index_type_query
(generic_type
(type_identifier)
(type_arguments
(type_identifier)
(literal_type
(string (string_fragment)))))))))
==================================
Definite assignment assertions
==================================
var a!: b;
let a!: b;
---
(program
(variable_declaration
(variable_declarator
(identifier)
(type_annotation
(type_identifier))))
(lexical_declaration
(variable_declarator
(identifier)
(type_annotation
(type_identifier)))))
====================================
Top-level exports
====================================
export default abstract class C { }
export default class C { }
export class C { }
export default interface I { }
export interface I { }
---
(program
(export_statement (abstract_class_declaration (type_identifier) (class_body)))
(export_statement (class_declaration (type_identifier) (class_body)))
(export_statement (class_declaration (type_identifier) (class_body)))
(export_statement (interface_declaration (type_identifier) (object_type)))
(export_statement (interface_declaration (type_identifier) (object_type))))

@ -0,0 +1,177 @@
==================================
As expressions
==================================
h as `hello`
T as {} & { [t: T]: T }
T as {} & { [t: T]: T } & { [g: G]: G }
---
(program
(expression_statement (as_expression (identifier) (template_string)))
(expression_statement
(as_expression
(identifier)
(intersection_type
(object_type)
(object_type
(index_signature
(identifier)
(type_identifier)
(type_annotation (type_identifier)))))))
(expression_statement
(as_expression
(identifier)
(intersection_type
(intersection_type
(object_type)
(object_type
(index_signature
(identifier)
(type_identifier)
(type_annotation (type_identifier)))))
(object_type
(index_signature
(identifier)
(type_identifier)
(type_annotation (type_identifier))))))))
==================================
Typeof expressions
==================================
typeof class {} === "function";
typeof module === "object" && typeof module.exports === "object"
---
(program
(expression_statement
(binary_expression
(unary_expression (class (class_body)))
(string (string_fragment))))
(expression_statement
(binary_expression
(binary_expression (unary_expression (identifier)) (string (string_fragment)))
(binary_expression (unary_expression (member_expression (identifier) (property_identifier))) (string (string_fragment))))))
==================================
Array with empty elements
==================================
[, a, , b, , , , s, , , ]
---
(program
(expression_statement
(array (identifier) (identifier) (identifier))))
==================================
Variable named 'module'
==================================
var module;
module;
---
(program
(variable_declaration (variable_declarator (identifier)))
(expression_statement (identifier)))
==================================
Multi-line variable declarations
==================================
var a = b
, c = d
, e = f
---
(program (variable_declaration (variable_declarator (identifier) (identifier)) (variable_declarator (identifier) (identifier)) (variable_declarator (identifier) (identifier))))
=====================================
The 'less than' operator
=====================================
i < foo.length;
i < type.length;
i < string.length;
---
(program
(expression_statement (binary_expression (identifier) (member_expression (identifier) (property_identifier))))
(expression_statement (binary_expression (identifier) (member_expression (identifier) (property_identifier))))
(expression_statement (binary_expression (identifier) (member_expression (identifier) (property_identifier)))))
=====================================
Subscript expressions in if statements
=====================================
if ( foo ) {
set[ 1 ].apply()
}
---
(program
(if_statement
(parenthesized_expression (identifier))
(statement_block
(expression_statement
(call_expression
(member_expression
(subscript_expression (identifier) (number)) (property_identifier))
(arguments))))))
====================================
Objects with reserved words as keys
====================================
{
public: true,
private: true,
readonly: true
}
---
(program (expression_statement (object
(pair key: (property_identifier) value: (true))
(pair key: (property_identifier) value: (true))
(pair key: (property_identifier) value: (true)))))
====================================
Assignment to non-null LHS
====================================
foo! = bar;
foo! += bar;
(foo)! = bar;
(foo)! += bar;
---
(program
(expression_statement
(assignment_expression
(non_null_expression (identifier))
(identifier)))
(expression_statement
(augmented_assignment_expression
(non_null_expression (identifier))
(identifier)))
(expression_statement
(assignment_expression
(non_null_expression (parenthesized_expression (identifier)))
(identifier)))
(expression_statement
(augmented_assignment_expression
(non_null_expression
(parenthesized_expression (identifier)))
(identifier))))

@ -0,0 +1,323 @@
==================================
Functions with typed parameters
==================================
function greeter(person: string) {
return "Hello, " + person;
}
function foo<T>(x: T): T {
}
function foo<T, U>(a: T[], f: (x: T) => U): U[] {
}
function foo<T, U>(this: T[]): U[] {
return []
}
---
(program
(function_declaration
name: (identifier)
parameters: (formal_parameters (required_parameter (identifier) (type_annotation (predefined_type))))
body: (statement_block
(return_statement (binary_expression
left: (string (string_fragment))
right: (identifier)))))
(function_declaration
name: (identifier)
type_parameters: (type_parameters (type_parameter (type_identifier)))
parameters: (formal_parameters (required_parameter (identifier) (type_annotation (type_identifier))))
return_type: (type_annotation (type_identifier))
body: (statement_block))
(function_declaration
name: (identifier)
type_parameters: (type_parameters (type_parameter (type_identifier)) (type_parameter (type_identifier)))
parameters: (formal_parameters
(required_parameter
(identifier)
(type_annotation (array_type (type_identifier))))
(required_parameter
(identifier)
(type_annotation
(function_type
(formal_parameters (required_parameter (identifier) (type_annotation (type_identifier))))
(type_identifier)))))
return_type: (type_annotation (array_type (type_identifier)))
body: (statement_block))
(function_declaration
name: (identifier)
type_parameters: (type_parameters (type_parameter (type_identifier)) (type_parameter (type_identifier)))
parameters: (formal_parameters
(required_parameter
(this)
(type_annotation (array_type (type_identifier)))))
return_type: (type_annotation (array_type (type_identifier)))
body: (statement_block (return_statement (array)))))
==================================
New object with type arguments
==================================
const lines = new Array<DiffLine>()
---
(program
(lexical_declaration
(variable_declarator
(identifier)
(new_expression
(identifier)
(type_arguments (type_identifier))
(arguments)))))
===================================================
Function calls with array and tuple type arguments
===================================================
a.b<[C]>();
a<C.D[]>();
---
(program
(expression_statement
(call_expression
function: (member_expression
object: (identifier)
property: (property_identifier))
type_arguments: (type_arguments (tuple_type (type_identifier)))
arguments: (arguments)))
(expression_statement
(call_expression
function: (identifier)
type_arguments: (type_arguments
(array_type (nested_type_identifier
module: (identifier)
name: (type_identifier))))
arguments: (arguments))))
=========================================================
Function calls with optional chaining and type arguments
=========================================================
A?.<B>();
---
(program
(expression_statement
(call_expression
(identifier)
(type_arguments (type_identifier))
(arguments))))
==================================
Arrow functions and generators with call signatures
==================================
<A>(amount, interestRate, duration): number => 2
function* foo<A>(amount, interestRate, duration): number {
yield amount * interestRate * duration / 12
}
(module: any): number => 2
---
(program
(expression_statement
(arrow_function
(type_parameters (type_parameter (type_identifier)))
(formal_parameters
(required_parameter (identifier))
(required_parameter (identifier))
(required_parameter (identifier)))
(type_annotation (predefined_type))
(number)))
(generator_function_declaration
(identifier)
(type_parameters (type_parameter (type_identifier)))
(formal_parameters
(required_parameter (identifier))
(required_parameter (identifier))
(required_parameter (identifier)))
(type_annotation (predefined_type))
(statement_block
(expression_statement (yield_expression (binary_expression
(binary_expression (binary_expression (identifier) (identifier)) (identifier))
(number))))))
(expression_statement
(arrow_function
(formal_parameters (required_parameter (identifier) (type_annotation (predefined_type))))
(type_annotation (predefined_type))
(number))))
==================================
Arrow function with parameter named async
==================================
const x = async => async;
---
(program
(lexical_declaration (variable_declarator (identifier) (arrow_function (identifier) (identifier)))))
==================================
Super
==================================
class A extends B {
constructor(x: number, y: number) {
super(x);
}
public toString() {
return super.toString() + " y=" + this.y;
}
}
---
(program
(class_declaration
name: (type_identifier)
(class_heritage (extends_clause (type_identifier)))
body: (class_body
(method_definition
name: (property_identifier)
parameters: (formal_parameters
(required_parameter (identifier) (type_annotation (predefined_type)))
(required_parameter (identifier) (type_annotation (predefined_type))))
body: (statement_block
(expression_statement (call_expression
function: (super)
arguments: (arguments (identifier))))))
(method_definition
(accessibility_modifier)
name: (property_identifier)
parameters: (formal_parameters)
body: (statement_block
(return_statement
(binary_expression
left: (binary_expression
left: (call_expression
function: (member_expression
object: (super)
property: (property_identifier))
arguments: (arguments))
right: (string (string_fragment)))
right: (member_expression
object: (this)
property: (property_identifier)))))))))
==================
Function signature
==================
export default function foo(): bar
---
(program
(export_statement
(function_signature
(identifier)
(formal_parameters)
(type_annotation (type_identifier)))))
=============================================================
Ambiguity between function signature and function declaration
=============================================================
function foo()
{}
function foo(bar)
function foo(bar): baz;
function foo(bar)
function foo(): () => { [key: foo]: bar }
function foo(): () => { [key: foo]: bar } {}
---
(program
(function_declaration
(identifier)
(formal_parameters)
(statement_block))
(function_signature
(identifier)
(formal_parameters (required_parameter (identifier))))
(function_signature
(identifier)
(formal_parameters (required_parameter (identifier)))
(type_annotation (type_identifier)))
(function_signature
(identifier)
(formal_parameters
(required_parameter (identifier))))
(function_signature
(identifier)
(formal_parameters)
(type_annotation
(function_type
(formal_parameters)
(object_type
(index_signature
(identifier)
(type_identifier)
(type_annotation (type_identifier)))))))
(function_declaration
(identifier)
(formal_parameters)
(type_annotation
(function_type
(formal_parameters)
(object_type
(index_signature
(identifier)
(type_identifier)
(type_annotation (type_identifier))))))
(statement_block)))
====================================================================================
Ambiguity between function signature and function declaration: comments and newlines
====================================================================================
function foo()
// above is a signature
foo();
function bar()
// above is a function declaration
{}
function foo()
: number;
---
(program
(function_signature (identifier) (formal_parameters))
(comment)
(expression_statement (call_expression (identifier) (arguments)))
(function_declaration
(identifier)
(formal_parameters)
(comment)
(statement_block))
(function_signature
(identifier)
(formal_parameters)
(type_annotation (predefined_type))))

File diff suppressed because it is too large Load Diff

@ -0,0 +1,834 @@
module.exports = function defineGrammar(dialect) {
return grammar(require('tree-sitter-javascript/grammar'), {
name: dialect,
externals: ($, previous) => previous.concat([
// Allow the external scanner to tell whether it is parsing an expression
// or a type by checking the validity of this binary operator. This is
// needed because the rules for automatic semicolon insertion are
// slightly different when parsing types. Any binary-only operator would
// work.
'||',
$._function_signature_automatic_semicolon,
]),
supertypes: ($, previous) => previous.concat([
$._primary_type,
]),
precedences: ($, previous) => previous.concat([
[
'call',
'unary',
'binary_as',
$.await_expression,
$.arrow_function,
],
[
$.intersection_type,
$.union_type,
$.conditional_type,
$.function_type,
'binary_as',
$.type_predicate,
$.readonly_type
],
[$.mapped_type_clause, $.primary_expression],
[$.accessibility_modifier, $.primary_expression],
['unary_void', $.expression],
['extends_type', $.primary_expression],
['unary', 'assign'],
['declaration', $.expression],
[$.predefined_type, $.unary_expression],
[$._type, $.flow_maybe_type],
[$.tuple_type, $.array_type, $.pattern, $._type],
[$.readonly_type, $.pattern],
[$.readonly_type, $.primary_expression],
[$.type_query, $.subscript_expression, $.expression],
[$.nested_type_identifier, $.generic_type, $._primary_type, $.lookup_type, $.index_type_query, $._type],
]),
conflicts: ($, previous) => previous.concat([
[$.call_expression, $.binary_expression],
[$.call_expression, $.binary_expression, $.unary_expression],
[$.call_expression, $.binary_expression, $.update_expression],
[$.call_expression, $.binary_expression, $.type_assertion],
[$.nested_identifier, $.nested_type_identifier, $.primary_expression],
[$.nested_identifier, $.nested_type_identifier],
[$.nested_identifier, $.member_expression],
[$.primary_expression, $.array_type],
[$.primary_expression, $.array_type, $.tuple_type],
[$._call_signature, $.function_type],
[$._call_signature, $.constructor_type],
[$._primary_type, $.type_parameter],
[$.jsx_opening_element, $.type_parameter],
[$.jsx_opening_element, $.type_parameter, $._primary_type],
[$.jsx_opening_element, $.generic_type],
[$.jsx_namespace_name, $._primary_type],
[$.primary_expression, $._parameter_name],
[$.primary_expression, $._parameter_name, $.predefined_type],
[$.primary_expression, $._parameter_name, $._primary_type],
[$.primary_expression, $._parameter_name, $.array_type, $.tuple_type],
[$.primary_expression, $.literal_type],
[$.primary_expression, $._primary_type],
[$.primary_expression, $.generic_type],
[$.primary_expression, $.predefined_type],
[$.primary_expression, $.pattern, $._primary_type],
[$.primary_expression, $.pattern, $.predefined_type],
[$._parameter_name, $.predefined_type],
[$._parameter_name, $._primary_type],
[$._parameter_name, $.assignment_expression],
[$._parameter_name, $.pattern],
[$.pattern, $._primary_type],
[$.pattern, $.predefined_type],
[$.optional_tuple_parameter, $._primary_type],
[$.optional_tuple_parameter, $._primary_type, $.primary_expression],
[$.rest_pattern, $._primary_type, $.primary_expression],
[$.rest_pattern, $._primary_type],
[$.object, $.object_type],
[$.object, $._property_name],
[$.object, $.object_pattern, $.object_type],
[$.object, $.object_pattern, $._property_name],
[$.object_pattern, $.object_type],
[$.object_pattern, $.object_type],
[$.object_pattern, $._property_name],
[$.array, $.tuple_type],
[$.array, $.array_pattern, $.tuple_type],
[$.array_pattern, $.tuple_type],
]),
inline: ($, previous) => previous
.filter(rule => ![
'_formal_parameter',
'_call_signature'
].includes(rule.name))
.concat([
$._type_identifier,
$._enum_member,
$._jsx_start_opening_element,
]),
rules: {
public_field_definition: $ => seq(
optional('declare'),
optional($.accessibility_modifier),
choice(
seq(optional('static'), optional('readonly')),
seq(optional('abstract'), optional('readonly')),
seq(optional('readonly'), optional('abstract')),
),
field('name', $._property_name),
optional(choice('?', '!')),
field('type', optional($.type_annotation)),
optional($._initializer)
),
// override original catch_clause, add optional type annotation
catch_clause: $ => seq(
'catch',
optional(
seq(
'(',
field(
'parameter',
choice($.identifier, $._destructuring_pattern)
),
optional(
// only types that resolve to 'any' or 'unknown' are supported
// by the language but it's simpler to accept any type here.
field('type', $.type_annotation),
),
')'
)
),
field('body', $.statement_block)
),
call_expression: $ => choice(
prec('call', seq(
field('function', $.expression),
field('type_arguments', optional($.type_arguments)),
field('arguments', choice($.arguments, $.template_string))
)),
prec('member', seq(
field('function', $.primary_expression),
'?.',
field('type_arguments', optional($.type_arguments)),
field('arguments', $.arguments)
))
),
new_expression: $ => prec.right('new', seq(
'new',
field('constructor', $.primary_expression),
field('type_arguments', optional($.type_arguments)),
field('arguments', optional($.arguments))
)),
_augmented_assignment_lhs: ($, previous) => choice(previous, $.non_null_expression),
_lhs_expression: ($, previous) => choice(previous, $.non_null_expression),
primary_expression: ($, previous) => choice(
previous,
$.non_null_expression,
),
// If the dialect is regular typescript, we exclude JSX expressions and
// include type assertions. If the dialect is TSX, we do the opposite.
expression: ($, previous) => {
const choices = [
$.as_expression,
$.internal_module,
];
if (dialect === 'typescript') {
choices.push($.type_assertion);
choices.push(...previous.members.filter(member =>
member.name !== '_jsx_element' && member.name !== 'jsx_fragment'
));
} else if (dialect === 'tsx') {
choices.push(...previous.members);
} else {
throw new Error(`Unknown dialect ${dialect}`);
}
return choice(...choices);
},
_jsx_start_opening_element: $ => seq(
'<',
choice(
field('name', choice(
$._jsx_identifier,
$.jsx_namespace_name
)),
seq(
field('name', choice(
$.identifier,
$.nested_identifier
)),
field('type_arguments', optional($.type_arguments))
)
),
repeat(field('attribute', $._jsx_attribute))
),
// This rule is only referenced by expression when the dialect is 'tsx'
jsx_opening_element: $ => prec.dynamic(-1, seq(
$._jsx_start_opening_element,
'>'
)),
// tsx only. See jsx_opening_element.
jsx_self_closing_element: $ => prec.dynamic(-1, seq(
$._jsx_start_opening_element,
'/',
'>'
)),
_import_export_specifier: ($, previous) => seq(
optional(choice('type', 'typeof')),
previous
),
import_statement: $ => seq(
'import',
optional(choice('type', 'typeof')),
choice(
seq($.import_clause, $._from_clause),
$.import_require_clause,
$.string
),
$._semicolon
),
export_statement: ($, previous) => choice(
previous,
seq('export', 'type', $.export_clause),
seq('export', '=', $.identifier, $._semicolon),
seq('export', 'as', 'namespace', $.identifier, $._semicolon)
),
non_null_expression: $ => prec.left('unary', seq(
$.expression, '!'
)),
variable_declarator: $ => choice(
seq(
field('name', choice($.identifier, $._destructuring_pattern)),
field('type', optional($.type_annotation)),
optional($._initializer)
),
prec('declaration', seq(
field('name', $.identifier),
'!',
field('type', $.type_annotation)
))
),
method_signature: $ => seq(
optional($.accessibility_modifier),
optional('static'),
optional('readonly'),
optional('async'),
optional(choice('get', 'set', '*')),
field('name', $._property_name),
optional('?'),
$._call_signature
),
abstract_method_signature: $ => seq(
optional($.accessibility_modifier),
'abstract',
optional(choice('get', 'set', '*')),
field('name', $._property_name),
optional('?'),
$._call_signature
),
parenthesized_expression: ($, previous) => seq(
'(',
choice(
seq($.expression, optional($.type_annotation)),
$.sequence_expression
),
')'
),
_formal_parameter: $ => choice(
$.required_parameter,
$.optional_parameter
),
function_signature: $ => seq(
optional('async'),
'function',
field('name', $.identifier),
$._call_signature,
choice($._semicolon, $._function_signature_automatic_semicolon),
),
class_body: $ => seq(
'{',
repeat(choice(
$.decorator,
seq($.method_definition, optional($._semicolon)),
seq(
choice(
$.abstract_method_signature,
$.index_signature,
$.method_signature,
$.public_field_definition
),
choice($._semicolon, ',')
)
)),
'}'
),
method_definition: $ => prec.left(seq(
optional($.accessibility_modifier),
optional('static'),
optional('readonly'),
optional('async'),
optional(choice('get', 'set', '*')),
field('name', $._property_name),
optional('?'),
$._call_signature,
field('body', $.statement_block)
)),
declaration: ($, previous) => choice(
previous,
$.function_signature,
$.abstract_class_declaration,
$.module,
prec('declaration', $.internal_module),
$.type_alias_declaration,
$.enum_declaration,
$.interface_declaration,
$.import_alias,
$.ambient_declaration
),
type_assertion: $ => prec.left('unary', seq(
$.type_arguments,
$.expression
)),
as_expression: $ => prec.left('binary_as', seq(
$.expression,
'as',
choice($._type, $.template_string)
)),
class_heritage: $ => choice(
seq($.extends_clause, optional($.implements_clause)),
$.implements_clause
),
import_require_clause: $ => seq($.identifier, '=', 'require', '(', $.string, ')'),
implements_clause: $ => seq(
'implements',
commaSep1($._type)
),
ambient_declaration: $ => seq(
'declare',
choice(
$.declaration,
seq('global', $.statement_block),
seq('module', '.', alias($.identifier, $.property_identifier), ':', $._type, $._semicolon)
)
),
class: $ => prec('literal', seq(
repeat(field('decorator', $.decorator)),
'class',
field('name', optional($._type_identifier)),
field('type_parameters', optional($.type_parameters)),
optional($.class_heritage),
field('body', $.class_body)
)),
abstract_class_declaration: $ => prec('declaration', seq(
repeat(field('decorator', $.decorator)),
'abstract',
'class',
field('name', $._type_identifier),
field('type_parameters', optional($.type_parameters)),
optional($.class_heritage),
field('body', $.class_body)
)),
class_declaration: $ => prec.left('declaration', seq(
repeat(field('decorator', $.decorator)),
'class',
field('name', $._type_identifier),
field('type_parameters', optional($.type_parameters)),
optional($.class_heritage),
field('body', $.class_body),
optional($._automatic_semicolon)
)),
module: $ => seq(
'module',
$._module
),
internal_module: $ => seq(
'namespace',
$._module
),
_module: $ => prec.right(seq(
field('name', choice($.string, $.identifier, $.nested_identifier)),
field('body', optional($.statement_block))
)),
import_alias: $ => seq(
'import',
$.identifier,
'=',
choice($.identifier, $.nested_identifier),
$._semicolon
),
nested_type_identifier: $ => prec('member', seq(
field('module', choice($.identifier, $.nested_identifier)),
'.',
field('name', $._type_identifier)
)),
interface_declaration: $ => seq(
'interface',
field('name', $._type_identifier),
field('type_parameters', optional($.type_parameters)),
optional($.extends_clause),
field('body', $.object_type)
),
extends_clause: $ => prec('extends_type', seq(
'extends',
commaSep1(choice(
prec('extends_type', choice(
$._type_identifier,
$.nested_type_identifier,
$.generic_type
)),
$.expression
))
)),
enum_declaration: $ => seq(
optional('const'),
'enum',
field('name', $.identifier),
field('body', $.enum_body)
),
enum_body: $ => seq(
'{',
optional(seq(
sepBy1(',', choice(
$._property_name,
$.enum_assignment
)),
optional(',')
)),
'}'
),
enum_assignment: $ => seq(
$._property_name,
$._initializer
),
type_alias_declaration: $ => seq(
'type',
field('name', $._type_identifier),
field('type_parameters', optional($.type_parameters)),
'=',
field('value', $._type),
$._semicolon
),
accessibility_modifier: $ => choice(
'public',
'private',
'protected'
),
required_parameter: $ => seq(
$._parameter_name,
optional($.type_annotation),
optional($._initializer)
),
optional_parameter: $ => seq(
$._parameter_name,
'?',
optional($.type_annotation),
optional($._initializer)
),
_parameter_name: $ => seq(
repeat(field('decorator', $.decorator)),
optional($.accessibility_modifier),
optional('readonly'),
choice($.pattern, $.this)
),
omitting_type_annotation: $ => seq('-?:', $._type),
opting_type_annotation: $ => seq('?:', $._type),
type_annotation: $ => seq(':', $._type),
asserts: $ => seq(
':',
'asserts',
choice($.type_predicate, $.identifier, $.this)
),
_type: $ => choice(
$._primary_type,
$.union_type,
$.intersection_type,
$.function_type,
$.readonly_type,
$.constructor_type,
$.infer_type
),
tuple_parameter: $ => seq(
choice($.identifier, $.rest_pattern),
$.type_annotation
),
optional_tuple_parameter: $ => seq(
$.identifier,
'?',
$.type_annotation
),
optional_type: $ => seq($._type, '?'),
rest_type: $ => seq('...', $._type),
_tuple_type_member: $ => choice(
alias($.tuple_parameter, $.required_parameter),
alias($.optional_tuple_parameter, $.optional_parameter),
$.optional_type,
$.rest_type,
$._type,
),
constructor_type: $ => prec.left(seq(
'new',
optional($.type_parameters),
$.formal_parameters,
'=>',
$._type
)),
_primary_type: $ => choice(
$.parenthesized_type,
$.predefined_type,
$._type_identifier,
$.nested_type_identifier,
$.generic_type,
$.object_type,
$.array_type,
$.tuple_type,
$.flow_maybe_type,
$.type_query,
$.index_type_query,
$.this,
$.existential_type,
$.literal_type,
$.lookup_type,
$.conditional_type,
),
infer_type: $ => seq("infer", $._type_identifier),
conditional_type: $ => prec.left(seq(
field('left', $._type),
'extends',
field('right', $._type),
'?',
field('consequence', $._type),
':',
field('alternative', $._type)
)),
generic_type: $ => prec('call', seq(
choice(
$._type_identifier,
$.nested_type_identifier
),
$.type_arguments
)),
type_predicate: $ => seq(
choice($.identifier, $.this),
'is',
$._type
),
type_predicate_annotation: $ => seq(
seq(':', $.type_predicate)
),
type_query: $ => prec.right(seq(
'typeof',
choice($.primary_expression, $.generic_type),
)),
index_type_query: $ => seq(
'keyof',
$._primary_type,
),
lookup_type: $ => seq(
$._primary_type,
'[',
$._type,
']'
),
mapped_type_clause: $ => seq(
$._type_identifier,
'in',
$._type,
),
literal_type: $ => choice(
alias($._number, $.unary_expression),
$.number,
$.string,
$.true,
$.false
),
_number: $ => prec.left(1, seq(
field('operator', choice('-', '+')),
field('argument', $.number)
)),
existential_type: $ => '*',
flow_maybe_type: $ => prec.right(seq( '?', $._primary_type)),
parenthesized_type: $ => seq(
'(', $._type, ')'
),
predefined_type: $ => choice(
'any',
'number',
'boolean',
'string',
'symbol',
'void'
),
type_arguments: $ => seq(
'<', commaSep1($._type), optional(','), '>'
),
object_type: $ => seq(
choice('{', '{|'),
optional(seq(
optional(choice(',', ';')),
sepBy1(
choice(',', $._semicolon),
choice(
$.export_statement,
$.property_signature,
$.call_signature,
$.construct_signature,
$.index_signature,
$.method_signature
)
),
optional(choice(',', $._semicolon))
)),
choice('}', '|}')
),
call_signature: $ => $._call_signature,
property_signature: $ => seq(
optional($.accessibility_modifier),
optional('static'),
optional('readonly'),
field('name', $._property_name),
optional('?'),
field('type', optional($.type_annotation))
),
_call_signature: $ => seq(
field('type_parameters', optional($.type_parameters)),
field('parameters', $.formal_parameters),
field('return_type', optional(
choice($.type_annotation, $.asserts, $.type_predicate_annotation)
))
),
type_parameters: $ => seq(
'<', commaSep1($.type_parameter), optional(','), '>'
),
type_parameter: $ => seq(
$._type_identifier,
optional($.constraint),
optional($.default_type)
),
default_type: $ => seq(
'=',
$._type
),
constraint: $ => seq(
choice('extends', ':'),
$._type
),
construct_signature: $ => seq(
'new',
optional($.type_parameters),
$.formal_parameters,
optional($.type_annotation)
),
index_signature: $ => seq(
optional(
seq(
field("sign", optional("-")),
'readonly'
)
),
'[',
choice(
seq(
choice(
$.identifier,
alias($._reserved_identifier, $.identifier)
),
':',
$._type,
),
$.mapped_type_clause
),
']',
choice(
$.type_annotation,
$.omitting_type_annotation,
$.opting_type_annotation
)
),
array_type: $ => seq($._primary_type, '[', ']'),
tuple_type: $ => seq(
'[', commaSep($._tuple_type_member), optional(','), ']'
),
readonly_type: $ => seq('readonly', $._type),
union_type: $ => prec.left(seq(optional($._type), '|', $._type)),
intersection_type: $ => prec.left(seq(optional($._type), '&', $._type)),
function_type: $ => prec.left(seq(
optional($.type_parameters),
$.formal_parameters,
'=>',
choice($._type, $.type_predicate),
)),
_type_identifier: $ => alias($.identifier, $.type_identifier),
_reserved_identifier: ($, previous) => choice(
'declare',
'namespace',
'type',
'public',
'private',
'protected',
'readonly',
'module',
'any',
'number',
'boolean',
'string',
'symbol',
'export',
previous
),
},
});
}
function commaSep1 (rule) {
return sepBy1(',', rule);
}
function commaSep (rule) {
return sepBy(',', rule);
}
function sepBy (sep, rule) {
return optional(sepBy1(sep, rule))
}
function sepBy1 (sep, rule) {
return seq(rule, repeat(seq(sep, rule)));
}

@ -0,0 +1,166 @@
#include <tree_sitter/parser.h>
#include <wctype.h>
enum TokenType {
AUTOMATIC_SEMICOLON,
TEMPLATE_CHARS,
BINARY_OPERATORS,
FUNCTION_SIGNATURE_AUTOMATIC_SEMICOLON,
};
static void advance(TSLexer *lexer) { lexer->advance(lexer, false); }
static bool scan_whitespace_and_comments(TSLexer *lexer) {
for (;;) {
while (iswspace(lexer->lookahead)) {
advance(lexer);
}
if (lexer->lookahead == '/') {
advance(lexer);
if (lexer->lookahead == '/') {
advance(lexer);
while (lexer->lookahead != 0 && lexer->lookahead != '\n') {
advance(lexer);
}
} else if (lexer->lookahead == '*') {
advance(lexer);
while (lexer->lookahead != 0) {
if (lexer->lookahead == '*') {
advance(lexer);
if (lexer->lookahead == '/') {
advance(lexer);
break;
}
} else {
advance(lexer);
}
}
} else {
return false;
}
} else {
return true;
}
}
}
static inline bool external_scanner_scan(void *payload, TSLexer *lexer, const bool *valid_symbols) {
if (valid_symbols[TEMPLATE_CHARS]) {
if (valid_symbols[AUTOMATIC_SEMICOLON]) return false;
lexer->result_symbol = TEMPLATE_CHARS;
for (bool notfirst = false;; notfirst = true) {
lexer->mark_end(lexer);
switch (lexer->lookahead) {
case '`':
return notfirst;
case '\0':
return false;
case '$':
advance(lexer);
if (lexer->lookahead == '{') return notfirst;
break;
case '\\':
advance(lexer);
advance(lexer);
break;
default:
advance(lexer);
}
}
} else if (
valid_symbols[AUTOMATIC_SEMICOLON] ||
valid_symbols[FUNCTION_SIGNATURE_AUTOMATIC_SEMICOLON]
) {
lexer->result_symbol = AUTOMATIC_SEMICOLON;
lexer->mark_end(lexer);
for (;;) {
if (lexer->lookahead == 0) return true;
if (lexer->lookahead == '}') {
// Automatic semicolon insertion breaks detection of object patterns
// in a typed context:
// type F = ({a}: {a: number}) => number;
// Therefore, disable automatic semicolons when followed by typing
do {
advance(lexer);
} while (iswspace(lexer->lookahead));
if (lexer->lookahead == ':') return false;
return true;
}
if (!iswspace(lexer->lookahead)) return false;
if (lexer->lookahead == '\n') break;
advance(lexer);
}
advance(lexer);
if (!scan_whitespace_and_comments(lexer)) return false;
switch (lexer->lookahead) {
case ',':
case '.':
case ';':
case '*':
case '%':
case '>':
case '<':
case '=':
case '?':
case '^':
case '|':
case '&':
case '/':
case ':':
return false;
case '{':
if (valid_symbols[FUNCTION_SIGNATURE_AUTOMATIC_SEMICOLON]) return false;
break;
// Don't insert a semicolon before a '[' or '(', unless we're parsing
// a type. Detect whether we're parsing a type or an expression using
// the validity of a binary operator token.
case '(':
case '[':
if (valid_symbols[BINARY_OPERATORS]) return false;
break;
// Insert a semicolon before `--` and `++`, but not before binary `+` or `-`.
case '+':
advance(lexer);
return lexer->lookahead == '+';
case '-':
advance(lexer);
return lexer->lookahead == '-';
// Don't insert a semicolon before `!=`, but do insert one before a unary `!`.
case '!':
advance(lexer);
return lexer->lookahead != '=';
// Don't insert a semicolon before `in` or `instanceof`, but do insert one
// before an identifier.
case 'i':
advance(lexer);
if (lexer->lookahead != 'n') return true;
advance(lexer);
if (!iswalpha(lexer->lookahead)) return false;
for (unsigned i = 0; i < 8; i++) {
if (lexer->lookahead != "stanceof"[i]) return true;
advance(lexer);
}
if (!iswalpha(lexer->lookahead)) return false;
break;
}
return true;
} else {
return false;
}
}

File diff suppressed because it is too large Load Diff

@ -0,0 +1,95 @@
{
"name": "tree-sitter-typescript",
"version": "0.19.0",
"description": "Typescript grammar for tree-sitter",
"keywords": [
"parser",
"tree-sitter",
"typescript"
],
"repository": {
"type": "git",
"url": "https://github.com/tree-sitter/tree-sitter-typescript.git"
},
"author": "Max Brunsfeld",
"license": "MIT",
"dependencies": {
"nan": "^2.14.0"
},
"main": "./bindings/node",
"devDependencies": {
"tree-sitter-cli": "^0.19.1",
"tree-sitter-javascript": "github:tree-sitter/tree-sitter-javascript#2c5b138"
},
"scripts": {
"build": "npm run build-typescript && npm run build-tsx",
"build-typescript": "cd typescript && tree-sitter generate",
"build-tsx": "cd tsx && tree-sitter generate",
"test-load": "node -e \"console.log(require('./typescript').name, require('./tsx').name)\"",
"test": "npm run test-typescript && npm run test-tsx && npm run test-load && script/parse-examples",
"test-typescript": "cd typescript && tree-sitter test",
"test-tsx": "cd tsx && tree-sitter test",
"test-windows": "pushd typescript && tree-sitter test && popd && pushd tsx && tree-sitter test"
},
"tree-sitter": [
{
"scope": "source.ts",
"file-types": [
"ts"
],
"path": "typescript",
"highlights": [
"queries/highlights.scm",
"node_modules/tree-sitter-javascript/queries/highlights.scm"
],
"locals": [
"queries/locals.scm",
"node_modules/tree-sitter-javascript/queries/locals.scm"
],
"tags": [
"queries/tags.scm",
"node_modules/tree-sitter-javascript/queries/tags.scm"
],
"injections": "node_modules/tree-sitter-javascript/queries/injections.scm",
"injection-regex": "^(ts|typescript)$"
},
{
"scope": "source.tsx",
"file-types": [
"tsx"
],
"path": "tsx",
"highlights": [
"queries/highlights.scm",
"node_modules/tree-sitter-javascript/queries/highlights-jsx.scm",
"node_modules/tree-sitter-javascript/queries/highlights.scm"
],
"locals": "node_modules/tree-sitter-javascript/queries/locals.scm",
"tags": [
"queries/tags.scm",
"node_modules/tree-sitter-javascript/queries/tags.scm"
],
"injections": "node_modules/tree-sitter-javascript/queries/injections.scm",
"injection-regex": "^(ts|typescript)$"
},
{
"scope": "source.js.flow",
"file-types": [
"js"
],
"path": "tsx",
"highlights": [
"queries/highlights.scm",
"node_modules/tree-sitter-javascript/queries/highlights-jsx.scm",
"node_modules/tree-sitter-javascript/queries/highlights.scm"
],
"locals": "node_modules/tree-sitter-javascript/queries/locals.scm",
"tags": [
"queries/tags.scm",
"node_modules/tree-sitter-javascript/queries/tags.scm"
],
"injections": "node_modules/tree-sitter-javascript/queries/injections.scm",
"content-regex": "@flow"
}
]
}

@ -0,0 +1,34 @@
; Types
(type_identifier) @type
(predefined_type) @type.builtin
((identifier) @type
(#match? @type "^[A-Z]"))
(type_arguments
"<" @punctuation.bracket
">" @punctuation.bracket)
; Variables
(required_parameter (identifier) @variable.parameter)
(optional_parameter (identifier) @variable.parameter)
; Keywords
[
"abstract"
"declare"
"enum"
"export"
"implements"
"interface"
"keyof"
"namespace"
"private"
"protected"
"public"
"type"
"readonly"
] @keyword

@ -0,0 +1,2 @@
(required_parameter (identifier) @local.definition)
(optional_parameter (identifier) @local.definition)

@ -0,0 +1,23 @@
(function_signature
name: (identifier) @name) @definition.function
(method_signature
name: (property_identifier) @name) @definition.method
(abstract_method_signature
name: (property_identifier) @name) @definition.method
(abstract_class_declaration
name: (type_identifier) @name) @definition.class
(module
name: (identifier) @name) @definition.module
(interface_declaration
name: (type_identifier) @name) @definition.interface
(type_annotation
(type_identifier) @name) @reference.type
(new_expression
constructor: (identifier) @name) @reference.class

@ -0,0 +1,24 @@
examples/redux/src/types/store.ts
examples/vscode/src/vs/base/browser/ui/grid/gridview.ts
examples/vscode/src/vs/platform/log/node/spdlogService.ts
examples/vscode/src/vs/platform/native/electron-main/nativeHostMainService.ts
examples/vscode/src/vs/platform/telemetry/common/errorTelemetry.ts
examples/vscode/src/vs/workbench/api/common/extHost.api.impl.ts
examples/vscode/src/vs/workbench/api/common/extHostQuickOpen.ts
examples/vscode/src/vs/workbench/api/common/extHostSecrets.ts
examples/vscode/src/vs/workbench/api/common/extHostStatusBar.ts
examples/vscode/src/vs/workbench/api/common/extHostTask.ts
examples/vscode/src/vs/workbench/api/common/extHostTesting.ts
examples/vscode/src/vs/workbench/api/common/extHostTypes.ts
examples/vscode/src/vs/workbench/api/common/extHostWebview.ts
examples/vscode/src/vs/workbench/api/common/extHostWebviewPanels.ts
examples/vscode/src/vs/workbench/api/common/extHostWebviewView.ts
examples/vscode/src/vs/workbench/api/node/extHostSearch.ts
examples/vscode/src/vs/workbench/contrib/terminal/common/terminalEnvironment.ts
examples/vscode/src/vs/workbench/contrib/testing/browser/testingExplorerView.ts
examples/vscode/src/vs/workbench/contrib/testing/common/testResultService.ts
examples/vscode/src/vs/workbench/contrib/webviewPanel/browser/webviewWorkbenchService.ts
examples/vscode/src/vs/workbench/contrib/welcome/gettingStarted/browser/gettingStarted.contribution.ts
examples/vscode/src/vs/workbench/services/search/node/textSearchManager.ts
examples/desktop/app/src/ui/history/commit-summary.tsx
examples/desktop/app/src/ui/lib/list/list.tsx

@ -0,0 +1,54 @@
#!/bin/bash
set -e
cd "$(dirname "$0")/.."
root="$PWD"
clone_repo() {
local owner=$1
local name=$2
local sha=$3
local path="$root/examples/$name"
if [ -d "$path" ]; then
pushd "$path" > /dev/null
if [ "$(git rev-parse HEAD 2>/dev/null)" == "$sha" ]; then
popd > /dev/null
return
else
popd > /dev/null
rm -rf "$path"
echo "Updating $owner/$name to $sha"
fi
else
echo "Cloning $owner/$name"
fi
mkdir -p "$path"
pushd "$path" > /dev/null
git init
git remote add origin "https://github.com/$owner/$name"
git pull --ff-only --depth 1 origin "$sha"
popd > /dev/null
}
clone_repo desktop desktop d1324f56d02dd9afca5d2e9da545905a7d41d671
clone_repo reduxjs redux 45111a63d39a0c0fbd8b5417b2ad623718d42d66
clone_repo microsoft vscode bead496a613e475819f89f08e9e882b841bc1fe8
known_failures="$(cat script/known-failures.txt)"
tree-sitter parse -q \
'examples/**/*.ts' \
'examples/**/*.tsx' \
$(for failure in $known_failures; do echo "!${failure}"; done)
example_count=$(find examples -name '*.ts*' -or -name '*.tx' | 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

@ -0,0 +1 @@
../../common/corpus

@ -0,0 +1,19 @@
==========================================================
Type arguments in JSX
==========================================================
<Element<T>>hi</Element>;
<Element<T> />;
---
(program
(expression_statement
(jsx_element
(jsx_opening_element (identifier) (type_arguments (type_identifier)))
(jsx_text)
(jsx_closing_element (identifier))))
(expression_statement
(jsx_self_closing_element
(identifier) (type_arguments (type_identifier))))
)

@ -0,0 +1,3 @@
const defineGrammar = require('../common/define-grammar');
module.exports = defineGrammar('tsx');

@ -0,0 +1,3 @@
{
"main": "../bindings/node/tsx"
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

@ -0,0 +1,11 @@
#include "../../common/scanner.h"
void *tree_sitter_tsx_external_scanner_create() { return NULL; }
void tree_sitter_tsx_external_scanner_destroy(void *p) {}
void tree_sitter_tsx_external_scanner_reset(void *p) {}
unsigned tree_sitter_tsx_external_scanner_serialize(void *p, char *buffer) { return 0; }
void tree_sitter_tsx_external_scanner_deserialize(void *p, const char *b, unsigned n) {}
bool tree_sitter_tsx_external_scanner_scan(void *payload, TSLexer *lexer, const bool *valid_symbols) {
return external_scanner_scan(payload, lexer, valid_symbols);
}

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

@ -0,0 +1,20 @@
==========================
Type assertions
==========================
<A>b;
<C<D>>e.f;
---
(program
(expression_statement (type_assertion
(type_arguments (type_identifier))
(identifier)))
(expression_statement (type_assertion
(type_arguments (generic_type
(type_identifier)
(type_arguments (type_identifier))))
(member_expression
(identifier)
(property_identifier)))))

@ -0,0 +1,3 @@
const defineGrammar = require('../common/define-grammar');
module.exports = defineGrammar('typescript');

@ -0,0 +1,3 @@
{
"main": "../bindings/node/typescript"
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

@ -0,0 +1,11 @@
#include "../../common/scanner.h"
void *tree_sitter_typescript_external_scanner_create() { return NULL; }
void tree_sitter_typescript_external_scanner_destroy(void *p) {}
void tree_sitter_typescript_external_scanner_reset(void *p) {}
unsigned tree_sitter_typescript_external_scanner_serialize(void *p, char *buffer) { return 0; }
void tree_sitter_typescript_external_scanner_deserialize(void *p, const char *b, unsigned n) {}
bool tree_sitter_typescript_external_scanner_scan(void *payload, TSLexer *lexer, const bool *valid_symbols) {
return external_scanner_scan(payload, lexer, valid_symbols);
}

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

@ -0,0 +1 @@
type a = readonly b[][];