Compare commits

..
Author SHA1 Message Date
sergeych aec4a3766e wasm generation bug workaround, docs and debugging tips 2026-01-24 18:10:49 +03:00
171 changed files with 2258 additions and 9243 deletions

No files matched your search

-6
View File
@@ -21,9 +21,3 @@ xcuserdata
debug.log debug.log
/build.log /build.log
/test.md /test.md
/build_output.txt
/build_output_full.txt
/check_output.txt
/compile_jvm_output.txt
/compile_metadata_output.txt
test_output*.txt
-11
View File
@@ -1,11 +0,0 @@
# Lyng Project Guidelines
This project uses the Lyng scripting language for multiplatform scripting.
## Coding in Lyng
When writing, refactoring, or analyzing Lyng code:
- **Reference**: Always use `LYNG_AI_SPEC.md` in the project root as the primary source of truth for syntax and idioms.
- **File Extensions**: Use `.lyng` for all script files.
- **Implicit Coroutines**: Remember that all Lyng functions are implicitly coroutines; do not look for `async/await`.
- **Everything is an Expression**: Leverage the fact that blocks, if-statements, and loops return values.
- **Maps vs Blocks**: Be careful: `{}` is a block/lambda, use `Map()` for an empty map.
-11
View File
@@ -2,17 +2,6 @@
### Unreleased ### Unreleased
- Language: Added `return` statement
- `return [expression]` exits the innermost enclosing callable (function or lambda).
- Supports non-local returns using `@label` syntax (e.g., `return@outer 42`).
- Named functions automatically provide their name as a label for non-local returns.
- Labeled lambdas: lambdas can be explicitly labeled using `@label { ... }`.
- Restriction: `return` is forbidden in shorthand function definitions (e.g., `fun f(x) = return x` is a syntax error).
- Control Flow: `return` and `break` are now protected from being caught by user-defined `try-catch` blocks in Lyng.
- Documentation: New `docs/return_statement.md` and updated `tutorial.md`.
- Language: stdlib improvements
- Added `with(self, block)` function to `root.lyng` which executes a block with `this` set to the provided object.
- Language: Abstract Classes and Interfaces - Language: Abstract Classes and Interfaces
- Support for `abstract` modifier on classes, methods, and variables. - Support for `abstract` modifier on classes, methods, and variables.
- Introduced `interface` as a synonym for `abstract class`, supporting full state (constructors, fields, `init` blocks) and implementation by parts via MI. - Introduced `interface` as a synonym for `abstract class`, supporting full state (constructors, fields, `init` blocks) and implementation by parts via MI.
-105
View File
@@ -1,105 +0,0 @@
# Lyng Language AI Specification (V1.3)
High-density specification for LLMs. Reference this for all Lyng code generation.
## 1. Core Philosophy & Syntax
- **Everything is an Expression**: Blocks, `if`, `when`, `for`, `while`, `do-while` return their last expression (or `void`).
- **Loops with `else`**: `for`, `while`, and `do-while` support an optional `else` block.
- `else` executes **only if** the loop finishes normally (without a `break`).
- `break <value>` exits the loop and sets its return value.
- Loop Return Value:
1. Value from `break <value>`.
2. Result of `else` block (if loop finished normally and `else` exists).
3. Result of the last iteration (if loop finished normally and no `else`).
4. `void` (if loop body never executed and no `else`).
- **Implicit Coroutines**: All functions are coroutines. No `async/await`. Use `launch { ... }` (returns `Deferred`) or `flow { ... }`.
- **Variables**: `val` (read-only), `var` (mutable). Supports late-init `val` in classes (must be assigned in `init` or body).
- **Null Safety**: `?` (nullable type), `?.` (safe access), `?( )` (safe invoke), `?{ }` (safe block invoke), `?[ ]` (safe index), `?:` or `??` (elvis), `?=` (assign-if-null).
- **Equality**: `==` (equals), `!=` (not equals), `===` (ref identity), `!==` (ref not identity).
- **Comparison**: `<`, `>`, `<=`, `>=`, `<=>` (shuttle/spaceship, returns -1, 0, 1).
- **Destructuring**: `val [a, b, rest...] = list`. Supports nested `[a, [b, c]]` and splats.
## 2. Object-Oriented Programming (OOP)
- **Multiple Inheritance**: Supported with **C3 MRO** (Python-style). Diamond-safe.
- **Header Arguments**: `class Foo(a, b) : Base(a)` defines fields `a`, `b` and passes `a` to `Base`.
- **Members**: `fun name(args) { ... }`, `val`, `var`, `static val`, `static fun`.
- **Properties (Get/Set)**: Pure accessors, no auto-backing fields.
```lyng
var age
get() = _age
private set(v) { if(v >= 0) _age = v }
// Laconic syntax:
val area get = π * r * r
```
- **Mandatory `override`**: Required for all members existing in the ancestor chain.
- **Visibility**: `public` (default), `protected` (subclasses), `private` (this class instance only). `private set` / `protected set` allowed on properties.
- **Disambiguation**: `this@Base.member()` or `(obj as Base).member()`. `as` returns a qualified view.
- **Abstract/Interface**: `interface` is a synonym for `abstract class`. Both support state and constructors.
- **Extensions**: `fun Class.ext()` or `val Class.ext get = ...`. Scope-isolated.
## 3. Delegation (`by`)
Unified model for `val`, `var`, and `fun`.
```lyng
val x by MyDelegate()
var y by Map() // Uses "y" as key in map
fn f(a, b) by RemoteProxy() // Calls Proxy.invoke(thisRef, "f", a, b)
```
Delegate Methods:
- `getValue(thisRef, name)`: for `val`/`var`.
- `setValue(thisRef, name, val)`: for `var`.
- `invoke(thisRef, name, args...)`: for `fn` (called if `getValue` is absent).
- `bind(name, access, thisRef)`: optional hook called at declaration/binding time. `access` is `DelegateAccess.Val`, `Var`, or `Callable`.
## 4. Standard Library & Functional Built-ins
- **Scope Functions**:
- `obj.let { it... }`: result of block. `it` is `obj`.
- `obj.apply { this... }`: returns `obj`. `this` is `obj`.
- `obj.also { it... }`: returns `obj`. `it` is `obj`.
- `obj.run { this... }`: result of block. `this` is `obj`.
- `with(obj, { ... })`: result of block. `this` is `obj`.
- **Functional**: `forEach`, `map`, `filter`, `any`, `all`, `sum`, `count`, `sortedBy`, `flatten`, `flatMap`, `associateBy`.
- **Lazy**: `val x = cached { expensive() }` (call as `x()`) or `val x by lazy { ... }`.
- **Collections**: `List` ( `[a, b]` ), `Map` ( `Map(k => v)` ), `Set` ( `Set(a, b)` ). `MapEntry` ( `k => v` ).
## 5. Patterns & Shorthands
- **Map Literals**: `{ key: value, identifier: }` (identifier shorthand `x:` is `x: x`). Empty map is `Map()`.
- **Named Arguments**: `fun(y: 10, x: 5)`. Shorthand: `Point(x:, y:)`.
- **Varargs & Splats**: `fun f(args...)`, `f(...otherList)`.
- **Labels**: `loop@ for(x in list) { if(x == 0) break@loop }`.
- **Dynamic**: `val d = dynamic { get { name -> ... } }` allows `d.anyName`.
## 6. Operators & Methods to Overload
| Op | Method | Op | Method |
| :--- | :--- | :--- | :--- |
| `+` | `plus` | `==` | `equals` |
| `-` | `minus` | `<=>` | `compareTo` |
| `*` | `mul` | `[]` | `getAt` / `putAt` |
| `/` | `div` | `!` | `logicalNot` |
| `%` | `mod` | `-` | `negate` (unary) |
| `=~` | `operatorMatch` | `+=` | `plusAssign` |
## 7. Common Snippets
```lyng
// Multiple Inheritance and Properties
class Warrior(id, hp) : Character(id), HealthPool(hp) {
override fun toString() = "Warrior #%s (%s HP)"(id, hp)
}
// Map entry and merging
val m = Map("a" => 1) + ("b" => 2)
m += "c" => 3
// Destructuring with splat
val [first, middle..., last] = [1, 2, 3, 4, 5]
// Safe Navigation and Elvis
val companyName = person?.job?.company?.name ?? "Freelancer"
```
## 8. Standard Library Discovery
To collect data on the standard library and available APIs, AI should inspect:
- **Global Symbols**: `lynglib/src/commonMain/kotlin/net/sergeych/lyng/Script.kt` (root functions like `println`, `sqrt`, `assert`).
- **Core Type Members**: `lynglib/src/commonMain/kotlin/net/sergeych/lyng/obj/*.kt` (e.g., `ObjList.kt`, `ObjString.kt`, `ObjMap.kt`) for methods on built-in types.
- **Lyng-side Extensions**: `lynglib/stdlib/lyng/root.lyng` for high-level functional APIs (e.g., `map`, `filter`, `any`, `lazy`).
- **I/O & Processes**: `lyngio/src/commonMain/kotlin/net/sergeych/lyng/io/` for `fs` and `process` modules.
- **Documentation**: `docs/*.md` (e.g., `tutorial.md`, `lyngio.md`) for high-level usage and module overviews.
+22 -35
View File
@@ -1,14 +1,6 @@
# Lyng: ideal scripting for kotlin multiplatform # Lyng: modern scripting for kotlin multiplatform
__Please visit the project homepage: [https://lynglang.com](https://lynglang.com) and a [telegram channel](https://t.me/lynglang).__
__Main development site:__ [https://gitea.sergeych.net/SergeychWorks/lyng](https://gitea.sergeych.net/SergeychWorks/lyng)
__github mirror__: [https://github.com/sergeych/lyng](https://github.com/sergeych/lyng)
We keep github as a mirror and backup for the project, while the main development site is hosted on gitea.sergeych.net. We use gitea for issues and pull requests, and as a main point of trust, as github access now is a thing that can momentarily be revoked for no apparent reason.
We encourage using the github if the main site is not accessible from your country and vice versa. We recommend to `publishToMavenLocal` and not depend on politics.
Please visit the project homepage: [https://lynglang.com](https://lynglang.com) and a [telegram channel](https://t.me/lynglang) for updates.
- simple, compact, intuitive and elegant modern code: - simple, compact, intuitive and elegant modern code:
@@ -29,9 +21,22 @@ fun swapEnds(first, args..., last, f) {
- extremely simple Kotlin integration on any platform (JVM, JS, WasmJS, Lunux, MacOS, iOS, Windows) - extremely simple Kotlin integration on any platform (JVM, JS, WasmJS, Lunux, MacOS, iOS, Windows)
- 100% secure: no access to any API you didn't explicitly provide - 100% secure: no access to any API you didn't explicitly provide
- 100% coroutines! Every function/script is a coroutine, it does not block the thread, no async/await/suspend keyword garbage, see [parallelism]. it is multithreaded on platforms supporting it (automatically, no code changes required, just `launch` more coroutines and they will be executed concurrently if possible). See [parallelism] - 100% coroutines! Every function/script is a coroutine, it does not block the thread, no async/await/suspend keyword garbage, see [parallelism]
- functional style and OOP together: multiple inheritance (so you got it all - mixins, interfaces, etc.), delegation, sigletons, anonymous classes,extensions.
- nice literals for maps and arrays, destructuring assignment, ranges. ```
val deferred = launch {
delay(1.5) // coroutine is delayed for 1.5s, thread is not blocked!
"done"
}
// ...
// suspend current coroutine, no thread is blocked again,
// and wait for deferred to return something:
assertEquals("donw", deferred.await())
```
and it is multithreaded on platforms supporting it (automatically, no code changes required, just
`launch` more coroutines and they will be executed concurrently if possible). See [parallelism]
- functional style and OOP together, multiple inheritance, implementing interfaces for existing classes, writing extensions.
- Any Unicode letters can be used as identifiers: `assert( sin(π/2) == 1 )`. - Any Unicode letters can be used as identifiers: `assert( sin(π/2) == 1 )`.
## Resources: ## Resources:
@@ -39,8 +44,6 @@ fun swapEnds(first, args..., last, f) {
- [Language home](https://lynglang.com) - [Language home](https://lynglang.com)
- [introduction and tutorial](docs/tutorial.md) - start here please - [introduction and tutorial](docs/tutorial.md) - start here please
- [Testing and Assertions](docs/Testing.md) - [Testing and Assertions](docs/Testing.md)
- [Filesystem and Processes (lyngio)](docs/lyngio.md)
- [Return Statement](docs/return_statement.md)
- [Efficient Iterables in Kotlin Interop](docs/EfficientIterables.md) - [Efficient Iterables in Kotlin Interop](docs/EfficientIterables.md)
- [Samples directory](docs/samples) - [Samples directory](docs/samples)
- [Formatter (core + CLI + IDE)](docs/formatter.md) - [Formatter (core + CLI + IDE)](docs/formatter.md)
@@ -76,7 +79,7 @@ Now you can import lyng and use it:
### Execute script: ### Execute script:
```kotlin ```kotlin
import net.sergeych.lyng.* import net.sergeyh.lyng.*
// we need a coroutine to start, as Lyng // we need a coroutine to start, as Lyng
// is a coroutine based language, async topdown // is a coroutine based language, async topdown
@@ -92,7 +95,9 @@ Script is executed over some `Scope`. Create instance,
add your specific vars and functions to it, and call: add your specific vars and functions to it, and call:
```kotlin ```kotlin
import net.sergeych.lyng.*
import com.sun.source.tree.Scope
import new.sergeych.lyng.*
// simple function // simple function
val scope = Script.newScope().apply { val scope = Script.newScope().apply {
@@ -140,12 +145,6 @@ Tips:
- After a dot, globals are intentionally suppressed (e.g., `lines().Path` is not valid), only the receiver’s members are suggested. - After a dot, globals are intentionally suppressed (e.g., `lines().Path` is not valid), only the receiver’s members are suggested.
- If completion seems sparse, make sure related modules are imported (e.g., `import lyng.io.fs` so that `Path` and its methods are known). - If completion seems sparse, make sure related modules are imported (e.g., `import lyng.io.fs` so that `Path` and its methods are known).
## AI Assistant Support
To help AI assistants (like Cursor, Windsurf, or GitHub Copilot) understand Lyng with minimal effort, we provide a high-density language specification:
- **[LYNG_AI_SPEC.md](LYNG_AI_SPEC.md)**: A concise guide for AI models to learn Lyng syntax, idioms, and core philosophy. We recommend pointing your AI tool to this file or including it in your project's custom instructions.
## Why? ## Why?
Designed to add scripting to kotlin multiplatform application in easy and efficient way. This is attempt to achieve what Lua is for C/++. Designed to add scripting to kotlin multiplatform application in easy and efficient way. This is attempt to achieve what Lua is for C/++.
@@ -177,7 +176,6 @@ Ready features:
- [x] ranges, lists, strings, interfaces: Iterable, Iterator, Collection, Array - [x] ranges, lists, strings, interfaces: Iterable, Iterator, Collection, Array
- [x] when(value), if-then-else - [x] when(value), if-then-else
- [x] exception handling: throw, try-catch-finally, exception classes. - [x] exception handling: throw, try-catch-finally, exception classes.
- [x] user-defined exception classes
- [x] multiplatform maven publication - [x] multiplatform maven publication
- [x] documentation for the current state - [x] documentation for the current state
- [x] maps, sets and sequences (flows?) - [x] maps, sets and sequences (flows?)
@@ -193,17 +191,6 @@ Ready features:
- [x] regular exceptions + extended `when` - [x] regular exceptions + extended `when`
- [x] multiple inheritance for user classes - [x] multiple inheritance for user classes
- [x] class properties (accessors) - [x] class properties (accessors)
- [x] `return` statement for local and non-local exit
- [x] Unified Delegation model: val, var and fun
- [x] `lazy val` using delegation
- [x] singletons `object TheOnly { ... }`
- [x] object expressions `object: List { ... }`
- [x] late-init vals in classes
- [x] properties with getters and setters
- [x] assign-if-null operator `?=`
- [x] user-defined exception classes
All of this is documented in the [language site](https://lynglang.com) and locally [docs/language.md](docs/tutorial.md). the current nightly builds published on the site and in the private maven repository.
## plan: towards v1.5 Enhancing ## plan: towards v1.5 Enhancing
-4
View File
@@ -1,4 +0,0 @@
# Obsolete files
__Do not rely on contents of the files in this directory. They are kept for historical reference only and may not be up-to-date or relevant.__
-117
View File
@@ -1,117 +0,0 @@
/*
This is a tech proposal under construction, please do not use it yet
for any purpose
*/
/*
Abstract delegate can be used to proxy read/wrtie field access
or method call. Default implementation reports error.
*/
interface Delegate {
fun getValue() = Unset
fun setValue(newValue) { throw NotImplementedException("delegate setter is not implemented") }
fun invoke(args...) { throw NotImplementedException("delegate setter is not implemented") }
}
/*
Delegate cam be used to implement a val, var or fun, so there are
access type enum to distinguish:
*/
enum DelegateAccess {
Val,
Var,
Callable
}
// Delegate can be associated by a val/var/fun in a declaraion site using `by` keyword
val proxiedVal by proxy(1)
var proxiedVar by proxy(2, 3)
fun proxiedFun by proxy()
// each proxy is a Lyng expression returning instance of the Proxy interface:
/*
Proxy interface is connecting some named property of a given kind with the `Delegate`.
It removes the burden of dealing with property name and this ref on each get/set value
or invoke allowing having one delegate per instance, execution buff.
*/
interface Proxy {
fun getDelegate(propertyName: String,access: DelegateAccess,thisRef: Obj?): Delegate
}
// val, var and fun can be delegated, local or class instance:
class TestProxy: Proxy {
override getDelegate(name,access,thisRef) {
Delegate()
}
}
val proxy = TestProxy()
class Allowed {
val v1 by proxy
var v2 by proxy
fun f1 by proxy
}
val v3 by proxy
var v4 by proxy
fun f2 by proxy
/*
It means that for example
Allowed().f1("foo")
would call a delegate.invoke("foo") on the `Delegate` instance supplied by `proxy`, etc.
*/
// The practic sample: lazy value
/*
The delegate that caches single time evaluated value
*/
class LazyDelegate(creator): Delegate {
private var currentValue=Unset
override fun getValue() {
if( currentValue == Unset )
currentValue = creator()
currentValue
}
}
/*
The proxy to assign it
*/
class LazyProxy(creator) {
fun getDelegate(name,access,thisRef) {
if( access != DelegateAccess.Val )
throw IllegalArgumentException("only lazy val are allowed")
LazyDelegate(creator)
}
}
/*
A helper function to simplify creation:
*/
fun lazy(creator) {
LazyProxy(creator)
}
// Usage sample and the test:
var callCounter = 0
assertEquals(0, clallCounter)
val lazyText by lazy { "evaluated text" }
// the lazy property is not yet evaluated:
assertEquals(0, clallCounter)
// now evaluate it by using it:
assertEquals("evaluated text", lazyText)
assertEquals(1, callCounter)
// lazy delegate should fail on vars or funs:
assertThrows { var bad by lazy { "should not happen" } }
assertThrows { fun bad by lazy { 42 } }
-40
View File
@@ -1,40 +0,0 @@
#!/bin/bash
#
# Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
set -e
echo "publishing all artifacts"
echo
./gradlew publishToMavenLocal
./gradlew publish
echo
echo "Creating plugin"
echo
./gradlew buildInstallablePlugin
echo
echo "building CLI tools"
echo
bin/local_jrelease
bin/local_release
echo
echo "Deploying site"
echo
./bin/deploy_site
-26
View File
@@ -1,26 +0,0 @@
#!/bin/sh
#
# Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
set -e
echo
echo "Creating plugin"
echo
./gradlew buildInstallablePlugin
deploy_site -u
+5 -17
View File
@@ -1,7 +1,7 @@
#!/bin/bash #!/bin/bash
# #
# Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com # Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. # you may not use this file except in compliance with the License.
@@ -17,14 +17,6 @@
# #
# #
upload_only=false
for arg in "$@"; do
if [[ "$arg" == "-u" || "$arg" == "--upload-only" ]]; then
upload_only=true
break
fi
done
function checkState() { function checkState() {
if [[ $? != 0 ]]; then if [[ $? != 0 ]]; then
echo echo
@@ -32,10 +24,9 @@ function checkState() {
echo echo
exit 100 exit 100
fi fi
} }
# Update docs/idea_plugin.md to point to the latest built IDEA plugin zip # Update docs/idea_plugin.md to point to the latest built IDEA plugin zip
# from ./distributables before building the site. The change is temporary and # from ./distributables before building the site. The change is temporary and
# the original file is restored right after the build. # the original file is restored right after the build.
@@ -116,8 +107,6 @@ function refreshTextmateZip() {
(cd editors && zip -rq ../distributables/lyng-textmate.zip .) (cd editors && zip -rq ../distributables/lyng-textmate.zip .)
} }
if [[ "$upload_only" == false ]]; then
# Update the IDEA plugin download link in docs (temporarily), then build, then restore the doc # Update the IDEA plugin download link in docs (temporarily), then build, then restore the doc
refreshTextmateZip refreshTextmateZip
updateIdeaPluginDownloadLink || echo "WARN: proceeding without updating IDEA plugin download link" updateIdeaPluginDownloadLink || echo "WARN: proceeding without updating IDEA plugin download link"
@@ -136,7 +125,9 @@ if [[ "$upload_only" == false ]]; then
echo echo
exit 100 exit 100
fi fi
fi
#exit 0
# Prepare working dir # Prepare working dir
ssh -p ${SSH_PORT} ${SSH_HOST} " ssh -p ${SSH_PORT} ${SSH_HOST} "
@@ -152,15 +143,12 @@ ssh -p ${SSH_PORT} ${SSH_HOST} "
fi fi
"; ";
if [[ "$upload_only" == false ]]; then
# sync files # sync files
SRC=./site/build/dist/js/productionExecutable SRC=./site/build/dist/js/productionExecutable
rsync -e "ssh -p ${SSH_PORT}" -avz -r -d --delete ${SRC}/* ${SSH_HOST}:${ROOT}/build/dist rsync -e "ssh -p ${SSH_PORT}" -avz -r -d --delete ${SRC}/* ${SSH_HOST}:${ROOT}/build/dist
checkState checkState
#rsync -e "ssh -p ${SSH_PORT}" -avz ./static/* ${SSH_HOST}:${ROOT}/build/dist #rsync -e "ssh -p ${SSH_PORT}" -avz ./static/* ${SSH_HOST}:${ROOT}/build/dist
#checkState #checkState
fi
rsync -e "ssh -p ${SSH_PORT}" -avz -r -d --delete distributables/* ${SSH_HOST}:${ROOT}/build/dist/distributables rsync -e "ssh -p ${SSH_PORT}" -avz -r -d --delete distributables/* ${SSH_HOST}:${ROOT}/build/dist/distributables
checkState checkState
+2 -2
View File
@@ -23,11 +23,11 @@ There are a lo of ways to construct a buffer:
assertEquals( 5, Buffer("hello").size ) assertEquals( 5, Buffer("hello").size )
// from bytes, e.g. integers in range 0..255 // from bytes, e.g. integers in range 0..255
assertEquals( 255, Buffer(1,2,3,255).last ) assertEquals( 255, Buffer(1,2,3,255).last() )
// from whatever iterable that produces bytes, e.g. // from whatever iterable that produces bytes, e.g.
// integers in 0..255 range: // integers in 0..255 range:
assertEquals( 129, Buffer([1,2,129]).last ) assertEquals( 129, Buffer([1,2,129]).last() )
// Empty buffer of fixed size: // Empty buffer of fixed size:
assertEquals(100, Buffer(100).size) assertEquals(100, Buffer(100).size)
+13 -176
View File
@@ -71,57 +71,14 @@ object DefaultLogger : Logger("Default") {
} }
``` ```
## Object Expressions
Object expressions allow you to create an instance of an anonymous class. This is useful when you need to provide a one-off implementation of an interface or inherit from a class without declaring a new named subclass.
```lyng
val worker = object : Runnable {
override fun run() {
println("Working...")
}
}
```
Object expressions can implement multiple interfaces and inherit from one class:
```lyng
val x = object : Base(arg1), Interface1, Interface2 {
val property = 42
override fun method() = property * 2
}
```
### Scoping and `this@object`
Object expressions capture their lexical scope, meaning they can access local variables and members of the outer class. When `this` is rebound (for example, inside an `apply` block), you can use the reserved alias `this@object` to refer to the innermost anonymous object instance.
```lyng
val handler = object {
fun process() {
this@object.apply {
// here 'this' is rebound to the map/context
// but we can still access the anonymous object via this@object
println("Processing in " + this@object)
}
}
}
```
### Serialization and Identity
- **Serialization**: Anonymous objects are **not serializable**. Attempting to encode an anonymous object via `Lynon` will throw a `SerializationException`. This is because their class definition is transient and cannot be safely restored in a different session or process.
- **Type Identity**: Every object expression creates a unique anonymous class. Two identical object expressions will result in two different classes with distinct type identities.
## Properties ## Properties
Properties allow you to define member accessors that look like fields but execute code when read or written. Unlike regular fields, properties in Lyng do **not** have automatic backing fields; they are pure accessors. Properties allow you to define member accessors that look like fields but execute code when read or written. Unlike regular fields, properties in Lyng do **not** have automatic backing fields; they are pure accessors.
### Basic Syntax ### Basic Syntax
Properties are declared using `val` (read-only) or `var` (read-write) followed by a name and a `get` (and optionally `set`) accessor. Unlike fields, properties do not have automatic storage and must compute their values or delegate to other members. Properties are declared using `val` (read-only) or `var` (read-write) followed by a name and `get()`/`set()` blocks:
The standard syntax uses parentheses:
```lyng ```lyng
class Person(private var _age: Int) { class Person(private var _age: Int) {
// Read-only property // Read-only property
@@ -137,17 +94,21 @@ class Person(private var _age: Int) {
if (value >= 0) _age = value if (value >= 0) _age = value
} }
} }
val p = Person(15)
assertEquals("Minor", p.ageCategory)
p.age = 20
assertEquals("Adult", p.ageCategory)
``` ```
### Laconic Syntax (Optional Parentheses) ### Laconic Expression Shorthand
For even cleaner code, you can omit the parentheses for `get` and `set`. This is especially useful for simple expression shorthand: For simple accessors and methods, you can use the `=` shorthand for a more elegant and laconic form:
```lyng ```lyng
class Circle(val radius: Real) { class Circle(val radius: Real) {
// Laconic expression shorthand val area get() = π * radius * radius
val area get = π * radius * radius val circumference get() = 2 * π * radius
val circumference get = 2 * π * radius
fun diameter() = radius * 2 fun diameter() = radius * 2
} }
@@ -156,16 +117,15 @@ fun median(a, b) = (a + b) / 2
class Counter { class Counter {
private var _count = 0 private var _count = 0
var count get = _count set(v) = _count = v var count get() = _count set(v) = _count = v
} }
``` ```
### Key Rules ### Key Rules
- **`val` properties** must have a `get` accessor (with or without parentheses) and cannot have a `set`. - **`val` properties** must have a `get()` accessor and cannot have a `set()`.
- **`var` properties** must have both `get` and `set` accessors. - **`var` properties** must have both `get()` and `set()` accessors.
- **Functions and methods** can use the `=` shorthand to return the result of a single expression. - **Functions and methods** can use the `=` shorthand to return the result of a single expression.
- **`override` is mandatory**: If you are overriding a member from a base class, you MUST use the `override` keyword.
- **No Backing Fields**: There is no magic `field` identifier. If you need to store state, you must declare a separate (usually `private`) field. - **No Backing Fields**: There is no magic `field` identifier. If you need to store state, you must declare a separate (usually `private`) field.
- **Type Inference**: You can omit the type declaration if it can be inferred or if you don't need strict typing. - **Type Inference**: You can omit the type declaration if it can be inferred or if you don't need strict typing.
@@ -222,19 +182,6 @@ A delegate is any object that provides the following methods (all optional depen
- `invoke(thisRef, name, args...)`: Called when a delegated `fun` is invoked. - `invoke(thisRef, name, args...)`: Called when a delegated `fun` is invoked.
- `bind(name, access, thisRef)`: Called once during initialization to configure or validate the delegate. - `bind(name, access, thisRef)`: Called once during initialization to configure or validate the delegate.
### Map as a Delegate
Maps can also be used as delegates. When delegated to a property, the map uses the property name as the key:
```lyng
val settings = { "theme": "dark", "fontSize": 14 }
val theme by settings
var fontSize by settings
println(theme) // "dark"
fontSize = 16 // Updates settings["fontSize"]
```
For more details and advanced patterns (like `lazy`, `observable`, and shared stateless delegates), see the [Delegation Guide](delegation.md). For more details and advanced patterns (like `lazy`, `observable`, and shared stateless delegates), see the [Delegation Guide](delegation.md).
## Instance initialization: init block ## Instance initialization: init block
@@ -542,99 +489,6 @@ class Critical {
Attempting to override a `closed` member results in a compile-time error. Attempting to override a `closed` member results in a compile-time error.
## Operator Overloading
Lyng allows you to overload standard operators by defining specific named methods in your classes. When an operator expression is evaluated, Lyng delegates the operation to these methods if they are available.
### Binary Operators
To overload a binary operator, define the corresponding method that takes one argument:
| Operator | Method Name |
| :--- | :--- |
| `a + b` | `plus(other)` |
| `a - b` | `minus(other)` |
| `a * b` | `mul(other)` |
| `a / b` | `div(other)` |
| `a % b` | `mod(other)` |
| `a && b` | `logicalAnd(other)` |
| `a \|\| b` | `logicalOr(other)` |
| `a =~ b` | `operatorMatch(other)` |
| `a & b` | `bitAnd(other)` |
| `a \| b` | `bitOr(other)` |
| `a ^ b` | `bitXor(other)` |
| `a << b` | `shl(other)` |
| `a >> b` | `shr(other)` |
Example:
```lyng
class Vector(val x, val y) {
fun plus(other) = Vector(x + other.x, y + other.y)
override fun toString() = "Vector(${x}, ${y})"
}
val v1 = Vector(1, 2)
val v2 = Vector(3, 4)
assertEquals(Vector(4, 6), v1 + v2)
```
### Unary Operators
Unary operators are overloaded by defining methods with no arguments:
| Operator | Method Name |
| :--- | :--- |
| `-a` | `negate()` |
| `!a` | `logicalNot()` |
| `~a` | `bitNot()` |
### Assignment Operators
Assignment operators like `+=` first attempt to call a specific assignment method. If that method is not defined, they fall back to a combination of the binary operator and a regular assignment (e.g., `a = a + b`).
| Operator | Method Name | Fallback |
| :--- | :--- | :--- |
| `a += b` | `plusAssign(other)` | `a = a + b` |
| `a -= b` | `minusAssign(other)` | `a = a - b` |
| `a *= b` | `mulAssign(other)` | `a = a * b` |
| `a /= b` | `divAssign(other)` | `a = a / b` |
| `a %= b` | `modAssign(other)` | `a = a % b` |
Example of in-place mutation:
```lyng
class Counter(var value) {
fun plusAssign(n) {
value = value + n
}
}
val c = Counter(10)
c += 5
assertEquals(15, c.value)
```
### Comparison Operators
Comparison operators use `compareTo` and `equals`.
| Operator | Method Name |
| :--- | :--- |
| `a == b`, `a != b` | `equals(other)` |
| `<`, `>`, `<=`, `>=`, `<=>` | `compareTo(other)` |
- `compareTo` should return:
- `0` if `a == b`
- A negative integer if `a < b`
- A positive integer if `a > b`
- The `<=>` (shuttle) operator returns the result of `compareTo` directly.
- `equals` returns a `Bool`. If `equals` is not explicitly defined, Lyng falls back to `compareTo(other) == 0`.
> **Note**: Methods that are already defined in the base `Obj` class (like `equals`, `toString`, or `contains`) require the `override` keyword when redefined in your class or as an extension. Other operator methods (like `plus` or `negate`) do not require `override` unless they are already present in your class's hierarchy.
### Increment and Decrement
`++` and `--` operators are implemented using `plus(1)` or `minus(1)` combined with an assignment back to the variable. If the variable is a field or local variable, it will be updated with the result of the operation.
Compatibility notes: Compatibility notes:
- Existing single‑inheritance code continues to work unchanged; its resolution order reduces to the single base. - Existing single‑inheritance code continues to work unchanged; its resolution order reduces to the single base.
@@ -718,23 +572,6 @@ Notes and limitations (current version):
- `name` and `ordinal` are read‑only properties of an entry. - `name` and `ordinal` are read‑only properties of an entry.
- `entries` is a read‑only list owned by the enum type. - `entries` is a read‑only list owned by the enum type.
## Exception Classes
You can define your own exception classes by inheriting from the built-in `Exception` class. User-defined exceptions are regular classes and can have their own properties and methods.
```lyng
class MyError(val code, m) : Exception(m)
try {
throw MyError(500, "Internal Server Error")
}
catch(e: MyError) {
println("Error " + e.code + ": " + e.message)
}
```
For more details on error handling, see the [Exceptions Handling Guide](exceptions_handling.md).
## fields and visibility ## fields and visibility
It is possible to add non-constructor fields: It is possible to add non-constructor fields:
+15
View File
@@ -0,0 +1,15 @@
# AI notes: avoid Kotlin/Wasm invalid IR with suspend lambdas
## Do
- Prefer explicit `object : Statement()` with `override suspend fun execute(...)` when building compiler statements.
- Keep `Statement` objects non-lambda, especially in compiler hot paths like parsing/var declarations.
- If you need conditional behavior, return early in `execute` instead of wrapping `parseExpression()` with `statement(...) { ... }`.
- When wasmJs tests hang in the browser, first check `wasmJsNodeTest` for a compile error; hangs often mean module instantiation failed.
## Don't
- Do not create suspend lambdas inside `Statement` factories (`statement { ... }`) for wasm targets.
- Do not "fix" hangs by increasing browser timeouts; it masks invalid wasm generation.
## Debugging tips
- Look for `$invokeCOROUTINE$` in wasm function names when mapping failures.
- If node test logs a wasm compile error, the browser hang is likely the same root cause.
+1 -20
View File
@@ -57,8 +57,7 @@ class lazy(val creator) : Delegate {
override fun getValue(thisRef, name) { override fun getValue(thisRef, name) {
if (value == Unset) { if (value == Unset) {
// calculate value using thisRef as this: value = creator()
value = with(thisRef) creator()
} }
value value
} }
@@ -151,24 +150,6 @@ fun test() {
} }
``` ```
### 6. Map as a Delegate
Maps can be used as delegates for `val` and `var` properties. When a map is used as a delegate, it uses the property name as a key to read from or write to the map.
```lyng
val m = { "a": 1, "b": 2 }
val a by m
var b by m
println(a) // 1
println(b) // 2
b = 42
println(m["b"]) // 42
```
Because `Map` implements `getValue` and `setValue`, it works seamlessly with any object that needs to store its properties in a map (e.g., when implementing dynamic schemas or JSON-backed objects).
## The `bind` Hook ## The `bind` Hook
The `bind(name, access, thisRef)` method is called exactly once when the member is being initialized. It allows the delegate to: The `bind(name, access, thisRef)` method is called exactly once when the member is being initialized. It allows the delegate to:
File renamed without changes.
-58
View File
@@ -103,11 +103,6 @@ scope.addVoidFn("log") {
println(items.joinToString(" ") { it.toString(this).value }) println(items.joinToString(" ") { it.toString(this).value })
} }
// When adding a member function to a class, you can use isOverride = true
// myClass.addFn("toString", isOverride = true) {
// ObjString("Custom string representation")
// }
// Call them from Lyng // Call them from Lyng
scope.eval("val y = inc(41); log('Answer:', y)") scope.eval("val y = inc(41); log('Answer:', y)")
``` ```
@@ -127,9 +122,6 @@ myClass.createField("version", ObjString("1.0.0"), isMutable = false)
// Add a mutable field with an initial value // Add a mutable field with an initial value
myClass.createField("count", ObjInt(0), isMutable = true) myClass.createField("count", ObjInt(0), isMutable = true)
// If you are overriding a field from a base class, use isOverride = true
// myClass.createField("someBaseField", ObjInt(42), isOverride = true)
scope.addConst("MyClass", myClass) scope.addConst("MyClass", myClass)
``` ```
@@ -161,16 +153,6 @@ myClass.addProperty(
} }
) )
// You can also create an ObjProperty explicitly
val explicitProp = ObjProperty(
name = "hexValue",
getter = statement { ObjString(internalValue.toString(16)) }
)
myClass.addProperty("hexValue", prop = explicitProp)
// Use isOverride = true when overriding a property from a base class
// myClass.addProperty("baseProp", getter = { ... }, isOverride = true)
scope.addConst("MyClass", myClass) scope.addConst("MyClass", myClass)
``` ```
@@ -304,46 +286,6 @@ val isolated = net.sergeych.lyng.Scope.new()
- When registering packages, names must be unique. Register before you compile/evaluate scripts that import them. - When registering packages, names must be unique. Register before you compile/evaluate scripts that import them.
- To debug scope content, `scope.toString()` and `scope.trace()` can help during development. - To debug scope content, `scope.toString()` and `scope.trace()` can help during development.
### 12) Handling and serializing exceptions
When Lyng code throws an exception, it is caught in Kotlin as an `ExecutionError`. This error wraps the actual Lyng `Obj` that was thrown (which could be a built-in `ObjException` or a user-defined `ObjInstance`).
To simplify handling these objects from Kotlin, several extension methods are provided on the `Obj` class. These methods work uniformly regardless of whether the exception is built-in or user-defined.
#### Uniform Exception API
| Method | Description |
| :--- | :--- |
| `obj.isLyngException()` | Returns `true` if the object is an instance of `Exception`. |
| `obj.isInstanceOf("ClassName")` | Returns `true` if the object is an instance of the named Lyng class or its ancestors. |
| `obj.getLyngExceptionMessage(scope?=null)` | Returns the exception message as a Kotlin `String`. |
| `obj.getLyngExceptionMessageWithStackTrace(scope?=null)` | Returns a detailed message with a formatted stack trace. |
| `obj.getLyngExceptionString(scope)` | Returns a formatted string including the class name, message, and primary throw site. |
| `obj.getLyngExceptionStackTrace(scope)` | Returns the stack trace as an `ObjList` of `StackTraceEntry`. |
| `obj.getLyngExceptionExtraData(scope)` | Returns the extra data associated with the exception. |
| `obj.raiseAsExecutionError(scope?=null)` | Rethrows the object as a Kotlin `ExecutionError`. |
#### Example: Serialization and Rethrowing
You can serialize Lyng exception objects using `Lynon` to transmit them across boundaries and then rethrow them.
```kotlin
try {
scope.eval("throw MyUserException(404, \"Not Found\")")
} catch (e: ExecutionError) {
// 1. Serialize the Lyng exception object
val encoded: UByteArray = lynonEncodeAny(scope, e.errorObject)
// ... (transmit 'encoded' byte array) ...
// 2. Deserialize it back to an Obj in a different context
val decoded: Obj = lynonDecodeAny(scope, encoded)
// 3. Properly rethrow it on the Kotlin side using the uniform API
decoded.raiseAsExecutionError(scope)
}
```
--- ---
That’s it. You now have Lyng embedded in your Kotlin app: you can expose your app’s API, evaluate user scripts, and organize your own packages to import from Lyng code. That’s it. You now have Lyng embedded in your Kotlin app: you can expose your app’s API, evaluate user scripts, and organize your own packages to import from Lyng code.
+5 -86
View File
@@ -129,11 +129,9 @@ Serializable class that conveys information about the exception. Important membe
| name | description | | name | description |
|-------------------|--------------------------------------------------------| |-------------------|--------------------------------------------------------|
| message | String message | | message | String message |
| stackTrace() | lyng stack trace, list of `StackTraceEntry`, see below | | stackTrace | lyng stack trace, list of `StackTraceEntry`, see below |
| printStackTrace() | format and print stack trace using println() | | printStackTrace() | format and print stack trace using println() |
> **Note for Kotlin users**: When working with Lyng exceptions from Kotlin, you can use extension methods like `getLyngExceptionMessageWithStackTrace()`. See [Embedding Lyng](embedding.md#12-handling-and-serializing-exceptions) for the full API.
## StackTraceEntry ## StackTraceEntry
A simple structire that stores single entry in Lyng stack, it is created automatically on exception creation: A simple structire that stores single entry in Lyng stack, it is created automatically on exception creation:
@@ -152,103 +150,24 @@ class StackTraceEntry(
# Custom error classes # Custom error classes
You can define your own exception classes by inheriting from the built-in `Exception` class. This allows you to create specific error types for your application logic and catch them specifically. _this functionality is not yet released_
## Defining a custom exception
To define a custom exception, create a class that inherits from `Exception`:
```lyng
class MyUserException : Exception("something went wrong")
```
You can also pass the message dynamically:
```lyng
class MyUserException(m) : Exception(m)
throw MyUserException("custom error message")
```
If you don't provide a message to the `Exception` constructor, the class name will be used as the default message:
```lyng
class SimpleException : Exception
val e = SimpleException()
assertEquals("SimpleException", e.message)
```
## Throwing and catching custom exceptions
Custom exceptions are thrown using the `throw` keyword and can be caught using `catch` blocks, just like standard exceptions:
```lyng
class ValidationException(m) : Exception(m)
try {
throw ValidationException("Invalid input")
}
catch(e: ValidationException) {
println("Caught validation error: " + e.message)
}
catch(e: Exception) {
println("Caught other exception: " + e.message)
}
```
Since user exceptions are real classes, inheritance works as expected:
```lyng
class BaseError : Exception
class DerivedError : BaseError
try {
throw DerivedError()
}
catch(e: BaseError) {
// This will catch DerivedError as well
assert(e is DerivedError)
}
```
## Accessing extra data
You can add your own fields to custom exception classes to carry additional information:
```lyng
class NetworkException(m, val statusCode) : Exception(m)
try {
throw NetworkException("Not Found", 404)
}
catch(e: NetworkException) {
println("Error " + e.statusCode + ": " + e.message)
}
```
# Standard exception classes # Standard exception classes
| class | notes | | class | notes |
|----------------------------|-------------------------------------------------------| |----------------------------|-------------------------------------------------------|
| Exception | root of all throwable objects | | Exception | root of al throwable objects |
| NullReferenceException | | | NullReferenceException | |
| AssertionFailedException | | | AssertionFailedException | |
| ClassCastException | | | ClassCastException | |
| IndexOutOfBoundsException | | | IndexOutOfBoundsException | |
| IllegalArgumentException | | | IllegalArgumentException | |
| IllegalStateException | |
| NoSuchElementException | |
| IllegalAssignmentException | assigning to val, etc. | | IllegalAssignmentException | assigning to val, etc. |
| SymbolNotDefinedException | | | SymbolNotDefinedException | |
| IterationEndException | attempt to read iterator past end, `hasNext == false` | | IterationEndException | attempt to read iterator past end, `hasNext == false` |
| IllegalAccessException | attempt to access private members or like | | IllegalAccessException | attempt to access private members or like |
| UnknownException | unexpected internal exception caught | | UnknownException | unexpected kotlin exception caught |
| NotFoundException | | | | |
| IllegalOperationException | |
| UnsetException | access to uninitialized late-init val |
| NotImplementedException | used by `TODO()` |
| SyntaxError | |
### Symbol resolution errors ### Symbol resolution errors
+3 -7
View File
@@ -8,7 +8,7 @@ This module provides a uniform, suspend-first filesystem API to Lyng scripts, ba
It exposes a Lyng class `Path` with methods for file and directory operations, including streaming readers for large files. It exposes a Lyng class `Path` with methods for file and directory operations, including streaming readers for large files.
It is a separate library because access to the filesystem is a security risk we compensate with a separate API that user must explicitly include to the dependency and allow. Together with `FsAccessPolicy` that is required to `createFs()` which actually adds the filesystem to the scope, the security risk is isolated. It is a separate library because access to teh filesystem is a security risk we compensate with a separate API that user must explicitly include to the dependency and allow. Together with `FsAceessPolicy` that is required to `createFs()` which actually adds the filesystem to the scope, the security risk is isolated.
Also, it helps keep Lyng core small and focused. Also, it helps keep Lyng core small and focused.
@@ -23,7 +23,7 @@ dependencies {
implementation("net.sergeych:lyngio:0.0.1-SNAPSHOT") implementation("net.sergeych:lyngio:0.0.1-SNAPSHOT")
} }
``` ```
Note on maven repository. Lyngio uses the same maven as Lyng code (`lynglib`) so it is most likely already in your project. If not, add it to the proper section of your `build.gradle.kts` or settings.gradle.kts: Note on maven repository. Lyngio uses ths same maven as Lyng code (`lynglib`) so it is most likely already in your project. If ont, add it to the proper section of your `build.gradle.kts` or settings.gradle.kts:
```kotlin ```kotlin
repositories { repositories {
@@ -43,13 +43,9 @@ This brings in:
The filesystem module is not installed automatically. You must explicitly register it in the scope’s `ImportManager` using the installer. You can customize access control via `FsAccessPolicy`. The filesystem module is not installed automatically. You must explicitly register it in the scope’s `ImportManager` using the installer. You can customize access control via `FsAccessPolicy`.
Kotlin (host) bootstrap example: Kotlin (host) bootstrap example (imports omitted for brevity):
```kotlin ```kotlin
import net.sergeych.lyng.Scope
import net.sergeych.lyng.io.fs.createFs
import net.sergeych.lyngio.fs.security.PermitAllAccessPolicy
val scope: Scope = Scope.new() val scope: Scope = Scope.new()
val installed: Boolean = createFs(PermitAllAccessPolicy, scope) val installed: Boolean = createFs(PermitAllAccessPolicy, scope)
// installed == true on first registration in this ImportManager, false on repeats // installed == true on first registration in this ImportManager, false on repeats
-136
View File
@@ -1,136 +0,0 @@
### lyng.io.process — Process execution and control for Lyng scripts
This module provides a way to run external processes and shell commands from Lyng scripts. It is designed to be multiplatform and uses coroutines for non-blocking execution.
> **Note:** `lyngio` is a separate library module. It must be explicitly added as a dependency to your host application and initialized in your Lyng scopes.
---
#### Add the library to your project (Gradle)
If you use this repository as a multi-module project, add a dependency on `:lyngio`:
```kotlin
dependencies {
implementation("net.sergeych:lyngio:0.0.1-SNAPSHOT")
}
```
For external projects, ensure you have the appropriate Maven repository configured (see `lyng.io.fs` documentation).
---
#### Install the module into a Lyng Scope
The process module is not installed automatically. You must explicitly register it in the scope’s `ImportManager` using `createProcessModule`. You can customize access control via `ProcessAccessPolicy`.
Kotlin (host) bootstrap example:
```kotlin
import net.sergeych.lyng.Scope
import net.sergeych.lyng.Script
import net.sergeych.lyng.io.process.createProcessModule
import net.sergeych.lyngio.process.security.PermitAllProcessAccessPolicy
// ... inside a suspend function or runBlocking
val scope: Scope = Script.newScope()
createProcessModule(PermitAllProcessAccessPolicy, scope)
// In scripts (or via scope.eval), import the module:
scope.eval("import lyng.io.process")
```
---
#### Using from Lyng scripts
```lyng
import lyng.io.process
// Execute a process with arguments
val p = Process.execute("ls", ["-l", "/tmp"])
for (line in p.stdout) {
println("OUT: " + line)
}
val exitCode = p.waitFor()
println("Process exited with: " + exitCode)
// Run a shell command
val sh = Process.shell("echo 'Hello from shell' | wc -w")
for (line in sh.stdout) {
println("Word count: " + line.trim())
}
// Platform information
val details = Platform.details()
println("OS: " + details.name + " " + details.version + " (" + details.arch + ")")
if (details.kernelVersion != null) {
println("Kernel: " + details.kernelVersion)
}
if (Platform.isSupported()) {
println("Processes are supported!")
}
```
---
#### API Reference
##### `Process` (static methods)
- `execute(executable: String, args: List<String>): RunningProcess` — Start an external process.
- `shell(command: String): RunningProcess` — Run a command through the system shell (e.g., `/bin/sh` or `cmd.exe`).
##### `RunningProcess` (instance methods)
- `stdout: Flow` — Standard output stream as a Lyng Flow of lines.
- `stderr: Flow` — Standard error stream as a Lyng Flow of lines.
- `waitFor(): Int` — Wait for the process to exit and return the exit code.
- `signal(name: String)` — Send a signal to the process (e.g., `"SIGINT"`, `"SIGTERM"`, `"SIGKILL"`).
- `destroy()` — Forcefully terminate the process.
##### `Platform` (static methods)
- `details(): Map` — Get platform details. Returned map keys: `name`, `version`, `arch`, `kernelVersion`.
- `isSupported(): Bool` — True if process execution is supported on the current platform.
---
#### Security Policy
Process execution is a sensitive operation. `lyngio` uses `ProcessAccessPolicy` to control access to `execute` and `shell` operations.
- `ProcessAccessPolicy` — Interface for custom policies.
- `PermitAllProcessAccessPolicy` — Allows all operations.
- `ProcessAccessOp` (sealed) — Operations to check:
- `Execute(executable, args)`
- `Shell(command)`
Example of a restricted policy in Kotlin:
```kotlin
import net.sergeych.lyngio.fs.security.AccessDecision
import net.sergeych.lyngio.fs.security.Decision
import net.sergeych.lyngio.process.security.ProcessAccessOp
import net.sergeych.lyngio.process.security.ProcessAccessPolicy
val restrictedPolicy = object : ProcessAccessPolicy {
override suspend fun check(op: ProcessAccessOp, ctx: AccessContext): AccessDecision {
return when (op) {
is ProcessAccessOp.Execute -> {
if (op.executable == "ls") AccessDecision(Decision.Allow)
else AccessDecision(Decision.Deny, "Only 'ls' is allowed")
}
is ProcessAccessOp.Shell -> AccessDecision(Decision.Deny, "Shell is forbidden")
}
}
}
createProcessModule(restrictedPolicy, scope)
```
---
#### Platform Support
- **JVM:** Full support using `ProcessBuilder`.
- **Native (Linux/macOS):** Support via POSIX.
- **Windows:** Support planned.
- **Android/JS/iOS/Wasm:** Currently not supported; `isSupported()` returns `false` and attempts to run processes will throw `UnsupportedOperationException`.
-87
View File
@@ -1,87 +0,0 @@
### lyngio — Extended I/O and System Library for Lyng
`lyngio` is a separate library that extends the Lyng core (`lynglib`) with powerful, multiplatform, and secure I/O capabilities.
#### Why a separate module?
1. **Security:** I/O and process execution are sensitive operations. By keeping them in a separate module, we ensure that the Lyng core remains 100% safe by default. You only enable what you explicitly need.
2. **Footprint:** Not every script needs filesystem or process access. Keeping these as a separate module helps minimize the dependency footprint for small embedded projects.
3. **Control:** `lyngio` provides fine-grained security policies (`FsAccessPolicy`, `ProcessAccessPolicy`) that allow you to control exactly what a script can do.
#### Included Modules
- **[lyng.io.fs](lyng.io.fs.md):** Async filesystem access. Provides the `Path` class for file/directory operations, streaming, and globbing.
- **[lyng.io.process](lyng.io.process.md):** External process execution and shell commands. Provides `Process`, `RunningProcess`, and `Platform` information.
---
#### Quick Start: Embedding lyngio
##### 1. Add Dependencies (Gradle)
```kotlin
repositories {
maven("https://gitea.sergeych.net/api/packages/SergeychWorks/maven")
}
dependencies {
// Both are required for full I/O support
implementation("net.sergeych:lynglib:0.0.1-SNAPSHOT")
implementation("net.sergeych:lyngio:0.0.1-SNAPSHOT")
}
```
##### 2. Initialize in Kotlin (JVM or Native)
To use `lyngio` modules in your scripts, you must install them into your Lyng scope and provide a security policy.
```kotlin
import net.sergeych.lyng.Script
import net.sergeych.lyng.io.fs.createFs
import net.sergeych.lyng.io.process.createProcessModule
import net.sergeych.lyngio.fs.security.PermitAllAccessPolicy
import net.sergeych.lyngio.process.security.PermitAllProcessAccessPolicy
suspend fun runMyScript() {
val scope = Script.newScope()
// Install modules with policies
createFs(PermitAllAccessPolicy, scope)
createProcessModule(PermitAllProcessAccessPolicy, scope)
// Now scripts can import them
scope.eval("""
import lyng.io.fs
import lyng.io.process
println("Working dir: " + Path(".").readUtf8())
println("OS: " + Platform.details().name)
""")
}
```
---
#### Security Tools
`lyngio` is built with a "Secure by Default" philosophy. Every I/O or process operation is checked against a policy.
- **Filesystem Security:** Implement `FsAccessPolicy` to restrict access to specific paths or operations (e.g., read-only access to a sandbox directory).
- **Process Security:** Implement `ProcessAccessPolicy` to restrict which executables can be run or to disable shell execution entirely.
For more details, see the specific module documentation:
- [Filesystem Security Details](lyng.io.fs.md#access-policy-security)
- [Process Security Details](lyng.io.process.md#security-policy)
---
#### Platform Support Overview
| Platform | lyng.io.fs | lyng.io.process |
| :--- | :---: | :---: |
| **JVM** | ✅ | ✅ |
| **Native (Linux/macOS)** | ✅ | ✅ |
| **Native (Windows)** | ✅ | 🚧 (Planned) |
| **Android** | ✅ | ❌ |
| **NodeJS** | ✅ | ❌ |
| **Browser / Wasm** | ✅ (In-memory) | ❌ |
+7 -5
View File
@@ -226,11 +226,13 @@ Future work: introduce thread‑safe pooling (e.g., per‑thread pools or confin
Closures executed by `launch { ... }` and `flow { ... }` resolve names using the `ClosureScope` rules: Closures executed by `launch { ... }` and `flow { ... }` resolve names using the `ClosureScope` rules:
1. **Current frame locals and arguments**: Variables defined within the current closure execution. 1. Closure frame locals/arguments
2. **Captured lexical ancestry**: Outer local variables captured at the site where the closure was defined (the "lexical environment"). 2. Captured receiver instance/class members
3. **Captured receiver members**: If the closure was defined within a class or explicitly bound to an object, it checks members of that object (`this`), following MRO and respecting visibility. 3. Closure ancestry locals + each frame’s `this` members (cycle‑safe)
4. **Caller environment**: Falls back to the calling context (e.g., the caller's `this` or local variables). 4. Caller `this` members
5. **Global/Module fallbacks**: Final check for module-level constants and global functions. 5. Caller ancestry locals + each frame’s `this` members (cycle‑safe)
6. Module pseudo‑symbols (e.g., `__PACKAGE__`)
7. Direct module/global fallback (nearest `ModuleScope` and its parent/root)
Implications: Implications:
- Outer locals (e.g., `counter`) stay visible across suspension points. - Outer locals (e.g., `counter`) stay visible across suspension points.
+1 -1
View File
@@ -33,7 +33,7 @@ PerfProfiles.restore(snap) // restore previous flags
- `ARG_BUILDER` — Efficient argument building: small‑arity no‑alloc and pooled builder on JVM (ON JVM default). - `ARG_BUILDER` — Efficient argument building: small‑arity no‑alloc and pooled builder on JVM (ON JVM default).
- `ARG_SMALL_ARITY_12` — Extends small‑arity no‑alloc call paths from 0–8 to 0–12 arguments (JVM‑first exploration; OFF by default). Use for codebases with many 9–12 arg calls; A/B before enabling. - `ARG_SMALL_ARITY_12` — Extends small‑arity no‑alloc call paths from 0–8 to 0–12 arguments (JVM‑first exploration; OFF by default). Use for codebases with many 9–12 arg calls; A/B before enabling.
- `SKIP_ARGS_ON_NULL_RECEIVER` — Early return on optional‑null receivers before building args (semantics‑compatible). A/B only. - `SKIP_ARGS_ON_NULL_RECEIVER` — Early return on optional‑null receivers before building args (semantics‑compatible). A/B only.
- `SCOPE_POOL` — Scope frame pooling for calls (per‑thread ThreadLocal pool on JVM/Android/Native; global deque on JS/Wasm). ON by default on all platforms; togglable at runtime. - `SCOPE_POOL` — Scope frame pooling for calls (JVM, per‑thread ThreadLocal pool). ON by default on JVM; togglable at runtime.
- `FIELD_PIC` — 2‑entry polymorphic inline cache for field reads/writes keyed by `(classId, layoutVersion)` (ON JVM default). - `FIELD_PIC` — 2‑entry polymorphic inline cache for field reads/writes keyed by `(classId, layoutVersion)` (ON JVM default).
- `METHOD_PIC` — 2‑entry PIC for instance method calls keyed by `(classId, layoutVersion)` (ON JVM default). - `METHOD_PIC` — 2‑entry PIC for instance method calls keyed by `(classId, layoutVersion)` (ON JVM default).
- `FIELD_PIC_SIZE_4` — Increases Field PIC size from 2 to 4 entries (JVM-first tuning; OFF by default). Use for sites with >2 receiver shapes. - `FIELD_PIC_SIZE_4` — Increases Field PIC size from 2 to 4 entries (JVM-first tuning; OFF by default). Use for sites with >2 receiver shapes.
File renamed without changes.
File renamed without changes.
-86
View File
@@ -1,86 +0,0 @@
# The `return` statement
The `return` statement is used to terminate the execution of the innermost enclosing callable (a function or a lambda) and optionally return a value to the caller.
## Basic Usage
By default, Lyng functions and blocks return the value of their last expression. However, `return` allows you to exit early, which is particularly useful for guard clauses.
```lyng
fun divide(a, b) {
if (b == 0) return null // Guard clause: early exit
a / b
}
```
If no expression is provided, `return` returns `void`:
```lyng
fun logIfDebug(msg) {
if (!DEBUG) return
println("[DEBUG] " + msg)
}
```
## Scoping Rules
In Lyng, `return` always exits the **innermost enclosing callable**. Callables include:
* Named functions (`fun` or `fn`)
* Anonymous functions/lambdas (`{ ... }`)
Standard control flow blocks like `if`, `while`, `do`, and `for` are **not** callables; `return` inside these blocks will return from the function or lambda that contains them.
```lyng
fun findFirstPositive(list) {
list.forEach {
if (it > 0) return it // ERROR: This returns from the lambda, not findFirstPositive!
}
null
}
```
*Note: To return from an outer scope, use [Non-local Returns](#non-local-returns).*
## Non-local Returns
Lyng supports returning from outer scopes using labels. This is a powerful feature for a closure-intensive language.
### Named Functions as Labels
Every named function automatically provides its name as a label.
```lyng
fun findFirstPositive(list) {
list.forEach {
if (it > 0) return@findFirstPositive it // Returns from findFirstPositive
}
null
}
```
### Labeled Lambdas
You can explicitly label a lambda using the `@label` syntax to return from it specifically when nested.
```lyng
val process = @outer { x ->
val result = {
if (x < 0) return@outer "negative" // Returns from the outer lambda
x * 2
}()
"Result: " + result
}
```
## Restriction on Shorthand Functions
To maintain Lyng's clean, expression-oriented style, the `return` keyword is **forbidden** in shorthand function definitions (those using `=`).
```lyng
fun square(x) = x * x // Correct
fun square(x) = return x * x // Syntax Error: 'return' not allowed here
```
## Summary
* `return [expression]` exits the innermost `fun` or `{}`.
* Use `return@label` for non-local returns.
* Named functions provide automatic labels.
* Cannot be used in `=` shorthand functions.
* Consistency: Mirrors the syntax and behavior of `break@label expression`.
-63
View File
@@ -1,63 +0,0 @@
// Sample: Operator Overloading in Lyng
class Vector(val x, val y) {
// Overload +
fun plus(other) = Vector(x + other.x, y + other.y)
// Overload -
fun minus(other) = Vector(x - other.x, y - other.y)
// Overload unary -
fun negate() = Vector(-x, -y)
// Overload ==
fun equals(other) {
if (other is Vector) x == other.x && y == other.y
else false
}
// Overload * (scalar multiplication)
fun mul(scalar) = Vector(x * scalar, y * scalar)
override fun toString() = "Vector(${x}, ${y})"
}
val v1 = Vector(10, 20)
val v2 = Vector(5, 5)
println("v1: " + v1)
println("v2: " + v2)
// Test binary +
val v3 = v1 + v2
println("v1 + v2 = " + v3)
assertEquals(Vector(15, 25), v3)
// Test unary -
val v4 = -v1
println("-v1 = " + v4)
assertEquals(Vector(-10, -20), v4)
// Test scalar multiplication
val v5 = v1 * 2
println("v1 * 2 = " + v5)
assertEquals(Vector(20, 40), v5)
// Test += (falls back to plus)
var v6 = Vector(1, 1)
v6 += Vector(2, 2)
println("v6 += (2,2) -> " + v6)
assertEquals(Vector(3, 3), v6)
// Test in-place mutation with plusAssign
class Counter(var count) {
fun plusAssign(n) {
count = count + n
}
}
val c = Counter(0)
c += 10
c += 5
println("Counter: " + c.count)
assertEquals(15, c.count)
+9 -6
View File
@@ -8,13 +8,16 @@ Name lookup across nested scopes and closures can accidentally form recursive re
## Resolution order in ClosureScope ## Resolution order in ClosureScope
When evaluating an identifier `name` inside a closure, `ClosureScope.get(name)` resolves in this order: When evaluating an identifier `name` inside a closure, `ClosureScope.get(name)` resolves in this order:
1. **Current frame locals and arguments**: Variables defined within the current closure execution. 1. Closure frame locals and arguments
2. **Captured lexical ancestry**: Outer local variables captured at the site where the closure was defined (the "lexical environment"). 2. Captured receiver (`closureScope.thisObj`) instance/class members
3. **Captured receiver members**: If the closure was defined within a class or explicitly bound to an object, it checks members of that object (`this`). This includes both instance fields/methods and class-level static members, following the MRO (C3) and respecting visibility rules (private members are only visible if the closure was defined in their class). 3. Closure ancestry locals + each frame’s `thisObj` members (cycle‑safe)
4. **Caller environment**: If not found lexically, it falls back to the calling context (e.g., the DSL's `this` or the caller's local variables). 4. Caller `this` members
5. **Global/Module fallbacks**: Final check for module-level constants and global functions. 5. Caller ancestry locals + each frame’s `thisObj` members (cycle‑safe)
6. Module pseudo‑symbols (e.g., `__PACKAGE__`) from the nearest `ModuleScope`
7. Direct module/global fallback (nearest `ModuleScope` and its parent/root scope)
8. Final fallback: base local/parent lookup for the current frame
This ensures that closures primarily interact with their defining environment (lexical capture) while still being able to participate in DSL-style calling contexts. This preserves intuitive visibility (locals → captured receiver → closure chain → caller members → caller chain → module/root) while preventing infinite recursion between scope types.
## Use raw‑chain helpers for ancestry walks ## Use raw‑chain helpers for ancestry walks
When authoring new scope types or advanced lookups, avoid calling virtual `get` while walking parents. Instead, use the non‑dispatch helpers on `Scope`: When authoring new scope types or advanced lookups, avoid calling virtual `get` while walking parents. Instead, use the non‑dispatch helpers on `Scope`:
+11 -36
View File
@@ -8,7 +8,7 @@ __Other documents to read__ maybe after this one:
- [Advanced topics](advanced_topics.md), [declaring arguments](declaring_arguments.md), [Scopes and Closures](scopes_and_closures.md) - [Advanced topics](advanced_topics.md), [declaring arguments](declaring_arguments.md), [Scopes and Closures](scopes_and_closures.md)
- [OOP notes](OOP.md), [exception handling](exceptions_handling.md) - [OOP notes](OOP.md), [exception handling](exceptions_handling.md)
- [math in Lyng](math.md), [the `when` statement](when.md), [return statement](return_statement.md) - [math in Lyng](math.md), [the `when` statement](when.md)
- [Testing and Assertions](Testing.md) - [Testing and Assertions](Testing.md)
- [time](time.md) and [parallelism](parallelism.md) - [time](time.md) and [parallelism](parallelism.md)
- [parallelism] - multithreaded code, coroutines, etc. - [parallelism] - multithreaded code, coroutines, etc.
@@ -32,15 +32,6 @@ any block also returns it's last expression:
} }
>>> 6 >>> 6
If you want to exit a function or lambda earlier, use the `return` statement:
fn divide(a, b) {
if( b == 0 ) return null
a / b
}
See [return statement](return_statement.md) for more details on scoping and non-local returns.
If you don't want block to return anything, use `void`: If you don't want block to return anything, use `void`:
fn voidFunction() { fn voidFunction() {
@@ -188,13 +179,14 @@ Note that assignment operator returns rvalue, it can't be assigned.
## Modifying arithmetics ## Modifying arithmetics
There is a set of assigning operations: `+=`, `-=`, `*=`, `/=` and even `%=`. There is a set of assigning operations: `+=`, `-=`, `*=`, `/=` and even `%=`.
There is also a special null-aware assignment operator `?=`: it performs the assignment only if the lvalue is `null`.
var x = null var x = 5
x ?= 10 assert( 25 == (x*=5) )
assertEquals(10, x) assert( 25 == x)
x ?= 20 assert( 24 == (x-=1) )
assertEquals(10, x) assert( 12 == (x/=2) )
x
>>> 12
Notice the parentheses here: the assignment has low priority! Notice the parentheses here: the assignment has low priority!
@@ -247,13 +239,6 @@ There is also "elvis operator", null-coalesce infix operator '?:' that returns r
null ?: "nothing" null ?: "nothing"
>>> "nothing" >>> "nothing"
There is also a null-aware assignment operator `?=`, which assigns a value only if the target is `null`:
var config = null
config ?= { port: 8080 }
config ?= { port: 9000 } // no-op, config is already not null
assertEquals(8080, config.port)
## Utility functions ## Utility functions
The following functions simplify nullable values processing and The following functions simplify nullable values processing and
@@ -313,16 +298,6 @@ It works much like `also`, but is executed in the context of the source object:
assertEquals(p, Point(2,3)) assertEquals(p, Point(2,3))
>>> void >>> void
## with
Sets `this` to the first argument and executes the block. Returns the value returned by the block:
class Point(x,y)
val p = Point(1,2)
val sum = with(p) { x + y }
assertEquals(3, sum)
>>> void
## run ## run
Executes a block after it returning the value passed by the block. for example, can be used with elvis operator: Executes a block after it returning the value passed by the block. for example, can be used with elvis operator:
@@ -1494,7 +1469,7 @@ Typical set of String functions includes:
| s1 += s2 | self-modifying concatenation | | s1 += s2 | self-modifying concatenation |
| toReal() | attempts to parse string as a Real value | | toReal() | attempts to parse string as a Real value |
| toInt() | parse string to Int value | | toInt() | parse string to Int value |
| characters | create [List] of characters (1) | | characters() | create [List] of characters (1) |
| encodeUtf8() | returns [Buffer] with characters encoded to utf8 | | encodeUtf8() | returns [Buffer] with characters encoded to utf8 |
| matches(re) | matches the regular expression (2) | | matches(re) | matches the regular expression (2) |
| | | | | |
@@ -1544,7 +1519,7 @@ See [math functions](math.md). Other general purpose functions are:
| flow {} | create flow sequence, see [parallelism] | | flow {} | create flow sequence, see [parallelism] |
| delay, launch, yield | see [parallelism] | | delay, launch, yield | see [parallelism] |
| cached(builder) | [Lazy evaluation with `cached`](#lazy-evaluation-with-cached) | | cached(builder) | [Lazy evaluation with `cached`](#lazy-evaluation-with-cached) |
| let, also, apply, run, with | see above, flow controls | | let, also, apply, run | see above, flow controls |
(1) (1)
: `fn` is optional lambda returning string message to add to exception string. : `fn` is optional lambda returning string message to add to exception string.
@@ -1571,7 +1546,7 @@ Lambda avoid unnecessary execution if assertion is not failed. for example:
[Range]: Range.md [Range]: Range.md
[String]: ../archived/development/String.md [String]: development/String.md
[string formatting]: https://github.com/sergeych/mp_stools?tab=readme-ov-file#sprintf-syntax-summary [string formatting]: https://github.com/sergeych/mp_stools?tab=readme-ov-file#sprintf-syntax-summary
+27
View File
@@ -0,0 +1,27 @@
# Wasm generation hang in wasmJs browser tests
## Summary
The wasmJs browser test runner hung after commit 5f819dc. The root cause was invalid WebAssembly generated by the Kotlin/Wasm backend when certain compiler paths emitted suspend lambdas for `Statement` execution. The invalid module failed to instantiate in the browser, and Karma kept the browser connected but never ran tests.
## Symptoms
- `:lynglib:wasmJsBrowserTest` hangs indefinitely in ChromeHeadless.
- `:lynglib:wasmJsNodeTest` fails with a WebAssembly compile error similar to:
- `struct.set expected type (ref null XXXX), found global.get of type (ref null YYYY)`
- The failing function name in the wasm name section looks like:
- `net.sergeych.lyng.$invokeCOROUTINE$.doResume`
## Root cause
The delegation/var-declaration changes introduced compiler-generated suspend lambdas inside `Statement` construction (e.g., `statement { ... }` wrappers). Kotlin/Wasm generates extra coroutine state for those suspend lambdas, which in this case produced invalid wasm IR (mismatched GC reference types). The browser loader then waits forever because the module fails to instantiate.
## Fix
Avoid suspend-lambda `Statement` construction in compiler code paths. Replace `statement { ... }` and other anonymous suspend lambdas with explicit `object : Statement()` implementations and move logic into `override suspend fun execute(...)`. This keeps the resulting wasm IR valid while preserving behavior.
## Where it was fixed
- `lynglib/src/commonMain/kotlin/net/sergeych/lyng/Compiler.kt`
- `lynglib/src/commonMain/kotlin/net/sergeych/lyng/Scope.kt`
## Verification
- `./gradlew :lynglib:wasmJsNodeTest --info`
- `./gradlew :lynglib:wasmJsBrowserTest --info`
Both tests finish quickly after the change.
-48
View File
@@ -105,22 +105,6 @@ object Config {
Config.show() Config.show()
``` ```
### Object Expressions
You can now create anonymous objects that inherit from classes or interfaces using the `object : Base { ... }` syntax. These expressions capture their lexical scope and support multiple inheritance.
```lyng
val worker = object : Runnable {
override fun run() = println("Working...")
}
val x = object : Base(arg1), Interface1 {
val property = 42
override fun method() = this@object.property * 2
}
```
Use `this@object` to refer to the innermost anonymous object instance when `this` is rebound.
### Unified Delegation Model ### Unified Delegation Model
A powerful new delegation system allows `val`, `var`, and `fun` members to delegate their logic to other objects using the `by` keyword. A powerful new delegation system allows `val`, `var`, and `fun` members to delegate their logic to other objects using the `by` keyword.
@@ -139,35 +123,6 @@ var name by Observable("initial") { n, old, new ->
The system features a unified interface (`getValue`, `setValue`, `invoke`) and a `bind` hook for initialization-time validation and configuration. See the [Delegation Guide](delegation.md) for more. The system features a unified interface (`getValue`, `setValue`, `invoke`) and a `bind` hook for initialization-time validation and configuration. See the [Delegation Guide](delegation.md) for more.
### User-Defined Exception Classes
You can now create custom exception types by inheriting from the built-in `Exception` class. Custom exceptions are real classes that can have their own fields and methods, and they work seamlessly with `throw` and `try-catch` blocks.
```lyng
class ValidationException(val field, m) : Exception(m)
try {
throw ValidationException("email", "Invalid format")
}
catch(e: ValidationException) {
println("Error in " + e.field + ": " + e.message)
}
```
### Assign-if-null Operator (`?=`)
The new `?=` operator provides a concise way to assign a value only if the target is `null`. It is especially useful for setting default values or lazy initialization.
```lyng
var x = null
x ?= 42 // x is now 42
x ?= 100 // x remains 42 (not null)
// Works with properties and index access
config.port ?= 8080
settings["theme"] ?= "dark"
```
The operator returns the final value of the receiver (the original value if it was not `null`, or the new value if the assignment occurred).
## Tooling and Infrastructure ## Tooling and Infrastructure
### CLI: Formatting Command ### CLI: Formatting Command
@@ -183,6 +138,3 @@ lyng fmt --check MyFile.lyng # Check if file needs formatting
Experimental lightweight autocompletion is now available in the IntelliJ plugin. It features type-aware member suggestions and inheritance-aware completion. Experimental lightweight autocompletion is now available in the IntelliJ plugin. It features type-aware member suggestions and inheritance-aware completion.
You can enable it in **Settings | Lyng Formatter | Enable Lyng autocompletion**. You can enable it in **Settings | Lyng Formatter | Enable Lyng autocompletion**.
### Kotlin API: Exception Handling
The `Obj.getLyngExceptionMessageWithStackTrace()` extension method has been added to simplify retrieving detailed error information from Lyng exception objects in Kotlin. Additionally, `getLyngExceptionMessage()` and `raiseAsExecutionError()` now accept an optional `Scope`, making it easier to use them when a scope is not immediately available.
@@ -42,7 +42,7 @@
{ "name": "constant.numeric.decimal.lyng", "match": "(?<![A-Za-z_])(?:[0-9][0-9_]*)\\.(?:[0-9_]+)(?:[eE][+-]?[0-9_]+)?|(?<![A-Za-z_])(?:[0-9][0-9_]*)(?:[eE][+-]?[0-9_]+)?" } { "name": "constant.numeric.decimal.lyng", "match": "(?<![A-Za-z_])(?:[0-9][0-9_]*)\\.(?:[0-9_]+)(?:[eE][+-]?[0-9_]+)?|(?<![A-Za-z_])(?:[0-9][0-9_]*)(?:[eE][+-]?[0-9_]+)?" }
] ]
}, },
"annotations": { "patterns": [ { "name": "entity.name.label.at.lyng", "match": "@[\\p{L}_][\\p{L}\\p{N}_]*" } ] }, "annotations": { "patterns": [ { "name": "entity.name.label.at.lyng", "match": "@[\\p{L}_][\\p{L}\\p{N}_]*:" }, { "name": "storage.modifier.annotation.lyng", "match": "@[\\p{L}_][\\p{L}\\p{N}_]*" } ] },
"mapLiterals": { "mapLiterals": {
"patterns": [ "patterns": [
{ {
@@ -74,11 +74,11 @@
} }
] ]
}, },
"labels": { "patterns": [ { "name": "entity.name.label.lyng", "match": "[\\p{L}_][\\p{L}\\p{N}_]*@" } ] }, "labels": { "patterns": [ { "name": "entity.name.label.lyng", "match": "[\\p{L}_][\\p{L}\\p{N}_]*:" } ] },
"directives": { "patterns": [ { "name": "meta.directive.lyng", "match": "^\\s*#[_A-Za-z][_A-Za-z0-9]*" } ] }, "directives": { "patterns": [ { "name": "meta.directive.lyng", "match": "^\\s*#[_A-Za-z][_A-Za-z0-9]*" } ] },
"declarations": { "patterns": [ { "name": "meta.function.declaration.lyng", "match": "\\b(fun|fn)\\s+(?:([\\p{L}_][\\p{L}\\p{N}_]*)\\.)?([\\p{L}_][\\p{L}\\p{N}_]*)", "captures": { "1": { "name": "keyword.declaration.lyng" }, "2": { "name": "entity.name.type.lyng" }, "3": { "name": "entity.name.function.lyng" } } }, { "name": "meta.type.declaration.lyng", "match": "\\b(?:class|enum|interface|object)(?:\\s+([\\p{L}_][\\p{L}\\p{N}_]*))?", "captures": { "1": { "name": "entity.name.type.lyng" } } }, { "name": "meta.variable.declaration.lyng", "match": "\\b(val|var)\\s+(?:([\\p{L}_][\\p{L}\\p{N}_]*)\\.)?([\\p{L}_][\\p{L}\\p{N}_]*)", "captures": { "1": { "name": "keyword.declaration.lyng" }, "2": { "name": "entity.name.type.lyng" }, "3": { "name": "variable.other.declaration.lyng" } } } ] }, "declarations": { "patterns": [ { "name": "meta.function.declaration.lyng", "match": "\\b(fun|fn)\\s+(?:([\\p{L}_][\\p{L}\\p{N}_]*)\\.)?([\\p{L}_][\\p{L}\\p{N}_]*)", "captures": { "1": { "name": "keyword.declaration.lyng" }, "2": { "name": "entity.name.type.lyng" }, "3": { "name": "entity.name.function.lyng" } } }, { "name": "meta.type.declaration.lyng", "match": "\\b(?:class|enum|interface)\\s+([\\p{L}_][\\p{L}\\p{N}_]*)", "captures": { "1": { "name": "entity.name.type.lyng" } } }, { "name": "meta.variable.declaration.lyng", "match": "\\b(val|var)\\s+(?:([\\p{L}_][\\p{L}\\p{N}_]*)\\.)?([\\p{L}_][\\p{L}\\p{N}_]*)", "captures": { "1": { "name": "keyword.declaration.lyng" }, "2": { "name": "entity.name.type.lyng" }, "3": { "name": "variable.other.declaration.lyng" } } } ] },
"keywords": { "patterns": [ { "name": "keyword.control.lyng", "match": "\\b(?:if|else|when|while|do|for|try|catch|finally|throw|return|break|continue)\\b" }, { "name": "keyword.declaration.lyng", "match": "\\b(?:fun|fn|class|enum|interface|val|var|import|package|constructor|property|abstract|override|open|closed|extern|private|protected|static|get|set|object|init|by)\\b" }, { "name": "keyword.operator.word.lyng", "match": "\\bnot\\s+(?:in|is)\\b" }, { "name": "keyword.operator.word.lyng", "match": "\\b(?:and|or|not|in|is|as|as\\?)\\b" } ] }, "keywords": { "patterns": [ { "name": "keyword.control.lyng", "match": "\\b(?:if|else|when|while|do|for|try|catch|finally|throw|return|break|continue)\\b" }, { "name": "keyword.declaration.lyng", "match": "\\b(?:fun|fn|class|enum|interface|val|var|import|package|constructor|property|abstract|override|open|closed|extern|private|protected|static|get|set)\\b" }, { "name": "keyword.operator.word.lyng", "match": "\\bnot\\s+(?:in|is)\\b" }, { "name": "keyword.operator.word.lyng", "match": "\\b(?:and|or|not|in|is|as|as\\?)\\b" } ] },
"constants": { "patterns": [ { "name": "constant.language.lyng", "match": "(?:\\b(?:true|false|null|this(?:@[\\p{L}_][\\p{L}\\p{N}_]*)?)\\b|π)" } ] }, "constants": { "patterns": [ { "name": "constant.language.lyng", "match": "(?:\\b(?:true|false|null|this)\\b|π)" } ] },
"types": { "patterns": [ { "name": "storage.type.lyng", "match": "\\b(?:Int|Real|String|Bool|Char|Regex)\\b" }, { "name": "entity.name.type.lyng", "match": "\\b[A-Z][A-Za-z0-9_]*\\b(?!\\s*\\()" } ] }, "types": { "patterns": [ { "name": "storage.type.lyng", "match": "\\b(?:Int|Real|String|Bool|Char|Regex)\\b" }, { "name": "entity.name.type.lyng", "match": "\\b[A-Z][A-Za-z0-9_]*\\b(?!\\s*\\()" } ] },
"operators": { "patterns": [ { "name": "keyword.operator.comparison.lyng", "match": "===|!==|==|!=|<=|>=|<|>" }, { "name": "keyword.operator.shuttle.lyng", "match": "<=>" }, { "name": "keyword.operator.arrow.lyng", "match": "=>|->|::" }, { "name": "keyword.operator.range.lyng", "match": "\\.\\.\\.|\\.\\.<|\\.\\." }, { "name": "keyword.operator.nullsafe.lyng", "match": "\\?\\.|\\?\\[|\\?\\(|\\?\\{|\\?:|\\?\\?" }, { "name": "keyword.operator.assignment.lyng", "match": "(?:\\+=|-=|\\*=|/=|%=|=)" }, { "name": "keyword.operator.logical.lyng", "match": "&&|\\|\\|" }, { "name": "keyword.operator.bitwise.lyng", "match": "<<|>>|&|\\||\\^|~" }, { "name": "keyword.operator.match.lyng", "match": "=~|!~" }, { "name": "keyword.operator.arithmetic.lyng", "match": "\\+\\+|--|[+\\-*/%]" }, { "name": "keyword.operator.other.lyng", "match": "[!?]" } ] }, "operators": { "patterns": [ { "name": "keyword.operator.comparison.lyng", "match": "===|!==|==|!=|<=|>=|<|>" }, { "name": "keyword.operator.shuttle.lyng", "match": "<=>" }, { "name": "keyword.operator.arrow.lyng", "match": "=>|->|::" }, { "name": "keyword.operator.range.lyng", "match": "\\.\\.\\.|\\.\\.<|\\.\\." }, { "name": "keyword.operator.nullsafe.lyng", "match": "\\?\\.|\\?\\[|\\?\\(|\\?\\{|\\?:|\\?\\?" }, { "name": "keyword.operator.assignment.lyng", "match": "(?:\\+=|-=|\\*=|/=|%=|=)" }, { "name": "keyword.operator.logical.lyng", "match": "&&|\\|\\|" }, { "name": "keyword.operator.bitwise.lyng", "match": "<<|>>|&|\\||\\^|~" }, { "name": "keyword.operator.match.lyng", "match": "=~|!~" }, { "name": "keyword.operator.arithmetic.lyng", "match": "\\+\\+|--|[+\\-*/%]" }, { "name": "keyword.operator.other.lyng", "match": "[!?]" } ] },
"punctuation": { "patterns": [ { "name": "punctuation.separator.comma.lyng", "match": "," }, { "name": "punctuation.terminator.statement.lyng", "match": ";" }, { "name": "punctuation.section.block.begin.lyng", "match": "[(]{1}|[{]{1}|\\[" }, { "name": "punctuation.section.block.end.lyng", "match": "[)]{1}|[}]{1}|\\]" }, { "name": "punctuation.accessor.dot.lyng", "match": "\\." }, { "name": "punctuation.separator.colon.lyng", "match": ":" } ] } "punctuation": { "patterns": [ { "name": "punctuation.separator.comma.lyng", "match": "," }, { "name": "punctuation.terminator.statement.lyng", "match": ";" }, { "name": "punctuation.section.block.begin.lyng", "match": "[(]{1}|[{]{1}|\\[" }, { "name": "punctuation.section.block.end.lyng", "match": "[)]{1}|[}]{1}|\\]" }, { "name": "punctuation.accessor.dot.lyng", "match": "\\." }, { "name": "punctuation.separator.colon.lyng", "match": ":" } ] }
+2 -4
View File
@@ -17,7 +17,7 @@
plugins { plugins {
kotlin("jvm") kotlin("jvm")
id("org.jetbrains.intellij") version "1.17.4" id("org.jetbrains.intellij") version "1.17.3"
} }
group = "net.sergeych.lyng" group = "net.sergeych.lyng"
@@ -45,14 +45,12 @@ dependencies {
// Tests for IntelliJ Platform fixtures rely on JUnit 3/4 API (junit.framework.TestCase) // Tests for IntelliJ Platform fixtures rely on JUnit 3/4 API (junit.framework.TestCase)
// Add JUnit 4 which contains the JUnit 3 compatibility classes used by BasePlatformTestCase/UsefulTestCase // Add JUnit 4 which contains the JUnit 3 compatibility classes used by BasePlatformTestCase/UsefulTestCase
testImplementation("junit:junit:4.13.2") testImplementation("junit:junit:4.13.2")
testRuntimeOnly("org.junit.vintage:junit-vintage-engine:5.10.2")
testImplementation("org.opentest4j:opentest4j:1.3.0")
} }
intellij { intellij {
type.set("IC") type.set("IC")
// Build against a modern baseline. Install range is controlled by since/until below. // Build against a modern baseline. Install range is controlled by since/until below.
version.set("2024.1.6") version.set("2024.3.1")
// We manage <idea-version> ourselves in plugin.xml to keep it open-ended (no upper cap) // We manage <idea-version> ourselves in plugin.xml to keep it open-ended (no upper cap)
updateSinceUntilBuild.set(false) updateSinceUntilBuild.set(false)
// Include only available bundled plugins for this IDE build // Include only available bundled plugins for this IDE build
@@ -1,33 +0,0 @@
/*
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package net.sergeych.lyng.idea
import com.intellij.openapi.fileTypes.FileTypeConsumer
import com.intellij.openapi.fileTypes.FileTypeFactory
import com.intellij.openapi.fileTypes.WildcardFileNameMatcher
/**
* Legacy way to register file type matchers, used here to robustly match *.lyng.d
* without conflicting with standard .d extensions from other plugins.
*/
@Suppress("DEPRECATION")
class LyngFileTypeFactory : FileTypeFactory() {
override fun createFileTypes(consumer: FileTypeConsumer) {
// Register the multi-dot pattern explicitly
consumer.consume(LyngFileType, WildcardFileNameMatcher("*.lyng.d"))
}
}
@@ -25,6 +25,9 @@ import com.intellij.openapi.progress.ProgressManager
import com.intellij.openapi.util.Key import com.intellij.openapi.util.Key
import com.intellij.openapi.util.TextRange import com.intellij.openapi.util.TextRange
import com.intellij.psi.PsiFile import com.intellij.psi.PsiFile
import kotlinx.coroutines.runBlocking
import net.sergeych.lyng.Compiler
import net.sergeych.lyng.ScriptError
import net.sergeych.lyng.Source import net.sergeych.lyng.Source
import net.sergeych.lyng.binding.Binder import net.sergeych.lyng.binding.Binder
import net.sergeych.lyng.binding.SymbolKind import net.sergeych.lyng.binding.SymbolKind
@@ -32,7 +35,7 @@ import net.sergeych.lyng.highlight.HighlightKind
import net.sergeych.lyng.highlight.SimpleLyngHighlighter import net.sergeych.lyng.highlight.SimpleLyngHighlighter
import net.sergeych.lyng.highlight.offsetOf import net.sergeych.lyng.highlight.offsetOf
import net.sergeych.lyng.idea.highlight.LyngHighlighterColors import net.sergeych.lyng.idea.highlight.LyngHighlighterColors
import net.sergeych.lyng.idea.util.LyngAstManager import net.sergeych.lyng.idea.util.IdeLenientImportProvider
import net.sergeych.lyng.miniast.* import net.sergeych.lyng.miniast.*
/** /**
@@ -40,7 +43,7 @@ import net.sergeych.lyng.miniast.*
* and applies semantic highlighting comparable with the web highlighter. * and applies semantic highlighting comparable with the web highlighter.
*/ */
class LyngExternalAnnotator : ExternalAnnotator<LyngExternalAnnotator.Input, LyngExternalAnnotator.Result>() { class LyngExternalAnnotator : ExternalAnnotator<LyngExternalAnnotator.Input, LyngExternalAnnotator.Result>() {
data class Input(val text: String, val modStamp: Long, val previousSpans: List<Span>?, val file: PsiFile) data class Input(val text: String, val modStamp: Long, val previousSpans: List<Span>?)
data class Span(val start: Int, val end: Int, val key: com.intellij.openapi.editor.colors.TextAttributesKey) data class Span(val start: Int, val end: Int, val key: com.intellij.openapi.editor.colors.TextAttributesKey)
data class Error(val start: Int, val end: Int, val message: String) data class Error(val start: Int, val end: Int, val message: String)
@@ -52,37 +55,45 @@ class LyngExternalAnnotator : ExternalAnnotator<LyngExternalAnnotator.Input, Lyn
override fun collectInformation(file: PsiFile): Input? { override fun collectInformation(file: PsiFile): Input? {
val doc: Document = file.viewProvider.document ?: return null val doc: Document = file.viewProvider.document ?: return null
val cached = file.getUserData(CACHE_KEY) val cached = file.getUserData(CACHE_KEY)
val combinedStamp = LyngAstManager.getCombinedStamp(file) // Fast fix (1): reuse cached spans only if they were computed for the same modification stamp
val prev = if (cached != null && cached.modStamp == doc.modificationStamp) cached.spans else null
val prev = if (cached != null && cached.modStamp == combinedStamp) cached.spans else null return Input(doc.text, doc.modificationStamp, prev)
return Input(doc.text, combinedStamp, prev, file)
} }
override fun doAnnotate(collectedInfo: Input?): Result? { override fun doAnnotate(collectedInfo: Input?): Result? {
if (collectedInfo == null) return null if (collectedInfo == null) return null
ProgressManager.checkCanceled() ProgressManager.checkCanceled()
val text = collectedInfo.text val text = collectedInfo.text
val tokens = try { SimpleLyngHighlighter().highlight(text) } catch (_: Throwable) { emptyList() } // Build Mini-AST using the same mechanism as web highlighter
val sink = MiniAstBuilder()
// Use LyngAstManager to get the (potentially merged) Mini-AST val source = Source("<ide>", text)
val mini = LyngAstManager.getMiniAst(collectedInfo.file) try {
?: return Result(collectedInfo.modStamp, collectedInfo.previousSpans ?: emptyList()) // Call suspend API from blocking context
val provider = IdeLenientImportProvider.create()
runBlocking { Compiler.compileWithMini(source, provider, sink) }
} catch (e: Throwable) {
if (e is com.intellij.openapi.progress.ProcessCanceledException) throw e
// On script parse error: keep previous spans and report the error location
if (e is ScriptError) {
val off = try { source.offsetOf(e.pos) } catch (_: Throwable) { -1 }
val start0 = off.coerceIn(0, text.length.coerceAtLeast(0))
val (start, end) = expandErrorRange(text, start0)
// Fast fix (5): clear cached highlighting after the error start position
val trimmed = collectedInfo.previousSpans?.filter { it.end <= start } ?: emptyList()
return Result(
collectedInfo.modStamp,
trimmed,
Error(start, end, e.errorMessage)
)
}
// Other failures: keep previous spans without error
return Result(collectedInfo.modStamp, collectedInfo.previousSpans ?: emptyList(), null)
}
ProgressManager.checkCanceled() ProgressManager.checkCanceled()
val source = Source(collectedInfo.file.name, text) val mini = sink.build() ?: return Result(collectedInfo.modStamp, collectedInfo.previousSpans ?: emptyList())
val out = ArrayList<Span>(256) val out = ArrayList<Span>(256)
fun isFollowedByParenOrBlock(rangeEnd: Int): Boolean {
var i = rangeEnd
while (i < text.length) {
val ch = text[i]
if (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n') { i++; continue }
return ch == '(' || ch == '{'
}
return false
}
fun putRange(start: Int, end: Int, key: com.intellij.openapi.editor.colors.TextAttributesKey) { fun putRange(start: Int, end: Int, key: com.intellij.openapi.editor.colors.TextAttributesKey) {
if (start in 0..end && end <= text.length && start < end) out += Span(start, end, key) if (start in 0..end && end <= text.length && start < end) out += Span(start, end, key)
} }
@@ -97,8 +108,7 @@ class LyngExternalAnnotator : ExternalAnnotator<LyngExternalAnnotator.Input, Lyn
} }
// Declarations // Declarations
mini.declarations.forEach { d -> for (d in mini.declarations) {
if (d.nameStart.source != source) return@forEach
when (d) { when (d) {
is MiniFunDecl -> putName(d.nameStart, d.name, LyngHighlighterColors.FUNCTION_DECLARATION) is MiniFunDecl -> putName(d.nameStart, d.name, LyngHighlighterColors.FUNCTION_DECLARATION)
is MiniClassDecl -> putName(d.nameStart, d.name, LyngHighlighterColors.TYPE) is MiniClassDecl -> putName(d.nameStart, d.name, LyngHighlighterColors.TYPE)
@@ -112,31 +122,19 @@ class LyngExternalAnnotator : ExternalAnnotator<LyngExternalAnnotator.Input, Lyn
} }
// Imports: each segment as namespace/path // Imports: each segment as namespace/path
mini.imports.forEach { imp -> for (imp in mini.imports) {
if (imp.range.start.source != source) return@forEach for (seg in imp.segments) putMiniRange(seg.range, LyngHighlighterColors.NAMESPACE)
imp.segments.forEach { seg -> putMiniRange(seg.range, LyngHighlighterColors.NAMESPACE) }
} }
// Parameters // Parameters
fun addParams(params: List<MiniParam>) { for (fn in mini.declarations.filterIsInstance<MiniFunDecl>()) {
params.forEach { p -> for (p in fn.params) putName(p.nameStart, p.name, LyngHighlighterColors.PARAMETER)
if (p.nameStart.source == source)
putName(p.nameStart, p.name, LyngHighlighterColors.PARAMETER)
}
}
mini.declarations.forEach { d ->
when (d) {
is MiniFunDecl -> addParams(d.params)
is MiniClassDecl -> d.members.filterIsInstance<MiniMemberFunDecl>().forEach { addParams(it.params) }
else -> {}
}
} }
// Type name segments (including generics base & args) // Type name segments (including generics base & args)
fun addTypeSegments(t: MiniTypeRef?) { fun addTypeSegments(t: MiniTypeRef?) {
when (t) { when (t) {
is MiniTypeName -> t.segments.forEach { seg -> is MiniTypeName -> t.segments.forEach { seg ->
if (seg.range.start.source != source) return@forEach
val s = source.offsetOf(seg.range.start) val s = source.offsetOf(seg.range.start)
putRange(s, (s + seg.name.length).coerceAtMost(text.length), LyngHighlighterColors.TYPE) putRange(s, (s + seg.name.length).coerceAtMost(text.length), LyngHighlighterColors.TYPE)
} }
@@ -150,14 +148,12 @@ class LyngExternalAnnotator : ExternalAnnotator<LyngExternalAnnotator.Input, Lyn
addTypeSegments(t.returnType) addTypeSegments(t.returnType)
} }
is MiniTypeVar -> { /* name is in range; could be highlighted as TYPE as well */ is MiniTypeVar -> { /* name is in range; could be highlighted as TYPE as well */
if (t.range.start.source == source)
putMiniRange(t.range, LyngHighlighterColors.TYPE) putMiniRange(t.range, LyngHighlighterColors.TYPE)
} }
null -> {} null -> {}
} }
} }
fun addDeclTypeSegments(d: MiniDecl) { for (d in mini.declarations) {
if (d.nameStart.source != source) return
when (d) { when (d) {
is MiniFunDecl -> { is MiniFunDecl -> {
addTypeSegments(d.returnType) addTypeSegments(d.returnType)
@@ -171,23 +167,10 @@ class LyngExternalAnnotator : ExternalAnnotator<LyngExternalAnnotator.Input, Lyn
is MiniClassDecl -> { is MiniClassDecl -> {
d.ctorFields.forEach { addTypeSegments(it.type) } d.ctorFields.forEach { addTypeSegments(it.type) }
d.classFields.forEach { addTypeSegments(it.type) } d.classFields.forEach { addTypeSegments(it.type) }
for (m in d.members) {
when (m) {
is MiniMemberFunDecl -> {
addTypeSegments(m.returnType)
m.params.forEach { addTypeSegments(it.type) }
}
is MiniMemberValDecl -> {
addTypeSegments(m.type)
}
else -> {}
}
}
} }
is MiniEnumDecl -> {} is MiniEnumDecl -> {}
} }
} }
mini.declarations.forEach { d -> addDeclTypeSegments(d) }
ProgressManager.checkCanceled() ProgressManager.checkCanceled()
@@ -197,7 +180,7 @@ class LyngExternalAnnotator : ExternalAnnotator<LyngExternalAnnotator.Input, Lyn
// Map declaration ranges to avoid duplicating them as usages // Map declaration ranges to avoid duplicating them as usages
val declKeys = HashSet<Pair<Int, Int>>(binding.symbols.size * 2) val declKeys = HashSet<Pair<Int, Int>>(binding.symbols.size * 2)
binding.symbols.forEach { sym -> declKeys += (sym.declStart to sym.declEnd) } for (sym in binding.symbols) declKeys += (sym.declStart to sym.declEnd)
fun keyForKind(k: SymbolKind) = when (k) { fun keyForKind(k: SymbolKind) = when (k) {
SymbolKind.Function -> LyngHighlighterColors.FUNCTION SymbolKind.Function -> LyngHighlighterColors.FUNCTION
@@ -210,57 +193,53 @@ class LyngExternalAnnotator : ExternalAnnotator<LyngExternalAnnotator.Input, Lyn
// Track covered ranges to not override later heuristics // Track covered ranges to not override later heuristics
val covered = HashSet<Pair<Int, Int>>() val covered = HashSet<Pair<Int, Int>>()
binding.references.forEach { ref -> for (ref in binding.references) {
val key = ref.start to ref.end val key = ref.start to ref.end
if (!declKeys.contains(key)) { if (declKeys.contains(key)) continue
val sym = binding.symbols.firstOrNull { it.id == ref.symbolId } val sym = binding.symbols.firstOrNull { it.id == ref.symbolId } ?: continue
if (sym != null) {
val color = keyForKind(sym.kind) val color = keyForKind(sym.kind)
putRange(ref.start, ref.end, color) putRange(ref.start, ref.end, color)
covered += key covered += key
} }
}
}
// Heuristics on top of binder: function call-sites and simple name-based roles // Heuristics on top of binder: function call-sites and simple name-based roles
ProgressManager.checkCanceled() ProgressManager.checkCanceled()
val tokens = try { SimpleLyngHighlighter().highlight(text) } catch (_: Throwable) { emptyList() }
fun isFollowedByParenOrBlock(rangeEnd: Int): Boolean {
var i = rangeEnd
while (i < text.length) {
val ch = text[i]
if (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n') { i++; continue }
return ch == '(' || ch == '{'
}
return false
}
// Build simple name -> role map for top-level vals/vars and parameters // Build simple name -> role map for top-level vals/vars and parameters
val nameRole = HashMap<String, com.intellij.openapi.editor.colors.TextAttributesKey>(8) val nameRole = HashMap<String, com.intellij.openapi.editor.colors.TextAttributesKey>(8)
mini.declarations.forEach { d -> for (d in mini.declarations) when (d) {
when (d) { is MiniValDecl -> nameRole[d.name] = if (d.mutable) LyngHighlighterColors.VARIABLE else LyngHighlighterColors.VALUE
is MiniValDecl -> nameRole[d.name] =
if (d.mutable) LyngHighlighterColors.VARIABLE else LyngHighlighterColors.VALUE
is MiniFunDecl -> d.params.forEach { p -> nameRole[p.name] = LyngHighlighterColors.PARAMETER } is MiniFunDecl -> d.params.forEach { p -> nameRole[p.name] = LyngHighlighterColors.PARAMETER }
is MiniClassDecl -> {
d.members.forEach { m ->
if (m is MiniMemberFunDecl) {
m.params.forEach { p -> nameRole[p.name] = LyngHighlighterColors.PARAMETER }
}
}
}
else -> {} else -> {}
} }
}
tokens.forEach { s -> for (s in tokens) if (s.kind == HighlightKind.Identifier) {
if (s.kind == HighlightKind.Identifier) {
val start = s.range.start val start = s.range.start
val end = s.range.endExclusive val end = s.range.endExclusive
val key = start to end val key = start to end
if (key !in covered && key !in declKeys) { if (key in covered || key in declKeys) continue
// Call-site detection first so it wins over var/param role // Call-site detection first so it wins over var/param role
if (isFollowedByParenOrBlock(end)) { if (isFollowedByParenOrBlock(end)) {
putRange(start, end, LyngHighlighterColors.FUNCTION) putRange(start, end, LyngHighlighterColors.FUNCTION)
covered += key covered += key
} else { continue
// Simple role by known names
val ident = try {
text.substring(start, end)
} catch (_: Throwable) {
null
} }
// Simple role by known names
val ident = try { text.substring(start, end) } catch (_: Throwable) { null }
if (ident != null) { if (ident != null) {
val roleKey = nameRole[ident] val roleKey = nameRole[ident]
if (roleKey != null) { if (roleKey != null) {
@@ -269,59 +248,30 @@ class LyngExternalAnnotator : ExternalAnnotator<LyngExternalAnnotator.Input, Lyn
} }
} }
} }
}
}
}
} catch (e: Throwable) { } catch (e: Throwable) {
// Must rethrow cancellation; otherwise ignore binder failures (best-effort) // Must rethrow cancellation; otherwise ignore binder failures (best-effort)
if (e is com.intellij.openapi.progress.ProcessCanceledException) throw e if (e is com.intellij.openapi.progress.ProcessCanceledException) throw e
} }
// Add annotation/label coloring using token highlighter // Add annotation coloring using token highlighter (treat @Label as annotation)
run { run {
tokens.forEach { s -> val tokens = try { SimpleLyngHighlighter().highlight(text) } catch (_: Throwable) { emptyList() }
if (s.kind == HighlightKind.Label) { for (s in tokens) if (s.kind == HighlightKind.Label) {
val start = s.range.start val start = s.range.start
val end = s.range.endExclusive val end = s.range.endExclusive
if (start in 0..end && end <= text.length && start < end) { if (start in 0..end && end <= text.length && start < end) {
val lexeme = try { val lexeme = try { text.substring(start, end) } catch (_: Throwable) { null }
text.substring(start, end) if (lexeme != null && lexeme.startsWith("@")) {
} catch (_: Throwable) {
null
}
if (lexeme != null) {
// Heuristic: if it starts with @ and follows a control keyword, it's likely a label
// Otherwise if it starts with @ it's an annotation.
// If it ends with @ it's a loop label.
when {
lexeme.endsWith("@") -> putRange(start, end, LyngHighlighterColors.LABEL)
lexeme.startsWith("@") -> {
// Try to see if it's an exit label
val prevNonWs = prevNonWs(text, start)
val prevWord = if (prevNonWs >= 0) {
var wEnd = prevNonWs + 1
var wStart = prevNonWs
while (wStart > 0 && text[wStart - 1].isLetter()) wStart--
text.substring(wStart, wEnd)
} else null
if (prevWord in setOf("return", "break", "continue") || isFollowedByParenOrBlock(end)) {
putRange(start, end, LyngHighlighterColors.LABEL)
} else {
putRange(start, end, LyngHighlighterColors.ANNOTATION) putRange(start, end, LyngHighlighterColors.ANNOTATION)
} }
} }
} }
} }
}
}
}
}
// Map Enum constants from token highlighter to IDEA enum constant color // Map Enum constants from token highlighter to IDEA enum constant color
run { run {
tokens.forEach { s -> val tokens = try { SimpleLyngHighlighter().highlight(text) } catch (_: Throwable) { emptyList() }
if (s.kind == HighlightKind.EnumConstant) { for (s in tokens) if (s.kind == HighlightKind.EnumConstant) {
val start = s.range.start val start = s.range.start
val end = s.range.endExclusive val end = s.range.endExclusive
if (start in 0..end && end <= text.length && start < end) { if (start in 0..end && end <= text.length && start < end) {
@@ -329,12 +279,23 @@ class LyngExternalAnnotator : ExternalAnnotator<LyngExternalAnnotator.Input, Lyn
} }
} }
} }
}
// Build spell index payload: identifiers + comments/strings from simple highlighter. // Build spell index payload: identifiers from symbols + references; comments/strings from simple highlighter
// We use the highlighter as the source of truth for all "words" to check, including val idRanges = mutableSetOf<IntRange>()
// identifiers that might not be bound by the Binder. try {
val idRanges = tokens.filter { it.kind == HighlightKind.Identifier }.map { it.range.start until it.range.endExclusive } val binding = Binder.bind(text, mini)
for (sym in binding.symbols) {
val s = sym.declStart; val e = sym.declEnd
if (s in 0..e && e <= text.length && s < e) idRanges += (s until e)
}
for (ref in binding.references) {
val s = ref.start; val e = ref.end
if (s in 0..e && e <= text.length && s < e) idRanges += (s until e)
}
} catch (_: Throwable) {
// Best-effort; no identifiers if binder fails
}
val tokens = try { SimpleLyngHighlighter().highlight(text) } catch (_: Throwable) { emptyList() }
val commentRanges = tokens.filter { it.kind == HighlightKind.Comment }.map { it.range.start until it.range.endExclusive } val commentRanges = tokens.filter { it.kind == HighlightKind.Comment }.map { it.range.start until it.range.endExclusive }
val stringRanges = tokens.filter { it.kind == HighlightKind.String }.map { it.range.start until it.range.endExclusive } val stringRanges = tokens.filter { it.kind == HighlightKind.String }.map { it.range.start until it.range.endExclusive }
@@ -347,12 +308,11 @@ class LyngExternalAnnotator : ExternalAnnotator<LyngExternalAnnotator.Input, Lyn
override fun apply(file: PsiFile, annotationResult: Result?, holder: AnnotationHolder) { override fun apply(file: PsiFile, annotationResult: Result?, holder: AnnotationHolder) {
if (annotationResult == null) return if (annotationResult == null) return
// Skip if cache is up-to-date // Skip if cache is up-to-date
val combinedStamp = LyngAstManager.getCombinedStamp(file)
val cached = file.getUserData(CACHE_KEY)
val result = if (cached != null && cached.modStamp == combinedStamp) cached else annotationResult
file.putUserData(CACHE_KEY, result)
val doc = file.viewProvider.document val doc = file.viewProvider.document
val currentStamp = doc?.modificationStamp
val cached = file.getUserData(CACHE_KEY)
val result = if (cached != null && currentStamp != null && cached.modStamp == currentStamp) cached else annotationResult
file.putUserData(CACHE_KEY, result)
// Store spell index for spell/grammar engines to consume (suspend until ready) // Store spell index for spell/grammar engines to consume (suspend until ready)
val ids = result.spellIdentifiers.map { TextRange(it.first, it.last + 1) } val ids = result.spellIdentifiers.map { TextRange(it.first, it.last + 1) }
@@ -404,16 +364,6 @@ class LyngExternalAnnotator : ExternalAnnotator<LyngExternalAnnotator.Input, Lyn
private val CACHE_KEY: Key<Result> = Key.create("LYNG_SEMANTIC_CACHE") private val CACHE_KEY: Key<Result> = Key.create("LYNG_SEMANTIC_CACHE")
} }
private fun prevNonWs(text: String, idxExclusive: Int): Int {
var i = idxExclusive - 1
while (i >= 0) {
val ch = text[i]
if (ch != ' ' && ch != '\t' && ch != '\n' && ch != '\r') return i
i--
}
return -1
}
/** /**
* Make the error highlight a bit wider than a single character so it is easier to see and click. * Make the error highlight a bit wider than a single character so it is easier to see and click.
* Strategy: * Strategy:
@@ -30,6 +30,7 @@ import com.intellij.patterns.PlatformPatterns
import com.intellij.psi.PsiFile import com.intellij.psi.PsiFile
import com.intellij.util.ProcessingContext import com.intellij.util.ProcessingContext
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import net.sergeych.lyng.highlight.offsetOf
import net.sergeych.lyng.idea.LyngLanguage import net.sergeych.lyng.idea.LyngLanguage
import net.sergeych.lyng.idea.highlight.LyngTokenTypes import net.sergeych.lyng.idea.highlight.LyngTokenTypes
import net.sergeych.lyng.idea.settings.LyngFormatterSettings import net.sergeych.lyng.idea.settings.LyngFormatterSettings
@@ -102,7 +103,7 @@ class LyngCompletionContributor : CompletionContributor() {
// Delegate computation to the shared engine to keep behavior in sync with tests // Delegate computation to the shared engine to keep behavior in sync with tests
val engineItems = try { val engineItems = try {
runBlocking { CompletionEngineLight.completeSuspend(text, caret, mini, binding) } runBlocking { CompletionEngineLight.completeSuspend(text, caret) }
} catch (t: Throwable) { } catch (t: Throwable) {
if (DEBUG_COMPLETION) log.warn("[LYNG_DEBUG] Engine completion failed: ${t.message}") if (DEBUG_COMPLETION) log.warn("[LYNG_DEBUG] Engine completion failed: ${t.message}")
emptyList() emptyList()
@@ -116,7 +117,7 @@ class LyngCompletionContributor : CompletionContributor() {
if (memberDotPos != null && engineItems.isEmpty()) { if (memberDotPos != null && engineItems.isEmpty()) {
if (DEBUG_COMPLETION) log.info("[LYNG_DEBUG] Fallback: engine returned 0 in member context; trying local inference") if (DEBUG_COMPLETION) log.info("[LYNG_DEBUG] Fallback: engine returned 0 in member context; trying local inference")
// Build imported modules from text (lenient) + stdlib; avoid heavy MiniAst here // Build imported modules from text (lenient) + stdlib; avoid heavy MiniAst here
val fromText = DocLookupUtils.extractImportsFromText(text) val fromText = extractImportsFromText(text)
val imported = LinkedHashSet<String>().apply { val imported = LinkedHashSet<String>().apply {
fromText.forEach { add(it) } fromText.forEach { add(it) }
add("lyng.stdlib") add("lyng.stdlib")
@@ -143,6 +144,11 @@ class LyngCompletionContributor : CompletionContributor() {
} }
} }
// In global context, add params in scope first (engine does not include them)
if (memberDotPos == null && mini != null) {
offerParamsInScope(emit, mini, text, caret)
}
// Render engine items // Render engine items
for (ci in engineItems) { for (ci in engineItems) {
val builder = when (ci.kind) { val builder = when (ci.kind) {
@@ -161,22 +167,18 @@ class LyngCompletionContributor : CompletionContributor() {
Kind.Enum -> LookupElementBuilder.create(ci.name) Kind.Enum -> LookupElementBuilder.create(ci.name)
.withIcon(AllIcons.Nodes.Enum) .withIcon(AllIcons.Nodes.Enum)
Kind.Value -> LookupElementBuilder.create(ci.name) Kind.Value -> LookupElementBuilder.create(ci.name)
.withIcon(AllIcons.Nodes.Variable) .withIcon(AllIcons.Nodes.Field)
.let { b -> if (!ci.typeText.isNullOrBlank()) b.withTypeText(ci.typeText, true) else b } .let { b -> if (!ci.typeText.isNullOrBlank()) b.withTypeText(ci.typeText, true) else b }
Kind.Field -> LookupElementBuilder.create(ci.name) Kind.Field -> LookupElementBuilder.create(ci.name)
.withIcon(AllIcons.Nodes.Field) .withIcon(AllIcons.Nodes.Field)
.let { b -> if (!ci.typeText.isNullOrBlank()) b.withTypeText(ci.typeText, true) else b } .let { b -> if (!ci.typeText.isNullOrBlank()) b.withTypeText(ci.typeText, true) else b }
} }
if (ci.priority != 0.0) {
emit(PrioritizedLookupElement.withPriority(builder, ci.priority))
} else {
emit(builder) emit(builder)
} }
}
// In member context, ensure stdlib extension-like methods (e.g., String.re) are present // In member context, ensure stdlib extension-like methods (e.g., String.re) are present
if (memberDotPos != null) { if (memberDotPos != null) {
val existing = engineItems.map { it.name }.toMutableSet() val existing = engineItems.map { it.name }.toMutableSet()
val fromText = DocLookupUtils.extractImportsFromText(text) val fromText = extractImportsFromText(text)
val imported = LinkedHashSet<String>().apply { val imported = LinkedHashSet<String>().apply {
fromText.forEach { add(it) } fromText.forEach { add(it) }
add("lyng.stdlib") add("lyng.stdlib")
@@ -189,51 +191,33 @@ class LyngCompletionContributor : CompletionContributor() {
?: DocLookupUtils.guessReturnClassAcrossKnownCallees(text, memberDotPos, imported, mini) ?: DocLookupUtils.guessReturnClassAcrossKnownCallees(text, memberDotPos, imported, mini)
?: DocLookupUtils.guessReceiverClass(text, memberDotPos, imported, mini) ?: DocLookupUtils.guessReceiverClass(text, memberDotPos, imported, mini)
if (!inferredClass.isNullOrBlank()) { if (!inferredClass.isNullOrBlank()) {
val ext = DocLookupUtils.collectExtensionMemberNames(imported, inferredClass, mini) val ext = BuiltinDocRegistry.extensionMemberNamesFor(inferredClass)
if (DEBUG_COMPLETION) log.info("[LYNG_DEBUG] Post-engine extension check for $inferredClass: ${ext}") if (DEBUG_COMPLETION) log.info("[LYNG_DEBUG] Post-engine extension check for $inferredClass: ${'$'}{ext}")
for (name in ext) { for (name in ext) {
if (existing.contains(name)) continue if (existing.contains(name)) continue
val resolved = DocLookupUtils.resolveMemberWithInheritance(imported, inferredClass, name, mini) val resolved = DocLookupUtils.resolveMemberWithInheritance(imported, inferredClass, name, mini)
if (resolved != null) { if (resolved != null) {
val m = resolved.second when (val member = resolved.second) {
val builder = when (m) {
is MiniMemberFunDecl -> { is MiniMemberFunDecl -> {
val params = m.params.joinToString(", ") { it.name } val params = member.params.joinToString(", ") { it.name }
val ret = typeOf(m.returnType) val ret = typeOf(member.returnType)
LookupElementBuilder.create(name) val builder = LookupElementBuilder.create(name)
.withIcon(AllIcons.Nodes.Method) .withIcon(AllIcons.Nodes.Method)
.withTailText("($params)", true) .withTailText("(${ '$' }params)", true)
.withTypeText(ret, true) .withTypeText(ret, true)
.withInsertHandler(ParenInsertHandler) .withInsertHandler(ParenInsertHandler)
}
is MiniFunDecl -> {
val params = m.params.joinToString(", ") { it.name }
val ret = typeOf(m.returnType)
LookupElementBuilder.create(name)
.withIcon(AllIcons.Nodes.Method)
.withTailText("($params)", true)
.withTypeText(ret, true)
.withInsertHandler(ParenInsertHandler)
}
is MiniMemberValDecl -> {
LookupElementBuilder.create(name)
.withIcon(if (m.mutable) AllIcons.Nodes.Variable else AllIcons.Nodes.Field)
.withTypeText(typeOf(m.type), true)
}
is MiniValDecl -> {
LookupElementBuilder.create(name)
.withIcon(if (m.mutable) AllIcons.Nodes.Variable else AllIcons.Nodes.Field)
.withTypeText(typeOf(m.type), true)
}
else -> {
LookupElementBuilder.create(name)
.withIcon(AllIcons.Nodes.Method)
.withTailText("()", true)
.withInsertHandler(ParenInsertHandler)
}
}
emit(builder) emit(builder)
existing.add(name) existing.add(name)
}
is MiniMemberValDecl -> {
val builder = LookupElementBuilder.create(name)
.withIcon(if (member.mutable) AllIcons.Nodes.Variable else AllIcons.Nodes.Field)
.withTypeText(typeOf(member.type), true)
emit(builder)
existing.add(name)
}
is MiniInitDecl -> {}
}
} else { } else {
// Fallback: emit simple method name without detailed types // Fallback: emit simple method name without detailed types
val builder = LookupElementBuilder.create(name) val builder = LookupElementBuilder.create(name)
@@ -249,7 +233,7 @@ class LyngCompletionContributor : CompletionContributor() {
// If in member context and engine items are suspiciously sparse, try to enrich via local inference + offerMembers // If in member context and engine items are suspiciously sparse, try to enrich via local inference + offerMembers
if (memberDotPos != null && engineItems.size < 3) { if (memberDotPos != null && engineItems.size < 3) {
if (DEBUG_COMPLETION) log.info("[LYNG_DEBUG] Engine produced only ${engineItems.size} items in member context — trying enrichment") if (DEBUG_COMPLETION) log.info("[LYNG_DEBUG] Engine produced only ${engineItems.size} items in member context — trying enrichment")
val fromText = DocLookupUtils.extractImportsFromText(text) val fromText = extractImportsFromText(text)
val imported = LinkedHashSet<String>().apply { val imported = LinkedHashSet<String>().apply {
fromText.forEach { add(it) } fromText.forEach { add(it) }
add("lyng.stdlib") add("lyng.stdlib")
@@ -405,22 +389,17 @@ class LyngCompletionContributor : CompletionContributor() {
} }
supplementPreferredBases(className) supplementPreferredBases(className)
fun emitGroup(map: LinkedHashMap<String, MutableList<MiniMemberDecl>>, groupPriority: Double) { fun emitGroup(map: LinkedHashMap<String, MutableList<MiniMemberDecl>>) {
val keys = map.keys.sortedBy { it.lowercase() } val keys = map.keys.sortedBy { it.lowercase() }
for (name in keys) { for (name in keys) {
val list = map[name] ?: continue val list = map[name] ?: continue
// Choose a representative for display: // Choose a representative for display:
// 1) Prefer a method with return type AND parameters // 1) Prefer a method with a known return type
// 2) Prefer a method with parameters // 2) Else any method
// 3) Prefer a method with return type // 3) Else the first variant
// 4) Else any method
// 5) Else the first variant
val rep = val rep =
list.asSequence().filterIsInstance<MiniMemberFunDecl>() list.asSequence()
.firstOrNull { it.returnType != null && it.params.isNotEmpty() } .filterIsInstance<MiniMemberFunDecl>()
?: list.asSequence().filterIsInstance<MiniMemberFunDecl>()
.firstOrNull { it.params.isNotEmpty() }
?: list.asSequence().filterIsInstance<MiniMemberFunDecl>()
.firstOrNull { it.returnType != null } .firstOrNull { it.returnType != null }
?: list.firstOrNull { it is MiniMemberFunDecl } ?: list.firstOrNull { it is MiniMemberFunDecl }
?: list.first() ?: list.first()
@@ -437,12 +416,8 @@ class LyngCompletionContributor : CompletionContributor() {
.withTailText(tail, true) .withTailText(tail, true)
.withTypeText(ret, true) .withTypeText(ret, true)
.withInsertHandler(ParenInsertHandler) .withInsertHandler(ParenInsertHandler)
if (groupPriority != 0.0) {
emit(PrioritizedLookupElement.withPriority(builder, groupPriority))
} else {
emit(builder) emit(builder)
} }
}
is MiniMemberValDecl -> { is MiniMemberValDecl -> {
val icon = if (rep.mutable) AllIcons.Nodes.Variable else AllIcons.Nodes.Field val icon = if (rep.mutable) AllIcons.Nodes.Variable else AllIcons.Nodes.Field
// Prefer a field variant with known type if available // Prefer a field variant with known type if available
@@ -452,20 +427,16 @@ class LyngCompletionContributor : CompletionContributor() {
val builder = LookupElementBuilder.create(name) val builder = LookupElementBuilder.create(name)
.withIcon(icon) .withIcon(icon)
.withTypeText(typeOf(chosen.type), true) .withTypeText(typeOf(chosen.type), true)
if (groupPriority != 0.0) {
emit(PrioritizedLookupElement.withPriority(builder, groupPriority))
} else {
emit(builder) emit(builder)
} }
}
is MiniInitDecl -> {} is MiniInitDecl -> {}
} }
} }
} }
// Emit what we have first // Emit what we have first
emitGroup(directMap, 100.0) emitGroup(directMap)
emitGroup(inheritedMap, 0.0) emitGroup(inheritedMap)
// If suggestions are suspiciously sparse for known container classes, // If suggestions are suspiciously sparse for known container classes,
// try to conservatively supplement using a curated list resolved via docs registry. // try to conservatively supplement using a curated list resolved via docs registry.
@@ -487,47 +458,30 @@ class LyngCompletionContributor : CompletionContributor() {
for (name in common) { for (name in common) {
if (name in already) continue if (name in already) continue
// Try resolve across classes first to get types/params; if it fails, emit a synthetic safe suggestion. // Try resolve across classes first to get types/params; if it fails, emit a synthetic safe suggestion.
val resolved = DocLookupUtils.findMemberAcrossClasses(imported, name, mini) val resolved = DocLookupUtils.findMemberAcrossClasses(imported, name)
if (resolved != null) { if (resolved != null) {
val member = resolved.second val member = resolved.second
val builder = when (member) { when (member) {
is MiniMemberFunDecl -> { is MiniMemberFunDecl -> {
val params = member.params.joinToString(", ") { it.name } val params = member.params.joinToString(", ") { it.name }
val ret = typeOf(member.returnType) val ret = typeOf(member.returnType)
LookupElementBuilder.create(name) val builder = LookupElementBuilder.create(name)
.withIcon(AllIcons.Nodes.Method) .withIcon(AllIcons.Nodes.Method)
.withTailText("($params)", true) .withTailText("(${params})", true)
.withTypeText(ret, true)
.withInsertHandler(ParenInsertHandler)
}
is MiniFunDecl -> {
val params = member.params.joinToString(", ") { it.name }
val ret = typeOf(member.returnType)
LookupElementBuilder.create(name)
.withIcon(AllIcons.Nodes.Method)
.withTailText("($params)", true)
.withTypeText(ret, true) .withTypeText(ret, true)
.withInsertHandler(ParenInsertHandler) .withInsertHandler(ParenInsertHandler)
emit(builder)
already.add(name)
} }
is MiniMemberValDecl -> { is MiniMemberValDecl -> {
LookupElementBuilder.create(name) val builder = LookupElementBuilder.create(name)
.withIcon(if (member.mutable) AllIcons.Nodes.Variable else AllIcons.Nodes.Field) .withIcon(AllIcons.Nodes.Field)
.withTypeText(typeOf(member.type), true) .withTypeText(typeOf(member.type), true)
} emit(builder)
is MiniValDecl -> {
LookupElementBuilder.create(name)
.withIcon(if (member.mutable) AllIcons.Nodes.Variable else AllIcons.Nodes.Field)
.withTypeText(typeOf(member.type), true)
}
else -> {
LookupElementBuilder.create(name)
.withIcon(AllIcons.Nodes.Method)
.withTailText("()", true)
.withInsertHandler(ParenInsertHandler)
}
}
emit(PrioritizedLookupElement.withPriority(builder, 50.0))
already.add(name) already.add(name)
}
is MiniInitDecl -> {}
}
} else { } else {
// Synthetic fallback: method without detailed params/types to improve UX in absence of docs // Synthetic fallback: method without detailed params/types to improve UX in absence of docs
val isProperty = name in setOf("size", "length") val isProperty = name in setOf("size", "length")
@@ -540,7 +494,7 @@ class LyngCompletionContributor : CompletionContributor() {
.withTailText("()", true) .withTailText("()", true)
.withInsertHandler(ParenInsertHandler) .withInsertHandler(ParenInsertHandler)
} }
emit(PrioritizedLookupElement.withPriority(builder, 50.0)) emit(builder)
already.add(name) already.add(name)
} }
} }
@@ -554,64 +508,90 @@ class LyngCompletionContributor : CompletionContributor() {
for (name in ext) { for (name in ext) {
if (already.contains(name)) continue if (already.contains(name)) continue
// Try to resolve full signature via registry first to get params and return type // Try to resolve full signature via registry first to get params and return type
val resolved = DocLookupUtils.resolveMemberWithInheritance(imported, className, name, mini) val resolved = DocLookupUtils.resolveMemberWithInheritance(imported, className, name)
if (resolved != null) { if (resolved != null) {
val m = resolved.second when (val member = resolved.second) {
val builder = when (m) {
is MiniMemberFunDecl -> { is MiniMemberFunDecl -> {
val params = m.params.joinToString(", ") { it.name } val params = member.params.joinToString(", ") { it.name }
val ret = typeOf(m.returnType) val ret = typeOf(member.returnType)
LookupElementBuilder.create(name) val builder = LookupElementBuilder.create(name)
.withIcon(AllIcons.Nodes.Method) .withIcon(AllIcons.Nodes.Method)
.withTailText("($params)", true) .withTailText("(${params})", true)
.withTypeText(ret, true) .withTypeText(ret, true)
.withInsertHandler(ParenInsertHandler) .withInsertHandler(ParenInsertHandler)
} emit(builder)
is MiniFunDecl -> {
val params = m.params.joinToString(", ") { it.name }
val ret = typeOf(m.returnType)
LookupElementBuilder.create(name)
.withIcon(AllIcons.Nodes.Method)
.withTailText("($params)", true)
.withTypeText(ret, true)
.withInsertHandler(ParenInsertHandler)
}
is MiniMemberValDecl -> {
LookupElementBuilder.create(name)
.withIcon(if (m.mutable) AllIcons.Nodes.Variable else AllIcons.Nodes.Field)
.withTypeText(typeOf(m.type), true)
}
is MiniValDecl -> {
LookupElementBuilder.create(name)
.withIcon(if (m.mutable) AllIcons.Nodes.Variable else AllIcons.Nodes.Field)
.withTypeText(typeOf(m.type), true)
}
else -> {
LookupElementBuilder.create(name)
.withIcon(AllIcons.Nodes.Method)
.withTailText("()", true)
.withInsertHandler(ParenInsertHandler)
}
}
emit(PrioritizedLookupElement.withPriority(builder, 50.0))
already.add(name) already.add(name)
continue continue
} }
is MiniMemberValDecl -> {
val builder = LookupElementBuilder.create(name)
.withIcon(if (member.mutable) AllIcons.Nodes.Variable else AllIcons.Nodes.Field)
.withTypeText(typeOf(member.type), true)
emit(builder)
already.add(name)
continue
}
is MiniInitDecl -> {}
}
}
// Fallback: emit without detailed types if we couldn't resolve // Fallback: emit without detailed types if we couldn't resolve
val builder = LookupElementBuilder.create(name) val builder = LookupElementBuilder.create(name)
.withIcon(AllIcons.Nodes.Method) .withIcon(AllIcons.Nodes.Method)
.withTailText("()", true) .withTailText("()", true)
.withInsertHandler(ParenInsertHandler) .withInsertHandler(ParenInsertHandler)
emit(PrioritizedLookupElement.withPriority(builder, 50.0)) emit(builder)
already.add(name) already.add(name)
} }
} }
} }
// --- MiniAst-based inference helpers ---
private fun offerParamsInScope(emit: (com.intellij.codeInsight.lookup.LookupElement) -> Unit, mini: MiniScript, text: String, caret: Int) {
val src = mini.range.start.source
// Find function whose body contains caret or whose whole range contains caret
val fns = mini.declarations.filterIsInstance<MiniFunDecl>()
for (fn in fns) {
val start = src.offsetOf(fn.range.start)
val end = src.offsetOf(fn.range.end).coerceAtMost(text.length)
if (caret in start..end) {
for (p in fn.params) {
val builder = LookupElementBuilder.create(p.name)
.withIcon(AllIcons.Nodes.Variable)
.withTypeText(typeOf(p.type), true)
emit(builder)
}
return
}
}
}
// Lenient textual import extractor (duplicated from QuickDoc privately)
private fun extractImportsFromText(text: String): List<String> {
val result = LinkedHashSet<String>()
val re = Regex("^\\s*import\\s+([a-zA-Z_][a-zA-Z0-9_]*(?:\\.[a-zA-Z_][a-zA-Z0-9_]*)*)", RegexOption.MULTILINE)
re.findAll(text).forEach { m ->
val raw = m.groupValues.getOrNull(1)?.trim().orEmpty()
if (raw.isNotEmpty()) {
val canon = if (raw.startsWith("lyng.")) raw else "lyng.$raw"
result.add(canon)
}
}
return result.toList()
}
private fun typeOf(t: MiniTypeRef?): String { private fun typeOf(t: MiniTypeRef?): String {
val s = DocLookupUtils.typeOf(t) return when (t) {
return if (s.isEmpty()) "" else ": $s" null -> ""
is MiniTypeName -> t.segments.lastOrNull()?.name?.let { ": $it" } ?: ""
is MiniGenericType -> {
val base = typeOf(t.base).removePrefix(": ")
val args = t.args.joinToString(",") { typeOf(it).removePrefix(": ") }
": ${base}<${args}>"
}
is MiniFunctionType -> ": (fn)"
is MiniTypeVar -> ": ${t.name}"
}
} }
} }
} }
@@ -24,9 +24,13 @@ import com.intellij.openapi.editor.Editor
import com.intellij.openapi.util.TextRange import com.intellij.openapi.util.TextRange
import com.intellij.psi.PsiElement import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile import com.intellij.psi.PsiFile
import kotlinx.coroutines.runBlocking
import net.sergeych.lyng.Compiler
import net.sergeych.lyng.Pos
import net.sergeych.lyng.Source
import net.sergeych.lyng.highlight.offsetOf import net.sergeych.lyng.highlight.offsetOf
import net.sergeych.lyng.idea.LyngLanguage import net.sergeych.lyng.idea.LyngLanguage
import net.sergeych.lyng.idea.util.LyngAstManager import net.sergeych.lyng.idea.util.IdeLenientImportProvider
import net.sergeych.lyng.idea.util.TextCtx import net.sergeych.lyng.idea.util.TextCtx
import net.sergeych.lyng.miniast.* import net.sergeych.lyng.miniast.*
@@ -65,66 +69,61 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
val ident = text.substring(idRange.startOffset, idRange.endOffset) val ident = text.substring(idRange.startOffset, idRange.endOffset)
if (DEBUG_LOG) log.info("[LYNG_DEBUG] QuickDoc: ident='$ident' at ${idRange.startOffset}..${idRange.endOffset} in ${file.name}") if (DEBUG_LOG) log.info("[LYNG_DEBUG] QuickDoc: ident='$ident' at ${idRange.startOffset}..${idRange.endOffset} in ${file.name}")
// 1. Get merged mini-AST from Manager (handles local + .lyng.d merged declarations) // Build MiniAst for this file (fast and resilient). Best-effort; on failure continue with partial AST.
val mini = LyngAstManager.getMiniAst(file) ?: return null val sink = MiniAstBuilder()
val miniSource = mini.range.start.source val provider = IdeLenientImportProvider.create()
val imported = DocLookupUtils.canonicalImportedModules(mini, text) val src = Source("<ide>", text)
val mini = try {
runBlocking { Compiler.compileWithMini(src, provider, sink) }
sink.build()
} catch (t: Throwable) {
if (DEBUG_LOG) log.warn("[LYNG_DEBUG] QuickDoc: compileWithMini produced partial AST: ${t.message}")
sink.build()
} ?: MiniScript(MiniRange(Pos(src, 1, 1), Pos(src, 1, 1)))
val source = src
// Try resolve to: function param at position, function/class/val declaration at position // Try resolve to: function param at position, function/class/val declaration at position
// 1) Use unified declaration detection // 1) Use unified declaration detection
DocLookupUtils.findDeclarationAt(mini, offset, ident)?.let { (name, kind) -> DocLookupUtils.findDeclarationAt(mini, offset, ident)?.let { (name, kind) ->
if (DEBUG_LOG) log.info("[LYNG_DEBUG] QuickDoc: matched declaration '$name' kind=$kind") if (DEBUG_LOG) log.info("[LYNG_DEBUG] QuickDoc: matched declaration '$name' kind=$kind")
// Find the actual declaration object to render // Find the actual declaration object to render
mini.declarations.forEach { d -> for (d in mini.declarations) {
if (d.name == name) { if (d.name == name && source.offsetOf(d.nameStart) <= offset && source.offsetOf(d.nameStart) + d.name.length > offset) {
val s: Int = miniSource.offsetOf(d.nameStart) return renderDeclDoc(d)
if (s <= offset && s + d.name.length > offset) {
return renderDeclDoc(d, text, mini, imported)
}
} }
// Handle members if it was a member // Handle members if it was a member
if (d is MiniClassDecl) { if (d is MiniClassDecl) {
d.members.forEach { m -> for (m in d.members) {
if (m.name == name) { if (m.name == name && source.offsetOf(m.nameStart) <= offset && source.offsetOf(m.nameStart) + m.name.length > offset) {
val s: Int = miniSource.offsetOf(m.nameStart)
if (s <= offset && s + m.name.length > offset) {
return when (m) { return when (m) {
is MiniMemberFunDecl -> renderMemberFunDoc(d.name, m) is MiniMemberFunDecl -> renderMemberFunDoc(d.name, m)
is MiniMemberValDecl -> renderMemberValDoc(d.name, m) is MiniMemberValDecl -> renderMemberValDoc(d.name, m)
else -> null is MiniInitDecl -> null
} }
} }
} }
} for (cf in d.ctorFields) {
d.ctorFields.forEach { cf -> if (cf.name == name && source.offsetOf(cf.nameStart) <= offset && source.offsetOf(cf.nameStart) + cf.name.length > offset) {
if (cf.name == name) {
val s: Int = miniSource.offsetOf(cf.nameStart)
if (s <= offset && s + cf.name.length > offset) {
// Render as a member val // Render as a member val
val mv = MiniMemberValDecl( val mv = MiniMemberValDecl(
range = MiniRange(cf.nameStart, cf.nameStart), // dummy range = MiniRange(cf.nameStart, cf.nameStart), // dummy
name = cf.name, name = cf.name,
mutable = cf.mutable, mutable = cf.mutable,
type = cf.type, type = cf.type,
initRange = null,
doc = null, doc = null,
nameStart = cf.nameStart nameStart = cf.nameStart
) )
return renderMemberValDoc(d.name, mv) return renderMemberValDoc(d.name, mv)
} }
} }
} for (cf in d.classFields) {
d.classFields.forEach { cf -> if (cf.name == name && source.offsetOf(cf.nameStart) <= offset && source.offsetOf(cf.nameStart) + cf.name.length > offset) {
if (cf.name == name) {
val s: Int = miniSource.offsetOf(cf.nameStart)
if (s <= offset && s + cf.name.length > offset) {
// Render as a member val // Render as a member val
val mv = MiniMemberValDecl( val mv = MiniMemberValDecl(
range = MiniRange(cf.nameStart, cf.nameStart), // dummy range = MiniRange(cf.nameStart, cf.nameStart), // dummy
name = cf.name, name = cf.name,
mutable = cf.mutable, mutable = cf.mutable,
type = cf.type, type = cf.type,
initRange = null,
doc = null, doc = null,
nameStart = cf.nameStart nameStart = cf.nameStart
) )
@@ -132,30 +131,22 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
} }
} }
} }
}
if (d is MiniEnumDecl) { if (d is MiniEnumDecl) {
if (d.entries.contains(name)) { if (d.entries.contains(name) && offset >= source.offsetOf(d.range.start) && offset <= source.offsetOf(d.range.end)) {
val s: Int = miniSource.offsetOf(d.range.start)
val e: Int = miniSource.offsetOf(d.range.end)
if (offset >= s && offset <= e) {
// For enum constant, we don't have detailed docs in MiniAst yet, but we can render a title // For enum constant, we don't have detailed docs in MiniAst yet, but we can render a title
return "<div class='doc-title'>enum constant ${d.name}.${name}</div>" return "<div class='doc-title'>enum constant ${d.name}.${name}</div>"
} }
} }
} }
}
// Check parameters // Check parameters
mini.declarations.filterIsInstance<MiniFunDecl>().forEach { fn -> for (fn in mini.declarations.filterIsInstance<MiniFunDecl>()) {
fn.params.forEach { p -> for (p in fn.params) {
if (p.name == name) { if (p.name == name && source.offsetOf(p.nameStart) <= offset && source.offsetOf(p.nameStart) + p.name.length > offset) {
val s: Int = miniSource.offsetOf(p.nameStart)
if (s <= offset && s + p.name.length > offset) {
return renderParamDoc(fn, p) return renderParamDoc(fn, p)
} }
} }
} }
} }
}
// 3) usages in current file via Binder (resolves local variables, parameters, and classes) // 3) usages in current file via Binder (resolves local variables, parameters, and classes)
try { try {
@@ -165,72 +156,58 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
val sym = binding.symbols.firstOrNull { it.id == ref.symbolId } val sym = binding.symbols.firstOrNull { it.id == ref.symbolId }
if (sym != null) { if (sym != null) {
// Find local declaration that matches this symbol // Find local declaration that matches this symbol
var dsFound: MiniDecl? = null val ds = mini.declarations.firstOrNull { decl ->
mini.declarations.forEach { decl -> val s = source.offsetOf(decl.nameStart)
if (decl.name == sym.name) { decl.name == sym.name && s == sym.declStart
val sOffset: Int = miniSource.offsetOf(decl.nameStart)
if (sOffset == sym.declStart) {
dsFound = decl
} }
} if (ds != null) return renderDeclDoc(ds)
}
if (dsFound != null) return renderDeclDoc(dsFound, text, mini, imported)
// Check parameters // Check parameters
mini.declarations.filterIsInstance<MiniFunDecl>().forEach { fn -> for (fn in mini.declarations.filterIsInstance<MiniFunDecl>()) {
fn.params.forEach { p -> for (p in fn.params) {
if (p.name == sym.name) { val s = source.offsetOf(p.nameStart)
val sOffset: Int = miniSource.offsetOf(p.nameStart) if (p.name == sym.name && s == sym.declStart) {
if (sOffset == sym.declStart) {
return renderParamDoc(fn, p) return renderParamDoc(fn, p)
} }
} }
} }
}
// Check class members (fields/functions) // Check class members (fields/functions)
mini.declarations.filterIsInstance<MiniClassDecl>().forEach { cls -> for (cls in mini.declarations.filterIsInstance<MiniClassDecl>()) {
cls.members.forEach { m -> for (m in cls.members) {
if (m.name == sym.name) { val s = source.offsetOf(m.nameStart)
val sOffset: Int = miniSource.offsetOf(m.nameStart) if (m.name == sym.name && s == sym.declStart) {
if (sOffset == sym.declStart) {
return when (m) { return when (m) {
is MiniMemberFunDecl -> renderMemberFunDoc(cls.name, m) is MiniMemberFunDecl -> renderMemberFunDoc(cls.name, m)
is MiniMemberValDecl -> renderMemberValDoc(cls.name, m) is MiniMemberValDecl -> renderMemberValDoc(cls.name, m)
else -> null is MiniInitDecl -> null
} }
} }
} }
} for (cf in cls.ctorFields) {
cls.ctorFields.forEach { cf -> val s = source.offsetOf(cf.nameStart)
if (cf.name == sym.name) { if (cf.name == sym.name && s == sym.declStart) {
val sOffset: Int = miniSource.offsetOf(cf.nameStart)
if (sOffset == sym.declStart) {
// Render as a member val // Render as a member val
val mv = MiniMemberValDecl( val mv = MiniMemberValDecl(
range = MiniRange(cf.nameStart, cf.nameStart), // dummy range = MiniRange(cf.nameStart, cf.nameStart), // dummy
name = cf.name, name = cf.name,
mutable = cf.mutable, mutable = cf.mutable,
type = cf.type, type = cf.type,
initRange = null,
doc = null, doc = null,
nameStart = cf.nameStart nameStart = cf.nameStart
) )
return renderMemberValDoc(cls.name, mv) return renderMemberValDoc(cls.name, mv)
} }
} }
} for (cf in cls.classFields) {
cls.classFields.forEach { cf -> val s = source.offsetOf(cf.nameStart)
if (cf.name == sym.name) { if (cf.name == sym.name && s == sym.declStart) {
val sOffset: Int = miniSource.offsetOf(cf.nameStart)
if (sOffset == sym.declStart) {
// Render as a member val // Render as a member val
val mv = MiniMemberValDecl( val mv = MiniMemberValDecl(
range = MiniRange(cf.nameStart, cf.nameStart), // dummy range = MiniRange(cf.nameStart, cf.nameStart), // dummy
name = cf.name, name = cf.name,
mutable = cf.mutable, mutable = cf.mutable,
type = cf.type, type = cf.type,
initRange = null,
doc = null, doc = null,
nameStart = cf.nameStart nameStart = cf.nameStart
) )
@@ -240,7 +217,6 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
} }
} }
} }
}
} catch (e: Throwable) { } catch (e: Throwable) {
if (DEBUG_LOG) log.warn("[LYNG_DEBUG] QuickDoc: local binder resolution failed: ${e.message}") if (DEBUG_LOG) log.warn("[LYNG_DEBUG] QuickDoc: local binder resolution failed: ${e.message}")
} }
@@ -284,9 +260,9 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
} else null } else null
} }
else -> { else -> {
DocLookupUtils.guessReceiverClassViaMini(mini, text, dotPos, importedModules) val guessed = DocLookupUtils.guessClassFromCallBefore(text, dotPos, importedModules)
?: DocLookupUtils.guessClassFromCallBefore(text, dotPos, importedModules, mini) if (guessed != null) guessed
?: run { else {
// handle this@Type or as Type // handle this@Type or as Type
val i2 = TextCtx.prevNonWs(text, dotPos - 1) val i2 = TextCtx.prevNonWs(text, dotPos - 1)
if (i2 >= 0) { if (i2 >= 0) {
@@ -307,16 +283,12 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
} }
if (DEBUG_LOG) log.info("[LYNG_DEBUG] QuickDoc: memberCtx dotPos=${dotPos} chBeforeDot='${if (dotPos>0) text[dotPos-1] else ' '}' classGuess=${className} imports=${importedModules}") if (DEBUG_LOG) log.info("[LYNG_DEBUG] QuickDoc: memberCtx dotPos=${dotPos} chBeforeDot='${if (dotPos>0) text[dotPos-1] else ' '}' classGuess=${className} imports=${importedModules}")
if (className != null) { if (className != null) {
DocLookupUtils.resolveMemberWithInheritance(importedModules, className, ident, mini)?.let { (owner, member) -> DocLookupUtils.resolveMemberWithInheritance(importedModules, className, ident)?.let { (owner, member) ->
if (DEBUG_INHERITANCE) log.info("[LYNG_DEBUG] QuickDoc: literal/call '$ident' resolved to $owner.${member.name}") if (DEBUG_INHERITANCE) log.info("[LYNG_DEBUG] QuickDoc: literal/call '$ident' resolved to $owner.${member.name}")
return when (member) { return when (member) {
is MiniMemberFunDecl -> renderMemberFunDoc(owner, member) is MiniMemberFunDecl -> renderMemberFunDoc(owner, member)
is MiniMemberValDecl -> renderMemberValDoc(owner, member) is MiniMemberValDecl -> renderMemberValDoc(owner, member)
is MiniInitDecl -> null is MiniInitDecl -> null
is MiniFunDecl -> renderDeclDoc(member, text, mini, importedModules)
is MiniValDecl -> renderDeclDoc(member, text, mini, importedModules)
is MiniClassDecl -> renderDeclDoc(member, text, mini, importedModules)
is MiniEnumDecl -> renderDeclDoc(member, text, mini, importedModules)
} }
} }
log.info("[LYNG_DEBUG] QuickDoc: resolve failed for ${className}.${ident}") log.info("[LYNG_DEBUG] QuickDoc: resolve failed for ${className}.${ident}")
@@ -327,7 +299,7 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
// 4) As a fallback, if the caret is on an identifier text that matches any declaration name, show that // 4) As a fallback, if the caret is on an identifier text that matches any declaration name, show that
mini.declarations.firstOrNull { it.name == ident }?.let { mini.declarations.firstOrNull { it.name == ident }?.let {
log.info("[LYNG_DEBUG] QuickDoc: fallback by name '${it.name}' kind=${it::class.simpleName}") log.info("[LYNG_DEBUG] QuickDoc: fallback by name '${it.name}' kind=${it::class.simpleName}")
return renderDeclDoc(it, text, mini, imported) return renderDeclDoc(it)
} }
// 4) Consult BuiltinDocRegistry for imported modules (top-level and class members) // 4) Consult BuiltinDocRegistry for imported modules (top-level and class members)
@@ -347,13 +319,13 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
if (arity != null && chosen.params.size != arity && matches.size > 1) { if (arity != null && chosen.params.size != arity && matches.size > 1) {
return renderOverloads(ident, matches) return renderOverloads(ident, matches)
} }
return renderDeclDoc(chosen, text, mini, imported) return renderDeclDoc(chosen)
} }
// Also allow values/consts // Also allow values/consts
docs.filterIsInstance<MiniValDecl>().firstOrNull { it.name == ident }?.let { return renderDeclDoc(it, text, mini, imported) } docs.filterIsInstance<MiniValDecl>().firstOrNull { it.name == ident }?.let { return renderDeclDoc(it) }
// And classes/enums // And classes/enums
docs.filterIsInstance<MiniClassDecl>().firstOrNull { it.name == ident }?.let { return renderDeclDoc(it, text, mini, imported) } docs.filterIsInstance<MiniClassDecl>().firstOrNull { it.name == ident }?.let { return renderDeclDoc(it) }
docs.filterIsInstance<MiniEnumDecl>().firstOrNull { it.name == ident }?.let { return renderDeclDoc(it, text, mini, imported) } docs.filterIsInstance<MiniEnumDecl>().firstOrNull { it.name == ident }?.let { return renderDeclDoc(it) }
} }
// Defensive fallback: if nothing found and it's a well-known stdlib function, render minimal inline docs // Defensive fallback: if nothing found and it's a well-known stdlib function, render minimal inline docs
if (ident == "println" || ident == "print") { if (ident == "println" || ident == "print") {
@@ -367,16 +339,12 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
val lhs = previousWordBefore(text, idRange.startOffset) val lhs = previousWordBefore(text, idRange.startOffset)
if (lhs != null && hasDotBetween(text, lhs.endOffset, idRange.startOffset)) { if (lhs != null && hasDotBetween(text, lhs.endOffset, idRange.startOffset)) {
val className = text.substring(lhs.startOffset, lhs.endOffset) val className = text.substring(lhs.startOffset, lhs.endOffset)
DocLookupUtils.resolveMemberWithInheritance(importedModules, className, ident, mini)?.let { (owner, member) -> DocLookupUtils.resolveMemberWithInheritance(importedModules, className, ident)?.let { (owner, member) ->
if (DEBUG_INHERITANCE) log.info("[LYNG_DEBUG] Inheritance resolved $className.$ident to $owner.${member.name}") if (DEBUG_INHERITANCE) log.info("[LYNG_DEBUG] Inheritance resolved $className.$ident to $owner.${member.name}")
return when (member) { return when (member) {
is MiniMemberFunDecl -> renderMemberFunDoc(owner, member) is MiniMemberFunDecl -> renderMemberFunDoc(owner, member)
is MiniMemberValDecl -> renderMemberValDoc(owner, member) is MiniMemberValDecl -> renderMemberValDoc(owner, member)
is MiniInitDecl -> null is MiniInitDecl -> null
is MiniFunDecl -> renderDeclDoc(member, text, mini, importedModules)
is MiniValDecl -> renderDeclDoc(member, text, mini, importedModules)
is MiniClassDecl -> renderDeclDoc(member, text, mini, importedModules)
is MiniEnumDecl -> renderDeclDoc(member, text, mini, importedModules)
} }
} }
} else { } else {
@@ -387,19 +355,15 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
if (dotPos != null) { if (dotPos != null) {
val guessed = when { val guessed = when {
looksLikeListLiteralBefore(text, dotPos) -> "List" looksLikeListLiteralBefore(text, dotPos) -> "List"
else -> DocLookupUtils.guessClassFromCallBefore(text, dotPos, importedModules, mini) else -> DocLookupUtils.guessClassFromCallBefore(text, dotPos, importedModules)
} }
if (guessed != null) { if (guessed != null) {
DocLookupUtils.resolveMemberWithInheritance(importedModules, guessed, ident, mini)?.let { (owner, member) -> DocLookupUtils.resolveMemberWithInheritance(importedModules, guessed, ident)?.let { (owner, member) ->
if (DEBUG_INHERITANCE) log.info("[LYNG_DEBUG] Heuristic '$guessed.$ident' resolved via inheritance to $owner.${member.name}") if (DEBUG_INHERITANCE) log.info("[LYNG_DEBUG] Heuristic '$guessed.$ident' resolved via inheritance to $owner.${member.name}")
return when (member) { return when (member) {
is MiniMemberFunDecl -> renderMemberFunDoc(owner, member) is MiniMemberFunDecl -> renderMemberFunDoc(owner, member)
is MiniMemberValDecl -> renderMemberValDoc(owner, member) is MiniMemberValDecl -> renderMemberValDoc(owner, member)
is MiniInitDecl -> null is MiniInitDecl -> null
is MiniFunDecl -> renderDeclDoc(member, text, mini, importedModules)
is MiniValDecl -> renderDeclDoc(member, text, mini, importedModules)
is MiniClassDecl -> renderDeclDoc(member, text, mini, importedModules)
is MiniEnumDecl -> renderDeclDoc(member, text, mini, importedModules)
} }
} }
} else { } else {
@@ -407,23 +371,19 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
run { run {
val candidates = listOf("String", "Iterable", "Iterator", "List", "Collection", "Array", "Dict", "Regex") val candidates = listOf("String", "Iterable", "Iterator", "List", "Collection", "Array", "Dict", "Regex")
for (c in candidates) { for (c in candidates) {
DocLookupUtils.resolveMemberWithInheritance(importedModules, c, ident, mini)?.let { (owner, member) -> DocLookupUtils.resolveMemberWithInheritance(importedModules, c, ident)?.let { (owner, member) ->
if (DEBUG_INHERITANCE) log.info("[LYNG_DEBUG] Candidate '$c.$ident' resolved via inheritance to $owner.${member.name}") if (DEBUG_INHERITANCE) log.info("[LYNG_DEBUG] Candidate '$c.$ident' resolved via inheritance to $owner.${member.name}")
return when (member) { return when (member) {
is MiniMemberFunDecl -> renderMemberFunDoc(owner, member) is MiniMemberFunDecl -> renderMemberFunDoc(owner, member)
is MiniMemberValDecl -> renderMemberValDoc(owner, member) is MiniMemberValDecl -> renderMemberValDoc(owner, member)
is MiniInitDecl -> null is MiniInitDecl -> null
is MiniFunDecl -> renderDeclDoc(member, text, mini, importedModules)
is MiniValDecl -> renderDeclDoc(member, text, mini, importedModules)
is MiniClassDecl -> renderDeclDoc(member, text, mini, importedModules)
is MiniEnumDecl -> renderDeclDoc(member, text, mini, importedModules)
} }
} }
} }
} }
// As a last resort try aggregated String members (extensions from stdlib text) // As a last resort try aggregated String members (extensions from stdlib text)
run { run {
val classes = DocLookupUtils.aggregateClasses(importedModules, mini) val classes = DocLookupUtils.aggregateClasses(importedModules)
val stringCls = classes["String"] val stringCls = classes["String"]
val m = stringCls?.members?.firstOrNull { it.name == ident } val m = stringCls?.members?.firstOrNull { it.name == ident }
if (m != null) { if (m != null) {
@@ -436,16 +396,12 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
} }
} }
// Search across classes; prefer Iterable, then Iterator, then List for common ops // Search across classes; prefer Iterable, then Iterator, then List for common ops
DocLookupUtils.findMemberAcrossClasses(importedModules, ident, mini)?.let { (owner, member) -> DocLookupUtils.findMemberAcrossClasses(importedModules, ident)?.let { (owner, member) ->
if (DEBUG_INHERITANCE) log.info("[LYNG_DEBUG] Cross-class '$ident' resolved to $owner.${member.name}") if (DEBUG_INHERITANCE) log.info("[LYNG_DEBUG] Cross-class '$ident' resolved to $owner.${member.name}")
return when (member) { return when (member) {
is MiniMemberFunDecl -> renderMemberFunDoc(owner, member) is MiniMemberFunDecl -> renderMemberFunDoc(owner, member)
is MiniMemberValDecl -> renderMemberValDoc(owner, member) is MiniMemberValDecl -> renderMemberValDoc(owner, member)
is MiniInitDecl -> null is MiniInitDecl -> null
is MiniFunDecl -> renderDeclDoc(member, text, mini, importedModules)
is MiniValDecl -> renderDeclDoc(member, text, mini, importedModules)
is MiniClassDecl -> renderDeclDoc(member, text, mini, importedModules)
is MiniEnumDecl -> renderDeclDoc(member, text, mini, importedModules)
} }
} }
} }
@@ -461,39 +417,25 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
private fun ensureExternalDocsRegistered() { @Suppress("UNUSED_EXPRESSION") externalDocsLoaded } private fun ensureExternalDocsRegistered() { @Suppress("UNUSED_EXPRESSION") externalDocsLoaded }
private fun tryLoadExternalDocs(): Boolean { private fun tryLoadExternalDocs(): Boolean {
var anyLoaded = false return try {
try {
// Try known registrars; ignore failures if module is absent // Try known registrars; ignore failures if module is absent
val cls = Class.forName("net.sergeych.lyngio.docs.FsBuiltinDocs") val cls = Class.forName("net.sergeych.lyngio.docs.FsBuiltinDocs")
val m = cls.getMethod("ensure") val m = cls.getMethod("ensure")
m.invoke(null) m.invoke(null)
log.info("[LYNG_DEBUG] QuickDoc: external docs loaded: net.sergeych.lyngio.docs.FsBuiltinDocs.ensure() OK") log.info("[LYNG_DEBUG] QuickDoc: external docs loaded: net.sergeych.lyngio.docs.FsBuiltinDocs.ensure() OK")
anyLoaded = true true
} catch (_: Throwable) {} } catch (_: Throwable) {
try {
val cls = Class.forName("net.sergeych.lyngio.docs.ProcessBuiltinDocs")
val m = cls.getMethod("ensure")
m.invoke(null)
log.info("[LYNG_DEBUG] QuickDoc: external docs loaded: net.sergeych.lyngio.docs.ProcessBuiltinDocs.ensure() OK")
anyLoaded = true
} catch (_: Throwable) {}
if (!anyLoaded) {
// Seed a minimal plugin-local fallback so Path docs still work without lyngio // Seed a minimal plugin-local fallback so Path docs still work without lyngio
val seeded = try { val seeded = try {
FsDocsFallback.ensureOnce() FsDocsFallback.ensureOnce()
ProcessDocsFallback.ensureOnce()
true
} catch (_: Throwable) { false } } catch (_: Throwable) { false }
if (seeded) { if (seeded) {
log.info("[LYNG_DEBUG] QuickDoc: external docs NOT found; seeded plugin fallbacks") log.info("[LYNG_DEBUG] QuickDoc: external docs NOT found; seeded plugin fallback for lyng.io.fs")
} else { } else {
log.info("[LYNG_DEBUG] QuickDoc: external docs NOT found (lyngio absent on classpath)") log.info("[LYNG_DEBUG] QuickDoc: external docs NOT found (lyngio absent on classpath)")
} }
return seeded seeded
} }
return true
} }
override fun getCustomDocumentationElement( override fun getCustomDocumentationElement(
@@ -507,37 +449,25 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
return contextElement ?: file.findElementAt(targetOffset) return contextElement ?: file.findElementAt(targetOffset)
} }
private fun renderDeclDoc(d: MiniDecl, text: String, mini: MiniScript, imported: List<String>): String { private fun renderDeclDoc(d: MiniDecl): String {
val title = when (d) { val title = when (d) {
is MiniFunDecl -> "function ${d.name}${signatureOf(d)}" is MiniFunDecl -> "function ${d.name}${signatureOf(d)}"
is MiniClassDecl -> "class ${d.name}" is MiniClassDecl -> "class ${d.name}"
is MiniEnumDecl -> "enum ${d.name} { ${d.entries.joinToString(", ")} }" is MiniEnumDecl -> "enum ${d.name} { ${d.entries.joinToString(", ")} }"
is MiniValDecl -> { is MiniValDecl -> if (d.mutable) "var ${d.name}${typeOf(d.type)}" else "val ${d.name}${typeOf(d.type)}"
val t = d.type ?: DocLookupUtils.inferTypeRefForVal(d, text, imported, mini)
val typeStr = if (t == null) ": Object?" else typeOf(t)
if (d.mutable) "var ${d.name}${typeStr}" else "val ${d.name}${typeStr}"
}
} }
// Show full detailed documentation, not just the summary
val raw = d.doc?.raw
val doc: String? = if (raw.isNullOrBlank()) null else MarkdownRenderer.render(raw)
val sb = StringBuilder() val sb = StringBuilder()
sb.append("<div class='doc-title'>").append(htmlEscape(title)).append("</div>") sb.append("<div class='doc-title'>").append(htmlEscape(title)).append("</div>")
sb.append(renderDocBody(d.doc)) if (!doc.isNullOrBlank()) sb.append(styledMarkdown(doc))
return sb.toString() return sb.toString()
} }
private fun renderParamDoc(fn: MiniFunDecl, p: MiniParam): String { private fun renderParamDoc(fn: MiniFunDecl, p: MiniParam): String {
val title = "parameter ${p.name}${typeOf(p.type)} in ${fn.name}${signatureOf(fn)}" val title = "parameter ${p.name}${typeOf(p.type)} in ${fn.name}${signatureOf(fn)}"
val sb = StringBuilder() return "<div class='doc-title'>${htmlEscape(title)}</div>"
sb.append("<div class='doc-title'>").append(htmlEscape(title)).append("</div>")
// Find matching @param tag
fn.doc?.tags?.get("param")?.forEach { tag ->
val parts = tag.split(Regex("\\s+"), 2)
if (parts.getOrNull(0) == p.name && parts.size > 1) {
sb.append(styledMarkdown(MarkdownRenderer.render(parts[1])))
}
}
return sb.toString()
} }
private fun renderMemberFunDoc(className: String, m: MiniMemberFunDecl): String { private fun renderMemberFunDoc(className: String, m: MiniMemberFunDecl): String {
@@ -548,26 +478,37 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
val ret = typeOf(m.returnType) val ret = typeOf(m.returnType)
val staticStr = if (m.isStatic) "static " else "" val staticStr = if (m.isStatic) "static " else ""
val title = "${staticStr}method $className.${m.name}(${params})${ret}" val title = "${staticStr}method $className.${m.name}(${params})${ret}"
val raw = m.doc?.raw
val doc: String? = if (raw.isNullOrBlank()) null else MarkdownRenderer.render(raw)
val sb = StringBuilder() val sb = StringBuilder()
sb.append("<div class='doc-title'>").append(htmlEscape(title)).append("</div>") sb.append("<div class='doc-title'>").append(htmlEscape(title)).append("</div>")
sb.append(renderDocBody(m.doc)) if (!doc.isNullOrBlank()) sb.append(styledMarkdown(doc))
return sb.toString() return sb.toString()
} }
private fun renderMemberValDoc(className: String, m: MiniMemberValDecl): String { private fun renderMemberValDoc(className: String, m: MiniMemberValDecl): String {
val ts = if (m.type == null) ": Object?" else typeOf(m.type) val ts = typeOf(m.type)
val kind = if (m.mutable) "var" else "val" val kind = if (m.mutable) "var" else "val"
val staticStr = if (m.isStatic) "static " else "" val staticStr = if (m.isStatic) "static " else ""
val title = "${staticStr}${kind} $className.${m.name}${ts}" val title = "${staticStr}${kind} $className.${m.name}${ts}"
val raw = m.doc?.raw
val doc: String? = if (raw.isNullOrBlank()) null else MarkdownRenderer.render(raw)
val sb = StringBuilder() val sb = StringBuilder()
sb.append("<div class='doc-title'>").append(htmlEscape(title)).append("</div>") sb.append("<div class='doc-title'>").append(htmlEscape(title)).append("</div>")
sb.append(renderDocBody(m.doc)) if (!doc.isNullOrBlank()) sb.append(styledMarkdown(doc))
return sb.toString() return sb.toString()
} }
private fun typeOf(t: MiniTypeRef?): String { private fun typeOf(t: MiniTypeRef?): String = when (t) {
val s = DocLookupUtils.typeOf(t) is MiniTypeName -> ": ${t.segments.joinToString(".") { it.name }}${if (t.nullable) "?" else ""}"
return if (s.isEmpty()) (if (t == null) ": Object?" else "") else ": $s" is MiniGenericType -> {
val base = typeOf(t.base).removePrefix(": ")
val args = t.args.joinToString(", ") { typeOf(it).removePrefix(": ") }
": ${base}<${args}>${if (t.nullable) "?" else ""}"
}
is MiniFunctionType -> ": (..) -> ..${if (t.nullable) "?" else ""}"
is MiniTypeVar -> ": ${t.name}${if (t.nullable) "?" else ""}"
null -> ""
} }
private fun signatureOf(fn: MiniFunDecl): String { private fun signatureOf(fn: MiniFunDecl): String {
@@ -667,64 +608,11 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
return if (e > s) TextRange(s, e) else null return if (e > s) TextRange(s, e) else null
} }
private fun renderDocBody(doc: MiniDoc?): String {
if (doc == null) return ""
val sb = StringBuilder()
if (doc.raw.isNotBlank()) {
sb.append(styledMarkdown(MarkdownRenderer.render(doc.raw)))
}
if (doc.tags.isNotEmpty()) {
sb.append(renderTags(doc.tags))
}
return sb.toString()
}
private fun renderTags(tags: Map<String, List<String>>): String {
if (tags.isEmpty()) return ""
val sb = StringBuilder()
sb.append("<table class='sections'>")
fun section(title: String, list: List<String>, isKeyValue: Boolean = false) {
if (list.isEmpty()) return
sb.append("<tr><td valign='top' class='section'><p>").append(htmlEscape(title)).append(":</p></td><td valign='top'>")
list.forEachIndexed { index, item ->
if (index > 0) sb.append("<p>")
if (isKeyValue) {
val parts = item.split(Regex("\\s+"), 2)
sb.append("<code>").append(htmlEscape(parts[0])).append("</code>")
if (parts.size > 1) {
sb.append(" — ").append(MarkdownRenderer.render(parts[1]).removePrefix("<p>").removeSuffix("</p>"))
}
} else {
sb.append(MarkdownRenderer.render(item).removePrefix("<p>").removeSuffix("</p>"))
}
}
sb.append("</td></tr>")
}
section("Parameters", tags["param"] ?: emptyList(), isKeyValue = true)
section("Returns", tags["return"] ?: emptyList())
section("Throws", tags["throws"] ?: emptyList(), isKeyValue = true)
tags.forEach { (name, list) ->
if (name !in listOf("param", "return", "throws")) {
section(name.replaceFirstChar { if (it.isLowerCase()) it.titlecase() else it.toString() }, list)
}
}
sb.append("</table>")
return sb.toString()
}
private fun previousWordBefore(text: String, offset: Int): TextRange? { private fun previousWordBefore(text: String, offset: Int): TextRange? {
// skip spaces and the dot to the left, but stop after hitting a non-identifier boundary // skip spaces and dots to the left, but stop after hitting a non-identifier or dot boundary
var i = (offset - 1).coerceAtLeast(0) var i = (offset - 1).coerceAtLeast(0)
// skip trailing spaces // first, move left past spaces
while (i >= 0 && text[i].isWhitespace()) i-- while (i > 0 && text[i].isWhitespace()) i--
// skip the dot if present
if (i >= 0 && text[i] == '.') i--
// skip spaces before the dot
while (i >= 0 && text[i].isWhitespace()) i--
// remember position to check for dot between words // remember position to check for dot between words
val end = i + 1 val end = i + 1
// now find the start of the identifier // now find the start of the identifier
@@ -1,69 +0,0 @@
/*
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
/*
* Minimal fallback docs seeding for `lyng.io.process` used only inside the IDEA plugin
* when external docs module (lyngio) is not present on the classpath.
*/
package net.sergeych.lyng.idea.docs
import net.sergeych.lyng.miniast.BuiltinDocRegistry
import net.sergeych.lyng.miniast.ParamDoc
import net.sergeych.lyng.miniast.type
internal object ProcessDocsFallback {
@Volatile
private var seeded = false
fun ensureOnce(): Boolean {
if (seeded) return true
synchronized(this) {
if (seeded) return true
BuiltinDocRegistry.module("lyng.io.process") {
classDoc(name = "Process", doc = "Process execution and control.") {
method(
name = "execute",
doc = "Execute a process with arguments.",
params = listOf(ParamDoc("executable", type("lyng.String")), ParamDoc("args", type("lyng.List"))),
returns = type("RunningProcess"),
isStatic = true
)
method(
name = "shell",
doc = "Execute a command via system shell.",
params = listOf(ParamDoc("command", type("lyng.String"))),
returns = type("RunningProcess"),
isStatic = true
)
}
classDoc(name = "RunningProcess", doc = "Handle to a running process.") {
method(name = "stdout", doc = "Get standard output stream as a Flow of lines.", returns = type("lyng.Flow"))
method(name = "stderr", doc = "Get standard error stream as a Flow of lines.", returns = type("lyng.Flow"))
method(name = "waitFor", doc = "Wait for the process to exit.", returns = type("lyng.Int"))
method(name = "signal", doc = "Send a signal to the process.", params = listOf(ParamDoc("signal", type("lyng.String"))))
method(name = "destroy", doc = "Forcefully terminate the process.")
}
valDoc(name = "Process", doc = "Process execution and control.", type = type("Process"))
valDoc(name = "RunningProcess", doc = "Handle to a running process.", type = type("RunningProcess"))
}
seeded = true
return true
}
}
}
@@ -1,5 +1,5 @@
/* /*
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com * Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -150,18 +150,16 @@ class LyngGrazieAnnotator : ExternalAnnotator<LyngGrazieAnnotator.Input, LyngGra
} }
log.info("LyngGrazieAnnotator.apply: used=${chosenEntry ?: "<none>"}, totalFindings=$totalReturned, painting=${findings.size}") log.info("LyngGrazieAnnotator.apply: used=${chosenEntry ?: "<none>"}, totalFindings=$totalReturned, painting=${findings.size}")
// IMPORTANT: Do NOT fallback to the tiny bundled vocabulary on modern IDEs.
// If Grazie/Natural Languages processing returned nothing, we simply exit here
// to avoid low‑quality results from the legacy dictionary.
if (findings.isEmpty()) return
for (f in findings) { for (f in findings) {
val ab = holder.newAnnotation(HighlightSeverity.INFORMATION, f.message).range(f.range) val ab = holder.newAnnotation(HighlightSeverity.INFORMATION, f.message).range(f.range)
applyTypoStyleIfRequested(file, ab) applyTypoStyleIfRequested(file, ab)
ab.create() ab.create()
} }
// SUPPLEMENT: Always run the fallback spellchecker to ensure spelling errors are not ignored.
// It will avoid duplicating findings already reported by Grazie.
val painted = fallbackWithLegacySpellcheckerIfAvailable(file, fragments, holder, findings)
if (painted > 0) {
log.info("LyngGrazieAnnotator.apply: supplemented with $painted typos from legacy engine")
}
} }
private fun scheduleOneShotRestart(file: PsiFile, modStamp: Long) { private fun scheduleOneShotRestart(file: PsiFile, modStamp: Long) {
@@ -383,8 +381,7 @@ class LyngGrazieAnnotator : ExternalAnnotator<LyngGrazieAnnotator.Input, LyngGra
private fun fallbackWithLegacySpellcheckerIfAvailable( private fun fallbackWithLegacySpellcheckerIfAvailable(
file: PsiFile, file: PsiFile,
fragments: List<Pair<TextContent, TextRange>>, fragments: List<Pair<TextContent, TextRange>>,
holder: AnnotationHolder, holder: AnnotationHolder
existingFindings: List<Finding>
): Int { ): Int {
return try { return try {
val mgrCls = Class.forName("com.intellij.spellchecker.SpellCheckerManager") val mgrCls = Class.forName("com.intellij.spellchecker.SpellCheckerManager")
@@ -392,12 +389,12 @@ class LyngGrazieAnnotator : ExternalAnnotator<LyngGrazieAnnotator.Input, LyngGra
val isCorrect = mgrCls.methods.firstOrNull { it.name == "isCorrect" && it.parameterCount == 1 && it.parameterTypes[0] == String::class.java } val isCorrect = mgrCls.methods.firstOrNull { it.name == "isCorrect" && it.parameterCount == 1 && it.parameterTypes[0] == String::class.java }
if (getInstance == null || isCorrect == null) { if (getInstance == null || isCorrect == null) {
// No legacy spellchecker API available — fall back to naive painter // No legacy spellchecker API available — fall back to naive painter
return naiveFallbackPaint(file, fragments, holder, existingFindings) return naiveFallbackPaint(file, fragments, holder)
} }
val mgr = getInstance.invoke(null, file.project) val mgr = getInstance.invoke(null, file.project)
if (mgr == null) { if (mgr == null) {
// Legacy manager not present for this project — use naive fallback // Legacy manager not present for this project — use naive fallback
return naiveFallbackPaint(file, fragments, holder, existingFindings) return naiveFallbackPaint(file, fragments, holder)
} }
var painted = 0 var painted = 0
val docText = file.viewProvider.document?.text ?: return 0 val docText = file.viewProvider.document?.text ?: return 0
@@ -414,18 +411,13 @@ class LyngGrazieAnnotator : ExternalAnnotator<LyngGrazieAnnotator.Input, LyngGra
for (part in parts) { for (part in parts) {
if (part.length <= 2) continue if (part.length <= 2) continue
if (isAllowedWord(part)) continue if (isAllowedWord(part)) continue
// Quick allowlist for very common words to reduce noise if dictionaries differ
val ok = try { isCorrect.invoke(mgr, part) as? Boolean } catch (_: Throwable) { null }
if (ok == false) {
// Map part back to original token occurrence within this hostRange // Map part back to original token occurrence within this hostRange
val localStart = m.range.first + token.indexOf(part) val localStart = m.range.first + token.indexOf(part)
val localEnd = localStart + part.length val localEnd = localStart + part.length
val abs = TextRange(hostRange.startOffset + localStart, hostRange.startOffset + localEnd) val abs = TextRange(hostRange.startOffset + localStart, hostRange.startOffset + localEnd)
// Avoid duplicating findings from Grazie
if (existingFindings.any { it.range.intersects(abs) }) continue
// Quick allowlist for very common words to reduce noise if dictionaries differ
val ok = try { isCorrect.invoke(mgr, part) as? Boolean } catch (_: Throwable) { null }
if (ok == false) {
paintTypoAnnotation(file, holder, abs, part) paintTypoAnnotation(file, holder, abs, part)
painted++ painted++
flagged++ flagged++
@@ -438,15 +430,14 @@ class LyngGrazieAnnotator : ExternalAnnotator<LyngGrazieAnnotator.Input, LyngGra
painted painted
} catch (_: Throwable) { } catch (_: Throwable) {
// If legacy manager is not available, fall back to a very naive heuristic (no external deps) // If legacy manager is not available, fall back to a very naive heuristic (no external deps)
return naiveFallbackPaint(file, fragments, holder, existingFindings) return naiveFallbackPaint(file, fragments, holder)
} }
} }
private fun naiveFallbackPaint( private fun naiveFallbackPaint(
file: PsiFile, file: PsiFile,
fragments: List<Pair<TextContent, TextRange>>, fragments: List<Pair<TextContent, TextRange>>,
holder: AnnotationHolder, holder: AnnotationHolder
existingFindings: List<Finding>
): Int { ): Int {
var painted = 0 var painted = 0
val docText = file.viewProvider.document?.text val docText = file.viewProvider.document?.text
@@ -470,14 +461,6 @@ class LyngGrazieAnnotator : ExternalAnnotator<LyngGrazieAnnotator.Input, LyngGra
seen++ seen++
val lower = part.lowercase() val lower = part.lowercase()
if (lower.length <= 2 || isAllowedWord(part)) continue if (lower.length <= 2 || isAllowedWord(part)) continue
val localStart = m.range.first + token.indexOf(part)
val localEnd = localStart + part.length
val abs = TextRange(hostRange.startOffset + localStart, hostRange.startOffset + localEnd)
// Avoid duplicating findings from Grazie
if (existingFindings.any { it.range.intersects(abs) }) continue
// Heuristic: no vowels OR 3 repeated chars OR ends with unlikely double consonants // Heuristic: no vowels OR 3 repeated chars OR ends with unlikely double consonants
val noVowel = lower.none { it in "aeiouy" } val noVowel = lower.none { it in "aeiouy" }
val triple = Regex("(.)\\1\\1").containsMatchIn(lower) val triple = Regex("(.)\\1\\1").containsMatchIn(lower)
@@ -497,6 +480,9 @@ class LyngGrazieAnnotator : ExternalAnnotator<LyngGrazieAnnotator.Input, LyngGra
} }
} }
if (looksWrong) { if (looksWrong) {
val localStart = m.range.first + token.indexOf(part)
val localEnd = localStart + part.length
val abs = TextRange(hostRange.startOffset + localStart, hostRange.startOffset + localEnd)
paintTypoAnnotation(file, holder, abs, part) paintTypoAnnotation(file, holder, abs, part)
painted++ painted++
flagged++ flagged++
@@ -43,15 +43,10 @@ class LyngColorSettingsPage : ColorSettingsPage {
} }
var counter = 0 var counter = 0
outer@ while (counter < 10) {
if (counter == 5) return@outer
counter = counter + 1 counter = counter + 1
}
""".trimIndent() """.trimIndent()
override fun getAdditionalHighlightingTagToDescriptorMap(): MutableMap<String, TextAttributesKey> = mutableMapOf( override fun getAdditionalHighlightingTagToDescriptorMap(): MutableMap<String, TextAttributesKey>? = null
"label" to LyngHighlighterColors.LABEL
)
override fun getAttributeDescriptors(): Array<AttributesDescriptor> = arrayOf( override fun getAttributeDescriptors(): Array<AttributesDescriptor> = arrayOf(
AttributesDescriptor("Keyword", LyngHighlighterColors.KEYWORD), AttributesDescriptor("Keyword", LyngHighlighterColors.KEYWORD),
@@ -63,7 +58,6 @@ class LyngColorSettingsPage : ColorSettingsPage {
AttributesDescriptor("Punctuation", LyngHighlighterColors.PUNCT), AttributesDescriptor("Punctuation", LyngHighlighterColors.PUNCT),
// Semantic // Semantic
AttributesDescriptor("Annotation (semantic)", LyngHighlighterColors.ANNOTATION), AttributesDescriptor("Annotation (semantic)", LyngHighlighterColors.ANNOTATION),
AttributesDescriptor("Label (semantic)", LyngHighlighterColors.LABEL),
AttributesDescriptor("Variable (semantic)", LyngHighlighterColors.VARIABLE), AttributesDescriptor("Variable (semantic)", LyngHighlighterColors.VARIABLE),
AttributesDescriptor("Value (semantic)", LyngHighlighterColors.VALUE), AttributesDescriptor("Value (semantic)", LyngHighlighterColors.VALUE),
AttributesDescriptor("Function (semantic)", LyngHighlighterColors.FUNCTION), AttributesDescriptor("Function (semantic)", LyngHighlighterColors.FUNCTION),
@@ -82,9 +82,4 @@ object LyngHighlighterColors {
val ENUM_CONSTANT: TextAttributesKey = TextAttributesKey.createTextAttributesKey( val ENUM_CONSTANT: TextAttributesKey = TextAttributesKey.createTextAttributesKey(
"LYNG_ENUM_CONSTANT", DefaultLanguageHighlighterColors.STATIC_FIELD "LYNG_ENUM_CONSTANT", DefaultLanguageHighlighterColors.STATIC_FIELD
) )
// Labels (label@ or @label used as exit target)
val LABEL: TextAttributesKey = TextAttributesKey.createTextAttributesKey(
"LYNG_LABEL", DefaultLanguageHighlighterColors.LABEL
)
} }
@@ -1,5 +1,5 @@
/* /*
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com * Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -33,10 +33,10 @@ class LyngLexer : LexerBase() {
private val keywords = setOf( private val keywords = setOf(
"fun", "val", "var", "class", "interface", "type", "import", "as", "fun", "val", "var", "class", "interface", "type", "import", "as",
"abstract", "closed", "override", "static", "extern", "open", "private", "protected", "abstract", "closed", "override",
"if", "else", "for", "while", "return", "true", "false", "null", "if", "else", "for", "while", "return", "true", "false", "null",
"when", "in", "is", "break", "continue", "try", "catch", "finally", "when", "in", "is", "break", "continue", "try", "catch", "finally",
"get", "set", "object", "enum", "init", "by", "property", "constructor" "get", "set"
) )
override fun start(buffer: CharSequence, startOffset: Int, endOffset: Int, initialState: Int) { override fun start(buffer: CharSequence, startOffset: Int, endOffset: Int, initialState: Int) {
@@ -101,9 +101,8 @@ class LyngLexer : LexerBase() {
return return
} }
// String "..." or '...' with simple escape handling // String "..." with simple escape handling
if (ch == '"' || ch == '\'') { if (ch == '"') {
val quote = ch
i++ i++
while (i < endOffset) { while (i < endOffset) {
val c = buffer[i] val c = buffer[i]
@@ -111,7 +110,7 @@ class LyngLexer : LexerBase() {
i += 2 i += 2
continue continue
} }
if (c == quote) { i++; break } if (c == '"') { i++; break }
i++ i++
} }
myTokenEnd = i myTokenEnd = i
@@ -134,24 +133,10 @@ class LyngLexer : LexerBase() {
return return
} }
// Labels / Annotations: @label or label@ // Identifier / keyword
if (ch == '@') {
i++
while (i < endOffset && (buffer[i].isIdentifierPart())) i++
myTokenEnd = i
myTokenType = LyngTokenTypes.LABEL
return
}
if (ch.isIdentifierStart()) { if (ch.isIdentifierStart()) {
i++ i++
while (i < endOffset && buffer[i].isIdentifierPart()) i++ while (i < endOffset && buffer[i].isIdentifierPart()) i++
if (i < endOffset && buffer[i] == '@') {
i++
myTokenEnd = i
myTokenType = LyngTokenTypes.LABEL
return
}
myTokenEnd = i myTokenEnd = i
val text = buffer.subSequence(myTokenStart, myTokenEnd).toString() val text = buffer.subSequence(myTokenStart, myTokenEnd).toString()
myTokenType = if (text in keywords) LyngTokenTypes.KEYWORD else LyngTokenTypes.IDENTIFIER myTokenType = if (text in keywords) LyngTokenTypes.KEYWORD else LyngTokenTypes.IDENTIFIER
@@ -175,5 +160,5 @@ class LyngLexer : LexerBase() {
private fun Char.isDigit(): Boolean = this in '0'..'9' private fun Char.isDigit(): Boolean = this in '0'..'9'
private fun Char.isIdentifierStart(): Boolean = this == '_' || this.isLetter() private fun Char.isIdentifierStart(): Boolean = this == '_' || this.isLetter()
private fun Char.isIdentifierPart(): Boolean = this.isIdentifierStart() || this.isDigit() private fun Char.isIdentifierPart(): Boolean = this.isIdentifierStart() || this.isDigit()
private fun isPunct(c: Char): Boolean = c in setOf('(', ')', '{', '}', '[', ']', '.', ',', ';', ':', '+', '-', '*', '/', '%', '=', '<', '>', '!', '?', '&', '|', '^', '~', '@') private fun isPunct(c: Char): Boolean = c in setOf('(', ')', '{', '}', '[', ']', '.', ',', ';', ':', '+', '-', '*', '/', '%', '=', '<', '>', '!', '?', '&', '|', '^', '~')
} }
@@ -33,7 +33,6 @@ class LyngSyntaxHighlighter : SyntaxHighlighter {
LyngTokenTypes.BLOCK_COMMENT -> pack(LyngHighlighterColors.BLOCK_COMMENT) LyngTokenTypes.BLOCK_COMMENT -> pack(LyngHighlighterColors.BLOCK_COMMENT)
LyngTokenTypes.PUNCT -> pack(LyngHighlighterColors.PUNCT) LyngTokenTypes.PUNCT -> pack(LyngHighlighterColors.PUNCT)
LyngTokenTypes.IDENTIFIER -> pack(LyngHighlighterColors.IDENTIFIER) LyngTokenTypes.IDENTIFIER -> pack(LyngHighlighterColors.IDENTIFIER)
LyngTokenTypes.LABEL -> pack(LyngHighlighterColors.LABEL)
else -> emptyArray() else -> emptyArray()
} }
@@ -29,7 +29,6 @@ object LyngTokenTypes {
val NUMBER = LyngTokenType("NUMBER") val NUMBER = LyngTokenType("NUMBER")
val KEYWORD = LyngTokenType("KEYWORD") val KEYWORD = LyngTokenType("KEYWORD")
val IDENTIFIER = LyngTokenType("IDENTIFIER") val IDENTIFIER = LyngTokenType("IDENTIFIER")
val LABEL = LyngTokenType("LABEL")
val PUNCT = LyngTokenType("PUNCT") val PUNCT = LyngTokenType("PUNCT")
val BAD_CHAR = LyngTokenType("BAD_CHAR") val BAD_CHAR = LyngTokenType("BAD_CHAR")
} }
@@ -63,10 +63,6 @@ class LyngPsiReference(element: PsiElement) : PsiPolyVariantReferenceBase<PsiEle
is MiniMemberFunDecl -> "Function" is MiniMemberFunDecl -> "Function"
is MiniMemberValDecl -> if (member.mutable) "Variable" else "Value" is MiniMemberValDecl -> if (member.mutable) "Variable" else "Value"
is MiniInitDecl -> "Initializer" is MiniInitDecl -> "Initializer"
is MiniFunDecl -> "Function"
is MiniValDecl -> if (member.mutable) "Variable" else "Value"
is MiniClassDecl -> "Class"
is MiniEnumDecl -> "Enum"
} }
results.add(PsiElementResolveResult(LyngDeclarationElement(it, member.name, kind))) results.add(PsiElementResolveResult(LyngDeclarationElement(it, member.name, kind)))
} }
@@ -1,5 +1,5 @@
/* /*
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com * Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -17,6 +17,9 @@
package net.sergeych.lyng.idea.spell package net.sergeych.lyng.idea.spell
// Avoid Tokenizers helper to keep compatibility; implement our own tokenizers // Avoid Tokenizers helper to keep compatibility; implement our own tokenizers
import com.intellij.ide.plugins.PluginManagerCore
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.extensions.PluginId
import com.intellij.openapi.util.TextRange import com.intellij.openapi.util.TextRange
import com.intellij.psi.PsiElement import com.intellij.psi.PsiElement
import com.intellij.spellchecker.inspections.PlainTextSplitter import com.intellij.spellchecker.inspections.PlainTextSplitter
@@ -35,21 +38,52 @@ import net.sergeych.lyng.idea.settings.LyngFormatterSettings
*/ */
class LyngSpellcheckingStrategy : SpellcheckingStrategy() { class LyngSpellcheckingStrategy : SpellcheckingStrategy() {
private val log = Logger.getInstance(LyngSpellcheckingStrategy::class.java)
@Volatile private var loggedOnce = false
private fun grazieInstalled(): Boolean {
// Support both historical and bundled IDs
return PluginManagerCore.isPluginInstalled(PluginId.getId("com.intellij.grazie")) ||
PluginManagerCore.isPluginInstalled(PluginId.getId("tanvd.grazi"))
}
private fun grazieApiAvailable(): Boolean = try {
// If this class is absent (as in IC-243), third-party plugins can't run Grazie programmatically
Class.forName("com.intellij.grazie.grammar.GrammarChecker")
true
} catch (_: Throwable) { false }
override fun getTokenizer(element: PsiElement): Tokenizer<*> { override fun getTokenizer(element: PsiElement): Tokenizer<*> {
if (element is com.intellij.psi.PsiFile) return EMPTY_TOKENIZER val hasGrazie = grazieInstalled()
val hasGrazieApi = grazieApiAvailable()
val settings = LyngFormatterSettings.getInstance(element.project) val settings = LyngFormatterSettings.getInstance(element.project)
val et = element.node?.elementType if (!loggedOnce) {
loggedOnce = true
log.info("LyngSpellcheckingStrategy activated: hasGrazie=$hasGrazie, grazieApi=$hasGrazieApi, preferGrazieForCommentsAndLiterals=${settings.preferGrazieForCommentsAndLiterals}, spellCheckStringLiterals=${settings.spellCheckStringLiterals}, grazieChecksIdentifiers=${settings.grazieChecksIdentifiers}")
}
if (et == net.sergeych.lyng.idea.highlight.LyngTokenTypes.IDENTIFIER || et == net.sergeych.lyng.idea.highlight.LyngTokenTypes.LABEL) { val file = element.containingFile ?: return EMPTY_TOKENIZER
return IDENTIFIER_TOKENIZER val index = LyngSpellIndex.getUpToDate(file) ?: run {
} // Suspend legacy spellcheck until MiniAst-based index is ready
if (et == net.sergeych.lyng.idea.highlight.LyngTokenTypes.LINE_COMMENT || et == net.sergeych.lyng.idea.highlight.LyngTokenTypes.BLOCK_COMMENT) { return EMPTY_TOKENIZER
return COMMENT_TEXT_TOKENIZER
}
if (et == net.sergeych.lyng.idea.highlight.LyngTokenTypes.STRING && settings.spellCheckStringLiterals) {
return STRING_WITH_PRINTF_EXCLUDES
} }
val elRange = element.textRange ?: return EMPTY_TOKENIZER
fun overlaps(list: List<TextRange>) = list.any { it.intersects(elRange) }
// Decide responsibility per settings
// If Grazie is present but its public API is not available (IC-243), do NOT delegate to it.
val preferGrazie = hasGrazie && hasGrazieApi && settings.preferGrazieForCommentsAndLiterals
val grazieIds = hasGrazie && hasGrazieApi && settings.grazieChecksIdentifiers
// Identifiers: only if range is within identifiers index and not delegated to Grazie
if (overlaps(index.identifiers) && !grazieIds) return IDENTIFIER_TOKENIZER
// Comments: only if not delegated to Grazie and overlapping indexed comments
if (!preferGrazie && overlaps(index.comments)) return COMMENT_TEXT_TOKENIZER
// Strings: only if not delegated to Grazie, literals checking enabled, and overlapping indexed strings
if (!preferGrazie && settings.spellCheckStringLiterals && overlaps(index.strings)) return STRING_WITH_PRINTF_EXCLUDES
return EMPTY_TOKENIZER return EMPTY_TOKENIZER
} }
@@ -59,7 +93,7 @@ class LyngSpellcheckingStrategy : SpellcheckingStrategy() {
} }
private object IDENTIFIER_TOKENIZER : Tokenizer<PsiElement>() { private object IDENTIFIER_TOKENIZER : Tokenizer<PsiElement>() {
private val splitter = com.intellij.spellchecker.inspections.IdentifierSplitter.getInstance() private val splitter = PlainTextSplitter.getInstance()
override fun tokenize(element: PsiElement, consumer: TokenConsumer) { override fun tokenize(element: PsiElement, consumer: TokenConsumer) {
val text = element.text val text = element.text
if (text.isNullOrEmpty()) return if (text.isNullOrEmpty()) return
@@ -1,20 +1,3 @@
/*
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
/* /*
* Ensure external/bundled docs are registered in BuiltinDocRegistry * Ensure external/bundled docs are registered in BuiltinDocRegistry
* so completion/quickdoc can resolve things like lyng.io.fs.Path. * so completion/quickdoc can resolve things like lyng.io.fs.Path.
@@ -23,7 +6,6 @@ package net.sergeych.lyng.idea.util
import com.intellij.openapi.diagnostic.Logger import com.intellij.openapi.diagnostic.Logger
import net.sergeych.lyng.idea.docs.FsDocsFallback import net.sergeych.lyng.idea.docs.FsDocsFallback
import net.sergeych.lyng.idea.docs.ProcessDocsFallback
object DocsBootstrap { object DocsBootstrap {
private val log = Logger.getInstance(DocsBootstrap::class.java) private val log = Logger.getInstance(DocsBootstrap::class.java)
@@ -38,32 +20,20 @@ object DocsBootstrap {
} }
} }
private fun tryLoadExternal(): Boolean { private fun tryLoadExternal(): Boolean = try {
var anyLoaded = false
try {
val cls = Class.forName("net.sergeych.lyngio.docs.FsBuiltinDocs") val cls = Class.forName("net.sergeych.lyngio.docs.FsBuiltinDocs")
val m = cls.getMethod("ensure") val m = cls.getMethod("ensure")
m.invoke(null) m.invoke(null)
log.info("[LYNG_DEBUG] DocsBootstrap: external docs loaded: net.sergeych.lyngio.docs.FsBuiltinDocs.ensure() OK") log.info("[LYNG_DEBUG] DocsBootstrap: external docs loaded: net.sergeych.lyngio.docs.FsBuiltinDocs.ensure() OK")
anyLoaded = true true
} catch (_: Throwable) {} } catch (_: Throwable) {
false
try {
val cls = Class.forName("net.sergeych.lyngio.docs.ProcessBuiltinDocs")
val m = cls.getMethod("ensure")
m.invoke(null)
log.info("[LYNG_DEBUG] DocsBootstrap: external docs loaded: net.sergeych.lyngio.docs.ProcessBuiltinDocs.ensure() OK")
anyLoaded = true
} catch (_: Throwable) {}
return anyLoaded
} }
private fun trySeedFallback(): Boolean = try { private fun trySeedFallback(): Boolean = try {
val seededFs = FsDocsFallback.ensureOnce() val seeded = FsDocsFallback.ensureOnce()
val seededProcess = ProcessDocsFallback.ensureOnce()
val seeded = seededFs || seededProcess
if (seeded) { if (seeded) {
log.info("[LYNG_DEBUG] DocsBootstrap: external docs not found; seeded plugin fallback for lyng.io.fs/process") log.info("[LYNG_DEBUG] DocsBootstrap: external docs not found; seeded plugin fallback for lyng.io.fs")
} else { } else {
log.info("[LYNG_DEBUG] DocsBootstrap: external docs not found; no fallback seeded") log.info("[LYNG_DEBUG] DocsBootstrap: external docs not found; no fallback seeded")
} }
@@ -17,10 +17,8 @@
package net.sergeych.lyng.idea.util package net.sergeych.lyng.idea.util
import com.intellij.openapi.application.runReadAction
import com.intellij.openapi.util.Key import com.intellij.openapi.util.Key
import com.intellij.psi.PsiFile import com.intellij.psi.PsiFile
import com.intellij.psi.PsiManager
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import net.sergeych.lyng.Compiler import net.sergeych.lyng.Compiler
import net.sergeych.lyng.Source import net.sergeych.lyng.Source
@@ -34,89 +32,43 @@ object LyngAstManager {
private val BINDING_KEY = Key.create<BindingSnapshot>("lyng.binding.cache") private val BINDING_KEY = Key.create<BindingSnapshot>("lyng.binding.cache")
private val STAMP_KEY = Key.create<Long>("lyng.mini.cache.stamp") private val STAMP_KEY = Key.create<Long>("lyng.mini.cache.stamp")
fun getMiniAst(file: PsiFile): MiniScript? = runReadAction { fun getMiniAst(file: PsiFile): MiniScript? {
val vFile = file.virtualFile ?: return@runReadAction null val doc = file.viewProvider.document ?: return null
val combinedStamp = getCombinedStamp(file) val stamp = doc.modificationStamp
val prevStamp = file.getUserData(STAMP_KEY) val prevStamp = file.getUserData(STAMP_KEY)
val cached = file.getUserData(MINI_KEY) val cached = file.getUserData(MINI_KEY)
if (cached != null && prevStamp != null && prevStamp == combinedStamp) return@runReadAction cached if (cached != null && prevStamp != null && prevStamp == stamp) return cached
val text = file.viewProvider.contents.toString() val text = doc.text
val sink = MiniAstBuilder() val sink = MiniAstBuilder()
val built = try { val built = try {
val provider = IdeLenientImportProvider.create() val provider = IdeLenientImportProvider.create()
val src = Source(file.name, text) val src = Source(file.name, text)
runBlocking { Compiler.compileWithMini(src, provider, sink) } runBlocking { Compiler.compileWithMini(src, provider, sink) }
val script = sink.build() sink.build()
if (script != null && !file.name.endsWith(".lyng.d")) {
val dFiles = collectDeclarationFiles(file)
for (df in dFiles) {
val scriptD = getMiniAst(df)
if (scriptD != null) {
script.declarations.addAll(scriptD.declarations)
script.imports.addAll(scriptD.imports)
}
}
}
script
} catch (_: Throwable) { } catch (_: Throwable) {
sink.build() sink.build()
} }
if (built != null) { if (built != null) {
file.putUserData(MINI_KEY, built) file.putUserData(MINI_KEY, built)
file.putUserData(STAMP_KEY, combinedStamp) file.putUserData(STAMP_KEY, stamp)
// Invalidate binding too // Invalidate binding too
file.putUserData(BINDING_KEY, null) file.putUserData(BINDING_KEY, null)
} }
built return built
}
fun getCombinedStamp(file: PsiFile): Long = runReadAction {
var combinedStamp = file.viewProvider.modificationStamp
if (!file.name.endsWith(".lyng.d")) {
collectDeclarationFiles(file).forEach { df ->
combinedStamp += df.viewProvider.modificationStamp
}
}
combinedStamp
}
private fun collectDeclarationFiles(file: PsiFile): List<PsiFile> = runReadAction {
val psiManager = PsiManager.getInstance(file.project)
var current = file.virtualFile?.parent
val seen = mutableSetOf<String>()
val result = mutableListOf<PsiFile>()
while (current != null) {
for (child in current.children) {
if (child.name.endsWith(".lyng.d") && child != file.virtualFile && seen.add(child.path)) {
val psiD = psiManager.findFile(child) ?: continue
result.add(psiD)
}
}
current = current.parent
}
result
}
fun getBinding(file: PsiFile): BindingSnapshot? = runReadAction {
val vFile = file.virtualFile ?: return@runReadAction null
var combinedStamp = file.viewProvider.modificationStamp
val dFiles = if (!file.name.endsWith(".lyng.d")) collectDeclarationFiles(file) else emptyList()
for (df in dFiles) {
combinedStamp += df.viewProvider.modificationStamp
} }
fun getBinding(file: PsiFile): BindingSnapshot? {
val doc = file.viewProvider.document ?: return null
val stamp = doc.modificationStamp
val prevStamp = file.getUserData(STAMP_KEY) val prevStamp = file.getUserData(STAMP_KEY)
val cached = file.getUserData(BINDING_KEY) val cached = file.getUserData(BINDING_KEY)
if (cached != null && prevStamp != null && prevStamp == combinedStamp) return@runReadAction cached if (cached != null && prevStamp != null && prevStamp == stamp) return cached
val mini = getMiniAst(file) ?: return@runReadAction null val mini = getMiniAst(file) ?: return null
val text = file.viewProvider.contents.toString() val text = doc.text
val binding = try { val binding = try {
Binder.bind(text, mini) Binder.bind(text, mini)
} catch (_: Throwable) { } catch (_: Throwable) {
@@ -125,9 +77,8 @@ object LyngAstManager {
if (binding != null) { if (binding != null) {
file.putUserData(BINDING_KEY, binding) file.putUserData(BINDING_KEY, binding)
// stamp is already set by getMiniAst or we set it here if getMiniAst was cached // stamp is already set by getMiniAst
file.putUserData(STAMP_KEY, combinedStamp)
} }
binding return binding
} }
} }
@@ -16,10 +16,10 @@
--> -->
<idea-plugin> <idea-plugin>
<!-- Open-ended compatibility: 2024.1+ (build 241 and newer) --> <!-- Open-ended compatibility: 2024.3+ (build 243 and newer) -->
<idea-version since-build="241"/> <idea-version since-build="243"/>
<id>net.sergeych.lyng.idea</id> <id>net.sergeych.lyng.idea</id>
<name>Lyng</name> <name>Lyng Language Support</name>
<vendor email="real.sergeych@gmail.com">Sergey Chernov</vendor> <vendor email="real.sergeych@gmail.com">Sergey Chernov</vendor>
<description> <description>
@@ -43,7 +43,6 @@
<extensions defaultExtensionNs="com.intellij"> <extensions defaultExtensionNs="com.intellij">
<!-- Language and file type --> <!-- Language and file type -->
<fileType implementationClass="net.sergeych.lyng.idea.LyngFileType" name="Lyng" extensions="lyng" fieldName="INSTANCE" language="Lyng"/> <fileType implementationClass="net.sergeych.lyng.idea.LyngFileType" name="Lyng" extensions="lyng" fieldName="INSTANCE" language="Lyng"/>
<fileTypeFactory implementation="net.sergeych.lyng.idea.LyngFileTypeFactory"/>
<!-- Minimal parser/PSI to fully wire editor services for the language --> <!-- Minimal parser/PSI to fully wire editor services for the language -->
<lang.parserDefinition language="Lyng" implementationClass="net.sergeych.lyng.idea.psi.LyngParserDefinition"/> <lang.parserDefinition language="Lyng" implementationClass="net.sergeych.lyng.idea.psi.LyngParserDefinition"/>
@@ -1,20 +1,3 @@
/*
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package net.sergeych.lyng.idea.completion package net.sergeych.lyng.idea.completion
import com.intellij.testFramework.fixtures.BasePlatformTestCase import com.intellij.testFramework.fixtures.BasePlatformTestCase
@@ -135,41 +118,4 @@ class LyngCompletionMemberTest : BasePlatformTestCase() {
// Heuristic: we expect more than a couple of items (not just size/toList) // Heuristic: we expect more than a couple of items (not just size/toList)
assertTrue("Too few member suggestions after list literal: $items", items.size >= 3) assertTrue("Too few member suggestions after list literal: $items", items.size >= 3)
} }
fun test_ProcessModule_Completion() {
val code = """
import lyng.io.process
Process.<caret>
""".trimIndent()
val imported = listOf("lyng.io.process")
ensureDocs(imported)
val items = complete(code)
assertTrue("Should contain 'execute'", items.contains("execute"))
assertTrue("Should contain 'shell'", items.contains("shell"))
}
fun test_RunningProcess_Completion() {
val code = """
import lyng.io.process
val p = Process.shell("ls")
p.<caret>
""".trimIndent()
val imported = listOf("lyng.io.process")
ensureDocs(imported)
val items = complete(code)
assertTrue("Should contain 'stdout'", items.contains("stdout"))
assertTrue("Should contain 'waitFor'", items.contains("waitFor"))
assertTrue("Should contain 'signal'", items.contains("signal"))
}
fun test_RegistryDirect() {
DocsBootstrap.ensure()
val docs = BuiltinDocRegistry.docsForModule("lyng.io.process")
assertTrue("Docs for lyng.io.process should not be empty", docs.isNotEmpty())
val processClass = docs.filterIsInstance<MiniClassDecl>().firstOrNull { it.name == "Process" }
assertNotNull("Should contain Process class", processClass)
assertTrue("Process should have members", processClass!!.members.isNotEmpty())
}
} }
+14 -14
View File
@@ -1,5 +1,5 @@
/* /*
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com * Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@
*/ */
import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi
import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
import org.jetbrains.kotlin.gradle.dsl.JvmTarget import org.jetbrains.kotlin.gradle.dsl.JvmTarget
plugins { plugins {
@@ -32,7 +33,6 @@ group = "net.sergeych"
version = "0.0.1-SNAPSHOT" version = "0.0.1-SNAPSHOT"
kotlin { kotlin {
jvmToolchain(17)
jvm() jvm()
androidTarget { androidTarget {
publishLibraryVariants("release") publishLibraryVariants("release")
@@ -53,11 +53,11 @@ kotlin {
browser() browser()
nodejs() nodejs()
} }
// @OptIn(ExperimentalWasmDsl::class) @OptIn(ExperimentalWasmDsl::class)
// wasmJs() { wasmJs() {
// browser() browser()
// nodejs() nodejs()
// } }
// Keep expect/actual warning suppressed consistently with other modules // Keep expect/actual warning suppressed consistently with other modules
targets.configureEach { targets.configureEach {
@@ -94,13 +94,13 @@ kotlin {
implementation("com.squareup.okio:okio-nodefilesystem:${libs.versions.okioVersion.get()}") implementation("com.squareup.okio:okio-nodefilesystem:${libs.versions.okioVersion.get()}")
} }
} }
// // For Wasm we use in-memory VFS for now // For Wasm we use in-memory VFS for now
// val wasmJsMain by getting { val wasmJsMain by getting {
// dependencies { dependencies {
// api(libs.okio) api(libs.okio)
// implementation(libs.okio.fakefilesystem) implementation(libs.okio.fakefilesystem)
// } }
// } }
} }
} }
@@ -1,33 +0,0 @@
/*
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package net.sergeych.lyngio.process
actual fun getPlatformDetails(): PlatformDetails {
return PlatformDetails(
name = "Android",
version = android.os.Build.VERSION.RELEASE,
arch = android.os.Build.SUPPORTED_ABIS.firstOrNull() ?: "unknown",
kernelVersion = System.getProperty("os.version")
)
}
actual fun isProcessSupported(): Boolean = false
actual fun getSystemProcessRunner(): LyngProcessRunner {
throw UnsupportedOperationException("Processes are not supported on Android yet")
}
@@ -1,234 +0,0 @@
/*
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package net.sergeych.lyng.io.process
import kotlinx.coroutines.flow.Flow
import net.sergeych.lyng.ModuleScope
import net.sergeych.lyng.Scope
import net.sergeych.lyng.miniast.*
import net.sergeych.lyng.obj.*
import net.sergeych.lyng.pacman.ImportManager
import net.sergeych.lyng.statement
import net.sergeych.lyngio.process.*
import net.sergeych.lyngio.process.security.ProcessAccessDeniedException
import net.sergeych.lyngio.process.security.ProcessAccessPolicy
/**
* Install Lyng module `lyng.io.process` into the given scope's ImportManager.
*/
fun createProcessModule(policy: ProcessAccessPolicy, scope: Scope): Boolean =
createProcessModule(policy, scope.importManager)
/** Same as [createProcessModule] but with explicit [ImportManager]. */
fun createProcessModule(policy: ProcessAccessPolicy, manager: ImportManager): Boolean {
val name = "lyng.io.process"
if (manager.packageNames.contains(name)) return false
manager.addPackage(name) { module ->
buildProcessModule(module, policy)
}
return true
}
private suspend fun buildProcessModule(module: ModuleScope, policy: ProcessAccessPolicy) {
val runner = try {
SecuredLyngProcessRunner(getSystemProcessRunner(), policy)
} catch (e: Exception) {
null
}
val runningProcessType = object : ObjClass("RunningProcess") {}
runningProcessType.apply {
addFnDoc(
name = "stdout",
doc = "Get standard output stream as a Flow of lines.",
returns = type("lyng.Flow"),
moduleName = module.packageName
) {
val self = thisAs<ObjRunningProcess>()
self.process.stdout.toLyngFlow(this)
}
addFnDoc(
name = "stderr",
doc = "Get standard error stream as a Flow of lines.",
returns = type("lyng.Flow"),
moduleName = module.packageName
) {
val self = thisAs<ObjRunningProcess>()
self.process.stderr.toLyngFlow(this)
}
addFnDoc(
name = "signal",
doc = "Send a signal to the process (e.g. 'SIGINT', 'SIGTERM', 'SIGKILL').",
params = listOf(ParamDoc("signal", type("lyng.String"))),
moduleName = module.packageName
) {
processGuard {
val sigStr = requireOnlyArg<ObjString>().value.uppercase()
val sig = try {
ProcessSignal.valueOf(sigStr)
} catch (e: Exception) {
try {
ProcessSignal.valueOf("SIG$sigStr")
} catch (e2: Exception) {
raiseIllegalArgument("Unknown signal: $sigStr")
}
}
thisAs<ObjRunningProcess>().process.sendSignal(sig)
ObjVoid
}
}
addFnDoc(
name = "waitFor",
doc = "Wait for the process to exit and return its exit code.",
returns = type("lyng.Int"),
moduleName = module.packageName
) {
processGuard {
thisAs<ObjRunningProcess>().process.waitFor().toObj()
}
}
addFnDoc(
name = "destroy",
doc = "Forcefully terminate the process.",
moduleName = module.packageName
) {
thisAs<ObjRunningProcess>().process.destroy()
ObjVoid
}
}
val processType = object : ObjClass("Process") {}
processType.apply {
addClassFnDoc(
name = "execute",
doc = "Execute a process with arguments.",
params = listOf(ParamDoc("executable", type("lyng.String")), ParamDoc("args", type("lyng.List"))),
returns = type("RunningProcess"),
moduleName = module.packageName
) {
if (runner == null) raiseError("Processes are not supported on this platform")
processGuard {
val executable = requiredArg<ObjString>(0).value
val args = requiredArg<ObjList>(1).list.map { it.toString() }
val lp = runner.execute(executable, args)
ObjRunningProcess(runningProcessType, lp)
}
}
addClassFnDoc(
name = "shell",
doc = "Execute a command via system shell.",
params = listOf(ParamDoc("command", type("lyng.String"))),
returns = type("RunningProcess"),
moduleName = module.packageName
) {
if (runner == null) raiseError("Processes are not supported on this platform")
processGuard {
val command = requireOnlyArg<ObjString>().value
val lp = runner.shell(command)
ObjRunningProcess(runningProcessType, lp)
}
}
}
val platformType = object : ObjClass("Platform") {}
platformType.apply {
addClassFnDoc(
name = "details",
doc = "Get platform core details.",
returns = type("lyng.Map"),
moduleName = module.packageName
) {
val d = getPlatformDetails()
ObjMap(mutableMapOf(
ObjString("name") to ObjString(d.name),
ObjString("version") to ObjString(d.version),
ObjString("arch") to ObjString(d.arch),
ObjString("kernelVersion") to (d.kernelVersion?.toObj() ?: ObjNull)
))
}
addClassFnDoc(
name = "isSupported",
doc = "Check if processes are supported on this platform.",
returns = type("lyng.Bool"),
moduleName = module.packageName
) {
isProcessSupported().toObj()
}
}
module.addConstDoc(
name = "Process",
value = processType,
doc = "Process execution and control.",
type = type("Process"),
moduleName = module.packageName
)
module.addConstDoc(
name = "Platform",
value = platformType,
doc = "Platform information.",
type = type("Platform"),
moduleName = module.packageName
)
module.addConstDoc(
name = "RunningProcess",
value = runningProcessType,
doc = "Handle to a running process.",
type = type("RunningProcess"),
moduleName = module.packageName
)
}
class ObjRunningProcess(
override val objClass: ObjClass,
val process: LyngProcess
) : Obj() {
override fun toString(): String = "RunningProcess($process)"
}
private suspend inline fun Scope.processGuard(crossinline block: suspend () -> Obj): Obj {
return try {
block()
} catch (e: ProcessAccessDeniedException) {
raiseError(ObjIllegalOperationException(this, e.reasonDetail ?: "process access denied"))
} catch (e: Exception) {
raiseError(ObjIllegalOperationException(this, e.message ?: "process error"))
}
}
private fun Flow<String>.toLyngFlow(flowScope: Scope): ObjFlow {
val producer = statement {
val builder = (this as? net.sergeych.lyng.ClosureScope)?.callScope?.thisObj as? ObjFlowBuilder
?: this.thisObj as? ObjFlowBuilder
this@toLyngFlow.collect {
try {
builder?.output?.send(ObjString(it))
} catch (e: Exception) {
// Channel closed or other error, stop collecting
return@collect
}
}
ObjVoid
}
return ObjFlow(producer, flowScope)
}
@@ -1,20 +1,3 @@
/*
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
/* /*
* Filesystem module builtin docs registration, located in lyngio so core library * Filesystem module builtin docs registration, located in lyngio so core library
* does not depend on external packages. The IDEA plugin (and any other tooling) * does not depend on external packages. The IDEA plugin (and any other tooling)
@@ -39,176 +22,45 @@ object FsBuiltinDocs {
name = "Path", name = "Path",
doc = "Filesystem path class. Construct with a string: `Path(\"/tmp\")`." doc = "Filesystem path class. Construct with a string: `Path(\"/tmp\")`."
) { ) {
method( // Common instance methods (subset sufficient for Quick Docs)
name = "name",
doc = "Base name of the path (last segment).",
returns = type("lyng.String")
)
method(
name = "parent",
doc = "Parent directory as a Path or null if none.",
returns = type("Path", nullable = true)
)
method(
name = "segments",
doc = "List of path segments.",
returns = TypeGenericDoc(type("lyng.List"), listOf(type("lyng.String")))
)
method( method(
name = "exists", name = "exists",
doc = "Check whether this path exists on the filesystem.", doc = "Whether the path exists on the filesystem.",
returns = type("lyng.Bool") returns = type("lyng.Bool")
) )
method( method(
name = "isFile", name = "isFile",
doc = "True if this path is a regular file (based on cached metadata).", doc = "Whether the path exists and is a file.",
returns = type("lyng.Bool") returns = type("lyng.Bool")
) )
method( method(
name = "isDirectory", name = "isDir",
doc = "True if this path is a directory (based on cached metadata).", doc = "Whether the path exists and is a directory.",
returns = type("lyng.Bool") returns = type("lyng.Bool")
) )
method(
name = "size",
doc = "File size in bytes, or null when unavailable.",
returns = type("lyng.Int", nullable = true)
)
method(
name = "createdAt",
doc = "Creation time as `Instant`, or null when unavailable.",
returns = type("lyng.Instant", nullable = true)
)
method(
name = "createdAtMillis",
doc = "Creation time in milliseconds since epoch, or null when unavailable.",
returns = type("lyng.Int", nullable = true)
)
method(
name = "modifiedAt",
doc = "Last modification time as `Instant`, or null when unavailable.",
returns = type("lyng.Instant", nullable = true)
)
method(
name = "modifiedAtMillis",
doc = "Last modification time in milliseconds since epoch, or null when unavailable.",
returns = type("lyng.Int", nullable = true)
)
method(
name = "list",
doc = "List directory entries as `Path` objects.",
returns = TypeGenericDoc(type("lyng.List"), listOf(type("Path")))
)
method(
name = "readBytes",
doc = "Read the entire file into a binary buffer.",
returns = type("lyng.Buffer")
)
method(
name = "writeBytes",
doc = "Write a binary buffer to the file, replacing content.",
params = listOf(ParamDoc("bytes", type("lyng.Buffer")))
)
method(
name = "appendBytes",
doc = "Append a binary buffer to the end of the file.",
params = listOf(ParamDoc("bytes", type("lyng.Buffer")))
)
method( method(
name = "readUtf8", name = "readUtf8",
doc = "Read the entire file as a UTF-8 string.", doc = "Read the entire file as UTF-8 string.",
returns = type("lyng.String") returns = type("lyng.String")
) )
method( method(
name = "writeUtf8", name = "writeUtf8",
doc = "Write a UTF-8 string to the file, replacing content.", doc = "Write UTF-8 string to the file (overwrite).",
params = listOf(ParamDoc("text", type("lyng.String"))) params = listOf(ParamDoc("text", type("lyng.String")))
) )
method( method(
name = "appendUtf8", name = "bytes",
doc = "Append UTF-8 text to the end of the file.", doc = "Iterate file content as `Buffer` chunks.",
params = listOf(ParamDoc("text", type("lyng.String")))
)
method(
name = "metadata",
doc = "Fetch cached metadata as a map of fields: `isFile`, `isDirectory`, `size`, `createdAtMillis`, `modifiedAtMillis`, `isSymlink`.",
returns = TypeGenericDoc(type("lyng.Map"), listOf(type("lyng.String"), type("lyng.Any")))
)
method(
name = "mkdirs",
doc = "Create directories (like `mkdir -p`). If `mustCreate` is true and the path already exists, the call fails.",
params = listOf(ParamDoc("mustCreate", type("lyng.Bool")))
)
method(
name = "move",
doc = "Move this path to a new location. `to` may be a `Path` or `String`.",
params = listOf(ParamDoc("to"), ParamDoc("overwrite", type("lyng.Bool")))
)
method(
name = "delete",
doc = "Delete this path. `recursively=true` removes directories with their contents.",
params = listOf(ParamDoc("mustExist", type("lyng.Bool")), ParamDoc("recursively", type("lyng.Bool")))
)
method(
name = "copy",
doc = "Copy this path to a new location. `to` may be a `Path` or `String`.",
params = listOf(ParamDoc("to"), ParamDoc("overwrite", type("lyng.Bool")))
)
method(
name = "glob",
doc = "List entries matching a glob pattern (no recursion).",
params = listOf(ParamDoc("pattern", type("lyng.String"))),
returns = TypeGenericDoc(type("lyng.List"), listOf(type("Path")))
)
method(
name = "readChunks",
doc = "Read file in fixed-size chunks as an iterator of `Buffer`.",
params = listOf(ParamDoc("size", type("lyng.Int"))), params = listOf(ParamDoc("size", type("lyng.Int"))),
returns = TypeGenericDoc(type("lyng.Iterator"), listOf(type("lyng.Buffer"))) returns = TypeGenericDoc(type("lyng.Iterator"), listOf(type("lyng.Buffer")))
) )
method(
name = "readUtf8Chunks",
doc = "Read UTF-8 text in fixed-size chunks as an iterator of `String`.",
params = listOf(ParamDoc("size", type("lyng.Int"))),
returns = TypeGenericDoc(type("lyng.Iterator"), listOf(type("lyng.String")))
)
method( method(
name = "lines", name = "lines",
doc = "Iterate lines of the file as `String` values.", doc = "Iterate file as lines of text.",
returns = TypeGenericDoc(type("lyng.Iterator"), listOf(type("lyng.String"))) returns = TypeGenericDoc(type("lyng.Iterator"), listOf(type("lyng.String")))
) )
} }
classDoc(
name = "BytesIterator",
doc = "Iterator over binary chunks."
) {
method("iterator", "Return this iterator instance.", returns = type("BytesIterator"))
method("hasNext", "Whether there is another chunk available.", returns = type("lyng.Bool"))
method("next", "Return the next chunk as a `Buffer`.", returns = type("lyng.Buffer"))
method("cancelIteration", "Stop the iteration early.")
}
classDoc(
name = "StringChunksIterator",
doc = "Iterator over UTF-8 text chunks."
) {
method("iterator", "Return this iterator instance.", returns = type("StringChunksIterator"))
method("hasNext", "Whether there is another chunk available.", returns = type("lyng.Bool"))
method("next", "Return the next UTF-8 chunk as a `String`.", returns = type("lyng.String"))
method("cancelIteration", "Stop the iteration early.")
}
classDoc(
name = "LinesIterator",
doc = "Iterator that yields lines of text."
) {
method("iterator", "Return this iterator instance.", returns = type("LinesIterator"))
method("hasNext", "Whether another line is available.", returns = type("lyng.Bool"))
method("next", "Return the next line as `String`.", returns = type("lyng.String"))
method("cancelIteration", "Stop the iteration early.")
}
// Top-level exported constants // Top-level exported constants
valDoc( valDoc(
name = "Path", name = "Path",
@@ -1,117 +0,0 @@
/*
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package net.sergeych.lyngio.docs
import net.sergeych.lyng.miniast.BuiltinDocRegistry
import net.sergeych.lyng.miniast.ParamDoc
import net.sergeych.lyng.miniast.type
object ProcessBuiltinDocs {
private var registered = false
fun ensure() {
if (registered) return
BuiltinDocRegistry.module("lyng.io.process") {
classDoc(
name = "Process",
doc = "Process execution and control."
) {
method(
name = "execute",
doc = "Execute a process with arguments.",
params = listOf(ParamDoc("executable", type("lyng.String")), ParamDoc("args", type("lyng.List"))),
returns = type("RunningProcess"),
isStatic = true
)
method(
name = "shell",
doc = "Execute a command via system shell.",
params = listOf(ParamDoc("command", type("lyng.String"))),
returns = type("RunningProcess"),
isStatic = true
)
}
classDoc(
name = "Platform",
doc = "Platform information."
) {
method(
name = "details",
doc = "Get platform core details.",
returns = type("lyng.Map"),
isStatic = true
)
method(
name = "isSupported",
doc = "Check if processes are supported on this platform.",
returns = type("lyng.Bool"),
isStatic = true
)
}
classDoc(
name = "RunningProcess",
doc = "Handle to a running process."
) {
method(
name = "stdout",
doc = "Get standard output stream as a Flow of lines.",
returns = type("lyng.Flow")
)
method(
name = "stderr",
doc = "Get standard error stream as a Flow of lines.",
returns = type("lyng.Flow")
)
method(
name = "signal",
doc = "Send a signal to the process (e.g. 'SIGINT', 'SIGTERM', 'SIGKILL').",
params = listOf(ParamDoc("signal", type("lyng.String")))
)
method(
name = "waitFor",
doc = "Wait for the process to exit and return its exit code.",
returns = type("lyng.Int")
)
method(
name = "destroy",
doc = "Forcefully terminate the process."
)
}
// Top-level exported constants
valDoc(
name = "Process",
doc = "Process execution and control.",
type = type("Process")
)
valDoc(
name = "Platform",
doc = "Platform information.",
type = type("Platform")
)
valDoc(
name = "RunningProcess",
doc = "Handle to a running process.",
type = type("RunningProcess")
)
}
registered = true
}
}
@@ -1,93 +0,0 @@
/*
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package net.sergeych.lyngio.process
import kotlinx.coroutines.flow.Flow
import net.sergeych.lyngio.process.security.ProcessAccessOp
import net.sergeych.lyngio.process.security.ProcessAccessPolicy
/**
* Common signals for process control.
*/
enum class ProcessSignal {
SIGINT, SIGTERM, SIGKILL
}
/**
* Multiplatform process representation.
*/
interface LyngProcess {
/**
* Standard output stream as a flow of strings (lines).
*/
val stdout: Flow<String>
/**
* Standard error stream as a flow of strings (lines).
*/
val stderr: Flow<String>
/**
* Send a signal to the process.
* Throws exception if signals are not supported on the platform or for this process.
*/
suspend fun sendSignal(signal: ProcessSignal)
/**
* Wait for the process to exit and return the exit code.
*/
suspend fun waitFor(): Int
/**
* Forcefully terminate the process.
*/
fun destroy()
}
/**
* Interface for running processes.
*/
interface LyngProcessRunner {
/**
* Execute a process with the given executable and arguments.
*/
suspend fun execute(executable: String, args: List<String>): LyngProcess
/**
* Execute a command via the platform's default shell.
*/
suspend fun shell(command: String): LyngProcess
}
/**
* Secured implementation of [LyngProcessRunner] that checks against a [ProcessAccessPolicy].
*/
class SecuredLyngProcessRunner(
private val runner: LyngProcessRunner,
private val policy: ProcessAccessPolicy
) : LyngProcessRunner {
override suspend fun execute(executable: String, args: List<String>): LyngProcess {
policy.require(ProcessAccessOp.Execute(executable, args))
return runner.execute(executable, args)
}
override suspend fun shell(command: String): LyngProcess {
policy.require(ProcessAccessOp.Shell(command))
return runner.shell(command)
}
}
@@ -1,44 +0,0 @@
/*
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package net.sergeych.lyngio.process
/**
* Platform core details.
*/
data class PlatformDetails(
val name: String,
val version: String,
val arch: String,
val kernelVersion: String? = null
)
/**
* Get the current platform core details.
*/
expect fun getPlatformDetails(): PlatformDetails
/**
* Check whether the current platform supports processes and shell execution.
*/
expect fun isProcessSupported(): Boolean
/**
* Get the system default [LyngProcessRunner].
* Throws [UnsupportedOperationException] if processes are not supported on this platform.
*/
expect fun getSystemProcessRunner(): LyngProcessRunner
@@ -1,59 +0,0 @@
/*
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package net.sergeych.lyngio.process.security
import net.sergeych.lyngio.fs.security.AccessContext
import net.sergeych.lyngio.fs.security.AccessDecision
import net.sergeych.lyngio.fs.security.Decision
/**
* Primitive process operations for access control decisions.
*/
sealed interface ProcessAccessOp {
data class Execute(val executable: String, val args: List<String>) : ProcessAccessOp
data class Shell(val command: String) : ProcessAccessOp
}
class ProcessAccessDeniedException(
val op: ProcessAccessOp,
val reasonDetail: String? = null,
) : IllegalStateException("Process access denied for $op" + (reasonDetail?.let { ": $it" } ?: ""))
/**
* Policy interface that decides whether a specific process operation is allowed.
*/
interface ProcessAccessPolicy {
suspend fun check(op: ProcessAccessOp, ctx: AccessContext = AccessContext()): AccessDecision
// Convenience helpers
suspend fun require(op: ProcessAccessOp, ctx: AccessContext = AccessContext()) {
val res = check(op, ctx)
if (!res.isAllowed()) throw ProcessAccessDeniedException(op, res.reason)
}
suspend fun canExecute(executable: String, args: List<String>, ctx: AccessContext = AccessContext()) =
check(ProcessAccessOp.Execute(executable, args), ctx).isAllowed()
suspend fun canShell(command: String, ctx: AccessContext = AccessContext()) =
check(ProcessAccessOp.Shell(command), ctx).isAllowed()
}
object PermitAllProcessAccessPolicy : ProcessAccessPolicy {
override suspend fun check(op: ProcessAccessOp, ctx: AccessContext): AccessDecision =
AccessDecision(Decision.Allow)
}
@@ -1,26 +0,0 @@
/*
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package net.sergeych.lyngio.process
internal actual fun getNativeKernelVersion(): String? = null
internal actual fun isNativeProcessSupported(): Boolean = false
internal actual fun getNativeProcessRunner(): LyngProcessRunner {
throw UnsupportedOperationException("Processes are not supported on iOS")
}
@@ -1,32 +0,0 @@
/*
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package net.sergeych.lyngio.process
actual fun getPlatformDetails(): PlatformDetails {
return PlatformDetails(
name = "JavaScript",
version = "unknown",
arch = "unknown"
)
}
actual fun isProcessSupported(): Boolean = false
actual fun getSystemProcessRunner(): LyngProcessRunner {
throw UnsupportedOperationException("Processes are not supported on JS")
}
@@ -1,107 +0,0 @@
/*
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package net.sergeych.lyngio.process
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.withContext
actual fun getPlatformDetails(): PlatformDetails {
val osName = System.getProperty("os.name")
return PlatformDetails(
name = osName,
version = System.getProperty("os.version"),
arch = System.getProperty("os.arch"),
kernelVersion = if (osName.lowercase().contains("linux")) {
System.getProperty("os.version")
} else null
)
}
actual fun isProcessSupported(): Boolean = true
actual fun getSystemProcessRunner(): LyngProcessRunner = JvmProcessRunner
object JvmProcessRunner : LyngProcessRunner {
override suspend fun execute(executable: String, args: List<String>): LyngProcess {
val process = ProcessBuilder(listOf(executable) + args)
.start()
return JvmLyngProcess(process)
}
override suspend fun shell(command: String): LyngProcess {
val os = System.getProperty("os.name").lowercase()
val shellCmd = if (os.contains("win")) {
listOf("cmd.exe", "/c", command)
} else {
listOf("sh", "-c", command)
}
val process = ProcessBuilder(shellCmd)
.start()
return JvmLyngProcess(process)
}
}
class JvmLyngProcess(private val process: Process) : LyngProcess {
override val stdout: Flow<String> = flow {
val reader = process.inputStream.bufferedReader()
while (true) {
val line = reader.readLine() ?: break
emit(line)
}
}
override val stderr: Flow<String> = flow {
val reader = process.errorStream.bufferedReader()
while (true) {
val line = reader.readLine() ?: break
emit(line)
}
}
override suspend fun sendSignal(signal: ProcessSignal) {
when (signal) {
ProcessSignal.SIGINT -> {
// SIGINT is hard on JVM without native calls or external 'kill'
val os = System.getProperty("os.name").lowercase()
if (os.contains("win")) {
throw UnsupportedOperationException("SIGINT not supported on Windows JVM")
} else {
// Try to use kill -2 <pid>
try {
val pid = process.pid()
Runtime.getRuntime().exec(arrayOf("kill", "-2", pid.toString())).waitFor()
} catch (e: Exception) {
throw UnsupportedOperationException("Failed to send SIGINT: ${e.message}")
}
}
}
ProcessSignal.SIGTERM -> process.destroy()
ProcessSignal.SIGKILL -> process.destroyForcibly()
}
}
override suspend fun waitFor(): Int = withContext(Dispatchers.IO) {
process.waitFor()
}
override fun destroy() {
process.destroy()
}
}
@@ -1,87 +0,0 @@
/*
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package net.sergeych.lyng.io.process
import kotlinx.coroutines.runBlocking
import net.sergeych.lyng.Compiler
import net.sergeych.lyng.Script
import net.sergeych.lyngio.process.security.PermitAllProcessAccessPolicy
import kotlin.test.Test
import kotlin.test.assertTrue
class LyngProcessModuleTest {
@Test
fun testLyngProcess() = runBlocking {
val scope = Script.newScope()
createProcessModule(PermitAllProcessAccessPolicy, scope)
val code = """
import lyng.io.process
var p = Process.execute("echo", ["hello", "lyng"])
var output = []
for (line in p.stdout()) {
output.add(line)
}
p.waitFor()
output
""".trimIndent()
val script = Compiler.compile(code)
val result = script.execute(scope)
assertTrue(result.inspect(scope).contains("hello lyng"))
}
@Test
fun testLyngShell() = runBlocking {
val scope = Script.newScope()
createProcessModule(PermitAllProcessAccessPolicy, scope)
val code = """
import lyng.io.process
var p = Process.shell("echo 'shell lyng'")
var output = ""
for (line in p.stdout()) {
output = output + line
}
p.waitFor()
output
""".trimIndent()
val script = Compiler.compile(code)
val result = script.execute(scope)
assertTrue(result.inspect(scope).contains("shell lyng"))
}
@Test
fun testPlatformDetails() = runBlocking {
val scope = Script.newScope()
createProcessModule(PermitAllProcessAccessPolicy, scope)
val code = """
import lyng.io.process
Platform.details()
""".trimIndent()
val script = Compiler.compile(code)
val result = script.execute(scope)
assertTrue(result.inspect(scope).contains("name"), "Result should contain 'name', but was: ${result.inspect(scope)}")
}
}
@@ -1,57 +0,0 @@
/*
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package net.sergeych.lyngio.process
import kotlinx.coroutines.flow.toList
import kotlinx.coroutines.runBlocking
import net.sergeych.lyngio.process.security.PermitAllProcessAccessPolicy
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue
class JvmProcessTest {
@Test
fun testExecute() = runBlocking {
val runner = getSystemProcessRunner()
val secured = SecuredLyngProcessRunner(runner, PermitAllProcessAccessPolicy)
val process = secured.execute("echo", listOf("hello", "world"))
val output = process.stdout.toList()
assertEquals(listOf("hello world"), output)
assertEquals(0, process.waitFor())
}
@Test
fun testShell() = runBlocking {
val runner = getSystemProcessRunner()
val secured = SecuredLyngProcessRunner(runner, PermitAllProcessAccessPolicy)
val process = secured.shell("echo 'hello shell'")
val output = process.stdout.toList()
assertEquals(listOf("hello shell"), output)
assertEquals(0, process.waitFor())
}
@Test
fun testPlatformDetails() {
val details = getPlatformDetails()
assertTrue(details.name.isNotEmpty())
assertTrue(isProcessSupported())
}
}
@@ -1,35 +0,0 @@
/*
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package net.sergeych.lyngio.process
import kotlinx.cinterop.*
import platform.posix.*
@OptIn(ExperimentalForeignApi::class)
internal actual fun getNativeKernelVersion(): String? {
return memScoped {
val u = alloc<utsname>()
if (uname(u.ptr) == 0) {
u.release.toKString()
} else null
}
}
internal actual fun isNativeProcessSupported(): Boolean = true
internal actual fun getNativeProcessRunner(): LyngProcessRunner = PosixProcessRunner
@@ -1,152 +0,0 @@
/*
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package net.sergeych.lyngio.process
import kotlinx.cinterop.*
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
import platform.posix.*
@OptIn(ExperimentalForeignApi::class)
internal class NativeLyngProcess(
private val pid: pid_t,
private val stdoutFd: Int,
private val stderrFd: Int
) : LyngProcess {
override val stdout: Flow<String> = createPipeFlow(stdoutFd)
override val stderr: Flow<String> = createPipeFlow(stderrFd)
override suspend fun sendSignal(signal: ProcessSignal) {
val sig = when (signal) {
ProcessSignal.SIGINT -> SIGINT
ProcessSignal.SIGTERM -> SIGTERM
ProcessSignal.SIGKILL -> SIGKILL
}
if (kill(pid, sig) != 0) {
throw RuntimeException("Failed to send signal $signal to process $pid: ${strerror(errno)?.toKString()}")
}
}
override suspend fun waitFor(): Int = withContext(Dispatchers.Default) {
memScoped {
val status = alloc<IntVar>()
if (waitpid(pid, status.ptr, 0) == -1) {
throw RuntimeException("Failed to wait for process $pid: ${strerror(errno)?.toKString()}")
}
val s = status.value
if ((s and 0x7f) == 0) (s shr 8) and 0xff else -1
}
}
override fun destroy() {
kill(pid, SIGKILL)
}
}
@OptIn(ExperimentalForeignApi::class)
private fun createPipeFlow(fd: Int): Flow<String> = flow {
val buffer = ByteArray(4096)
val lineBuffer = StringBuilder()
try {
while (true) {
val bytesRead = buffer.usePinned { pinned ->
read(fd, pinned.addressOf(0), buffer.size.toULong())
}
if (bytesRead <= 0L) break
val text = buffer.decodeToString(endIndex = bytesRead.toInt())
lineBuffer.append(text)
var newlineIdx = lineBuffer.indexOf('\n')
while (newlineIdx != -1) {
val line = lineBuffer.substring(0, newlineIdx)
emit(line)
lineBuffer.deleteRange(0, newlineIdx + 1)
newlineIdx = lineBuffer.indexOf('\n')
}
}
if (lineBuffer.isNotEmpty()) {
emit(lineBuffer.toString())
}
} finally {
close(fd)
}
}.flowOn(Dispatchers.Default)
@OptIn(ExperimentalForeignApi::class)
object PosixProcessRunner : LyngProcessRunner {
override suspend fun execute(executable: String, args: List<String>): LyngProcess = withContext(Dispatchers.Default) {
memScoped {
val pipeStdout = allocArray<IntVar>(2)
val pipeStderr = allocArray<IntVar>(2)
if (pipe(pipeStdout) != 0) throw RuntimeException("Failed to create stdout pipe")
if (pipe(pipeStderr) != 0) {
close(pipeStdout[0])
close(pipeStdout[1])
throw RuntimeException("Failed to create stderr pipe")
}
val pid = fork()
if (pid == -1) {
close(pipeStdout[0])
close(pipeStdout[1])
close(pipeStderr[0])
close(pipeStderr[1])
throw RuntimeException("Failed to fork: ${strerror(errno)?.toKString()}")
}
if (pid == 0) {
// Child process
dup2(pipeStdout[1], 1)
dup2(pipeStderr[1], 2)
close(pipeStdout[0])
close(pipeStdout[1])
close(pipeStderr[0])
close(pipeStderr[1])
val argv = allocArray<CPointerVar<ByteVar>>(args.size + 2)
argv[0] = executable.cstr.ptr
for (i in args.indices) {
argv[i + 1] = args[i].cstr.ptr
}
argv[args.size + 1] = null
execvp(executable, argv)
// If we are here, exec failed
_exit(1)
}
// Parent process
close(pipeStdout[1])
close(pipeStderr[1])
NativeLyngProcess(pid, pipeStdout[0], pipeStderr[0])
}
}
override suspend fun shell(command: String): LyngProcess {
return execute("/bin/sh", listOf("-c", command))
}
}
@@ -1,95 +0,0 @@
/*
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package net.sergeych.lyngio.process
import kotlinx.coroutines.flow.toList
import kotlinx.coroutines.runBlocking
import net.sergeych.lyng.Compiler
import net.sergeych.lyng.Script
import net.sergeych.lyng.io.process.createProcessModule
import net.sergeych.lyngio.process.security.PermitAllProcessAccessPolicy
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue
class LinuxProcessTest {
@Test
fun testExecuteEcho() = runBlocking {
val process = PosixProcessRunner.execute("echo", listOf("hello", "native"))
val stdout = process.stdout.toList()
val exitCode = process.waitFor()
assertEquals(0, exitCode)
assertEquals(listOf("hello native"), stdout)
}
@Test
fun testShellCommand() = runBlocking {
val process = PosixProcessRunner.shell("echo 'shell native' && printf 'line2'")
val stdout = process.stdout.toList()
val exitCode = process.waitFor()
assertEquals(0, exitCode)
assertEquals(listOf("shell native", "line2"), stdout)
}
@Test
fun testStderrCapture() = runBlocking {
val process = PosixProcessRunner.shell("echo 'to stdout'; echo 'to stderr' >&2")
val stdout = process.stdout.toList()
val stderr = process.stderr.toList()
process.waitFor()
assertEquals(listOf("to stdout"), stdout)
assertEquals(listOf("to stderr"), stderr)
}
@Test
fun testPlatformDetails() {
val details = getPlatformDetails()
assertEquals("LINUX", details.name)
assertTrue(details.kernelVersion != null)
assertTrue(details.kernelVersion!!.isNotEmpty())
println("Linux Native Details: $details")
}
@Test
fun testLyngModuleNative() = runBlocking {
val scope = Script.newScope()
createProcessModule(PermitAllProcessAccessPolicy, scope)
val code = """
import lyng.io.process
var p = Process.execute("echo", ["hello", "lyng", "native"])
var output = []
for (line in p.stdout()) {
output.add(line)
}
p.waitFor()
println(output)
assertEquals("hello lyng native", output.joinToString(" "))
output
""".trimIndent()
val script = Compiler.compile(code)
val result = script.execute(scope)
assertTrue(result.inspect(scope).contains("hello lyng native"))
}
}
@@ -1,35 +0,0 @@
/*
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package net.sergeych.lyngio.process
import kotlinx.cinterop.*
import platform.posix.*
@OptIn(ExperimentalForeignApi::class)
internal actual fun getNativeKernelVersion(): String? {
return memScoped {
val u = alloc<utsname>()
if (uname(u.ptr) == 0) {
u.release.toKString()
} else null
}
}
internal actual fun isNativeProcessSupported(): Boolean = true
internal actual fun getNativeProcessRunner(): LyngProcessRunner = PosixProcessRunner
@@ -1,152 +0,0 @@
/*
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package net.sergeych.lyngio.process
import kotlinx.cinterop.*
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
import platform.posix.*
@OptIn(ExperimentalForeignApi::class)
internal class NativeLyngProcess(
private val pid: pid_t,
private val stdoutFd: Int,
private val stderrFd: Int
) : LyngProcess {
override val stdout: Flow<String> = createPipeFlow(stdoutFd)
override val stderr: Flow<String> = createPipeFlow(stderrFd)
override suspend fun sendSignal(signal: ProcessSignal) {
val sig = when (signal) {
ProcessSignal.SIGINT -> SIGINT
ProcessSignal.SIGTERM -> SIGTERM
ProcessSignal.SIGKILL -> SIGKILL
}
if (kill(pid, sig) != 0) {
throw RuntimeException("Failed to send signal $signal to process $pid: ${strerror(errno)?.toKString()}")
}
}
override suspend fun waitFor(): Int = withContext(Dispatchers.Default) {
memScoped {
val status = alloc<IntVar>()
if (waitpid(pid, status.ptr, 0) == -1) {
throw RuntimeException("Failed to wait for process $pid: ${strerror(errno)?.toKString()}")
}
val s = status.value
if ((s and 0x7f) == 0) (s shr 8) and 0xff else -1
}
}
override fun destroy() {
kill(pid, SIGKILL)
}
}
@OptIn(ExperimentalForeignApi::class)
private fun createPipeFlow(fd: Int): Flow<String> = flow {
val buffer = ByteArray(4096)
val lineBuffer = StringBuilder()
try {
while (true) {
val bytesRead = buffer.usePinned { pinned ->
read(fd, pinned.addressOf(0), buffer.size.toULong())
}
if (bytesRead <= 0L) break
val text = buffer.decodeToString(endIndex = bytesRead.toInt())
lineBuffer.append(text)
var newlineIdx = lineBuffer.indexOf('\n')
while (newlineIdx != -1) {
val line = lineBuffer.substring(0, newlineIdx)
emit(line)
lineBuffer.deleteRange(0, newlineIdx + 1)
newlineIdx = lineBuffer.indexOf('\n')
}
}
if (lineBuffer.isNotEmpty()) {
emit(lineBuffer.toString())
}
} finally {
close(fd)
}
}.flowOn(Dispatchers.Default)
@OptIn(ExperimentalForeignApi::class)
object PosixProcessRunner : LyngProcessRunner {
override suspend fun execute(executable: String, args: List<String>): LyngProcess = withContext(Dispatchers.Default) {
memScoped {
val pipeStdout = allocArray<IntVar>(2)
val pipeStderr = allocArray<IntVar>(2)
if (pipe(pipeStdout) != 0) throw RuntimeException("Failed to create stdout pipe")
if (pipe(pipeStderr) != 0) {
close(pipeStdout[0])
close(pipeStdout[1])
throw RuntimeException("Failed to create stderr pipe")
}
val pid = fork()
if (pid == -1) {
close(pipeStdout[0])
close(pipeStdout[1])
close(pipeStderr[0])
close(pipeStderr[1])
throw RuntimeException("Failed to fork: ${strerror(errno)?.toKString()}")
}
if (pid == 0) {
// Child process
dup2(pipeStdout[1], 1)
dup2(pipeStderr[1], 2)
close(pipeStdout[0])
close(pipeStdout[1])
close(pipeStderr[0])
close(pipeStderr[1])
val argv = allocArray<CPointerVar<ByteVar>>(args.size + 2)
argv[0] = executable.cstr.ptr
for (i in args.indices) {
argv[i + 1] = args[i].cstr.ptr
}
argv[args.size + 1] = null
execvp(executable, argv)
// If we are here, exec failed
_exit(1)
}
// Parent process
close(pipeStdout[1])
close(pipeStderr[1])
NativeLyngProcess(pid, pipeStdout[0], pipeStderr[0])
}
}
override suspend fun shell(command: String): LyngProcess {
return execute("/bin/sh", listOf("-c", command))
}
}
@@ -1,34 +0,0 @@
/*
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package net.sergeych.lyngio.process
internal actual fun getNativeKernelVersion(): String? = null
internal actual fun isNativeProcessSupported(): Boolean = true
internal actual fun getNativeProcessRunner(): LyngProcessRunner = WindowsProcessRunner
object WindowsProcessRunner : LyngProcessRunner {
override suspend fun execute(executable: String, args: List<String>): LyngProcess {
throw UnsupportedOperationException("Windows native process execution not implemented yet")
}
override suspend fun shell(command: String): LyngProcess {
return execute("cmd.exe", listOf("/c", command))
}
}
@@ -1,40 +0,0 @@
/*
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package net.sergeych.lyngio.process
import kotlin.experimental.ExperimentalNativeApi
@OptIn(ExperimentalNativeApi::class)
actual fun getPlatformDetails(): PlatformDetails {
return PlatformDetails(
name = Platform.osFamily.name,
version = "unknown",
arch = Platform.cpuArchitecture.name,
kernelVersion = getNativeKernelVersion()
)
}
internal expect fun getNativeKernelVersion(): String?
actual fun isProcessSupported(): Boolean = isNativeProcessSupported()
internal expect fun isNativeProcessSupported(): Boolean
actual fun getSystemProcessRunner(): LyngProcessRunner = getNativeProcessRunner()
internal expect fun getNativeProcessRunner(): LyngProcessRunner
+1 -2
View File
@@ -21,7 +21,7 @@ import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
import org.jetbrains.kotlin.gradle.dsl.JvmTarget import org.jetbrains.kotlin.gradle.dsl.JvmTarget
group = "net.sergeych" group = "net.sergeych"
version = "1.2.0-SNAPSHOT" version = "1.1.0-SNAPSHOT"
// Removed legacy buildscript classpath declarations; plugins are applied via the plugins DSL below // Removed legacy buildscript classpath declarations; plugins are applied via the plugins DSL below
@@ -46,7 +46,6 @@ buildkonfig {
} }
kotlin { kotlin {
jvmToolchain(17)
jvm() jvm()
androidTarget { androidTarget {
publishLibraryVariants("release") publishLibraryVariants("release")
@@ -23,7 +23,7 @@ actual object ArgBuilderProvider {
private val tl = object : ThreadLocal<AndroidArgsBuilder>() { private val tl = object : ThreadLocal<AndroidArgsBuilder>() {
override fun initialValue(): AndroidArgsBuilder = AndroidArgsBuilder() override fun initialValue(): AndroidArgsBuilder = AndroidArgsBuilder()
} }
actual fun acquire(): ArgsBuilder = tl.get()!! actual fun acquire(): ArgsBuilder = tl.get()
} }
private class AndroidArgsBuilder : ArgsBuilder { private class AndroidArgsBuilder : ArgsBuilder {
@@ -1,5 +1,5 @@
/* /*
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com * Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -23,7 +23,7 @@ actual object PerfDefaults {
actual val ARG_BUILDER: Boolean = true actual val ARG_BUILDER: Boolean = true
actual val SKIP_ARGS_ON_NULL_RECEIVER: Boolean = true actual val SKIP_ARGS_ON_NULL_RECEIVER: Boolean = true
actual val SCOPE_POOL: Boolean = true actual val SCOPE_POOL: Boolean = false
actual val FIELD_PIC: Boolean = true actual val FIELD_PIC: Boolean = true
actual val METHOD_PIC: Boolean = true actual val METHOD_PIC: Boolean = true
@@ -1,5 +1,5 @@
/* /*
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com * Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
package net.sergeych.lyng package net.sergeych.lyng
import net.sergeych.lyng.obj.Obj import net.sergeych.lyng.obj.Obj
import net.sergeych.lyng.obj.ObjVoid
/** /**
* Android actual: per-thread scope frame pool backed by ThreadLocal. * Android actual: per-thread scope frame pool backed by ThreadLocal.
@@ -37,31 +38,24 @@ actual object ScopePool {
actual fun borrow(parent: Scope, args: Arguments, pos: Pos, thisObj: Obj): Scope { actual fun borrow(parent: Scope, args: Arguments, pos: Pos, thisObj: Obj): Scope {
val pool = pool() val pool = pool()
if (pool.isNotEmpty()) { val s = if (pool.isNotEmpty()) pool.removeLast() else Scope(parent, args, pos, thisObj)
val s = pool.removeLast() return try {
try { if (s.parent !== parent || s.args !== args || s.pos !== pos || s.thisObj !== thisObj) {
// Re-initialize pooled instance
s.resetForReuse(parent, args, pos, thisObj) s.resetForReuse(parent, args, pos, thisObj)
return s
} catch (e: IllegalStateException) {
// Defensive fallback: if a cycle in scope parent chain is detected during reuse,
// discard pooled instance for this call frame and allocate a fresh scope instead.
if (e.message?.contains("cycle") == true && e.message?.contains("scope parent chain") == true) {
return Scope(parent, args, pos, thisObj)
} else { } else {
throw e s.frameId = nextFrameId()
} }
s
} catch (e: IllegalStateException) {
if (e.message?.contains("cycle") == true && e.message?.contains("scope parent chain") == true) {
Scope(parent, args, pos, thisObj)
} else throw e
} }
} }
return Scope(parent, args, pos, thisObj)
}
actual fun release(scope: Scope) { actual fun release(scope: Scope) {
val pool = pool() val pool = pool()
if (pool.size < MAX_POOL_SIZE) { scope.resetForReuse(parent = null, args = Arguments.EMPTY, pos = Pos.builtIn, thisObj = ObjVoid)
// Scrub sensitive references to avoid accidental retention before returning to pool if (pool.size < MAX_POOL_SIZE) pool.addLast(scope)
scope.scrub()
pool.addLast(scope)
}
} }
} }
@@ -51,32 +51,6 @@ data class ArgsDeclaration(val params: List<Item>, val endTokenType: Token.Type)
defaultVisibility: Visibility = Visibility.Public, defaultVisibility: Visibility = Visibility.Public,
declaringClass: net.sergeych.lyng.obj.ObjClass? = scope.currentClassCtx declaringClass: net.sergeych.lyng.obj.ObjClass? = scope.currentClassCtx
) { ) {
// Fast path for simple positional-only calls with no ellipsis and no defaults
if (arguments.named.isEmpty() && !arguments.tailBlockMode) {
var hasComplex = false
for (p in params) {
if (p.isEllipsis || p.defaultValue != null) {
hasComplex = true
break
}
}
if (!hasComplex) {
if (arguments.list.size != params.size)
scope.raiseIllegalArgument("expected ${params.size} arguments, got ${arguments.list.size}")
for (i in params.indices) {
val a = params[i]
val value = arguments.list[i]
scope.addItem(a.name, (a.accessType ?: defaultAccessType).isMutable,
value.byValueCopy(),
a.visibility ?: defaultVisibility,
recordType = ObjRecord.Type.Argument,
declaringClass = declaringClass)
}
return
}
}
fun assign(a: Item, value: Obj) { fun assign(a: Item, value: Obj) {
scope.addItem(a.name, (a.accessType ?: defaultAccessType).isMutable, scope.addItem(a.name, (a.accessType ?: defaultAccessType).isMutable,
value.byValueCopy(), value.byValueCopy(),
@@ -1,5 +1,5 @@
/* /*
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com * Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -17,8 +17,6 @@
package net.sergeych.lyng package net.sergeych.lyng
import net.sergeych.lyng.obj.Obj
import net.sergeych.lyng.obj.ObjClass
import net.sergeych.lyng.obj.ObjRecord import net.sergeych.lyng.obj.ObjRecord
/** /**
@@ -40,34 +38,133 @@ class ClosureScope(val callScope: Scope, val closureScope: Scope) :
} }
override fun get(name: String): ObjRecord? { override fun get(name: String): ObjRecord? {
// Fast-path built-ins
if (name == "this") return thisObj.asReadonly if (name == "this") return thisObj.asReadonly
// 1. Current frame locals (parameters, local variables) // Priority:
tryGetLocalRecord(this, name, currentClassCtx)?.let { return it } // 1) Locals and arguments declared in this lambda frame (including values defined before suspension)
// 2) Instance/class members of the captured receiver (`closureScope.thisObj`)
// 3) Symbols from the captured closure scope chain (locals/parents), ignoring nested ClosureScope overrides
// 4) Instance members of the caller's `this` (e.g., FlowBuilder.emit)
// 5) Symbols from the caller chain (locals/parents), ignoring nested ClosureScope overrides
// 6) Special fallback for module pseudo-symbols (e.g., __PACKAGE__)
// 2. Lexical environment (captured locals from entire ancestry) // 1) Locals/arguments in this closure frame
closureScope.chainLookupIgnoreClosure(name, followClosure = true, caller = currentClassCtx)?.let { return it } super.objects[name]?.let { return it }
super.localBindings[name]?.let { return it }
// 3. Lexical this members (captured receiver) // 1a) Priority: if we are in a class context, prefer our own private members to support
val receiver = thisObj // non-virtual private dispatch. This prevents a subclass from accidentally
val effectiveClass = receiver as? ObjClass ?: receiver.objClass // capturing a private member call from a base class.
for (cls in effectiveClass.mro) { // We only return non-field/non-property members (methods) here; fields must
val rec = cls.members[name] ?: cls.classScope?.objects?.get(name) // be resolved via instance storage in priority 2.
if (rec != null && !rec.isAbstract) { currentClassCtx?.let { ctx ->
if (canAccessMember(rec.visibility, rec.declaringClass ?: cls, currentClassCtx)) { ctx.members[name]?.let { rec ->
return rec.copy(receiver = receiver) if (rec.visibility == Visibility.Private &&
rec.type != net.sergeych.lyng.obj.ObjRecord.Type.Field &&
rec.type != net.sergeych.lyng.obj.ObjRecord.Type.Property) return rec
}
}
// 2) Members on the captured receiver instance
(closureScope.thisObj as? net.sergeych.lyng.obj.ObjInstance)?.let { inst ->
// Check direct locals in instance scope (unmangled)
inst.instanceScope.objects[name]?.let { rec ->
if (rec.type != net.sergeych.lyng.obj.ObjRecord.Type.Property &&
canAccessMember(rec.visibility, rec.declaringClass, currentClassCtx)) return rec
}
// Check mangled names for fields along MRO
for (cls in inst.objClass.mro) {
inst.instanceScope.objects["${cls.className}::$name"]?.let { rec ->
if (rec.type != net.sergeych.lyng.obj.ObjRecord.Type.Property &&
canAccessMember(rec.visibility, rec.declaringClass ?: cls, currentClassCtx)) return rec
} }
} }
} }
// Finally, root object fallback
Obj.rootObjectType.members[name]?.let { rec -> findExtension(closureScope.thisObj.objClass, name)?.let { return it }
closureScope.thisObj.objClass.getInstanceMemberOrNull(name)?.let { rec ->
if (canAccessMember(rec.visibility, rec.declaringClass, currentClassCtx)) { if (canAccessMember(rec.visibility, rec.declaringClass, currentClassCtx)) {
return rec.copy(receiver = receiver) // Return only non-field/non-property members (methods) from class-level records.
// Fields and properties must be resolved via instance storage (mangled) or readField.
if (rec.type != net.sergeych.lyng.obj.ObjRecord.Type.Field &&
rec.type != net.sergeych.lyng.obj.ObjRecord.Type.Property &&
!rec.isAbstract) return rec
} }
} }
// 4. Call environment (caller locals, caller this, and global fallback) // 3) Closure scope chain (locals/parents + members), ignore ClosureScope overrides to prevent recursion
return callScope.get(name) closureScope.chainLookupWithMembers(name, currentClassCtx)?.let { return it }
// 4) Caller `this` members
(callScope.thisObj as? net.sergeych.lyng.obj.ObjInstance)?.let { inst ->
// Check direct locals in instance scope (unmangled)
inst.instanceScope.objects[name]?.let { rec ->
if (rec.type != net.sergeych.lyng.obj.ObjRecord.Type.Property &&
canAccessMember(rec.visibility, rec.declaringClass, currentClassCtx)) return rec
}
// Check mangled names for fields along MRO
for (cls in inst.objClass.mro) {
inst.instanceScope.objects["${cls.className}::$name"]?.let { rec ->
if (rec.type != net.sergeych.lyng.obj.ObjRecord.Type.Property &&
canAccessMember(rec.visibility, rec.declaringClass ?: cls, currentClassCtx)) return rec
}
}
}
findExtension(callScope.thisObj.objClass, name)?.let { return it }
callScope.thisObj.objClass.getInstanceMemberOrNull(name)?.let { rec ->
if (canAccessMember(rec.visibility, rec.declaringClass, currentClassCtx)) {
if (rec.type != net.sergeych.lyng.obj.ObjRecord.Type.Field &&
rec.type != net.sergeych.lyng.obj.ObjRecord.Type.Property &&
!rec.isAbstract) return rec
}
}
// 5) Caller chain (locals/parents + members)
callScope.chainLookupWithMembers(name, currentClassCtx)?.let { return it }
// 6) Module pseudo-symbols (e.g., __PACKAGE__) — walk caller ancestry and query ModuleScope directly
if (name.startsWith("__")) {
var s: Scope? = callScope
val visited = HashSet<Long>(4)
while (s != null) {
if (!visited.add(s.frameId)) break
if (s is ModuleScope) return s.get(name)
s = s.parent
}
}
// 7) Direct module/global fallback: try to locate nearest ModuleScope and check its own locals
fun lookupInModuleAncestry(from: Scope): ObjRecord? {
var s: Scope? = from
val visited = HashSet<Long>(4)
while (s != null) {
if (!visited.add(s.frameId)) break
if (s is ModuleScope) {
s.objects[name]?.let { return it }
s.localBindings[name]?.let { return it }
// check immediate parent (root scope) locals/constants for globals like `delay`
val p = s.parent
if (p != null) {
p.objects[name]?.let { return it }
p.localBindings[name]?.let { return it }
p.thisObj.objClass.getInstanceMemberOrNull(name)?.let { return it }
}
return null
}
s = s.parent
}
return null
}
lookupInModuleAncestry(closureScope)?.let { return it }
lookupInModuleAncestry(callScope)?.let { return it }
// 8) Global root scope constants/functions (e.g., delay, yield) via current import provider
runCatching { this.currentImportProvider.rootScope.objects[name] }.getOrNull()?.let { return it }
// Final safe fallback: base scope lookup from this frame walking raw parents
return baseGetIgnoreClosure(name)
} }
} }
@@ -20,7 +20,7 @@ package net.sergeych.lyng
sealed class CodeContext { sealed class CodeContext {
class Module(@Suppress("unused") val packageName: String?): CodeContext() class Module(@Suppress("unused") val packageName: String?): CodeContext()
class Function(val name: String): CodeContext() class Function(val name: String): CodeContext()
class ClassBody(val name: String, val isExtern: Boolean = false): CodeContext() { class ClassBody(val name: String): CodeContext() {
val pendingInitializations = mutableMapOf<String, Pos>() val pendingInitializations = mutableMapOf<String, Pos>()
} }
} }
File diff suppressed because it is too large. Load diff
@@ -1,5 +1,5 @@
/* /*
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com * Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -34,34 +34,18 @@ class CompilerContext(val tokens: List<Token>) {
} }
var currentIndex = 0 var currentIndex = 0
private var pendingGT = 0
fun hasNext() = currentIndex < tokens.size || pendingGT > 0 fun hasNext() = currentIndex < tokens.size
fun hasPrevious() = currentIndex > 0 fun hasPrevious() = currentIndex > 0
fun next(): Token { fun next() =
if (pendingGT > 0) { if (currentIndex < tokens.size) tokens[currentIndex++]
pendingGT--
val last = tokens[currentIndex - 1]
return Token(">", last.pos.copy(column = last.pos.column + 1), Token.Type.GT)
}
return if (currentIndex < tokens.size) tokens[currentIndex++]
else Token("", tokens.last().pos, Token.Type.EOF) else Token("", tokens.last().pos, Token.Type.EOF)
}
fun pushPendingGT() { fun previous() = if (!hasPrevious()) throw IllegalStateException("No previous token") else tokens[--currentIndex]
pendingGT++
}
fun previous() = if (pendingGT > 0) { fun savePos() = currentIndex
pendingGT-- // This is wrong, previous should go back.
// But we don't really use previous() in generics parser after splitting.
throw IllegalStateException("previous() not supported after pushPendingGT")
} else if (!hasPrevious()) throw IllegalStateException("No previous token") else tokens[--currentIndex]
fun savePos() = (currentIndex shl 2) or (pendingGT and 3)
fun restorePos(pos: Int) { fun restorePos(pos: Int) {
currentIndex = pos shr 2 currentIndex = pos
pendingGT = pos and 3
} }
fun ensureLabelIsValid(pos: Pos, label: String) { fun ensureLabelIsValid(pos: Pos, label: String) {
@@ -122,13 +106,12 @@ class CompilerContext(val tokens: List<Token>) {
errorMessage: String = "expected ${tokenType.name}", errorMessage: String = "expected ${tokenType.name}",
isOptional: Boolean = false isOptional: Boolean = false
): Boolean { ): Boolean {
val pos = savePos()
val t = next() val t = next()
return if (t.type != tokenType) { return if (t.type != tokenType) {
if (!isOptional) { if (!isOptional) {
throw ScriptError(t.pos, errorMessage) throw ScriptError(t.pos, errorMessage)
} else { } else {
restorePos(pos) previous()
false false
} }
} else true } else true
@@ -139,25 +122,20 @@ class CompilerContext(val tokens: List<Token>) {
* @return true if token was found and skipped * @return true if token was found and skipped
*/ */
fun skipNextIf(vararg types: Token.Type): Boolean { fun skipNextIf(vararg types: Token.Type): Boolean {
val pos = savePos()
val t = next() val t = next()
return if (t.type in types) return if (t.type in types)
true true
else { else {
restorePos(pos) previous()
false false
} }
} }
@Suppress("unused") @Suppress("unused")
fun skipTokens(vararg tokenTypes: Token.Type) { fun skipTokens(vararg tokenTypes: Token.Type) {
while (hasNext()) { while (next().type in tokenTypes) { /**/
val pos = savePos()
if (next().type !in tokenTypes) {
restorePos(pos)
break
}
} }
previous()
} }
fun nextNonWhitespace(): Token { fun nextNonWhitespace(): Token {
@@ -173,11 +151,10 @@ class CompilerContext(val tokens: List<Token>) {
* @return next non-whitespace token without extracting it from tokens list * @return next non-whitespace token without extracting it from tokens list
*/ */
fun peekNextNonWhitespace(): Token { fun peekNextNonWhitespace(): Token {
val saved = savePos()
while (true) { while (true) {
val t = next() val t = next()
if (t.type !in wstokens) { if (t.type !in wstokens) {
restorePos(saved) previous()
return t return t
} }
} }
@@ -185,13 +162,12 @@ class CompilerContext(val tokens: List<Token>) {
inline fun ifNextIs(typeId: Token.Type, f: (Token) -> Unit): Boolean { inline fun ifNextIs(typeId: Token.Type, f: (Token) -> Unit): Boolean {
val pos = savePos()
val t = next() val t = next()
return if (t.type == typeId) { return if (t.type == typeId) {
f(t) f(t)
true true
} else { } else {
restorePos(pos) previous()
false false
} }
} }
@@ -238,7 +214,7 @@ class CompilerContext(val tokens: List<Token>) {
} }
} }
Token.Type.MULTILINE_COMMENT, Token.Type.SINGLE_LINE_COMMENT, Token.Type.NEWLINE -> {} Token.Type.MULTILINE_COMMENT, Token.Type.SINLGE_LINE_COMMENT, Token.Type.NEWLINE -> {}
else -> { else -> {
restorePos(pos); return false restorePos(pos); return false
} }
@@ -265,6 +241,6 @@ class CompilerContext(val tokens: List<Token>) {
} }
companion object { companion object {
val wstokens = setOf(Token.Type.NEWLINE, Token.Type.MULTILINE_COMMENT, Token.Type.SINGLE_LINE_COMMENT) val wstokens = setOf(Token.Type.NEWLINE, Token.Type.MULTILINE_COMMENT, Token.Type.SINLGE_LINE_COMMENT)
} }
} }
@@ -131,7 +131,7 @@ private class Parser(fromPos: Pos) {
pos.advance() pos.advance()
val body = loadToEndOfLine() val body = loadToEndOfLine()
// Include the leading '//' and do not trim; keep exact lexeme (excluding preceding codepoint) // Include the leading '//' and do not trim; keep exact lexeme (excluding preceding codepoint)
Token("//" + body, from, Token.Type.SINGLE_LINE_COMMENT) Token("//" + body, from, Token.Type.SINLGE_LINE_COMMENT)
} }
'*' -> { '*' -> {
@@ -325,7 +325,6 @@ private class Parser(fromPos: Pos) {
'?' -> { '?' -> {
when (currentChar) { when (currentChar) {
'=' -> { pos.advance(); Token("?=", from, Token.Type.IFNULLASSIGN) }
':' -> { pos.advance(); Token("?:", from, Token.Type.ELVIS) } ':' -> { pos.advance(); Token("?:", from, Token.Type.ELVIS) }
'?' -> { pos.advance(); Token("??", from, Token.Type.ELVIS) } '?' -> { pos.advance(); Token("??", from, Token.Type.ELVIS) }
'.' -> { pos.advance(); Token("?.", from, Token.Type.NULL_COALESCE) } '.' -> { pos.advance(); Token("?.", from, Token.Type.NULL_COALESCE) }
@@ -1,30 +0,0 @@
/*
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package net.sergeych.lyng
import net.sergeych.lyng.obj.Obj
import net.sergeych.lyng.obj.ObjVoid
/**
* Exception used to implement `return` statement.
* It carries the return value and an optional label for non-local returns.
*/
class ReturnException(
val result: Obj = ObjVoid,
val label: String? = null
) : RuntimeException()
@@ -70,8 +70,9 @@ open class Scope(
internal fun findExtension(receiverClass: ObjClass, name: String): ObjRecord? { internal fun findExtension(receiverClass: ObjClass, name: String): ObjRecord? {
var s: Scope? = this var s: Scope? = this
var hops = 0 val visited = HashSet<Long>(4)
while (s != null && hops++ < 1024) { while (s != null) {
if (!visited.add(s.frameId)) break
// Proximity rule: check all extensions in the current scope before going to parent. // Proximity rule: check all extensions in the current scope before going to parent.
// Priority within scope: more specific class in MRO wins. // Priority within scope: more specific class in MRO wins.
for (cls in receiverClass.mro) { for (cls in receiverClass.mro) {
@@ -105,20 +106,10 @@ open class Scope(
* intertwined closure frames. They traverse the plain parent chain and consult only locals * intertwined closure frames. They traverse the plain parent chain and consult only locals
* and bindings of each frame. Instance/class member fallback must be decided by the caller. * and bindings of each frame. Instance/class member fallback must be decided by the caller.
*/ */
internal fun tryGetLocalRecord(s: Scope, name: String, caller: net.sergeych.lyng.obj.ObjClass?): ObjRecord? { private fun tryGetLocalRecord(s: Scope, name: String, caller: net.sergeych.lyng.obj.ObjClass?): ObjRecord? {
caller?.let { ctx ->
s.objects[ctx.mangledName(name)]?.let { rec ->
if (rec.visibility == Visibility.Private) return rec
}
}
s.objects[name]?.let { rec -> s.objects[name]?.let { rec ->
if (rec.declaringClass == null || canAccessMember(rec.visibility, rec.declaringClass, caller)) return rec if (rec.declaringClass == null || canAccessMember(rec.visibility, rec.declaringClass, caller)) return rec
} }
caller?.let { ctx ->
s.localBindings[ctx.mangledName(name)]?.let { rec ->
if (rec.visibility == Visibility.Private) return rec
}
}
s.localBindings[name]?.let { rec -> s.localBindings[name]?.let { rec ->
if (rec.declaringClass == null || canAccessMember(rec.visibility, rec.declaringClass, caller)) return rec if (rec.declaringClass == null || canAccessMember(rec.visibility, rec.declaringClass, caller)) return rec
} }
@@ -129,14 +120,14 @@ open class Scope(
return null return null
} }
internal fun chainLookupIgnoreClosure(name: String, followClosure: Boolean = false, caller: net.sergeych.lyng.obj.ObjClass? = null): ObjRecord? { internal fun chainLookupIgnoreClosure(name: String): ObjRecord? {
var s: Scope? = this var s: Scope? = this
// use hop counter to detect unexpected structural cycles in the parent chain // use frameId to detect unexpected structural cycles in the parent chain
var hops = 0 val visited = HashSet<Long>(4)
val effectiveCaller = caller ?: currentClassCtx while (s != null) {
while (s != null && hops++ < 1024) { if (!visited.add(s.frameId)) return null
tryGetLocalRecord(s, name, effectiveCaller)?.let { return it } tryGetLocalRecord(s, name, currentClassCtx)?.let { return it }
s = if (followClosure && s is ClosureScope) s.closureScope else s.parent s = s.parent
} }
return null return null
} }
@@ -153,8 +144,9 @@ open class Scope(
tryGetLocalRecord(this, name, currentClassCtx)?.let { return it } tryGetLocalRecord(this, name, currentClassCtx)?.let { return it }
// 2) walk parents for plain locals/bindings only // 2) walk parents for plain locals/bindings only
var s = parent var s = parent
var hops = 0 val visited = HashSet<Long>(4)
while (s != null && hops++ < 1024) { while (s != null) {
if (!visited.add(s.frameId)) return null
tryGetLocalRecord(s, name, currentClassCtx)?.let { return it } tryGetLocalRecord(s, name, currentClassCtx)?.let { return it }
s = s.parent s = s.parent
} }
@@ -177,10 +169,11 @@ open class Scope(
* This completely avoids invoking overridden `get` implementations, preventing * This completely avoids invoking overridden `get` implementations, preventing
* ping-pong recursion between `ClosureScope` frames. * ping-pong recursion between `ClosureScope` frames.
*/ */
internal fun chainLookupWithMembers(name: String, caller: net.sergeych.lyng.obj.ObjClass? = currentClassCtx, followClosure: Boolean = false): ObjRecord? { internal fun chainLookupWithMembers(name: String, caller: net.sergeych.lyng.obj.ObjClass? = currentClassCtx): ObjRecord? {
var s: Scope? = this var s: Scope? = this
var hops = 0 val visited = HashSet<Long>(4)
while (s != null && hops++ < 1024) { while (s != null) {
if (!visited.add(s.frameId)) return null
tryGetLocalRecord(s, name, caller)?.let { return it } tryGetLocalRecord(s, name, caller)?.let { return it }
for (cls in s.thisObj.objClass.mro) { for (cls in s.thisObj.objClass.mro) {
s.extensions[cls]?.get(name)?.let { return it } s.extensions[cls]?.get(name)?.let { return it }
@@ -192,7 +185,7 @@ open class Scope(
} else return rec } else return rec
} }
} }
s = if (followClosure && s is ClosureScope) s.closureScope else s.parent s = s.parent
} }
return null return null
} }
@@ -284,22 +277,16 @@ open class Scope(
raiseError(ObjSymbolNotDefinedException(this, "symbol is not defined: $name")) raiseError(ObjSymbolNotDefinedException(this, "symbol is not defined: $name"))
fun raiseError(message: String): Nothing { fun raiseError(message: String): Nothing {
val ex = ObjException(this, message) throw ExecutionError(ObjException(this, message))
throw ExecutionError(ex, pos, ex.message.value)
} }
fun raiseError(obj: ObjException): Nothing { fun raiseError(obj: ObjException): Nothing {
throw ExecutionError(obj, obj.scope.pos, obj.message.value) throw ExecutionError(obj)
}
fun raiseError(obj: Obj, pos: Pos, message: String): Nothing {
throw ExecutionError(obj, pos, message)
} }
@Suppress("unused") @Suppress("unused")
fun raiseNotFound(message: String = "not found"): Nothing { fun raiseNotFound(message: String = "not found"): Nothing {
val ex = ObjNotFoundException(this, message) throw ExecutionError(ObjNotFoundException(this, message))
throw ExecutionError(ex, ex.scope.pos, ex.message.value)
} }
inline fun <reified T : Obj> requiredArg(index: Int): T { inline fun <reified T : Obj> requiredArg(index: Int): T {
@@ -327,51 +314,37 @@ open class Scope(
inline fun <reified T : Obj> thisAs(): T { inline fun <reified T : Obj> thisAs(): T {
var s: Scope? = this var s: Scope? = this
while (s != null) { do {
val t = s.thisObj val t = s!!.thisObj
if (t is T) return t if (t is T) return t
s = s.parent s = s.parent
} } while (s != null)
raiseClassCastError("Cannot cast ${thisObj.objClass.className} to ${T::class.simpleName}") raiseClassCastError("Cannot cast ${thisObj.objClass.className} to ${T::class.simpleName}")
} }
internal val objects = mutableMapOf<String, ObjRecord>() internal val objects = mutableMapOf<String, ObjRecord>()
open operator fun get(name: String): ObjRecord? { open operator fun get(name: String): ObjRecord? =
if (name == "this") return thisObj.asReadonly if (name == "this") thisObj.asReadonly
else {
// 1. Prefer direct locals/bindings declared in this frame // Prefer direct locals/bindings declared in this frame
tryGetLocalRecord(this, name, currentClassCtx)?.let { return it } (objects[name]?.let { rec ->
if (rec.declaringClass == null || canAccessMember(rec.visibility, rec.declaringClass, currentClassCtx)) rec else null
val p = parent
// 2. If we share thisObj with parent, delegate to parent to maintain
// "locals shadow members" priority across the this-context.
if (p != null && p.thisObj === thisObj) {
return p.get(name)
} }
// Then, check known local bindings in this frame (helps after suspension)
// 3. Otherwise, we are the "primary" scope for this thisObj (or have no parent), ?: localBindings[name]?.let { rec ->
// so check members of thisObj before walking up to a different this-context. if (rec.declaringClass == null || canAccessMember(rec.visibility, rec.declaringClass, currentClassCtx)) rec else null
val receiver = thisObj
val effectiveClass = receiver as? ObjClass ?: receiver.objClass
for (cls in effectiveClass.mro) {
val rec = cls.members[name] ?: cls.classScope?.objects?.get(name)
if (rec != null && !rec.isAbstract) {
if (canAccessMember(rec.visibility, rec.declaringClass ?: cls, currentClassCtx)) {
return rec.copy(receiver = receiver)
} }
} // Walk up ancestry
} ?: parent?.get(name)
// Finally, root object fallback // Finally, fallback to class members on thisObj
Obj.rootObjectType.members[name]?.let { rec -> ?: thisObj.objClass.getInstanceMemberOrNull(name)?.let { rec ->
if (canAccessMember(rec.visibility, rec.declaringClass, currentClassCtx)) { if (canAccessMember(rec.visibility, rec.declaringClass, currentClassCtx)) {
return rec.copy(receiver = receiver) if (rec.type == ObjRecord.Type.Field || rec.type == ObjRecord.Type.Property || rec.isAbstract) null
else rec
} else null
} }
} )
// 4. Finally, walk up ancestry to a scope with a different thisObj context
return p?.get(name)
} }
// Slot fast-path API // Slot fast-path API
@@ -392,20 +365,6 @@ open class Scope(
nameToSlot[name]?.let { slots[it] = record } nameToSlot[name]?.let { slots[it] = record }
} }
/**
* Clear all references and maps to prevent memory leaks when pooled.
*/
fun scrub() {
this.parent = null
this.skipScopeCreation = false
this.currentClassCtx = null
objects.clear()
slots.clear()
nameToSlot.clear()
localBindings.clear()
extensions.clear()
}
/** /**
* Reset this scope instance so it can be safely reused as a fresh child frame. * Reset this scope instance so it can be safely reused as a fresh child frame.
* Clears locals and slots, assigns new frameId, and sets parent/args/pos/thisObj. * Clears locals and slots, assigns new frameId, and sets parent/args/pos/thisObj.
@@ -415,7 +374,6 @@ open class Scope(
// that could interact badly with the new parent and produce a cycle. // that could interact badly with the new parent and produce a cycle.
this.parent = null this.parent = null
this.skipScopeCreation = false this.skipScopeCreation = false
this.currentClassCtx = parent?.currentClassCtx
// fresh identity for PIC caches // fresh identity for PIC caches
this.frameId = nextFrameId() this.frameId = nextFrameId()
// clear locals and slot maps // clear locals and slot maps
@@ -543,16 +501,12 @@ open class Scope(
} }
} }
// Map to a slot for fast local access (ensure consistency) // Map to a slot for fast local access (ensure consistency)
if (nameToSlot.isEmpty()) { val idx = getSlotIndexOf(name)
allocateSlotFor(name, rec)
} else {
val idx = nameToSlot[name]
if (idx == null) { if (idx == null) {
allocateSlotFor(name, rec) allocateSlotFor(name, rec)
} else { } else {
slots[idx] = rec slots[idx] = rec
} }
}
return rec return rec
} }
@@ -668,34 +622,31 @@ open class Scope(
} }
suspend fun resolve(rec: ObjRecord, name: String): Obj { suspend fun resolve(rec: ObjRecord, name: String): Obj {
val receiver = rec.receiver ?: thisObj if (rec.type == ObjRecord.Type.Delegated) {
return receiver.resolveRecord(this, rec, name, rec.declaringClass).value val del = rec.delegate ?: raiseError("Internal error: delegated property $name has no delegate")
val th = if (thisObj === ObjVoid) ObjNull else thisObj
if (del.objClass.getInstanceMemberOrNull("getValue") == null) {
return object : Statement() {
override val pos: Pos = Pos.builtIn
override suspend fun execute(scope: Scope): Obj {
val th2 = if (scope.thisObj === ObjVoid) ObjNull else scope.thisObj
val allArgs = (listOf(th2, ObjString(name)) + scope.args.list).toTypedArray()
return del.invokeInstanceMethod(scope, "invoke", Arguments(*allArgs))
}
}
}
return del.invokeInstanceMethod(this, "getValue", Arguments(th, ObjString(name)))
}
return rec.value
} }
suspend fun assign(rec: ObjRecord, name: String, newValue: Obj) { suspend fun assign(rec: ObjRecord, name: String, newValue: Obj) {
if (rec.type == ObjRecord.Type.Delegated) { if (rec.type == ObjRecord.Type.Delegated) {
val receiver = rec.receiver ?: thisObj val del = rec.delegate ?: raiseError("Internal error: delegated property $name has no delegate")
val del = rec.delegate ?: run { val th = if (thisObj === ObjVoid) ObjNull else thisObj
if (receiver is ObjInstance) {
(receiver as ObjInstance).writeField(this, name, newValue)
return
}
raiseError("Internal error: delegated property $name has no delegate")
}
val th = if (receiver === ObjVoid) ObjNull else receiver
del.invokeInstanceMethod(this, "setValue", Arguments(th, ObjString(name), newValue)) del.invokeInstanceMethod(this, "setValue", Arguments(th, ObjString(name), newValue))
return return
} }
if (rec.value is ObjProperty) {
(rec.value as ObjProperty).callSetter(this, rec.receiver ?: thisObj, newValue, rec.declaringClass)
return
}
// If it's a member (explicitly tracked by receiver or declaringClass), use writeField.
// Important: locals have receiver == null and declaringClass == null (enforced in addItem).
if (rec.receiver != null || (rec.declaringClass != null && (rec.type == ObjRecord.Type.Field || rec.type == ObjRecord.Type.Property))) {
(rec.receiver ?: thisObj).writeField(this, name, newValue)
return
}
if (!rec.isMutable && rec.value !== ObjUnset) raiseIllegalAssignment("can't reassign val $name") if (!rec.isMutable && rec.value !== ObjUnset) raiseIllegalAssignment("can't reassign val $name")
rec.value = newValue rec.value = newValue
} }
@@ -1,5 +1,5 @@
/* /*
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com * Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -21,7 +21,7 @@ import net.sergeych.lyng.obj.Obj
/** /**
* Expect/actual portable scope frame pool. Used only when [PerfFlags.SCOPE_POOL] is true. * Expect/actual portable scope frame pool. Used only when [PerfFlags.SCOPE_POOL] is true.
* Provides per-thread pooling on JVM, Android, and Native; global pooling on JS and Wasm. * JVM actual provides a ThreadLocal-backed pool; other targets may use a simple global deque.
*/ */
expect object ScopePool { expect object ScopePool {
fun borrow(parent: Scope, args: Arguments, pos: Pos, thisObj: Obj): Scope fun borrow(parent: Scope, args: Arguments, pos: Pos, thisObj: Obj): Scope
@@ -248,9 +248,10 @@ class Script(
) )
) )
expectedClass?.let { expectedClass?.let {
if (!result.isInstanceOf(it)) { if (result !is ObjException)
val actual = if (result is ObjException) result.exceptionClass else result.objClass raiseError("Expected $expectedClass, got non-lyng exception $result")
raiseError("Expected $it, got $actual") if (result.exceptionClass != expectedClass) {
raiseError("Expected $expectedClass, got ${result.exceptionClass}")
} }
} }
result result
@@ -293,14 +294,10 @@ class Script(
ObjVoid ObjVoid
} }
// Delay in milliseconds (plain numeric). For time-aware variants use lyng.time.Duration API.
addVoidFn("delay") { addVoidFn("delay") {
val a = args.firstAndOnly() val ms = (this.args.firstAndOnly().toDouble()).roundToLong()
when (a) { delay(ms)
is ObjInt -> delay(a.value)
is ObjReal -> delay((a.value * 1000).roundToLong())
is ObjDuration -> delay(a.duration)
else -> raiseIllegalArgument("Expected Int, Real or Duration, got ${a.inspect(this)}")
}
} }
addConst("Object", rootObjectType) addConst("Object", rootObjectType)
@@ -1,5 +1,5 @@
/* /*
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com * Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -19,7 +19,7 @@
package net.sergeych.lyng package net.sergeych.lyng
import net.sergeych.lyng.obj.Obj import net.sergeych.lyng.obj.ObjException
open class ScriptError(val pos: Pos, val errorMessage: String, cause: Throwable? = null) : Exception( open class ScriptError(val pos: Pos, val errorMessage: String, cause: Throwable? = null) : Exception(
""" """
@@ -33,6 +33,6 @@ open class ScriptError(val pos: Pos, val errorMessage: String, cause: Throwable?
class ScriptFlowIsNoMoreCollected: Exception() class ScriptFlowIsNoMoreCollected: Exception()
class ExecutionError(val errorObject: Obj, pos: Pos, message: String) : ScriptError(pos, message) class ExecutionError(val errorObject: ObjException) : ScriptError(errorObject.scope.pos, errorObject.message.value)
class ImportException(pos: Pos, message: String) : ScriptError(pos, message) class ImportException(pos: Pos, message: String) : ScriptError(pos, message)
@@ -40,7 +40,7 @@ class Source(val fileName: String, val text: String) {
fun extractPackageName(): String { fun extractPackageName(): String {
for ((n,line) in lines.withIndex()) { for ((n,line) in lines.withIndex()) {
if( line.isBlank() ) if( line.isBlank() || line.isEmpty() )
continue continue
if( line.startsWith("package ") ) if( line.startsWith("package ") )
return line.substring(8).trim() return line.substring(8).trim()
@@ -22,7 +22,7 @@ data class Token(val value: String, val pos: Pos, val type: Type) {
throw ScriptError(pos, text) throw ScriptError(pos, text)
} }
val isComment: Boolean by lazy { type == Type.SINGLE_LINE_COMMENT || type == Type.MULTILINE_COMMENT } val isComment: Boolean by lazy { type == Type.SINLGE_LINE_COMMENT || type == Type.MULTILINE_COMMENT }
fun isId(text: String) = fun isId(text: String) =
type == Type.ID && value == text type == Type.ID && value == text
@@ -34,14 +34,14 @@ data class Token(val value: String, val pos: Pos, val type: Type) {
LPAREN, RPAREN, LBRACE, RBRACE, LBRACKET, RBRACKET, COMMA, LPAREN, RPAREN, LBRACE, RBRACE, LBRACKET, RBRACKET, COMMA,
SEMICOLON, COLON, SEMICOLON, COLON,
PLUS, MINUS, STAR, SLASH, PERCENT, PLUS, MINUS, STAR, SLASH, PERCENT,
ASSIGN, PLUSASSIGN, MINUSASSIGN, STARASSIGN, SLASHASSIGN, PERCENTASSIGN, IFNULLASSIGN, ASSIGN, PLUSASSIGN, MINUSASSIGN, STARASSIGN, SLASHASSIGN, PERCENTASSIGN,
PLUS2, MINUS2, PLUS2, MINUS2,
IN, NOTIN, IS, NOTIS, BY, IN, NOTIN, IS, NOTIS, BY,
EQ, NEQ, LT, LTE, GT, GTE, REF_EQ, REF_NEQ, MATCH, NOTMATCH, EQ, NEQ, LT, LTE, GT, GTE, REF_EQ, REF_NEQ, MATCH, NOTMATCH,
SHUTTLE, SHUTTLE,
AND, BITAND, OR, BITOR, BITXOR, NOT, BITNOT, DOT, ARROW, EQARROW, QUESTION, COLONCOLON, AND, BITAND, OR, BITOR, BITXOR, NOT, BITNOT, DOT, ARROW, EQARROW, QUESTION, COLONCOLON,
SHL, SHR, SHL, SHR,
SINGLE_LINE_COMMENT, MULTILINE_COMMENT, SINLGE_LINE_COMMENT, MULTILINE_COMMENT,
LABEL, ATLABEL, // label@ at@label LABEL, ATLABEL, // label@ at@label
// type-checking/casting // type-checking/casting
AS, ASNULL, OBJECT, AS, ASNULL, OBJECT,
@@ -28,5 +28,4 @@ sealed class TypeDecl(val isNullable:Boolean = false) {
object TypeNullableAny : TypeDecl(true) object TypeNullableAny : TypeDecl(true)
class Simple(val name: String,isNullable: Boolean) : TypeDecl(isNullable) class Simple(val name: String,isNullable: Boolean) : TypeDecl(isNullable)
class Generic(val name: String, val args: List<TypeDecl>, isNullable: Boolean) : TypeDecl(isNullable)
} }
@@ -35,8 +35,7 @@ data class Symbol(
val kind: SymbolKind, val kind: SymbolKind,
val declStart: Int, val declStart: Int,
val declEnd: Int, val declEnd: Int,
val containerId: Int?, val containerId: Int?
val type: String? = null
) )
data class Reference(val symbolId: Int, val start: Int, val end: Int) data class Reference(val symbolId: Int, val start: Int, val end: Int)
@@ -98,83 +97,59 @@ object Binder {
// First pass (classes only): register classes so we can attach methods/fields // First pass (classes only): register classes so we can attach methods/fields
for (d in mini.declarations) if (d is MiniClassDecl) { for (d in mini.declarations) if (d is MiniClassDecl) {
val (s, e) = nameOffsets(d.nameStart, d.name) val (s, e) = nameOffsets(d.nameStart, d.name)
val sym = Symbol(nextId++, d.name, SymbolKind.Class, s, e, containerId = null, type = d.name) val sym = Symbol(nextId++, d.name, SymbolKind.Class, s, e, containerId = null)
symbols += sym symbols += sym
topLevelByName.getOrPut(d.name) { mutableListOf() }.add(sym.id) topLevelByName.getOrPut(d.name) { mutableListOf() }.add(sym.id)
// Prefer explicit body range; otherwise use the whole class declaration range // Prefer explicit body range; otherwise use the whole class declaration range
val bodyStart = d.bodyRange?.start?.let { source.offsetOf(it) } ?: source.offsetOf(d.range.start) val bodyStart = d.bodyRange?.start?.let { source.offsetOf(it) } ?: source.offsetOf(d.range.start)
val bodyEnd = d.bodyRange?.end?.let { source.offsetOf(it) } ?: source.offsetOf(d.range.end) val bodyEnd = d.bodyRange?.end?.let { source.offsetOf(it) } ?: source.offsetOf(d.range.end)
val classScope = ClassScope(sym.id, bodyStart, bodyEnd, mutableListOf()) classes += ClassScope(sym.id, bodyStart, bodyEnd, mutableListOf())
classes += classScope
// Constructor fields (val/var in primary ctor) // Constructor fields (val/var in primary ctor)
for (cf in d.ctorFields) { for (cf in d.ctorFields) {
val fs = source.offsetOf(cf.nameStart) val fs = source.offsetOf(cf.nameStart)
val fe = fs + cf.name.length val fe = fs + cf.name.length
val kind = if (cf.mutable) SymbolKind.Variable else SymbolKind.Value val kind = if (cf.mutable) SymbolKind.Variable else SymbolKind.Value
val fieldSym = Symbol(nextId++, cf.name, kind, fs, fe, containerId = sym.id, type = DocLookupUtils.typeOf(cf.type)) val fieldSym = Symbol(nextId++, cf.name, kind, fs, fe, containerId = sym.id)
symbols += fieldSym symbols += fieldSym
classScope.fields += fieldSym.id classes.last().fields += fieldSym.id
} }
// Class fields (val/var in class body, if any are reported here) // Class fields (val/var in class body, if any are reported here)
for (cf in d.classFields) { for (cf in d.classFields) {
val fs = source.offsetOf(cf.nameStart) val fs = source.offsetOf(cf.nameStart)
val fe = fs + cf.name.length val fe = fs + cf.name.length
val kind = if (cf.mutable) SymbolKind.Variable else SymbolKind.Value val kind = if (cf.mutable) SymbolKind.Variable else SymbolKind.Value
val fieldSym = Symbol(nextId++, cf.name, kind, fs, fe, containerId = sym.id, type = DocLookupUtils.typeOf(cf.type)) val fieldSym = Symbol(nextId++, cf.name, kind, fs, fe, containerId = sym.id)
symbols += fieldSym symbols += fieldSym
classScope.fields += fieldSym.id classes.last().fields += fieldSym.id
} }
// Members (including fields and methods)
for (m in d.members) {
if (m is MiniMemberValDecl) {
val fs = source.offsetOf(m.nameStart)
val fe = fs + m.name.length
val kind = if (m.mutable) SymbolKind.Variable else SymbolKind.Value
val fieldSym = Symbol(nextId++, m.name, kind, fs, fe, containerId = sym.id, type = DocLookupUtils.typeOf(m.type))
symbols += fieldSym
classScope.fields += fieldSym.id
}
}
}
fun registerFun(name: String, nameStart: net.sergeych.lyng.Pos, params: List<MiniParam>, returnType: MiniTypeRef?, bodyRange: MiniRange?, isTopLevel: Boolean) {
val (s, e) = nameOffsets(nameStart, name)
val ownerClass = classContaining(s)
val sym = Symbol(nextId++, name, SymbolKind.Function, s, e, containerId = ownerClass?.symId, type = DocLookupUtils.typeOf(returnType))
symbols += sym
if (isTopLevel) {
topLevelByName.getOrPut(name) { mutableListOf() }.add(sym.id)
}
// Determine body range if present; otherwise, derive a conservative end at decl range end
val bodyStart = bodyRange?.start?.let { source.offsetOf(it) } ?: e
val bodyEnd = bodyRange?.end?.let { source.offsetOf(it) } ?: e
val fnScope = FnScope(sym.id, bodyStart, bodyEnd, mutableListOf(), ownerClass?.symId)
// Params
for (p in params) {
val ps = source.offsetOf(p.nameStart)
val pe = ps + p.name.length
val pk = SymbolKind.Parameter
val paramSym = Symbol(nextId++, p.name, pk, ps, pe, containerId = sym.id, type = DocLookupUtils.typeOf(p.type))
fnScope.locals += paramSym.id
symbols += paramSym
}
functions += fnScope
} }
// Second pass: functions and top-level/class vals/vars // Second pass: functions and top-level/class vals/vars
for (d in mini.declarations) { for (d in mini.declarations) {
when (d) { when (d) {
is MiniClassDecl -> { is MiniClassDecl -> { /* already processed in first pass */ }
for (m in d.members) {
if (m is MiniMemberFunDecl) {
registerFun(m.name, m.nameStart, m.params, m.returnType, m.body?.range, false)
}
}
}
is MiniFunDecl -> { is MiniFunDecl -> {
registerFun(d.name, d.nameStart, d.params, d.returnType, d.body?.range, true) val (s, e) = nameOffsets(d.nameStart, d.name)
val ownerClass = classContaining(s)
val sym = Symbol(nextId++, d.name, SymbolKind.Function, s, e, containerId = ownerClass?.symId)
symbols += sym
topLevelByName.getOrPut(d.name) { mutableListOf() }.add(sym.id)
// Determine body range if present; otherwise, derive a conservative end at decl range end
val bodyStart = d.body?.range?.start?.let { source.offsetOf(it) } ?: e
val bodyEnd = d.body?.range?.end?.let { source.offsetOf(it) } ?: e
val fnScope = FnScope(sym.id, bodyStart, bodyEnd, mutableListOf(), ownerClass?.symId)
// Params
for (p in d.params) {
val ps = source.offsetOf(p.nameStart)
val pe = ps + p.name.length
val pk = SymbolKind.Parameter
val paramSym = Symbol(nextId++, p.name, pk, ps, pe, containerId = sym.id)
fnScope.locals += paramSym.id
symbols += paramSym
}
functions += fnScope
} }
is MiniValDecl -> { is MiniValDecl -> {
val (s, e) = nameOffsets(d.nameStart, d.name) val (s, e) = nameOffsets(d.nameStart, d.name)
@@ -182,18 +157,18 @@ object Binder {
val ownerClass = classContaining(s) val ownerClass = classContaining(s)
if (ownerClass != null) { if (ownerClass != null) {
// class field // class field
val fieldSym = Symbol(nextId++, d.name, kind, s, e, containerId = ownerClass.symId, type = DocLookupUtils.typeOf(d.type)) val fieldSym = Symbol(nextId++, d.name, kind, s, e, containerId = ownerClass.symId)
symbols += fieldSym symbols += fieldSym
ownerClass.fields += fieldSym.id ownerClass.fields += fieldSym.id
} else { } else {
val sym = Symbol(nextId++, d.name, kind, s, e, containerId = null, type = DocLookupUtils.typeOf(d.type)) val sym = Symbol(nextId++, d.name, kind, s, e, containerId = null)
symbols += sym symbols += sym
topLevelByName.getOrPut(d.name) { mutableListOf() }.add(sym.id) topLevelByName.getOrPut(d.name) { mutableListOf() }.add(sym.id)
} }
} }
is MiniEnumDecl -> { is MiniEnumDecl -> {
val (s, e) = nameOffsets(d.nameStart, d.name) val (s, e) = nameOffsets(d.nameStart, d.name)
val sym = Symbol(nextId++, d.name, SymbolKind.Enum, s, e, containerId = null, type = d.name) val sym = Symbol(nextId++, d.name, SymbolKind.Enum, s, e, containerId = null)
symbols += sym symbols += sym
topLevelByName.getOrPut(d.name) { mutableListOf() }.add(sym.id) topLevelByName.getOrPut(d.name) { mutableListOf() }.add(sym.id)
} }
@@ -212,7 +187,7 @@ object Binder {
"iterator", "hasNext", "next" "iterator", "hasNext", "next"
) )
for (name in stdFns) { for (name in stdFns) {
val sym = Symbol(nextId++, name, SymbolKind.Function, 0, name.length, containerId = null, type = null) val sym = Symbol(nextId++, name, SymbolKind.Function, 0, name.length, containerId = null)
symbols += sym symbols += sym
topLevelByName.getOrPut(name) { mutableListOf() }.add(sym.id) topLevelByName.getOrPut(name) { mutableListOf() }.add(sym.id)
} }
@@ -229,7 +204,7 @@ object Binder {
if (containerFn != null) { if (containerFn != null) {
val fnSymId = containerFn.id val fnSymId = containerFn.id
val kind = if (d.mutable) SymbolKind.Variable else SymbolKind.Value val kind = if (d.mutable) SymbolKind.Variable else SymbolKind.Value
val localSym = Symbol(nextId++, d.name, kind, s, e, containerId = fnSymId, type = DocLookupUtils.typeOf(d.type)) val localSym = Symbol(nextId++, d.name, kind, s, e, containerId = fnSymId)
symbols += localSym symbols += localSym
containerFn.locals += localSym.id containerFn.locals += localSym.id
} }
@@ -270,11 +245,11 @@ object Binder {
.maxByOrNull { it.rangeEnd - it.rangeStart } .maxByOrNull { it.rangeEnd - it.rangeStart }
val kind = if (kw.equals("var", true)) SymbolKind.Variable else SymbolKind.Value val kind = if (kw.equals("var", true)) SymbolKind.Variable else SymbolKind.Value
if (inFn != null) { if (inFn != null) {
val localSym = Symbol(nextId++, text.substring(nameStart, nameEnd), kind, nameStart, nameEnd, containerId = inFn.id, type = null) val localSym = Symbol(nextId++, text.substring(nameStart, nameEnd), kind, nameStart, nameEnd, containerId = inFn.id)
symbols += localSym symbols += localSym
inFn.locals += localSym.id inFn.locals += localSym.id
} else { } else {
val localSym = Symbol(nextId++, text.substring(nameStart, nameEnd), kind, nameStart, nameEnd, containerId = null, type = null) val localSym = Symbol(nextId++, text.substring(nameStart, nameEnd), kind, nameStart, nameEnd, containerId = null)
symbols += localSym symbols += localSym
topLevelByName.getOrPut(localSym.name) { mutableListOf() }.add(localSym.id) topLevelByName.getOrPut(localSym.name) { mutableListOf() }.add(localSym.id)
} }
@@ -42,31 +42,6 @@ object LyngFormatter {
return false return false
} }
private fun isAccessorRelated(code: String): Boolean {
val t = code.trim()
if (t.isEmpty()) return false
if (isPropertyAccessor(t)) return true
// If it contains 'fun' or 'fn' as a word, it's probably a function declaration, not an accessor
if (Regex("\\b(fun|fn)\\b").containsMatchIn(t)) return false
val hasDecl = startsWithWord(t, "var") || startsWithWord(t, "val") ||
startsWithWord(t, "private") || startsWithWord(t, "protected") ||
startsWithWord(t, "override") || startsWithWord(t, "public")
if (hasDecl) {
val getSetMatch = Regex("\\b(get|set)\\b").find(t)
if (getSetMatch != null) {
// Check it's not part of an assignment to the property itself (e.g. val x = get())
val equalIndex = t.indexOf('=')
if (equalIndex == -1 || equalIndex > getSetMatch.range.first) {
return true
}
}
}
return false
}
/** Returns the input with indentation recomputed from scratch, line by line. */ /** Returns the input with indentation recomputed from scratch, line by line. */
fun reindent(text: String, config: LyngFormatConfig = LyngFormatConfig()): String { fun reindent(text: String, config: LyngFormatConfig = LyngFormatConfig()): String {
// Normalize tabs to spaces globally before any transformation; results must contain no tabs // Normalize tabs to spaces globally before any transformation; results must contain no tabs
@@ -107,7 +82,7 @@ object LyngFormatter {
if (isIf || isElseIf || isElse) return true if (isIf || isElseIf || isElse) return true
// property accessors ending with ) or = // property accessors ending with ) or =
if (isAccessorRelated(t)) { if (isPropertyAccessor(t)) {
return if (t.contains('=')) t.endsWith('=') else t.endsWith(')') return if (t.contains('=')) t.endsWith('=') else t.endsWith(')')
} }
return false return false
@@ -193,8 +168,7 @@ object LyngFormatter {
} }
val newBlockLevel = blockLevel val newBlockLevel = blockLevel
if (newBlockLevel > oldBlockLevel) { if (newBlockLevel > oldBlockLevel) {
val isAccessorRelatedLine = isAccessor || (!inBlockComment && isAccessorRelated(code)) val addedThisLine = (if (applyAwaiting) awaitingExtraIndent else 0) + (if (isAccessor) 1 else 0)
val addedThisLine = (if (applyAwaiting) awaitingExtraIndent else 0) + (if (isAccessorRelatedLine) 1 else 0)
repeat(newBlockLevel - oldBlockLevel) { repeat(newBlockLevel - oldBlockLevel) {
extraIndents.add(addedThisLine) extraIndents.add(addedThisLine)
} }
@@ -212,8 +186,7 @@ object LyngFormatter {
val endsWithBrace = code.trimEnd().endsWith("{") val endsWithBrace = code.trimEnd().endsWith("{")
if (!endsWithBrace && isControlHeaderNoBrace(code)) { if (!endsWithBrace && isControlHeaderNoBrace(code)) {
// It's another header, increment // It's another header, increment
val isAccessorRelatedLine = isAccessor || (!inBlockComment && isAccessorRelated(code)) awaitingExtraIndent += if (isAccessor) 2 else 1
awaitingExtraIndent += if (isAccessorRelatedLine) 2 else 1
} else { } else {
// It's the body, reset // It's the body, reset
awaitingExtraIndent = 0 awaitingExtraIndent = 0
@@ -222,8 +195,7 @@ object LyngFormatter {
// start awaiting if current line is a control header without '{' // start awaiting if current line is a control header without '{'
val endsWithBrace = code.trimEnd().endsWith("{") val endsWithBrace = code.trimEnd().endsWith("{")
if (!endsWithBrace && isControlHeaderNoBrace(code)) { if (!endsWithBrace && isControlHeaderNoBrace(code)) {
val isAccessorRelatedLine = isAccessor || (!inBlockComment && isAccessorRelated(code)) awaitingExtraIndent = if (isAccessor) 2 else 1
awaitingExtraIndent = if (isAccessorRelatedLine) 2 else 1
} }
} }
@@ -505,7 +477,7 @@ private fun splitIntoParts(
private fun applyMinimalSpacingRules(code: String): String { private fun applyMinimalSpacingRules(code: String): String {
var s = code var s = code
// Ensure space before '(' for control-flow keywords // Ensure space before '(' for control-flow keywords
s = s.replace(Regex("\\b(if|for|while|return|break|continue)\\("), "$1 (") s = s.replace(Regex("\\b(if|for|while)\\("), "$1 (")
// Space before '{' for control-flow headers only (avoid function declarations) // Space before '{' for control-flow headers only (avoid function declarations)
s = s.replace(Regex("\\b(if|for|while)(\\s*\\([^)]*\\))\\s*\\{"), "$1$2 {") s = s.replace(Regex("\\b(if|for|while)(\\s*\\([^)]*\\))\\s*\\{"), "$1$2 {")
s = s.replace(Regex("\\belse\\s+if(\\s*\\([^)]*\\))\\s*\\{"), "else if$1 {") s = s.replace(Regex("\\belse\\s+if(\\s*\\([^)]*\\))\\s*\\{"), "else if$1 {")
@@ -526,8 +498,6 @@ private fun applyMinimalSpacingRules(code: String): String {
s = s.replace(Regex("(\\bcatch\\s*\\([^)]*\\))\\s*\\{"), "$1 {") s = s.replace(Regex("(\\bcatch\\s*\\([^)]*\\))\\s*\\{"), "$1 {")
// Ensure space before '(' for catch parameter // Ensure space before '(' for catch parameter
s = s.replace(Regex("\\bcatch\\("), "catch (") s = s.replace(Regex("\\bcatch\\("), "catch (")
// Remove space between control keyword and label: return @label -> return@label
s = s.replace(Regex("\\b(return|break|continue)\\s+(@[\\p{L}_][\\p{L}\\p{N}_]*)"), "$1$2")
// Remove spaces just inside parentheses/brackets: "( a )" -> "(a)" // Remove spaces just inside parentheses/brackets: "( a )" -> "(a)"
s = s.replace(Regex("\\(\\s+"), "(") s = s.replace(Regex("\\(\\s+"), "(")
// Do not strip leading indentation before a closing bracket/paren on its own line // Do not strip leading indentation before a closing bracket/paren on its own line
@@ -67,7 +67,7 @@ private fun kindOf(type: Type, value: String): HighlightKind? = when (type) {
Type.REGEX -> HighlightKind.Regex Type.REGEX -> HighlightKind.Regex
// comments // comments
Type.SINGLE_LINE_COMMENT, Type.MULTILINE_COMMENT -> HighlightKind.Comment Type.SINLGE_LINE_COMMENT, Type.MULTILINE_COMMENT -> HighlightKind.Comment
// punctuation // punctuation
Type.LPAREN, Type.RPAREN, Type.LBRACE, Type.RBRACE, Type.LBRACKET, Type.RBRACKET, Type.LPAREN, Type.RPAREN, Type.LBRACE, Type.RBRACE, Type.LBRACKET, Type.RBRACKET,
@@ -82,7 +82,7 @@ private fun kindOf(type: Type, value: String): HighlightKind? = when (type) {
// operators and symbolic constructs // operators and symbolic constructs
Type.PLUS, Type.MINUS, Type.STAR, Type.SLASH, Type.PERCENT, Type.PLUS, Type.MINUS, Type.STAR, Type.SLASH, Type.PERCENT,
Type.ASSIGN, Type.PLUSASSIGN, Type.MINUSASSIGN, Type.STARASSIGN, Type.SLASHASSIGN, Type.PERCENTASSIGN, Type.IFNULLASSIGN, Type.ASSIGN, Type.PLUSASSIGN, Type.MINUSASSIGN, Type.STARASSIGN, Type.SLASHASSIGN, Type.PERCENTASSIGN,
Type.PLUS2, Type.MINUS2, Type.PLUS2, Type.MINUS2,
Type.EQ, Type.NEQ, Type.LT, Type.LTE, Type.GT, Type.GTE, Type.REF_EQ, Type.REF_NEQ, Type.MATCH, Type.NOTMATCH, Type.EQ, Type.NEQ, Type.LT, Type.LTE, Type.GT, Type.GTE, Type.REF_EQ, Type.REF_NEQ, Type.MATCH, Type.NOTMATCH,
Type.DOT, Type.ARROW, Type.EQARROW, Type.QUESTION, Type.COLONCOLON, Type.DOT, Type.ARROW, Type.EQARROW, Type.QUESTION, Type.COLONCOLON,
@@ -148,8 +148,6 @@ class ModuleDocsBuilder internal constructor(private val moduleName: String) {
body = null, body = null,
doc = md, doc = md,
nameStart = Pos.builtIn, nameStart = Pos.builtIn,
isExtern = false,
isStatic = false
) )
} }
@@ -169,8 +167,6 @@ class ModuleDocsBuilder internal constructor(private val moduleName: String) {
initRange = null, initRange = null,
doc = md, doc = md,
nameStart = Pos.builtIn, nameStart = Pos.builtIn,
isExtern = false,
isStatic = false
) )
} }
@@ -240,7 +236,6 @@ class ClassDocsBuilder internal constructor(private val className: String) {
name = name, name = name,
mutable = mutable, mutable = mutable,
type = type?.toMiniTypeRef(), type = type?.toMiniTypeRef(),
initRange = null,
doc = md, doc = md,
nameStart = Pos.builtIn, nameStart = Pos.builtIn,
isStatic = isStatic, isStatic = isStatic,
@@ -255,7 +250,8 @@ class ClassDocsBuilder internal constructor(private val className: String) {
private fun builtinRange() = MiniRange(Pos.builtIn, Pos.builtIn) private fun builtinRange() = MiniRange(Pos.builtIn, Pos.builtIn)
private fun miniDoc(text: String, tags: Map<String, List<String>>): MiniDoc { private fun miniDoc(text: String, tags: Map<String, List<String>>): MiniDoc {
return MiniDoc.parse(builtinRange(), listOf(text), tags) val summary = text.lineSequence().map { it.trim() }.firstOrNull { it.isNotEmpty() }
return MiniDoc(range = builtinRange(), raw = text, summary = summary, tags = tags)
} }
private fun TypeDoc.toDisplayName(): String = when (this) { private fun TypeDoc.toDisplayName(): String = when (this) {
@@ -538,9 +534,6 @@ private fun buildStdlibDocs(): List<MiniDecl> {
) )
// Concurrency helpers // Concurrency helpers
mod.classDoc(name = "Deferred", doc = "Represents a value that will be available in the future.", bases = listOf(type("Obj"))) {
method(name = "await", doc = "Suspend until the value is available and return it.")
}
mod.funDoc( mod.funDoc(
name = "launch", name = "launch",
doc = StdlibInlineDocIndex.topFunDoc("launch") ?: "Launch an asynchronous task and return a `Deferred`.", doc = StdlibInlineDocIndex.topFunDoc("launch") ?: "Launch an asynchronous task and return a `Deferred`.",
@@ -558,33 +551,18 @@ private fun buildStdlibDocs(): List<MiniDecl> {
returns = type("lyng.Iterable") returns = type("lyng.Iterable")
) )
// Common types
mod.classDoc(name = "Int", doc = "64-bit signed integer.", bases = listOf(type("Obj")))
mod.classDoc(name = "Real", doc = "64-bit floating point number.", bases = listOf(type("Obj")))
mod.classDoc(name = "Bool", doc = "Boolean value (true or false).", bases = listOf(type("Obj")))
mod.classDoc(name = "Char", doc = "Single character (UTF-16 code unit).", bases = listOf(type("Obj")))
mod.classDoc(name = "Buffer", doc = "Mutable byte array.", bases = listOf(type("Obj")))
mod.classDoc(name = "Regex", doc = "Regular expression.", bases = listOf(type("Obj")))
mod.classDoc(name = "Range", doc = "Arithmetic progression.", bases = listOf(type("Obj")))
// Common Iterable helpers (document top-level extension-like APIs as class members) // Common Iterable helpers (document top-level extension-like APIs as class members)
mod.classDoc(name = "Iterable", doc = StdlibInlineDocIndex.classDoc("Iterable") ?: "Helper operations for iterable collections.", bases = listOf(type("Obj"))) { mod.classDoc(name = "Iterable", doc = StdlibInlineDocIndex.classDoc("Iterable") ?: "Helper operations for iterable collections.") {
fun md(name: String, fallback: String) = StdlibInlineDocIndex.methodDoc("Iterable", name) ?: fallback fun md(name: String, fallback: String) = StdlibInlineDocIndex.methodDoc("Iterable", name) ?: fallback
method(name = "filter", doc = md("filter", "Filter elements by predicate."), params = listOf(ParamDoc("predicate")), returns = type("lyng.Iterable")) method(name = "filter", doc = md("filter", "Filter elements by predicate."), params = listOf(ParamDoc("predicate")), returns = type("lyng.Iterable"))
method(name = "filterFlow", doc = md("filterFlow", "Filter elements by predicate and return a Flow."), params = listOf(ParamDoc("predicate")), returns = type("lyng.Flow"))
method(name = "filterNotNull", doc = md("filterNotNull", "Filter non-null elements."), returns = type("lyng.List"))
method(name = "drop", doc = md("drop", "Skip the first N elements."), params = listOf(ParamDoc("n", type("lyng.Int"))), returns = type("lyng.Iterable")) method(name = "drop", doc = md("drop", "Skip the first N elements."), params = listOf(ParamDoc("n", type("lyng.Int"))), returns = type("lyng.Iterable"))
field(name = "first", doc = md("first", "Return the first element or throw if empty.")) method(name = "first", doc = md("first", "Return the first element or throw if empty."))
field(name = "last", doc = md("last", "Return the last element or throw if empty.")) method(name = "last", doc = md("last", "Return the last element or throw if empty."))
method(name = "findFirst", doc = md("findFirst", "Return the first matching element or throw."), params = listOf(ParamDoc("predicate")))
method(name = "findFirstOrNull", doc = md("findFirstOrNull", "Return the first matching element or null."), params = listOf(ParamDoc("predicate")))
method(name = "dropLast", doc = md("dropLast", "Drop the last N elements."), params = listOf(ParamDoc("n", type("lyng.Int"))), returns = type("lyng.Iterable")) method(name = "dropLast", doc = md("dropLast", "Drop the last N elements."), params = listOf(ParamDoc("n", type("lyng.Int"))), returns = type("lyng.Iterable"))
method(name = "takeLast", doc = md("takeLast", "Take the last N elements."), params = listOf(ParamDoc("n", type("lyng.Int"))), returns = type("lyng.List")) method(name = "takeLast", doc = md("takeLast", "Take the last N elements."), params = listOf(ParamDoc("n", type("lyng.Int"))), returns = type("lyng.List"))
method(name = "joinToString", doc = md("joinToString", "Join elements into a string with an optional separator and transformer."), params = listOf(ParamDoc("separator", type("lyng.String")), ParamDoc("transformer")), returns = type("lyng.String")) method(name = "joinToString", doc = md("joinToString", "Join elements into a string with an optional separator and transformer."), params = listOf(ParamDoc("prefix", type("lyng.String")), ParamDoc("transformer")), returns = type("lyng.String"))
method(name = "any", doc = md("any", "Return true if any element matches the predicate."), params = listOf(ParamDoc("predicate")), returns = type("lyng.Bool")) method(name = "any", doc = md("any", "Return true if any element matches the predicate."), params = listOf(ParamDoc("predicate")), returns = type("lyng.Bool"))
method(name = "all", doc = md("all", "Return true if all elements match the predicate."), params = listOf(ParamDoc("predicate")), returns = type("lyng.Bool")) method(name = "all", doc = md("all", "Return true if all elements match the predicate."), params = listOf(ParamDoc("predicate")), returns = type("lyng.Bool"))
method(name = "forEach", doc = md("forEach", "Execute `action` for each element."), params = listOf(ParamDoc("action")))
method(name = "count", doc = md("count", "Count elements matching the predicate."), params = listOf(ParamDoc("predicate")), returns = type("lyng.Int"))
method(name = "sum", doc = md("sum", "Sum all elements; returns null for empty collections."), returns = type("lyng.Number", nullable = true)) method(name = "sum", doc = md("sum", "Sum all elements; returns null for empty collections."), returns = type("lyng.Number", nullable = true))
method(name = "sumOf", doc = md("sumOf", "Sum mapped values of elements; returns null for empty collections."), params = listOf(ParamDoc("f"))) method(name = "sumOf", doc = md("sumOf", "Sum mapped values of elements; returns null for empty collections."), params = listOf(ParamDoc("f")))
method(name = "minOf", doc = md("minOf", "Minimum of mapped values."), params = listOf(ParamDoc("lambda"))) method(name = "minOf", doc = md("minOf", "Minimum of mapped values."), params = listOf(ParamDoc("lambda")))
@@ -613,7 +591,7 @@ private fun buildStdlibDocs(): List<MiniDecl> {
} }
// Iterator helpers // Iterator helpers
mod.classDoc(name = "Iterator", doc = StdlibInlineDocIndex.classDoc("Iterator") ?: "Iterator protocol for sequential access.", bases = listOf(type("Obj"))) { mod.classDoc(name = "Iterator", doc = StdlibInlineDocIndex.classDoc("Iterator") ?: "Iterator protocol for sequential access.") {
fun md(name: String, fallback: String) = StdlibInlineDocIndex.methodDoc("Iterator", name) ?: fallback fun md(name: String, fallback: String) = StdlibInlineDocIndex.methodDoc("Iterator", name) ?: fallback
method(name = "hasNext", doc = md("hasNext", "Whether another element is available."), returns = type("lyng.Bool")) method(name = "hasNext", doc = md("hasNext", "Whether another element is available."), returns = type("lyng.Bool"))
method(name = "next", doc = md("next", "Return the next element.")) method(name = "next", doc = md("next", "Return the next element."))
@@ -622,22 +600,22 @@ private fun buildStdlibDocs(): List<MiniDecl> {
} }
// Exceptions and utilities // Exceptions and utilities
mod.classDoc(name = "Exception", doc = StdlibInlineDocIndex.classDoc("Exception") ?: "Exception helpers.", bases = listOf(type("Obj"))) { mod.classDoc(name = "Exception", doc = StdlibInlineDocIndex.classDoc("Exception") ?: "Exception helpers.") {
method(name = "printStackTrace", doc = StdlibInlineDocIndex.methodDoc("Exception", "printStackTrace") ?: "Print this exception and its stack trace to standard output.") method(name = "printStackTrace", doc = StdlibInlineDocIndex.methodDoc("Exception", "printStackTrace") ?: "Print this exception and its stack trace to standard output.")
} }
mod.classDoc(name = "Enum", doc = StdlibInlineDocIndex.classDoc("Enum") ?: "Base class for all enums.", bases = listOf(type("Obj"))) { mod.classDoc(name = "Enum", doc = StdlibInlineDocIndex.classDoc("Enum") ?: "Base class for all enums.") {
method(name = "name", doc = "Returns the name of this enum constant.", returns = type("lyng.String")) method(name = "name", doc = "Returns the name of this enum constant.", returns = type("lyng.String"))
method(name = "ordinal", doc = "Returns the ordinal of this enum constant.", returns = type("lyng.Int")) method(name = "ordinal", doc = "Returns the ordinal of this enum constant.", returns = type("lyng.Int"))
} }
mod.classDoc(name = "String", doc = StdlibInlineDocIndex.classDoc("String") ?: "String helpers.", bases = listOf(type("Obj"))) { mod.classDoc(name = "String", doc = StdlibInlineDocIndex.classDoc("String") ?: "String helpers.") {
// Only include inline-source method here; Kotlin-embedded methods are now documented via DocHelpers near definitions. // Only include inline-source method here; Kotlin-embedded methods are now documented via DocHelpers near definitions.
method(name = "re", doc = StdlibInlineDocIndex.methodDoc("String", "re") ?: "Compile this string into a regular expression.", params = listOf(ParamDoc("flags", type("lyng.String"))), returns = type("lyng.Regex")) method(name = "re", doc = StdlibInlineDocIndex.methodDoc("String", "re") ?: "Compile this string into a regular expression.", returns = type("lyng.Regex"))
} }
// StackTraceEntry structure // StackTraceEntry structure
mod.classDoc(name = "StackTraceEntry", doc = StdlibInlineDocIndex.classDoc("StackTraceEntry") ?: "Represents a single stack trace element.", bases = listOf(type("Obj"))) { mod.classDoc(name = "StackTraceEntry", doc = StdlibInlineDocIndex.classDoc("StackTraceEntry") ?: "Represents a single stack trace element.") {
// Fields are not present as declarations in root.lyng's class header docs. Keep seeded defaults. // Fields are not present as declarations in root.lyng's class header docs. Keep seeded defaults.
field(name = "sourceName", doc = "Source (file) name.", type = type("lyng.String")) field(name = "sourceName", doc = "Source (file) name.", type = type("lyng.String"))
field(name = "line", doc = "Line number (1-based).", type = type("lyng.Int")) field(name = "line", doc = "Line number (1-based).", type = type("lyng.Int"))
@@ -23,8 +23,6 @@ package net.sergeych.lyng.miniast
import net.sergeych.lyng.Compiler import net.sergeych.lyng.Compiler
import net.sergeych.lyng.Script import net.sergeych.lyng.Script
import net.sergeych.lyng.Source import net.sergeych.lyng.Source
import net.sergeych.lyng.binding.BindingSnapshot
import net.sergeych.lyng.highlight.offsetOf
import net.sergeych.lyng.pacman.ImportProvider import net.sergeych.lyng.pacman.ImportProvider
/** Minimal completion item description (IDE-agnostic). */ /** Minimal completion item description (IDE-agnostic). */
@@ -33,7 +31,6 @@ data class CompletionItem(
val kind: Kind, val kind: Kind,
val tailText: String? = null, val tailText: String? = null,
val typeText: String? = null, val typeText: String? = null,
val priority: Double = 0.0,
) )
enum class Kind { Function, Class_, Enum, Value, Method, Field } enum class Kind { Function, Class_, Enum, Value, Method, Field }
@@ -60,11 +57,11 @@ object CompletionEngineLight {
return completeSuspend(text, idx) return completeSuspend(text, idx)
} }
suspend fun completeSuspend(text: String, caret: Int, providedMini: MiniScript? = null, binding: BindingSnapshot? = null): List<CompletionItem> { suspend fun completeSuspend(text: String, caret: Int): List<CompletionItem> {
// Ensure stdlib Obj*-defined docs (e.g., String methods) are initialized before registry lookup // Ensure stdlib Obj*-defined docs (e.g., String methods) are initialized before registry lookup
StdlibDocsBootstrap.ensure() StdlibDocsBootstrap.ensure()
val prefix = prefixAt(text, caret) val prefix = prefixAt(text, caret)
val mini = providedMini ?: buildMiniAst(text) val mini = buildMiniAst(text)
val imported: List<String> = DocLookupUtils.canonicalImportedModules(mini ?: return emptyList(), text) val imported: List<String> = DocLookupUtils.canonicalImportedModules(mini ?: return emptyList(), text)
val cap = 200 val cap = 200
@@ -74,12 +71,7 @@ object CompletionEngineLight {
val word = DocLookupUtils.wordRangeAt(text, caret) val word = DocLookupUtils.wordRangeAt(text, caret)
val memberDot = DocLookupUtils.findDotLeft(text, word?.first ?: caret) val memberDot = DocLookupUtils.findDotLeft(text, word?.first ?: caret)
if (memberDot != null) { if (memberDot != null) {
val inferredCls = (DocLookupUtils.guessReturnClassFromMemberCallBeforeMini(mini, text, memberDot, imported, binding) ?: DocLookupUtils.guessReceiverClass(text, memberDot, imported, mini))
// 0) Try chained member call return type inference // 0) Try chained member call return type inference
DocLookupUtils.guessReturnClassFromMemberCallBeforeMini(mini, text, memberDot, imported, binding)?.let { cls ->
offerMembersAdd(out, prefix, imported, cls, mini)
return out
}
DocLookupUtils.guessReturnClassFromMemberCallBefore(text, memberDot, imported, mini)?.let { cls -> DocLookupUtils.guessReturnClassFromMemberCallBefore(text, memberDot, imported, mini)?.let { cls ->
offerMembersAdd(out, prefix, imported, cls, mini) offerMembersAdd(out, prefix, imported, cls, mini)
return out return out
@@ -95,7 +87,7 @@ object CompletionEngineLight {
return out return out
} }
// 1) Receiver inference fallback // 1) Receiver inference fallback
(DocLookupUtils.guessReceiverClassViaMini(mini, text, memberDot, imported, binding) ?: DocLookupUtils.guessReceiverClass(text, memberDot, imported, mini))?.let { cls -> (DocLookupUtils.guessReceiverClassViaMini(mini, text, memberDot, imported) ?: DocLookupUtils.guessReceiverClass(text, memberDot, imported, mini))?.let { cls ->
offerMembersAdd(out, prefix, imported, cls, mini) offerMembersAdd(out, prefix, imported, cls, mini)
return out return out
} }
@@ -104,16 +96,8 @@ object CompletionEngineLight {
} }
// Global identifiers: params > local decls > imported > stdlib; Functions > Classes > Values; alphabetical // Global identifiers: params > local decls > imported > stdlib; Functions > Classes > Values; alphabetical
offerParamsInScope(out, prefix, mini, text, caret) mini?.let { m ->
val decls = m.declarations
val locals = DocLookupUtils.extractLocalsAt(text, caret)
for (name in locals) {
if (name.startsWith(prefix, true)) {
out.add(CompletionItem(name, Kind.Value, priority = 150.0))
}
}
val decls = mini.declarations
val funs = decls.filterIsInstance<MiniFunDecl>().sortedBy { it.name.lowercase() } val funs = decls.filterIsInstance<MiniFunDecl>().sortedBy { it.name.lowercase() }
val classes = decls.filterIsInstance<MiniClassDecl>().sortedBy { it.name.lowercase() } val classes = decls.filterIsInstance<MiniClassDecl>().sortedBy { it.name.lowercase() }
val enums = decls.filterIsInstance<MiniEnumDecl>().sortedBy { it.name.lowercase() } val enums = decls.filterIsInstance<MiniEnumDecl>().sortedBy { it.name.lowercase() }
@@ -122,6 +106,7 @@ object CompletionEngineLight {
classes.forEach { offerDeclAdd(out, prefix, it) } classes.forEach { offerDeclAdd(out, prefix, it) }
enums.forEach { offerDeclAdd(out, prefix, it) } enums.forEach { offerDeclAdd(out, prefix, it) }
vals.forEach { offerDeclAdd(out, prefix, it) } vals.forEach { offerDeclAdd(out, prefix, it) }
}
// Imported and builtin // Imported and builtin
val (nonStd, std) = imported.partition { it != "lyng.stdlib" } val (nonStd, std) = imported.partition { it != "lyng.stdlib" }
@@ -147,74 +132,6 @@ object CompletionEngineLight {
// --- Emission helpers --- // --- Emission helpers ---
private fun offerParamsInScope(out: MutableList<CompletionItem>, prefix: String, mini: MiniScript, text: String, caret: Int) {
val src = mini.range.start.source
val already = mutableSetOf<String>()
fun add(ci: CompletionItem) {
if (ci.name.startsWith(prefix, true) && already.add(ci.name)) {
out.add(ci)
}
}
fun checkNode(node: Any) {
val range: MiniRange = when (node) {
is MiniDecl -> node.range
is MiniMemberDecl -> node.range
else -> return
}
val start = src.offsetOf(range.start)
val end = src.offsetOf(range.end).coerceAtMost(text.length)
if (caret in start..end) {
when (node) {
is MiniFunDecl -> {
for (p in node.params) {
add(CompletionItem(p.name, Kind.Value, typeText = typeOf(p.type), priority = 200.0))
}
}
is MiniClassDecl -> {
// Propose constructor parameters (ctorFields)
for (p in node.ctorFields) {
add(CompletionItem(p.name, if (p.mutable) Kind.Value else Kind.Field, typeText = typeOf(p.type), priority = 200.0))
}
// Propose class-level fields
for (p in node.classFields) {
add(CompletionItem(p.name, if (p.mutable) Kind.Value else Kind.Field, typeText = typeOf(p.type), priority = 100.0))
}
// Process members (methods/fields)
for (m in node.members) {
// If the member itself contains the caret (like a method), recurse
checkNode(m)
// Also offer the member itself for the class scope
when (m) {
is MiniMemberFunDecl -> {
val params = m.params.joinToString(", ") { it.name }
add(CompletionItem(m.name, Kind.Method, tailText = "(${params})", typeText = typeOf(m.returnType), priority = 100.0))
}
is MiniMemberValDecl -> {
add(CompletionItem(m.name, if (m.mutable) Kind.Value else Kind.Field, typeText = typeOf(m.type), priority = 100.0))
}
is MiniInitDecl -> {}
}
}
}
is MiniMemberFunDecl -> {
for (p in node.params) {
add(CompletionItem(p.name, Kind.Value, typeText = typeOf(p.type), priority = 200.0))
}
}
else -> {}
}
}
}
for (decl in mini.declarations) {
checkNode(decl)
}
}
private fun offerDeclAdd(out: MutableList<CompletionItem>, prefix: String, d: MiniDecl) { private fun offerDeclAdd(out: MutableList<CompletionItem>, prefix: String, d: MiniDecl) {
fun add(ci: CompletionItem) { if (ci.name.startsWith(prefix, true)) out += ci } fun add(ci: CompletionItem) { if (ci.name.startsWith(prefix, true)) out += ci }
when (d) { when (d) {
@@ -239,9 +156,6 @@ object CompletionEngineLight {
fun addMembersOf(name: String, direct: Boolean) { fun addMembersOf(name: String, direct: Boolean) {
val cls = classes[name] ?: return val cls = classes[name] ?: return
val target = if (direct) directMap else inheritedMap val target = if (direct) directMap else inheritedMap
for (cf in cls.ctorFields + cls.classFields) {
target.getOrPut(cf.name) { mutableListOf() }.add(DocLookupUtils.toMemberVal(cf))
}
for (m in cls.members) target.getOrPut(m.name) { mutableListOf() }.add(m) for (m in cls.members) target.getOrPut(m.name) { mutableListOf() }.add(m)
for (b in cls.bases) if (visited.add(b)) addMembersOf(b, false) for (b in cls.bases) if (visited.add(b)) addMembersOf(b, false)
} }
@@ -255,21 +169,13 @@ object CompletionEngineLight {
"Array" -> listOf("Collection", "Iterable").forEach { if (visited.add(it)) addMembersOf(it, false) } "Array" -> listOf("Collection", "Iterable").forEach { if (visited.add(it)) addMembersOf(it, false) }
} }
fun emitGroup(map: LinkedHashMap<String, MutableList<MiniMemberDecl>>, groupPriority: Double) { fun emitGroup(map: LinkedHashMap<String, MutableList<MiniMemberDecl>>) {
for (name in map.keys.sortedBy { it.lowercase() }) { for (name in map.keys.sortedBy { it.lowercase() }) {
val variants = map[name] ?: continue val variants = map[name] ?: continue
// Choose a representative for display: // Prefer a method with a known return type; else any method; else first variant
// 1) Prefer a method with return type AND parameters
// 2) Prefer a method with parameters
// 3) Prefer a method with return type
// 4) Else any method
// 5) Else the first variant
val rep = val rep =
variants.asSequence().filterIsInstance<MiniMemberFunDecl>() variants.asSequence()
.firstOrNull { it.returnType != null && it.params.isNotEmpty() } .filterIsInstance<MiniMemberFunDecl>()
?: variants.asSequence().filterIsInstance<MiniMemberFunDecl>()
.firstOrNull { it.params.isNotEmpty() }
?: variants.asSequence().filterIsInstance<MiniMemberFunDecl>()
.firstOrNull { it.returnType != null } .firstOrNull { it.returnType != null }
?: variants.firstOrNull { it is MiniMemberFunDecl } ?: variants.firstOrNull { it is MiniMemberFunDecl }
?: variants.first() ?: variants.first()
@@ -278,7 +184,7 @@ object CompletionEngineLight {
val params = rep.params.joinToString(", ") { it.name } val params = rep.params.joinToString(", ") { it.name }
val extra = variants.count { it is MiniMemberFunDecl } - 1 val extra = variants.count { it is MiniMemberFunDecl } - 1
val ov = if (extra > 0) " (+$extra overloads)" else "" val ov = if (extra > 0) " (+$extra overloads)" else ""
val ci = CompletionItem(name, Kind.Method, tailText = "(${params})$ov", typeText = typeOf(rep.returnType), priority = groupPriority) val ci = CompletionItem(name, Kind.Method, tailText = "(${params})$ov", typeText = typeOf(rep.returnType))
if (ci.name.startsWith(prefix, true)) out += ci if (ci.name.startsWith(prefix, true)) out += ci
} }
is MiniMemberValDecl -> { is MiniMemberValDecl -> {
@@ -286,7 +192,7 @@ object CompletionEngineLight {
val chosen = variants.asSequence() val chosen = variants.asSequence()
.filterIsInstance<MiniMemberValDecl>() .filterIsInstance<MiniMemberValDecl>()
.firstOrNull { it.type != null } ?: rep .firstOrNull { it.type != null } ?: rep
val ci = CompletionItem(name, Kind.Field, typeText = typeOf(chosen.type), priority = groupPriority) val ci = CompletionItem(name, Kind.Field, typeText = typeOf((chosen as MiniMemberValDecl).type))
if (ci.name.startsWith(prefix, true)) out += ci if (ci.name.startsWith(prefix, true)) out += ci
} }
is MiniInitDecl -> {} is MiniInitDecl -> {}
@@ -294,45 +200,40 @@ object CompletionEngineLight {
} }
} }
emitGroup(directMap, 100.0) emitGroup(directMap)
emitGroup(inheritedMap, 0.0) emitGroup(inheritedMap)
// Supplement with extension members (both stdlib and local) // Supplement with stdlib extension members defined in root.lyng (e.g., fun String.re(...))
run { run {
val already = (directMap.keys + inheritedMap.keys).toMutableSet() val already = (directMap.keys + inheritedMap.keys).toMutableSet()
val extensions = DocLookupUtils.collectExtensionMemberNames(imported, className, mini) val ext = BuiltinDocRegistry.extensionMemberNamesFor(className)
for (name in extensions) { for (name in ext) {
if (already.contains(name)) continue if (already.contains(name)) continue
val resolved = DocLookupUtils.resolveMemberWithInheritance(imported, className, name, mini) val resolved = DocLookupUtils.resolveMemberWithInheritance(imported, className, name)
if (resolved != null) { if (resolved != null) {
val m = resolved.second when (val member = resolved.second) {
val ci = when (m) {
is MiniMemberFunDecl -> { is MiniMemberFunDecl -> {
val params = m.params.joinToString(", ") { it.name } val params = member.params.joinToString(", ") { it.name }
CompletionItem(name, Kind.Method, tailText = "(${params})", typeText = typeOf(m.returnType), priority = 50.0) val ci = CompletionItem(name, Kind.Method, tailText = "(${params})", typeText = typeOf(member.returnType))
} if (ci.name.startsWith(prefix, true)) out += ci
is MiniFunDecl -> {
val params = m.params.joinToString(", ") { it.name }
CompletionItem(name, Kind.Method, tailText = "(${params})", typeText = typeOf(m.returnType), priority = 50.0)
}
is MiniMemberValDecl -> CompletionItem(name, Kind.Field, typeText = typeOf(m.type), priority = 50.0)
is MiniValDecl -> CompletionItem(name, Kind.Field, typeText = typeOf(m.type), priority = 50.0)
else -> CompletionItem(name, Kind.Method, tailText = "()", typeText = null, priority = 50.0)
}
if (ci.name.startsWith(prefix, true)) {
out += ci
already.add(name) already.add(name)
} }
is MiniMemberValDecl -> {
val ci = CompletionItem(name, Kind.Field, typeText = typeOf(member.type))
if (ci.name.startsWith(prefix, true)) out += ci
already.add(name)
}
is MiniInitDecl -> {}
}
} else { } else {
val ci = CompletionItem(name, Kind.Method, tailText = "()", typeText = null, priority = 50.0) // Fallback: emit simple method name without detailed types
if (ci.name.startsWith(prefix, true)) { val ci = CompletionItem(name, Kind.Method, tailText = "()", typeText = null)
out += ci if (ci.name.startsWith(prefix, true)) out += ci
already.add(name) already.add(name)
} }
} }
} }
} }
}
// --- Inference helpers (text-only, PSI-free) --- // --- Inference helpers (text-only, PSI-free) ---
@@ -348,9 +249,16 @@ object CompletionEngineLight {
} }
} }
private fun typeOf(t: MiniTypeRef?): String { private fun typeOf(t: MiniTypeRef?): String = when (t) {
val s = DocLookupUtils.typeOf(t) null -> ""
return if (s.isEmpty()) "" else ": $s" is MiniTypeName -> t.segments.lastOrNull()?.name?.let { ": $it" } ?: ""
is MiniGenericType -> {
val base = typeOf(t.base).removePrefix(": ")
val args = t.args.joinToString(",") { typeOf(it).removePrefix(": ") }
": ${base}<${args}>"
}
is MiniFunctionType -> ": (fn)"
is MiniTypeVar -> ": ${t.name}"
} }
// Note: we intentionally skip "params in scope" in the isolated engine to avoid PSI/offset mapping. // Note: we intentionally skip "params in scope" in the isolated engine to avoid PSI/offset mapping.
@@ -78,11 +78,6 @@ object DocLookupUtils {
} }
for (m in members) { for (m in members) {
if (m is MiniMemberFunDecl) {
for (p in m.params) {
if (matches(p.nameStart, p.name.length)) return p.name to "Parameter"
}
}
if (matches(m.nameStart, m.name.length)) { if (matches(m.nameStart, m.name.length)) {
val kind = when (m) { val kind = when (m) {
is MiniMemberFunDecl -> "Function" is MiniMemberFunDecl -> "Function"
@@ -96,28 +91,14 @@ object DocLookupUtils {
return null return null
} }
fun findEnclosingClass(mini: MiniScript, offset: Int): MiniClassDecl? { fun findTypeByRange(mini: MiniScript?, name: String, startOffset: Int): MiniTypeRef? {
val src = mini.range.start.source
return mini.declarations.filterIsInstance<MiniClassDecl>()
.filter {
val start = src.offsetOf(it.range.start)
val end = src.offsetOf(it.range.end)
offset in start..end
}
.minByOrNull {
src.offsetOf(it.range.end) - src.offsetOf(it.range.start)
}
}
fun findTypeByRange(mini: MiniScript?, name: String, startOffset: Int, text: String? = null, imported: List<String>? = null): MiniTypeRef? {
if (mini == null) return null if (mini == null) return null
val src = mini.range.start.source val src = mini.range.start.source
fun matches(p: net.sergeych.lyng.Pos, len: Int) = src.offsetOf(p).let { s -> startOffset >= s && startOffset < s + len }
for (d in mini.declarations) { for (d in mini.declarations) {
if (d.name == name && matches(d.nameStart, d.name.length)) { if (d.name == name && src.offsetOf(d.nameStart) == startOffset) {
return when (d) { return when (d) {
is MiniValDecl -> d.type ?: if (text != null && imported != null) inferTypeRefForVal(d, text, imported, mini) else null is MiniValDecl -> d.type
is MiniFunDecl -> d.returnType is MiniFunDecl -> d.returnType
else -> null else -> null
} }
@@ -125,33 +106,25 @@ object DocLookupUtils {
if (d is MiniFunDecl) { if (d is MiniFunDecl) {
for (p in d.params) { for (p in d.params) {
if (p.name == name && matches(p.nameStart, p.name.length)) return p.type if (p.name == name && src.offsetOf(p.nameStart) == startOffset) return p.type
} }
} }
if (d is MiniClassDecl) { if (d is MiniClassDecl) {
for (m in d.members) { for (m in d.members) {
if (m is MiniMemberFunDecl) { if (m.name == name && src.offsetOf(m.nameStart) == startOffset) {
for (p in m.params) {
if (p.name == name && matches(p.nameStart, p.name.length)) return p.type
}
}
if (m.name == name && matches(m.nameStart, m.name.length)) {
return when (m) { return when (m) {
is MiniMemberFunDecl -> m.returnType is MiniMemberFunDecl -> m.returnType
is MiniMemberValDecl -> m.type ?: if (text != null && imported != null) { is MiniMemberValDecl -> m.type
inferTypeRefFromInitRange(m.initRange, m.nameStart, text, imported, mini)
} else null
else -> null else -> null
} }
} }
} }
for (cf in d.ctorFields) { for (cf in d.ctorFields) {
if (cf.name == name && matches(cf.nameStart, cf.name.length)) return cf.type if (cf.name == name && src.offsetOf(cf.nameStart) == startOffset) return cf.type
} }
for (cf in d.classFields) { for (cf in d.classFields) {
if (cf.name == name && matches(cf.nameStart, cf.name.length)) return cf.type if (cf.name == name && src.offsetOf(cf.nameStart) == startOffset) return cf.type
} }
} }
} }
@@ -184,21 +157,6 @@ object DocLookupUtils {
return result.toList() return result.toList()
} }
fun extractLocalsAt(text: String, offset: Int): Set<String> {
val res = mutableSetOf<String>()
// 1) find val/var declarations
val re = Regex("(?:^|[\\n;])\\s*(?:val|var)\\s+([A-Za-z_][A-Za-z0-9_]*)")
re.findAll(text).forEach { m ->
if (m.range.first < offset) res.add(m.groupValues[1])
}
// 2) find implicit assignments
val re2 = Regex("(?:^|[\\n;])\\s*([A-Za-z_][A-Za-z0-9_]*)\\s*=[^=]")
re2.findAll(text).forEach { m ->
if (m.range.first < offset) res.add(m.groupValues[1])
}
return res
}
fun extractImportsFromText(text: String): List<String> { fun extractImportsFromText(text: String): List<String> {
val result = LinkedHashSet<String>() val result = LinkedHashSet<String>()
val re = Regex("^\\s*import\\s+([a-zA-Z_][a-zA-Z0-9_]*(?:\\.[a-zA-Z_][a-zA-Z0-9_]*)*)", RegexOption.MULTILINE) val re = Regex("^\\s*import\\s+([a-zA-Z_][a-zA-Z0-9_]*(?:\\.[a-zA-Z_][a-zA-Z0-9_]*)*)", RegexOption.MULTILINE)
@@ -246,14 +204,10 @@ object DocLookupUtils {
val rep = list.first() val rep = list.first()
val bases = LinkedHashSet<String>() val bases = LinkedHashSet<String>()
val members = LinkedHashMap<String, MutableList<MiniMemberDecl>>() val members = LinkedHashMap<String, MutableList<MiniMemberDecl>>()
val ctorFields = LinkedHashMap<String, MiniCtorField>()
val classFields = LinkedHashMap<String, MiniCtorField>()
var doc: MiniDoc? = null var doc: MiniDoc? = null
for (c in list) { for (c in list) {
bases.addAll(c.bases) bases.addAll(c.bases)
if (doc == null && c.doc != null && c.doc.raw.isNotBlank()) doc = c.doc if (doc == null && c.doc != null && c.doc.raw.isNotBlank()) doc = c.doc
for (cf in c.ctorFields) ctorFields[cf.name] = cf
for (cf in c.classFields) classFields[cf.name] = cf
for (m in c.members) { for (m in c.members) {
// Group by name to keep overloads together // Group by name to keep overloads together
members.getOrPut(m.name) { mutableListOf() }.add(m) members.getOrPut(m.name) { mutableListOf() }.add(m)
@@ -266,8 +220,8 @@ object DocLookupUtils {
name = rep.name, name = rep.name,
bases = bases.toList(), bases = bases.toList(),
bodyRange = rep.bodyRange, bodyRange = rep.bodyRange,
ctorFields = ctorFields.values.toList(), ctorFields = rep.ctorFields,
classFields = classFields.values.toList(), classFields = rep.classFields,
doc = doc, doc = doc,
nameStart = rep.nameStart, nameStart = rep.nameStart,
members = mergedMembers members = mergedMembers
@@ -278,88 +232,24 @@ object DocLookupUtils {
for ((name, list) in buckets) { for ((name, list) in buckets) {
result[name] = mergeClassDecls(name, list) result[name] = mergeClassDecls(name, list)
} }
// Root object alias
if (result.containsKey("Obj") && !result.containsKey("Any")) {
result["Any"] = result["Obj"]!!
}
return result return result
} }
internal fun toMemberVal(cf: MiniCtorField): MiniMemberValDecl = MiniMemberValDecl( fun resolveMemberWithInheritance(importedModules: List<String>, className: String, member: String, localMini: MiniScript? = null): Pair<String, MiniMemberDecl>? {
range = MiniRange(cf.nameStart, cf.nameStart),
name = cf.name,
mutable = cf.mutable,
type = cf.type,
initRange = null,
doc = null,
nameStart = cf.nameStart,
isStatic = false,
isExtern = false
)
fun resolveMemberWithInheritance(importedModules: List<String>, className: String, member: String, localMini: MiniScript? = null): Pair<String, MiniNamedDecl>? {
val classes = aggregateClasses(importedModules, localMini) val classes = aggregateClasses(importedModules, localMini)
fun dfs(name: String, visited: MutableSet<String>): Pair<String, MiniNamedDecl>? { fun dfs(name: String, visited: MutableSet<String>): Pair<String, MiniMemberDecl>? {
if (!visited.add(name)) return null val cls = classes[name] ?: return null
val cls = classes[name]
if (cls != null) {
cls.members.firstOrNull { it.name == member }?.let { return name to it } cls.members.firstOrNull { it.name == member }?.let { return name to it }
cls.ctorFields.firstOrNull { it.name == member }?.let { return name to toMemberVal(it) } if (!visited.add(name)) return null
cls.classFields.firstOrNull { it.name == member }?.let { return name to toMemberVal(it) }
for (baseName in cls.bases) { for (baseName in cls.bases) {
dfs(baseName, visited)?.let { return it } dfs(baseName, visited)?.let { return it }
} }
}
// 1) local extensions in this class or bases
localMini?.declarations?.firstOrNull { d ->
(d is MiniFunDecl && d.receiver != null && simpleClassNameOf(d.receiver) == name && d.name == member) ||
(d is MiniValDecl && d.receiver != null && simpleClassNameOf(d.receiver) == name && d.name == member)
}?.let { return name to it as MiniNamedDecl }
// 2) built-in extensions from BuiltinDocRegistry
for (mod in importedModules) {
val decls = BuiltinDocRegistry.docsForModule(mod)
decls.firstOrNull { d ->
(d is MiniFunDecl && d.receiver != null && simpleClassNameOf(d.receiver) == name && d.name == member) ||
(d is MiniValDecl && d.receiver != null && simpleClassNameOf(d.receiver) == name && d.name == member)
}?.let { return name to it as MiniNamedDecl }
}
return null return null
} }
return dfs(className, mutableSetOf()) return dfs(className, mutableSetOf())
} }
fun collectExtensionMemberNames(importedModules: List<String>, className: String, localMini: MiniScript? = null): Set<String> { fun findMemberAcrossClasses(importedModules: List<String>, member: String, localMini: MiniScript? = null): Pair<String, MiniMemberDecl>? {
val classes = aggregateClasses(importedModules, localMini)
val visited = mutableSetOf<String>()
val result = mutableSetOf<String>()
fun dfs(name: String) {
if (!visited.add(name)) return
// 1) stdlib extensions from BuiltinDocRegistry
result.addAll(BuiltinDocRegistry.extensionMemberNamesFor(name))
// 2) local extensions from mini
localMini?.declarations?.forEach { d ->
if (d is MiniFunDecl && d.receiver != null && simpleClassNameOf(d.receiver) == name) result.add(d.name)
if (d is MiniValDecl && d.receiver != null && simpleClassNameOf(d.receiver) == name) result.add(d.name)
}
// 3) bases
classes[name]?.bases?.forEach { dfs(it) }
}
dfs(className)
// Hardcoded supplements for common containers if not explicitly in bases
if (className == "List" || className == "Array") {
dfs("Collection")
dfs("Iterable")
}
dfs("Any")
dfs("Obj")
return result
}
fun findMemberAcrossClasses(importedModules: List<String>, member: String, localMini: MiniScript? = null): Pair<String, MiniNamedDecl>? {
val classes = aggregateClasses(importedModules, localMini) val classes = aggregateClasses(importedModules, localMini)
// Preferred order for ambiguous common ops // Preferred order for ambiguous common ops
val preference = listOf("Iterable", "Iterator", "List") val preference = listOf("Iterable", "Iterator", "List")
@@ -411,12 +301,6 @@ object DocLookupUtils {
if (mini == null) return null if (mini == null) return null
val i = prevNonWs(text, dotPos - 1) val i = prevNonWs(text, dotPos - 1)
if (i < 0) return null if (i < 0) return null
// Handle indexing x[0]. or literal [1].
if (text[i] == ']') {
return guessReceiverClass(text, dotPos, imported, mini)
}
val wordRange = wordRangeAt(text, i + 1) ?: return null val wordRange = wordRangeAt(text, i + 1) ?: return null
val ident = text.substring(wordRange.first, wordRange.second) val ident = text.substring(wordRange.first, wordRange.second)
@@ -426,14 +310,14 @@ object DocLookupUtils {
if (ref != null) { if (ref != null) {
val sym = binding.symbols.firstOrNull { it.id == ref.symbolId } val sym = binding.symbols.firstOrNull { it.id == ref.symbolId }
if (sym != null) { if (sym != null) {
val type = findTypeByRange(mini, sym.name, sym.declStart, text, imported) val type = findTypeByRange(mini, sym.name, sym.declStart)
simpleClassNameOf(type)?.let { return it } simpleClassNameOf(type)?.let { return it }
} }
} else { } else {
// Check if it's a declaration (e.g. static access to a class) // Check if it's a declaration (e.g. static access to a class)
val sym = binding.symbols.firstOrNull { it.declStart == wordRange.first && it.name == ident } val sym = binding.symbols.firstOrNull { it.declStart == wordRange.first && it.name == ident }
if (sym != null) { if (sym != null) {
val type = findTypeByRange(mini, sym.name, sym.declStart, text, imported) val type = findTypeByRange(mini, sym.name, sym.declStart)
simpleClassNameOf(type)?.let { return it } simpleClassNameOf(type)?.let { return it }
// if it's a class/enum, return its name directly // if it's a class/enum, return its name directly
if (sym.kind == net.sergeych.lyng.binding.SymbolKind.Class || sym.kind == net.sergeych.lyng.binding.SymbolKind.Enum) return sym.name if (sym.kind == net.sergeych.lyng.binding.SymbolKind.Class || sym.kind == net.sergeych.lyng.binding.SymbolKind.Enum) return sym.name
@@ -441,17 +325,13 @@ object DocLookupUtils {
} }
} }
// 1) Declarations in current file (val/var/fun/class/enum), prioritized by proximity // 1) Global declarations in current file (val/var/fun/class/enum)
val src = mini.range.start.source val d = mini.declarations.firstOrNull { it.name == ident }
val d = mini.declarations
.filter { it.name == ident && src.offsetOf(it.nameStart) < dotPos }
.maxByOrNull { src.offsetOf(it.nameStart) }
if (d != null) { if (d != null) {
return when (d) { return when (d) {
is MiniClassDecl -> d.name is MiniClassDecl -> d.name
is MiniEnumDecl -> d.name is MiniEnumDecl -> d.name
is MiniValDecl -> simpleClassNameOf(d.type ?: inferTypeRefForVal(d, text, imported, mini)) is MiniValDecl -> simpleClassNameOf(d.type)
is MiniFunDecl -> simpleClassNameOf(d.returnType) is MiniFunDecl -> simpleClassNameOf(d.returnType)
} }
} }
@@ -463,9 +343,6 @@ object DocLookupUtils {
} }
} }
// 2a) Try to find plain assignment in text if not found in declarations: x = test()
inferTypeFromAssignmentInText(ident, text, imported, mini, beforeOffset = dotPos)?.let { return simpleClassNameOf(it) }
// 3) Recursive chaining: Base.ident. // 3) Recursive chaining: Base.ident.
val dotBefore = findDotLeft(text, wordRange.first) val dotBefore = findDotLeft(text, wordRange.first)
if (dotBefore != null) { if (dotBefore != null) {
@@ -476,7 +353,7 @@ object DocLookupUtils {
if (resolved != null) { if (resolved != null) {
val rt = when (val m = resolved.second) { val rt = when (val m = resolved.second) {
is MiniMemberFunDecl -> m.returnType is MiniMemberFunDecl -> m.returnType
is MiniMemberValDecl -> m.type ?: inferTypeRefFromInitRange(m.initRange, m.nameStart, text, imported, mini) is MiniMemberValDecl -> m.type
else -> null else -> null
} }
return simpleClassNameOf(rt) return simpleClassNameOf(rt)
@@ -491,43 +368,6 @@ object DocLookupUtils {
return null return null
} }
private fun inferTypeFromAssignmentInText(ident: String, text: String, imported: List<String>, mini: MiniScript?, beforeOffset: Int = Int.MAX_VALUE): MiniTypeRef? {
// Heuristic: search for "val ident =" or "ident =" in text
val re = Regex("(?:^|[\\n;])\\s*(?:val|var)?\\s*${ident}\\s*(?::\\s*([A-Za-z_][A-Za-z0-9_]*))?\\s*(?:=|by)\\s*([^\\n;]+)")
val match = re.findAll(text)
.filter { it.range.first < beforeOffset }
.lastOrNull() ?: return null
val explicitType = match.groupValues.getOrNull(1)?.takeIf { it.isNotBlank() }
if (explicitType != null) return syntheticTypeRef(explicitType)
val expr = match.groupValues.getOrNull(2)?.let { stripComments(it) } ?: return null
return inferTypeRefFromExpression(expr, imported, mini, contextText = text, beforeOffset = beforeOffset)
}
private fun stripComments(text: String): String {
var result = ""
var i = 0
var inString = false
while (i < text.length) {
val ch = text[i]
if (ch == '"' && (i == 0 || text[i - 1] != '\\')) {
inString = !inString
}
if (!inString && ch == '/' && i + 1 < text.length) {
if (text[i + 1] == '/') break // single line comment
if (text[i + 1] == '*') {
// Skip block comment
i += 2
while (i + 1 < text.length && !(text[i] == '*' && text[i + 1] == '/')) i++
i += 2 // Skip '*/'
continue
}
}
result += ch
i++
}
return result.trim()
}
fun guessReturnClassFromMemberCallBeforeMini(mini: MiniScript?, text: String, dotPos: Int, imported: List<String>, binding: BindingSnapshot? = null): String? { fun guessReturnClassFromMemberCallBeforeMini(mini: MiniScript?, text: String, dotPos: Int, imported: List<String>, binding: BindingSnapshot? = null): String? {
if (mini == null) return null if (mini == null) return null
var i = prevNonWs(text, dotPos - 1) var i = prevNonWs(text, dotPos - 1)
@@ -580,9 +420,7 @@ object DocLookupUtils {
val rt = when (m) { val rt = when (m) {
is MiniMemberFunDecl -> m.returnType is MiniMemberFunDecl -> m.returnType
is MiniMemberValDecl -> m.type is MiniMemberValDecl -> m.type
is MiniFunDecl -> m.returnType is MiniInitDecl -> null
is MiniValDecl -> m.type
else -> null
} }
simpleClassNameOf(rt) simpleClassNameOf(rt)
} }
@@ -592,7 +430,6 @@ object DocLookupUtils {
fun scanLocalClassMembersFromText(mini: MiniScript, text: String, cls: MiniClassDecl): Map<String, ScannedSig> { fun scanLocalClassMembersFromText(mini: MiniScript, text: String, cls: MiniClassDecl): Map<String, ScannedSig> {
val src = mini.range.start.source val src = mini.range.start.source
if (cls.nameStart.source != src) return emptyMap()
val start = src.offsetOf(cls.bodyRange?.start ?: cls.range.start) val start = src.offsetOf(cls.bodyRange?.start ?: cls.range.start)
val end = src.offsetOf(cls.bodyRange?.end ?: cls.range.end).coerceAtMost(text.length) val end = src.offsetOf(cls.bodyRange?.end ?: cls.range.end).coerceAtMost(text.length)
if (start !in 0..end) return emptyMap() if (start !in 0..end) return emptyMap()
@@ -617,25 +454,13 @@ object DocLookupUtils {
return map return map
} }
fun guessReceiverClass(text: String, dotPos: Int, imported: List<String>, mini: MiniScript? = null, beforeOffset: Int = dotPos): String? { fun guessReceiverClass(text: String, dotPos: Int, imported: List<String>, mini: MiniScript? = null): String? {
guessClassFromCallBefore(text, dotPos, imported, mini)?.let { return it } guessClassFromCallBefore(text, dotPos, imported, mini)?.let { return it }
var i = prevNonWs(text, dotPos - 1) var i = prevNonWs(text, dotPos - 1)
if (i >= 0) { if (i >= 0) {
when (text[i]) { when (text[i]) {
'"' -> return "String" '"' -> return "String"
']' -> { ']' -> return "List"
// Check if literal or indexing
val matchingOpen = findMatchingOpenBracket(text, i)
if (matchingOpen != null && matchingOpen > 0) {
val beforeOpen = prevNonWs(text, matchingOpen - 1)
if (beforeOpen >= 0 && (isIdentChar(text[beforeOpen]) || text[beforeOpen] == ')' || text[beforeOpen] == ']')) {
// Likely indexing: infer type of full expression
val exprText = text.substring(0, i + 1)
return simpleClassNameOf(inferTypeRefFromExpression(exprText, imported, mini, beforeOffset = beforeOffset))
}
}
return "List"
}
'}' -> return "Dict" '}' -> return "Dict"
')' -> { ')' -> {
// Parenthesized expression: walk back to matching '(' and inspect the inner expression // Parenthesized expression: walk back to matching '(' and inspect the inner expression
@@ -687,12 +512,6 @@ object DocLookupUtils {
val identRange = wordRangeAt(text, i + 1) val identRange = wordRangeAt(text, i + 1)
if (identRange != null) { if (identRange != null) {
val ident = text.substring(identRange.first, identRange.second) val ident = text.substring(identRange.first, identRange.second)
// 3a) Handle plain "this"
if (ident == "this" && mini != null) {
findEnclosingClass(mini, identRange.first)?.let { return it.name }
}
// if it's "as Type", we want Type // if it's "as Type", we want Type
var k = prevNonWs(text, identRange.first - 1) var k = prevNonWs(text, identRange.first - 1)
if (k >= 1 && text[k] == 's' && text[k - 1] == 'a' && (k - 1 == 0 || !text[k - 2].isLetterOrDigit())) { if (k >= 1 && text[k] == 's' && text[k - 1] == 'a' && (k - 1 == 0 || !text[k - 2].isLetterOrDigit())) {
@@ -744,9 +563,7 @@ object DocLookupUtils {
val ret = when (member) { val ret = when (member) {
is MiniMemberFunDecl -> member.returnType is MiniMemberFunDecl -> member.returnType
is MiniMemberValDecl -> member.type is MiniMemberValDecl -> member.type
is MiniFunDecl -> member.returnType is MiniInitDecl -> null
is MiniValDecl -> member.type
else -> null
} }
return simpleClassNameOf(ret) return simpleClassNameOf(ret)
} }
@@ -809,216 +626,11 @@ object DocLookupUtils {
val ret = when (member) { val ret = when (member) {
is MiniMemberFunDecl -> member.returnType is MiniMemberFunDecl -> member.returnType
is MiniMemberValDecl -> member.type is MiniMemberValDecl -> member.type
is MiniFunDecl -> member.returnType is MiniInitDecl -> null
is MiniValDecl -> member.type
else -> null
} }
return simpleClassNameOf(ret) return simpleClassNameOf(ret)
} }
fun inferTypeRefFromExpression(text: String, imported: List<String>, mini: MiniScript? = null, contextText: String? = null, beforeOffset: Int = Int.MAX_VALUE): MiniTypeRef? {
val trimmed = stripComments(text)
if (trimmed.isEmpty()) return null
val fullText = contextText ?: text
// 1) Literals
if (trimmed.startsWith("\"")) return syntheticTypeRef("String")
if (trimmed.startsWith("[")) return syntheticTypeRef("List")
if (trimmed.startsWith("{")) return syntheticTypeRef("Dict")
if (trimmed == "true" || trimmed == "false") return syntheticTypeRef("Boolean")
if (trimmed.all { it.isDigit() || it == '.' || it == '_' || it == 'e' || it == 'E' }) {
val hasDigits = trimmed.any { it.isDigit() }
if (hasDigits)
return if (trimmed.contains('.') || trimmed.contains('e', ignoreCase = true)) syntheticTypeRef("Real") else syntheticTypeRef("Int")
}
// 2) Function/Constructor calls or Indexing
if (trimmed.endsWith(")")) {
val openParen = findMatchingOpenParen(trimmed, trimmed.length - 1)
if (openParen != null && openParen > 0) {
var j = openParen - 1
while (j >= 0 && trimmed[j].isWhitespace()) j--
val end = j + 1
while (j >= 0 && isIdentChar(trimmed[j])) j--
val start = j + 1
if (start < end) {
val callee = trimmed.substring(start, end)
// Check if it's a member call (dot before callee)
var k = start - 1
while (k >= 0 && trimmed[k].isWhitespace()) k--
if (k >= 0 && trimmed[k] == '.') {
val prevDot = k
// Recursive: try to infer type of what's before the dot
val receiverText = trimmed.substring(0, prevDot)
val receiverType = inferTypeRefFromExpression(receiverText, imported, mini, contextText = fullText, beforeOffset = beforeOffset)
val receiverClass = simpleClassNameOf(receiverType)
if (receiverClass != null) {
val resolved = resolveMemberWithInheritance(imported, receiverClass, callee, mini)
if (resolved != null) {
return when (val m = resolved.second) {
is MiniMemberFunDecl -> m.returnType
is MiniMemberValDecl -> m.type ?: inferTypeRefFromInitRange(m.initRange, m.nameStart, fullText, imported, mini)
else -> null
}
}
}
} else {
// Top-level call or constructor
val classes = aggregateClasses(imported, mini)
if (classes.containsKey(callee)) return syntheticTypeRef(callee)
for (mod in imported) {
val decls = BuiltinDocRegistry.docsForModule(mod)
val fn = decls.asSequence().filterIsInstance<MiniFunDecl>().firstOrNull { it.name == callee }
if (fn != null) return fn.returnType
}
mini?.declarations?.filterIsInstance<MiniFunDecl>()?.firstOrNull { it.name == callee }?.let { return it.returnType }
}
}
}
}
if (trimmed.endsWith("]")) {
val openBracket = findMatchingOpenBracket(trimmed, trimmed.length - 1)
if (openBracket != null && openBracket > 0) {
val receiverText = trimmed.substring(0, openBracket).trim()
if (receiverText.isNotEmpty()) {
val receiverType = inferTypeRefFromExpression(receiverText, imported, mini, contextText = fullText, beforeOffset = beforeOffset)
if (receiverType is MiniGenericType) {
val baseName = simpleClassNameOf(receiverType.base)
if (baseName == "List" && receiverType.args.isNotEmpty()) {
return receiverType.args[0]
}
if (baseName == "Map" && receiverType.args.size >= 2) {
return receiverType.args[1]
}
}
// Fallback for non-generic collections or if base name matches
val baseName = simpleClassNameOf(receiverType)
if (baseName == "List" || baseName == "Array" || baseName == "String") {
if (baseName == "String") return syntheticTypeRef("Char")
return syntheticTypeRef("Any")
}
}
}
}
// 3) Member field or simple identifier at the end
val lastWord = wordRangeAt(trimmed, trimmed.length)
if (lastWord != null && lastWord.second == trimmed.length) {
val ident = trimmed.substring(lastWord.first, lastWord.second)
var k = lastWord.first - 1
while (k >= 0 && trimmed[k].isWhitespace()) k--
if (k >= 0 && trimmed[k] == '.') {
// Member field: receiver.ident
val receiverText = trimmed.substring(0, k).trim()
val receiverType = inferTypeRefFromExpression(receiverText, imported, mini, contextText = fullText, beforeOffset = beforeOffset)
val receiverClass = simpleClassNameOf(receiverType)
if (receiverClass != null) {
val resolved = resolveMemberWithInheritance(imported, receiverClass, ident, mini)
if (resolved != null) {
return when (val m = resolved.second) {
is MiniMemberFunDecl -> m.returnType
is MiniMemberValDecl -> m.type ?: inferTypeRefFromInitRange(m.initRange, m.nameStart, fullText, imported, mini)
else -> null
}
}
}
} else {
// Simple identifier
// 1) Declarations in current file (val/var/fun/class/enum), prioritized by proximity
val src = mini?.range?.start?.source
val d = if (src != null) {
mini.declarations
.filter { it.name == ident && src.offsetOf(it.nameStart) < beforeOffset }
.maxByOrNull { src.offsetOf(it.nameStart) }
} else {
mini?.declarations?.firstOrNull { it.name == ident }
}
if (d != null) {
return when (d) {
is MiniClassDecl -> syntheticTypeRef(d.name)
is MiniEnumDecl -> syntheticTypeRef(d.name)
is MiniValDecl -> d.type ?: inferTypeRefForVal(d, fullText, imported, mini)
is MiniFunDecl -> d.returnType
}
}
// 2) Parameters in any function
for (fd in mini?.declarations?.filterIsInstance<MiniFunDecl>() ?: emptyList()) {
for (p in fd.params) {
if (p.name == ident) return p.type
}
}
// 3) Try to find plain assignment in text: ident = expr
inferTypeFromAssignmentInText(ident, fullText, imported, mini, beforeOffset = beforeOffset)?.let { return it }
// 4) Check if it's a known class (static access)
val classes = aggregateClasses(imported, mini)
if (classes.containsKey(ident)) return syntheticTypeRef(ident)
}
}
return null
}
private fun findMatchingOpenBracket(text: String, closeBracketPos: Int): Int? {
if (closeBracketPos < 0 || closeBracketPos >= text.length || text[closeBracketPos] != ']') return null
var depth = 0
var i = closeBracketPos - 1
while (i >= 0) {
when (text[i]) {
']' -> depth++
'[' -> if (depth == 0) return i else depth--
}
i--
}
return null
}
private fun findMatchingOpenParen(text: String, closeParenPos: Int): Int? {
if (closeParenPos < 0 || closeParenPos >= text.length || text[closeParenPos] != ')') return null
var depth = 0
var i = closeParenPos - 1
while (i >= 0) {
when (text[i]) {
')' -> depth++
'(' -> if (depth == 0) return i else depth--
}
i--
}
return null
}
private fun syntheticTypeRef(name: String): MiniTypeRef =
MiniTypeName(MiniRange(net.sergeych.lyng.Pos.builtIn, net.sergeych.lyng.Pos.builtIn),
listOf(MiniTypeName.Segment(name, MiniRange(net.sergeych.lyng.Pos.builtIn, net.sergeych.lyng.Pos.builtIn))), false)
fun inferTypeRefForVal(vd: MiniValDecl, text: String, imported: List<String>, mini: MiniScript?): MiniTypeRef? {
return inferTypeRefFromInitRange(vd.initRange, vd.nameStart, text, imported, mini)
}
fun inferTypeRefFromInitRange(initRange: MiniRange?, nameStart: net.sergeych.lyng.Pos, text: String, imported: List<String>, mini: MiniScript?): MiniTypeRef? {
val range = initRange ?: return null
val src = mini?.range?.start?.source ?: return null
val start = src.offsetOf(range.start)
val end = src.offsetOf(range.end)
if (start < 0 || start >= end || end > text.length) return null
var exprText = text.substring(start, end).trim()
if (exprText.startsWith("=")) {
exprText = exprText.substring(1).trim()
}
if (exprText.startsWith("by")) {
exprText = exprText.substring(2).trim()
}
val beforeOffset = src.offsetOf(nameStart)
return inferTypeRefFromExpression(exprText, imported, mini, contextText = text, beforeOffset = beforeOffset)
}
fun simpleClassNameOf(t: MiniTypeRef?): String? = when (t) { fun simpleClassNameOf(t: MiniTypeRef?): String? = when (t) {
null -> null null -> null
is MiniTypeName -> t.segments.lastOrNull()?.name is MiniTypeName -> t.segments.lastOrNull()?.name
@@ -1027,17 +639,6 @@ object DocLookupUtils {
is MiniTypeVar -> null is MiniTypeVar -> null
} }
fun typeOf(t: MiniTypeRef?): String = when (t) {
is MiniTypeName -> t.segments.joinToString(".") { it.name } + (if (t.nullable) "?" else "")
is MiniGenericType -> typeOf(t.base) + "<" + t.args.joinToString(", ") { typeOf(it) } + ">" + (if (t.nullable) "?" else "")
is MiniFunctionType -> {
val r = t.receiver?.let { typeOf(it) + "." } ?: ""
r + "(" + t.params.joinToString(", ") { typeOf(it) } + ") -> " + typeOf(t.returnType) + (if (t.nullable) "?" else "")
}
is MiniTypeVar -> t.name + (if (t.nullable) "?" else "")
null -> ""
}
fun findDotLeft(text: String, offset: Int): Int? { fun findDotLeft(text: String, offset: Int): Int? {
var i = (offset - 1).coerceAtLeast(0) var i = (offset - 1).coerceAtLeast(0)
while (i >= 0 && text[i].isWhitespace()) i-- while (i >= 0 && text[i].isWhitespace()) i--
@@ -1065,13 +666,13 @@ object DocLookupUtils {
fun enumToSyntheticClass(en: MiniEnumDecl): MiniClassDecl { fun enumToSyntheticClass(en: MiniEnumDecl): MiniClassDecl {
val staticMembers = mutableListOf<MiniMemberDecl>() val staticMembers = mutableListOf<MiniMemberDecl>()
// entries: List // entries: List
staticMembers.add(MiniMemberValDecl(en.range, "entries", false, null, null, null, en.nameStart, isStatic = true)) staticMembers.add(MiniMemberValDecl(en.range, "entries", false, null, null, en.nameStart, isStatic = true))
// valueOf(name: String): Enum // valueOf(name: String): Enum
staticMembers.add(MiniMemberFunDecl(en.range, "valueOf", listOf(MiniParam("name", null, en.nameStart)), null, null, en.nameStart, isStatic = true)) staticMembers.add(MiniMemberFunDecl(en.range, "valueOf", listOf(MiniParam("name", null, en.nameStart)), null, null, en.nameStart, isStatic = true))
// Also add each entry as a static member (const) // Also add each entry as a static member (const)
for (entry in en.entries) { for (entry in en.entries) {
staticMembers.add(MiniMemberValDecl(en.range, entry, false, MiniTypeName(en.range, listOf(MiniTypeName.Segment(en.name, en.range)), false), null, null, en.nameStart, isStatic = true)) staticMembers.add(MiniMemberValDecl(en.range, entry, false, MiniTypeName(en.range, listOf(MiniTypeName.Segment(en.name, en.range)), false), null, en.nameStart, isStatic = true))
} }
return MiniClassDecl( return MiniClassDecl(
@@ -1,5 +1,5 @@
/* /*
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com * Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -145,23 +145,6 @@ fun ObjClass.addClassFnDoc(
} }
} }
fun ObjClass.addPropertyDoc(
name: String,
doc: String,
type: TypeDoc? = null,
visibility: Visibility = Visibility.Public,
moduleName: String? = null,
getter: (suspend Scope.() -> Obj)? = null,
setter: (suspend Scope.(Obj) -> Unit)? = null
) {
addProperty(name, getter, setter, visibility)
BuiltinDocRegistry.module(moduleName ?: ownerModuleNameFromClassOrUnknown()) {
classDoc(this@addPropertyDoc.className, doc = "") {
field(name = name, doc = doc, type = type, mutable = setter != null)
}
}
}
fun ObjClass.addClassConstDoc( fun ObjClass.addClassConstDoc(
name: String, name: String,
value: Obj, value: Obj,
@@ -39,56 +39,7 @@ data class MiniDoc(
val raw: String, val raw: String,
val summary: String?, val summary: String?,
val tags: Map<String, List<String>> = emptyMap() val tags: Map<String, List<String>> = emptyMap()
) { )
companion object {
fun parse(range: MiniRange, lines: Iterable<String>, extraTags: Map<String, List<String>> = emptyMap()): MiniDoc {
val parsedTags = mutableMapOf<String, MutableList<String>>()
var currentTag: String? = null
val currentContent = StringBuilder()
fun flush() {
currentTag?.let { tag ->
parsedTags.getOrPut(tag) { mutableListOf() }.add(currentContent.toString().trim())
}
currentContent.setLength(0)
}
val descriptionLines = mutableListOf<String>()
var inTags = false
for (rawLine in lines) {
for (line in rawLine.lines()) {
val trimmed = line.trim()
if (trimmed.startsWith("@")) {
inTags = true
flush()
val parts = trimmed.substring(1).split(Regex("\\s+"), 2)
currentTag = parts[0]
currentContent.append(parts.getOrNull(1) ?: "")
} else {
if (inTags) {
if (currentContent.isNotEmpty()) currentContent.append("\n")
currentContent.append(line)
} else {
descriptionLines.add(line)
}
}
}
}
flush()
val raw = descriptionLines.joinToString("\n").trimEnd()
val summary = raw.lines().firstOrNull { it.isNotBlank() }?.trim()
val finalTags = parsedTags.toMutableMap()
extraTags.forEach { (k, v) ->
finalTags.getOrPut(k) { mutableListOf() }.addAll(v)
}
return MiniDoc(range, raw, summary, finalTags)
}
}
}
sealed interface MiniNode { val range: MiniRange } sealed interface MiniNode { val range: MiniRange }
@@ -130,17 +81,13 @@ data class MiniTypeVar(
) : MiniTypeRef ) : MiniTypeRef
// Script and declarations (lean subset; can be extended later) // Script and declarations (lean subset; can be extended later)
sealed interface MiniNamedDecl : MiniNode { sealed interface MiniDecl : MiniNode {
val name: String val name: String
val doc: MiniDoc? val doc: MiniDoc?
// Start position of the declaration name identifier in source; end can be derived as start + name.length // Start position of the declaration name identifier in source; end can be derived as start + name.length
val nameStart: Pos val nameStart: Pos
val isExtern: Boolean
val isStatic: Boolean
} }
sealed interface MiniDecl : MiniNamedDecl
data class MiniScript( data class MiniScript(
override val range: MiniRange, override val range: MiniRange,
val declarations: MutableList<MiniDecl> = mutableListOf(), val declarations: MutableList<MiniDecl> = mutableListOf(),
@@ -163,9 +110,7 @@ data class MiniFunDecl(
val body: MiniBlock?, val body: MiniBlock?,
override val doc: MiniDoc?, override val doc: MiniDoc?,
override val nameStart: Pos, override val nameStart: Pos,
val receiver: MiniTypeRef? = null, val receiver: MiniTypeRef? = null
override val isExtern: Boolean = false,
override val isStatic: Boolean = false,
) : MiniDecl ) : MiniDecl
data class MiniValDecl( data class MiniValDecl(
@@ -176,9 +121,7 @@ data class MiniValDecl(
val initRange: MiniRange?, val initRange: MiniRange?,
override val doc: MiniDoc?, override val doc: MiniDoc?,
override val nameStart: Pos, override val nameStart: Pos,
val receiver: MiniTypeRef? = null, val receiver: MiniTypeRef? = null
override val isExtern: Boolean = false,
override val isStatic: Boolean = false,
) : MiniDecl ) : MiniDecl
data class MiniClassDecl( data class MiniClassDecl(
@@ -191,10 +134,7 @@ data class MiniClassDecl(
override val doc: MiniDoc?, override val doc: MiniDoc?,
override val nameStart: Pos, override val nameStart: Pos,
// Built-in extension: list of member declarations (functions and fields) // Built-in extension: list of member declarations (functions and fields)
val members: List<MiniMemberDecl> = emptyList(), val members: List<MiniMemberDecl> = emptyList()
override val isExtern: Boolean = false,
override val isStatic: Boolean = false,
val isObject: Boolean = false
) : MiniDecl ) : MiniDecl
data class MiniEnumDecl( data class MiniEnumDecl(
@@ -202,9 +142,7 @@ data class MiniEnumDecl(
override val name: String, override val name: String,
val entries: List<String>, val entries: List<String>,
override val doc: MiniDoc?, override val doc: MiniDoc?,
override val nameStart: Pos, override val nameStart: Pos
override val isExtern: Boolean = false,
override val isStatic: Boolean = false,
) : MiniDecl ) : MiniDecl
data class MiniCtorField( data class MiniCtorField(
@@ -228,8 +166,11 @@ data class MiniIdentifier(
) : MiniNode ) : MiniNode
// --- Class member declarations (for built-in/registry docs) --- // --- Class member declarations (for built-in/registry docs) ---
sealed interface MiniMemberDecl : MiniNamedDecl { sealed interface MiniMemberDecl : MiniNode {
override val isStatic: Boolean val name: String
val doc: MiniDoc?
val nameStart: Pos
val isStatic: Boolean
} }
data class MiniMemberFunDecl( data class MiniMemberFunDecl(
@@ -240,8 +181,6 @@ data class MiniMemberFunDecl(
override val doc: MiniDoc?, override val doc: MiniDoc?,
override val nameStart: Pos, override val nameStart: Pos,
override val isStatic: Boolean = false, override val isStatic: Boolean = false,
override val isExtern: Boolean = false,
val body: MiniBlock? = null
) : MiniMemberDecl ) : MiniMemberDecl
data class MiniMemberValDecl( data class MiniMemberValDecl(
@@ -249,11 +188,9 @@ data class MiniMemberValDecl(
override val name: String, override val name: String,
val mutable: Boolean, val mutable: Boolean,
val type: MiniTypeRef?, val type: MiniTypeRef?,
val initRange: MiniRange?,
override val doc: MiniDoc?, override val doc: MiniDoc?,
override val nameStart: Pos, override val nameStart: Pos,
override val isStatic: Boolean = false, override val isStatic: Boolean = false,
override val isExtern: Boolean = false,
) : MiniMemberDecl ) : MiniMemberDecl
data class MiniInitDecl( data class MiniInitDecl(
@@ -263,7 +200,6 @@ data class MiniInitDecl(
override val name: String get() = "init" override val name: String get() = "init"
override val doc: MiniDoc? get() = null override val doc: MiniDoc? get() = null
override val isStatic: Boolean get() = false override val isStatic: Boolean get() = false
override val isExtern: Boolean get() = false
} }
// Streaming sink to collect mini-AST during parsing. Implementations may assemble a tree or process events. // Streaming sink to collect mini-AST during parsing. Implementations may assemble a tree or process events.
@@ -273,12 +209,6 @@ interface MiniAstSink {
fun onDocCandidate(doc: MiniDoc) {} fun onDocCandidate(doc: MiniDoc) {}
fun onEnterClass(node: MiniClassDecl) {}
fun onExitClass(end: Pos) {}
fun onEnterFunction(node: MiniFunDecl?) {}
fun onExitFunction(end: Pos) {}
fun onImport(node: MiniImport) {} fun onImport(node: MiniImport) {}
fun onFunDecl(node: MiniFunDecl) {} fun onFunDecl(node: MiniFunDecl) {}
fun onValDecl(node: MiniValDecl) {} fun onValDecl(node: MiniValDecl) {}
@@ -308,10 +238,8 @@ interface MiniTypeTrace {
class MiniAstBuilder : MiniAstSink { class MiniAstBuilder : MiniAstSink {
private var currentScript: MiniScript? = null private var currentScript: MiniScript? = null
private val blocks = ArrayDeque<MiniBlock>() private val blocks = ArrayDeque<MiniBlock>()
private val classStack = ArrayDeque<MiniClassDecl>()
private var lastDoc: MiniDoc? = null private var lastDoc: MiniDoc? = null
private var scriptDepth: Int = 0 private var scriptDepth: Int = 0
private var functionDepth: Int = 0
fun build(): MiniScript? = currentScript fun build(): MiniScript? = currentScript
@@ -334,120 +262,27 @@ class MiniAstBuilder : MiniAstSink {
lastDoc = doc lastDoc = doc
} }
override fun onEnterClass(node: MiniClassDecl) {
val attach = node.copy(doc = node.doc ?: lastDoc)
classStack.addLast(attach)
lastDoc = null
}
override fun onExitClass(end: Pos) {
val finished = classStack.removeLastOrNull()
if (finished != null) {
val updated = finished.copy(range = MiniRange(finished.range.start, end))
// Always add to top-level for now to ensure visibility in light engine
currentScript?.declarations?.add(updated)
}
}
override fun onEnterFunction(node: MiniFunDecl?) {
functionDepth++
}
override fun onExitFunction(end: Pos) {
functionDepth--
}
override fun onImport(node: MiniImport) { override fun onImport(node: MiniImport) {
currentScript?.imports?.add(node) currentScript?.imports?.add(node)
} }
override fun onFunDecl(node: MiniFunDecl) { override fun onFunDecl(node: MiniFunDecl) {
val attach = node.copy(doc = node.doc ?: lastDoc) val attach = node.copy(doc = node.doc ?: lastDoc)
val currentClass = classStack.lastOrNull()
if (currentClass != null && functionDepth == 0) {
// Convert MiniFunDecl to MiniMemberFunDecl for inclusion in members
val member = MiniMemberFunDecl(
range = attach.range,
name = attach.name,
params = attach.params,
returnType = attach.returnType,
doc = attach.doc,
nameStart = attach.nameStart,
isStatic = attach.isStatic,
isExtern = attach.isExtern,
body = attach.body
)
// Need to update the class in the stack since it's immutable-ish (data class)
// Check if we already have this member (from a previous onFunDecl call for the same function)
val existing = currentClass.members.filterIsInstance<MiniMemberFunDecl>().find { it.name == attach.name && it.nameStart == attach.nameStart }
if (existing != null) {
val members = currentClass.members.map { if (it === existing) member else it }
classStack.removeLast()
classStack.addLast(currentClass.copy(members = members))
} else {
classStack.removeLast()
classStack.addLast(currentClass.copy(members = currentClass.members + member))
}
} else {
// Check if already in declarations to avoid duplication
val existing = currentScript?.declarations?.find { it.name == attach.name && it.nameStart == attach.nameStart }
if (existing != null) {
val idx = currentScript?.declarations?.indexOf(existing) ?: -1
if (idx >= 0) currentScript?.declarations?.set(idx, attach)
} else {
currentScript?.declarations?.add(attach) currentScript?.declarations?.add(attach)
}
}
lastDoc = null lastDoc = null
} }
override fun onValDecl(node: MiniValDecl) { override fun onValDecl(node: MiniValDecl) {
val attach = node.copy(doc = node.doc ?: lastDoc) val attach = node.copy(doc = node.doc ?: lastDoc)
val currentClass = classStack.lastOrNull()
if (currentClass != null && functionDepth == 0) {
val member = MiniMemberValDecl(
range = attach.range,
name = attach.name,
mutable = attach.mutable,
type = attach.type,
initRange = attach.initRange,
doc = attach.doc,
nameStart = attach.nameStart,
isStatic = attach.isStatic,
isExtern = attach.isExtern
)
// Duplicates for vals are rare but possible if Compiler calls it twice
val existing = currentClass.members.filterIsInstance<MiniMemberValDecl>().find { it.name == attach.name && it.nameStart == attach.nameStart }
if (existing != null) {
val members = currentClass.members.map { if (it === existing) member else it }
classStack.removeLast()
classStack.addLast(currentClass.copy(members = members))
} else {
classStack.removeLast()
classStack.addLast(currentClass.copy(members = currentClass.members + member))
}
} else {
val existing = currentScript?.declarations?.find { it.name == attach.name && it.nameStart == attach.nameStart }
if (existing != null) {
val idx = currentScript?.declarations?.indexOf(existing) ?: -1
if (idx >= 0) currentScript?.declarations?.set(idx, attach)
} else {
currentScript?.declarations?.add(attach) currentScript?.declarations?.add(attach)
}
}
lastDoc = null lastDoc = null
} }
override fun onClassDecl(node: MiniClassDecl) { override fun onClassDecl(node: MiniClassDecl) {
// This is the old way, we might want to deprecate it or make it call onEnterClass
// For now, if we are NOT using enter/exit, keep behavior.
// But Compiler.kt will be updated to use enter/exit.
if (classStack.isEmpty()) {
val attach = node.copy(doc = node.doc ?: lastDoc) val attach = node.copy(doc = node.doc ?: lastDoc)
currentScript?.declarations?.add(attach) currentScript?.declarations?.add(attach)
lastDoc = null lastDoc = null
} }
}
override fun onEnumDecl(node: MiniEnumDecl) { override fun onEnumDecl(node: MiniEnumDecl) {
val attach = node.copy(doc = node.doc ?: lastDoc) val attach = node.copy(doc = node.doc ?: lastDoc)
@@ -4,6 +4,8 @@
*/ */
package net.sergeych.lyng.miniast package net.sergeych.lyng.miniast
import net.sergeych.lyng.obj.ObjString
object StdlibDocsBootstrap { object StdlibDocsBootstrap {
// Simple idempotent guard; races are harmless as initializer side-effects are idempotent // Simple idempotent guard; races are harmless as initializer side-effects are idempotent
private var ensured = false private var ensured = false
@@ -14,25 +16,7 @@ object StdlibDocsBootstrap {
// Touch core Obj* types whose docs are registered via addFnDoc/addConstDoc // Touch core Obj* types whose docs are registered via addFnDoc/addConstDoc
// Accessing .type forces their static initializers to run and register docs. // Accessing .type forces their static initializers to run and register docs.
@Suppress("UNUSED_VARIABLE") @Suppress("UNUSED_VARIABLE")
val _string = net.sergeych.lyng.obj.ObjString.type val _string = ObjString.type
@Suppress("UNUSED_VARIABLE")
val _any = net.sergeych.lyng.obj.Obj.rootObjectType
@Suppress("UNUSED_VARIABLE")
val _list = net.sergeych.lyng.obj.ObjList.type
@Suppress("UNUSED_VARIABLE")
val _map = net.sergeych.lyng.obj.ObjMap.type
@Suppress("UNUSED_VARIABLE")
val _int = net.sergeych.lyng.obj.ObjInt.type
@Suppress("UNUSED_VARIABLE")
val _real = net.sergeych.lyng.obj.ObjReal.type
@Suppress("UNUSED_VARIABLE")
val _bool = net.sergeych.lyng.obj.ObjBool.type
@Suppress("UNUSED_VARIABLE")
val _regex = net.sergeych.lyng.obj.ObjRegex.type
@Suppress("UNUSED_VARIABLE")
val _range = net.sergeych.lyng.obj.ObjRange.type
@Suppress("UNUSED_VARIABLE")
val _buffer = net.sergeych.lyng.obj.ObjBuffer.type
} catch (_: Throwable) { } catch (_: Throwable) {
// Best-effort; absence should not break consumers // Best-effort; absence should not break consumers
} finally { } finally {
@@ -25,9 +25,6 @@ import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.JsonNull import kotlinx.serialization.json.JsonNull
import kotlinx.serialization.serializer import kotlinx.serialization.serializer
import net.sergeych.lyng.* import net.sergeych.lyng.*
import net.sergeych.lyng.miniast.ParamDoc
import net.sergeych.lyng.miniast.addFnDoc
import net.sergeych.lyng.miniast.type
import net.sergeych.lynon.LynonDecoder import net.sergeych.lynon.LynonDecoder
import net.sergeych.lynon.LynonEncoder import net.sergeych.lynon.LynonEncoder
import net.sergeych.lynon.LynonType import net.sergeych.lynon.LynonType
@@ -64,12 +61,7 @@ open class Obj {
@Suppress("SuspiciousEqualsCombination") @Suppress("SuspiciousEqualsCombination")
fun isInstanceOf(someClass: Obj) = someClass === objClass || fun isInstanceOf(someClass: Obj) = someClass === objClass ||
objClass.allParentsSet.contains(someClass) || objClass.allParentsSet.contains(someClass) ||
someClass == rootObjectType || someClass == rootObjectType
(someClass is ObjClass && objClass.allImplementingNames.contains(someClass.className))
fun isInstanceOf(className: String) =
objClass.mro.any { it.className == className } ||
objClass.allImplementingNames.contains(className)
suspend fun invokeInstanceMethod(scope: Scope, name: String, vararg args: Obj): Obj = suspend fun invokeInstanceMethod(scope: Scope, name: String, vararg args: Obj): Obj =
@@ -93,46 +85,24 @@ open class Obj {
args: Arguments = Arguments.EMPTY, args: Arguments = Arguments.EMPTY,
onNotFoundResult: (suspend () -> Obj?)? = null onNotFoundResult: (suspend () -> Obj?)? = null
): Obj { ): Obj {
// 0. Prefer private member of current class context
scope.currentClassCtx?.let { caller ->
caller.members[name]?.let { rec ->
if (rec.visibility == Visibility.Private && !rec.isAbstract) {
if (rec.type == ObjRecord.Type.Property) {
if (args.isEmpty()) return (rec.value as ObjProperty).callGetter(scope, this, caller)
} else if (rec.type != ObjRecord.Type.Delegated) {
return rec.value.invoke(scope, this, args, caller)
}
}
}
}
// 1. Hierarchy members (excluding root fallback) // 1. Hierarchy members (excluding root fallback)
for (cls in objClass.mro) { for (cls in objClass.mro) {
if (cls.className == "Obj") break if (cls.className == "Obj") break
val rec = cls.members[name] ?: cls.classScope?.objects?.get(name) val rec = cls.members[name] ?: cls.classScope?.objects?.get(name)
if (rec != null && !rec.isAbstract) { if (rec != null && !rec.isAbstract && rec.type != ObjRecord.Type.Property) {
val decl = rec.declaringClass ?: cls val decl = rec.declaringClass ?: cls
val caller = scope.currentClassCtx val caller = scope.currentClassCtx
if (!canAccessMember(rec.visibility, decl, caller)) if (!canAccessMember(rec.visibility, decl, caller))
scope.raiseError(ObjIllegalAccessException(scope, "can't invoke ${name}: not visible (declared in ${decl.className}, caller ${caller?.className ?: "?"})")) scope.raiseError(ObjIllegalAccessException(scope, "can't invoke ${name}: not visible (declared in ${decl.className}, caller ${caller?.className ?: "?"})"))
if (rec.type == ObjRecord.Type.Property) {
if (args.isEmpty()) return (rec.value as ObjProperty).callGetter(scope, this, decl)
} else if (rec.type != ObjRecord.Type.Delegated) {
return rec.value.invoke(scope, this, args, decl) return rec.value.invoke(scope, this, args, decl)
} }
} }
}
// 2. Extensions in scope // 2. Extensions in scope
val extension = scope.findExtension(objClass, name) val extension = scope.findExtension(objClass, name)
if (extension != null) { if (extension != null) {
if (extension.type == ObjRecord.Type.Property) {
if (args.isEmpty()) return (extension.value as ObjProperty).callGetter(scope, this, extension.declaringClass)
} else if (extension.type != ObjRecord.Type.Delegated) {
return extension.value.invoke(scope, this, args) return extension.value.invoke(scope, this, args)
} }
}
// 3. Root object fallback // 3. Root object fallback
for (cls in objClass.mro) { for (cls in objClass.mro) {
@@ -142,15 +112,10 @@ open class Obj {
val caller = scope.currentClassCtx val caller = scope.currentClassCtx
if (!canAccessMember(rec.visibility, decl, caller)) if (!canAccessMember(rec.visibility, decl, caller))
scope.raiseError(ObjIllegalAccessException(scope, "can't invoke ${name}: not visible (declared in ${decl.className}, caller ${caller?.className ?: "?"})")) scope.raiseError(ObjIllegalAccessException(scope, "can't invoke ${name}: not visible (declared in ${decl.className}, caller ${caller?.className ?: "?"})"))
if (rec.type == ObjRecord.Type.Property) {
if (args.isEmpty()) return (rec.value as ObjProperty).callGetter(scope, this, decl)
} else if (rec.type != ObjRecord.Type.Delegated) {
return rec.value.invoke(scope, this, args, decl) return rec.value.invoke(scope, this, args, decl)
} }
} }
} }
}
return onNotFoundResult?.invoke() return onNotFoundResult?.invoke()
?: scope.raiseError( ?: scope.raiseError(
@@ -173,23 +138,8 @@ open class Obj {
open suspend fun compareTo(scope: Scope, other: Obj): Int { open suspend fun compareTo(scope: Scope, other: Obj): Int {
if( other === this) return 0 if( other === this) return 0
if (other === ObjNull || other === ObjUnset || other === ObjVoid) return 2 if( other === ObjNull ) return 2
return invokeInstanceMethod(scope, "compareTo", Arguments(other)) { scope.raiseNotImplemented()
scope.raiseNotImplemented("compareTo for ${objClass.className}")
}.cast<ObjInt>(scope).toInt()
}
open suspend fun equals(scope: Scope, other: Obj): Boolean {
if (other === this) return true
val m = objClass.getInstanceMemberOrNull("equals") ?: scope.findExtension(objClass, "equals")
if (m != null) {
return invokeInstanceMethod(scope, "equals", Arguments(other)).toBool()
}
return try {
compareTo(scope, other) == 0
} catch (e: ExecutionError) {
false
}
} }
open suspend fun contains(scope: Scope, other: Obj): Boolean { open suspend fun contains(scope: Scope, other: Obj): Boolean {
@@ -262,66 +212,46 @@ open class Obj {
* Class of the object: definition of member functions (top-level), etc. * Class of the object: definition of member functions (top-level), etc.
* Note that using lazy allows to avoid endless recursion here * Note that using lazy allows to avoid endless recursion here
*/ */
open val objClass: ObjClass get() = rootObjectType open val objClass: ObjClass = rootObjectType
open suspend fun plus(scope: Scope, other: Obj): Obj { open suspend fun plus(scope: Scope, other: Obj): Obj {
return invokeInstanceMethod(scope, "plus", Arguments(other)) { scope.raiseNotImplemented()
scope.raiseNotImplemented("plus for ${objClass.className}")
}
} }
open suspend fun minus(scope: Scope, other: Obj): Obj { open suspend fun minus(scope: Scope, other: Obj): Obj {
return invokeInstanceMethod(scope, "minus", Arguments(other)) { scope.raiseNotImplemented()
scope.raiseNotImplemented("minus for ${objClass.className}")
}
} }
open suspend fun negate(scope: Scope): Obj { open suspend fun negate(scope: Scope): Obj {
return invokeInstanceMethod(scope, "negate", Arguments.EMPTY) { scope.raiseNotImplemented()
scope.raiseNotImplemented("negate for ${objClass.className}")
}
} }
open suspend fun mul(scope: Scope, other: Obj): Obj { open suspend fun mul(scope: Scope, other: Obj): Obj {
return invokeInstanceMethod(scope, "mul", Arguments(other)) { scope.raiseNotImplemented()
scope.raiseNotImplemented("mul for ${objClass.className}")
}
} }
open suspend fun div(scope: Scope, other: Obj): Obj { open suspend fun div(scope: Scope, other: Obj): Obj {
return invokeInstanceMethod(scope, "div", Arguments(other)) { scope.raiseNotImplemented()
scope.raiseNotImplemented("div for ${objClass.className}")
}
} }
open suspend fun mod(scope: Scope, other: Obj): Obj { open suspend fun mod(scope: Scope, other: Obj): Obj {
return invokeInstanceMethod(scope, "mod", Arguments(other)) { scope.raiseNotImplemented()
scope.raiseNotImplemented("mod for ${objClass.className}")
}
} }
open suspend fun logicalNot(scope: Scope): Obj { open suspend fun logicalNot(scope: Scope): Obj {
return invokeInstanceMethod(scope, "logicalNot", Arguments.EMPTY) { scope.raiseNotImplemented()
scope.raiseNotImplemented("logicalNot for ${objClass.className}")
}
} }
open suspend fun logicalAnd(scope: Scope, other: Obj): Obj { open suspend fun logicalAnd(scope: Scope, other: Obj): Obj {
return invokeInstanceMethod(scope, "logicalAnd", Arguments(other)) { scope.raiseNotImplemented()
scope.raiseNotImplemented("logicalAnd for ${objClass.className}")
}
} }
open suspend fun logicalOr(scope: Scope, other: Obj): Obj { open suspend fun logicalOr(scope: Scope, other: Obj): Obj {
return invokeInstanceMethod(scope, "logicalOr", Arguments(other)) { scope.raiseNotImplemented()
scope.raiseNotImplemented("logicalOr for ${objClass.className}")
}
} }
open suspend fun operatorMatch(scope: Scope, other: Obj): Obj { open suspend fun operatorMatch(scope: Scope, other: Obj): Obj {
return invokeInstanceMethod(scope, "operatorMatch", Arguments(other)) { scope.raiseNotImplemented()
scope.raiseNotImplemented("operatorMatch for ${objClass.className}")
}
} }
open suspend fun operatorNotMatch(scope: Scope, other: Obj): Obj { open suspend fun operatorNotMatch(scope: Scope, other: Obj): Obj {
@@ -330,39 +260,27 @@ open class Obj {
// Bitwise ops default (override in numeric types that support them) // Bitwise ops default (override in numeric types that support them)
open suspend fun bitAnd(scope: Scope, other: Obj): Obj { open suspend fun bitAnd(scope: Scope, other: Obj): Obj {
return invokeInstanceMethod(scope, "bitAnd", Arguments(other)) { scope.raiseNotImplemented()
scope.raiseNotImplemented("bitAnd for ${objClass.className}")
}
} }
open suspend fun bitOr(scope: Scope, other: Obj): Obj { open suspend fun bitOr(scope: Scope, other: Obj): Obj {
return invokeInstanceMethod(scope, "bitOr", Arguments(other)) { scope.raiseNotImplemented()
scope.raiseNotImplemented("bitOr for ${objClass.className}")
}
} }
open suspend fun bitXor(scope: Scope, other: Obj): Obj { open suspend fun bitXor(scope: Scope, other: Obj): Obj {
return invokeInstanceMethod(scope, "bitXor", Arguments(other)) { scope.raiseNotImplemented()
scope.raiseNotImplemented("bitXor for ${objClass.className}")
}
} }
open suspend fun shl(scope: Scope, other: Obj): Obj { open suspend fun shl(scope: Scope, other: Obj): Obj {
return invokeInstanceMethod(scope, "shl", Arguments(other)) { scope.raiseNotImplemented()
scope.raiseNotImplemented("shl for ${objClass.className}")
}
} }
open suspend fun shr(scope: Scope, other: Obj): Obj { open suspend fun shr(scope: Scope, other: Obj): Obj {
return invokeInstanceMethod(scope, "shr", Arguments(other)) { scope.raiseNotImplemented()
scope.raiseNotImplemented("shr for ${objClass.className}")
}
} }
open suspend fun bitNot(scope: Scope): Obj { open suspend fun bitNot(scope: Scope): Obj {
return invokeInstanceMethod(scope, "bitNot", Arguments.EMPTY) { scope.raiseNotImplemented()
scope.raiseNotImplemented("bitNot for ${objClass.className}")
}
} }
open suspend fun assign(scope: Scope, other: Obj): Obj? = null open suspend fun assign(scope: Scope, other: Obj): Obj? = null
@@ -374,43 +292,15 @@ open class Obj {
* if( the operation is not defined, it returns null and the compiler would try * if( the operation is not defined, it returns null and the compiler would try
* to generate it as 'this = this + other', reassigning its variable * to generate it as 'this = this + other', reassigning its variable
*/ */
open suspend fun plusAssign(scope: Scope, other: Obj): Obj? { open suspend fun plusAssign(scope: Scope, other: Obj): Obj? = null
val m = objClass.getInstanceMemberOrNull("plusAssign") ?: scope.findExtension(objClass, "plusAssign")
return if (m != null) {
invokeInstanceMethod(scope, "plusAssign", Arguments(other))
} else null
}
/** /**
* `-=` operations, see [plusAssign] * `-=` operations, see [plusAssign]
*/ */
open suspend fun minusAssign(scope: Scope, other: Obj): Obj? { open suspend fun minusAssign(scope: Scope, other: Obj): Obj? = null
val m = objClass.getInstanceMemberOrNull("minusAssign") ?: scope.findExtension(objClass, "minusAssign") open suspend fun mulAssign(scope: Scope, other: Obj): Obj? = null
return if (m != null) { open suspend fun divAssign(scope: Scope, other: Obj): Obj? = null
invokeInstanceMethod(scope, "minusAssign", Arguments(other)) open suspend fun modAssign(scope: Scope, other: Obj): Obj? = null
} else null
}
open suspend fun mulAssign(scope: Scope, other: Obj): Obj? {
val m = objClass.getInstanceMemberOrNull("mulAssign") ?: scope.findExtension(objClass, "mulAssign")
return if (m != null) {
invokeInstanceMethod(scope, "mulAssign", Arguments(other))
} else null
}
open suspend fun divAssign(scope: Scope, other: Obj): Obj? {
val m = objClass.getInstanceMemberOrNull("divAssign") ?: scope.findExtension(objClass, "divAssign")
return if (m != null) {
invokeInstanceMethod(scope, "divAssign", Arguments(other))
} else null
}
open suspend fun modAssign(scope: Scope, other: Obj): Obj? {
val m = objClass.getInstanceMemberOrNull("modAssign") ?: scope.findExtension(objClass, "modAssign")
return if (m != null) {
invokeInstanceMethod(scope, "modAssign", Arguments(other))
} else null
}
open suspend fun getAndIncrement(scope: Scope): Obj { open suspend fun getAndIncrement(scope: Scope): Obj {
scope.raiseNotImplemented() scope.raiseNotImplemented()
@@ -442,52 +332,29 @@ open class Obj {
// suspend fun <T> sync(block: () -> T): T = monitor.withLock { block() } // suspend fun <T> sync(block: () -> T): T = monitor.withLock { block() }
open suspend fun readField(scope: Scope, name: String): ObjRecord { open suspend fun readField(scope: Scope, name: String): ObjRecord {
// 0. Prefer private member of current class context
scope.currentClassCtx?.let { caller ->
caller.members[name]?.let { rec ->
if (rec.visibility == Visibility.Private && !rec.isAbstract) {
val resolved = resolveRecord(scope, rec, name, caller)
if (resolved.type == ObjRecord.Type.Fun && resolved.value is Statement)
return resolved.copy(value = resolved.value.invoke(scope, this, Arguments.EMPTY, caller))
return resolved
}
}
}
// 1. Hierarchy members (excluding root fallback) // 1. Hierarchy members (excluding root fallback)
for (cls in objClass.mro) { for (cls in objClass.mro) {
if (cls.className == "Obj") break if (cls.className == "Obj") break
val rec = cls.members[name] ?: cls.classScope?.objects?.get(name) val rec = cls.members[name] ?: cls.classScope?.objects?.get(name)
if (rec != null && !rec.isAbstract) { if (rec != null) {
val decl = rec.declaringClass ?: cls if (!rec.isAbstract) {
val resolved = resolveRecord(scope, rec, name, decl) return resolveRecord(scope, rec, name, rec.declaringClass)
if (resolved.type == ObjRecord.Type.Fun && resolved.value is Statement) }
return resolved.copy(value = resolved.value.invoke(scope, this, Arguments.EMPTY, decl))
return resolved
} }
} }
// 2. Extensions // 2. Extensions
val extension = scope.findExtension(objClass, name) val extension = scope.findExtension(objClass, name)
if (extension != null) { if (extension != null) {
val resolved = resolveRecord(scope, extension, name, extension.declaringClass) return resolveRecord(scope, extension, name, extension.declaringClass)
if (resolved.type == ObjRecord.Type.Fun && resolved.value is Statement)
return resolved.copy(value = resolved.value.invoke(scope, this, Arguments.EMPTY, extension.declaringClass))
return resolved
} }
// 3. Root fallback // 3. Root fallback
for (cls in objClass.mro) { for (cls in objClass.mro) {
if (cls.className == "Obj") { if (cls.className == "Obj") {
cls.members[name]?.let { rec -> cls.members[name]?.let {
val decl = rec.declaringClass ?: cls val decl = it.declaringClass ?: cls
val caller = scope.currentClassCtx return resolveRecord(scope, it, name, decl)
if (!canAccessMember(rec.visibility, decl, caller))
scope.raiseError(ObjIllegalAccessException(scope, "can't access field ${name}: not visible (declared in ${decl.className}, caller ${caller?.className ?: "?"})"))
val resolved = resolveRecord(scope, rec, name, decl)
if (resolved.type == ObjRecord.Type.Fun && resolved.value is Statement)
return resolved.copy(value = resolved.value.invoke(scope, this, Arguments.EMPTY, decl))
return resolved
} }
} }
} }
@@ -497,32 +364,20 @@ open class Obj {
) )
} }
open suspend fun resolveRecord(scope: Scope, obj: ObjRecord, name: String, decl: ObjClass?): ObjRecord { protected open suspend fun resolveRecord(scope: Scope, obj: ObjRecord, name: String, decl: ObjClass?): ObjRecord {
if (obj.type == ObjRecord.Type.Delegated) { if (obj.type == ObjRecord.Type.Delegated) {
val del = obj.delegate ?: scope.raiseError("Internal error: delegated property $name has no delegate") val del = obj.delegate ?: scope.raiseError("Internal error: delegated property $name has no delegate")
val th = if (this === ObjVoid) ObjNull else this
val res = del.invokeInstanceMethod(scope, "getValue", Arguments(th, ObjString(name)), onNotFoundResult = {
// If getValue not found, return a wrapper that calls invoke
object : Statement() {
override val pos: Pos = Pos.builtIn
override suspend fun execute(s: Scope): Obj {
val th2 = if (s.thisObj === ObjVoid) ObjNull else s.thisObj
val allArgs = (listOf(th2, ObjString(name)) + s.args.list).toTypedArray()
return del.invokeInstanceMethod(s, "invoke", Arguments(*allArgs))
}
}
})
return obj.copy( return obj.copy(
value = res, value = del.invokeInstanceMethod(scope, "getValue", Arguments(this, ObjString(name))),
type = ObjRecord.Type.Other type = ObjRecord.Type.Other
) )
} }
val value = obj.value val value = obj.value
if (value is ObjProperty || obj.type == ObjRecord.Type.Property) { if (value is ObjProperty) {
val prop = if (value is ObjProperty) value else (value as? Statement)?.execute(scope.createChildScope(scope.pos, newThisObj = this)) as? ObjProperty return ObjRecord(value.callGetter(scope, this, decl), obj.isMutable)
?: scope.raiseError("Expected ObjProperty for property member $name, got ${value::class}") }
val res = prop.callGetter(scope, this, decl) if (value is Statement && decl != null) {
return ObjRecord(res, obj.isMutable) return ObjRecord(value.execute(scope.createChildScope(scope.pos, newThisObj = this)), obj.isMutable)
} }
val caller = scope.currentClassCtx val caller = scope.currentClassCtx
// Check visibility for non-property members here if they weren't checked before // Check visibility for non-property members here if they weren't checked before
@@ -534,17 +389,7 @@ open class Obj {
open suspend fun writeField(scope: Scope, name: String, newValue: Obj) { open suspend fun writeField(scope: Scope, name: String, newValue: Obj) {
willMutate(scope) willMutate(scope)
var field: ObjRecord? = null var field: ObjRecord? = null
// 0. Prefer private member of current class context
scope.currentClassCtx?.let { caller ->
caller.members[name]?.let { rec ->
if (rec.visibility == Visibility.Private && !rec.isAbstract) {
field = rec
}
}
}
// 1. Hierarchy members (excluding root fallback) // 1. Hierarchy members (excluding root fallback)
if (field == null) {
for (cls in objClass.mro) { for (cls in objClass.mro) {
if (cls.className == "Obj") break if (cls.className == "Obj") break
val rec = cls.members[name] ?: cls.classScope?.objects?.get(name) val rec = cls.members[name] ?: cls.classScope?.objects?.get(name)
@@ -553,7 +398,6 @@ open class Obj {
break break
} }
} }
}
// 2. Extensions // 2. Extensions
if (field == null) { if (field == null) {
field = scope.findExtension(objClass, name) field = scope.findExtension(objClass, name)
@@ -585,19 +429,12 @@ open class Obj {
} }
open suspend fun getAt(scope: Scope, index: Obj): Obj { open suspend fun getAt(scope: Scope, index: Obj): Obj {
if (index is ObjString) {
return readField(scope, index.value).value
}
scope.raiseNotImplemented("indexing") scope.raiseNotImplemented("indexing")
} }
suspend fun getAt(scope: Scope, index: Int): Obj = getAt(scope, ObjInt(index.toLong())) suspend fun getAt(scope: Scope, index: Int): Obj = getAt(scope, ObjInt(index.toLong()))
open suspend fun putAt(scope: Scope, index: Obj, newValue: Obj) { open suspend fun putAt(scope: Scope, index: Obj, newValue: Obj) {
if (index is ObjString) {
writeField(scope, index.value, newValue)
return
}
scope.raiseNotImplemented("indexing") scope.raiseNotImplemented("indexing")
} }
@@ -605,16 +442,17 @@ open class Obj {
scope.raiseNotImplemented() scope.raiseNotImplemented()
} }
suspend fun invoke(scope: Scope, thisObj: Obj, args: Arguments, declaringClass: ObjClass? = null): Obj = suspend fun invoke(scope: Scope, thisObj: Obj, args: Arguments, declaringClass: ObjClass? = null): Obj {
if (PerfFlags.SCOPE_POOL) if (PerfFlags.SCOPE_POOL) {
scope.withChildFrame(args, newThisObj = thisObj) { child -> return scope.withChildFrame(args, newThisObj = thisObj) { child ->
if (declaringClass != null) child.currentClassCtx = declaringClass if (declaringClass != null) child.currentClassCtx = declaringClass
callOn(child) callOn(child)
} }
else }
callOn(scope.createChildScope(scope.pos, args = args, newThisObj = thisObj).also { val child = scope.createChildScope(scope.pos, args = args, newThisObj = thisObj)
if (declaringClass != null) it.currentClassCtx = declaringClass if (declaringClass != null) child.currentClassCtx = declaringClass
}) return callOn(child)
}
suspend fun invoke(scope: Scope, thisObj: Obj, vararg args: Obj): Obj = suspend fun invoke(scope: Scope, thisObj: Obj, vararg args: Obj): Obj =
callOn( callOn(
@@ -677,46 +515,20 @@ open class Obj {
companion object { companion object {
val rootObjectType = ObjClass("Obj").apply { val rootObjectType = ObjClass("Obj").apply {
addFnDoc( addFn("toString", true) {
name = "toString",
doc = "Returns a string representation of the object.",
returns = type("lyng.String"),
moduleName = "lyng.stdlib"
) {
thisObj.toString(this, true) thisObj.toString(this, true)
} }
addFnDoc( addFn("inspect", true) {
name = "inspect",
doc = "Returns a detailed string representation for debugging.",
returns = type("lyng.String"),
moduleName = "lyng.stdlib"
) {
thisObj.inspect(this).toObj() thisObj.inspect(this).toObj()
} }
addFnDoc( addFn("contains") {
name = "contains",
doc = "Returns true if the object contains the given element.",
params = listOf(ParamDoc("element")),
returns = type("lyng.Bool"),
moduleName = "lyng.stdlib"
) {
ObjBool(thisObj.contains(this, args.firstAndOnly())) ObjBool(thisObj.contains(this, args.firstAndOnly()))
} }
// utilities // utilities
addFnDoc( addFn("let") {
name = "let",
doc = "Calls the specified function block with `this` value as its argument and returns its result.",
params = listOf(ParamDoc("block")),
moduleName = "lyng.stdlib"
) {
args.firstAndOnly().callOn(createChildScope(Arguments(thisObj))) args.firstAndOnly().callOn(createChildScope(Arguments(thisObj)))
} }
addFnDoc( addFn("apply") {
name = "apply",
doc = "Calls the specified function block with `this` value as its receiver and returns `this` value.",
params = listOf(ParamDoc("block")),
moduleName = "lyng.stdlib"
) {
val body = args.firstAndOnly() val body = args.firstAndOnly()
(thisObj as? ObjInstance)?.let { (thisObj as? ObjInstance)?.let {
body.callOn(ApplyScope(this, it.instanceScope)) body.callOn(ApplyScope(this, it.instanceScope))
@@ -725,21 +537,11 @@ open class Obj {
} }
thisObj thisObj
} }
addFnDoc( addFn("also") {
name = "also",
doc = "Calls the specified function block with `this` value as its argument and returns `this` value.",
params = listOf(ParamDoc("block")),
moduleName = "lyng.stdlib"
) {
args.firstAndOnly().callOn(createChildScope(Arguments(thisObj))) args.firstAndOnly().callOn(createChildScope(Arguments(thisObj)))
thisObj thisObj
} }
addFnDoc( addFn("run") {
name = "run",
doc = "Calls the specified function block with `this` value as its receiver and returns its result.",
params = listOf(ParamDoc("block")),
moduleName = "lyng.stdlib"
) {
args.firstAndOnly().callOn(this) args.firstAndOnly().callOn(this)
} }
addFn("getAt") { addFn("getAt") {
@@ -752,18 +554,15 @@ open class Obj {
thisObj.putAt(this, requiredArg<Obj>(0), newValue) thisObj.putAt(this, requiredArg<Obj>(0), newValue)
newValue newValue
} }
addFnDoc( addFn("toJsonString") {
name = "toJsonString",
doc = "Encodes this object to a JSON string.",
returns = type("lyng.String"),
moduleName = "lyng.stdlib"
) {
thisObj.toJson(this).toString().toObj() thisObj.toJson(this).toString().toObj()
} }
} }
fun from(obj: Any?): Obj { @Suppress("NOTHING_TO_INLINE")
inline fun from(obj: Any?): Obj {
@Suppress("UNCHECKED_CAST")
return when (obj) { return when (obj) {
is Obj -> obj is Obj -> obj
is Double -> ObjReal(obj) is Double -> ObjReal(obj)
@@ -772,16 +571,16 @@ open class Obj {
is Long -> ObjInt.of(obj) is Long -> ObjInt.of(obj)
is String -> ObjString(obj) is String -> ObjString(obj)
is CharSequence -> ObjString(obj.toString()) is CharSequence -> ObjString(obj.toString())
is Char -> ObjChar(obj)
is Boolean -> ObjBool(obj) is Boolean -> ObjBool(obj)
is Set<*> -> ObjSet(obj.map { from(it) }.toMutableSet()) is Set<*> -> ObjSet((obj as Set<Obj>).toMutableSet())
is List<*> -> ObjList(obj.map { from(it) }.toMutableList())
is Map<*, *> -> ObjMap(obj.entries.associate { from(it.key) to from(it.value) }.toMutableMap())
is Map.Entry<*, *> -> ObjMapEntry(from(obj.key), from(obj.value))
is Enum<*> -> ObjString(obj.name)
Unit -> ObjVoid Unit -> ObjVoid
null -> ObjNull null -> ObjNull
is Iterator<*> -> ObjKotlinIterator(obj as Iterator<Any?>) is Iterator<*> -> ObjKotlinIterator(obj)
is Map.Entry<*, *> -> {
obj as MutableMap.MutableEntry<Obj, Obj>
ObjMapEntry(obj.key, obj.value)
}
else -> throw IllegalArgumentException("cannot convert to Obj: $obj") else -> throw IllegalArgumentException("cannot convert to Obj: $obj")
} }
} }
@@ -1,5 +1,5 @@
/* /*
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com * Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -17,7 +17,10 @@
package net.sergeych.lyng.obj package net.sergeych.lyng.obj
import net.sergeych.lyng.miniast.* import net.sergeych.lyng.miniast.ParamDoc
import net.sergeych.lyng.miniast.TypeGenericDoc
import net.sergeych.lyng.miniast.addFnDoc
import net.sergeych.lyng.miniast.type
val ObjArray by lazy { val ObjArray by lazy {
@@ -49,35 +52,32 @@ val ObjArray by lazy {
ObjFalse ObjFalse
} }
addPropertyDoc( addFnDoc(
name = "last", name = "last",
doc = "The last element of this array.", doc = "The last element of this array.",
type = type("lyng.Any"), returns = type("lyng.Any"),
moduleName = "lyng.stdlib", moduleName = "lyng.stdlib"
getter = { ) {
this.thisObj.invokeInstanceMethod( thisObj.invokeInstanceMethod(
this, this,
"getAt", "getAt",
(this.thisObj.invokeInstanceMethod(this, "size").toInt() - 1).toObj() (thisObj.invokeInstanceMethod(this, "size").toInt() - 1).toObj()
) )
} }
)
addPropertyDoc( addFnDoc(
name = "lastIndex", name = "lastIndex",
doc = "Index of the last element (size - 1).", doc = "Index of the last element (size - 1).",
type = type("lyng.Int"), returns = type("lyng.Int"),
moduleName = "lyng.stdlib", moduleName = "lyng.stdlib"
getter = { (this.thisObj.invokeInstanceMethod(this, "size").toInt() - 1).toObj() } ) { (thisObj.invokeInstanceMethod(this, "size").toInt() - 1).toObj() }
)
addPropertyDoc( addFnDoc(
name = "indices", name = "indices",
doc = "Range of valid indices for this array.", doc = "Range of valid indices for this array.",
type = type("lyng.Range"), returns = type("lyng.Range"),
moduleName = "lyng.stdlib", moduleName = "lyng.stdlib"
getter = { ObjRange(0.toObj(), this.thisObj.invokeInstanceMethod(this, "size"), false) } ) { ObjRange(0.toObj(), thisObj.invokeInstanceMethod(this, "size"), false) }
)
addFnDoc( addFnDoc(
name = "binarySearch", name = "binarySearch",
Loaded 100 of 171 files, more files were not shown because too many files have changed in this diff. Show more