Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tree-sitter #102

Draft
wants to merge 121 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
121 commits
Select commit Hold shift + click to select a range
84b5a77
WIP: tree-sitter grammar
kubukoz Aug 14, 2022
df716b7
Add tree-sitter submodule
kubukoz Aug 14, 2022
3a881b0
Add grammar
kubukoz Aug 15, 2022
be256cb
Improve grammar, start working on adaptation
kubukoz Aug 16, 2022
4b32252
wip
kubukoz Aug 16, 2022
bf09246
reintroduce NodeDecoder
kubukoz Aug 16, 2022
a81baa9
wip field extraction
kubukoz Aug 16, 2022
036e46d
Merge branch 'main' into tree-sitter
kubukoz Aug 22, 2022
bfbe4ee
Merge branch 'main' into tree-sitter
kubukoz Sep 11, 2022
9a38c2c
Merge branch 'main' into tree-sitter
kubukoz Sep 11, 2022
1380ee8
Support raw service references
kubukoz Sep 11, 2022
b073597
WIP POC more syntax
kubukoz Sep 14, 2022
b801085
Merge branch 'main' into tree-sitter
kubukoz Oct 13, 2022
fe16844
Add treesitter4s
kubukoz Oct 13, 2022
02943aa
rename, add flag
kubukoz Oct 13, 2022
d3a34b4
Add binaries, remove codegen'd code
kubukoz Oct 13, 2022
cd67e5d
remove even more generated stuff
kubukoz Oct 13, 2022
539124b
bump t4s
kubukoz Oct 13, 2022
2dc5dc7
Remove gitmodules
kubukoz Oct 13, 2022
5a04629
Remove boilerplate
kubukoz Oct 13, 2022
4997910
Add s01 resolver
kubukoz Oct 13, 2022
5417ece
Merge branch 'main' into tree-sitter
kubukoz Oct 13, 2022
83e1136
Merge branch 'main' into tree-sitter
kubukoz Oct 15, 2022
6aebdd2
Merge branch 'main' into tree-sitter
kubukoz Oct 18, 2022
bfbd5ef
Less boilerplate in grammar farm build
kubukoz Oct 30, 2022
2808a7f
Update ts4s
kubukoz Oct 30, 2022
23741a5
reusable library packaging
kubukoz Oct 30, 2022
af7d5cc
cleanup
kubukoz Oct 30, 2022
438c12b
Full blown grammar build
kubukoz Oct 31, 2022
eae7d8b
Move library code to ts4s
kubukoz Oct 31, 2022
aa6a324
More work in nix, update things
kubukoz Oct 31, 2022
dc64ccf
cleanup
kubukoz Oct 31, 2022
a67db72
Merge branch 'main' into tree-sitter
kubukoz Oct 29, 2024
7cc3713
use an existing snapshot instead
kubukoz Oct 29, 2024
3b5af71
new age of treesitter4s
kubukoz Oct 30, 2024
88d410f
setup sbt
kubukoz Oct 30, 2024
0949bf1
use adoptium 21
kubukoz Oct 30, 2024
7ea8ce3
updates
kubukoz Oct 30, 2024
bf9f6d4
update build settings
kubukoz Nov 1, 2024
1cd0977
rebuild linux binaries
kubukoz Nov 1, 2024
906d50d
move corpus to new location
kubukoz Nov 1, 2024
a0fb6e1
downgrade ts so that we have ui support
kubukoz Nov 1, 2024
79ff5a9
Merge branch 'main' into tree-sitter
kubukoz Nov 1, 2024
3390152
Generating some tree-sitter code
kubukoz Nov 2, 2024
6f16fd5
Improvements for more coverage
kubukoz Nov 2, 2024
5473186
Render typedChildren
kubukoz Nov 3, 2024
2c619ee
Simpler whitespace
kubukoz Nov 3, 2024
53f77df
get rid of underscores
kubukoz Nov 3, 2024
30fe370
Add precise typed children
kubukoz Nov 3, 2024
6a9083c
Generate projections for ADTs
kubukoz Nov 3, 2024
0652614
Mark unsafe pieces
kubukoz Nov 3, 2024
17d7538
Private nodes + safe projections
kubukoz Nov 3, 2024
6393892
Account for field/child optionality
kubukoz Nov 3, 2024
e469698
update dep, demo
kubukoz Nov 3, 2024
908f08c
something something everything is optional now, old man
kubukoz Nov 4, 2024
2945a42
More progress on error safety
kubukoz Nov 4, 2024
c2757f3
Generate opaque types
kubukoz Nov 4, 2024
b9ca07e
Add a couple tests
kubukoz Nov 5, 2024
567bb2a
Backport things from tree-sitter branch
kubukoz Nov 5, 2024
9871fc1
ignore deprecations in examples
kubukoz Nov 5, 2024
81dd169
Merge branch 'ts-backports' into tree-sitter
kubukoz Nov 5, 2024
1c2446f
Use nixpkgs 24.05
kubukoz Nov 5, 2024
58e7f8f
oops
kubukoz Nov 5, 2024
53dae2d
update gitignore
kubukoz Nov 5, 2024
f65eba4
Use nixpkgs-unstable
kubukoz Nov 5, 2024
385df93
Use nixpkgs-unstable
kubukoz Nov 5, 2024
cf86d3d
Merge branch 'ts-backports' into tree-sitter
kubukoz Nov 5, 2024
68e3f62
never mind
kubukoz Nov 5, 2024
05a6d23
Merge branch 'main' into tree-sitter
kubukoz Nov 5, 2024
d250c74
Involve tree-sitter in parser suites
kubukoz Nov 5, 2024
4713b31
eh
kubukoz Nov 5, 2024
e0793b4
Revamp grammar a bit for more coverage
kubukoz Nov 7, 2024
b83f791
And just like that, we have parity?
kubukoz Nov 7, 2024
c249e1a
Make e2e tests detect snapshot issues
kubukoz Nov 9, 2024
6653084
Merge branch 'main' into tree-sitter
kubukoz Nov 9, 2024
12101f6
Move treesitter sources to another module
kubukoz Nov 9, 2024
75e5b2a
Implement RangeIndex
kubukoz Nov 10, 2024
4177ade
Use tree-sitter in completions
kubukoz Nov 10, 2024
ef1fe2a
Add POC of selector DSL
kubukoz Nov 10, 2024
737e8cd
Remove debugDump comment
kubukoz Nov 10, 2024
dedb2be
Introduce IR for ADTs
kubukoz Nov 11, 2024
fd95b3d
some privacy
kubukoz Nov 11, 2024
4ec6124
first generate, then delete
kubukoz Nov 11, 2024
7629f98
Add IR (which doesn't help much but hey)
kubukoz Nov 11, 2024
c4bce3a
expand selector demo to account for unions
kubukoz Nov 11, 2024
d156026
ADT -> Union
kubukoz Nov 11, 2024
abe7c7e
refactor for consistency
kubukoz Nov 11, 2024
9eca761
Add typed selectors for real
kubukoz Nov 11, 2024
170bebb
Remove selector demo
kubukoz Nov 11, 2024
163325b
Add union unsafeApply and some space
kubukoz Nov 11, 2024
c833e17
Add typed apply on unions
kubukoz Nov 11, 2024
e5946c3
format
kubukoz Nov 11, 2024
d790595
Remove some replaced code, start using selectors
kubukoz Nov 11, 2024
e2809c8
renames
kubukoz Nov 11, 2024
bcc6a66
remove monoid
kubukoz Nov 11, 2024
40455a3
update ci
kubukoz Nov 11, 2024
5458c9b
use ts4s 0.4.0
kubukoz Nov 11, 2024
08fbbd6
More powerful select
kubukoz Nov 11, 2024
a8a2b90
Fix e2e
kubukoz Nov 11, 2024
d3fc988
Small grammar refactor
kubukoz Nov 11, 2024
88ce227
tiny grammar cleanup
kubukoz Nov 12, 2024
9c24e99
Generate typed visitors on unions
kubukoz Nov 12, 2024
6a39e2c
Mark generated files as such
kubukoz Nov 12, 2024
c86bc7a
remove demos
kubukoz Nov 13, 2024
6a57268
Remove a duplicate ContextRange
kubukoz Nov 13, 2024
51184d6
well it is pretty now...
kubukoz Nov 13, 2024
b3b7d01
Merge branch 'main' into tree-sitter
kubukoz Dec 2, 2024
63b40eb
Merge branch 'main' into tree-sitter
kubukoz Dec 22, 2024
71242ab
Merge branch 'main' into tree-sitter
kubukoz Dec 24, 2024
e1d050c
Update binaries for linux
kubukoz Dec 24, 2024
fadba1d
remove now-unused code
kubukoz Dec 24, 2024
a931eda
Update grammar to be more precise wrt lists
kubukoz Dec 24, 2024
4408463
resolve todos
kubukoz Dec 24, 2024
703c927
Even better
kubukoz Dec 24, 2024
74e2042
update smithy4s
kubukoz Dec 24, 2024
4804283
Regenerate smithy-build.json
kubukoz Dec 24, 2024
1eb2eb9
Update smithy-build to actually pass validation
kubukoz Dec 24, 2024
d5e542f
Move to good old oslib
kubukoz Dec 24, 2024
4f1e82f
Reformat nix
kubukoz Dec 24, 2024
af93f95
Update nixpkgs to 24.11
kubukoz Dec 24, 2024
c6be485
Fix nix after nixpkgs bump broke TS grammars
kubukoz Dec 24, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
modules/treesitter/src/main/scala/playground/generated/** linguist-generated
17 changes: 17 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,20 @@ jobs:
- name: Show extension test logs
if: always() && job.status == 'failure'
run: cat vscode-extension/fixture/smithyql-log.txt | tail --lines 1000

build-parser:
name: "Build parser"
strategy:
matrix:
os: [ubuntu-latest, macos-latest, macos-13]
runs-on: ${{matrix.os}}
timeout-minutes: 10
steps:
- uses: actions/[email protected]
- uses: coursier/setup-action@v1
with:
apps: sbt
jvm: adoptium:1.21
- name: Parser tests
# intentionally not setting up nix
run: sbt treesitter/test
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ smithyql-log.txt

result
.version
build/smithy
26 changes: 26 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ val commonSettings = Seq(
Test / scalacOptions += "-Wconf:cat=deprecation:silent,msg=Specify both message and version:silent",
scalacOptions += "-release:11",
mimaFailOnNoPrevious := false,
resolvers += "Sonatype S01 snapshots" at "https://s01.oss.sonatype.org/content/repositories/snapshots",
)

def module(
Expand Down Expand Up @@ -133,8 +134,29 @@ lazy val parser = module("parser")
.dependsOn(
ast % "test->test;compile->compile",
source % "test->test;compile->compile",
treesitter % "test->compile",
)

lazy val treesitter = module("treesitter")
.settings(
libraryDependencies ++= Seq(
"org.polyvariant.treesitter4s" %% "core" % "0.4.0"
)
)

lazy val parsergen = module("parser-gen")
.settings(
libraryDependencies ++= Seq(
"dev.optics" %% "monocle-core" % "3.3.0",
"com.disneystreaming.smithy4s" %% "smithy4s-json" % smithy4sVersion.value,
("org.scalameta" %% "scalameta" % "4.11.0").cross(CrossVersion.for3Use2_13),
"org.polyvariant.treesitter4s" %% "core" % "0.4.0",
"com.lihaoyi" %% "os-lib" % "0.11.3",
),
scalacOptions -= "-release:11",
)
.enablePlugins(Smithy4sCodegenPlugin)

// Formatter for the SmithyQL language constructs
lazy val formatter = module("formatter")
.settings(
Expand Down Expand Up @@ -189,6 +211,7 @@ lazy val core = module("core")
examples % "test->compile",
pluginCore,
ast,
treesitter,
source % "test->test;compile->compile",
parser % "test->compile;test->test",
formatter,
Expand Down Expand Up @@ -239,6 +262,7 @@ lazy val e2e = module("e2e")
parser / publishLocal,
pluginCore / publishLocal,
source / publishLocal,
treesitter / publishLocal,
ast / publishLocal,
formatter / publishLocal,
protocol4s / publishLocal,
Expand Down Expand Up @@ -269,11 +293,13 @@ lazy val root = project
core,
examples,
parser,
parsergen,
formatter,
languageSupport,
lsp,
protocol4s,
pluginCore,
pluginSample,
e2e,
treesitter,
)
8 changes: 4 additions & 4 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 29 additions & 9 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -1,27 +1,47 @@
{
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-24.05";
nixpkgs.url = "github:nixos/nixpkgs/nixos-24.11";
flake-utils.url = "github:numtide/flake-utils";
};

outputs = { self, nixpkgs, flake-utils, ... }:
flake-utils.lib.eachDefaultSystem (
system:
let
pkgs = import nixpkgs { inherit system; };
in
{
flake-utils.lib.eachDefaultSystem (system:
let pkgs = import nixpkgs { inherit system; };
in {
devShells.default = pkgs.mkShell {
buildInputs = [
pkgs.yarn
pkgs.nodejs
pkgs.sbt
pkgs.jless
pkgs.gnupg
(pkgs.tree-sitter.override { webUISupport = true; })
# temporary, while we don't download coursier ourselves
pkgs.coursier
] ++ pkgs.lib.optionals pkgs.stdenv.isLinux [ pkgs.xvfb-run ];
};
}
);
packages.tree-sitter-smithyql = pkgs.stdenv.mkDerivation {
name = "tree-sitter-smithyql";
src = ./tree-sitter-smithyql;
buildInputs = [ pkgs.tree-sitter pkgs.nodejs ];
buildPhase = ''
tree-sitter generate
cc src/parser.c -shared -o $out
'';
dontInstall = true;
};
packages.tree-sitter-smithyql-all = pkgs.stdenv.mkDerivation {
name = "tree-sitter-smithyql-all";
src = ./tree-sitter-smithyql;
dontBuild = true;
installPhase = ''
mkdir $out
cd $out
mkdir darwin-aarch64 && cp ${self.packages.aarch64-darwin.tree-sitter-smithyql} darwin-aarch64/libtree-sitter-smithyql.dylib
mkdir darwin-x86-64 && cp ${self.packages.x86_64-darwin.tree-sitter-smithyql} darwin-x86-64/libtree-sitter-smithyql.dylib
mkdir linux-aarch64 && cp ${self.packages.aarch64-linux.tree-sitter-smithyql} linux-aarch64/libtree-sitter-smithyql.so
mkdir linux-x86-64 && cp ${self.packages.x86_64-linux.tree-sitter-smithyql} linux-x86-64/libtree-sitter-smithyql.so
'';
};
});
}
12 changes: 12 additions & 0 deletions modules/core/src/main/scala/playground/ASTAdapter.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package playground

import cats.syntax.all.*
import playground.smithyql.QualifiedIdentifier

object ASTAdapter {

def decodeQI(qi: playground.generated.nodes.QualifiedIdentifier): Option[QualifiedIdentifier] =
(qi.namespace.map(_.source).toNel, qi.selection.map(_.source))
.mapN(QualifiedIdentifier.apply)

}
86 changes: 86 additions & 0 deletions modules/core/src/main/scala/playground/MultiServiceResolver.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import playground.smithyql.UseClause
import playground.smithyql.WithSource

object MultiServiceResolver {
import playground.smithyql.tsutils.*

/** Determines which service should be used for a query. The rules are:
* - If the operation name has a service identifier, there MUST be a service with that name
Expand Down Expand Up @@ -37,6 +38,34 @@ object MultiServiceResolver {
case None => resolveImplicit(queryOperationName.operationName, serviceIndex, useClauses)
}

/** Determines which service should be used for a query. The rules are:
* - If the operation name has a service identifier, there MUST be a service with that name
* that contains the given operation.
* - If there's no service identifier, find all matching services that are included in the use
* clauses. MUST find exactly one entry.
*
* In other cases, such as when we can't find a unique entry, or the explicitly referenced
* service doesn't have an operation with a matching name, we fail. The latter might eventually
* be refactored to a separate piece of code.
*
* **Important**!
*
* This method assumes that all of the use clauses match the available service set. It does NOT
* perform a check on that. For the actual check, see PreludeCompiler.
*/
def resolveServiceTs(
queryOperationName: playground.generated.nodes.QueryOperationName,
serviceIndex: ServiceIndex,
useClauses: List[playground.generated.nodes.UseClause],
): EitherNel[CompilationError, Option[QualifiedIdentifier]] = queryOperationName
.name
.flatTraverse { opName =>
queryOperationName.service_identifier match {
case Some(explicitRef) => resolveExplicitTs(serviceIndex, explicitRef, opName)
case None => resolveImplicitTs(opName, serviceIndex, useClauses).map(_.some)
}
}

private def resolveExplicit(
index: ServiceIndex,
explicitRef: WithSource[QualifiedIdentifier],
Expand Down Expand Up @@ -66,6 +95,39 @@ object MultiServiceResolver {
case Some(_) => explicitRef.value.asRight
}

private def resolveExplicitTs(
index: ServiceIndex,
explicitRef: playground.generated.nodes.QualifiedIdentifier,
operationName: playground.generated.nodes.OperationName,
): EitherNel[CompilationError, Option[QualifiedIdentifier]] = ASTAdapter
.decodeQI(explicitRef)
.traverse { ref =>
index.getService(ref) match {
// explicit reference exists, but the service doesn't
case None =>
CompilationError
.error(
CompilationErrorDetails.UnknownService(index.serviceIds.toList),
explicitRef.range,
)
.leftNel

// the service exists, but doesn't have the requested operation
case Some(service)
if !service.operationNames.contains_(OperationName(operationName.source)) =>
CompilationError
.error(
CompilationErrorDetails.OperationMissing(service.operationNames.toList),
operationName.range,
)
.leftNel

// all good
case Some(_) => ref.asRight
}

}

private def resolveImplicit(
operationName: WithSource[OperationName[WithSource]],
index: ServiceIndex,
Expand All @@ -90,4 +152,28 @@ object MultiServiceResolver {
}
}

private def resolveImplicitTs(
operationName: playground.generated.nodes.OperationName,
index: ServiceIndex,
useClauses: List[playground.generated.nodes.UseClause],
): EitherNel[CompilationError, QualifiedIdentifier] = {
val matchingServices = index
.getServices(useClauses.flatMap(_.identifier).flatMap(ASTAdapter.decodeQI).toSet)
.filter(_.hasOperation(OperationName(operationName.source)))

matchingServices match {
case one :: Nil => one.id.asRight
case _ =>
CompilationError
.error(
CompilationErrorDetails
.AmbiguousService(
workspaceServices = index.serviceIds.toList
),
operationName.range,
)
.leftNel
}
}

}
Loading
Loading