### Install Tree-sitter Scala Header, Pkgconfig, and Library Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/CMakeLists.txt This section configures and installs the `tree-sitter-scala.pc` pkg-config file, the `tree-sitter-scala.h` header, and the compiled `tree-sitter-scala` library to standard installation directories, ensuring proper system integration. ```CMake configure_file(bindings/c/tree-sitter-scala.pc.in "${CMAKE_CURRENT_BINARY_DIR}/tree-sitter-scala.pc" @ONLY) include(GNUInstallDirs) install(FILES bindings/c/tree-sitter-scala.h DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/tree_sitter") install(FILES "${CMAKE_CURRENT_BINARY_DIR}/tree-sitter-scala.pc" DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig") install(TARGETS tree-sitter-scala LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}") ``` -------------------------------- ### Install Project Dependencies Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/CONTRIBUTING.md Installs the necessary Node.js dependencies for the tree-sitter-scala project, which are required for development and testing. ```Shell npm install ``` -------------------------------- ### Scala Subclass and Trait Implementations Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/definitions.txt Examples demonstrating various ways to define subclasses in Scala, including extending generic types, passing arguments to superclass constructors, and mixing in traits, along with their Tree-sitter parse trees. ```Scala class A extends B.C[D, E] { } class A(b: B) extends C(b || ok) { } object C { class A extends B[T] with C[T] } class D(c: String) extends E(c) with F class MyClass extends Potato() with Tomato ``` ```Tree-sitter S-expression (compilation_unit (class_definition (identifier) (extends_clause (generic_type (stable_type_identifier (identifier) (type_identifier)) (type_arguments (type_identifier) (type_identifier)))) (template_body)) (class_definition (identifier) (class_parameters (class_parameter (identifier) (type_identifier))) (extends_clause (type_identifier) (arguments (infix_expression (identifier) (operator_identifier) (identifier)))) (template_body)) (object_definition (identifier) (template_body (class_definition (identifier) (extends_clause (generic_type (type_identifier) (type_arguments (type_identifier))) (generic_type (type_identifier) (type_arguments (type_identifier))))))) (class_definition (identifier) (class_parameters (class_parameter (identifier) (type_identifier))) (extends_clause (type_identifier) (arguments (identifier)) (type_identifier)))) ``` -------------------------------- ### Scala 2 Macro Definition Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/expressions.txt Shows the syntax for defining a macro in Scala 2, illustrating how a method can be implemented as a macro. ```Scala class Foo { def a: A = macro B.b } ``` -------------------------------- ### Basic Scala Class Definitions with Parameters Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/definitions.txt Examples of Scala class definitions, including classes with type parameters, various parameter types (lazy, repeated), and multiple empty parameter lists, alongside their Tree-sitter parse structures. ```Scala class C[ T, U, ](a: => A, b: B, c: C*) { } class A () () {} ``` ```Tree-sitter S-expression (compilation_unit (class_definition (identifier) (type_parameters (identifier) (identifier)) (class_parameters (class_parameter (identifier) (lazy_parameter_type (type_identifier))) (class_parameter (identifier) (type_identifier)) (class_parameter (identifier) (repeated_parameter_type (type_identifier)))) (template_body)) (class_definition (identifier) (class_parameters) (class_parameters) (template_body))) ``` -------------------------------- ### Scala Function Definitions with Various Syntaxes and Tree-sitter Grammar Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/definitions.txt Presents diverse examples of function definitions in Scala, including multi-parameter lists, explicit return types, unit-returning functions, functions with 'using' clauses (context parameters), and functions with type constraints. The Tree-sitter grammar demonstrates the parsing of these different definition styles, showing 'function_definition', 'parameters', 'indented_block', 'unit', and 'infix_type'. ```Scala class A { def b( c: D, e: F, ) = 1 def l: Int = 1 def m = () def n(using a: A) (using B <:< B, C =:= C) = () def o() : Int = 42 } ``` ```Tree-sitter Grammar (compilation_unit (class_definition (identifier) (template_body (function_definition (identifier) (parameters (parameter (identifier) (type_identifier)) (parameter (identifier) (type_identifier))) (integer_literal)) (function_definition (identifier) (type_identifier) (indented_block (integer_literal))) (function_definition (identifier) (unit)) (function_definition (identifier) (parameters (parameter (identifier) (type_identifier))) (parameters (infix_type (type_identifier) (operator_identifier) (type_identifier)) (infix_type (type_identifier) (operator_identifier) (type_identifier))) (unit)) (function_definition (identifier) (parameters) (type_identifier) (integer_literal))))) ``` -------------------------------- ### Scala 3 Fewer Braces Syntax (SIP-44) Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/expressions.txt Shows examples of the 'fewer braces' syntax introduced in Scala 3, applied to method calls with block arguments, and distinguishing between ascription and call expressions. ```Scala class C: xs.map: x => x + 1 xs.map: x => x + 1 xs: println("") xs `++`: val x = 1 List(x) xs.map: case x => x // This is ascription xs: Int // This is call_expression xs: Int ``` -------------------------------- ### Scala Identifier Examples Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/definitions.txt Demonstrates various valid Scala identifier forms, including alphanumeric, operator-like, and Unicode characters. Shows how Tree-sitter parses these into `identifier` and `operator_identifier` nodes. ```Scala def m = ??? def unary_! = true def a_-> = ??? def __symtem = ??? def empty_? = ??? def ひらがな = ??? def a_^ = ??? ``` ```Tree-sitter S-expression (compilation_unit (function_definition (identifier) (operator_identifier)) (function_definition (identifier) (boolean_literal)) (function_definition (identifier) (operator_identifier)) (function_definition (identifier) (operator_identifier)) (function_definition (identifier) (operator_identifier)) (function_definition (identifier) (operator_identifier)) (function_definition (identifier) (operator_identifier))) ``` -------------------------------- ### Scala While Loops (Traditional Syntax) Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/expressions.txt Examples of traditional `while` loop syntax in Scala, demonstrating both single-line and block-style loop bodies. ```Scala def f = { while (a) g() while (b < c) { d } } ``` ```Tree-sitter Grammar (compilation_unit (function_definition (identifier) (block (while_expression (parenthesized_expression (identifier)) (call_expression (identifier) (arguments))) (while_expression (parenthesized_expression (infix_expression (identifier) (operator_identifier) (identifier))) (block (identifier)))))) ``` -------------------------------- ### Lambda Expression (Scala 3 Syntax) Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/expressions.txt Provides an example of a basic object definition using Scala 3's new colon syntax, demonstrating a common structural element. ```Scala object O: ``` -------------------------------- ### Trait definitions Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/definitions.txt Explores different ways to define traits in Scala, including simple extensions, multiple inheritance with 'with', and the 'derives' clause for automatic derivation of type class instances. This section provides Scala code examples for various trait definition scenarios. ```Scala trait A extends B trait A extends B with C derives D trait T[U] { } trait T[U] extends V.W[U] { } ``` -------------------------------- ### Scala Class Initialization and Tree-sitter Grammar Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/definitions.txt Shows how class parameters are defined and how initialization expressions, such as assertions, are represented within a class body in Scala 3. Includes the corresponding Tree-sitter grammar for these constructs. ```Scala class A(val x: Int, val y: Int) { assert(x != 0) } ``` ```Tree-sitter Grammar (compilation_unit (class_definition (identifier) (class_parameters (class_parameter (identifier) (type_identifier)) (class_parameter (identifier) (type_identifier))) (template_body (call_expression (identifier) (arguments (infix_expression (identifier) (operator_identifier) (integer_literal))))))) ``` -------------------------------- ### Scala Chained Expressions and Tree-sitter Grammar Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/expressions.txt Illustrates method chaining in Scala, including single-line and multi-line invocations, and how these are represented in the Tree-sitter grammar. ```Scala def main() { val myList = List(1, 2, 3) myList.map(_ * 2) myList .map(_ * 2) myList .length } ``` ```Tree-sitter Grammar (compilation_unit (function_definition (identifier) (parameters) (block (val_definition (identifier) (call_expression (identifier) (arguments (integer_literal) (integer_literal) (integer_literal)))) (call_expression (field_expression (identifier) (identifier)) (arguments (infix_expression (wildcard) (operator_identifier) (integer_literal)))) (call_expression (field_expression (identifier) (identifier)) (arguments (infix_expression (wildcard) (operator_identifier) (integer_literal)))) (field_expression (identifier) (identifier))))) ``` -------------------------------- ### Define Top-Level Expressions in Scala Scripts Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/expressions.txt Examples of top-level variable definitions ('var', 'val'), assignments, string concatenation, conditional statements, and plugin declarations in Scala scripts, illustrating common scripting patterns. ```Scala var greeting = "Good bye" greeting = "Hello" val message = greeting + " from Scala script!" if true then println(message) addSbtPlugin("foo" % "bar" % "0.1") ``` ```Tree-sitter Grammar (compilation_unit (function_definition (identifier) (indented_block (if_expression (inline_modifier) (parenthesized_expression (identifier)) (block (match_expression (inline_modifier) (identifier) (case_block))))))) ``` -------------------------------- ### If Expressions in Scala 3 (New Syntax) Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/expressions.txt Presents examples of if expressions using the new indented syntax introduced in Scala 3, including then and end if keywords, and block expressions within conditions. ```Scala class C: def main() = if a then b() end if if val c = false c then d() 1 else if f then g() else h() if true then () // comment else () ``` -------------------------------- ### Nested Inline If and Match Expressions in Scala 3 Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/expressions.txt Provides an example of nesting 'inline if' and 'inline match' expressions in Scala 3, demonstrating how complex compile-time logic can be structured. ```Scala def hello = inline if (x) { inline x match { } } ``` ```Tree-sitter Grammar (compilation_unit (function_definition (identifier) (indented_block (if_expression (inline_modifier) (parenthesized_expression (identifier)) (block) (block))))) ``` -------------------------------- ### Basic Scala Object Definitions Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/definitions.txt Illustrates various forms of Scala object definitions, including simple objects, case objects, objects with `extends` clauses, and objects with structural types, along with their Tree-sitter parse trees. ```Scala // o1 object O1 { } case object O2 { } object O3 extends A { } object O4 extends { val a: A = b } object O5 { ;; ; ; } ``` ```Tree-sitter S-expression (compilation_unit (comment) (object_definition (identifier) (template_body)) (object_definition (identifier) (template_body)) (object_definition (identifier) (extends_clause (type_identifier)) (template_body)) (object_definition (identifier) (extends_clause (structural_type (val_definition (identifier) (type_identifier) (identifier))))) (object_definition (identifier) (template_body))) ``` -------------------------------- ### Define Infix Methods with Modifiers in Scala 3 Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/definitions.txt Shows how to define methods using the `infix` modifier in Scala 3, allowing them to be called in infix notation. This example also combines `inline` and `private` modifiers, demonstrating advanced method definition. ```Scala object Test: inline infix private def hello = 25 ``` ```Tree-sitter (compilation_unit (object_definition (identifier) (template_body (function_definition (modifiers (inline_modifier) (infix_modifier) (access_modifier)))))) ``` -------------------------------- ### Scala Instance Creation Expressions and Tree-sitter Parse Tree Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/expressions.txt Demonstrates various forms of Scala instance creation (e.g., `new B`, `new D(e, f)`, anonymous class instances, and type ascriptions) and their corresponding Tree-sitter parse tree structures. ```Scala class C { def main() { val a = new B val c = new D(e, f) val e = new E: def app = 1 val f = new: def app = 1 val g = new G {} val i = new { "ok" } new Array: Array } } ``` ```Tree-sitter (S-expression) (compilation_unit (class_definition (identifier) (template_body (function_definition (identifier) (parameters) (block (val_definition (identifier) (instance_expression (type_identifier))) (val_definition (identifier) (instance_expression (type_identifier) (arguments (identifier) (identifier)))) (val_definition (identifier) (instance_expression (type_identifier) (template_body (function_definition (identifier) (integer_literal))))) (val_definition (identifier) (instance_expression (template_body (function_definition (identifier) (integer_literal))))) (val_definition (identifier) (instance_expression (type_identifier) (template_body))) (val_definition (identifier) (instance_expression (template_body (string)))) (ascription_expression (instance_expression (type_identifier)) (type_identifier))))))) ``` -------------------------------- ### Scala Wildcard Imports (Scala 3 Syntax) Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/definitions.txt Illustrates the Scala 3 wildcard import syntax (`*`) and how Tree-sitter parses it into a `namespace_wildcard` node, even when combined with other specific imports. ```Scala import tools.nsc.classpath.* import a.b.*, b.c, c.* ``` ```Tree-sitter Grammar (compilation_unit (import_declaration (identifier) (identifier) (identifier) (namespace_wildcard)) (import_declaration (identifier) (identifier) (namespace_wildcard) (identifier) (identifier) (identifier) (namespace_wildcard))) ``` -------------------------------- ### Scala Do-While Loops Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/expressions.txt Examples of `do-while` loop syntax in Scala, showing both single-statement and block-style loop bodies, where the condition is evaluated after the loop body executes. ```Scala def f() = { do g(a, b) while(c && d) do { g(a, b) h(a, b) } while (c < d) } ``` ```Tree-sitter Grammar (compilation_unit (function_definition (identifier) (parameters) (block (do_while_expression (call_expression (identifier) (arguments (identifier) (identifier))) (parenthesized_expression (infix_expression (identifier) (operator_identifier) (identifier)))) (do_while_expression (block (call_expression (identifier) (arguments (identifier) (identifier))) (call_expression (identifier) (arguments (identifier))))))) ``` -------------------------------- ### Scala 3 For Comprehensions and Tree-sitter Grammar Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/expressions.txt Demonstrates the updated syntax for Scala 3 for comprehensions, including 'yield' and 'do' clauses, along with its corresponding Tree-sitter grammar structure. ```Scala def f(): Unit = for n <- nums yield n + 1 for a = 1 x <- xs y <- ys yield g(x, y) for annot <- annotations if shouldEmitAnnotation(annot) do () ``` ```Tree-sitter Grammar (compilation_unit (function_definition (identifier) (parameters) (type_identifier) (indented_block (for_expression (enumerators (enumerator (identifier) (identifier))) (infix_expression (identifier) (operator_identifier) (integer_literal))) (for_expression (enumerators (enumerator (identifier) (integer_literal)) (enumerator (identifier) (identifier)) (enumerator (identifier) (identifier))) (indented_block (call_expression (identifier) (arguments (identifier) (identifier))))) (for_expression (enumerators (enumerator (identifier) (identifier)) (enumerator (guard (call_expression (identifier) (arguments (identifier)))))) (indented_block (unit)))))) ``` -------------------------------- ### Scala 3 Call Expressions with Using Clauses and Instance Expressions Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/expressions.txt Illustrates call expressions using Scala 3 specific syntax, including 'using' clauses for contextual parameters and instance expressions defined inline as arguments. ```Scala class C { a(b, c)(using A.B) foo(new A: def a = new B: def b = 1) } ``` -------------------------------- ### Scala Wildcard Imports (Scala 2 Syntax) Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/definitions.txt Shows the traditional Scala wildcard import syntax (`_`) and its corresponding Tree-sitter representation using a `namespace_wildcard` node within an `import_declaration`. ```Scala import tools.nsc.classpath._ ``` ```Tree-sitter Grammar (compilation_unit (import_declaration (identifier) (identifier) (identifier) (namespace_wildcard))) ``` -------------------------------- ### Class definitions with parameters Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/definitions.txt Illustrates how to define classes with constructor parameters, including value parameters (val), implicit parameters, and parameters with annotations. This section covers both standard and annotated parameter lists, showing their representation in Tree-sitter grammar. ```Scala class Point( val x: Int, val y: Int, )(implicit coord: Coord) // TODO: The last argument should become class_parameters class A @Inject()(x: Int, y: Int) ``` ```Tree-sitter Grammar (compilation_unit (class_definition (identifier) (class_parameters (class_parameter (identifier) (type_identifier)) (class_parameter (identifier) (type_identifier))) (class_parameters (class_parameter (identifier) (type_identifier)))) (comment) (class_definition (identifier) (annotation (type_identifier) (arguments) (arguments (ascription_expression (identifier) (type_identifier)) (ascription_expression (identifier) (type_identifier)))))) ``` -------------------------------- ### Scala Call Expressions Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/expressions.txt Demonstrates various forms of call expressions in Scala, including simple calls, calls with multiple arguments, operator calls, method chaining, and calls with block arguments. ```Scala class C { def main() { a() a( 123, 234, ) ::(123) a(b, c) a.map(x => x + 1) a { 123 } a { b } a.map{ x => x + 1 } /* comment */ a.exists { case ((i, _)) => i } } } ``` -------------------------------- ### Class definitions with parameters (Scala 3 syntax) Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/definitions.txt Shows class definitions with parameters specifically using Scala 3's 'using' clause for context parameters and various annotation placements. It highlights how annotations can be applied to class definitions with different argument structures and their corresponding Tree-sitter parse trees. ```Scala class Point(val x: Int, val y: Int)(using coord: Coord) // TODO: The last argument should become class_parameters class A @ann (x: Int, y: Int) // TODO: The last argument should become class_parameters class A @ann(1) (x: Int, y: Int) // TODO: The last argument should become class_parameters class A @ann(1)(1) (x: Int, y: Int) ``` ```Tree-sitter Grammar (compilation_unit (class_definition (identifier) (class_parameters (class_parameter (identifier) (type_identifier)) (class_parameter (identifier) (type_identifier))) (class_parameters (class_parameter (identifier) (type_identifier)))) (comment) (class_definition (identifier) (annotation (type_identifier) (arguments (ascription_expression (identifier) (type_identifier)) (ascription_expression (identifier) (type_identifier))))) (comment) (class_definition (identifier) (annotation (type_identifier) (arguments (integer_literal)) (arguments (ascription_expression (identifier) (type_identifier)) (ascription_expression (identifier) (type_identifier))))) (comment) (class_definition (identifier) (annotation (type_identifier) (arguments (integer_literal)) (arguments (integer_literal)) (arguments (ascription_expression (identifier) (type_identifier)) (ascription_expression (identifier) (type_identifier)))))) ``` -------------------------------- ### Tree-sitter Grammar for Given and Trait Definitions Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/definitions.txt Illustrates the Tree-sitter grammar structure for various forms of `given` definitions (contextual abstractions) and `trait` definitions in Scala, including those with parameters, type parameters, and template bodies. ```Tree-sitter Grammar (with_template_body (function_definition (identifier) (parameters (parameter (identifier) (generic_type (type_identifier) (type_arguments (type_identifier))))) (type_identifier) (integer_literal)))) (given_definition (identifier) (parameters (parameter (identifier) (type_identifier))) (type_identifier) (identifier)) (given_definition (type_identifier) (identifier)) (given_definition (generic_type (type_identifier) (type_arguments (type_identifier))) (identifier)) (given_definition (generic_type (type_identifier) (type_arguments (type_identifier))) (indented_cases (case_clause (identifier) (string)))) (given_definition (parameters (parameter (identifier) (type_identifier))) (type_identifier) (identifier)) (given_definition (type_parameters (identifier) (context_bound (type_identifier))) (generic_type (type_identifier) (type_arguments (generic_type (type_identifier) (type_arguments (type_identifier))))) (with_template_body (function_definition (identifier) (unit)))) (given_definition (type_parameters (identifier)) (parameters (generic_type (type_identifier) (type_arguments (type_identifier)))) (generic_type (type_identifier) (type_arguments (generic_type (type_identifier) (type_arguments (type_identifier))))) (with_template_body (function_definition (identifier) (unit)))) (trait_definition (identifier) (template_body (given_definition (identifier) (generic_type (type_identifier) (type_arguments (type_identifier)))))) (given_definition (given_conditional (parameter (identifier) (type_identifier))) (type_identifier) (call_expression (identifier) (arguments)))))) ``` -------------------------------- ### Scala Top-level Definitions and Tree-sitter Grammar Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/definitions.txt Demonstrates the Scala 3 syntax for defining top-level classes and functions, alongside their parsed Tree-sitter grammar representations. This includes a simple class with a method and a standalone function. ```Scala class A: def a() = () def a() = 1 ``` ```Tree-sitter Grammar (compilation_unit (class_definition (identifier) (template_body (function_definition (identifier) (parameters) (indented_block (unit))))) (function_definition (identifier) (parameters) (integer_literal))) ``` -------------------------------- ### Scala Package Declarations (Traditional Syntax) Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/definitions.txt Illustrates traditional Scala package declarations, including nested packages and packages containing object definitions. The Tree-sitter parse tree shows how these are structured using `package_clause` and `package_identifier` nodes. ```Scala package a.b package c { object A } package d { package e { object B } } ``` ```Tree-sitter S-expression (compilation_unit (package_clause (package_identifier (identifier) (identifier))) (package_clause (package_identifier (identifier)) (template_body (object_definition (identifier)))) (package_clause (package_identifier (identifier)) (template_body (package_clause (package_identifier (identifier)) (template_body (object_definition (identifier))))))) ``` -------------------------------- ### Postfix Expressions in Scala Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/expressions.txt Demonstrates Scala postfix expressions where a method is called without a dot or parentheses, and its corresponding Tree-sitter parse tree structure. ```Scala object O { val v = "value" toUpperCase val c = 1 + 2 toString } ``` ```Tree-sitter Grammar (compilation_unit (object_definition (identifier) (template_body (val_definition (identifier) (postfix_expression (string) (identifier))) (val_definition (identifier) (postfix_expression (infix_expression (integer_literal) (operator_identifier) (integer_literal)) (identifier)))))) ``` -------------------------------- ### Scala Try-Catch-Finally Expressions Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/expressions.txt Illustrates various forms of 'try-catch-finally' blocks in Scala for error handling. Examples include single-line 'finally' clauses, multi-line 'catch' blocks with pattern matching for exceptions, and combined 'catch' and 'finally' clauses. ```Scala def main() { try a() finally depth -= 1 try { doThing() } catch { case e: SomeException => prinlnt(e.message) case NonFatal(ex) => throw new SomeException(ex) } finally { tryAnotherThing() } try b() catch { case e => println(e) } finally doFinalThing() try a() finally b() } ``` -------------------------------- ### Lambda Expression in Scala Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/expressions.txt Shows various forms of Scala lambda expressions, including single-parameter, multi-parameter, wildcard, and block-style lambdas, and their corresponding Tree-sitter parse trees. ```Scala object O { val l = a => a + 1 val b = (x: Int, y: Int) => { x * y } val f = _ => 2 foo { i => val x = 2 + i x } { x => val y = 2 * x y * y } { b => if (c) d.e } { a => implicit b => b } { (_, a) => a } } ``` ```Tree-sitter Grammar (compilation_unit (object_definition (identifier) (template_body (val_definition (identifier) (lambda_expression (identifier) (infix_expression (identifier) (operator_identifier) (integer_literal)))) (val_definition (identifier) (lambda_expression (bindings (binding (identifier) (type_identifier)) (binding (identifier) (type_identifier))) (block (infix_expression (identifier) (operator_identifier) (identifier))))) (val_definition (identifier) (lambda_expression (wildcard) (integer_literal))) (call_expression (identifier) (block (lambda_expression (identifier) (indented_block (val_definition (identifier) (infix_expression (integer_literal) (operator_identifier) (identifier))) (identifier))))) (block (lambda_expression (identifier) (indented_block (val_definition (identifier) (infix_expression (integer_literal) (operator_identifier) (identifier))) (infix_expression (identifier) (operator_identifier) (identifier))))) (block (lambda_expression (identifier) (indented_block (if_expression (parenthesized_expression (identifier)) (field_expression (identifier) (identifier)))))) (block (lambda_expression (identifier) (lambda_expression (identifier) (identifier)))) (block (lambda_expression (bindings (binding (wildcard)) (binding (identifier))) (identifier)))))) ``` -------------------------------- ### Scala Variable Definitions with Initializers and Tree-sitter Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/definitions.txt Shows the basic syntax for defining a mutable 'var' field with an explicit type and an initial value in Scala, and its Tree-sitter parse tree. ```Scala class A { var b : Int = 1 } ``` ```Tree-sitter (compilation_unit (class_definition (identifier) (template_body (var_definition (identifier) (type_identifier) (integer_literal))))) ``` -------------------------------- ### Tree-sitter Scala Parse Tree Fragment: Case Clause Details Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/expressions.txt An example of a Tree-sitter parse tree fragment, illustrating the detailed structure of `case_clause` nodes, including `guard` conditions, `val_definition`, and nested patterns like `tuple_pattern` and `wildcard`. ```Tree-sitter (S-expression) (integer_literal) (string)) (case_clause (identifier) (guard (infix_expression (identifier) (operator_identifier) (integer_literal))) (val_definition (identifier) (string)) (identifier)) (case_clause (tuple_pattern (tuple_pattern (identifier) (wildcard)))) (identifier)) (case_clause (wildcard) (val_definition (identifier) (string)) (string))))))) ``` -------------------------------- ### Generic Functions in Scala and Tree-sitter Parse Tree Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/expressions.txt Demonstrates the syntax for defining and calling generic functions in Scala, including type arguments, and its corresponding Tree-sitter S-expression parse tree structure. ```Scala class C { def main() { a[ A1, A2, ]() List[Traversable[ClassPath]]() } } ``` ```Tree-sitter S-expression (compilation_unit (class_definition (identifier) (template_body (function_definition (identifier) (parameters) (block (call_expression (generic_function (identifier) (type_arguments (type_identifier) (type_identifier))) (arguments)) (call_expression (generic_function (identifier) (type_arguments (generic_type (type_identifier) (type_arguments (type_identifier))))) (arguments))))))) ``` -------------------------------- ### Scala Return Expressions Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/expressions.txt Illustrates explicit `return` statements in Scala functions. Shows returning `Unit` and returning a value after a side effect, including type casting with `asInstanceOf`. ```Scala def f: Unit = return def g(a: A): B = { f(); return null.asInstanceOf[B] } ``` ```Tree-sitter Grammar (compilation_unit (function_definition (identifier) (type_identifier) (return_expression)) (function_definition (identifier) (parameters (parameter (identifier) (type_identifier))) (type_identifier) (block (call_expression (identifier) (arguments)) (return_expression (generic_function (field_expression (null_literal) (identifier)) (type_arguments (type_identifier))))))) ``` -------------------------------- ### Tree-sitter Scala: Interpolated String Parse Tree Example Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/literals.txt An example of the Tree-sitter parse tree structure for a Scala interpolated string, demonstrating how escape sequences and interpolations with identifiers and blocks are represented. ```Tree-sitter S-expression (interpolated_string (escape_sequence) (interpolation (identifier)) (escape_sequence) (escape_sequence) (escape_sequence)))) (val_definition (identifier) (interpolated_string_expression (identifier) (interpolated_string (escape_sequence) (interpolation (block (identifier))))))) ``` -------------------------------- ### Scala 2 For Comprehensions and Tree-sitter Grammar Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/expressions.txt Illustrates the syntax of Scala 2 for comprehensions, including 'yield', multiple generators, 'val' definitions, 'if' guards, and pattern matching, alongside its Tree-sitter grammar representation. ```Scala def f() = { for (n <- nums) yield n + 1 for (x <- xs; y <- ys) g(x, y) for { x <- xs a = b + c y <- g() if d (t, u) = y } yield { h(x, y) } } ``` ```Tree-sitter Grammar (compilation_unit (function_definition (identifier) (parameters) (block (for_expression (enumerators (enumerator (identifier) (identifier))) (infix_expression (identifier) (operator_identifier) (integer_literal))) (for_expression (enumerators (enumerator (identifier) (identifier)) (enumerator (identifier) (identifier))) (call_expression (identifier) (arguments (identifier) (identifier)))) (for_expression (enumerators (enumerator (identifier) (identifier)) (enumerator (identifier) (infix_expression (identifier) (operator_identifier) (identifier))) (enumerator (identifier) (call_expression (identifier) (arguments)) (guard (identifier))) (enumerator (tuple_pattern (identifier) (identifier)) (identifier))) (block (call_expression (identifier) (arguments (identifier) (identifier)))))))) ``` -------------------------------- ### Scala 3 Try-Catch-Finally Expressions with Indentation Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/expressions.txt Showcases 'try-catch-finally' expressions using Scala 3's significant indentation syntax. This includes examples of single-line and multi-line 'try', 'catch', and 'finally' blocks where curly braces are replaced by indentation, demonstrating modern Scala error handling patterns. ```Scala def main(): Unit = try a() finally depth -= 1 try a() b() catch case e: SomeException => prinlnt(e.message) case NonFatal(ex) => throw new SomeException(ex) finally tryAnotherThing() tryAnotherThing2() try a catch case b: C => e finally f try a catch case b: C => e ``` -------------------------------- ### Scala Package Object Definition Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/definitions.txt Illustrates the definition of Scala package objects, including nested package objects and those with extensions and member definitions. Shows how Tree-sitter parses these constructs into `package_object` nodes with identifiers, extends clauses, and template bodies containing definitions. ```Scala package object d extends A { val hello: String = "there" } package object p1 { package object p2 { val a = 1 } } ``` ```Tree-sitter Grammar (compilation_unit (package_object (identifier) (extends_clause (type_identifier)) (template_body (val_definition (identifier) (type_identifier) (string)))) (package_object (identifier) (template_body (package_object (identifier) (template_body (val_definition (identifier) (integer_literal))))))) ``` -------------------------------- ### Scala Using Directives and Tree-sitter Parse Tree Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/comments.txt Examples of Scala 'using' directives for JVM, Scala version, dependencies, and exclusions, showing how Tree-sitter identifies them within comments. ```Scala //> using jvm graalvm:21 //> using scala 3.3.0 //> using dep foo:bar:1,2,3,url=https://github.com //> using exclude "examples/*" "*/resources/*" // > just a comment ``` ```Tree-sitter (compilation_unit (comment (using_directive (using_directive_key) (using_directive_value))) (comment (using_directive (using_directive_key) (using_directive_value))) (comment (using_directive (using_directive_key) (using_directive_value))) (comment (using_directive (using_directive_key) (using_directive_value))) (comment)) ``` -------------------------------- ### Assignment Expressions in Scala and Tree-sitter Parse Tree Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/expressions.txt Illustrates basic assignment operations in Scala, including simple variable assignment and assignment to an indexed element, along with their Tree-sitter S-expression parse tree representations. ```Scala class C { def main() { a = b a(1) = c } } ``` ```Tree-sitter S-expression (compilation_unit (class_definition (identifier) (template_body (function_definition (identifier) (parameters) (block (assignment_expression (identifier) (identifier)) (assignment_expression (call_expression (identifier) (arguments (integer_literal))) (identifier))))))) ``` -------------------------------- ### Scala 3 Enum Definition with Annotations Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/definitions.txt Presents an example of an enum definition in Scala 3, including annotations, type parameters, extension of a Java enumeration, and derivation clauses. This snippet focuses on the Scala syntax without its Tree-sitter grammar representation. ```Scala @A enum Hello[Y] extends java.Enumeration derives Codec, Eq { @A("") @B case World, You ``` -------------------------------- ### Scala Import Declaration with Renaming Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/definitions.txt Demonstrates parsing a Scala import declaration that renames an imported member using the `=> _` syntax, showing its corresponding Tree-sitter S-expression structure. ```Scala import lang.System.{lineSeparator => _} ``` ```Tree-sitter S-expression (compilation_unit (import_declaration (identifier) (identifier) (namespace_selectors (arrow_renamed_identifier (identifier) (identifier))))) ``` -------------------------------- ### Ascription Expression in Scala Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/expressions.txt Illustrates Scala ascription expressions used for type casting, type annotations, and repeated parameters, along with their Tree-sitter parse tree representation. ```Scala object O { val l = a: List val x = 2: Byte Nil: List[String] 3: _* x: @unchecked } ``` ```Tree-sitter Grammar (compilation_unit (object_definition (identifier) (template_body (val_definition (identifier) (ascription_expression (identifier) (type_identifier))) (val_definition (identifier) (ascription_expression (integer_literal) (type_identifier))) (ascription_expression (identifier) (generic_type (type_identifier) (type_arguments (type_identifier)))) (ascription_expression (integer_literal) (repeated_parameter_type (wildcard))) (ascription_expression (identifier) (annotation (type_identifier)))))) ``` -------------------------------- ### Scala 3 While Loops (Indented Syntax) Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/expressions.txt Demonstrates the new indented `while` loop syntax introduced in Scala 3, where `do` keyword replaces curly braces for block definition. ```Scala def f: Unit = while a do g() while val x = 1 b < x do () ``` ```Tree-sitter Grammar (compilation_unit (function_definition (identifier) (type_identifier) (indented_block (while_expression (identifier) (call_expression (identifier) (arguments))) (while_expression (indented_block (val_definition (identifier) (integer_literal)) (infix_expression (identifier) (operator_identifier) (identifier))) (indented_block (unit)))))) ``` -------------------------------- ### Scala Import Declarations Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/definitions.txt Demonstrates various Scala import declaration syntaxes, including single imports, multiple imports on one line, selective imports with curly braces, and imports of operators. The Tree-sitter grammar shows how these are represented as `import_declaration` nodes with identifiers and `namespace_selectors`. ```Scala import PartialFunction.condOpt import a.b, c.e import reflect.io.{Directory, File, Path} import a.{ b, } import Opts.+ import a.{ :: } ``` ```Tree-sitter Grammar (compilation_unit (import_declaration (identifier) (identifier)) (import_declaration (identifier) (identifier) (identifier) (identifier)) (import_declaration (identifier) (identifier) (namespace_selectors (identifier) (identifier) (identifier))) (import_declaration (identifier) (namespace_selectors (identifier))) (import_declaration (identifier) (operator_identifier)) (import_declaration (identifier) (namespace_selectors (operator_identifier)))) ``` -------------------------------- ### Scala Package Declaration with Semicolon Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/definitions.txt Demonstrates a Scala package declaration terminated by a semicolon, a less common but valid syntax. The Tree-sitter output confirms it's parsed as a `package_clause`. ```Scala package a.b; ``` ```Tree-sitter S-expression (compilation_unit (package_clause (package_identifier (identifier) (identifier)))) ``` -------------------------------- ### Scala 3 Object Definition with Derives Clause Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/definitions.txt Shows a Scala 3 `case object` definition that includes both an `extends` clause and a `derives` clause, demonstrating how Tree-sitter parses these modern Scala features. ```Scala case object A extends B derives C, D {} ``` ```Tree-sitter S-expression (compilation_unit (object_definition (identifier) (extends_clause (type_identifier)) (derives_clause (type_identifier) (type_identifier)) (template_body))) ``` -------------------------------- ### Implement Inline Match Expressions in Scala 3 Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/expressions.txt Illustrates the use of 'inline match' expressions in Scala 3 for compile-time pattern matching, allowing for conditional compilation based on constant values. ```Scala def hello = inline c match { case 1 => } ``` ```Tree-sitter Grammar (compilation_unit (class_definition (identifier) (template_body (function_definition (modifiers (inline_modifier)) (identifier) (parameters (parameter (inline_modifier) (identifier) (type_identifier))) (type_identifier) (indented_block (splice_expression (call_expression (identifier) (arguments (quote_expression (identifier))))))) (function_definition (identifier) (indented_block (quote_expression (identifier))))))) ``` -------------------------------- ### Define and Use Macros in Scala 3 Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/expressions.txt Demonstrates the definition and usage of 'inline def' for macros in Scala 3, including 'inspectCode' and quote/splice expressions, showcasing compile-time code generation. ```Scala class A: inline def inspect(inline a: Any): Any = ${ inspectCode('a) } def foo = '{ $b } ``` ```Tree-sitter Grammar (compilation_unit (class_definition (identifier) (template_body (function_definition (identifier) (type_identifier) (indented_block (macro_body (field_expression (identifier) (identifier)))))))) ``` -------------------------------- ### Define and Parse Scala 3 Extension Methods with Tree-sitter Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/definitions.txt This snippet demonstrates the Scala 3 syntax for defining extension methods within objects and traits. It also provides the corresponding Tree-sitter grammar structure, showing how these language constructs are parsed into an abstract syntax tree. ```Scala object A: extension (c: C) def foo: Int = 1 extension [A1](d: D) def foo = "foo" trait B: extension (x: Int) def bar: String ``` ```Tree-sitter Grammar (compilation_unit (object_definition (identifier) (template_body (extension_definition (parameters (parameter (identifier) (type_identifier))) (function_definition (identifier) (type_identifier) (integer_literal))) (extension_definition (type_parameters (identifier)) (parameters (parameter (identifier) (type_identifier))) (function_definition (identifier) (string))))) (trait_definition (identifier) (template_body (extension_definition (parameters (parameter (identifier) (type_identifier))) (function_declaration (identifier) (type_identifier)))))) ``` -------------------------------- ### Define Open Class in Scala 3 Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/definitions.txt Demonstrates the syntax for defining an `open class` in Scala 3, including a constructor parameter and a method. This snippet is paired with its corresponding Tree-sitter grammar representation, showing how the class structure, modifiers, parameters, and methods are parsed. ```Scala open class Test(a: Int): def test = 25 end Test ``` ```Tree-sitter Grammar (compilation_unit (class_definition (modifiers (open_modifier)) (identifier) (class_parameters (class_parameter (identifier) (type_identifier))) (template_body (function_definition (identifier) (integer_literal))))) ``` -------------------------------- ### Scala Lambda Expressions with Contextual Abstractions Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/expressions.txt Illustrates Scala lambda expressions, including the new contextual function type `?=>` (context function type) and polymorphic lambda expressions with context bounds `[X: Ord]`. ```Scala val f = (a: Int) ?=> (b: Int) => b val less: Comparer = [X: Ord] => (x: X, y: X) => ??? ``` ```Tree-sitter Grammar (compilation_unit (object_definition (identifier) (template_body (val_definition (identifier) (lambda_expression (bindings (binding (identifier) (type_identifier))) (lambda_expression (bindings (binding (identifier) (type_identifier))) (identifier)))) (val_definition (identifier) (type_identifier) (lambda_expression (type_parameters (identifier) (context_bound (type_identifier))) (bindings (binding (identifier) (type_identifier)) (binding (identifier) (type_identifier))) (operator_identifier)))))) ``` -------------------------------- ### Scala 3 Wildcard and Wildcard Givens Imports Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/definitions.txt Demonstrates Scala 3's advanced import syntax for wildcard and wildcard `given` imports. The Tree-sitter grammar shows how both `*` and `given` are represented as `namespace_wildcard` nodes within `namespace_selectors`. ```Scala import tools.nsc.classpath.{*, given} import tools.given ``` ```Tree-sitter Grammar (compilation_unit (import_declaration (identifier) (identifier) (identifier) (namespace_selectors (namespace_wildcard) (namespace_wildcard))) (import_declaration (identifier) (namespace_wildcard))) ``` -------------------------------- ### Scala Infix Expressions and Tree-sitter Parse Tree Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/expressions.txt Shows how Scala infix expressions, including custom operators and chained operations, are parsed into Tree-sitter's `infix_expression` nodes. ```Scala class C { def main() { a = b max c d + e + f a ** b } } ``` ```Tree-sitter (S-expression) (compilation_unit (class_definition (identifier) (template_body (function_definition (identifier) (parameters) (block (assignment_expression (identifier) (infix_expression (identifier) (identifier) (identifier))) (infix_expression (infix_expression (identifier) (operator_identifier) (identifier)) (operator_identifier) (identifier)) (infix_expression (identifier) (operator_identifier) (identifier))))))) ``` -------------------------------- ### Scala 3 Match Expression Parsing with Tree-sitter Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/expressions.txt Illustrates Tree-sitter parsing of Scala 3 `match` expressions, highlighting the indented syntax. This snippet shows the Scala code and its corresponding Tree-sitter S-expression. ```Scala def matchTest(x: Int): String = x match case 0 => case 1 => "one"; "uno" case 2 => "two" case x if x == 3 => val x = "3" x case ((i, _)) => i case _ => val x = "many" "more" ``` ```Tree-sitter Grammar (compilation_unit (function_definition (identifier) (parameters (parameter (identifier) (type_identifier))) (type_identifier) (indented_block (match_expression (identifier) (indented_cases (case_clause (integer_literal)) (case_clause (integer_literal) (string) (string)) (case_clause (integer_literal) (string)) (case_clause ``` -------------------------------- ### Define Enum Case Classes and Simple Cases in Scala Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/definitions.txt Illustrates the syntax for defining enum cases in Scala, including full case classes with type parameters and constructor parameters, and simple enum cases with inheritance and arguments. Shows how annotations can be applied to enum cases. ```Scala @A case Test[A](bla: Int, yo: String) extends Hello[A] case T extends Hello[String](25) ``` ```Tree-sitter (compilation_unit (enum_definition (annotation (type_identifier)) (identifier) (type_parameters (identifier)) (extends_clause (stable_type_identifier (identifier) (type_identifier))) (derives_clause (type_identifier) (type_identifier)) (enum_body (enum_case_definitions (annotation (type_identifier) (arguments (string))) (annotation (type_identifier)) (simple_enum_case (identifier)) (simple_enum_case (identifier))) (enum_case_definitions (annotation (type_identifier)) (full_enum_case (identifier) (type_parameters (identifier)) (class_parameters (class_parameter (identifier) (type_identifier)) (class_parameter (identifier) (type_identifier)))) (extends_clause (generic_type (type_identifier) (type_arguments (type_identifier)))))) (enum_case_definitions (simple_enum_case (identifier) (extends_clause (generic_type (type_identifier) (type_arguments (type_identifier))) (arguments (integer_literal)))))))) ``` -------------------------------- ### Partial Tree-sitter Grammar for Scala Type Definitions Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/definitions.txt This initial snippet provides a partial Tree-sitter grammar structure for various forms of type definitions in Scala, including those with type parameters, demonstrating the raw parse tree output. ```Tree-sitter Grammar (type_identifier) (type_identifier)) (type_definition (type_identifier) (type_parameters (identifier)) (type_identifier)) (type_definition (type_identifier)) (type_definition (type_identifier) (type_parameters (identifier)))))) ``` -------------------------------- ### Parse Scala Tuple Literals with Tree-sitter Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/literals.txt Presents an example of a multi-element tuple literal in Scala and its corresponding Tree-sitter grammar, highlighting how `tuple_expression` nodes are formed from `integer_literal` elements. ```Scala val x = ( 1, 2, 3, ) ``` ```Tree-sitter Grammar (compilation_unit (val_definition (identifier) (tuple_expression (integer_literal) (integer_literal) (integer_literal)))) ``` -------------------------------- ### Scala Type Definitions and Aliases with Tree-sitter Parse Tree Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/definitions.txt Demonstrates various forms of type definitions and type aliases within a Scala class, including simple aliases, generic type aliases, and abstract type members, along with their Tree-sitter parse tree representations. ```Scala class A { type B = C type D[E] = F type Abs type Beta[B] } ``` ```Tree-sitter (compilation_unit (class_definition (identifier) (template_body (type_definition ``` -------------------------------- ### Scala Unit Expressions Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/expressions.txt Demonstrates the use of the `Unit` type in Scala, representing a value that carries no meaningful information, similar to `void` in other languages. Shows `Unit` literal `()` and `Unit` return type for functions. ```Scala val x = () def f(): Unit = { ( ); } ``` ```Tree-sitter Grammar (compilation_unit (val_definition (identifier) (unit)) (function_definition (identifier) (parameters) (type_identifier) (block (unit)))) ``` -------------------------------- ### Scala Function with Optional Parameters and Tree-sitter Grammar Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/definitions.txt Illustrates the definition of a Scala function with optional parameters, showing how default values are assigned. The Tree-sitter grammar representation for such function definitions is also provided. ```Scala def mkLines(header: String, indented: Boolean = false, repeated: Long = 0L): String = {} ``` ```Tree-sitter Grammar (compilation_unit (function_definition (identifier) (parameters (parameter (identifier) (type_identifier)) (parameter (identifier) (type_identifier) (boolean_literal)) (parameter (identifier) (type_identifier) (integer_literal))) (type_identifier) (block))) ``` -------------------------------- ### Scala If Expressions Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/expressions.txt Demonstrates basic conditional 'if' expressions in Scala, including the use of logical operators '||' (OR) and '&&' (AND) with 'then' clauses. These snippets show simple boolean conditions and field access. ```Scala if (a) || b(c) then return true if (a) && b.c then () ``` -------------------------------- ### Scala 3 Variable Definitions with Indented Blocks and Tree-sitter Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/definitions.txt Illustrates 'var' definitions in Scala 3, including cases where the initializer is an indented block containing other expressions or definitions, and their Tree-sitter parse trees. ```Scala class A: var b: Int = 1 var c: Int = val d = 2 d ``` ```Tree-sitter (compilation_unit (class_definition (identifier) (template_body (var_definition (identifier) (type_identifier) (integer_literal)) (var_definition (identifier) (type_identifier) (indented_block (val_definition (identifier) (integer_literal)) (identifier)))))) ``` -------------------------------- ### Scala Prefix Expressions and Tree-sitter Parse Tree Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/expressions.txt Illustrates how Scala prefix expressions (e.g., `!a`, `+a`) are represented in the Tree-sitter parse tree. Note: The provided Tree-sitter snippet for this section is incomplete. ```Scala class C { def main() { !a +a + b } } ``` ```Tree-sitter (S-expression) (compilation_unit (class_definition (identifier) (template_body (function_definition ``` -------------------------------- ### Scala 3 Package Declarations (Indentation Syntax) Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/definitions.txt Presents Scala 3's new indentation-based package syntax, including `end` markers. The Tree-sitter parse tree reflects these structural changes, showing `package_clause` and `template_body` nodes. ```Scala package a.b package c: object A package d: object A end d ``` ```Tree-sitter S-expression (compilation_unit (package_clause (package_identifier (identifier) (identifier))) (package_clause (package_identifier (identifier)) (template_body (object_definition (identifier)))) (package_clause (package_identifier (identifier)) (template_body ``` -------------------------------- ### Scala Value Declarations and Tree-sitter Parse Tree Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/definitions.txt Demonstrates the syntax for declaring multiple 'val' fields of the same type and single 'val' fields within a Scala class, without initial assignment, and their corresponding Tree-sitter parse trees. ```Scala class A { val b, c : Int val d : String } ``` ```Tree-sitter (compilation_unit (class_definition (identifier) (template_body (val_declaration (identifier) (identifier) (type_identifier)) (val_declaration (identifier) (type_identifier))))) ``` -------------------------------- ### Use Inline If Expressions in Scala 3 Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/expressions.txt Shows how to use 'inline if' expressions in Scala 3 for conditional compilation or compile-time evaluation, enabling code branches to be selected at compile time. ```Scala def hello = inline if (x) {} else {} ``` ```Tree-sitter Grammar (compilation_unit (function_definition (identifier) (indented_block (match_expression (inline_modifier) (identifier) (case_block (case_clause (integer_literal))))))) ``` -------------------------------- ### Scala 3 Import Renaming (as keyword) Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/definitions.txt Illustrates Scala 3's import renaming syntax using `as`, including renaming to a specific identifier or to a wildcard (`_`). The Tree-sitter grammar captures this with `as_renamed_identifier` nodes. ```Scala import lang.System.{lineSeparator as EOL} import lang.System.lineSeparator as EOL import lang.System.lineSeparator as _ ``` ```Tree-sitter Grammar (compilation_unit (import_declaration (identifier) (identifier) (namespace_selectors (as_renamed_identifier (identifier) (identifier)))) (import_declaration (identifier) (identifier) (as_renamed_identifier (identifier) (identifier))) (import_declaration (identifier) (identifier) (as_renamed_identifier (identifier) (wildcard)))) ``` -------------------------------- ### Scala Variable Declarations and Tree-sitter Parse Tree Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/definitions.txt Demonstrates the syntax for declaring multiple 'var' fields of the same type and single 'var' fields within a Scala class, without initial assignment, and their corresponding Tree-sitter parse trees. ```Scala class A { var b, c : Int var d : String } ``` ```Tree-sitter (compilation_unit (class_definition (identifier) (template_body (var_declaration (identifier) (identifier) (type_identifier)) (var_declaration (identifier) (type_identifier))))) ``` -------------------------------- ### Scala Function Declarations and Their Tree-sitter Grammar Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/definitions.txt Illustrates various forms of function declarations in Scala, including a standard method, an operator method with type parameters, and a multi-line parameter list. The accompanying Tree-sitter grammar shows how these different declaration styles are represented in the parse tree, highlighting 'function_declaration', 'parameters', 'type_parameters', and 'operator_identifier'. ```Scala class A { def b(c: D) : E def <*[B](that: IO[B]): IO[A] def c (a: A): B } ``` ```Tree-sitter Grammar (compilation_unit (class_definition (identifier) (template_body (function_declaration (identifier) (parameters (parameter (identifier) (type_identifier))) (type_identifier)) (function_declaration (operator_identifier) (type_parameters (identifier)) (parameters (parameter (identifier) (generic_type (type_identifier) (type_arguments (type_identifier))))) (generic_type (type_identifier) (type_arguments (type_identifier)))) (function_declaration (identifier) (parameters (parameter (identifier) (type_identifier))) (type_identifier))))) ``` -------------------------------- ### Scala 3 Value Declarations with Indentation Syntax and Tree-sitter Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/definitions.txt Shows 'val' declarations using Scala 3's indentation-based syntax for class bodies, including how comments are parsed by Tree-sitter and their impact on the parse tree structure. ```Scala class A: // Comments that should not // influence indentation val b, c : Int val d : String ``` ```Tree-sitter (compilation_unit (class_definition (identifier) (template_body (comment) (comment) (val_declaration (identifier) (identifier) (type_identifier)) (val_declaration (identifier) (type_identifier))))) ``` -------------------------------- ### Define Inline Given Instances in Scala 3 Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/definitions.txt Demonstrates how to use the `inline` modifier with `given` instances in Scala 3. Inline givens are resolved and expanded at compile time, optimizing implicit resolution and usage. ```Scala inline given Test = new Test ``` ```Tree-sitter (compilation_unit (given_definition (modifiers (inline_modifier)) (type_identifier) (indented_block (instance_expression (type_identifier))))) ``` -------------------------------- ### General Scala Tree-sitter Grammar Fragment Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/expressions.txt A fragment of a Tree-sitter S-expression demonstrating various parsing constructs like colon_argument, indented_block, val_definition, call_expression, field_expression, indented_cases, case_clause, ascription_expression, and comment. ```Tree-sitter S-expression (identifier) (colon_argument (indented_block (val_definition (identifier) (integer_literal)) (call_expression (identifier) (arguments (identifier))))) (call_expression (field_expression (identifier) (identifier)) (colon_argument (indented_cases (case_clause (identifier) (identifier)) (comment)))) (ascription_expression (identifier) (type_identifier)) (comment) (call_expression (identifier) (colon_argument (indented_block (identifier))))))) ``` -------------------------------- ### Scala 3 Givens Imports by Type Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/definitions.txt Explores Scala 3's `given` import by type syntax, allowing specific `given` instances to be imported. The Tree-sitter grammar shows these as `type_identifier` nodes within `namespace_selectors`. ```Scala import tools.nsc.classpath.{given Test, given Test2, Test3} ``` ```Tree-sitter Grammar (compilation_unit (import_declaration (identifier) (identifier) (identifier) (namespace_selectors (type_identifier) (type_identifier) (identifier)))) ``` -------------------------------- ### Soft Keywords as Identifiers in Scala 3 Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/expressions.txt Illustrates how Scala 3's soft keywords ('infix', 'opaque', 'open', 'transparent', 'as', 'derives', 'end', 'throws', 'using') can be used as identifiers when followed by parentheses, typically for method calls, demonstrating language flexibility. ```Scala infix() opaque() open() transparent() as() derives() end() throws() using() ``` ```Tree-sitter Grammar (compilation_unit (var_definition (identifier) (string)) (assignment_expression (identifier) (string)) (val_definition (identifier) (infix_expression (identifier) (operator_identifier) (string))) (if_expression (boolean_literal) (indented_block (call_expression (identifier) (arguments (identifier))))) (call_expression (identifier) (arguments (infix_expression (infix_expression (string) (operator_identifier) (string)) (operator_identifier) (string))))) ``` -------------------------------- ### Parse Scala Boolean Literals and Expressions with Tree-sitter Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/literals.txt Shows examples of boolean literal definitions and their usage in function parameters and infix expressions in Scala, along with the Tree-sitter grammar for `boolean_literal` and `infix_expression`. ```Scala val myBool = true def foo(a: Boolean = false) = a && true ``` ```Tree-sitter Grammar (compilation_unit (val_definition (identifier) (boolean_literal)) (function_definition (identifier) (parameters (parameter (identifier) (type_identifier) (boolean_literal))) (infix_expression (identifier) (operator_identifier) (boolean_literal)))) ``` -------------------------------- ### Scala Identifiers with Dollar Signs Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/definitions.txt Illustrates the use of the dollar sign (`$`) within Scala identifier names for classes and values. The Tree-sitter parse tree shows these are recognized as standard `identifier` nodes. ```Scala class $A$B$ { val b$, c$ : Int val d$ : String } ``` ```Tree-sitter S-expression (compilation_unit (class_definition (identifier) (template_body (val_declaration (identifier) (identifier) (type_identifier)) (val_declaration (identifier) (type_identifier))))) ``` -------------------------------- ### Scala Value and Variable Definitions with Initializers and Tree-sitter Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/definitions.txt Illustrates various ways to define 'val' (immutable) and 'var' (mutable) fields in Scala, including single and multiple definitions with explicit types and initializers, along with their Tree-sitter representations. ```Scala class A { val b = 1 val c : String = "d" val a, b, c: T3 = triple var a, b, c = triple } ``` ```Tree-sitter (compilation_unit (class_definition (identifier) (template_body (val_definition (identifier) (integer_literal)) (val_definition (identifier) (type_identifier) (string)) (val_definition (identifiers (identifier) (identifier) (identifier)) (type_identifier) (identifier)) (var_definition (identifiers (identifier) (identifier) (identifier)) (identifier))))) ``` -------------------------------- ### Define and Parse Scala 3 Given Instance Definitions with Tree-sitter Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/definitions.txt This snippet illustrates various forms of Scala 3 'given' instance definitions, including named, anonymous, and contextual givens. It is paired with the Tree-sitter grammar representation, detailing how these complex implicit definitions are structured in the parse tree. ```Scala object A: given a: A = x given intFoo: CanFoo[Int] with def foo(x: Int): Int = 0 given intFoo: CanFoo[Int]: def foo(x: Int): Int = 0 given CanFoo[Int] with def foo(x: Int): Int = 0 private given listFoo[A1](using ev: CanFoo[A1]): CanFoo[List[A1]] with def foo(xs: List[A1]): Int = 0 given foo(using ev: Foo) : Foo = ev given Context = ctx given Context[T] = ctx given Foo[Int] = case n => "123" given (using a: Int): Int = a given [A: Foo]: Foo[List[A]] with def x = () given [A](using Foo[A]): Foo[List[A]] with def x = () trait B: given c: Context[T] given (config: Config) => Factory = ConcreteFactory() ``` ```Tree-sitter Grammar (compilation_unit (object_definition (identifier) (template_body (given_definition (identifier) (type_identifier) (identifier)) (given_definition (identifier) (generic_type (type_identifier) (type_arguments (type_identifier))) (with_template_body (function_definition (identifier) (parameters (parameter (identifier) (type_identifier))) (type_identifier) (integer_literal)))) (given_definition (identifier) (generic_type (type_identifier) (type_arguments (type_identifier))) (with_template_body (function_definition (identifier) (parameters (parameter (identifier) (type_identifier))) (type_identifier) (integer_literal)))) (given_definition (generic_type (type_identifier) (type_arguments (type_identifier))) (with_template_body (function_definition (identifier) (parameters (parameter (identifier) (type_identifier))) (type_identifier) (integer_literal)))) (given_definition (modifiers (access_modifier)) (identifier) (type_parameters (identifier)) (parameters (parameter (identifier) (generic_type (type_identifier) (type_arguments (type_identifier))))) (generic_type (type_identifier) (type_arguments (generic_type (type_identifier) (type_arguments (type_identifier))))) ``` -------------------------------- ### Capture patterns Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/patterns.txt Demonstrates various forms of capture patterns in Scala, allowing a matched value or sub-pattern to be bound to a variable. Examples include simple captures, nested captures, and captures with infix and sequence patterns. ```Scala val x = y match { case a @ B(1) => a case b @ C(d @ (e @ X, _: Y)) => e case req @ (POST | GET) -> Root / "test" => 5 case Array(a: Type, _@_*) => y } ``` ```Tree-sitter Grammar (compilation_unit (val_definition (identifier) (match_expression (identifier) (case_block (case_clause (capture_pattern (identifier) (case_class_pattern (type_identifier) (integer_literal))) (identifier)) (case_clause (capture_pattern (identifier) (case_class_pattern (type_identifier) (capture_pattern (identifier) (tuple_pattern (capture_pattern (identifier) (identifier)) (typed_pattern (wildcard) (type_identifier)))))) (identifier)) (case_clause (infix_pattern (infix_pattern (capture_pattern (identifier) (tuple_pattern (alternative_pattern (identifier) (identifier)))) (operator_identifier) (identifier)) (operator_identifier) (string)) (integer_literal)) (case_clause (case_class_pattern (type_identifier) (typed_pattern (identifier) (type_identifier)) (repeat_pattern (capture_pattern (wildcard) (wildcard)))) (identifier)))))) ``` -------------------------------- ### Test Syntax Highlighting Locally with tree-sitter Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/CONTRIBUTING.md Uses the `tree-sitter highlight` command to display the syntax highlighting output for a given Scala file. This is useful for local testing and generating expected output for highlight tests. ```Shell tree-sitter highlight some/scala/file.scala ``` -------------------------------- ### Scala 3 For Expression with Given Pattern and Tree-sitter Parse Tree Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/patterns.txt Illustrates a Scala 3 `for` expression using the `given` pattern, followed by its complete Tree-sitter S-expression parse tree. This example demonstrates how Tree-sitter parses and represents complex Scala 3 language features. ```Scala for given Int <- Some(1) yield () ``` ```Tree-sitter S-expression (compilation_unit (for_expression (enumerators (enumerator (given_pattern (type_identifier)) (call_expression (identifier) (arguments (integer_literal)))))) (unit))) ``` -------------------------------- ### Scala Integer Literals and Tree-sitter Parse Trees Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/literals.txt Examples of various Scala integer literals, including decimal, hexadecimal, long suffixes, and digit separators, along with their Tree-sitter parse tree representations. ```Scala val i1 = 0 val i2 = 1234 val i3 = -0xF2 val i4 = 0XA0 val l1 = -0l val l2 = 1234L val l3 = 0xF23l val l4 = 0XA03L val l5 = 150_000_000 val l6 = 0xFF_FF_FF ``` ```Tree-sitter S-expression (compilation_unit (val_definition (identifier) (integer_literal)) (val_definition (identifier) (integer_literal)) (val_definition (identifier) (integer_literal)) (val_definition (identifier) (integer_literal)) (val_definition (identifier) (integer_literal)) (val_definition (identifier) (integer_literal)) (val_definition (identifier) (integer_literal)) (val_definition (identifier) (integer_literal)) (val_definition (identifier) (integer_literal)) (val_definition (identifier) (integer_literal))) ``` -------------------------------- ### Subclass definitions (Scala 3 syntax) Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/definitions.txt Demonstrates how to define a class that extends multiple traits or classes using the Scala 3 syntax, which allows for comma-separated parent types in the extends clause. The accompanying Tree-sitter grammar illustrates how these multi-parent extensions are parsed. ```Scala class A extends B, C: 1 end A ``` ```Tree-sitter Grammar (class_definition (identifier) (extends_clause (type_identifier) (arguments) (type_identifier))) (compilation_unit (class_definition (identifier) (extends_clause (type_identifier) (type_identifier)) (template_body (integer_literal)))) ``` -------------------------------- ### Scala Field Expressions and Tree-sitter Parse Tree Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/expressions.txt Illustrates how Scala field access expressions (e.g., `a.b`, `a.b.d`) are represented in the Tree-sitter parse tree, including handling of comments within chained field access. ```Scala class C { def main() { a.b = c a.b.d a .b // comment1 /* comment2 */ .c } } ``` ```Tree-sitter (S-expression) (compilation_unit (class_definition (identifier) (template_body (function_definition (identifier) (parameters) (block (assignment_expression (field_expression (identifier) (identifier)) (identifier)) (field_expression (field_expression (identifier) (identifier)) (identifier)) (field_expression (field_expression (identifier) (identifier)) (comment) (block_comment) (identifier))))))) ``` -------------------------------- ### Tree-sitter Parse Trees for Scala Trait Definitions Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/definitions.txt Illustrates various Tree-sitter parse tree structures for Scala trait definitions, including those with 'extends_clause', 'type_parameters', and 'derives_clause', showcasing different levels of complexity in the grammar's recognition. ```Tree-sitter (compilation_unit (trait_definition (identifier) (extends_clause (type_identifier))) (trait_definition (identifier) (extends_clause (type_identifier) (type_identifier)) (derives_clause (type_identifier))) (trait_definition (identifier) (type_parameters (identifier)) (template_body)) (trait_definition (identifier) (type_parameters (identifier)) (extends_clause (generic_type (stable_type_identifier (identifier) (type_identifier)) (type_arguments (type_identifier)))) (template_body))) ``` -------------------------------- ### Scala 3 Class Definition with Modifiers and Derives Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/definitions.txt Demonstrates a Scala 3 `final case class` definition incorporating `extends` and `derives` clauses, including a stable type identifier in the `derives` clause, and its Tree-sitter representation. ```Scala final case class C() extends A derives B, C.D ``` ```Tree-sitter S-expression (compilation_unit (class_definition (modifiers) (identifier) (class_parameters) (extends_clause (type_identifier)) (derives_clause (type_identifier) (stable_type_identifier (identifier) (type_identifier))))) ``` -------------------------------- ### Scala Operator Identifiers and Symbols Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/definitions.txt Showcases various Scala operator identifiers, including symbolic names for types, values, and methods like `::`, `+:`, `:+`, `→`, and `##`. The Tree-sitter output distinguishes these as `operator_identifier` nodes, and correctly identifies comments. ```Scala type ::[+Ab] = scala.collection.immutable.::[Ab] val :: = scala.collection.immutable.:: val +: = scala.collection.+: val :+ = scala.collection.:+ def → = ??? val test = id.## val x = y ///////// // avoid matching slashes as operator ///////// ``` ```Tree-sitter S-expression (compilation_unit (type_definition (type_identifier) (type_parameters (covariant_type_parameter (identifier))) (generic_type (stable_type_identifier (stable_identifier (stable_identifier (identifier) (identifier)) (identifier)) (type_identifier)) (type_arguments (type_identifier)))) (val_definition (operator_identifier) (field_expression (field_expression (field_expression (identifier) (identifier)) (identifier)) (operator_identifier))) (val_definition (operator_identifier) (field_expression (field_expression (identifier) (identifier)) (operator_identifier))) (val_definition (operator_identifier) (field_expression (field_expression (identifier) (identifier)) (operator_identifier))) (function_definition (operator_identifier) (operator_identifier)) (val_definition (identifier) (field_expression (identifier) (operator_identifier))) (val_definition (identifier) (identifier)) (comment) (comment) (comment)) ``` -------------------------------- ### Scala 2 Match Expression Parsing with Tree-sitter Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/expressions.txt Demonstrates how Tree-sitter parses Scala 2 `match` expressions, including various `case` patterns like literals, blocks, guards, tuples, and string interpolations. This snippet shows the Scala code and its corresponding Tree-sitter S-expression. ```Scala def matchTest(x: Int): String = x match { case 0 => case 1 => "one"; "uno" case 2 => "two" case 3 => { "3" } case 4 => ; case A if a == 1 => case A if a == 2 => 2 case ((i, _)) => i case s"$l1 -- $l2" => l1 + l2 case _ => val x = "many" "more" case a if b => c } ``` ```Tree-sitter Grammar (compilation_unit (function_definition (identifier) (parameters (parameter (identifier) (type_identifier))) (type_identifier) (match_expression (identifier) (case_block (case_clause (integer_literal)) (case_clause (integer_literal) (string) (string)) (case_clause (integer_literal) (string)) (case_clause (integer_literal) (block (string))) (case_clause (integer_literal)) (case_clause (identifier) (guard (infix_expression (identifier) (operator_identifier) (integer_literal)))) (case_clause (identifier) (guard (infix_expression (identifier) (operator_identifier) (integer_literal))) (integer_literal)) (case_clause (tuple_pattern (tuple_pattern (identifier) (wildcard))) (identifier)) (case_clause (interpolated_string_expression (identifier) (interpolated_string (interpolation (identifier)) (interpolation (identifier)))) (infix_expression (identifier) (operator_identifier) (identifier))) (case_clause (wildcard) (val_definition (identifier) (string)) (string)) (case_clause (identifier) (guard (identifier)) (identifier)))))) ``` -------------------------------- ### If Expressions in Scala (Traditional Syntax) and Tree-sitter Parse Tree Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/expressions.txt Covers various forms of if-else if-else expressions in traditional Scala syntax, including single-line, block, and pattern matching within if conditions, and their unified Tree-sitter S-expression parse tree. ```Scala class C { def main() { if (a) b() if (c) { d() e() } else if (f) { g() } else { h() } } } def other() { if (a) { b }; else c() } def another() { if (a) b match { case _ => c } else d } ``` ```Tree-sitter S-expression (compilation_unit (class_definition (identifier) (template_body (function_definition (identifier) (parameters) (block (if_expression (parenthesized_expression (identifier)) (call_expression (identifier) (arguments))) (if_expression (parenthesized_expression (identifier)) (block (call_expression (identifier) (arguments)) (call_expression (identifier) (arguments))) (if_expression (parenthesized_expression (identifier)) (block (call_expression (identifier) (arguments))) (block (call_expression (identifier) (arguments))))))))) (function_definition (identifier) (parameters) (block (if_expression (parenthesized_expression (identifier)) (block (identifier)) (call_expression (identifier) (arguments))))) (function_definition (identifier) (parameters) (block (if_expression (parenthesized_expression (identifier)) (match_expression (identifier) (case_block (case_clause (wildcard) (identifier)))) (identifier))))) ``` -------------------------------- ### Scala 3 Type Definition with Context Bound and Tree-sitter Grammar Source: https://github.com/tree-sitter/tree-sitter-scala/blob/master/test/corpus/definitions.txt Demonstrates a type definition within a class in Scala 3 syntax, specifically showing a type member 'Element' with a context bound 'Order'. The corresponding Tree-sitter grammar illustrates how this structure is parsed, including 'class_definition', 'template_body', 'type_definition', and 'context_bound'. ```Scala class A: type Element: Order ``` ```Tree-sitter Grammar (compilation_unit (class_definition (identifier) (template_body (type_definition (type_identifier) (context_bound (type_identifier)))))) ```