Skip to content

Commit

Permalink
Small grammar refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
kubukoz committed Nov 11, 2024
1 parent a8a2b90 commit d3fc988
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 39 deletions.
16 changes: 15 additions & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
flake-utils.url = "github:numtide/flake-utils";
};

outputs = { nixpkgs, flake-utils, ... }:
outputs = { self, nixpkgs, flake-utils, ... }:
flake-utils.lib.eachDefaultSystem (
system:
let
Expand Down Expand Up @@ -38,6 +38,20 @@
cp libtree-sitter-smithyql.so $out
'';
};
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
'';
};
}
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ object MultiServiceResolver {
* perform a check on that. For the actual check, see PreludeCompiler.
*/
def resolveServiceTs(
queryOperationName: playground.generated.nodes.OperationName,
queryOperationName: playground.generated.nodes.QueryOperationName,
serviceIndex: ServiceIndex,
useClauses: List[playground.generated.nodes.UseClause],
): EitherNel[CompilationError, QualifiedIdentifier] =
Expand Down Expand Up @@ -104,7 +104,7 @@ object MultiServiceResolver {
private def resolveExplicitTs(
index: ServiceIndex,
explicitRef: playground.generated.nodes.QualifiedIdentifier,
operationName: playground.generated.nodes.Identifier,
operationName: playground.generated.nodes.OperationName,
): EitherNel[CompilationError, QualifiedIdentifier] =
ASTAdapter.decodeQI(explicitRef) match {
case None => ??? /* todo - I don't really know xD */
Expand Down Expand Up @@ -161,8 +161,7 @@ object MultiServiceResolver {
}

private def resolveImplicitTs(
// todo: introduce type wrapper for OperationName, rename current to QueryOperationName
operationName: playground.generated.nodes.Identifier,
operationName: playground.generated.nodes.OperationName,
index: ServiceIndex,
useClauses: List[playground.generated.nodes.UseClause],
): EitherNel[CompilationError, QualifiedIdentifier] = {
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,15 @@ object OperationName {
extension (node: OperationName) {
def select[A](f: OperationName.Selector => Selection[A]): List[A] = f(OperationName.Selector(List(node))).path
// fields
def identifier: List[QualifiedIdentifier] = node.fields.getOrElse("identifier", Nil).toList.collect {
case QualifiedIdentifier(node) => node
}

def name: Option[Identifier] = node.fields.getOrElse("name", Nil).headOption.map {
// typed children
def typedChildren: Option[Identifier] = node.children.collectFirst {
case Identifier(node) => node
}
// typed children

// precise typed children

def identifier: Option[Identifier] = node.children.collectFirst {
case Identifier(node) => node
}
}

def apply(node: Node): Either[String, OperationName] =
Expand All @@ -33,8 +31,7 @@ object OperationName {
def unapply(node: Node): Option[OperationName] = apply(node).toOption

final case class Selector(path: List[OperationName]) extends Selection[OperationName] {
def identifier: QualifiedIdentifier.Selector = QualifiedIdentifier.Selector(path.flatMap(_.identifier))
def name: Identifier.Selector = Identifier.Selector(path.flatMap(_.name))
def identifier: Identifier.Selector = Identifier.Selector(path.flatMap(_.identifier))

type Self = Selector
protected val remake = Selector.apply
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Generated code! Do not modify by hand.
package playground.generated.nodes

import org.polyvariant.treesitter4s.Node
import playground.treesitter4s.std.Selection

opaque type QueryOperationName <: Node = Node

object QueryOperationName {
extension (node: QueryOperationName) {
def select[A](f: QueryOperationName.Selector => Selection[A]): List[A] = f(QueryOperationName.Selector(List(node))).path
// fields
def identifier: List[QualifiedIdentifier] = node.fields.getOrElse("identifier", Nil).toList.collect {
case QualifiedIdentifier(node) => node
}

def name: Option[OperationName] = node.fields.getOrElse("name", Nil).headOption.map {
case OperationName(node) => node
}
// typed children

// precise typed children

}

def apply(node: Node): Either[String, QueryOperationName] =
if node.tpe == "query_operation_name"
then Right(node)
else Left(s"Expected QueryOperationName, got ${node.tpe}")

def unsafeApply(node: Node): QueryOperationName = apply(node).fold(sys.error, identity)

def unapply(node: Node): Option[QueryOperationName] = apply(node).toOption

final case class Selector(path: List[QueryOperationName]) extends Selection[QueryOperationName] {
def identifier: QualifiedIdentifier.Selector = QualifiedIdentifier.Selector(path.flatMap(_.identifier))
def name: OperationName.Selector = OperationName.Selector(path.flatMap(_.name))

type Self = Selector
protected val remake = Selector.apply
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ object RunQuery {
case Struct(node) => node
}

def operation_name: Option[OperationName] = node.fields.getOrElse("operation_name", Nil).headOption.map {
case OperationName(node) => node
def operation_name: Option[QueryOperationName] = node.fields.getOrElse("operation_name", Nil).headOption.map {
case QueryOperationName(node) => node
}
// typed children

Expand All @@ -34,7 +34,7 @@ object RunQuery {

final case class Selector(path: List[RunQuery]) extends Selection[RunQuery] {
def input: Struct.Selector = Struct.Selector(path.flatMap(_.input))
def operation_name: OperationName.Selector = OperationName.Selector(path.flatMap(_.operation_name))
def operation_name: QueryOperationName.Selector = QueryOperationName.Selector(path.flatMap(_.operation_name))

type Self = Selector
protected val remake = Selector.apply
Expand Down
11 changes: 8 additions & 3 deletions tree-sitter-smithyql/grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ module.exports = grammar({
top_level_statement: ($) => choice($.run_query),

run_query: ($) =>
seq(field("operation_name", $.operation_name), field("input", $.struct)),
seq(
field("operation_name", $.query_operation_name),
field("input", $.struct)
),

qualified_identifier: ($) =>
seq(
Expand All @@ -42,15 +45,17 @@ module.exports = grammar({
field("selection", $.identifier)
),

operation_name: ($) =>
query_operation_name: ($) =>
prec.left(
1,
seq(
field("identifier", optional(seq($.qualified_identifier, "."))),
field("name", $.identifier)
field("name", $.operation_name)
)
),

operation_name: ($) => $.identifier,

_input_node: ($) =>
choice($.struct, $.list, $.number, $.string, $.boolean, $.null),

Expand Down
23 changes: 4 additions & 19 deletions update-libs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,7 @@

set -e

mkdir -p modules/treesitter/src/main/resources/darwin-aarch64
BINARY_PATH=$(nix build .#packages.aarch64-darwin.tree-sitter-smithyql --no-link --print-out-paths --print-build-logs)
cp $BINARY_PATH modules/treesitter/src/main/resources/darwin-aarch64/libtree-sitter-smithyql.dylib
chmod 755 modules/treesitter/src/main/resources/darwin-aarch64/libtree-sitter-smithyql.dylib
mkdir -p modules/treesitter/src/main/resources/darwin-x86-64
BINARY_PATH=$(nix build .#packages.x86_64-darwin.tree-sitter-smithyql --no-link --print-out-paths --print-build-logs)
cp $BINARY_PATH modules/treesitter/src/main/resources/darwin-x86-64/libtree-sitter-smithyql.dylib
chmod 755 modules/treesitter/src/main/resources/darwin-x86-64/libtree-sitter-smithyql.dylib
mkdir -p modules/treesitter/src/main/resources/linux-aarch64
BINARY_PATH=$(nix build .#packages.aarch64-linux.tree-sitter-smithyql --no-link --print-out-paths --print-build-logs)
cp $BINARY_PATH modules/treesitter/src/main/resources/linux-aarch64/libtree-sitter-smithyql.so
chmod 755 modules/treesitter/src/main/resources/linux-aarch64/libtree-sitter-smithyql.so
mkdir -p modules/treesitter/src/main/resources/linux-x86-64
BINARY_PATH=$(nix build .#packages.x86_64-linux.tree-sitter-smithyql --no-link --print-out-paths --print-build-logs)
cp $BINARY_PATH modules/treesitter/src/main/resources/linux-x86-64/libtree-sitter-smithyql.so
chmod 755 modules/treesitter/src/main/resources/linux-x86-64/libtree-sitter-smithyql.so
LIBS_PATH=$(nix build .#tree-sitter-smithyql-all --no-link --print-out-paths --print-build-logs)
mkdir -p modules/treesitter/src/main/resources
cp -R "$LIBS_PATH"/* modules/treesitter/src/main/resources
chmod -R 755 modules/treesitter/src/main/resources

0 comments on commit d3fc988

Please sign in to comment.