Compare commits
86
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5144a720ba | ||
|
|
bc6613ec01 | ||
|
|
59aefc5bc2 | ||
|
|
fac58675d5 | ||
|
|
22f6c149db | ||
|
|
6d8b60fcfa | ||
|
|
54ecffc803 | ||
|
|
0731d63adf | ||
|
|
434d74b07f | ||
|
|
55b27f831f | ||
|
|
5586e027ea | ||
|
|
cd2b1a9cb7 | ||
|
|
3b6504d3b1 | ||
|
|
357585d3ba | ||
|
|
3a9c567c32 | ||
|
|
8300c2a3ab | ||
|
|
17b4904ae3 | ||
|
|
7e8f1406b5 | ||
|
|
3f235878c0 | ||
|
|
5f3a54d08f | ||
|
|
157b716eb7 | ||
|
|
c9f96464f3 | ||
|
|
0732202c80 | ||
|
|
1e18a162c4 | ||
|
|
0dbfb473bf | ||
|
|
0add0ab54c | ||
|
|
3acd56f55a | ||
|
|
b7838b45ec | ||
|
|
92cb088f36 | ||
|
|
3dfe98a93d | ||
|
|
f6d51e9b94 | ||
|
|
3ac7fd7ceb | ||
|
|
99f883cfc7 | ||
|
|
76a1804dc1 | ||
|
|
35cc8fa930 | ||
|
|
6f86e6ff97 | ||
|
|
fa91afa92b | ||
|
|
00503fa105 | ||
|
|
f7a4497f3f | ||
|
|
1aef64e0fa | ||
|
|
5d8fdce637 | ||
|
|
5a8881bfd5 | ||
|
|
d487886c8f | ||
|
|
180471e4cd | ||
|
|
71a37a2906 | ||
|
|
ab05f83e77 | ||
|
|
9e11519608 | ||
|
|
a2d26fc777 | ||
|
|
dd1a1544c6 | ||
|
|
fba44622e5 | ||
|
|
2737aaa14e | ||
|
|
bce88ced43 | ||
|
|
fd473a32d8 | ||
|
|
d15dfb6087 | ||
|
|
b953282251 | ||
|
|
bcabfc8962 | ||
|
|
c0fab3d60e | ||
|
|
55caa65f97 | ||
|
|
b73891d19b | ||
|
|
8750040926 | ||
|
|
708b908415 | ||
|
|
c35d684df1 | ||
|
|
678cfbf45e | ||
|
|
bfffea7e69 | ||
|
|
40f11b6f29 | ||
|
|
e25fc95cbf | ||
|
|
a6085b11a1 | ||
|
|
819fdd82b3 | ||
|
|
2e96d75b9f | ||
|
|
f616326383 | ||
|
|
1e2bbe1fc5 | ||
|
|
b630d69186 | ||
|
|
20f4e54a02 | ||
|
|
e58896f087 | ||
|
|
080eac2e1a | ||
|
|
a31befef0b | ||
|
|
65a7555e93 | ||
|
|
84f2f8fac4 | ||
|
|
0c31ec63ee | ||
|
|
603023962e | ||
|
|
e765784170 | ||
|
|
171e413c5f | ||
|
|
5cfc15cf17 | ||
|
|
b8f27c7a18 | ||
|
|
6a6de83972 | ||
|
|
a3b8dbd9d8 |
No files matched your search
@@ -16,3 +16,7 @@ xcuserdata
|
||||
/test.lyng
|
||||
/sample_texts/1.txt.gz
|
||||
/kotlin-js-store/wasm/yarn.lock
|
||||
/distributables
|
||||
.output*.txt
|
||||
debug.log
|
||||
/build.log
|
||||
@@ -0,0 +1,28 @@
|
||||
<component name="ProjectRunConfigurationManager">
|
||||
<configuration default="false" name="Tests in 'lyng.lynglib.jvmTest'" type="GradleRunConfiguration" factoryName="Gradle">
|
||||
<ExternalSystemSettings>
|
||||
<option name="executionName" />
|
||||
<option name="externalProjectPath" value="$PROJECT_DIR$" />
|
||||
<option name="externalSystemIdString" value="GRADLE" />
|
||||
<option name="scriptParameters" value="" />
|
||||
<option name="taskDescriptions">
|
||||
<list />
|
||||
</option>
|
||||
<option name="taskNames">
|
||||
<list>
|
||||
<option value=":lynglib:cleanJvmTest" />
|
||||
<option value=":lynglib:jvmTest" />
|
||||
</list>
|
||||
</option>
|
||||
<option name="vmOptions" />
|
||||
</ExternalSystemSettings>
|
||||
<ExternalSystemDebugServerProcess>false</ExternalSystemDebugServerProcess>
|
||||
<ExternalSystemReattachDebugProcess>true</ExternalSystemReattachDebugProcess>
|
||||
<ExternalSystemDebugDisabled>false</ExternalSystemDebugDisabled>
|
||||
<DebugAllEnabled>false</DebugAllEnabled>
|
||||
<RunAsTest>true</RunAsTest>
|
||||
<GradleProfilingDisabled>false</GradleProfilingDisabled>
|
||||
<GradleCoverageDisabled>false</GradleCoverageDisabled>
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
</component>
|
||||
@@ -1,3 +1,20 @@
|
||||
<!--
|
||||
~ Copyright 2025 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.
|
||||
~
|
||||
-->
|
||||
|
||||
<component name="ProjectRunConfigurationManager">
|
||||
<configuration default="false" name="lyng:site [jsBrowserDevelopmentRun]" type="GradleRunConfiguration" factoryName="Gradle">
|
||||
<ExternalSystemSettings>
|
||||
@@ -17,8 +34,11 @@
|
||||
</ExternalSystemSettings>
|
||||
<ExternalSystemDebugServerProcess>true</ExternalSystemDebugServerProcess>
|
||||
<ExternalSystemReattachDebugProcess>true</ExternalSystemReattachDebugProcess>
|
||||
<ExternalSystemDebugDisabled>false</ExternalSystemDebugDisabled>
|
||||
<DebugAllEnabled>false</DebugAllEnabled>
|
||||
<RunAsTest>false</RunAsTest>
|
||||
<GradleProfilingDisabled>true</GradleProfilingDisabled>
|
||||
<GradleCoverageDisabled>true</GradleCoverageDisabled>
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
</component>
|
||||
@@ -2,6 +2,29 @@
|
||||
|
||||
### Unreleased
|
||||
|
||||
- Language: Class properties with accessors
|
||||
- Support for `val` (read-only) and `var` (read-write) properties in classes.
|
||||
- Syntax: `val name [ : Type ] get() { body }` or `var name [ : Type ] get() { body } set(value) { body }`.
|
||||
- Laconic Expression Shorthand: `val prop get() = expression` and `var prop get() = read set(v) = write`.
|
||||
- Properties are pure accessors and do **not** have automatic backing fields.
|
||||
- Validation: `var` properties must have both accessors; `val` must have only a getter.
|
||||
- Integration: Updated TextMate grammar and IntelliJ plugin (highlighting + keywords).
|
||||
- Documentation: New "Properties" section in `docs/OOP.md`.
|
||||
|
||||
- Docs: Scopes and Closures guidance
|
||||
- New page: `docs/scopes_and_closures.md` detailing `ClosureScope` resolution order, recursion‑safe helpers (`chainLookupIgnoreClosure`, `chainLookupWithMembers`, `baseGetIgnoreClosure`), cycle prevention, and capturing lexical environments for callbacks (`snapshotForClosure`).
|
||||
- Updated: `docs/advanced_topics.md` (link to the new page), `docs/parallelism.md` (closures in `launch`/`flow`), `docs/OOP.md` (visibility from closures with preserved `currentClassCtx`), `docs/exceptions_handling.md` (compatibility alias `SymbolNotFound`).
|
||||
- Tutorial: added quick link to Scopes and Closures.
|
||||
|
||||
- IDEA plugin: Lightweight autocompletion (experimental)
|
||||
- Global completion: local declarations, in‑scope parameters, imported modules, and stdlib symbols.
|
||||
- Member completion: after a dot, suggests only members of the inferred receiver type (incl. chained calls like `Path(".." ).lines().` → `Iterator` methods). No global identifiers appear after a dot.
|
||||
- Inheritance-aware: direct class members first, then inherited (e.g., `List` includes `Collection`/`Iterable` methods).
|
||||
- Heuristics: handles literals (`"…"` → `String`, numbers → `Int/Real`, `[...]` → `List`, `{...}` → `Dict`) and static `Namespace.` members.
|
||||
- Performance: capped results, early prefix filtering, per‑document MiniAst cache, cancellation checks.
|
||||
- Toggle: Settings | Lyng Formatter → "Enable Lyng autocompletion (experimental)" (default ON).
|
||||
- Stabilization: DEBUG completion/Quick Doc logs are OFF by default; behavior aligned between IDE and isolated engine tests.
|
||||
|
||||
- Language: Named arguments and named splats
|
||||
- New call-site syntax for named arguments using colon: `name: value`.
|
||||
- Positional arguments must come before named; positionals after a named argument inside parentheses are rejected.
|
||||
@@ -43,3 +66,27 @@
|
||||
Notes:
|
||||
- Existing single-inheritance code continues to work; resolution reduces to the single base.
|
||||
- If code previously relied on non-deterministic parent set iteration, C3 MRO provides a predictable order; disambiguate explicitly if needed using `this@Type`/casts.
|
||||
|
||||
# Changelog
|
||||
|
||||
All notable changes to this project will be documented in this file.
|
||||
|
||||
## Unreleased
|
||||
|
||||
- CLI: Added `fmt` as a first-class Clikt subcommand.
|
||||
- Default behavior: formats files to stdout (no in-place edits by default).
|
||||
- Options:
|
||||
- `--check`: check only; print files that would change; exit with code 2 if any changes are needed.
|
||||
- `-i, --in-place`: write formatted result back to files.
|
||||
- `--spacing`: apply spacing normalization.
|
||||
- `--wrap`, `--wrapping`: enable line wrapping.
|
||||
- Mutually exclusive: `--check` and `--in-place` together now produce an error and exit with code 1.
|
||||
- Multi-file stdout prints headers `--- <path> ---` per file.
|
||||
- `lyng --help` shows `fmt`; `lyng fmt --help` displays dedicated help.
|
||||
|
||||
- CLI: Preserved legacy script invocation fast-paths:
|
||||
- `lyng script.lyng [args...]` executes the script directly.
|
||||
- `lyng -- -file.lyng [args...]` executes a script whose name begins with `-`.
|
||||
|
||||
- CLI: Fixed a regression where the root help banner could print before subcommands.
|
||||
- Root command no longer prints help when a subcommand (e.g., `fmt`) is invoked.
|
||||
@@ -4,11 +4,15 @@ Please visit the project homepage: [https://lynglang.com](https://lynglang.com)
|
||||
|
||||
- simple, compact, intuitive and elegant modern code:
|
||||
|
||||
```
|
||||
class Point(x,y) {
|
||||
```lyng
|
||||
class Point(x, y) {
|
||||
fun dist() { sqrt(x*x + y*y) }
|
||||
}
|
||||
Point(3,4).dist() //< 5
|
||||
|
||||
// Auto-named arguments shorthand (x: is x: x):
|
||||
val x = 3
|
||||
val y = 4
|
||||
Point(x:, y:).dist() //< 5
|
||||
|
||||
fun swapEnds(first, args..., last, f) {
|
||||
f( last, ...args, first)
|
||||
@@ -39,6 +43,8 @@ and it is multithreaded on platforms supporting it (automatically, no code chang
|
||||
|
||||
- [Language home](https://lynglang.com)
|
||||
- [introduction and tutorial](docs/tutorial.md) - start here please
|
||||
- [Testing and Assertions](docs/Testing.md)
|
||||
- [Efficient Iterables in Kotlin Interop](docs/EfficientIterables.md)
|
||||
- [Samples directory](docs/samples)
|
||||
- [Formatter (core + CLI + IDE)](docs/formatter.md)
|
||||
- [Books directory](docs)
|
||||
@@ -117,6 +123,28 @@ scope.eval("sumOf(1,2,3)") // <- 6
|
||||
```
|
||||
Note that the scope stores all changes in it so you can make calls on a single scope to preserve state between calls.
|
||||
|
||||
## IntelliJ IDEA plugin: Lightweight autocompletion (experimental)
|
||||
|
||||
The IDEA plugin provides a fast, lightweight BASIC completion for Lyng code (IntelliJ IDEA 2024.3+).
|
||||
|
||||
What it does:
|
||||
- Global suggestions: in-scope parameters, same-file declarations (functions/classes/vals), imported modules, and stdlib symbols.
|
||||
- Member completion after dot: offers only members of the inferred receiver type. It works for chained calls like `Path(".." ).lines().` (suggests `Iterator` methods), and for literals like `"abc".` (String methods) or `[1,2,3].` (List/Iterable methods).
|
||||
- Inheritance-aware: shows direct class members first, then inherited. For example, `List` also exposes common `Collection`/`Iterable` methods.
|
||||
- Static/namespace members: `Name.` lists only static members when `Name` is a known class or container (e.g., `Math`).
|
||||
- Performance: suggestions are capped; prefix filtering is early; parsing is cached; computation is cancellation-friendly.
|
||||
|
||||
What it does NOT do (yet):
|
||||
- No heavy resolve or project-wide indexing. It’s best-effort, driven by a tiny MiniAst + built-in docs registry.
|
||||
- No control/data-flow type inference.
|
||||
|
||||
Enable/disable:
|
||||
- Settings | Lyng Formatter → "Enable Lyng autocompletion (experimental)" (default: ON).
|
||||
|
||||
Tips:
|
||||
- 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).
|
||||
|
||||
## Why?
|
||||
|
||||
Designed to add scripting to kotlin multiplatform application in easy and efficient way. This is attempt to achieve what Lua is for C/++.
|
||||
@@ -162,6 +190,7 @@ Ready features:
|
||||
- [x] better stack reporting
|
||||
- [x] regular exceptions + extended `when`
|
||||
- [x] multiple inheritance for user classes
|
||||
- [x] class properties (accessors)
|
||||
|
||||
## plan: towards v1.5 Enhancing
|
||||
|
||||
|
||||
+12
-2
@@ -82,7 +82,7 @@ function updateIdeaPluginDownloadLink() {
|
||||
# default target settings
|
||||
case "com" in
|
||||
com)
|
||||
SSH_HOST=sergeych@lynglang.com # host to deploy to
|
||||
SSH_HOST=sergeych@d.lynglang.com # host to deploy to
|
||||
SSH_PORT=22 # ssh port on it
|
||||
ROOT=/bigstore/sergeych_pub/lyng # directory to rsync to
|
||||
;;
|
||||
@@ -98,10 +98,20 @@ esac
|
||||
|
||||
die() { echo "ERROR: $*" 1>&2 ; exit 1; }
|
||||
|
||||
function refreshTextmateZip() {
|
||||
echo "Refreshing distributables/lyng-textmate.zip from editors/..."
|
||||
mkdir -p distributables
|
||||
# We use -r for recursive and -q for quiet (optional)
|
||||
# -j can be used if we want to junk paths, but the request says "contents of editors/"
|
||||
# usually we want to preserve the structure inside editors/
|
||||
(cd editors && zip -rq ../distributables/lyng-textmate.zip .)
|
||||
}
|
||||
|
||||
# Update the IDEA plugin download link in docs (temporarily), then build, then restore the doc
|
||||
refreshTextmateZip
|
||||
updateIdeaPluginDownloadLink || echo "WARN: proceeding without updating IDEA plugin download link"
|
||||
|
||||
./gradlew site:clean site:jsBrowserDistribution
|
||||
./gradlew site:jsBrowserDistribution
|
||||
BUILD_RC=$?
|
||||
|
||||
# Always restore original doc if backup exists
|
||||
|
||||
Executable
+256
@@ -0,0 +1,256 @@
|
||||
#!/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
|
||||
|
||||
# Configuration
|
||||
DOCS_DIR="docs"
|
||||
OUTPUT_DIR="distributables"
|
||||
TEMP_DIR="build/temp_docs"
|
||||
MERGED_MD="$TEMP_DIR/merged.md"
|
||||
OUTPUT_HTML="$OUTPUT_DIR/lyng_documentation.html"
|
||||
|
||||
mkdir -p "$OUTPUT_DIR"
|
||||
mkdir -p "$TEMP_DIR"
|
||||
|
||||
# Files that should come first in specific order
|
||||
PRIORITY_FILES=(
|
||||
"tutorial.md"
|
||||
"OOP.md"
|
||||
"advanced_topics.md"
|
||||
"declaring_arguments.md"
|
||||
"scopes_and_closures.md"
|
||||
"exceptions_handling.md"
|
||||
"when.md"
|
||||
"parallelism.md"
|
||||
"Testing.md"
|
||||
)
|
||||
|
||||
# Files that should come next (reference)
|
||||
REFERENCE_FILES=(
|
||||
"Collection.md"
|
||||
"Iterable.md"
|
||||
"Iterator.md"
|
||||
"List.md"
|
||||
"Set.md"
|
||||
"Map.md"
|
||||
"Array.md"
|
||||
"Buffer.md"
|
||||
"RingBuffer.md"
|
||||
"Range.md"
|
||||
"Real.md"
|
||||
"Regex.md"
|
||||
"math.md"
|
||||
"time.md"
|
||||
)
|
||||
|
||||
# Files that are about integration/tools
|
||||
INTEGRATION_FILES=(
|
||||
"serialization.md"
|
||||
"json_and_kotlin_serialization.md"
|
||||
"embedding.md"
|
||||
"lyng_cli.md"
|
||||
"lyng.io.fs.md"
|
||||
"formatter.md"
|
||||
"EfficientIterables.md"
|
||||
)
|
||||
|
||||
# Tracking processed files to avoid duplicates
|
||||
PROCESSED_PATHS=()
|
||||
|
||||
is_excluded() {
|
||||
local full_path="$1"
|
||||
if grep -q "excludeFromIndex" "$full_path"; then
|
||||
return 0 # true in bash
|
||||
fi
|
||||
return 1 # false
|
||||
}
|
||||
|
||||
process_file() {
|
||||
local rel_path="$1"
|
||||
local full_path="$DOCS_DIR/$rel_path"
|
||||
|
||||
if [[ ! -f "$full_path" ]]; then
|
||||
return
|
||||
fi
|
||||
|
||||
if is_excluded "$full_path"; then
|
||||
echo "Skipping excluded: $rel_path"
|
||||
return
|
||||
fi
|
||||
|
||||
# Check for duplicates
|
||||
for p in "${PROCESSED_PATHS[@]}"; do
|
||||
if [[ "$p" == "$rel_path" ]]; then
|
||||
return
|
||||
fi
|
||||
done
|
||||
PROCESSED_PATHS+=("$rel_path")
|
||||
|
||||
echo "Processing: $rel_path"
|
||||
|
||||
# 1. Add an anchor for the file based on its path
|
||||
local anchor_name=$(echo "$rel_path" | sed 's/\//_/g')
|
||||
echo "<div id=\"$anchor_name\"></div>" >> "$MERGED_MD"
|
||||
echo "" >> "$MERGED_MD"
|
||||
|
||||
# 2. Append content with fixed links
|
||||
# - [text](file.md) -> [text](#file.md)
|
||||
# - [text](dir/file.md) -> [text](#dir_file.md)
|
||||
# - [text](file.md#anchor) -> [text](#anchor)
|
||||
# - Fix image links: [alt](../images/...) -> [alt](images/...) if needed, but none found yet.
|
||||
|
||||
cat "$full_path" | \
|
||||
perl -pe 's/\[([^\]]+)\]\(([^)]+)\.md\)/"[$1](#" . ($2 =~ s|\/|_|gr) . ".md)"/ge' | \
|
||||
perl -pe 's/\[([^\]]+)\]\(([^)]+)\.md#([^)]+)\)/[$1](#$3)/g' >> "$MERGED_MD"
|
||||
|
||||
echo -e "\n\n---\n\n" >> "$MERGED_MD"
|
||||
}
|
||||
|
||||
# Start with an empty merged file
|
||||
echo "% Lyng Language Documentation" > "$MERGED_MD"
|
||||
echo "" >> "$MERGED_MD"
|
||||
|
||||
# 1. Process priority files
|
||||
for f in "${PRIORITY_FILES[@]}"; do
|
||||
process_file "$f"
|
||||
done
|
||||
|
||||
# 2. Process reference files
|
||||
for f in "${REFERENCE_FILES[@]}"; do
|
||||
process_file "$f"
|
||||
done
|
||||
|
||||
# 3. Process integration files
|
||||
for f in "${INTEGRATION_FILES[@]}"; do
|
||||
process_file "$f"
|
||||
done
|
||||
|
||||
# 4. Process remaining files in docs root
|
||||
for f in "$DOCS_DIR"/*.md; do
|
||||
rel_f=${f#"$DOCS_DIR/"}
|
||||
process_file "$rel_f"
|
||||
done
|
||||
|
||||
# 5. Process remaining files in subdirs (like samples)
|
||||
find "$DOCS_DIR" -name "*.md" | sort | while read -r f; do
|
||||
rel_f=${f#"$DOCS_DIR/"}
|
||||
process_file "$rel_f"
|
||||
done
|
||||
|
||||
echo "Running pandoc to generate $OUTPUT_HTML..."
|
||||
|
||||
# Use a basic but clean CSS
|
||||
pandoc "$MERGED_MD" -o "$OUTPUT_HTML" \
|
||||
--toc --toc-depth=2 \
|
||||
--standalone \
|
||||
--embed-resources \
|
||||
--metadata title="Lyng Language Documentation" \
|
||||
--css <(echo "
|
||||
body {
|
||||
font-family: -apple-system, BlinkMacSystemFont, \"Segoe UI\", Helvetica, Arial, sans-serif;
|
||||
line-height: 1.6;
|
||||
max-width: 1000px;
|
||||
margin: 0 auto;
|
||||
padding: 2em;
|
||||
color: #24292e;
|
||||
background-color: #fff;
|
||||
}
|
||||
code {
|
||||
background-color: rgba(27,31,35,0.05);
|
||||
padding: 0.2em 0.4em;
|
||||
border-radius: 3px;
|
||||
font-family: SFMono-Regular, Consolas, \"Liberation Mono\", Menlo, monospace;
|
||||
font-size: 85%;
|
||||
}
|
||||
pre {
|
||||
background-color: #f6f8fa;
|
||||
padding: 16px;
|
||||
overflow: auto;
|
||||
border-radius: 3px;
|
||||
line-height: 1.45;
|
||||
}
|
||||
pre code {
|
||||
background-color: transparent;
|
||||
padding: 0;
|
||||
font-size: 100%;
|
||||
}
|
||||
h1 {
|
||||
border-bottom: 1px solid #eaecef;
|
||||
padding-bottom: 0.3em;
|
||||
margin-top: 24px;
|
||||
margin-bottom: 16px;
|
||||
font-weight: 600;
|
||||
}
|
||||
h2 {
|
||||
border-bottom: 1px solid #eaecef;
|
||||
padding-bottom: 0.3em;
|
||||
margin-top: 24px;
|
||||
margin-bottom: 16px;
|
||||
font-weight: 600;
|
||||
}
|
||||
hr {
|
||||
height: 0.25em;
|
||||
padding: 0;
|
||||
margin: 24px 0;
|
||||
background-color: #e1e4e8;
|
||||
border: 0;
|
||||
}
|
||||
blockquote {
|
||||
padding: 0 1em;
|
||||
color: #6a737d;
|
||||
border-left: 0.25em solid #dfe2e5;
|
||||
margin: 0 0 16px 0;
|
||||
}
|
||||
nav#TOC {
|
||||
background: #f9f9f9;
|
||||
padding: 1em;
|
||||
border: 1px solid #eee;
|
||||
margin-bottom: 2.5em;
|
||||
border-radius: 6px;
|
||||
}
|
||||
nav#TOC ul {
|
||||
list-style: none;
|
||||
padding-left: 1.5em;
|
||||
}
|
||||
nav#TOC > ul {
|
||||
padding-left: 0;
|
||||
}
|
||||
table {
|
||||
border-spacing: 0;
|
||||
border-collapse: collapse;
|
||||
margin-top: 0;
|
||||
margin-bottom: 16px;
|
||||
}
|
||||
table th, table td {
|
||||
padding: 6px 13px;
|
||||
border: 1px solid #dfe2e5;
|
||||
}
|
||||
table tr {
|
||||
background-color: #fff;
|
||||
border-top: 1px solid #c6cbd1;
|
||||
}
|
||||
table tr:nth-child(2n) {
|
||||
background-color: #f6f8fa;
|
||||
}
|
||||
")
|
||||
|
||||
echo "-------------------------------------------------------"
|
||||
echo "Done! Documentation generated successfully."
|
||||
echo "Location: $OUTPUT_HTML"
|
||||
echo "-------------------------------------------------------"
|
||||
+3
-3
@@ -21,9 +21,9 @@ set -e
|
||||
|
||||
file=./lyng/build/bin/linuxX64/releaseExecutable/lyng.kexe
|
||||
|
||||
#./gradlew :lyng:linkReleaseExecutableLinuxX64
|
||||
#strip $file
|
||||
#upx $file
|
||||
./gradlew :lyng:linkReleaseExecutableLinuxX64
|
||||
strip $file
|
||||
upx $file
|
||||
cp $file ~/bin/lyng
|
||||
cp $file ./distributables/lyng
|
||||
zip ./distributables/lyng-linuxX64 ./distributables/lyng
|
||||
|
||||
+16
-1
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
|
||||
* 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.
|
||||
@@ -20,3 +20,18 @@ plugins {
|
||||
alias(libs.plugins.kotlinMultiplatform) apply false
|
||||
alias(libs.plugins.vanniktech.mavenPublish) apply false
|
||||
}
|
||||
|
||||
// Convenience alias to run the IntelliJ IDE with the Lyng plugin from the project root.
|
||||
// Usage: ./gradlew runIde
|
||||
// It simply delegates to :lyng-idea:runIde provided by the Gradle IntelliJ Plugin.
|
||||
tasks.register<org.gradle.api.DefaultTask>("runIde") {
|
||||
group = "intellij"
|
||||
description = "Run IntelliJ IDEA with the Lyng plugin (:lyng-idea)"
|
||||
dependsOn(":lyng-idea:runIde")
|
||||
}
|
||||
|
||||
tasks.register<Exec>("generateDocs") {
|
||||
group = "documentation"
|
||||
description = "Generates a single-file documentation HTML using bin/generate_docs.sh"
|
||||
commandLine("./bin/generate_docs.sh")
|
||||
}
|
||||
+41198
File diff suppressed because it is too large.
Load diff
Binary file not shown.
Binary file not shown.
Binary file not shown.
+1
-1
@@ -12,7 +12,7 @@ Is a [Iterable] with known `size`, a finite [Iterable]:
|
||||
(1)
|
||||
: `comparator(a,b)` should return -1 if `a < b`, +1 if `a > b` or zero.
|
||||
|
||||
See [List], [Set] and [Iterable]
|
||||
See [List], [Set], [Iterable] and [Efficient Iterables in Kotlin Interop](EfficientIterables.md)
|
||||
|
||||
[Iterable]: Iterable.md
|
||||
[List]: List.md
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
# Efficient Iterables in Kotlin Interop
|
||||
|
||||
Lyng provides high-performance iteration mechanisms that allow Kotlin-side code to interact with Lyng iterables efficiently and vice versa.
|
||||
|
||||
## 1. Enumerating Lyng Objects from Kotlin
|
||||
|
||||
To iterate over a Lyng object (like a `List`, `Set`, or `Range`) from Kotlin code, use the virtual `enumerate` method:
|
||||
|
||||
```kotlin
|
||||
val lyngList: Obj = ...
|
||||
lyngList.enumerate(scope) { item ->
|
||||
println("Processing $item")
|
||||
true // return true to continue, false to break
|
||||
}
|
||||
```
|
||||
|
||||
### Why it's efficient:
|
||||
- **Zero allocation**: Unlike traditional iterators, it doesn't create a `LyngIterator` object or any intermediate wrappers.
|
||||
- **Direct access**: Subclasses like `ObjList` override `enumerate` to iterate directly over their internal Kotlin collections.
|
||||
- **Reduced overhead**: It avoids multiple `invokeInstanceMethod` calls for `hasNext()` and `next()` on every step, which would normally involve dynamic dispatch and scope overhead.
|
||||
|
||||
## 2. Reactive Enumeration with Flow
|
||||
|
||||
If you prefer a reactive approach or need to integrate with Kotlin Coroutines flows, use `toFlow()`:
|
||||
|
||||
```kotlin
|
||||
lyngList.toFlow(scope).collect { item ->
|
||||
// ...
|
||||
}
|
||||
```
|
||||
*Note: `toFlow()` internally uses the Lyng iterator protocol (`iterator()`, `hasNext()`, `next()`), so it's slightly less efficient than `enumerate()` for performance-critical loops, but more idiomatic for flow-based processing.*
|
||||
|
||||
## 3. Creating Efficient Iterables for Lyng in Kotlin
|
||||
|
||||
When implementing a custom object in Kotlin that should be iterable in Lyng (e.g., usable in `for (x in myObj) { ... }`), follow these steps to ensure maximum performance.
|
||||
|
||||
### A. Inherit from `Obj` and use `ObjIterable`
|
||||
Ensure your object's class has `ObjIterable` as a parent so the Lyng compiler recognizes it as an iterable.
|
||||
|
||||
```kotlin
|
||||
class MyCollection(val items: List<Obj>) : Obj() {
|
||||
override val objClass = MyCollection.type
|
||||
|
||||
companion object {
|
||||
val type = ObjClass("MyCollection", ObjIterable).apply {
|
||||
// Provide a Lyng-side iterator for compatibility with
|
||||
// manual iterator usage in Lyng scripts.
|
||||
// Using ObjKotlinObjIterator if items are already Obj instances:
|
||||
addFn("iterator") {
|
||||
ObjKotlinObjIterator(thisAs<MyCollection>().items.iterator())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### B. Override `enumerate` for Maximum Performance
|
||||
The Lyng compiler's `for` loops use the `enumerate` method. By overriding it in your Kotlin class, you provide a "fast path" for iteration.
|
||||
|
||||
```kotlin
|
||||
class MyCollection(val items: List<Obj>) : Obj() {
|
||||
// ...
|
||||
override suspend fun enumerate(scope: Scope, callback: suspend (Obj) -> Boolean) {
|
||||
for (item in items) {
|
||||
// If callback returns false, it means 'break' was called in Lyng
|
||||
if (!callback(item)) break
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### C. Use `ObjInt.of()` for Numeric Data
|
||||
If your iterable contains integers, always use `ObjInt.of(Long)` instead of the `ObjInt(Long)` constructor. Lyng maintains a cache for small integers (-128 to 127), which significantly reduces object allocations and GC pressure during tight loops.
|
||||
|
||||
```kotlin
|
||||
// Efficiently creating an integer object
|
||||
val obj = ObjInt.of(42L)
|
||||
|
||||
// Or using extension methods which also use the cache:
|
||||
val obj2 = 42.toObj()
|
||||
val obj3 = 42L.toObj()
|
||||
```
|
||||
|
||||
#### Note on `toObj()` extensions:
|
||||
While `<reified T> T.toObj()` is convenient, using specific extensions like `Int.toObj()` or `Long.toObj()` is slightly more efficient as they use the `ObjInt` cache.
|
||||
|
||||
## 4. Summary of Best Practices
|
||||
|
||||
- **To Consume**: Use `enumerate(scope) { item -> ... true }`.
|
||||
- **To Implement**: Override `enumerate` in your `Obj` subclass.
|
||||
- **To Register**: Use `ObjIterable` (or `ObjCollection`) as a parent class in your `ObjClass` definition.
|
||||
- **To Optimize**: Use `ObjInt.of()` (or `.toObj()`) for all integer object allocations.
|
||||
+102
-22
@@ -40,13 +40,13 @@ available, for example
|
||||
## joinToString
|
||||
|
||||
This methods convert any iterable to a string joining string representation of each element, optionally transforming it
|
||||
and joining using specified suffix.
|
||||
and joining using specified separator.
|
||||
|
||||
Iterable.joinToString(suffux=' ', transform=null)
|
||||
Iterable.joinToString(separator=' ', transformer=null)
|
||||
|
||||
- if `Iterable` `isEmpty`, the empty string `""` is returned.
|
||||
- `suffix` is inserted between items when there are more than one.
|
||||
- `transform` of specified is applied to each element, otherwise its `toString()` method is used.
|
||||
- `separator` is inserted between items when there are more than one.
|
||||
- `transformer` of specified is applied to each element, otherwise its `toString()` method is used.
|
||||
|
||||
Here is the sample:
|
||||
|
||||
@@ -55,7 +55,7 @@ Here is the sample:
|
||||
assertEquals( (1..3).joinToString { it * 10 }, "10 20 30")
|
||||
>>> void
|
||||
|
||||
## `sum` and `sumBy`
|
||||
## `sum` and `sumOf`
|
||||
|
||||
These, again, does the thing:
|
||||
|
||||
@@ -68,6 +68,80 @@ These, again, does the thing:
|
||||
|
||||
>>> void
|
||||
|
||||
## map, filter and their variations
|
||||
|
||||
Used to transform or filter the whole iterable stream:
|
||||
|
||||
val source = [1,2,3,4]
|
||||
|
||||
// map: transform every element to something else
|
||||
assertEquals(["n1", "n2", "n3", "n4"], source.map { "n"+it } )
|
||||
|
||||
// filter: keep only elements matching the predicate
|
||||
assertEquals([2, 4], source.filter { it % 2 == 0 } )
|
||||
|
||||
// count: count elements matching the predicate
|
||||
assertEquals(2, source.count { it % 2 == 0 } )
|
||||
|
||||
// mapNotNull: transform every element, skipping null results:
|
||||
assertEquals(["n1", "n2", "n4"], source.mapNotNull { if( it == 3 ) null else "n"+it } )
|
||||
|
||||
// filterNotNull: skip all null elements:
|
||||
assertEquals([1, 2, 4], [1, 2, null, 4].filterNotNull())
|
||||
|
||||
>>> void
|
||||
|
||||
You can also use flow variations that return a cold `Flow` instead of a `List`, which is useful for large or infinite sequences:
|
||||
|
||||
val source = [1, 2, 3, 4]
|
||||
|
||||
// filterFlow: returns a Flow of filtered elements
|
||||
assert( source.filterFlow { it % 2 == 0 } is Flow )
|
||||
|
||||
// filterFlowNotNull: returns a Flow of non-null elements
|
||||
assert( [1, null, 2].filterFlowNotNull() is Flow )
|
||||
|
||||
>>> void
|
||||
|
||||
## minOf and maxOf
|
||||
|
||||
Find the minimum or maximum value of a function applied to each element:
|
||||
|
||||
val source = ["abc", "de", "fghi"]
|
||||
assertEquals(2, source.minOf { it.length })
|
||||
assertEquals(4, source.maxOf { it.length })
|
||||
>>> void
|
||||
|
||||
## flatten and flatMap
|
||||
|
||||
Work with nested collections:
|
||||
|
||||
val nested = [[1, 2], [3, 4]]
|
||||
|
||||
// flatten: combine nested collections into one list
|
||||
assertEquals([1, 2, 3, 4], nested.flatten())
|
||||
|
||||
// flatMap: map each element to a collection and flatten the result
|
||||
assertEquals([1, 10, 2, 20], [1, 2].flatMap { [it, it*10] })
|
||||
|
||||
>>> void
|
||||
|
||||
## findFirst and findFirstOrNull
|
||||
|
||||
Search for the first element that satisfies the given predicate:
|
||||
|
||||
val source = [1, 2, 3, 4]
|
||||
assertEquals( 2, source.findFirst { it % 2 == 0 } )
|
||||
assertEquals( 2, source.findFirstOrNull { it % 2 == 0 } )
|
||||
|
||||
// findFirst throws if not found:
|
||||
assertThrows( NoSuchElementException ) { source.findFirst { it > 10 } }
|
||||
|
||||
// findFirstOrNull returns null if not found:
|
||||
assertEquals( null, source.findFirstOrNull { it > 10 } )
|
||||
|
||||
>>> void
|
||||
|
||||
## Instance methods:
|
||||
|
||||
| fun/method | description |
|
||||
@@ -75,52 +149,56 @@ These, again, does the thing:
|
||||
| toList() | create a list from iterable |
|
||||
| toSet() | create a set from iterable |
|
||||
| contains(i) | check that iterable contains `i` |
|
||||
| `i in iterator` | same as `contains(i)` |
|
||||
| `i in iterable` | same as `contains(i)` |
|
||||
| isEmpty() | check iterable is empty |
|
||||
| forEach(f) | call f for each element |
|
||||
| toMap() | create a map from list of key-value pairs (arrays of 2 items or like) |
|
||||
| any(p) | true if any element matches predicate `p` |
|
||||
| all(p) | true if all elements match predicate `p` |
|
||||
| map(f) | create a list of values returned by `f` called for each element of the iterable |
|
||||
| indexOf(i) | return index if the first encounter of i or a negative value if not found |
|
||||
| associateBy(kf) | create a map where keys are returned by kf that will be called for each element |
|
||||
| filter(p) | create a list of elements matching predicate `p` |
|
||||
| count(p) | count elements matching predicate `p` |
|
||||
| filterFlow(p) | create a [Flow] of elements matching predicate `p` |
|
||||
| filterNotNull() | create a list of non-null elements |
|
||||
| filterFlowNotNull() | create a [Flow] of non-null elements |
|
||||
| minOf(f) | return minimum value of `f` applied to elements |
|
||||
| maxOf(f) | return maximum value of `f` applied to elements |
|
||||
| flatten() | flatten nested collections into a single [List] |
|
||||
| flatMap(f) | map each element with `f` and flatten results into a [List] |
|
||||
| findFirst(p) | return first element matching predicate `p` or throw (1) |
|
||||
| findFirstOrNull(p) | return first element matching predicate `p` or `null` |
|
||||
| first | first element (1) |
|
||||
| last | last element (1) |
|
||||
| take(n) | return [Iterable] of up to n first elements |
|
||||
| taleLast(n) | return [Iterable] of up to n last elements |
|
||||
| takeLast(n) | return [Iterable] of up to n last elements |
|
||||
| drop(n) | return new [Iterable] without first n elements |
|
||||
| dropLast(n) | return new [Iterable] without last n elements |
|
||||
| sum() | return sum of the collection applying `+` to its elements (3) |
|
||||
| sumOf(predicate) | sum of the modified collection items (3) |
|
||||
| sumOf(f) | sum of the modified collection items (3) |
|
||||
| sorted() | return [List] with collection items sorted naturally |
|
||||
| sortedWith(comparator) | sort using a comparator that compares elements (1) |
|
||||
| sortedBy(predicate) | sort by comparing results of the predicate function |
|
||||
| joinToString(s,t) | convert iterable to string, see (2) |
|
||||
| reversed() | create a list containing items from this in reverse order |
|
||||
| shuffled() | create a listof shiffled elements |
|
||||
| shuffled() | create a list of shuffled elements |
|
||||
|
||||
(1)
|
||||
: throws `NoSuchElementException` if there is no such element
|
||||
:: throws `NoSuchElementException` if there is no such element
|
||||
|
||||
(2)
|
||||
: `joinToString(suffix=" ",transform=null)`: suffix is inserted between items if there are more than one, trasnfom is
|
||||
:: `joinToString(separator=" ", transformer=null)`: separator is inserted between items if there are more than one, transformer is
|
||||
optional function applied to each item that must return result string for an item, otherwise `item.toString()` is used.
|
||||
|
||||
(3)
|
||||
: sum of empty collection is `null`
|
||||
|
||||
fun Iterable.toList(): List
|
||||
fun Iterable.toSet(): Set
|
||||
fun Iterable.indexOf(element): Int
|
||||
fun Iterable.contains(element): Bool
|
||||
fun Iterable.isEmpty(element): Bool
|
||||
fun Iterable.forEach(block: (Any?)->Void ): Void
|
||||
fun Iterable.map(block: (Any?)->Void ): List
|
||||
fun Iterable.associateBy( keyMaker: (Any?)->Any): Map
|
||||
:: sum of empty collection is `null`
|
||||
|
||||
## Abstract methods:
|
||||
|
||||
fun iterator(): Iterator
|
||||
|
||||
Creates a list by iterating to the end. So, the Iterator should be finite to be used with it.
|
||||
For high-performance Kotlin-side interop and custom iterable implementation details, see [Efficient Iterables in Kotlin Interop](EfficientIterables.md).
|
||||
|
||||
## Included in interfaces:
|
||||
|
||||
@@ -134,6 +212,8 @@ Creates a list by iterating to the end. So, the Iterator should be finite to be
|
||||
|
||||
[List]: List.md
|
||||
|
||||
[Flow]: parallelism.md#flow
|
||||
|
||||
[Range]: Range.md
|
||||
|
||||
[Set]: Set.md
|
||||
|
||||
@@ -23,6 +23,8 @@ must throw `ObjIterationFinishedError`.
|
||||
|
||||
Iterators are returned when implementing [Iterable] interface.
|
||||
|
||||
For high-performance Kotlin-side interop and custom iterable implementation details, see [Efficient Iterables in Kotlin Interop](EfficientIterables.md).
|
||||
|
||||
## Implemented for classes:
|
||||
|
||||
- [List], [Range]
|
||||
|
||||
+30
-1
@@ -92,6 +92,34 @@ Open end ranges remove head and tail elements:
|
||||
assert( [1, 2, 3] !== [1, 2, 3])
|
||||
>>> void
|
||||
|
||||
## Destructuring
|
||||
|
||||
Lists can be used as L-values for destructuring assignments. This allows you to unpack list elements into multiple variables.
|
||||
|
||||
### Basic Destructuring
|
||||
```lyng
|
||||
val [a, b, c] = [1, 2, 3]
|
||||
```
|
||||
|
||||
### With Splats (Variadic)
|
||||
A single ellipsis `...` can be used to capture remaining elements into a list. It can be placed at the beginning, middle, or end of the pattern.
|
||||
```lyng
|
||||
val [head, rest...] = [1, 2, 3] // head=1, rest=[2, 3]
|
||||
val [first, middle..., last] = [1, 2, 3, 4, 5] // first=1, middle=[2, 3, 4], last=5
|
||||
```
|
||||
|
||||
### Nested Patterns
|
||||
Destructuring patterns can be nested to unpack multi-dimensional lists.
|
||||
```lyng
|
||||
val [a, [b, c...], d] = [1, [2, 3, 4], 5]
|
||||
```
|
||||
|
||||
### Reassignment
|
||||
Destructuring can also be used to reassign existing variables:
|
||||
```lyng
|
||||
[x, y] = [y, x] // Swap values
|
||||
```
|
||||
|
||||
## In-place sort
|
||||
|
||||
List could be sorted in place, just like [Collection] provide sorted copies, in a very like way:
|
||||
@@ -130,7 +158,8 @@ List could be sorted in place, just like [Collection] provide sorted copies, in
|
||||
| `sort()` | in-place sort, natural order | void |
|
||||
| `sortBy(predicate)` | in-place sort bu `predicate` call result (3) | void |
|
||||
| `sortWith(comparator)` | in-place sort using `comarator` function (4) | void |
|
||||
| `shiffle()` | in-place shiffle contents | |
|
||||
| `shuffle()` | in-place shuffle contents | |
|
||||
| `toString()` | string representation like `[a,b,c]` | |
|
||||
|
||||
(1)
|
||||
: optimized implementation that override `Array` one
|
||||
|
||||
+2
-2
@@ -172,7 +172,7 @@ Maps and entries can also be merged with `+` and `+=`:
|
||||
|
||||
Notes:
|
||||
- Map literals always use string keys (identifier keys are converted to strings).
|
||||
- Spreads inside map literals and `+`/`+=` merges require string keys on the right-hand side; this aligns with named-argument splats.
|
||||
- When you need computed or non-string keys, use the constructor form `Map(...)` or build entries with `=>` and then merge.
|
||||
- Spreads inside map literals and `+`/`+=` merges allow any objects as keys.
|
||||
- When you need computed or non-string keys, use the constructor form `Map(...)`, map literals with computed keys (if supported), or build entries with `=>` and then merge.
|
||||
|
||||
[Collection](Collection.md)
|
||||
+243
-7
@@ -42,6 +42,92 @@ a _constructor_ that requires two parameters for fields. So when creating it wit
|
||||
Form now on `Point` is a class, it's type is `Class`, and we can create instances with it as in the
|
||||
example above.
|
||||
|
||||
## 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.
|
||||
|
||||
### Basic Syntax
|
||||
|
||||
Properties are declared using `val` (read-only) or `var` (read-write) followed by a name and `get()`/`set()` blocks:
|
||||
|
||||
```kotlin
|
||||
class Person(private var _age: Int) {
|
||||
// Read-only property
|
||||
val ageCategory
|
||||
get() {
|
||||
if (_age < 18) "Minor" else "Adult"
|
||||
}
|
||||
|
||||
// Read-write property
|
||||
var age: Int
|
||||
get() { _age }
|
||||
set(value) {
|
||||
if (value >= 0) _age = value
|
||||
}
|
||||
}
|
||||
|
||||
val p = Person(15)
|
||||
assertEquals("Minor", p.ageCategory)
|
||||
p.age = 20
|
||||
assertEquals("Adult", p.ageCategory)
|
||||
```
|
||||
|
||||
### Laconic Expression Shorthand
|
||||
|
||||
For simple accessors and methods, you can use the `=` shorthand for a more elegant and laconic form:
|
||||
|
||||
```kotlin
|
||||
class Circle(val radius: Real) {
|
||||
val area get() = π * radius * radius
|
||||
val circumference get() = 2 * π * radius
|
||||
|
||||
fun diameter() = radius * 2
|
||||
}
|
||||
|
||||
fun median(a, b) = (a + b) / 2
|
||||
|
||||
class Counter {
|
||||
private var _count = 0
|
||||
var count get() = _count set(v) = _count = v
|
||||
}
|
||||
```
|
||||
|
||||
### Key Rules
|
||||
|
||||
- **`val` properties** must have a `get()` accessor and cannot have a `set()`.
|
||||
- **`var` properties** must have both `get()` and `set()` accessors.
|
||||
- **Functions and methods** can use the `=` shorthand to return the result of a single expression.
|
||||
- **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.
|
||||
|
||||
## Instance initialization: init block
|
||||
|
||||
In addition to the primary constructor arguments, you can provide an `init` block that runs on each instance creation. This is useful for more complex initializations, side effects, or setting up fields that depend on multiple constructor parameters.
|
||||
|
||||
class Point(val x, val y) {
|
||||
var magnitude
|
||||
|
||||
init {
|
||||
magnitude = Math.sqrt(x*x + y*y)
|
||||
}
|
||||
}
|
||||
|
||||
Key features of `init` blocks:
|
||||
- **Scope**: They have full access to `this` members and all primary constructor parameters.
|
||||
- **Order**: In a single-inheritance scenario, `init` blocks run immediately after the instance fields are prepared but before the primary constructor body logic.
|
||||
- **Multiple blocks**: You can have multiple `init` blocks; they will be executed in the order they appear in the class body.
|
||||
|
||||
### Initialization in Multiple Inheritance
|
||||
|
||||
In cases of multiple inheritance, `init` blocks are executed following the constructor chaining rule:
|
||||
1. All ancestors are initialized first, following the inheritance hierarchy (diamond-safe: each ancestor is initialized exactly once).
|
||||
2. The `init` blocks of each class are executed after its parents have been fully initialized.
|
||||
3. For a hierarchy `class D : B, C`, the initialization order is: `B`'s chain, then `C`'s chain (skipping common ancestors with `B`), and finally `D`'s own `init` blocks.
|
||||
|
||||
### Initialization during Deserialization
|
||||
|
||||
When an object is restored from a serialized form (e.g., using `Lynon`), `init` blocks are **re-executed**. This ensures that transient state or derived fields are correctly recalculated upon restoration. However, primary constructors are **not** re-called during deserialization; only the `init` blocks and field initializers are executed to restore the instance state.
|
||||
|
||||
Class point has a _method_, or a _member function_ `length()` that uses its _fields_ `x` and `y` to
|
||||
calculate the magnitude. Length is called
|
||||
|
||||
@@ -53,8 +139,20 @@ statements discussed later, there could be default values, ellipsis, etc.
|
||||
class Point(x=0,y=0)
|
||||
val p = Point()
|
||||
assert( p.x == 0 && p.y == 0 )
|
||||
|
||||
// Named arguments in constructor calls use colon syntax:
|
||||
val p2 = Point(y: 10, x: 5)
|
||||
assert( p2.x == 5 && p2.y == 10 )
|
||||
|
||||
// Auto-substitution shorthand for named arguments:
|
||||
val x = 1
|
||||
val y = 2
|
||||
val p3 = Point(x:, y:)
|
||||
assert( p3.x == 1 && p3.y == 2 )
|
||||
>>> void
|
||||
|
||||
Note that unlike **Kotlin**, which uses `=` for named arguments, Lyng uses `:` to avoid ambiguity with assignment expressions.
|
||||
|
||||
## Methods
|
||||
|
||||
Functions defined inside a class body are methods, and unless declared
|
||||
@@ -168,6 +266,80 @@ Compatibility notes:
|
||||
|
||||
Earlier drafts and docs described a declaration‑order depth‑first linearization. Lyng now uses C3 MRO for member lookup and disambiguation. Most code should continue to work unchanged, but in rare edge cases involving diamonds or complex multiple inheritance, the chosen base for an ambiguous member may change to reflect C3. If needed, disambiguate explicitly using `this@Type.member(...)` inside class bodies or casts `(expr as Type).member(...)` from outside.
|
||||
|
||||
## Enums
|
||||
|
||||
Lyng provides lightweight enums for representing a fixed set of named constants. Enums are classes whose instances are predefined and singletons.
|
||||
|
||||
Current syntax supports simple enum declarations with just entry names:
|
||||
|
||||
enum Color {
|
||||
RED, GREEN, BLUE
|
||||
}
|
||||
|
||||
Usage:
|
||||
|
||||
- Type of entries: every entry is an instance of its enum type.
|
||||
|
||||
assert( Color.RED is Color )
|
||||
|
||||
- Order and names: each entry has zero‑based `ordinal` and string `name`.
|
||||
|
||||
assertEquals(0, Color.RED.ordinal)
|
||||
assertEquals("BLUE", Color.BLUE.name)
|
||||
|
||||
- All entries as a list in declaration order: `EnumType.entries`.
|
||||
|
||||
assertEquals([Color.RED, Color.GREEN, Color.BLUE], Color.entries)
|
||||
|
||||
- Lookup by name: `EnumType.valueOf("NAME")` → entry.
|
||||
|
||||
assertEquals(Color.GREEN, Color.valueOf("GREEN"))
|
||||
|
||||
- Equality and comparison:
|
||||
- Equality uses identity of entries, e.g., `Color.RED == Color.valueOf("RED")`.
|
||||
- Cross‑enum comparisons are not allowed.
|
||||
- Ordering comparisons use `ordinal`.
|
||||
|
||||
assert( Color.RED == Color.valueOf("RED") )
|
||||
assert( Color.RED.ordinal < Color.BLUE.ordinal )
|
||||
>>> void
|
||||
|
||||
### Enums with `when`
|
||||
|
||||
Use `when(subject)` with equality branches for enums. See full `when` guide: [The `when` statement](when.md).
|
||||
|
||||
enum Color { RED, GREEN, BLUE }
|
||||
|
||||
fun describe(c) {
|
||||
when(c) {
|
||||
Color.RED, Color.GREEN -> "primary-like"
|
||||
Color.BLUE -> "blue"
|
||||
else -> "unknown" // if you pass something that is not a Color
|
||||
}
|
||||
}
|
||||
assertEquals("primary-like", describe(Color.RED))
|
||||
assertEquals("blue", describe(Color.BLUE))
|
||||
>>> void
|
||||
|
||||
### Serialization
|
||||
|
||||
Enums are serialized compactly with Lynon: the encoded value stores just the entry ordinal within the enum type, which is both space‑efficient and fast.
|
||||
|
||||
import lyng.serialization
|
||||
|
||||
enum Color { RED, GREEN, BLUE }
|
||||
|
||||
val e = Lynon.encode(Color.BLUE)
|
||||
val decoded = Lynon.decode(e)
|
||||
assertEquals(Color.BLUE, decoded)
|
||||
>>> void
|
||||
|
||||
Notes and limitations (current version):
|
||||
|
||||
- Enum declarations support only simple entry lists: no per‑entry bodies, no custom constructors, and no user‑defined methods/fields on the enum itself yet.
|
||||
- `name` and `ordinal` are read‑only properties of an entry.
|
||||
- `entries` is a read‑only list owned by the enum type.
|
||||
|
||||
## fields and visibility
|
||||
|
||||
It is possible to add non-constructor fields:
|
||||
@@ -319,9 +491,11 @@ As usual, private statics are not accessible from the outside:
|
||||
|
||||
# Extending classes
|
||||
|
||||
It sometimes happen that the class is missing some particular functionality that can be _added to it_ without rewriting its inner logic and using its private state. In this case _extension methods_ could be used, for example. we want to create an extension method
|
||||
that would test if some object of unknown type contains something that can be interpreted
|
||||
as an integer. In this case we _extend_ class `Object`, as it is the parent class for any instance of any type:
|
||||
It sometimes happen that the class is missing some particular functionality that can be _added to it_ without rewriting its inner logic and using its private state. In this case _extension members_ could be used.
|
||||
|
||||
## Extension methods
|
||||
|
||||
For example, we want to create an extension method that would test if some object of unknown type contains something that can be interpreted as an integer. In this case we _extend_ class `Object`, as it is the parent class for any instance of any type:
|
||||
|
||||
fun Object.isInteger() {
|
||||
when(this) {
|
||||
@@ -346,10 +520,67 @@ as an integer. In this case we _extend_ class `Object`, as it is the parent clas
|
||||
assert( ! "5.2".isInteger() )
|
||||
>>> void
|
||||
|
||||
__Important note__ as for version 0.6.9, extensions are in __global scope__. It means, that once applied to a global type (Int in our sample), they will be available for _all_ contexts, even new created,
|
||||
as they are modifying the type, not the context.
|
||||
## Extension properties
|
||||
|
||||
Beware of it. We might need to reconsider it later.
|
||||
Just like methods, you can extend existing classes with properties. These can be defined using simple initialization (for `val` only) or with custom accessors.
|
||||
|
||||
### Simple val extension
|
||||
|
||||
A read-only extension can be defined by assigning an expression:
|
||||
|
||||
```kotlin
|
||||
val String.isLong = length > 10
|
||||
|
||||
val s = "Hello, world!"
|
||||
assert(s.isLong)
|
||||
```
|
||||
|
||||
### Properties with accessors
|
||||
|
||||
For more complex logic, use `get()` and `set()` blocks:
|
||||
|
||||
```kotlin
|
||||
class Box(var value: Int)
|
||||
|
||||
var Box.doubledValue
|
||||
get() = value * 2
|
||||
set(v) = value = v / 2
|
||||
|
||||
val b = Box(10)
|
||||
assertEquals(20, b.doubledValue)
|
||||
b.doubledValue = 30
|
||||
assertEquals(15, b.value)
|
||||
```
|
||||
|
||||
Extension members are strictly barred from accessing private members of the class they extend, maintaining encapsulation.
|
||||
|
||||
### Extension Scoping and Isolation
|
||||
|
||||
Extensions in Lyng are **scope-isolated**. This means an extension is only visible within the scope where it is defined and its child scopes. This reduces the "attack surface" and prevents extensions from polluting the global space or other modules.
|
||||
|
||||
#### Scope Isolation Example
|
||||
|
||||
You can define different extensions with the same name in different scopes:
|
||||
|
||||
```kotlin
|
||||
fun scopeA() {
|
||||
val Int.description = "Number: " + toString()
|
||||
assertEquals("Number: 42", 42.description)
|
||||
}
|
||||
|
||||
fun scopeB() {
|
||||
val Int.description = "Value: " + toString()
|
||||
assertEquals("Value: 42", 42.description)
|
||||
}
|
||||
|
||||
scopeA()
|
||||
scopeB()
|
||||
|
||||
// Outside those scopes, Int.description is not defined
|
||||
assertThrows { 42.description }
|
||||
```
|
||||
|
||||
This isolation ensures that libraries can use extensions internally without worrying about name collisions with other libraries or the user's code. When a module is imported using `use`, its top-level extensions become available in the importing scope.
|
||||
|
||||
## dynamic symbols
|
||||
|
||||
@@ -529,4 +760,9 @@ Regular methods are called on instances as usual `instance.method()`. The method
|
||||
|
||||
TBD
|
||||
|
||||
[argument list](declaring_arguments.md)
|
||||
[argument list](declaring_arguments.md)
|
||||
### Visibility from within closures and instance scopes
|
||||
|
||||
When a closure executes within a method, the closure retains the lexical class context of its creation site. This means private/protected members of that class remain accessible where expected (subject to usual visibility rules). Field resolution checks the declaring class and validates access using the preserved `currentClassCtx`.
|
||||
|
||||
See also: [Scopes and Closures: resolution and safety](scopes_and_closures.md)
|
||||
+109
@@ -0,0 +1,109 @@
|
||||
# Testing and Assertions
|
||||
|
||||
Lyng provides several built-in functions for testing and verifying code behavior. These are available in all scripts.
|
||||
|
||||
## Basic Assertions
|
||||
|
||||
### `assert`
|
||||
|
||||
Assert that a condition is true.
|
||||
|
||||
assert(condition, message=null)
|
||||
|
||||
- `condition`: A boolean expression.
|
||||
- `message` (optional): A string message to include in the exception if the assertion fails.
|
||||
|
||||
If the condition is false, it throws an `AssertionFailedException`.
|
||||
|
||||
```lyng
|
||||
assert(1 + 1 == 2)
|
||||
assert(true, "This should be true")
|
||||
```
|
||||
|
||||
### `assertEquals` and `assertEqual`
|
||||
|
||||
Assert that two values are equal. `assertEqual` is an alias for `assertEquals`.
|
||||
|
||||
assertEquals(expected, actual)
|
||||
assertEqual(expected, actual)
|
||||
|
||||
If `expected != actual`, it throws an `AssertionFailedException` with a message showing both values.
|
||||
|
||||
```lyng
|
||||
assertEquals(4, 2 * 2)
|
||||
assertEqual("hello", "hel" + "lo")
|
||||
```
|
||||
|
||||
### `assertNotEquals`
|
||||
|
||||
Assert that two values are not equal.
|
||||
|
||||
assertNotEquals(unexpected, actual)
|
||||
|
||||
If `unexpected == actual`, it throws an `AssertionFailedException`.
|
||||
|
||||
```lyng
|
||||
assertNotEquals(5, 2 * 2)
|
||||
```
|
||||
|
||||
## Exception Testing
|
||||
|
||||
### `assertThrows`
|
||||
|
||||
Assert that a block of code throws an exception.
|
||||
|
||||
assertThrows(code)
|
||||
assertThrows(expectedExceptionClass, code)
|
||||
|
||||
- `expectedExceptionClass` (optional): The class of the exception that is expected to be thrown.
|
||||
- `code`: A lambda block or statement to execute.
|
||||
|
||||
If the code does not throw an exception, an `AssertionFailedException` is raised.
|
||||
If an `expectedExceptionClass` is provided, the thrown exception must be of that class (or its subclass), otherwise an error is raised.
|
||||
|
||||
`assertThrows` returns the caught exception object if successful.
|
||||
|
||||
```lyng
|
||||
// Just assert that something is thrown
|
||||
assertThrows { 1 / 0 }
|
||||
|
||||
// Assert that a specific exception class is thrown
|
||||
assertThrows(NoSuchElementException) {
|
||||
[1, 2, 3].findFirst { it > 10 }
|
||||
}
|
||||
|
||||
// You can use the returned exception
|
||||
val ex = assertThrows { throw Exception("custom error") }
|
||||
assertEquals("custom error", ex.message)
|
||||
```
|
||||
|
||||
## Other Validation Functions
|
||||
|
||||
While not strictly for testing, these functions help in defensive programming:
|
||||
|
||||
### `require`
|
||||
|
||||
require(condition, message="requirement not met")
|
||||
|
||||
Throws an `IllegalArgumentException` if the condition is false. Use this for validating function arguments.
|
||||
|
||||
If we want to evaluate the message lazily:
|
||||
|
||||
require(condition) { "requirement not met: %s"(someData) }
|
||||
|
||||
In this case, formatting will only occur if the condition is not met.
|
||||
|
||||
|
||||
|
||||
### `check`
|
||||
|
||||
check(condition, message="check failed")
|
||||
|
||||
Throws an `IllegalStateException` if the condition is false. Use this for validating internal state.
|
||||
|
||||
With lazy message evaluation:
|
||||
|
||||
check(condition) { "check failed: %s"(someData) }
|
||||
|
||||
In this case, formatting will only occur if the condition is not met.
|
||||
|
||||
@@ -158,3 +158,9 @@ Function annotation can have more args specified at call time. There arguments m
|
||||
>>> void
|
||||
|
||||
[parallelism]: parallelism.md
|
||||
|
||||
## Scopes and Closures: resolution and safety
|
||||
|
||||
Closures and dynamic scope graphs require care to avoid accidental recursion and to keep name resolution predictable. See the dedicated page for detailed rules, helper APIs, and best practices:
|
||||
|
||||
- Scopes and Closures: resolution and safety → [scopes_and_closures.md](scopes_and_closures.md)
|
||||
+37
-17
@@ -75,6 +75,13 @@ destructuring arrays when calling functions and lambdas:
|
||||
getFirstAndLast( ...(1..10) ) // see "splats" section below
|
||||
>>> [1,10]
|
||||
|
||||
Note that array destructuring can also be used in assignments:
|
||||
|
||||
val [first, middle..., last] = [1, 2, 3, 4, 5]
|
||||
[x, y] = [y, x] // Swap
|
||||
|
||||
See [tutorial] and [List] documentation for more details on destructuring assignments.
|
||||
|
||||
# Splats
|
||||
|
||||
Ellipsis allows to convert argument lists to lists. The inversa algorithm that converts [List],
|
||||
@@ -100,41 +107,53 @@ There could be any number of splats at any positions. You can splat any other [I
|
||||
|
||||
## Named arguments in calls
|
||||
|
||||
Lyng supports named arguments at call sites using colon syntax `name: value`:
|
||||
Lyng supports named arguments at call sites using colon syntax `name: value`.
|
||||
|
||||
### Shorthand for Named Arguments
|
||||
|
||||
If you want to pass a variable as a named argument and the variable has the same name as the parameter, you can omit the value and use the shorthand `name:`. This is highly readable and matches the shorthand for map literals.
|
||||
|
||||
```lyng
|
||||
fun test(a="foo", b="bar", c="bazz") { [a, b, c] }
|
||||
|
||||
assertEquals(["foo", "b", "bazz"], test(b: "b"))
|
||||
assertEquals(["a", "bar", "c"], test("a", c: "c"))
|
||||
fun test(a, b, c) { [a, b, c] }
|
||||
|
||||
val a = 1
|
||||
val b = 2
|
||||
val c = 3
|
||||
|
||||
// Explicit:
|
||||
assertEquals([1, 2, 3], test(a: a, b: b, c: c))
|
||||
|
||||
// Shorthand (preferred):
|
||||
assertEquals([1, 2, 3], test(a:, b:, c:))
|
||||
```
|
||||
|
||||
Rules:
|
||||
This shorthand is elegant, reduces boilerplate, and is consistent with Lyng's map literal syntax. It works for both function calls and class constructors.
|
||||
|
||||
Rules for named arguments:
|
||||
|
||||
- Named arguments must follow positional arguments. After the first named argument, no positional arguments may appear inside the parentheses.
|
||||
- The only exception is the syntactic trailing block after the call: `f(args) { ... }`. This block is outside the parentheses and is handled specially (see below).
|
||||
- A named argument cannot reassign a parameter already set positionally.
|
||||
- If the last parameter has already been assigned by a named argument (or named splat), a trailing block is not allowed and results in an error.
|
||||
|
||||
Why `:` and not `=` at call sites? In Lyng, `=` is an expression (assignment), so we use `:` to avoid ambiguity. Declarations continue to use `:` for types, while call sites use `as` / `as?` for type operations.
|
||||
Why `:` and not `=` at call sites? In Lyng, `=` is an expression (assignment), so we use `:` to avoid ambiguity. This is a key difference from **Kotlin**, which uses `=` for named arguments. Declarations in Lyng continue to use `:` for types, while call sites use `as` / `as?` for type operations.
|
||||
|
||||
## Named splats (map splats)
|
||||
|
||||
Splat (`...`) of a Map provides named arguments to the call. Only string keys are allowed:
|
||||
Splat (`...`) of a Map provides named arguments to the call. Only string keys are allowed. You can use the same auto-substitution shorthand inside map literals used for splats:
|
||||
|
||||
```lyng
|
||||
fun test(a="a", b="b", c="c", d="d") { [a, b, c, d] }
|
||||
val r = test("A?", ...Map("d" => "D!", "b" => "B!"))
|
||||
assertEquals(["A?","B!","c","D!"], r)
|
||||
```
|
||||
|
||||
The same with a map literal is often more concise. Define the literal, then splat the variable:
|
||||
|
||||
fun test(a="a", b="b", c="c", d="d") { [a, b, c, d] }
|
||||
val patch = { d: "D!", b: "B!" }
|
||||
|
||||
val b = "B!"
|
||||
val d = "D!"
|
||||
|
||||
// Auto-substitution in map literal:
|
||||
val patch = { d:, b: }
|
||||
|
||||
val r = test("A?", ...patch)
|
||||
assertEquals(["A?","B!","c","D!"], r)
|
||||
>>> void
|
||||
```
|
||||
|
||||
Constraints:
|
||||
|
||||
@@ -155,3 +174,4 @@ If a call is immediately followed by a block `{ ... }`, it is treated as an extr
|
||||
|
||||
|
||||
[tutorial]: tutorial.md
|
||||
[List]: List.md
|
||||
+60
-5
@@ -109,7 +109,62 @@ scope.eval("val y = inc(41); log('Answer:', y)")
|
||||
|
||||
You can register multiple names (aliases) at once: `addFn<ObjInt>("inc", "increment") { ... }`.
|
||||
|
||||
### 5) Read variable values back in Kotlin
|
||||
### 5) Add Kotlin‑backed fields
|
||||
|
||||
If you need a simple field (with a value) instead of a computed property, use `createField`. This adds a field to the class that will be present in all its instances.
|
||||
|
||||
```kotlin
|
||||
val myClass = ObjClass("MyClass")
|
||||
|
||||
// Add a read-only field (constant)
|
||||
myClass.createField("version", ObjString("1.0.0"), isMutable = false)
|
||||
|
||||
// Add a mutable field with an initial value
|
||||
myClass.createField("count", ObjInt(0), isMutable = true)
|
||||
|
||||
scope.addConst("MyClass", myClass)
|
||||
```
|
||||
|
||||
In Lyng:
|
||||
```lyng
|
||||
val instance = MyClass()
|
||||
println(instance.version) // -> "1.0.0"
|
||||
instance.count = 5
|
||||
println(instance.count) // -> 5
|
||||
```
|
||||
|
||||
### 6) Add Kotlin‑backed properties
|
||||
|
||||
Properties in Lyng are pure accessors (getters and setters) and do not have automatic backing fields. You can add them to a class using `addProperty`.
|
||||
|
||||
```kotlin
|
||||
val myClass = ObjClass("MyClass")
|
||||
var internalValue: Long = 10
|
||||
|
||||
myClass.addProperty(
|
||||
name = "value",
|
||||
getter = {
|
||||
// Return current value as a Lyng object
|
||||
ObjInt(internalValue)
|
||||
},
|
||||
setter = { newValue ->
|
||||
// newValue is passed as a Lyng object (the first and only argument)
|
||||
internalValue = (newValue as ObjInt).value
|
||||
}
|
||||
)
|
||||
|
||||
scope.addConst("MyClass", myClass)
|
||||
```
|
||||
|
||||
Usage in Lyng:
|
||||
```lyng
|
||||
val instance = MyClass()
|
||||
println(instance.value) // -> 10
|
||||
instance.value = 42
|
||||
println(instance.value) // -> 42
|
||||
```
|
||||
|
||||
### 7) Read variable values back in Kotlin
|
||||
|
||||
The simplest approach: evaluate an expression that yields the value and convert it.
|
||||
|
||||
@@ -124,7 +179,7 @@ val kotlinName = scope.eval("name").toKotlin(scope) // -> "Lyng rocks!"
|
||||
|
||||
Advanced: you can also grab a variable record directly via `scope.get(name)` and work with its `Obj` value, but evaluating `"name"` is often clearer and enforces Lyng semantics consistently.
|
||||
|
||||
### 6) Execute scripts with parameters; call Lyng functions from Kotlin
|
||||
### 8) Execute scripts with parameters; call Lyng functions from Kotlin
|
||||
|
||||
There are two convenient patterns.
|
||||
|
||||
@@ -157,7 +212,7 @@ val result = resultObj.toKotlin(scope) // -> 42
|
||||
|
||||
If you need to pass complex data (lists, maps), construct the corresponding Lyng `Obj` types (`ObjList`, `ObjMap`, etc.) and pass them in `Arguments`.
|
||||
|
||||
### 7) Create your own packages and import them in Lyng
|
||||
### 9) Create your own packages and import them in Lyng
|
||||
|
||||
Lyng supports packages that are imported from scripts. You can register packages programmatically via `ImportManager` or by providing source texts that declare `package ...`.
|
||||
|
||||
@@ -212,7 +267,7 @@ val s = scope.eval("s").toKotlin(scope) // -> 144
|
||||
|
||||
You can also register from parsed `Source` instances via `addSourcePackages(source)`.
|
||||
|
||||
### 8) Executing from files, security, and isolation
|
||||
### 10) Executing from files, security, and isolation
|
||||
|
||||
- To run code from a file, read it and pass to `scope.eval(text)` or compile with `Compiler.compile(Source(fileName, text))`.
|
||||
- `ImportManager` takes an optional `SecurityManager` if you need to restrict what packages or operations are available. By default, `Script.defaultImportManager` allows everything suitable for embedded use; clamp it down in sandboxed environments.
|
||||
@@ -223,7 +278,7 @@ You can also register from parsed `Source` instances via `addSourcePackages(sour
|
||||
val isolated = net.sergeych.lyng.Scope.new()
|
||||
```
|
||||
|
||||
### 9) Tips and troubleshooting
|
||||
### 11) Tips and troubleshooting
|
||||
|
||||
- All values that cross the boundary must be Lyng `Obj` instances. Convert Kotlin values explicitly (e.g., `ObjInt`, `ObjReal`, `ObjString`).
|
||||
- Use `toKotlin(scope)` to get Kotlin values back. Collections convert to Kotlin collections recursively.
|
||||
|
||||
@@ -169,3 +169,18 @@ _this functionality is not yet released_
|
||||
| UnknownException | unexpected kotlin exception caught |
|
||||
| | |
|
||||
|
||||
|
||||
### Symbol resolution errors
|
||||
|
||||
For compatibility, `SymbolNotFound` is an alias of `SymbolNotDefinedException`. You can catch either name in examples and tests.
|
||||
|
||||
Example:
|
||||
|
||||
```lyng
|
||||
try {
|
||||
nonExistingMethod()
|
||||
}
|
||||
catch(e: SymbolNotFound) {
|
||||
// handle
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,34 @@
|
||||
## Fix: prevent cycles in scope parent chain during pooled frame reuse
|
||||
|
||||
### What changed
|
||||
- Scope.resetForReuse now fully detaches a reused scope from its previous chain/state before re-parenting:
|
||||
- sets `parent = null` and regenerates `frameId`
|
||||
- clears locals/slots/bindings caches
|
||||
- only after that, validates the new parent with `ensureNoCycle` and assigns it
|
||||
- ScopePool.borrow on all targets (JVM, Android, JS, Native, Wasm) now has a defensive fallback:
|
||||
- if `resetForReuse` throws `IllegalStateException` indicating a parent-chain cycle, the pool allocates a fresh `Scope` instead of failing.
|
||||
|
||||
### Why
|
||||
In some nested call patterns (notably instance method calls where an instance is produced by another function and immediately used), the same pooled `Scope` object can be rebound into a chain that already (transitively) contains it. Reassigning `parent` in that case forms a structural cycle, which `ensureNoCycle` correctly detects and throws. This could surface as:
|
||||
|
||||
```
|
||||
IllegalStateException: cycle detected in scope parent chain assignment
|
||||
at net.sergeych.lyng.Scope.ensureNoCycle(...)
|
||||
at net.sergeych.lyng.Scope.resetForReuse(...)
|
||||
at net.sergeych.lyng.ScopePool.borrow(...)
|
||||
... during instance method invocation
|
||||
```
|
||||
|
||||
The fix removes the failure mode by:
|
||||
1) Detaching the reused frame from its prior chain/state before validating and assigning the new parent.
|
||||
2) Falling back to a new frame allocation if a cycle is still detected (extremely rare and cheap vs. a crash).
|
||||
|
||||
### Expected effects
|
||||
- Eliminates sporadic `cycle detected in scope parent chain` crashes during instance method invocation.
|
||||
- No change to public API or normal semantics.
|
||||
- Pooling remains enabled by default; the fallback only triggers on the detected cycle edge case.
|
||||
- Negligible performance impact: fresh allocation is used only when a cycle would have crashed the VM previously.
|
||||
|
||||
### Notes
|
||||
- The fix is platform-wide (all ScopePool actuals are covered).
|
||||
- We recommend adding/keeping a regression test that exercises: a class with a method, a function returning an instance, and an exported function calling the instance method. The test should pass without exceptions.
|
||||
@@ -0,0 +1,167 @@
|
||||
# Json support
|
||||
|
||||
Since 1.0.5 we start adding JSON support. Versions 1,0,6* support serialization of the basic types, including lists and
|
||||
maps, and simple classes. Multiple inheritance may produce incorrect results, it is work in progress.
|
||||
|
||||
## Serialization in Lyng
|
||||
|
||||
// in lyng
|
||||
assertEquals("{\"a\":1}", {a: 1}.toJsonString())
|
||||
void
|
||||
>>> void
|
||||
|
||||
Simple classes serialization is supported:
|
||||
|
||||
import lyng.serialization
|
||||
class Point(foo,bar) {
|
||||
val t = 42
|
||||
}
|
||||
// val is not serialized
|
||||
assertEquals( "{\"foo\":1,\"bar\":2}", Point(1,2).toJsonString() )
|
||||
>>> void
|
||||
|
||||
Note that mutable members are serialized:
|
||||
|
||||
import lyng.serialization
|
||||
|
||||
class Point2(foo,bar) {
|
||||
var reason = 42
|
||||
// but we override json serialization:
|
||||
fun toJsonObject() {
|
||||
{ "custom": true }
|
||||
}
|
||||
}
|
||||
// var is serialized instead
|
||||
assertEquals( "{\"custom\":true}", Point2(1,2).toJsonString() )
|
||||
>>> void
|
||||
|
||||
Custom serialization of user classes is possible by overriding `toJsonObject` method. It must return an object which is
|
||||
serializable to Json. Most often it is a map, but any object is accepted, that makes it very flexible:
|
||||
|
||||
import lyng.serialization
|
||||
|
||||
class Point2(foo,bar) {
|
||||
var reason = 42
|
||||
// but we override json serialization:
|
||||
fun toJsonObject() {
|
||||
{ "custom": true }
|
||||
}
|
||||
}
|
||||
class Custom {
|
||||
fun toJsonObject() {
|
||||
"full freedom"
|
||||
}
|
||||
}
|
||||
// var is serialized instead
|
||||
assertEquals( "\"full freedom\"", Custom().toJsonString() )
|
||||
>>> void
|
||||
|
||||
Please note that `toJsonString` should be used to get serialized string representation of the object. Don't call
|
||||
`toJsonObject` directly, it is not intended to be used outside the serialization library.
|
||||
|
||||
## Kotlin side interfaces
|
||||
|
||||
The "Batteries included" principle is also applied to serialization.
|
||||
|
||||
- `Obj.toJson()` provides Kotlin `JsonElement`
|
||||
- `Obj.toJsonString()` provides Json string representation
|
||||
- `Obj.decodeSerializableWith()` and `Obj.decodeSerializable()` allows to decode Lyng classes as Kotlin objects using
|
||||
`kotlinx.serialization`:
|
||||
|
||||
```kotlin
|
||||
/**
|
||||
* Decodes the current object into a deserialized form using the provided deserialization strategy.
|
||||
* It is based on [Obj.toJson] and uses existing Kotlin Json serialization, without string representation
|
||||
* (only `JsonElement` to carry information between Kotlin and Lyng serialization worlds), thus efficient.
|
||||
*
|
||||
* @param strategy The deserialization strategy that defines how the object should be decoded.
|
||||
* @param scope An optional scope used during deserialization to define the context. Defaults to a new instance of Scope.
|
||||
* @return The deserialized object of type T.
|
||||
*/
|
||||
suspend fun <T> Obj.decodeSerializableWith(strategy: DeserializationStrategy<T>, scope: Scope = Scope()): T =
|
||||
Json.decodeFromJsonElement(strategy, toJson(scope))
|
||||
|
||||
/**
|
||||
* Decodes a serializable object of type [T] using the provided decoding scope. The deserialization uses
|
||||
* [Obj.toJson] and existing Json based serialization ithout using actual string representation, thus
|
||||
* efficient.
|
||||
*
|
||||
* @param T The type of the object to be decoded. Must be a reified type.
|
||||
* @param scope The scope used during decoding. Defaults to a new instance of [Scope].
|
||||
*/
|
||||
suspend inline fun <reified T> Obj.decodeSerializable(scope: Scope = Scope()) =
|
||||
decodeSerializableWith<T>(serializer<T>(), scope)
|
||||
```
|
||||
|
||||
Note that lyng-2-kotlin deserialization with `kotlinx.serialization` uses JsonElement as information carrier without
|
||||
formatting and parsing actual Json strings. This is why we use `Json.decodeFromJsonElement` instead of
|
||||
`Json.decodeFromString`. Such an approach gives satisfactory performance without writing and supporting custom
|
||||
`kotlinx.serialization` codecs.
|
||||
|
||||
### Pitfall: JSON objects and Map<String, Any?>
|
||||
|
||||
Kotlin serialization does not support `Map<String, Any?>` as a serializable type, more general, it can't serialize `Any`. This in particular means that you can deserialize Kotlin `Map<String, T>` as long as `T` is `@Serializable` in Kotlin:
|
||||
|
||||
```kotlin
|
||||
@Serializable
|
||||
data class TestJson2(
|
||||
val value: Int,
|
||||
val inner: Map<String,Int>
|
||||
)
|
||||
|
||||
@Test
|
||||
fun deserializeMapWithJsonTest() = runTest {
|
||||
val x = eval("""
|
||||
import lyng.serialization
|
||||
{ value: 1, inner: { "foo": 1, "bar": 2 }}
|
||||
""".trimIndent()).decodeSerializable<TestJson2>()
|
||||
// That works perfectly well:
|
||||
assertEquals(TestJson2(1, mapOf("foo" to 1, "bar" to 2)), x)
|
||||
}
|
||||
```
|
||||
|
||||
But what if your map has objects of different types? The approach of using polymorphism is partially applicable, but what to do with `{ one: 1, two: "two" }`?
|
||||
|
||||
The answer is pretty simple: use `JsonObject` in your deserializable object. This class is capable of holding any JSON types and structures and is sort of a silver bullet for such cases:
|
||||
|
||||
~~~kotlin
|
||||
@Serializable
|
||||
data class TestJson3(
|
||||
val value: Int,
|
||||
val inner: JsonObject
|
||||
)
|
||||
@Test
|
||||
fun deserializeAnyMapWithJsonTest() = runTest {
|
||||
val x = eval("""
|
||||
import lyng.serialization
|
||||
{ value: 12, inner: { "foo": 1, "bar": "two" }}
|
||||
""".trimIndent()).decodeSerializable<TestJson3>()
|
||||
assertEquals(TestJson3(12, JsonObject(mapOf("foo" to JsonPrimitive(1), "bar" to Json.encodeToJsonElement("two")))), x)
|
||||
}
|
||||
~~~
|
||||
|
||||
|
||||
# List of supported types
|
||||
|
||||
| Lyng type | JSON type | notes |
|
||||
|-----------|-----------|-------------|
|
||||
| `Int` | number | |
|
||||
| `Real` | number | |
|
||||
| `String` | string | |
|
||||
| `Bool` | boolean | |
|
||||
| `null` | null | |
|
||||
| `Instant` | string | ISO8601 (1) |
|
||||
| `List` | array | (2) |
|
||||
| `Map` | object | (2) |
|
||||
|
||||
|
||||
(1)
|
||||
: ISO8601 flavor 1970-05-06T06:00:00.000Z in used; number of fractional digits depends on the truncation
|
||||
on [Instant](time.md), see `Instant.truncateTo...` functions.
|
||||
|
||||
(2)
|
||||
: List may contain any objects serializable to Json.
|
||||
|
||||
(3)
|
||||
: Map keys must be strings, map values may be any objects serializable to Json.
|
||||
|
||||
@@ -0,0 +1,141 @@
|
||||
### Lyng CLI (`lyng`)
|
||||
|
||||
The Lyng CLI is the reference command-line tool for the Lyng language. It lets you:
|
||||
|
||||
- Run Lyng scripts from files or inline strings (shebangs accepted)
|
||||
- Use standard argument passing (`ARGV`) to your scripts.
|
||||
- Format Lyng source files via the built-in `fmt` subcommand.
|
||||
|
||||
|
||||
#### Building on Linux
|
||||
|
||||
Requirements:
|
||||
- JDK 17+ (for Gradle and the JVM distribution)
|
||||
- GNU zip utilities (for packaging the native executable)
|
||||
- upx tool (executable in-place compression)
|
||||
|
||||
The repository provides convenience scripts in `bin/` for local builds and installation into `~/bin`.
|
||||
|
||||
Note: In this repository the scripts are named `bin/local_release` and `bin/local_jrelease`. In some environments these may be aliased as `bin/release` and `bin/jrelease`. The steps below use the actual file names present here.
|
||||
|
||||
|
||||
##### Option A: Native linuxX64 executable (`lyng`)
|
||||
|
||||
1) Build the native binary:
|
||||
|
||||
```
|
||||
./gradlew :lyng:linkReleaseExecutableLinuxX64
|
||||
```
|
||||
|
||||
2) Install and package locally:
|
||||
|
||||
```
|
||||
bin/local_release
|
||||
```
|
||||
|
||||
What this does:
|
||||
- Copies the built executable to `~/bin/lyng` for easy use in your shell.
|
||||
- Produces `distributables/lyng-linuxX64.zip` containing the `lyng` executable.
|
||||
|
||||
|
||||
##### Option B: JVM distribution (`jlyng` launcher)
|
||||
|
||||
This creates a JVM distribution with a launcher script and links it to `~/bin/jlyng`.
|
||||
|
||||
```
|
||||
bin/local_jrelease
|
||||
```
|
||||
|
||||
What this does:
|
||||
- Runs `./gradlew :lyng:installJvmDist` to build the JVM app distribution to `lyng/build/install/lyng-jvm`.
|
||||
- Copies the distribution under `~/bin/jlyng-jvm`.
|
||||
- Creates a symlink `~/bin/jlyng` pointing to the launcher script.
|
||||
|
||||
|
||||
#### Usage
|
||||
|
||||
Once installed, ensure `~/bin` is on your `PATH`. You can then use either the native `lyng` or the JVM `jlyng` launcher (both have the same CLI surface).
|
||||
|
||||
|
||||
##### Running scripts
|
||||
|
||||
- Run a script by file name and pass arguments to `ARGV`:
|
||||
|
||||
```
|
||||
lyng path/to/script.lyng arg1 arg2
|
||||
```
|
||||
|
||||
- Run a script whose name starts with `-` using `--` to stop option parsing:
|
||||
|
||||
```
|
||||
lyng -- -my-script.lyng arg1 arg2
|
||||
```
|
||||
|
||||
- Execute inline code with `-x/--execute` and pass positional args to `ARGV`:
|
||||
|
||||
```
|
||||
lyng -x "println(\"Hello\")" more args
|
||||
```
|
||||
|
||||
- Print version/help:
|
||||
|
||||
```
|
||||
lyng --version
|
||||
lyng --help
|
||||
```
|
||||
|
||||
### Use in shell scripts
|
||||
|
||||
Standard unix shebangs (`#!`) are supported, so you can make Lyng scripts directly executable on Unix-like systems. For example:
|
||||
|
||||
#!/usr/bin/env lyng
|
||||
println("Hello, world!")
|
||||
|
||||
|
||||
##### Formatting source: `fmt` subcommand
|
||||
|
||||
Format Lyng files with the built-in formatter.
|
||||
|
||||
Basic usage:
|
||||
|
||||
```
|
||||
lyng fmt [OPTIONS] FILE...
|
||||
```
|
||||
|
||||
Options:
|
||||
- `--check` — Check-only mode. Prints file paths that would change and exits with code 2 if any changes are needed, 0 otherwise.
|
||||
- `-i, --in-place` — Write formatted content back to the source files (off by default).
|
||||
- `--spacing` — Apply spacing normalization.
|
||||
- `--wrap`, `--wrapping` — Enable line wrapping.
|
||||
|
||||
Semantics and exit codes:
|
||||
- Default behavior is to write formatted content to stdout. When multiple files are provided, the output is separated with `--- <path> ---` headers.
|
||||
- `--check` and `--in-place` are mutually exclusive; using both results in an error and exit code 1.
|
||||
- `--check` exits with 2 if any file would change, with 0 otherwise.
|
||||
- Other errors (e.g., I/O issues) result in a non-zero exit code.
|
||||
|
||||
Examples:
|
||||
|
||||
```
|
||||
# Print formatted content to stdout
|
||||
lyng fmt src/file.lyng
|
||||
|
||||
# Format multiple files to stdout with headers
|
||||
lyng fmt src/a.lyng src/b.lyng
|
||||
|
||||
# Check mode: list files that would change; exit 2 if changes are needed
|
||||
lyng fmt --check src/**/*.lyng
|
||||
|
||||
# In-place formatting
|
||||
lyng fmt -i src/**/*.lyng
|
||||
|
||||
# Enable spacing normalization and wrapping
|
||||
lyng fmt --spacing --wrap src/file.lyng
|
||||
```
|
||||
|
||||
|
||||
#### Notes
|
||||
|
||||
- Both native and JVM distributions expose the same CLI interface. Use whichever best fits your environment.
|
||||
- When executing scripts, all positional arguments after the script name are available in Lyng as `ARGV`.
|
||||
- The interpreter recognizes shebang lines (`#!`) at the beginning of a script file and ignores them at runtime, so you can make Lyng scripts directly executable on Unix-like systems.
|
||||
@@ -0,0 +1,25 @@
|
||||
# Migration of Instant and Clock
|
||||
|
||||
## History
|
||||
|
||||
Before kotlin 2.0, there was an excellent library, kotlinx.datetime, which was widely used everywhere, also in Lyng and its dependencies.
|
||||
|
||||
When kotlin 2.0 was released, or soon after, JetBrains made an exptic decision to remove `Instant` and `Clock` from kotlinx.datetime and replace it with _yet experimental_ analogs in `kotlin.time`.
|
||||
|
||||
The problem is, these were not quite the same (these weren't `@Serializable`!), so people didn't migrate with ease. Okay, then JetBrains decided to not only deprecate it but also make them unusable on Apple targets. It sort of split auditories of many published libraries to those who hate JetBrains and Apple and continue to use 1.9-2.0 compatible versions that no longer work with Kotlin 2.2 on Apple targets (but work pretty well with earlier Kotlin or on other platforms).
|
||||
|
||||
Later JetBrains added serializers for their new `Instant` and `Clock` types, but strangely not in the stdlib, but in newer versions of `kotlinx.serialization`. This means that plain upgrade of dependencies to 2.2 is not enough to make them work.
|
||||
|
||||
## Solution
|
||||
|
||||
We hereby publish a new version of Lyng, 1.0.8-SNAPSHOT, which uses `ktlin.time.Instant` and `kotlin.time.Clock` instead of `kotlinx.datetime.Instant` and `kotlinx.datetime.Clock; it is in other aspects compatible also with Lynon encoded binaries. Still you might need to migrate your code to use `kotlinx.datetime` types.
|
||||
|
||||
So, if you are getting errors with new version, plase do:
|
||||
|
||||
- upgrade to Kotlin 2.2
|
||||
- upgrade to Lyng 1.0.8-SNAPSHOT
|
||||
- replace in your code imports (or other uses) of`kotlinx.datetime.Clock` to `kotlin.time.Clock` and `kotlinx.datetime.Instant` to `kotlin.time.Instant`.
|
||||
|
||||
This should solve the problem and hopefully we'll see no more suh a brillant ideas from IDEA ideologspersons.
|
||||
|
||||
Sorry for inconvenicence and send a ray of hate to JetBrains ;)
|
||||
@@ -221,3 +221,22 @@ Lyng includes an optional optimization for function/method calls on JVM: scope f
|
||||
- Expected effect (from our JVM micro‑benchmarks): in deep call loops, enabling pooling reduced total time by about 1.38× in a dedicated pooling benchmark; mileage may vary depending on workload.
|
||||
|
||||
Future work: introduce thread‑safe pooling (e.g., per‑thread pools or confinement strategies) before considering enabling it by default in multi‑threaded environments.
|
||||
|
||||
### Closures inside coroutine helpers (launch/flow)
|
||||
|
||||
Closures executed by `launch { ... }` and `flow { ... }` resolve names using the `ClosureScope` rules:
|
||||
|
||||
1. Closure frame locals/arguments
|
||||
2. Captured receiver instance/class members
|
||||
3. Closure ancestry locals + each frame’s `this` members (cycle‑safe)
|
||||
4. Caller `this` members
|
||||
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:
|
||||
- Outer locals (e.g., `counter`) stay visible across suspension points.
|
||||
- Global helpers like `delay(ms)` and `yield()` are available from inside closures.
|
||||
- If you write your own async helpers, execute user lambdas under `ClosureScope(callScope, capturedCreatorScope)` and avoid manual ancestry walking.
|
||||
|
||||
See also: [Scopes and Closures: resolution and safety](scopes_and_closures.md)
|
||||
@@ -7,6 +7,11 @@ val files = Path("../..").list().toList()
|
||||
// most long is longest?
|
||||
val longestNameLength = files.maxOf { it.name.length }
|
||||
|
||||
// testdoc
|
||||
fun test() {
|
||||
22
|
||||
}
|
||||
|
||||
|
||||
val format = "%"+(longestNameLength+1) +"s %d"
|
||||
for( f in files )
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
# Scopes and Closures: resolution and safety
|
||||
|
||||
This page documents how name resolution works with `ClosureScope`, how to avoid recursion pitfalls, and how to safely capture and execute callbacks that need access to outer locals.
|
||||
|
||||
## Why this matters
|
||||
Name lookup across nested scopes and closures can accidentally form recursive resolution paths or hide expected symbols (outer locals, module/global functions). The rules below ensure predictable resolution and prevent infinite recursion.
|
||||
|
||||
## Resolution order in ClosureScope
|
||||
When evaluating an identifier `name` inside a closure, `ClosureScope.get(name)` resolves in this order:
|
||||
|
||||
1. Closure frame locals and arguments
|
||||
2. Captured receiver (`closureScope.thisObj`) instance/class members
|
||||
3. Closure ancestry locals + each frame’s `thisObj` members (cycle‑safe)
|
||||
4. Caller `this` members
|
||||
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 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
|
||||
When authoring new scope types or advanced lookups, avoid calling virtual `get` while walking parents. Instead, use the non‑dispatch helpers on `Scope`:
|
||||
|
||||
- `chainLookupIgnoreClosure(name)`
|
||||
- Walk raw `parent` chain and check only per‑frame locals/bindings/slots.
|
||||
- Ignores overridden `get` (e.g., in `ClosureScope`). Cycle‑safe.
|
||||
- `chainLookupWithMembers(name)`
|
||||
- Like above, but after locals/bindings it also checks each frame’s `thisObj` members.
|
||||
- Ignores overridden `get`. Cycle‑safe.
|
||||
- `baseGetIgnoreClosure(name)`
|
||||
- For the current frame only: check locals/bindings, then walk raw parents (locals/bindings), then fallback to this frame’s `thisObj` members.
|
||||
|
||||
These helpers avoid ping‑pong recursion and make structural cycles harmless (lookups terminate).
|
||||
|
||||
## Preventing structural cycles
|
||||
- Don’t construct parent chains that can point back to a descendant.
|
||||
- A debug‑time guard throws if assigning a parent would create a cycle; keep it enabled for development builds.
|
||||
- Even with a cycle, chain helpers break out via a small `visited` set keyed by `frameId`.
|
||||
|
||||
## Capturing lexical environments for callbacks
|
||||
For dynamic objects or custom builders, capture the creator’s lexical scope so callbacks can see outer locals/parameters:
|
||||
|
||||
1. Use `snapshotForClosure()` on the caller scope to capture locals/bindings/slots and parent.
|
||||
2. Store this snapshot and run callbacks under `ClosureScope(callScope, captured)`.
|
||||
|
||||
Kotlin sketch:
|
||||
```kotlin
|
||||
val captured = scope.snapshotForClosure()
|
||||
val execScope = ClosureScope(currentCallScope, captured)
|
||||
callback.execute(execScope)
|
||||
```
|
||||
|
||||
This ensures expressions like `contractName` used inside dynamic `get { name -> ... }` resolve to outer variables defined at the creation site.
|
||||
|
||||
## Closures in coroutines (launch/flow)
|
||||
- The closure frame still prioritizes its own locals/args.
|
||||
- Outer locals declared before suspension points remain visible through slot‑aware ancestry lookups.
|
||||
- Global functions like `delay(ms)` and `yield()` are resolved via module/root fallbacks from within closures.
|
||||
|
||||
Tip: If a closure unexpectedly cannot see an outer local, check whether an intermediate runtime helper introduced an extra call frame; the built‑in lookup already traverses caller ancestry, so prefer the standard helpers rather than custom dispatch.
|
||||
|
||||
## Local variable references and missing symbols
|
||||
- Unqualified identifier resolution first prefers locals/bindings/slots before falling back to `this` members.
|
||||
- If neither locals nor members contain the symbol, missing field lookups map to `SymbolNotFound` (compatibility alias for `SymbolNotDefinedException`).
|
||||
|
||||
## Performance notes
|
||||
- The `visited` sets used for cycle detection are tiny and short‑lived; in typical scripts the overhead is negligible.
|
||||
- If profiling shows hotspots, consider limiting ancestry depth in your custom helpers or using small fixed arrays instead of hash sets—only for extremely hot code paths.
|
||||
|
||||
## Dos and Don’ts
|
||||
- Do use `chainLookupIgnoreClosure` / `chainLookupWithMembers` for ancestry traversals.
|
||||
- Do maintain the resolution order above for predictable behavior.
|
||||
- Don’t call virtual `get` while walking parents; it risks recursion across scope types.
|
||||
- Don’t attach instance scopes to transient/pool frames; bind to a stable parent scope instead.
|
||||
+127
-31
@@ -6,9 +6,10 @@ In other word, the code usually works as expected when you see it. So, nothing u
|
||||
|
||||
__Other documents to read__ maybe after this one:
|
||||
|
||||
- [Advanced topics](advanced_topics.md), [declaring arguments](declaring_arguments.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)
|
||||
- [math in Lyng](math.md), [the `when` statement](when.md)
|
||||
- [Testing and Assertions](Testing.md)
|
||||
- [time](time.md) and [parallelism](parallelism.md)
|
||||
- [parallelism] - multithreaded code, coroutines, etc.
|
||||
- Some class
|
||||
@@ -66,6 +67,26 @@ You can use blocks in if statement, as expected:
|
||||
limited
|
||||
>>> 120.0
|
||||
|
||||
## Enums (quick intro)
|
||||
|
||||
Lyng supports simple enums for a fixed set of named constants. Declare with `enum Name { ... }` and use entries as `Name.ENTRY`.
|
||||
|
||||
enum Color {
|
||||
RED, GREEN, BLUE
|
||||
}
|
||||
|
||||
assert( Color.RED is Color )
|
||||
assertEquals( 0, Color.RED.ordinal )
|
||||
assertEquals( "BLUE", Color.BLUE.name )
|
||||
|
||||
// All entries as a list, in order:
|
||||
assertEquals( [Color.RED, Color.GREEN, Color.BLUE], Color.entries )
|
||||
|
||||
// Lookup by name:
|
||||
assertEquals( Color.GREEN, Color.valueOf("GREEN") )
|
||||
|
||||
For more details (usage patterns, `when` switching, serialization), see OOP notes: [Enums in detail](OOP.md#enums).
|
||||
|
||||
When putting multiple statments in the same line it is convenient and recommended to use `;`:
|
||||
|
||||
var from; var to
|
||||
@@ -86,6 +107,41 @@ Assignemnt is an expression that changes its lvalue and return assigned value:
|
||||
>>> 11
|
||||
>>> 6
|
||||
|
||||
### Destructuring assignments
|
||||
|
||||
Lyng supports destructuring assignments for lists. This allows you to unpack list elements into multiple variables at once:
|
||||
|
||||
val [a, b, c] = [1, 2, 3]
|
||||
assertEquals(1, a)
|
||||
assertEquals(2, b)
|
||||
assertEquals(3, c)
|
||||
|
||||
It also supports *splats* (ellipsis) to capture multiple elements into a list:
|
||||
|
||||
val [head, rest...] = [1, 2, 3]
|
||||
assertEquals(1, head)
|
||||
assertEquals([2, 3], rest)
|
||||
|
||||
val [first, middle..., last] = [1, 2, 3, 4, 5]
|
||||
assertEquals(1, first)
|
||||
assertEquals([2, 3, 4], middle)
|
||||
assertEquals(5, last)
|
||||
|
||||
Destructuring can be nested:
|
||||
|
||||
val [x, [y, z...]] = [1, [2, 3, 4]]
|
||||
assertEquals(1, x)
|
||||
assertEquals(2, y)
|
||||
assertEquals([3, 4], z)
|
||||
|
||||
And it can be used for reassigning existing variables, for example, to swap values:
|
||||
|
||||
var x = 5
|
||||
var y = 10
|
||||
[x, y] = [y, x]
|
||||
assertEquals(10, x)
|
||||
assertEquals(5, y)
|
||||
|
||||
As the assignment itself is an expression, you can use it in strange ways. Just remember
|
||||
to use parentheses as assignment operation insofar is left-associated and will not
|
||||
allow chained assignments (we might fix it later). Use parentheses insofar:
|
||||
@@ -205,7 +261,7 @@ Much like let, but it does not alter returned value:
|
||||
>>> void
|
||||
|
||||
While it is not altering return value, the source object could be changed:
|
||||
|
||||
also
|
||||
class Point(x,y)
|
||||
val p = Point(1,2).also { it.x++ }
|
||||
assertEquals(p.x, 2)
|
||||
@@ -291,8 +347,6 @@ Reference quality and object equality example:
|
||||
assert( null == null) // singletons
|
||||
assert( null === null)
|
||||
// but, for non-singletons:
|
||||
assert( 5 == 5)
|
||||
assert( 5 !== 5)
|
||||
assert( "foo" !== "foo" )
|
||||
>>> void
|
||||
|
||||
@@ -309,7 +363,7 @@ will be thrown:
|
||||
// WRONG! Exception will be thrown at next line:
|
||||
foo + "bar"
|
||||
|
||||
Correct pattern is:
|
||||
The correct pattern is:
|
||||
|
||||
foo = "foo"
|
||||
// now is OK:
|
||||
@@ -383,6 +437,19 @@ It is possible to define also vararg using ellipsis:
|
||||
|
||||
See the [arguments reference](declaring_arguments.md) for more details.
|
||||
|
||||
## Named arguments
|
||||
|
||||
When calling functions, you can use named arguments with the colon syntax `name: value`. This is particularly useful when you have many parameters with default values.
|
||||
|
||||
```lyng
|
||||
fun test(a="foo", b="bar", c="bazz") { [a, b, c] }
|
||||
|
||||
assertEquals(["foo", "b", "bazz"], test(b: "b"))
|
||||
assertEquals(["a", "bar", "c"], test("a", c: "c"))
|
||||
```
|
||||
|
||||
**Note for Kotlin users:** Lyng uses `:` instead of `=` for named arguments at call sites. This is because in Lyng, `=` is an expression that returns the assigned value, and using it in an argument list would create ambiguity.
|
||||
|
||||
## Closures
|
||||
|
||||
Each __block has an isolated context that can be accessed from closures__. For example:
|
||||
@@ -458,6 +525,8 @@ one could be with ellipsis that means "the rest pf arguments as List":
|
||||
|
||||
### Using lambda as the parameter
|
||||
|
||||
See also: [Testing and Assertions](Testing.md)
|
||||
|
||||
// note that fun returns its last calculated value,
|
||||
// in our case, result after in-place addition:
|
||||
fun mapValues(iterable, transform) {
|
||||
@@ -1358,30 +1427,31 @@ Part match:
|
||||
|
||||
Typical set of String functions includes:
|
||||
|
||||
| fun/prop | description / notes |
|
||||
|--------------------|------------------------------------------------------------|
|
||||
| lower() | change case to unicode upper |
|
||||
| upper() | change case to unicode lower |
|
||||
| trim() | trim space chars from both ends |
|
||||
| startsWith(prefix) | true if starts with a prefix |
|
||||
| endsWith(prefix) | true if ends with a prefix |
|
||||
| take(n) | get a new string from up to n first characters |
|
||||
| takeLast(n) | get a new string from up to n last characters |
|
||||
| drop(n) | get a new string dropping n first chars, or empty string |
|
||||
| dropLast(n) | get a new string dropping n last chars, or empty string |
|
||||
| size | size in characters like `length` because String is [Array] |
|
||||
| (args...) | sprintf-like formatting, see [string formatting] |
|
||||
| [index] | character at index |
|
||||
| [Range] | substring at range (2) |
|
||||
| [Regex] | find first match of regex, like [Regex.find] (2) |
|
||||
| s1 + s2 | concatenation |
|
||||
| s1 += s2 | self-modifying concatenation |
|
||||
| toReal() | attempts to parse string as a Real value |
|
||||
| toInt() | parse string to Int value |
|
||||
| characters() | create [List] of characters (1) |
|
||||
| encodeUtf8() | returns [Buffer] with characters encoded to utf8 |
|
||||
| matches(re) | matches the regular expression (2) |
|
||||
| | |
|
||||
| fun/prop | description / notes |
|
||||
|----------------------|------------------------------------------------------------|
|
||||
| lower(), lowercase() | change case to unicode upper |
|
||||
| upper(), uppercase() | change case to unicode lower |
|
||||
| trim() | trim space chars from both ends |
|
||||
| startsWith(prefix) | true if starts with a prefix |
|
||||
| endsWith(prefix) | true if ends with a prefix |
|
||||
| last() | get last character of a string or throw |
|
||||
| take(n) | get a new string from up to n first characters |
|
||||
| takeLast(n) | get a new string from up to n last characters |
|
||||
| drop(n) | get a new string dropping n first chars, or empty string |
|
||||
| dropLast(n) | get a new string dropping n last chars, or empty string |
|
||||
| size | size in characters like `length` because String is [Array] |
|
||||
| (args...) | sprintf-like formatting, see [string formatting] |
|
||||
| [index] | character at index |
|
||||
| [Range] | substring at range (2) |
|
||||
| [Regex] | find first match of regex, like [Regex.find] (2) |
|
||||
| s1 + s2 | concatenation |
|
||||
| s1 += s2 | self-modifying concatenation |
|
||||
| toReal() | attempts to parse string as a Real value |
|
||||
| toInt() | parse string to Int value |
|
||||
| characters() | create [List] of characters (1) |
|
||||
| encodeUtf8() | returns [Buffer] with characters encoded to utf8 |
|
||||
| matches(re) | matches the regular expression (2) |
|
||||
| | |
|
||||
|
||||
(1)
|
||||
: List is mutable therefore a new copy is created on each call.
|
||||
@@ -1445,6 +1515,8 @@ Lambda avoid unnecessary execution if assertion is not failed. for example:
|
||||
|
||||
[List]: List.md
|
||||
|
||||
[Testing]: Testing.md
|
||||
|
||||
[Iterable]: Iterable.md
|
||||
|
||||
[Iterator]: Iterator.md
|
||||
@@ -1533,7 +1605,31 @@ assertEquals(null, (buzz as? Foo)?.runA())
|
||||
|
||||
Notes:
|
||||
- Resolution order uses C3 MRO (active): deterministic, monotonic order suitable for diamonds and complex hierarchies. Example: for `class D() : B(), C()` where both `B()` and `C()` derive from `A()`, the C3 order is `D → B → C → A`. The first visible match wins.
|
||||
- `private` is visible only inside the declaring class; `protected` is visible from the declaring class and any of its transitive subclasses. Qualification (`this@Type`) or casts do not bypass visibility.
|
||||
- `private` is visible only inside the declaring class; `protected` is visible from the declaring class and any of its transitive subclasses. Qualialsofication (`this@Type`) or casts do not bypass visibility.
|
||||
- Safe‑call `?.` works with `as?` for optional dispatch.
|
||||
|
||||
To get details on OOP in Lyng, see [OOP notes](oop.md).
|
||||
## Extension members
|
||||
|
||||
You can add new methods and properties to existing classes without modifying them.
|
||||
|
||||
### Extension functions
|
||||
|
||||
fun String.shout() = this.upper() + "!!!"
|
||||
"hello".shout()
|
||||
>>> "HELLO!!!"
|
||||
|
||||
### Extension properties
|
||||
|
||||
val Int.isEven = this % 2 == 0
|
||||
4.isEven
|
||||
>>> true
|
||||
|
||||
Example with custom accessors:
|
||||
|
||||
val String.firstChar get() = this[0]
|
||||
"abc".firstChar
|
||||
>>> 'a'
|
||||
|
||||
Extension members are **scope-isolated**: they are visible only in the scope where they are defined and its children. This prevents name collisions and improves security.
|
||||
|
||||
To get details on OOP in Lyng, see [OOP notes](OOP.md).
|
||||
@@ -76,8 +76,8 @@
|
||||
},
|
||||
"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]*" } ] },
|
||||
"declarations": { "patterns": [ { "name": "meta.function.declaration.lyng", "match": "\\b(?:fun|fn)\\s+([\\p{L}_][\\p{L}\\p{N}_]*)", "captures": { "1": { "name": "entity.name.function.lyng" } } }, { "name": "meta.type.declaration.lyng", "match": "\\b(?:class|enum)\\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}_]*)", "captures": { "1": { "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|val|var|import|package|constructor|property|open|extern|private|protected|static)\\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" } ] },
|
||||
"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)\\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|val|var|import|package|constructor|property|open|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)\\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*\\()" } ] },
|
||||
"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": "[!?]" } ] },
|
||||
|
||||
+2
-2
@@ -32,9 +32,9 @@ kotlin.native.cacheKind.linuxX64=none
|
||||
|
||||
# Workaround: Ensure Gradle uses a JDK with `jlink` available for AGP's JDK image transform.
|
||||
# On this environment, the system JDK 21 installation lacks `jlink`, causing
|
||||
# :lynglib:androidJdkImage to fail. Point Gradle to JDK 17 which includes `jlink`.
|
||||
# :lynglib:androidJdkImage to fail. Point Gradle to a JDK that includes `jlink`.
|
||||
# This affects only the JDK Gradle runs with; Kotlin/JVM target remains compatible.
|
||||
#org.gradle.java.home=/usr/lib/jvm/java-17-openjdk-amd64
|
||||
#org.gradle.java.home=/home/sergeych/.jdks/corretto-21.0.9
|
||||
android.experimental.lint.migrateToK2=false
|
||||
android.lint.useK2Uast=false
|
||||
kotlin.mpp.applyDefaultHierarchyTemplate=true
|
||||
@@ -1,11 +1,11 @@
|
||||
[versions]
|
||||
agp = "8.5.2"
|
||||
clikt = "5.0.3"
|
||||
kotlin = "2.2.21"
|
||||
kotlin = "2.3.0"
|
||||
android-minSdk = "24"
|
||||
android-compileSdk = "34"
|
||||
kotlinx-coroutines = "1.10.2"
|
||||
mp_bintools = "0.1.12"
|
||||
mp_bintools = "0.3.2"
|
||||
firebaseCrashlyticsBuildtools = "3.0.3"
|
||||
okioVersion = "3.10.2"
|
||||
compiler = "3.2.0-alpha11"
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
|
||||
* 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.
|
||||
@@ -21,7 +21,7 @@ plugins {
|
||||
}
|
||||
|
||||
group = "net.sergeych.lyng"
|
||||
version = "0.0.3-SNAPSHOT"
|
||||
version = "0.0.5-SNAPSHOT"
|
||||
|
||||
kotlin {
|
||||
jvmToolchain(17)
|
||||
@@ -37,8 +37,14 @@ repositories {
|
||||
|
||||
dependencies {
|
||||
implementation(project(":lynglib"))
|
||||
// Include lyngio so Quick Docs can reflectively load fs docs registrar (FsBuiltinDocs)
|
||||
implementation(project(":lyngio"))
|
||||
// Rich Markdown renderer for Quick Docs
|
||||
implementation("com.vladsch.flexmark:flexmark-all:0.64.8")
|
||||
|
||||
// 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
|
||||
testImplementation("junit:junit:4.13.2")
|
||||
}
|
||||
|
||||
intellij {
|
||||
|
||||
+29
-7
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
|
||||
* 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.
|
||||
@@ -54,7 +54,9 @@ class LyngExternalAnnotator : ExternalAnnotator<LyngExternalAnnotator.Input, Lyn
|
||||
|
||||
override fun collectInformation(file: PsiFile): Input? {
|
||||
val doc: Document = file.viewProvider.document ?: return null
|
||||
val prev = file.getUserData(CACHE_KEY)?.spans
|
||||
val cached = file.getUserData(CACHE_KEY)
|
||||
// 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
|
||||
return Input(doc.text, doc.modificationStamp, prev)
|
||||
}
|
||||
|
||||
@@ -76,9 +78,11 @@ class LyngExternalAnnotator : ExternalAnnotator<LyngExternalAnnotator.Input, Lyn
|
||||
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,
|
||||
collectedInfo.previousSpans ?: emptyList(),
|
||||
trimmed,
|
||||
Error(start, end, e.errorMessage)
|
||||
)
|
||||
}
|
||||
@@ -113,6 +117,7 @@ class LyngExternalAnnotator : ExternalAnnotator<LyngExternalAnnotator.Input, Lyn
|
||||
d.name,
|
||||
if (d.mutable) LyngHighlighterColors.VARIABLE else LyngHighlighterColors.VALUE
|
||||
)
|
||||
is MiniEnumDecl -> putName(d.nameStart, d.name, LyngHighlighterColors.TYPE)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -153,12 +158,17 @@ class LyngExternalAnnotator : ExternalAnnotator<LyngExternalAnnotator.Input, Lyn
|
||||
is MiniFunDecl -> {
|
||||
addTypeSegments(d.returnType)
|
||||
d.params.forEach { addTypeSegments(it.type) }
|
||||
addTypeSegments(d.receiver)
|
||||
}
|
||||
is MiniValDecl -> {
|
||||
addTypeSegments(d.type)
|
||||
addTypeSegments(d.receiver)
|
||||
}
|
||||
is MiniValDecl -> addTypeSegments(d.type)
|
||||
is MiniClassDecl -> {
|
||||
d.ctorFields.forEach { addTypeSegments(it.type) }
|
||||
d.classFields.forEach { addTypeSegments(it.type) }
|
||||
}
|
||||
is MiniEnumDecl -> {}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -175,9 +185,9 @@ class LyngExternalAnnotator : ExternalAnnotator<LyngExternalAnnotator.Input, Lyn
|
||||
fun keyForKind(k: SymbolKind) = when (k) {
|
||||
SymbolKind.Function -> LyngHighlighterColors.FUNCTION
|
||||
SymbolKind.Class, SymbolKind.Enum -> LyngHighlighterColors.TYPE
|
||||
SymbolKind.Param -> LyngHighlighterColors.PARAMETER
|
||||
SymbolKind.Val -> LyngHighlighterColors.VALUE
|
||||
SymbolKind.Var -> LyngHighlighterColors.VARIABLE
|
||||
SymbolKind.Parameter -> LyngHighlighterColors.PARAMETER
|
||||
SymbolKind.Value -> LyngHighlighterColors.VALUE
|
||||
SymbolKind.Variable -> LyngHighlighterColors.VARIABLE
|
||||
}
|
||||
|
||||
// Track covered ranges to not override later heuristics
|
||||
@@ -258,6 +268,18 @@ class LyngExternalAnnotator : ExternalAnnotator<LyngExternalAnnotator.Input, Lyn
|
||||
}
|
||||
}
|
||||
|
||||
// Map Enum constants from token highlighter to IDEA enum constant color
|
||||
run {
|
||||
val tokens = try { SimpleLyngHighlighter().highlight(text) } catch (_: Throwable) { emptyList() }
|
||||
for (s in tokens) if (s.kind == HighlightKind.EnumConstant) {
|
||||
val start = s.range.start
|
||||
val end = s.range.endExclusive
|
||||
if (start in 0..end && end <= text.length && start < end) {
|
||||
putRange(start, end, LyngHighlighterColors.ENUM_CONSTANT)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Build spell index payload: identifiers from symbols + references; comments/strings from simple highlighter
|
||||
val idRanges = mutableSetOf<IntRange>()
|
||||
try {
|
||||
|
||||
+597
@@ -0,0 +1,597 @@
|
||||
/*
|
||||
* 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.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* Lightweight BASIC completion for Lyng, MVP version.
|
||||
* Uses MiniAst (best-effort) + BuiltinDocRegistry to suggest symbols.
|
||||
*/
|
||||
package net.sergeych.lyng.idea.completion
|
||||
|
||||
import com.intellij.codeInsight.completion.*
|
||||
import com.intellij.codeInsight.lookup.LookupElementBuilder
|
||||
import com.intellij.icons.AllIcons
|
||||
import com.intellij.openapi.diagnostic.Logger
|
||||
import com.intellij.openapi.editor.Document
|
||||
import com.intellij.patterns.PlatformPatterns
|
||||
import com.intellij.psi.PsiFile
|
||||
import com.intellij.util.ProcessingContext
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import net.sergeych.lyng.highlight.offsetOf
|
||||
import net.sergeych.lyng.idea.LyngLanguage
|
||||
import net.sergeych.lyng.idea.highlight.LyngTokenTypes
|
||||
import net.sergeych.lyng.idea.settings.LyngFormatterSettings
|
||||
import net.sergeych.lyng.idea.util.DocsBootstrap
|
||||
import net.sergeych.lyng.idea.util.LyngAstManager
|
||||
import net.sergeych.lyng.idea.util.TextCtx
|
||||
import net.sergeych.lyng.miniast.*
|
||||
|
||||
class LyngCompletionContributor : CompletionContributor() {
|
||||
init {
|
||||
extend(
|
||||
CompletionType.BASIC,
|
||||
PlatformPatterns.psiElement().withLanguage(LyngLanguage),
|
||||
Provider
|
||||
)
|
||||
}
|
||||
|
||||
private object Provider : CompletionProvider<CompletionParameters>() {
|
||||
private val log = Logger.getInstance(LyngCompletionContributor::class.java)
|
||||
private const val DEBUG_COMPLETION = false
|
||||
|
||||
override fun addCompletions(
|
||||
parameters: CompletionParameters,
|
||||
context: ProcessingContext,
|
||||
result: CompletionResultSet
|
||||
) {
|
||||
// Ensure external/bundled docs are registered (e.g., lyng.io.fs with Path)
|
||||
DocsBootstrap.ensure()
|
||||
// Ensure stdlib Obj*-defined docs (e.g., String methods via ObjString.addFnDoc) are initialized
|
||||
StdlibDocsBootstrap.ensure()
|
||||
val file: PsiFile = parameters.originalFile
|
||||
if (file.language != LyngLanguage) return
|
||||
|
||||
// Disable completion inside comments
|
||||
val pos = parameters.position
|
||||
val et = pos.node.elementType
|
||||
if (et == LyngTokenTypes.LINE_COMMENT || et == LyngTokenTypes.BLOCK_COMMENT) return
|
||||
|
||||
// Feature toggle: allow turning completion off from settings
|
||||
val settings = LyngFormatterSettings.getInstance(file.project)
|
||||
if (!settings.enableLyngCompletionExperimental) return
|
||||
val document: Document = file.viewProvider.document ?: return
|
||||
val text = document.text
|
||||
val caret = parameters.offset.coerceIn(0, text.length)
|
||||
|
||||
val prefix = TextCtx.prefixAt(text, caret)
|
||||
val withPrefix = result.withPrefixMatcher(prefix).caseInsensitive()
|
||||
|
||||
// Emission with cap
|
||||
val cap = 200
|
||||
var added = 0
|
||||
val emit: (com.intellij.codeInsight.lookup.LookupElement) -> Unit = { le ->
|
||||
if (added < cap) {
|
||||
withPrefix.addElement(le)
|
||||
added++
|
||||
}
|
||||
}
|
||||
|
||||
// Determine if we are in member context (dot before caret or before word start)
|
||||
val wordRange = TextCtx.wordRangeAt(text, caret)
|
||||
val memberDotPos = (wordRange?.let { TextCtx.findDotLeft(text, it.startOffset) })
|
||||
?: TextCtx.findDotLeft(text, caret)
|
||||
if (DEBUG_COMPLETION) {
|
||||
log.info("[LYNG_DEBUG] Completion: caret=$caret prefix='${prefix}' memberDotPos=${memberDotPos} file='${file.name}'")
|
||||
}
|
||||
|
||||
// Build MiniAst (cached) for both global and member contexts to enable local class/val inference
|
||||
val mini = LyngAstManager.getMiniAst(file)
|
||||
val binding = LyngAstManager.getBinding(file)
|
||||
|
||||
// Delegate computation to the shared engine to keep behavior in sync with tests
|
||||
val engineItems = try {
|
||||
runBlocking { CompletionEngineLight.completeSuspend(text, caret) }
|
||||
} catch (t: Throwable) {
|
||||
if (DEBUG_COMPLETION) log.warn("[LYNG_DEBUG] Engine completion failed: ${t.message}")
|
||||
emptyList()
|
||||
}
|
||||
if (DEBUG_COMPLETION) {
|
||||
val preview = engineItems.take(10).joinToString { it.name }
|
||||
log.info("[LYNG_DEBUG] Engine items: count=${engineItems.size} preview=[${preview}]")
|
||||
}
|
||||
|
||||
// If we are in member context and the engine produced nothing, try a guarded local fallback
|
||||
if (memberDotPos != null && engineItems.isEmpty()) {
|
||||
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
|
||||
val fromText = extractImportsFromText(text)
|
||||
val imported = LinkedHashSet<String>().apply {
|
||||
fromText.forEach { add(it) }
|
||||
add("lyng.stdlib")
|
||||
}.toList()
|
||||
|
||||
// Try inferring return/receiver class around the dot
|
||||
val inferred =
|
||||
// Prefer MiniAst-based inference (return type from member call or receiver type)
|
||||
DocLookupUtils.guessReturnClassFromMemberCallBeforeMini(mini, text, memberDotPos, imported, binding)
|
||||
?: DocLookupUtils.guessReceiverClassViaMini(mini, text, memberDotPos, imported, binding)
|
||||
?:
|
||||
DocLookupUtils.guessReturnClassFromMemberCallBefore(text, memberDotPos, imported, mini)
|
||||
?: DocLookupUtils.guessReturnClassFromTopLevelCallBefore(text, memberDotPos, imported, mini)
|
||||
?: DocLookupUtils.guessReturnClassAcrossKnownCallees(text, memberDotPos, imported, mini)
|
||||
?: DocLookupUtils.guessReceiverClass(text, memberDotPos, imported, mini)
|
||||
|
||||
if (inferred != null) {
|
||||
if (DEBUG_COMPLETION) log.info("[LYNG_DEBUG] Fallback inferred receiver/return class='$inferred' — offering its members")
|
||||
offerMembers(emit, imported, inferred, sourceText = text, mini = mini)
|
||||
return
|
||||
} else {
|
||||
if (DEBUG_COMPLETION) log.info("[LYNG_DEBUG] Fallback could not infer class; keeping list empty (no globals after dot)")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
for (ci in engineItems) {
|
||||
val builder = when (ci.kind) {
|
||||
Kind.Function -> LookupElementBuilder.create(ci.name)
|
||||
.withIcon(AllIcons.Nodes.Function)
|
||||
.let { b -> if (!ci.tailText.isNullOrBlank()) b.withTailText(ci.tailText, true) else b }
|
||||
.let { b -> if (!ci.typeText.isNullOrBlank()) b.withTypeText(ci.typeText, true) else b }
|
||||
.withInsertHandler(ParenInsertHandler)
|
||||
Kind.Method -> LookupElementBuilder.create(ci.name)
|
||||
.withIcon(AllIcons.Nodes.Method)
|
||||
.let { b -> if (!ci.tailText.isNullOrBlank()) b.withTailText(ci.tailText, true) else b }
|
||||
.let { b -> if (!ci.typeText.isNullOrBlank()) b.withTypeText(ci.typeText, true) else b }
|
||||
.withInsertHandler(ParenInsertHandler)
|
||||
Kind.Class_ -> LookupElementBuilder.create(ci.name)
|
||||
.withIcon(AllIcons.Nodes.Class)
|
||||
Kind.Enum -> LookupElementBuilder.create(ci.name)
|
||||
.withIcon(AllIcons.Nodes.Enum)
|
||||
Kind.Value -> LookupElementBuilder.create(ci.name)
|
||||
.withIcon(AllIcons.Nodes.Field)
|
||||
.let { b -> if (!ci.typeText.isNullOrBlank()) b.withTypeText(ci.typeText, true) else b }
|
||||
Kind.Field -> LookupElementBuilder.create(ci.name)
|
||||
.withIcon(AllIcons.Nodes.Field)
|
||||
.let { b -> if (!ci.typeText.isNullOrBlank()) b.withTypeText(ci.typeText, true) else b }
|
||||
}
|
||||
emit(builder)
|
||||
}
|
||||
// In member context, ensure stdlib extension-like methods (e.g., String.re) are present
|
||||
if (memberDotPos != null) {
|
||||
val existing = engineItems.map { it.name }.toMutableSet()
|
||||
val fromText = extractImportsFromText(text)
|
||||
val imported = LinkedHashSet<String>().apply {
|
||||
fromText.forEach { add(it) }
|
||||
add("lyng.stdlib")
|
||||
}.toList()
|
||||
val inferredClass =
|
||||
DocLookupUtils.guessReturnClassFromMemberCallBeforeMini(mini, text, memberDotPos, imported, binding)
|
||||
?: DocLookupUtils.guessReceiverClassViaMini(mini, text, memberDotPos, imported, binding)
|
||||
?: DocLookupUtils.guessReturnClassFromMemberCallBefore(text, memberDotPos, imported, mini)
|
||||
?: DocLookupUtils.guessReturnClassFromTopLevelCallBefore(text, memberDotPos, imported, mini)
|
||||
?: DocLookupUtils.guessReturnClassAcrossKnownCallees(text, memberDotPos, imported, mini)
|
||||
?: DocLookupUtils.guessReceiverClass(text, memberDotPos, imported, mini)
|
||||
if (!inferredClass.isNullOrBlank()) {
|
||||
val ext = BuiltinDocRegistry.extensionMemberNamesFor(inferredClass)
|
||||
if (DEBUG_COMPLETION) log.info("[LYNG_DEBUG] Post-engine extension check for $inferredClass: ${'$'}{ext}")
|
||||
for (name in ext) {
|
||||
if (existing.contains(name)) continue
|
||||
val resolved = DocLookupUtils.resolveMemberWithInheritance(imported, inferredClass, name, mini)
|
||||
if (resolved != null) {
|
||||
when (val member = resolved.second) {
|
||||
is MiniMemberFunDecl -> {
|
||||
val params = member.params.joinToString(", ") { it.name }
|
||||
val ret = typeOf(member.returnType)
|
||||
val builder = LookupElementBuilder.create(name)
|
||||
.withIcon(AllIcons.Nodes.Method)
|
||||
.withTailText("(${ '$' }params)", true)
|
||||
.withTypeText(ret, true)
|
||||
.withInsertHandler(ParenInsertHandler)
|
||||
emit(builder)
|
||||
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 {
|
||||
// Fallback: emit simple method name without detailed types
|
||||
val builder = LookupElementBuilder.create(name)
|
||||
.withIcon(AllIcons.Nodes.Method)
|
||||
.withTailText("()", true)
|
||||
.withInsertHandler(ParenInsertHandler)
|
||||
emit(builder)
|
||||
existing.add(name)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// If in member context and engine items are suspiciously sparse, try to enrich via local inference + offerMembers
|
||||
if (memberDotPos != null && engineItems.size < 3) {
|
||||
if (DEBUG_COMPLETION) log.info("[LYNG_DEBUG] Engine produced only ${engineItems.size} items in member context — trying enrichment")
|
||||
val fromText = extractImportsFromText(text)
|
||||
val imported = LinkedHashSet<String>().apply {
|
||||
fromText.forEach { add(it) }
|
||||
add("lyng.stdlib")
|
||||
}.toList()
|
||||
val inferred =
|
||||
DocLookupUtils.guessReturnClassFromMemberCallBeforeMini(mini, text, memberDotPos, imported, binding)
|
||||
?: DocLookupUtils.guessReceiverClassViaMini(mini, text, memberDotPos, imported, binding)
|
||||
?: DocLookupUtils.guessReturnClassFromMemberCallBefore(text, memberDotPos, imported, mini)
|
||||
?: DocLookupUtils.guessReturnClassFromTopLevelCallBefore(text, memberDotPos, imported, mini)
|
||||
?: DocLookupUtils.guessReturnClassAcrossKnownCallees(text, memberDotPos, imported, mini)
|
||||
?: DocLookupUtils.guessReceiverClass(text, memberDotPos, imported, mini)
|
||||
if (inferred != null) {
|
||||
if (DEBUG_COMPLETION) log.info("[LYNG_DEBUG] Enrichment inferred class='$inferred' — offering its members")
|
||||
offerMembers(emit, imported, inferred, sourceText = text, mini = mini)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
private fun offerDecl(emit: (com.intellij.codeInsight.lookup.LookupElement) -> Unit, d: MiniDecl) {
|
||||
val name = d.name
|
||||
val builder = when (d) {
|
||||
is MiniFunDecl -> {
|
||||
val params = d.params.joinToString(", ") { it.name }
|
||||
val ret = typeOf(d.returnType)
|
||||
val tail = "(${params})"
|
||||
LookupElementBuilder.create(name)
|
||||
.withIcon(AllIcons.Nodes.Function)
|
||||
.withTailText(tail, true)
|
||||
.withTypeText(ret, true)
|
||||
.withInsertHandler(ParenInsertHandler)
|
||||
}
|
||||
is MiniClassDecl -> LookupElementBuilder.create(name)
|
||||
.withIcon(AllIcons.Nodes.Class)
|
||||
is MiniValDecl -> {
|
||||
val kindIcon = if (d.mutable) AllIcons.Nodes.Variable else AllIcons.Nodes.Field
|
||||
LookupElementBuilder.create(name)
|
||||
.withIcon(kindIcon)
|
||||
.withTypeText(typeOf(d.type), true)
|
||||
}
|
||||
is MiniEnumDecl -> LookupElementBuilder.create(name)
|
||||
.withIcon(AllIcons.Nodes.Enum)
|
||||
}
|
||||
emit(builder)
|
||||
}
|
||||
|
||||
private object ParenInsertHandler : InsertHandler<com.intellij.codeInsight.lookup.LookupElement> {
|
||||
override fun handleInsert(context: InsertionContext, item: com.intellij.codeInsight.lookup.LookupElement) {
|
||||
val doc = context.document
|
||||
val tailOffset = context.tailOffset
|
||||
val nextChar = doc.charsSequence.getOrNull(tailOffset)
|
||||
if (nextChar != '(') {
|
||||
doc.insertString(tailOffset, "()")
|
||||
context.editor.caretModel.moveToOffset(tailOffset + 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// --- Member completion helpers ---
|
||||
|
||||
private fun offerMembers(
|
||||
emit: (com.intellij.codeInsight.lookup.LookupElement) -> Unit,
|
||||
imported: List<String>,
|
||||
className: String,
|
||||
staticOnly: Boolean = false
|
||||
, sourceText: String,
|
||||
mini: MiniScript? = null
|
||||
) {
|
||||
val classes = DocLookupUtils.aggregateClasses(imported, mini)
|
||||
if (DEBUG_COMPLETION) {
|
||||
val keys = classes.keys.joinToString(", ")
|
||||
log.info("[LYNG_DEBUG] offerMembers: imported=${imported} classes=[${keys}] target=${className}")
|
||||
}
|
||||
val visited = mutableSetOf<String>()
|
||||
// Collect separated to keep tiers: direct first, then inherited
|
||||
val directMap = LinkedHashMap<String, MutableList<MiniMemberDecl>>()
|
||||
val inheritedMap = LinkedHashMap<String, MutableList<MiniMemberDecl>>()
|
||||
|
||||
// 0) Prefer locally-declared class members (same-file) when available
|
||||
val localClass = mini?.declarations?.filterIsInstance<MiniClassDecl>()?.firstOrNull { it.name == className }
|
||||
if (localClass != null) {
|
||||
for (m in localClass.members) {
|
||||
val list = directMap.getOrPut(m.name) { mutableListOf() }
|
||||
list.add(m)
|
||||
}
|
||||
// If MiniAst didn't populate members (empty), try to scan class body text for member signatures
|
||||
if (localClass.members.isEmpty()) {
|
||||
val scanned = DocLookupUtils.scanLocalClassMembersFromText(mini, text = sourceText, cls = localClass)
|
||||
if (DEBUG_COMPLETION) log.info("[LYNG_DEBUG] Local scan for class ${localClass.name}: found ${scanned.size} members -> ${scanned.keys}")
|
||||
for ((name, sig) in scanned) {
|
||||
when (sig.kind) {
|
||||
"fun" -> {
|
||||
val builder = LookupElementBuilder.create(name)
|
||||
.withIcon(AllIcons.Nodes.Method)
|
||||
.withTailText("(" + (sig.params?.joinToString(", ") ?: "") + ")", true)
|
||||
.let { b -> sig.typeText?.let { b.withTypeText(": $it", true) } ?: b }
|
||||
.withInsertHandler(ParenInsertHandler)
|
||||
emit(builder)
|
||||
}
|
||||
"val", "var" -> {
|
||||
val builder = LookupElementBuilder.create(name)
|
||||
.withIcon(if (sig.kind == "var") AllIcons.Nodes.Variable else AllIcons.Nodes.Field)
|
||||
.let { b -> sig.typeText?.let { b.withTypeText(": $it", true) } ?: b }
|
||||
emit(builder)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun addMembersOf(clsName: String, tierDirect: Boolean) {
|
||||
val cls = classes[clsName] ?: return
|
||||
val target = if (tierDirect) directMap else inheritedMap
|
||||
for (m in cls.members) {
|
||||
if (staticOnly) {
|
||||
// Filter only static members in namespace/static context
|
||||
when (m) {
|
||||
is MiniMemberFunDecl -> if (!m.isStatic) continue
|
||||
is MiniMemberValDecl -> if (!m.isStatic) continue
|
||||
is MiniInitDecl -> continue
|
||||
}
|
||||
}
|
||||
val list = target.getOrPut(m.name) { mutableListOf() }
|
||||
list.add(m)
|
||||
}
|
||||
// Then inherited
|
||||
for (base in cls.bases) {
|
||||
if (visited.add(base)) addMembersOf(base, false)
|
||||
}
|
||||
}
|
||||
|
||||
visited.add(className)
|
||||
addMembersOf(className, true)
|
||||
if (DEBUG_COMPLETION) {
|
||||
log.info("[LYNG_DEBUG] offerMembers: direct=${directMap.size} inherited=${inheritedMap.size} for ${className}")
|
||||
}
|
||||
|
||||
// If the docs model lacks explicit bases for some core container classes,
|
||||
// conservatively supplement with preferred parents to expose common ops.
|
||||
fun supplementPreferredBases(receiver: String) {
|
||||
// Preference/known lineage map kept tiny and safe
|
||||
val extras = when (receiver) {
|
||||
"List" -> listOf("Collection", "Iterable")
|
||||
"Array" -> listOf("Collection", "Iterable")
|
||||
// In practice, many high-level ops users expect on iteration live on Iterable.
|
||||
// For editor assistance, expose Iterable ops for Iterator receivers too.
|
||||
"Iterator" -> listOf("Iterable")
|
||||
else -> emptyList()
|
||||
}
|
||||
for (base in extras) {
|
||||
if (visited.add(base)) addMembersOf(base, false)
|
||||
}
|
||||
}
|
||||
supplementPreferredBases(className)
|
||||
|
||||
fun emitGroup(map: LinkedHashMap<String, MutableList<MiniMemberDecl>>) {
|
||||
val keys = map.keys.sortedBy { it.lowercase() }
|
||||
for (name in keys) {
|
||||
val list = map[name] ?: continue
|
||||
// Choose a representative for display:
|
||||
// 1) Prefer a method with a known return type
|
||||
// 2) Else any method
|
||||
// 3) Else the first variant
|
||||
val rep =
|
||||
list.asSequence()
|
||||
.filterIsInstance<MiniMemberFunDecl>()
|
||||
.firstOrNull { it.returnType != null }
|
||||
?: list.firstOrNull { it is MiniMemberFunDecl }
|
||||
?: list.first()
|
||||
when (rep) {
|
||||
is MiniMemberFunDecl -> {
|
||||
val params = rep.params.joinToString(", ") { it.name }
|
||||
val ret = typeOf(rep.returnType)
|
||||
val extra = list.count { it is MiniMemberFunDecl } - 1
|
||||
val overloads = if (extra > 0) " (+$extra overloads)" else ""
|
||||
val tail = "(${params})$overloads"
|
||||
val icon = AllIcons.Nodes.Method
|
||||
val builder = LookupElementBuilder.create(name)
|
||||
.withIcon(icon)
|
||||
.withTailText(tail, true)
|
||||
.withTypeText(ret, true)
|
||||
.withInsertHandler(ParenInsertHandler)
|
||||
emit(builder)
|
||||
}
|
||||
is MiniMemberValDecl -> {
|
||||
val icon = if (rep.mutable) AllIcons.Nodes.Variable else AllIcons.Nodes.Field
|
||||
// Prefer a field variant with known type if available
|
||||
val chosen = list.asSequence()
|
||||
.filterIsInstance<MiniMemberValDecl>()
|
||||
.firstOrNull { it.type != null } ?: rep
|
||||
val builder = LookupElementBuilder.create(name)
|
||||
.withIcon(icon)
|
||||
.withTypeText(typeOf(chosen.type), true)
|
||||
emit(builder)
|
||||
}
|
||||
is MiniInitDecl -> {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Emit what we have first
|
||||
emitGroup(directMap)
|
||||
emitGroup(inheritedMap)
|
||||
|
||||
// If suggestions are suspiciously sparse for known container classes,
|
||||
// try to conservatively supplement using a curated list resolved via docs registry.
|
||||
val totalSuggested = directMap.size + inheritedMap.size
|
||||
val isContainer = className in setOf("Iterator", "Iterable", "Collection", "List", "Array")
|
||||
if (isContainer && totalSuggested < 3) {
|
||||
if (DEBUG_COMPLETION) log.info("[LYNG_DEBUG] Supplementing members for $className; had=$totalSuggested")
|
||||
val common = when (className) {
|
||||
"Iterator" -> listOf(
|
||||
"hasNext", "next", "forEach", "map", "filter", "take", "drop", "toList", "count", "any", "all"
|
||||
)
|
||||
else -> listOf(
|
||||
// Iterable/Collection/List/Array common ops
|
||||
"size", "isEmpty", "map", "flatMap", "filter", "first", "last", "contains",
|
||||
"any", "all", "count", "forEach", "toList", "toSet"
|
||||
)
|
||||
}
|
||||
val already = (directMap.keys + inheritedMap.keys).toMutableSet()
|
||||
for (name in common) {
|
||||
if (name in already) continue
|
||||
// Try resolve across classes first to get types/params; if it fails, emit a synthetic safe suggestion.
|
||||
val resolved = DocLookupUtils.findMemberAcrossClasses(imported, name)
|
||||
if (resolved != null) {
|
||||
val member = resolved.second
|
||||
when (member) {
|
||||
is MiniMemberFunDecl -> {
|
||||
val params = member.params.joinToString(", ") { it.name }
|
||||
val ret = typeOf(member.returnType)
|
||||
val builder = LookupElementBuilder.create(name)
|
||||
.withIcon(AllIcons.Nodes.Method)
|
||||
.withTailText("(${params})", true)
|
||||
.withTypeText(ret, true)
|
||||
.withInsertHandler(ParenInsertHandler)
|
||||
emit(builder)
|
||||
already.add(name)
|
||||
}
|
||||
is MiniMemberValDecl -> {
|
||||
val builder = LookupElementBuilder.create(name)
|
||||
.withIcon(AllIcons.Nodes.Field)
|
||||
.withTypeText(typeOf(member.type), true)
|
||||
emit(builder)
|
||||
already.add(name)
|
||||
}
|
||||
is MiniInitDecl -> {}
|
||||
}
|
||||
} else {
|
||||
// Synthetic fallback: method without detailed params/types to improve UX in absence of docs
|
||||
val isProperty = name in setOf("size", "length")
|
||||
val builder = if (isProperty) {
|
||||
LookupElementBuilder.create(name)
|
||||
.withIcon(AllIcons.Nodes.Field)
|
||||
} else {
|
||||
LookupElementBuilder.create(name)
|
||||
.withIcon(AllIcons.Nodes.Method)
|
||||
.withTailText("()", true)
|
||||
.withInsertHandler(ParenInsertHandler)
|
||||
}
|
||||
emit(builder)
|
||||
already.add(name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Supplement with stdlib extension members defined in root.lyng (e.g., fun String.trim(...))
|
||||
run {
|
||||
val already = (directMap.keys + inheritedMap.keys).toMutableSet()
|
||||
val ext = BuiltinDocRegistry.extensionMemberNamesFor(className)
|
||||
if (DEBUG_COMPLETION) log.info("[LYNG_DEBUG] Extensions for $className: count=${ext.size} -> ${ext}")
|
||||
for (name in ext) {
|
||||
if (already.contains(name)) continue
|
||||
// Try to resolve full signature via registry first to get params and return type
|
||||
val resolved = DocLookupUtils.resolveMemberWithInheritance(imported, className, name)
|
||||
if (resolved != null) {
|
||||
when (val member = resolved.second) {
|
||||
is MiniMemberFunDecl -> {
|
||||
val params = member.params.joinToString(", ") { it.name }
|
||||
val ret = typeOf(member.returnType)
|
||||
val builder = LookupElementBuilder.create(name)
|
||||
.withIcon(AllIcons.Nodes.Method)
|
||||
.withTailText("(${params})", true)
|
||||
.withTypeText(ret, true)
|
||||
.withInsertHandler(ParenInsertHandler)
|
||||
emit(builder)
|
||||
already.add(name)
|
||||
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
|
||||
val builder = LookupElementBuilder.create(name)
|
||||
.withIcon(AllIcons.Nodes.Method)
|
||||
.withTailText("()", true)
|
||||
.withInsertHandler(ParenInsertHandler)
|
||||
emit(builder)
|
||||
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 {
|
||||
return when (t) {
|
||||
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}"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Minimal fallback docs seeding for `lyng.io.fs` used only inside the IDEA plugin
|
||||
* when external docs module (lyngio) is not present on the classpath.
|
||||
*
|
||||
* We keep it tiny and plugin-local to avoid coupling core library to external packages.
|
||||
*/
|
||||
package net.sergeych.lyng.idea.docs
|
||||
|
||||
import net.sergeych.lyng.miniast.BuiltinDocRegistry
|
||||
import net.sergeych.lyng.miniast.ParamDoc
|
||||
import net.sergeych.lyng.miniast.TypeGenericDoc
|
||||
import net.sergeych.lyng.miniast.type
|
||||
|
||||
internal object FsDocsFallback {
|
||||
@Volatile
|
||||
private var seeded = false
|
||||
|
||||
fun ensureOnce(): Boolean {
|
||||
if (seeded) return true
|
||||
synchronized(this) {
|
||||
if (seeded) return true
|
||||
BuiltinDocRegistry.module("lyng.io.fs") {
|
||||
// Class Path summary and a few commonly used methods
|
||||
classDoc(name = "Path", doc = "Filesystem path class. Construct with a string: `Path(\"/tmp\")`.") {
|
||||
method(name = "exists", doc = "Whether the path exists on the filesystem.", returns = type("lyng.Bool"))
|
||||
method(name = "isFile", doc = "Whether the path exists and is a file.", returns = type("lyng.Bool"))
|
||||
method(name = "isDir", doc = "Whether the path exists and is a directory.", returns = type("lyng.Bool"))
|
||||
method(name = "readUtf8", doc = "Read the entire file as UTF-8 string.", returns = type("lyng.String"))
|
||||
method(
|
||||
name = "writeUtf8",
|
||||
doc = "Write UTF-8 string to the file (overwrite).",
|
||||
params = listOf(ParamDoc("text", type("lyng.String")))
|
||||
)
|
||||
method(
|
||||
name = "bytes",
|
||||
doc = "Iterate file content as `Buffer` chunks.",
|
||||
params = listOf(ParamDoc("size", type("lyng.Int"))),
|
||||
returns = TypeGenericDoc(type("lyng.Iterator"), listOf(type("lyng.Buffer")))
|
||||
)
|
||||
method(
|
||||
name = "lines",
|
||||
doc = "Iterate file as lines of text.",
|
||||
returns = TypeGenericDoc(type("lyng.Iterator"), listOf(type("lyng.String")))
|
||||
)
|
||||
}
|
||||
|
||||
// Top-level exported constants
|
||||
valDoc(name = "Path", doc = "Filesystem path class. Construct with a string: `Path(\"/tmp\")`.", type = type("Path"))
|
||||
valDoc(name = "Paths", doc = "Alias of `Path` for those who prefer plural form.", type = type("Path"))
|
||||
}
|
||||
seeded = true
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
+318
-84
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
|
||||
* 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.
|
||||
@@ -26,10 +26,12 @@ import com.intellij.psi.PsiElement
|
||||
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.idea.LyngLanguage
|
||||
import net.sergeych.lyng.idea.util.IdeLenientImportProvider
|
||||
import net.sergeych.lyng.idea.util.TextCtx
|
||||
import net.sergeych.lyng.miniast.*
|
||||
|
||||
/**
|
||||
@@ -41,7 +43,17 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
|
||||
private val log = Logger.getInstance(LyngDocumentationProvider::class.java)
|
||||
// Toggle to trace inheritance-based resolutions in Quick Docs. Keep false for normal use.
|
||||
private val DEBUG_INHERITANCE = false
|
||||
// Global Quick Doc debug toggle (OFF by default). When false, [LYNG_DEBUG] logs are suppressed.
|
||||
private val DEBUG_LOG = false
|
||||
override fun generateDoc(element: PsiElement?, originalElement: PsiElement?): String? {
|
||||
// Try load external docs registrars (e.g., lyngio) if present on classpath
|
||||
ensureExternalDocsRegistered()
|
||||
// Ensure stdlib Obj*-defined docs (e.g., String methods via ObjString.addFnDoc) are initialized
|
||||
try {
|
||||
net.sergeych.lyng.miniast.StdlibDocsBootstrap.ensure()
|
||||
} catch (_: Throwable) {
|
||||
// best-effort; absence must not break Quick Doc
|
||||
}
|
||||
if (element == null) return null
|
||||
val file: PsiFile = element.containingFile ?: return null
|
||||
val document: Document = file.viewProvider.document ?: return null
|
||||
@@ -49,61 +61,254 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
|
||||
|
||||
// Determine caret/lookup offset from the element range
|
||||
val offset = originalElement?.textRange?.startOffset ?: element.textRange.startOffset
|
||||
val idRange = wordRangeAt(text, offset) ?: run {
|
||||
log.info("[LYNG_DEBUG] QuickDoc: no word at offset=$offset in ${file.name}")
|
||||
val idRange = TextCtx.wordRangeAt(text, offset) ?: run {
|
||||
if (DEBUG_LOG) log.info("[LYNG_DEBUG] QuickDoc: no word at offset=$offset in ${file.name}")
|
||||
return null
|
||||
}
|
||||
if (idRange.isEmpty) return null
|
||||
val ident = text.substring(idRange.startOffset, idRange.endOffset)
|
||||
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}")
|
||||
|
||||
// Build MiniAst for this file (fast and resilient). Best-effort; on failure return null.
|
||||
// Build MiniAst for this file (fast and resilient). Best-effort; on failure continue with partial AST.
|
||||
val sink = MiniAstBuilder()
|
||||
// Use lenient import provider so unresolved imports (e.g., lyng.io.fs) don't break docs
|
||||
val provider = IdeLenientImportProvider.create()
|
||||
try {
|
||||
val src = Source("<ide>", text)
|
||||
val src = Source("<ide>", text)
|
||||
val mini = try {
|
||||
runBlocking { Compiler.compileWithMini(src, provider, sink) }
|
||||
sink.build()
|
||||
} catch (t: Throwable) {
|
||||
log.warn("[LYNG_DEBUG] QuickDoc: compileWithMini failed: ${t.message}")
|
||||
return null
|
||||
}
|
||||
val mini = sink.build() ?: return null
|
||||
val source = Source("<ide>", text)
|
||||
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
|
||||
// 1) Check declarations whose name range contains offset
|
||||
for (d in mini.declarations) {
|
||||
val s = source.offsetOf(d.nameStart)
|
||||
val e = (s + d.name.length).coerceAtMost(text.length)
|
||||
if (offset in s until e) {
|
||||
log.info("[LYNG_DEBUG] QuickDoc: matched decl '${d.name}' kind=${d::class.simpleName}")
|
||||
return renderDeclDoc(d)
|
||||
// 1) Use unified declaration detection
|
||||
DocLookupUtils.findDeclarationAt(mini, offset, ident)?.let { (name, kind) ->
|
||||
if (DEBUG_LOG) log.info("[LYNG_DEBUG] QuickDoc: matched declaration '$name' kind=$kind")
|
||||
// Find the actual declaration object to render
|
||||
for (d in mini.declarations) {
|
||||
if (d.name == name && source.offsetOf(d.nameStart) <= offset && source.offsetOf(d.nameStart) + d.name.length > offset) {
|
||||
return renderDeclDoc(d)
|
||||
}
|
||||
// Handle members if it was a member
|
||||
if (d is MiniClassDecl) {
|
||||
for (m in d.members) {
|
||||
if (m.name == name && source.offsetOf(m.nameStart) <= offset && source.offsetOf(m.nameStart) + m.name.length > offset) {
|
||||
return when (m) {
|
||||
is MiniMemberFunDecl -> renderMemberFunDoc(d.name, m)
|
||||
is MiniMemberValDecl -> renderMemberValDoc(d.name, m)
|
||||
is MiniInitDecl -> null
|
||||
}
|
||||
}
|
||||
}
|
||||
for (cf in d.ctorFields) {
|
||||
if (cf.name == name && source.offsetOf(cf.nameStart) <= offset && source.offsetOf(cf.nameStart) + cf.name.length > offset) {
|
||||
// Render as a member val
|
||||
val mv = MiniMemberValDecl(
|
||||
range = MiniRange(cf.nameStart, cf.nameStart), // dummy
|
||||
name = cf.name,
|
||||
mutable = cf.mutable,
|
||||
type = cf.type,
|
||||
doc = null,
|
||||
nameStart = cf.nameStart
|
||||
)
|
||||
return renderMemberValDoc(d.name, mv)
|
||||
}
|
||||
}
|
||||
for (cf in d.classFields) {
|
||||
if (cf.name == name && source.offsetOf(cf.nameStart) <= offset && source.offsetOf(cf.nameStart) + cf.name.length > offset) {
|
||||
// Render as a member val
|
||||
val mv = MiniMemberValDecl(
|
||||
range = MiniRange(cf.nameStart, cf.nameStart), // dummy
|
||||
name = cf.name,
|
||||
mutable = cf.mutable,
|
||||
type = cf.type,
|
||||
doc = null,
|
||||
nameStart = cf.nameStart
|
||||
)
|
||||
return renderMemberValDoc(d.name, mv)
|
||||
}
|
||||
}
|
||||
}
|
||||
if (d is MiniEnumDecl) {
|
||||
if (d.entries.contains(name) && offset >= source.offsetOf(d.range.start) && offset <= source.offsetOf(d.range.end)) {
|
||||
// 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>"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// 2) Check parameters of functions
|
||||
for (fn in mini.declarations.filterIsInstance<MiniFunDecl>()) {
|
||||
for (p in fn.params) {
|
||||
val s = source.offsetOf(p.nameStart)
|
||||
val e = (s + p.name.length).coerceAtMost(text.length)
|
||||
if (offset in s until e) {
|
||||
log.info("[LYNG_DEBUG] QuickDoc: matched param '${p.name}' in fun '${fn.name}'")
|
||||
return renderParamDoc(fn, p)
|
||||
// Check parameters
|
||||
for (fn in mini.declarations.filterIsInstance<MiniFunDecl>()) {
|
||||
for (p in fn.params) {
|
||||
if (p.name == name && source.offsetOf(p.nameStart) <= offset && source.offsetOf(p.nameStart) + p.name.length > offset) {
|
||||
return renderParamDoc(fn, p)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// 3) As a fallback, if the caret is on an identifier text that matches any declaration name, show that
|
||||
|
||||
// 3) usages in current file via Binder (resolves local variables, parameters, and classes)
|
||||
try {
|
||||
val binding = net.sergeych.lyng.binding.Binder.bind(text, mini)
|
||||
val ref = binding.references.firstOrNull { offset in it.start until it.end }
|
||||
if (ref != null) {
|
||||
val sym = binding.symbols.firstOrNull { it.id == ref.symbolId }
|
||||
if (sym != null) {
|
||||
// Find local declaration that matches this symbol
|
||||
val ds = mini.declarations.firstOrNull { decl ->
|
||||
val s = source.offsetOf(decl.nameStart)
|
||||
decl.name == sym.name && s == sym.declStart
|
||||
}
|
||||
if (ds != null) return renderDeclDoc(ds)
|
||||
|
||||
// Check parameters
|
||||
for (fn in mini.declarations.filterIsInstance<MiniFunDecl>()) {
|
||||
for (p in fn.params) {
|
||||
val s = source.offsetOf(p.nameStart)
|
||||
if (p.name == sym.name && s == sym.declStart) {
|
||||
return renderParamDoc(fn, p)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check class members (fields/functions)
|
||||
for (cls in mini.declarations.filterIsInstance<MiniClassDecl>()) {
|
||||
for (m in cls.members) {
|
||||
val s = source.offsetOf(m.nameStart)
|
||||
if (m.name == sym.name && s == sym.declStart) {
|
||||
return when (m) {
|
||||
is MiniMemberFunDecl -> renderMemberFunDoc(cls.name, m)
|
||||
is MiniMemberValDecl -> renderMemberValDoc(cls.name, m)
|
||||
is MiniInitDecl -> null
|
||||
}
|
||||
}
|
||||
}
|
||||
for (cf in cls.ctorFields) {
|
||||
val s = source.offsetOf(cf.nameStart)
|
||||
if (cf.name == sym.name && s == sym.declStart) {
|
||||
// Render as a member val
|
||||
val mv = MiniMemberValDecl(
|
||||
range = MiniRange(cf.nameStart, cf.nameStart), // dummy
|
||||
name = cf.name,
|
||||
mutable = cf.mutable,
|
||||
type = cf.type,
|
||||
doc = null,
|
||||
nameStart = cf.nameStart
|
||||
)
|
||||
return renderMemberValDoc(cls.name, mv)
|
||||
}
|
||||
}
|
||||
for (cf in cls.classFields) {
|
||||
val s = source.offsetOf(cf.nameStart)
|
||||
if (cf.name == sym.name && s == sym.declStart) {
|
||||
// Render as a member val
|
||||
val mv = MiniMemberValDecl(
|
||||
range = MiniRange(cf.nameStart, cf.nameStart), // dummy
|
||||
name = cf.name,
|
||||
mutable = cf.mutable,
|
||||
type = cf.type,
|
||||
doc = null,
|
||||
nameStart = cf.nameStart
|
||||
)
|
||||
return renderMemberValDoc(cls.name, mv)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e: Throwable) {
|
||||
if (DEBUG_LOG) log.warn("[LYNG_DEBUG] QuickDoc: local binder resolution failed: ${e.message}")
|
||||
}
|
||||
// 4) Member-context resolution first (dot immediately before identifier): handle literals and calls
|
||||
run {
|
||||
val dotPos = TextCtx.findDotLeft(text, idRange.startOffset)
|
||||
?: TextCtx.findDotLeft(text, offset)
|
||||
if (dotPos != null) {
|
||||
// Build imported modules (MiniAst-derived if available, else lenient from text) and ensure stdlib is present
|
||||
val importedModules = DocLookupUtils.canonicalImportedModules(mini, text)
|
||||
|
||||
// Try literal and call-based receiver inference around the dot
|
||||
val i = TextCtx.prevNonWs(text, dotPos - 1)
|
||||
val className: String? = when {
|
||||
i >= 0 && text[i] == '"' -> "String"
|
||||
i >= 0 && text[i] == ']' -> "List"
|
||||
i >= 0 && text[i] == '}' -> "Dict"
|
||||
i >= 0 && text[i] == ')' -> {
|
||||
// Parenthesized expression: walk back to matching '(' and inspect the inner expression
|
||||
var j = i - 1
|
||||
var depth = 0
|
||||
while (j >= 0) {
|
||||
when (text[j]) {
|
||||
')' -> depth++
|
||||
'(' -> if (depth == 0) break else depth--
|
||||
}
|
||||
j--
|
||||
}
|
||||
if (j >= 0 && text[j] == '(') {
|
||||
val innerS = (j + 1).coerceAtLeast(0)
|
||||
val innerE = i.coerceAtMost(text.length)
|
||||
if (innerS < innerE) {
|
||||
val inner = text.substring(innerS, innerE).trim()
|
||||
when {
|
||||
inner.startsWith('"') && inner.endsWith('"') -> "String"
|
||||
inner.startsWith('[') && inner.endsWith(']') -> "List"
|
||||
inner.startsWith('{') && inner.endsWith('}') -> "Dict"
|
||||
else -> null
|
||||
}
|
||||
} else null
|
||||
} else null
|
||||
}
|
||||
else -> {
|
||||
val guessed = DocLookupUtils.guessClassFromCallBefore(text, dotPos, importedModules)
|
||||
if (guessed != null) guessed
|
||||
else {
|
||||
// handle this@Type or as Type
|
||||
val i2 = TextCtx.prevNonWs(text, dotPos - 1)
|
||||
if (i2 >= 0) {
|
||||
val identRange = TextCtx.wordRangeAt(text, i2 + 1)
|
||||
if (identRange != null) {
|
||||
val id = text.substring(identRange.startOffset, identRange.endOffset)
|
||||
val k = TextCtx.prevNonWs(text, identRange.startOffset - 1)
|
||||
if (k >= 1 && text[k] == 's' && text[k-1] == 'a' && (k-1 == 0 || !text[k-2].isLetterOrDigit())) {
|
||||
id
|
||||
} else if (k >= 0 && text[k] == '@') {
|
||||
val k2 = TextCtx.prevNonWs(text, k - 1)
|
||||
if (k2 >= 3 && text.substring(k2 - 3, k2 + 1) == "this") id else null
|
||||
} else null
|
||||
} else null
|
||||
} else null
|
||||
}
|
||||
}
|
||||
}
|
||||
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) {
|
||||
DocLookupUtils.resolveMemberWithInheritance(importedModules, className, ident)?.let { (owner, member) ->
|
||||
if (DEBUG_INHERITANCE) log.info("[LYNG_DEBUG] QuickDoc: literal/call '$ident' resolved to $owner.${member.name}")
|
||||
return when (member) {
|
||||
is MiniMemberFunDecl -> renderMemberFunDoc(owner, member)
|
||||
is MiniMemberValDecl -> renderMemberValDoc(owner, member)
|
||||
is MiniInitDecl -> null
|
||||
}
|
||||
}
|
||||
log.info("[LYNG_DEBUG] QuickDoc: resolve failed for ${className}.${ident}")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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 {
|
||||
log.info("[LYNG_DEBUG] QuickDoc: fallback by name '${it.name}' kind=${it::class.simpleName}")
|
||||
return renderDeclDoc(it)
|
||||
}
|
||||
|
||||
// 4) Consult BuiltinDocRegistry for imported modules (top-level and class members)
|
||||
var importedModules = mini.imports.map { it.segments.joinToString(".") { s -> s.name } }
|
||||
// Core-module fallback: in scratch/repl-like files without imports, consult stdlib by default
|
||||
if (importedModules.isEmpty()) importedModules = listOf("lyng.stdlib")
|
||||
// Canonicalize import names using ImportManager, as users may write shortened names (e.g., "io.fs")
|
||||
var importedModules = DocLookupUtils.canonicalImportedModules(mini, text)
|
||||
// Always include stdlib as a fallback context
|
||||
if (!importedModules.contains("lyng.stdlib")) importedModules = importedModules + "lyng.stdlib"
|
||||
// 4a) try top-level decls
|
||||
for (mod in importedModules) {
|
||||
importedModules.forEach { mod ->
|
||||
val docs = BuiltinDocRegistry.docsForModule(mod)
|
||||
val matches = docs.filterIsInstance<MiniFunDecl>().filter { it.name == ident }
|
||||
if (matches.isNotEmpty()) {
|
||||
@@ -118,18 +323,28 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
|
||||
}
|
||||
// Also allow values/consts
|
||||
docs.filterIsInstance<MiniValDecl>().firstOrNull { it.name == ident }?.let { return renderDeclDoc(it) }
|
||||
// And classes
|
||||
// And classes/enums
|
||||
docs.filterIsInstance<MiniClassDecl>().firstOrNull { it.name == ident }?.let { return renderDeclDoc(it) }
|
||||
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
|
||||
if (ident == "println" || ident == "print") {
|
||||
val fallback = if (ident == "println")
|
||||
"Print values to the standard output and append a newline. Accepts any number of arguments." else
|
||||
"Print values to the standard output without a trailing newline. Accepts any number of arguments."
|
||||
val title = "function $ident(values)"
|
||||
return "<div class='doc-title'>${htmlEscape(title)}</div>" + styledMarkdown(htmlEscape(fallback))
|
||||
}
|
||||
// 4b) try class members like ClassName.member with inheritance fallback
|
||||
val lhs = previousWordBefore(text, idRange.startOffset)
|
||||
if (lhs != null && hasDotBetween(text, lhs.endOffset, idRange.startOffset)) {
|
||||
val className = text.substring(lhs.startOffset, lhs.endOffset)
|
||||
resolveMemberWithInheritance(importedModules, className, ident)?.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}")
|
||||
return when (member) {
|
||||
is MiniMemberFunDecl -> renderMemberFunDoc(owner, member)
|
||||
is MiniMemberValDecl -> renderMemberValDoc(owner, member)
|
||||
is MiniInitDecl -> null
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@@ -140,33 +355,89 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
|
||||
if (dotPos != null) {
|
||||
val guessed = when {
|
||||
looksLikeListLiteralBefore(text, dotPos) -> "List"
|
||||
else -> null
|
||||
else -> DocLookupUtils.guessClassFromCallBefore(text, dotPos, importedModules)
|
||||
}
|
||||
if (guessed != null) {
|
||||
resolveMemberWithInheritance(importedModules, guessed, ident)?.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}")
|
||||
return when (member) {
|
||||
is MiniMemberFunDecl -> renderMemberFunDoc(owner, member)
|
||||
is MiniMemberValDecl -> renderMemberValDoc(owner, member)
|
||||
is MiniInitDecl -> null
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Extra fallback: try a small set of known receiver classes (covers literals when guess failed)
|
||||
run {
|
||||
val candidates = listOf("String", "Iterable", "Iterator", "List", "Collection", "Array", "Dict", "Regex")
|
||||
for (c in candidates) {
|
||||
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}")
|
||||
return when (member) {
|
||||
is MiniMemberFunDecl -> renderMemberFunDoc(owner, member)
|
||||
is MiniMemberValDecl -> renderMemberValDoc(owner, member)
|
||||
is MiniInitDecl -> null
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// As a last resort try aggregated String members (extensions from stdlib text)
|
||||
run {
|
||||
val classes = DocLookupUtils.aggregateClasses(importedModules)
|
||||
val stringCls = classes["String"]
|
||||
val m = stringCls?.members?.firstOrNull { it.name == ident }
|
||||
if (m != null) {
|
||||
if (DEBUG_INHERITANCE) log.info("[LYNG_DEBUG] Aggregated fallback resolved String.$ident")
|
||||
return when (m) {
|
||||
is MiniMemberFunDecl -> renderMemberFunDoc("String", m)
|
||||
is MiniMemberValDecl -> renderMemberValDoc("String", m)
|
||||
is MiniInitDecl -> null
|
||||
}
|
||||
}
|
||||
}
|
||||
// Search across classes; prefer Iterable, then Iterator, then List for common ops
|
||||
findMemberAcrossClasses(importedModules, ident)?.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}")
|
||||
return when (member) {
|
||||
is MiniMemberFunDecl -> renderMemberFunDoc(owner, member)
|
||||
is MiniMemberValDecl -> renderMemberValDoc(owner, member)
|
||||
is MiniInitDecl -> null
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
log.info("[LYNG_DEBUG] QuickDoc: nothing found for ident='$ident'")
|
||||
if (DEBUG_LOG) log.info("[LYNG_DEBUG] QuickDoc: nothing found for ident='$ident'")
|
||||
return null
|
||||
}
|
||||
|
||||
private val externalDocsLoaded: Boolean by lazy { tryLoadExternalDocs() }
|
||||
|
||||
private fun ensureExternalDocsRegistered() { @Suppress("UNUSED_EXPRESSION") externalDocsLoaded }
|
||||
|
||||
private fun tryLoadExternalDocs(): Boolean {
|
||||
return try {
|
||||
// Try known registrars; ignore failures if module is absent
|
||||
val cls = Class.forName("net.sergeych.lyngio.docs.FsBuiltinDocs")
|
||||
val m = cls.getMethod("ensure")
|
||||
m.invoke(null)
|
||||
log.info("[LYNG_DEBUG] QuickDoc: external docs loaded: net.sergeych.lyngio.docs.FsBuiltinDocs.ensure() OK")
|
||||
true
|
||||
} catch (_: Throwable) {
|
||||
// Seed a minimal plugin-local fallback so Path docs still work without lyngio
|
||||
val seeded = try {
|
||||
FsDocsFallback.ensureOnce()
|
||||
} catch (_: Throwable) { false }
|
||||
if (seeded) {
|
||||
log.info("[LYNG_DEBUG] QuickDoc: external docs NOT found; seeded plugin fallback for lyng.io.fs")
|
||||
} else {
|
||||
log.info("[LYNG_DEBUG] QuickDoc: external docs NOT found (lyngio absent on classpath)")
|
||||
}
|
||||
seeded
|
||||
}
|
||||
}
|
||||
|
||||
override fun getCustomDocumentationElement(
|
||||
editor: Editor,
|
||||
file: PsiFile,
|
||||
@@ -182,15 +453,15 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
|
||||
val title = when (d) {
|
||||
is MiniFunDecl -> "function ${d.name}${signatureOf(d)}"
|
||||
is MiniClassDecl -> "class ${d.name}"
|
||||
is MiniEnumDecl -> "enum ${d.name} { ${d.entries.joinToString(", ")} }"
|
||||
is MiniValDecl -> if (d.mutable) "var ${d.name}${typeOf(d.type)}" else "val ${d.name}${typeOf(d.type)}"
|
||||
else -> d.name
|
||||
}
|
||||
// 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()
|
||||
sb.append("<div class='doc-title'>").append(htmlEscape(title)).append("</div>")
|
||||
if (!doc.isNullOrBlank()) sb.append(styledMarkdown(doc!!))
|
||||
if (!doc.isNullOrBlank()) sb.append(styledMarkdown(doc))
|
||||
return sb.toString()
|
||||
}
|
||||
|
||||
@@ -211,7 +482,7 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
|
||||
val doc: String? = if (raw.isNullOrBlank()) null else MarkdownRenderer.render(raw)
|
||||
val sb = StringBuilder()
|
||||
sb.append("<div class='doc-title'>").append(htmlEscape(title)).append("</div>")
|
||||
if (!doc.isNullOrBlank()) sb.append(styledMarkdown(doc!!))
|
||||
if (!doc.isNullOrBlank()) sb.append(styledMarkdown(doc))
|
||||
return sb.toString()
|
||||
}
|
||||
|
||||
@@ -224,7 +495,7 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
|
||||
val doc: String? = if (raw.isNullOrBlank()) null else MarkdownRenderer.render(raw)
|
||||
val sb = StringBuilder()
|
||||
sb.append("<div class='doc-title'>").append(htmlEscape(title)).append("</div>")
|
||||
if (!doc.isNullOrBlank()) sb.append(styledMarkdown(doc!!))
|
||||
if (!doc.isNullOrBlank()) sb.append(styledMarkdown(doc))
|
||||
return sb.toString()
|
||||
}
|
||||
|
||||
@@ -362,46 +633,7 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
|
||||
|
||||
// --- Helpers for inheritance-aware and heuristic member lookup ---
|
||||
|
||||
private fun aggregateClasses(importedModules: List<String>): Map<String, MiniClassDecl> {
|
||||
val map = LinkedHashMap<String, MiniClassDecl>()
|
||||
for (mod in importedModules) {
|
||||
val docs = BuiltinDocRegistry.docsForModule(mod)
|
||||
docs.filterIsInstance<MiniClassDecl>().forEach { cls ->
|
||||
// Prefer the first occurrence; allow later duplicates to be ignored
|
||||
map.putIfAbsent(cls.name, cls)
|
||||
}
|
||||
}
|
||||
return map
|
||||
}
|
||||
|
||||
private fun resolveMemberWithInheritance(importedModules: List<String>, className: String, member: String): Pair<String, MiniMemberDecl>? {
|
||||
val classes = aggregateClasses(importedModules)
|
||||
fun dfs(name: String, visited: MutableSet<String>): Pair<String, MiniMemberDecl>? {
|
||||
val cls = classes[name] ?: return null
|
||||
cls.members.firstOrNull { it.name == member }?.let { return name to it }
|
||||
if (!visited.add(name)) return null
|
||||
for (baseName in cls.bases) {
|
||||
dfs(baseName, visited)?.let { return it }
|
||||
}
|
||||
return null
|
||||
}
|
||||
return dfs(className, mutableSetOf())
|
||||
}
|
||||
|
||||
private fun findMemberAcrossClasses(importedModules: List<String>, member: String): Pair<String, MiniMemberDecl>? {
|
||||
val classes = aggregateClasses(importedModules)
|
||||
// Preferred order for ambiguous common ops
|
||||
val preference = listOf("Iterable", "Iterator", "List")
|
||||
// First, try preference order
|
||||
for (name in preference) {
|
||||
resolveMemberWithInheritance(importedModules, name, member)?.let { return it }
|
||||
}
|
||||
// Then, scan all
|
||||
for ((name, cls) in classes) {
|
||||
cls.members.firstOrNull { it.name == member }?.let { return name to it }
|
||||
}
|
||||
return null
|
||||
}
|
||||
// Removed: member/class resolution helpers moved to lynglib DocLookupUtils for reuse
|
||||
|
||||
private fun findDotLeft(text: String, rightStart: Int): Int? {
|
||||
var i = (rightStart - 1).coerceAtLeast(0)
|
||||
@@ -433,4 +665,6 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Removed: guessClassFromCallBefore moved to DocLookupUtils
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
|
||||
* 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.
|
||||
@@ -35,6 +35,8 @@ import com.intellij.psi.codeStyle.CodeStyleManager
|
||||
import net.sergeych.lyng.format.LyngFormatConfig
|
||||
import net.sergeych.lyng.format.LyngFormatter
|
||||
import net.sergeych.lyng.idea.LyngLanguage
|
||||
import net.sergeych.lyng.idea.util.FormattingUtils.computeDesiredIndent
|
||||
import net.sergeych.lyng.idea.util.FormattingUtils.findFirstNonWs
|
||||
|
||||
class LyngEnterHandler : EnterHandlerDelegate {
|
||||
private val log = Logger.getInstance(LyngEnterHandler::class.java)
|
||||
@@ -80,10 +82,22 @@ class LyngEnterHandler : EnterHandlerDelegate {
|
||||
val trimmed = prevText.trimStart()
|
||||
// consider only code part before // comment
|
||||
val code = trimmed.substringBefore("//").trim()
|
||||
if (code == "}") {
|
||||
// Previously we reindented the enclosed block on Enter after a lone '}'.
|
||||
// Per new behavior, this action is now bound to typing '}' instead.
|
||||
// Keep Enter flow limited to indenting the new line only.
|
||||
if (code == "}" || code == "*/") {
|
||||
// Adjust indent for the previous line if it's a block or comment closer
|
||||
val prevStart = doc.getLineStartOffset(prevLine)
|
||||
CodeStyleManager.getInstance(project).adjustLineIndent(file, prevStart)
|
||||
|
||||
// Fallback for previous line: manual application
|
||||
val desiredPrev = computeDesiredIndent(project, doc, prevLine)
|
||||
val lineStartPrev = doc.getLineStartOffset(prevLine)
|
||||
val lineEndPrev = doc.getLineEndOffset(prevLine)
|
||||
val firstNonWsPrev = findFirstNonWs(doc, lineStartPrev, lineEndPrev)
|
||||
val currentIndentLenPrev = firstNonWsPrev - lineStartPrev
|
||||
if (doc.getText(TextRange(lineStartPrev, lineStartPrev + currentIndentLenPrev)) != desiredPrev) {
|
||||
WriteCommandAction.runWriteCommandAction(project) {
|
||||
doc.replaceString(lineStartPrev, lineStartPrev + currentIndentLenPrev, desiredPrev)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Adjust indent for the current (new) line
|
||||
@@ -159,35 +173,6 @@ class LyngEnterHandler : EnterHandlerDelegate {
|
||||
caret.moveToOffset(target)
|
||||
}
|
||||
|
||||
private fun computeDesiredIndent(project: Project, doc: Document, line: Int): String {
|
||||
val options = CodeStyle.getIndentOptions(project, doc)
|
||||
val start = 0
|
||||
val end = doc.getLineEndOffset(line)
|
||||
val snippet = doc.getText(TextRange(start, end))
|
||||
val isBlankLine = doc.getLineText(line).trim().isEmpty()
|
||||
val snippetForCalc = if (isBlankLine) snippet + "x" else snippet
|
||||
val cfg = LyngFormatConfig(
|
||||
indentSize = options.INDENT_SIZE.coerceAtLeast(1),
|
||||
useTabs = options.USE_TAB_CHARACTER,
|
||||
continuationIndentSize = options.CONTINUATION_INDENT_SIZE.coerceAtLeast(options.INDENT_SIZE.coerceAtLeast(1)),
|
||||
)
|
||||
val formatted = LyngFormatter.reindent(snippetForCalc, cfg)
|
||||
val lastNl = formatted.lastIndexOf('\n')
|
||||
val lastLine = if (lastNl >= 0) formatted.substring(lastNl + 1) else formatted
|
||||
val wsLen = lastLine.indexOfFirst { it != ' ' && it != '\t' }.let { if (it < 0) lastLine.length else it }
|
||||
return lastLine.substring(0, wsLen)
|
||||
}
|
||||
|
||||
private fun findFirstNonWs(doc: Document, start: Int, end: Int): Int {
|
||||
var i = start
|
||||
val text = doc.charsSequence
|
||||
while (i < end) {
|
||||
val ch = text[i]
|
||||
if (ch != ' ' && ch != '\t') break
|
||||
i++
|
||||
}
|
||||
return i
|
||||
}
|
||||
|
||||
private fun Document.safeLineNumber(offset: Int): Int =
|
||||
getLineNumber(offset.coerceIn(0, textLength))
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
|
||||
* 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.
|
||||
@@ -32,31 +32,54 @@ import net.sergeych.lyng.format.LyngFormatConfig
|
||||
import net.sergeych.lyng.format.LyngFormatter
|
||||
import net.sergeych.lyng.idea.LyngLanguage
|
||||
import net.sergeych.lyng.idea.settings.LyngFormatterSettings
|
||||
import net.sergeych.lyng.idea.util.FormattingUtils.computeDesiredIndent
|
||||
import net.sergeych.lyng.idea.util.FormattingUtils.findFirstNonWs
|
||||
|
||||
class LyngTypedHandler : TypedHandlerDelegate() {
|
||||
private val log = Logger.getInstance(LyngTypedHandler::class.java)
|
||||
|
||||
override fun charTyped(c: Char, project: Project, editor: Editor, file: PsiFile): Result {
|
||||
if (file.language != LyngLanguage) return Result.CONTINUE
|
||||
if (c != '}') return Result.CONTINUE
|
||||
|
||||
if (c == '}') {
|
||||
val doc = editor.document
|
||||
PsiDocumentManager.getInstance(project).commitDocument(doc)
|
||||
|
||||
val doc = editor.document
|
||||
PsiDocumentManager.getInstance(project).commitDocument(doc)
|
||||
val offset = editor.caretModel.offset
|
||||
val line = doc.getLineNumber((offset - 1).coerceAtLeast(0))
|
||||
if (line < 0) return Result.CONTINUE
|
||||
|
||||
val offset = editor.caretModel.offset
|
||||
val line = doc.getLineNumber((offset - 1).coerceAtLeast(0))
|
||||
if (line < 0) return Result.CONTINUE
|
||||
|
||||
val rawLine = doc.getLineText(line)
|
||||
val code = rawLine.substringBefore("//").trim()
|
||||
if (code == "}") {
|
||||
val settings = LyngFormatterSettings.getInstance(project)
|
||||
if (settings.reindentClosedBlockOnEnter) {
|
||||
reindentClosedBlockAroundBrace(project, file, doc, line)
|
||||
val rawLine = doc.getLineText(line)
|
||||
val code = rawLine.substringBefore("//").trim()
|
||||
if (code == "}") {
|
||||
val settings = LyngFormatterSettings.getInstance(project)
|
||||
if (settings.reindentClosedBlockOnEnter) {
|
||||
reindentClosedBlockAroundBrace(project, file, doc, line)
|
||||
}
|
||||
// After block reindent, adjust line indent to what platform thinks (no-op in many cases)
|
||||
val lineStart = doc.getLineStartOffset(line)
|
||||
CodeStyleManager.getInstance(project).adjustLineIndent(file, lineStart)
|
||||
}
|
||||
} else if (c == '/') {
|
||||
val doc = editor.document
|
||||
val offset = editor.caretModel.offset
|
||||
if (offset >= 2 && doc.getText(TextRange(offset - 2, offset)) == "*/") {
|
||||
PsiDocumentManager.getInstance(project).commitDocument(doc)
|
||||
val line = doc.getLineNumber(offset - 1)
|
||||
val lineStart = doc.getLineStartOffset(line)
|
||||
CodeStyleManager.getInstance(project).adjustLineIndent(file, lineStart)
|
||||
|
||||
// Manual application fallback
|
||||
val desired = computeDesiredIndent(project, doc, line)
|
||||
val lineEnd = doc.getLineEndOffset(line)
|
||||
val firstNonWs = findFirstNonWs(doc, lineStart, lineEnd)
|
||||
val currentIndentLen = firstNonWs - lineStart
|
||||
if (doc.getText(TextRange(lineStart, lineStart + currentIndentLen)) != desired) {
|
||||
WriteCommandAction.runWriteCommandAction(project) {
|
||||
doc.replaceString(lineStart, lineStart + currentIndentLen, desired)
|
||||
}
|
||||
}
|
||||
}
|
||||
// After block reindent, adjust line indent to what platform thinks (no-op in many cases)
|
||||
val lineStart = doc.getLineStartOffset(line)
|
||||
CodeStyleManager.getInstance(project).adjustLineIndent(file, lineStart)
|
||||
}
|
||||
return Result.CONTINUE
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
|
||||
* 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.
|
||||
@@ -24,9 +24,8 @@ import com.intellij.openapi.util.TextRange
|
||||
import com.intellij.psi.PsiDocumentManager
|
||||
import com.intellij.psi.codeStyle.CommonCodeStyleSettings.IndentOptions
|
||||
import com.intellij.psi.codeStyle.lineIndent.LineIndentProvider
|
||||
import net.sergeych.lyng.format.LyngFormatConfig
|
||||
import net.sergeych.lyng.format.LyngFormatter
|
||||
import net.sergeych.lyng.idea.LyngLanguage
|
||||
import net.sergeych.lyng.idea.util.FormattingUtils
|
||||
|
||||
/**
|
||||
* Lightweight indentation provider for Lyng.
|
||||
@@ -45,8 +44,7 @@ class LyngLineIndentProvider : LineIndentProvider {
|
||||
val options = CodeStyle.getIndentOptions(project, doc)
|
||||
|
||||
val line = doc.getLineNumberSafe(offset)
|
||||
val indent = computeDesiredIndentFromCore(doc, line, options)
|
||||
return indent
|
||||
return FormattingUtils.computeDesiredIndent(project, doc, line)
|
||||
}
|
||||
|
||||
override fun isSuitableFor(language: Language?): Boolean = language == null || language == LyngLanguage
|
||||
@@ -79,25 +77,4 @@ class LyngLineIndentProvider : LineIndentProvider {
|
||||
return spaces / size
|
||||
}
|
||||
|
||||
private fun computeDesiredIndentFromCore(doc: Document, line: Int, options: IndentOptions): String {
|
||||
// Build a minimal text consisting of all previous lines and the current line.
|
||||
// Special case: when the current line is blank (newly created by Enter), compute the
|
||||
// indent as if there was a non-whitespace character at line start (append a sentinel).
|
||||
val start = 0
|
||||
val end = doc.getLineEndOffset(line)
|
||||
val snippet = doc.getText(TextRange(start, end))
|
||||
val isBlankLine = doc.getLineText(line).trim().isEmpty()
|
||||
val snippetForCalc = if (isBlankLine) snippet + "x" else snippet
|
||||
val cfg = LyngFormatConfig(
|
||||
indentSize = options.INDENT_SIZE.coerceAtLeast(1),
|
||||
useTabs = options.USE_TAB_CHARACTER,
|
||||
continuationIndentSize = options.CONTINUATION_INDENT_SIZE.coerceAtLeast(options.INDENT_SIZE.coerceAtLeast(1)),
|
||||
)
|
||||
val formatted = LyngFormatter.reindent(snippetForCalc, cfg)
|
||||
// Grab the last line's leading whitespace as the indent for the current line
|
||||
val lastNl = formatted.lastIndexOf('\n')
|
||||
val lastLine = if (lastNl >= 0) formatted.substring(lastNl + 1) else formatted
|
||||
val wsLen = lastLine.indexOfFirst { it != ' ' && it != '\t' }.let { if (it < 0) lastLine.length else it }
|
||||
return lastLine.substring(0, wsLen)
|
||||
}
|
||||
}
|
||||
@@ -46,6 +46,27 @@ import net.sergeych.lyng.idea.spell.LyngSpellIndex
|
||||
class LyngGrazieAnnotator : ExternalAnnotator<LyngGrazieAnnotator.Input, LyngGrazieAnnotator.Result>(), DumbAware {
|
||||
private val log = Logger.getInstance(LyngGrazieAnnotator::class.java)
|
||||
|
||||
companion object {
|
||||
// Cache GrammarChecker availability to avoid repeated reflection + noisy logs
|
||||
@Volatile
|
||||
private var grammarCheckerAvailable: Boolean? = null
|
||||
|
||||
@Volatile
|
||||
private var grammarCheckerMissingLogged: Boolean = false
|
||||
|
||||
private fun isGrammarCheckerKnownMissing(): Boolean = (grammarCheckerAvailable == false)
|
||||
|
||||
private fun markGrammarCheckerMissingOnce(log: Logger, message: String) {
|
||||
if (!grammarCheckerMissingLogged) {
|
||||
// Downgrade to debug to reduce log noise across projects/sessions
|
||||
log.debug(message)
|
||||
grammarCheckerMissingLogged = true
|
||||
}
|
||||
}
|
||||
|
||||
private val RETRY_KEY: Key<Long> = Key.create("LYNG_GRAZIE_ANN_RETRY_STAMP")
|
||||
}
|
||||
|
||||
data class Input(val modStamp: Long)
|
||||
data class Finding(val range: TextRange, val message: String)
|
||||
data class Result(val modStamp: Long, val findings: List<Finding>)
|
||||
@@ -209,10 +230,14 @@ class LyngGrazieAnnotator : ExternalAnnotator<LyngGrazieAnnotator.Input, LyngGra
|
||||
|
||||
private fun runGrazieChecksWithTracing(file: PsiFile, content: TextContent): Pair<Collection<Any>?, String?> {
|
||||
// Try known entry points via reflection to avoid hard dependencies on Grazie internals
|
||||
if (isGrammarCheckerKnownMissing()) return null to null
|
||||
try {
|
||||
// 1) Static GrammarChecker.check(TextContent)
|
||||
val checkerCls = try { Class.forName("com.intellij.grazie.grammar.GrammarChecker") } catch (t: Throwable) {
|
||||
log.info("LyngGrazieAnnotator: GrammarChecker class not found: ${t.javaClass.simpleName}: ${t.message}")
|
||||
val checkerCls = try {
|
||||
Class.forName("com.intellij.grazie.grammar.GrammarChecker").also { grammarCheckerAvailable = true }
|
||||
} catch (t: Throwable) {
|
||||
grammarCheckerAvailable = false
|
||||
markGrammarCheckerMissingOnce(log, "LyngGrazieAnnotator: GrammarChecker class not found: ${t.javaClass.simpleName}: ${t.message}")
|
||||
null
|
||||
}
|
||||
if (checkerCls != null) {
|
||||
@@ -350,9 +375,6 @@ class LyngGrazieAnnotator : ExternalAnnotator<LyngGrazieAnnotator.Input, LyngGra
|
||||
} catch (_: Throwable) { null }
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val RETRY_KEY: Key<Long> = Key.create("LYNG_GRAZIE_ANN_RETRY_STAMP")
|
||||
}
|
||||
|
||||
// Fallback that uses legacy SpellCheckerManager (if present) via reflection to validate words in fragments.
|
||||
// Returns number of warnings painted.
|
||||
|
||||
@@ -65,6 +65,7 @@ class LyngColorSettingsPage : ColorSettingsPage {
|
||||
AttributesDescriptor("Type (semantic)", LyngHighlighterColors.TYPE),
|
||||
AttributesDescriptor("Namespace (semantic)", LyngHighlighterColors.NAMESPACE),
|
||||
AttributesDescriptor("Parameter (semantic)", LyngHighlighterColors.PARAMETER),
|
||||
AttributesDescriptor("Enum constant (semantic)", LyngHighlighterColors.ENUM_CONSTANT),
|
||||
)
|
||||
|
||||
override fun getColorDescriptors(): Array<ColorDescriptor> = ColorDescriptor.EMPTY_ARRAY
|
||||
|
||||
@@ -77,4 +77,9 @@ object LyngHighlighterColors {
|
||||
val ANNOTATION: TextAttributesKey = TextAttributesKey.createTextAttributesKey(
|
||||
"LYNG_ANNOTATION", DefaultLanguageHighlighterColors.METADATA
|
||||
)
|
||||
|
||||
// Enum constant (declaration or usage)
|
||||
val ENUM_CONSTANT: TextAttributesKey = TextAttributesKey.createTextAttributesKey(
|
||||
"LYNG_ENUM_CONSTANT", DefaultLanguageHighlighterColors.STATIC_FIELD
|
||||
)
|
||||
}
|
||||
@@ -34,7 +34,8 @@ class LyngLexer : LexerBase() {
|
||||
private val keywords = setOf(
|
||||
"fun", "val", "var", "class", "type", "import", "as",
|
||||
"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"
|
||||
)
|
||||
|
||||
override fun start(buffer: CharSequence, startOffset: Int, endOffset: Int, initialState: Int) {
|
||||
|
||||
+109
@@ -0,0 +1,109 @@
|
||||
/*
|
||||
* 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.navigation
|
||||
|
||||
import com.intellij.icons.AllIcons
|
||||
import com.intellij.navigation.ItemPresentation
|
||||
import com.intellij.openapi.util.TextRange
|
||||
import com.intellij.psi.PsiDocumentManager
|
||||
import com.intellij.psi.PsiElement
|
||||
import com.intellij.psi.PsiFile
|
||||
import com.intellij.psi.PsiNameIdentifierOwner
|
||||
import com.intellij.psi.impl.light.LightElement
|
||||
import com.intellij.util.IncorrectOperationException
|
||||
import net.sergeych.lyng.idea.LyngLanguage
|
||||
import javax.swing.Icon
|
||||
|
||||
/**
|
||||
* A light PSI element representing a Lyng declaration (function, class, enum, or variable).
|
||||
* Used for navigation and to provide a stable anchor for "Find Usages".
|
||||
*/
|
||||
class LyngDeclarationElement(
|
||||
private val nameElement: PsiElement,
|
||||
private val name: String,
|
||||
val kind: String = "declaration"
|
||||
) : LightElement(nameElement.manager, LyngLanguage), PsiNameIdentifierOwner {
|
||||
|
||||
override fun getName(): String = name
|
||||
|
||||
override fun setName(name: String): PsiElement {
|
||||
throw IncorrectOperationException("Renaming is not yet supported")
|
||||
}
|
||||
|
||||
override fun getNameIdentifier(): PsiElement = nameElement
|
||||
|
||||
override fun getNavigationElement(): PsiElement = nameElement
|
||||
|
||||
override fun getTextRange(): TextRange = nameElement.textRange
|
||||
|
||||
override fun getContainingFile(): PsiFile = nameElement.containingFile
|
||||
|
||||
override fun isValid(): Boolean = nameElement.isValid
|
||||
|
||||
override fun getPresentation(): ItemPresentation {
|
||||
return object : ItemPresentation {
|
||||
override fun getPresentableText(): String = name
|
||||
override fun getLocationString(): String {
|
||||
val file = containingFile
|
||||
val document = PsiDocumentManager.getInstance(file.project).getDocument(file)
|
||||
val line = if (document != null) document.getLineNumber(textRange.startOffset) + 1 else "?"
|
||||
val column = if (document != null) {
|
||||
val lineStart = document.getLineStartOffset(document.getLineNumber(textRange.startOffset))
|
||||
textRange.startOffset - lineStart + 1
|
||||
} else "?"
|
||||
return "${file.name}:$line:$column"
|
||||
}
|
||||
override fun getIcon(unused: Boolean): Icon {
|
||||
return when (kind) {
|
||||
"Function" -> AllIcons.Nodes.Function
|
||||
"Class" -> AllIcons.Nodes.Class
|
||||
"Enum" -> AllIcons.Nodes.Enum
|
||||
"EnumConstant" -> AllIcons.Nodes.Enum
|
||||
"Variable" -> AllIcons.Nodes.Variable
|
||||
"Value" -> AllIcons.Nodes.Field
|
||||
"Parameter" -> AllIcons.Nodes.Parameter
|
||||
"Initializer" -> AllIcons.Nodes.Method
|
||||
else -> AllIcons.Nodes.Property
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun toString(): String = "$kind:$name"
|
||||
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (this === other) return true
|
||||
if (other !is LyngDeclarationElement) return false
|
||||
return name == other.name && nameElement == other.nameElement
|
||||
}
|
||||
|
||||
override fun isEquivalentTo(another: PsiElement?): Boolean {
|
||||
if (this === another) return true
|
||||
if (another == nameElement) return true
|
||||
if (another is LyngDeclarationElement) {
|
||||
return name == another.name && nameElement == another.nameElement
|
||||
}
|
||||
return super.isEquivalentTo(another)
|
||||
}
|
||||
|
||||
override fun hashCode(): Int {
|
||||
var result = nameElement.hashCode()
|
||||
result = 31 * result + name.hashCode()
|
||||
return result
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
/*
|
||||
* 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.navigation
|
||||
|
||||
import com.intellij.lang.cacheBuilder.DefaultWordsScanner
|
||||
import com.intellij.lang.cacheBuilder.WordsScanner
|
||||
import com.intellij.lang.findUsages.FindUsagesProvider
|
||||
import com.intellij.psi.PsiDocumentManager
|
||||
import com.intellij.psi.PsiElement
|
||||
import com.intellij.psi.tree.TokenSet
|
||||
import net.sergeych.lyng.idea.highlight.LyngLexer
|
||||
import net.sergeych.lyng.idea.highlight.LyngTokenTypes
|
||||
import net.sergeych.lyng.idea.util.LyngAstManager
|
||||
import net.sergeych.lyng.miniast.DocLookupUtils
|
||||
|
||||
class LyngFindUsagesProvider : FindUsagesProvider {
|
||||
override fun getWordsScanner(): WordsScanner {
|
||||
return DefaultWordsScanner(
|
||||
LyngLexer(),
|
||||
TokenSet.create(LyngTokenTypes.IDENTIFIER),
|
||||
TokenSet.create(LyngTokenTypes.LINE_COMMENT, LyngTokenTypes.BLOCK_COMMENT),
|
||||
TokenSet.create(LyngTokenTypes.STRING)
|
||||
)
|
||||
}
|
||||
|
||||
override fun canFindUsagesFor(psiElement: PsiElement): Boolean {
|
||||
return psiElement is LyngDeclarationElement || isDeclaration(psiElement)
|
||||
}
|
||||
|
||||
private fun isDeclaration(element: PsiElement): Boolean {
|
||||
val file = element.containingFile ?: return false
|
||||
val mini = LyngAstManager.getMiniAst(file) ?: return false
|
||||
val offset = element.textRange.startOffset
|
||||
val name = element.text ?: ""
|
||||
return DocLookupUtils.findDeclarationAt(mini, offset, name) != null
|
||||
}
|
||||
|
||||
override fun getHelpId(psiElement: PsiElement): String? = null
|
||||
|
||||
override fun getType(element: PsiElement): String {
|
||||
if (element is LyngDeclarationElement) return element.kind
|
||||
val file = element.containingFile ?: return "Lyng declaration"
|
||||
val mini = LyngAstManager.getMiniAst(file) ?: return "Lyng declaration"
|
||||
val info = DocLookupUtils.findDeclarationAt(mini, element.textRange.startOffset, element.text ?: "")
|
||||
return info?.second ?: "Lyng declaration"
|
||||
}
|
||||
|
||||
override fun getDescriptiveName(element: PsiElement): String {
|
||||
if (element is LyngDeclarationElement) {
|
||||
val file = element.containingFile
|
||||
val document = PsiDocumentManager.getInstance(file.project).getDocument(file)
|
||||
val line = if (document != null) document.getLineNumber(element.textRange.startOffset) + 1 else "?"
|
||||
val column = if (document != null) {
|
||||
val lineStart = document.getLineStartOffset(document.getLineNumber(element.textRange.startOffset))
|
||||
element.textRange.startOffset - lineStart + 1
|
||||
} else "?"
|
||||
return "${element.name} (${file.name}:$line:$column)"
|
||||
}
|
||||
val file = element.containingFile ?: return element.text ?: "unknown"
|
||||
val mini = LyngAstManager.getMiniAst(file) ?: return element.text ?: "unknown"
|
||||
val info = DocLookupUtils.findDeclarationAt(mini, element.textRange.startOffset, element.text ?: "")
|
||||
|
||||
val document = PsiDocumentManager.getInstance(file.project).getDocument(file)
|
||||
val line = if (document != null) document.getLineNumber(element.textRange.startOffset) + 1 else "?"
|
||||
val column = if (document != null) {
|
||||
val lineStart = document.getLineStartOffset(document.getLineNumber(element.textRange.startOffset))
|
||||
element.textRange.startOffset - lineStart + 1
|
||||
} else "?"
|
||||
|
||||
val name = info?.first ?: element.text ?: "unknown"
|
||||
return "$name (${file.name}:$line:$column)"
|
||||
}
|
||||
|
||||
override fun getNodeText(element: PsiElement, useFullName: Boolean): String {
|
||||
return (element as? LyngDeclarationElement)?.name ?: element.text ?: "unknown"
|
||||
}
|
||||
}
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* 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.navigation
|
||||
|
||||
import com.intellij.codeInsight.navigation.actions.GotoDeclarationHandler
|
||||
import com.intellij.openapi.editor.Editor
|
||||
import com.intellij.psi.PsiElement
|
||||
|
||||
/**
|
||||
* Ensures Ctrl+B (Go to Definition) works on Lyng identifiers by resolving through LyngPsiReference.
|
||||
*/
|
||||
class LyngGotoDeclarationHandler : GotoDeclarationHandler {
|
||||
override fun getGotoDeclarationTargets(sourceElement: PsiElement?, offset: Int, editor: Editor?): Array<PsiElement>? {
|
||||
if (sourceElement == null) return null
|
||||
|
||||
val allTargets = mutableListOf<PsiElement>()
|
||||
|
||||
// Find reference at the element or its parent (sometimes the identifier token is wrapped)
|
||||
val ref = sourceElement.reference ?: sourceElement.parent?.reference
|
||||
if (ref is LyngPsiReference) {
|
||||
val resolved = ref.multiResolve(false)
|
||||
allTargets.addAll(resolved.mapNotNull { it.element })
|
||||
} else {
|
||||
// Manual check if not picked up by reference (e.g. if contributor didn't run yet)
|
||||
val manualRef = LyngPsiReference(sourceElement)
|
||||
val manualResolved = manualRef.multiResolve(false)
|
||||
allTargets.addAll(manualResolved.mapNotNull { it.element })
|
||||
}
|
||||
|
||||
if (allTargets.isEmpty()) return null
|
||||
|
||||
// If there is only one target and it's equivalent to the source, return null.
|
||||
// This allows IDEA to treat it as a declaration site and trigger "Show Usages".
|
||||
if (allTargets.size == 1) {
|
||||
val target = allTargets[0]
|
||||
if (target == sourceElement || target.isEquivalentTo(sourceElement)) {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
return allTargets.toTypedArray()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* 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.navigation
|
||||
|
||||
import com.intellij.icons.AllIcons
|
||||
import com.intellij.ide.IconProvider
|
||||
import com.intellij.psi.PsiElement
|
||||
import net.sergeych.lyng.idea.util.LyngAstManager
|
||||
import net.sergeych.lyng.miniast.DocLookupUtils
|
||||
import javax.swing.Icon
|
||||
|
||||
class LyngIconProvider : IconProvider() {
|
||||
override fun getIcon(element: PsiElement, flags: Int): Icon? {
|
||||
val file = element.containingFile ?: return null
|
||||
val mini = LyngAstManager.getMiniAst(file) ?: return null
|
||||
|
||||
val info = DocLookupUtils.findDeclarationAt(mini, element.textRange.startOffset, element.text ?: "")
|
||||
if (info != null) {
|
||||
return when (info.second) {
|
||||
"Function" -> AllIcons.Nodes.Function
|
||||
"Class" -> AllIcons.Nodes.Class
|
||||
"Enum" -> AllIcons.Nodes.Enum
|
||||
"EnumConstant" -> AllIcons.Nodes.Enum
|
||||
"Variable" -> AllIcons.Nodes.Variable
|
||||
"Value" -> AllIcons.Nodes.Field
|
||||
"Parameter" -> AllIcons.Nodes.Parameter
|
||||
"Initializer" -> AllIcons.Nodes.Method
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,204 @@
|
||||
/*
|
||||
* 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.navigation
|
||||
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.openapi.util.TextRange
|
||||
import com.intellij.psi.*
|
||||
import com.intellij.psi.search.FilenameIndex
|
||||
import com.intellij.psi.search.GlobalSearchScope
|
||||
import net.sergeych.lyng.highlight.offsetOf
|
||||
import net.sergeych.lyng.idea.util.LyngAstManager
|
||||
import net.sergeych.lyng.idea.util.TextCtx
|
||||
import net.sergeych.lyng.miniast.*
|
||||
|
||||
class LyngPsiReference(element: PsiElement) : PsiPolyVariantReferenceBase<PsiElement>(element, TextRange(0, element.textLength)) {
|
||||
|
||||
override fun multiResolve(incompleteCode: Boolean): Array<ResolveResult> {
|
||||
val file = element.containingFile
|
||||
val text = file.text
|
||||
val offset = element.textRange.startOffset
|
||||
val name = element.text ?: ""
|
||||
val results = mutableListOf<ResolveResult>()
|
||||
|
||||
val mini = LyngAstManager.getMiniAst(file) ?: return emptyArray()
|
||||
val binding = LyngAstManager.getBinding(file)
|
||||
|
||||
// 1. Member resolution (obj.member)
|
||||
val dotPos = TextCtx.findDotLeft(text, offset)
|
||||
if (dotPos != null) {
|
||||
val imported = DocLookupUtils.canonicalImportedModules(mini, text)
|
||||
val receiverClass = DocLookupUtils.guessReceiverClassViaMini(mini, text, dotPos, imported, binding)
|
||||
?: DocLookupUtils.guessReceiverClass(text, dotPos, imported, mini)
|
||||
|
||||
if (receiverClass != null) {
|
||||
val resolved = DocLookupUtils.resolveMemberWithInheritance(imported, receiverClass, name, mini)
|
||||
if (resolved != null) {
|
||||
val owner = resolved.first
|
||||
val member = resolved.second
|
||||
|
||||
// We need to find the actual PSI element for this member
|
||||
val targetFile = findFileForClass(file.project, owner) ?: file
|
||||
val targetMini = LyngAstManager.getMiniAst(targetFile)
|
||||
if (targetMini != null) {
|
||||
val targetSrc = targetMini.range.start.source
|
||||
val off = targetSrc.offsetOf(member.nameStart)
|
||||
targetFile.findElementAt(off)?.let {
|
||||
val kind = when(member) {
|
||||
is MiniMemberFunDecl -> "Function"
|
||||
is MiniMemberValDecl -> if (member.mutable) "Variable" else "Value"
|
||||
is MiniInitDecl -> "Initializer"
|
||||
}
|
||||
results.add(PsiElementResolveResult(LyngDeclarationElement(it, member.name, kind)))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// If we couldn't resolve exactly, we might still want to search globally but ONLY for members
|
||||
if (results.isEmpty()) {
|
||||
results.addAll(resolveGlobally(file.project, name, membersOnly = true))
|
||||
}
|
||||
} else {
|
||||
// 2. Local resolution via Binder
|
||||
if (binding != null) {
|
||||
val ref = binding.references.firstOrNull { offset >= it.start && offset < it.end }
|
||||
if (ref != null) {
|
||||
val sym = binding.symbols.firstOrNull { it.id == ref.symbolId }
|
||||
if (sym != null && sym.declStart >= 0) {
|
||||
file.findElementAt(sym.declStart)?.let {
|
||||
results.add(PsiElementResolveResult(LyngDeclarationElement(it, sym.name, sym.kind.name)))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Global project scan
|
||||
// Only search globally if we haven't found a strong local match
|
||||
if (results.isEmpty()) {
|
||||
results.addAll(resolveGlobally(file.project, name))
|
||||
}
|
||||
}
|
||||
|
||||
// 4. Filter results to exclude duplicates
|
||||
// Use a more robust de-duplication that prefers the raw element if multiple refer to the same thing
|
||||
val filtered = mutableListOf<ResolveResult>()
|
||||
for (res in results) {
|
||||
val el = res.element ?: continue
|
||||
val nav = if (el is LyngDeclarationElement) el.navigationElement else el
|
||||
if (filtered.none { existing ->
|
||||
val exEl = existing.element
|
||||
val exNav = if (exEl is LyngDeclarationElement) exEl.navigationElement else exEl
|
||||
exNav == nav || (exNav != null && exNav.isEquivalentTo(nav))
|
||||
}) {
|
||||
filtered.add(res)
|
||||
}
|
||||
}
|
||||
|
||||
return filtered.toTypedArray()
|
||||
}
|
||||
|
||||
private fun findFileForClass(project: Project, className: String): PsiFile? {
|
||||
val psiManager = PsiManager.getInstance(project)
|
||||
|
||||
// 1. Try file with matching name first (optimization)
|
||||
val matchingFiles = FilenameIndex.getFilesByName(project, "$className.lyng", GlobalSearchScope.projectScope(project))
|
||||
for (file in matchingFiles) {
|
||||
val mini = LyngAstManager.getMiniAst(file) ?: continue
|
||||
if (mini.declarations.any { (it is MiniClassDecl && it.name == className) || (it is MiniEnumDecl && it.name == className) }) {
|
||||
return file
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Fallback to full project scan
|
||||
val allFiles = FilenameIndex.getAllFilesByExt(project, "lyng", GlobalSearchScope.projectScope(project))
|
||||
for (vFile in allFiles) {
|
||||
val file = psiManager.findFile(vFile) ?: continue
|
||||
if (matchingFiles.contains(file)) continue // already checked
|
||||
val mini = LyngAstManager.getMiniAst(file) ?: continue
|
||||
if (mini.declarations.any { (it is MiniClassDecl && it.name == className) || (it is MiniEnumDecl && it.name == className) }) {
|
||||
return file
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
override fun resolve(): PsiElement? {
|
||||
val results = multiResolve(false)
|
||||
if (results.isEmpty()) return null
|
||||
val target = results[0].element ?: return null
|
||||
// If the target is equivalent to our source element, return the source element itself.
|
||||
// This is crucial for IDEA to recognize we are already at the declaration site
|
||||
// and trigger "Show Usages" instead of performing a no-op navigation.
|
||||
if (target == element || target.isEquivalentTo(element)) {
|
||||
return element
|
||||
}
|
||||
return target
|
||||
}
|
||||
|
||||
private fun resolveGlobally(project: Project, name: String, membersOnly: Boolean = false): List<ResolveResult> {
|
||||
val results = mutableListOf<ResolveResult>()
|
||||
val files = FilenameIndex.getAllFilesByExt(project, "lyng", GlobalSearchScope.projectScope(project))
|
||||
val psiManager = PsiManager.getInstance(project)
|
||||
|
||||
for (vFile in files) {
|
||||
val file = psiManager.findFile(vFile) ?: continue
|
||||
val mini = LyngAstManager.getMiniAst(file) ?: continue
|
||||
val src = mini.range.start.source
|
||||
|
||||
fun addIfMatch(dName: String, nameStart: net.sergeych.lyng.Pos, dKind: String) {
|
||||
if (dName == name) {
|
||||
val off = src.offsetOf(nameStart)
|
||||
file.findElementAt(off)?.let {
|
||||
results.add(PsiElementResolveResult(LyngDeclarationElement(it, dName, dKind)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (d in mini.declarations) {
|
||||
if (!membersOnly) {
|
||||
val dKind = when(d) {
|
||||
is net.sergeych.lyng.miniast.MiniFunDecl -> "Function"
|
||||
is net.sergeych.lyng.miniast.MiniClassDecl -> "Class"
|
||||
is net.sergeych.lyng.miniast.MiniEnumDecl -> "Enum"
|
||||
is net.sergeych.lyng.miniast.MiniValDecl -> if (d.mutable) "Variable" else "Value"
|
||||
}
|
||||
addIfMatch(d.name, d.nameStart, dKind)
|
||||
}
|
||||
|
||||
// Check members of classes and enums
|
||||
val members = when(d) {
|
||||
is MiniClassDecl -> d.members
|
||||
is MiniEnumDecl -> DocLookupUtils.enumToSyntheticClass(d).members
|
||||
else -> emptyList()
|
||||
}
|
||||
|
||||
for (m in members) {
|
||||
val mKind = when(m) {
|
||||
is net.sergeych.lyng.miniast.MiniMemberFunDecl -> "Function"
|
||||
is net.sergeych.lyng.miniast.MiniMemberValDecl -> if (m.mutable) "Variable" else "Value"
|
||||
is net.sergeych.lyng.miniast.MiniInitDecl -> "Initializer"
|
||||
}
|
||||
addIfMatch(m.name, m.nameStart, mKind)
|
||||
}
|
||||
}
|
||||
}
|
||||
return results
|
||||
}
|
||||
|
||||
override fun getVariants(): Array<Any> = emptyArray()
|
||||
}
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* 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.navigation
|
||||
|
||||
import com.intellij.patterns.PlatformPatterns
|
||||
import com.intellij.psi.*
|
||||
import com.intellij.util.ProcessingContext
|
||||
import net.sergeych.lyng.idea.LyngLanguage
|
||||
import net.sergeych.lyng.idea.highlight.LyngTokenTypes
|
||||
import net.sergeych.lyng.idea.util.LyngAstManager
|
||||
import net.sergeych.lyng.miniast.DocLookupUtils
|
||||
|
||||
class LyngPsiReferenceContributor : PsiReferenceContributor() {
|
||||
override fun registerReferenceProviders(registrar: PsiReferenceRegistrar) {
|
||||
registrar.registerReferenceProvider(
|
||||
PlatformPatterns.psiElement().withLanguage(LyngLanguage),
|
||||
object : PsiReferenceProvider() {
|
||||
override fun getReferencesByElement(
|
||||
element: PsiElement,
|
||||
context: ProcessingContext
|
||||
): Array<PsiReference> {
|
||||
if (element.node.elementType == LyngTokenTypes.IDENTIFIER) {
|
||||
val file = element.containingFile
|
||||
val mini = LyngAstManager.getMiniAst(file)
|
||||
if (mini != null) {
|
||||
val offset = element.textRange.startOffset
|
||||
val name = element.text ?: ""
|
||||
if (DocLookupUtils.findDeclarationAt(mini, offset, name) != null) {
|
||||
return PsiReference.EMPTY_ARRAY
|
||||
}
|
||||
}
|
||||
return arrayOf(LyngPsiReference(element))
|
||||
}
|
||||
return PsiReference.EMPTY_ARRAY
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -50,6 +50,8 @@ class LyngFormatterSettings(private val project: Project) : PersistentStateCompo
|
||||
var offerLyngTypoQuickFixes: Boolean = true,
|
||||
// Per-project learned words (do not flag again)
|
||||
var learnedWords: MutableSet<String> = mutableSetOf(),
|
||||
// Experimental: enable Lyng autocompletion (can be disabled if needed)
|
||||
var enableLyngCompletionExperimental: Boolean = true,
|
||||
)
|
||||
|
||||
private var myState: State = State()
|
||||
@@ -116,6 +118,10 @@ class LyngFormatterSettings(private val project: Project) : PersistentStateCompo
|
||||
get() = myState.learnedWords
|
||||
set(value) { myState.learnedWords = value }
|
||||
|
||||
var enableLyngCompletionExperimental: Boolean
|
||||
get() = myState.enableLyngCompletionExperimental
|
||||
set(value) { myState.enableLyngCompletionExperimental = value }
|
||||
|
||||
companion object {
|
||||
@JvmStatic
|
||||
fun getInstance(project: Project): LyngFormatterSettings = project.getService(LyngFormatterSettings::class.java)
|
||||
|
||||
+8
-1
@@ -38,6 +38,7 @@ class LyngFormatterSettingsConfigurable(private val project: Project) : Configur
|
||||
private var debugShowSpellFeedCb: JCheckBox? = null
|
||||
private var showTyposGreenCb: JCheckBox? = null
|
||||
private var offerQuickFixesCb: JCheckBox? = null
|
||||
private var enableCompletionCb: JCheckBox? = null
|
||||
|
||||
override fun getDisplayName(): String = "Lyng Formatter"
|
||||
|
||||
@@ -57,6 +58,7 @@ class LyngFormatterSettingsConfigurable(private val project: Project) : Configur
|
||||
debugShowSpellFeedCb = JCheckBox("Debug: show spell-feed ranges (weak warnings)")
|
||||
showTyposGreenCb = JCheckBox("Show Lyng typos with green underline (TYPO styling)")
|
||||
offerQuickFixesCb = JCheckBox("Offer Lyng typo quick fixes (Replace…, Add to dictionary) without Spell Checker")
|
||||
enableCompletionCb = JCheckBox("Enable Lyng autocompletion (experimental)")
|
||||
|
||||
// Tooltips / short help
|
||||
spacingCb?.toolTipText = "Applies minimal, safe spacing (e.g., around commas/operators, control-flow parens)."
|
||||
@@ -71,6 +73,7 @@ class LyngFormatterSettingsConfigurable(private val project: Project) : Configur
|
||||
debugShowSpellFeedCb?.toolTipText = "Show the exact ranges we feed to spellcheckers (ids/comments/strings) as weak warnings."
|
||||
showTyposGreenCb?.toolTipText = "Render Lyng typos using the platform's green TYPO underline instead of generic warnings."
|
||||
offerQuickFixesCb?.toolTipText = "Provide lightweight Replace… and Add to dictionary quick-fixes without requiring the legacy Spell Checker."
|
||||
enableCompletionCb?.toolTipText = "Turn on/off the lightweight Lyng code completion (BASIC)."
|
||||
p.add(spacingCb)
|
||||
p.add(wrappingCb)
|
||||
p.add(reindentClosedBlockCb)
|
||||
@@ -84,6 +87,7 @@ class LyngFormatterSettingsConfigurable(private val project: Project) : Configur
|
||||
p.add(debugShowSpellFeedCb)
|
||||
p.add(showTyposGreenCb)
|
||||
p.add(offerQuickFixesCb)
|
||||
p.add(enableCompletionCb)
|
||||
panel = p
|
||||
reset()
|
||||
return p
|
||||
@@ -103,7 +107,8 @@ class LyngFormatterSettingsConfigurable(private val project: Project) : Configur
|
||||
grazieLiteralsAsCommentsCb?.isSelected != s.grazieTreatLiteralsAsComments ||
|
||||
debugShowSpellFeedCb?.isSelected != s.debugShowSpellFeed ||
|
||||
showTyposGreenCb?.isSelected != s.showTyposWithGreenUnderline ||
|
||||
offerQuickFixesCb?.isSelected != s.offerLyngTypoQuickFixes
|
||||
offerQuickFixesCb?.isSelected != s.offerLyngTypoQuickFixes ||
|
||||
enableCompletionCb?.isSelected != s.enableLyngCompletionExperimental
|
||||
}
|
||||
|
||||
override fun apply() {
|
||||
@@ -121,6 +126,7 @@ class LyngFormatterSettingsConfigurable(private val project: Project) : Configur
|
||||
s.debugShowSpellFeed = debugShowSpellFeedCb?.isSelected == true
|
||||
s.showTyposWithGreenUnderline = showTyposGreenCb?.isSelected == true
|
||||
s.offerLyngTypoQuickFixes = offerQuickFixesCb?.isSelected == true
|
||||
s.enableLyngCompletionExperimental = enableCompletionCb?.isSelected == true
|
||||
}
|
||||
|
||||
override fun reset() {
|
||||
@@ -138,5 +144,6 @@ class LyngFormatterSettingsConfigurable(private val project: Project) : Configur
|
||||
debugShowSpellFeedCb?.isSelected = s.debugShowSpellFeed
|
||||
showTyposGreenCb?.isSelected = s.showTyposWithGreenUnderline
|
||||
offerQuickFixesCb?.isSelected = s.offerLyngTypoQuickFixes
|
||||
enableCompletionCb?.isSelected = s.enableLyngCompletionExperimental
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Ensure external/bundled docs are registered in BuiltinDocRegistry
|
||||
* so completion/quickdoc can resolve things like lyng.io.fs.Path.
|
||||
*/
|
||||
package net.sergeych.lyng.idea.util
|
||||
|
||||
import com.intellij.openapi.diagnostic.Logger
|
||||
import net.sergeych.lyng.idea.docs.FsDocsFallback
|
||||
|
||||
object DocsBootstrap {
|
||||
private val log = Logger.getInstance(DocsBootstrap::class.java)
|
||||
@Volatile private var ensured = false
|
||||
|
||||
fun ensure() {
|
||||
if (ensured) return
|
||||
synchronized(this) {
|
||||
if (ensured) return
|
||||
val loaded = tryLoadExternal() || trySeedFallback()
|
||||
if (loaded) ensured = true else ensured = true // mark done to avoid repeated attempts
|
||||
}
|
||||
}
|
||||
|
||||
private fun tryLoadExternal(): Boolean = try {
|
||||
val cls = Class.forName("net.sergeych.lyngio.docs.FsBuiltinDocs")
|
||||
val m = cls.getMethod("ensure")
|
||||
m.invoke(null)
|
||||
log.info("[LYNG_DEBUG] DocsBootstrap: external docs loaded: net.sergeych.lyngio.docs.FsBuiltinDocs.ensure() OK")
|
||||
true
|
||||
} catch (_: Throwable) {
|
||||
false
|
||||
}
|
||||
|
||||
private fun trySeedFallback(): Boolean = try {
|
||||
val seeded = FsDocsFallback.ensureOnce()
|
||||
if (seeded) {
|
||||
log.info("[LYNG_DEBUG] DocsBootstrap: external docs not found; seeded plugin fallback for lyng.io.fs")
|
||||
} else {
|
||||
log.info("[LYNG_DEBUG] DocsBootstrap: external docs not found; no fallback seeded")
|
||||
}
|
||||
seeded
|
||||
} catch (_: Throwable) {
|
||||
false
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* 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.util
|
||||
|
||||
import com.intellij.application.options.CodeStyle
|
||||
import com.intellij.openapi.editor.Document
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.openapi.util.TextRange
|
||||
import net.sergeych.lyng.format.LyngFormatConfig
|
||||
import net.sergeych.lyng.format.LyngFormatter
|
||||
|
||||
object FormattingUtils {
|
||||
fun computeDesiredIndent(project: Project, doc: Document, line: Int): String {
|
||||
val options = CodeStyle.getIndentOptions(project, doc)
|
||||
val start = 0
|
||||
val end = doc.getLineEndOffset(line)
|
||||
val snippet = doc.getText(TextRange(start, end))
|
||||
val lineText = if (line < doc.lineCount) {
|
||||
val ls = doc.getLineStartOffset(line)
|
||||
val le = doc.getLineEndOffset(line)
|
||||
doc.getText(TextRange(ls, le))
|
||||
} else ""
|
||||
val isBlankLine = lineText.trim().isEmpty()
|
||||
val snippetForCalc = if (isBlankLine) snippet + "x" else snippet
|
||||
val cfg = LyngFormatConfig(
|
||||
indentSize = options.INDENT_SIZE.coerceAtLeast(1),
|
||||
useTabs = options.USE_TAB_CHARACTER,
|
||||
continuationIndentSize = options.CONTINUATION_INDENT_SIZE.coerceAtLeast(options.INDENT_SIZE.coerceAtLeast(1)),
|
||||
)
|
||||
val formatted = LyngFormatter.reindent(snippetForCalc, cfg)
|
||||
val lastNl = formatted.lastIndexOf('\n')
|
||||
val lastLine = if (lastNl >= 0) formatted.substring(lastNl + 1) else formatted
|
||||
val wsLen = lastLine.indexOfFirst { it != ' ' && it != '\t' }.let { if (it < 0) lastLine.length else it }
|
||||
return lastLine.substring(0, wsLen)
|
||||
}
|
||||
|
||||
fun findFirstNonWs(doc: Document, start: Int, end: Int): Int {
|
||||
var i = start
|
||||
val text = doc.charsSequence
|
||||
while (i < end) {
|
||||
val ch = text[i]
|
||||
if (ch != ' ' && ch != '\t') break
|
||||
i++
|
||||
}
|
||||
return i
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* 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.util
|
||||
|
||||
import com.intellij.openapi.util.Key
|
||||
import com.intellij.psi.PsiFile
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import net.sergeych.lyng.Compiler
|
||||
import net.sergeych.lyng.Source
|
||||
import net.sergeych.lyng.binding.Binder
|
||||
import net.sergeych.lyng.binding.BindingSnapshot
|
||||
import net.sergeych.lyng.miniast.MiniAstBuilder
|
||||
import net.sergeych.lyng.miniast.MiniScript
|
||||
|
||||
object LyngAstManager {
|
||||
private val MINI_KEY = Key.create<MiniScript>("lyng.mini.cache")
|
||||
private val BINDING_KEY = Key.create<BindingSnapshot>("lyng.binding.cache")
|
||||
private val STAMP_KEY = Key.create<Long>("lyng.mini.cache.stamp")
|
||||
|
||||
fun getMiniAst(file: PsiFile): MiniScript? {
|
||||
val doc = file.viewProvider.document ?: return null
|
||||
val stamp = doc.modificationStamp
|
||||
val prevStamp = file.getUserData(STAMP_KEY)
|
||||
val cached = file.getUserData(MINI_KEY)
|
||||
if (cached != null && prevStamp != null && prevStamp == stamp) return cached
|
||||
|
||||
val text = doc.text
|
||||
val sink = MiniAstBuilder()
|
||||
val built = try {
|
||||
val provider = IdeLenientImportProvider.create()
|
||||
val src = Source(file.name, text)
|
||||
runBlocking { Compiler.compileWithMini(src, provider, sink) }
|
||||
sink.build()
|
||||
} catch (_: Throwable) {
|
||||
sink.build()
|
||||
}
|
||||
|
||||
if (built != null) {
|
||||
file.putUserData(MINI_KEY, built)
|
||||
file.putUserData(STAMP_KEY, stamp)
|
||||
// Invalidate binding too
|
||||
file.putUserData(BINDING_KEY, null)
|
||||
}
|
||||
return built
|
||||
}
|
||||
|
||||
fun getBinding(file: PsiFile): BindingSnapshot? {
|
||||
val doc = file.viewProvider.document ?: return null
|
||||
val stamp = doc.modificationStamp
|
||||
val prevStamp = file.getUserData(STAMP_KEY)
|
||||
val cached = file.getUserData(BINDING_KEY)
|
||||
|
||||
if (cached != null && prevStamp != null && prevStamp == stamp) return cached
|
||||
|
||||
val mini = getMiniAst(file) ?: return null
|
||||
val text = doc.text
|
||||
val binding = try {
|
||||
Binder.bind(text, mini)
|
||||
} catch (_: Throwable) {
|
||||
null
|
||||
}
|
||||
|
||||
if (binding != null) {
|
||||
file.putUserData(BINDING_KEY, binding)
|
||||
// stamp is already set by getMiniAst
|
||||
}
|
||||
return binding
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* Shared tiny, PSI-free text helpers for Lyng editor features (Quick Doc, Completion).
|
||||
*/
|
||||
package net.sergeych.lyng.idea.util
|
||||
|
||||
import com.intellij.openapi.util.TextRange
|
||||
|
||||
object TextCtx {
|
||||
fun prefixAt(text: String, offset: Int): String {
|
||||
val off = offset.coerceIn(0, text.length)
|
||||
var i = (off - 1).coerceAtLeast(0)
|
||||
while (i >= 0 && isIdentChar(text[i])) i--
|
||||
val start = i + 1
|
||||
return if (start in 0..text.length && start <= off) text.substring(start, off) else ""
|
||||
}
|
||||
|
||||
fun wordRangeAt(text: String, offset: Int): TextRange? {
|
||||
if (text.isEmpty()) return null
|
||||
val off = offset.coerceIn(0, text.length)
|
||||
var s = off
|
||||
var e = off
|
||||
while (s > 0 && isIdentChar(text[s - 1])) s--
|
||||
while (e < text.length && isIdentChar(text[e])) e++
|
||||
return if (s < e) TextRange(s, e) else null
|
||||
}
|
||||
|
||||
fun findDotLeft(text: String, offset: Int): Int? {
|
||||
var i = (offset - 1).coerceAtLeast(0)
|
||||
while (i >= 0 && text[i].isWhitespace()) i--
|
||||
return if (i >= 0 && text[i] == '.') i else null
|
||||
}
|
||||
|
||||
fun previousWordBefore(text: String, offset: Int): String? {
|
||||
var i = prevNonWs(text, (offset - 1).coerceAtLeast(0))
|
||||
// Skip trailing identifier at caret if inside word
|
||||
while (i >= 0 && isIdentChar(text[i])) i--
|
||||
i = prevNonWs(text, i)
|
||||
if (i < 0) return null
|
||||
val end = i + 1
|
||||
while (i >= 0 && isIdentChar(text[i])) i--
|
||||
val start = i + 1
|
||||
return if (start < end) text.substring(start, end) else null
|
||||
}
|
||||
|
||||
fun hasDotBetween(text: String, start: Int, end: Int): Boolean {
|
||||
if (start >= end) return false
|
||||
val s = start.coerceAtLeast(0)
|
||||
val e = end.coerceAtMost(text.length)
|
||||
for (i in s until e) if (text[i] == '.') return true
|
||||
return false
|
||||
}
|
||||
|
||||
fun prevNonWs(text: String, start: Int): Int {
|
||||
var i = start.coerceAtMost(text.length - 1)
|
||||
while (i >= 0 && text[i].isWhitespace()) i--
|
||||
return i
|
||||
}
|
||||
|
||||
fun isIdentChar(c: Char): Boolean = c == '_' || c.isLetterOrDigit()
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
<!--
|
||||
~ Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
|
||||
~ 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.
|
||||
@@ -62,6 +62,9 @@
|
||||
<!-- Quick documentation provider bound to Lyng language -->
|
||||
<lang.documentationProvider language="Lyng" implementationClass="net.sergeych.lyng.idea.docs.LyngDocumentationProvider"/>
|
||||
|
||||
<!-- Basic code completion (MVP) -->
|
||||
<completion.contributor language="Lyng" implementationClass="net.sergeych.lyng.idea.completion.LyngCompletionContributor"/>
|
||||
|
||||
<!-- Comment toggling support -->
|
||||
<lang.commenter language="Lyng" implementationClass="net.sergeych.lyng.idea.comment.LyngCommenter"/>
|
||||
|
||||
@@ -93,6 +96,12 @@
|
||||
<!-- If targeting SDKs with stable RawText API, the EP below can be enabled instead: -->
|
||||
<!-- <copyPastePreProcessor implementation="net.sergeych.lyng.idea.editor.LyngCopyPastePreProcessor"/> -->
|
||||
|
||||
<!-- Navigation and Find Usages -->
|
||||
<psi.referenceContributor language="Lyng" implementation="net.sergeych.lyng.idea.navigation.LyngPsiReferenceContributor"/>
|
||||
<gotoDeclarationHandler implementation="net.sergeych.lyng.idea.navigation.LyngGotoDeclarationHandler"/>
|
||||
<lang.findUsagesProvider language="Lyng" implementationClass="net.sergeych.lyng.idea.navigation.LyngFindUsagesProvider"/>
|
||||
<iconProvider implementation="net.sergeych.lyng.idea.navigation.LyngIconProvider"/>
|
||||
|
||||
</extensions>
|
||||
|
||||
<actions/>
|
||||
|
||||
+121
@@ -0,0 +1,121 @@
|
||||
package net.sergeych.lyng.idea.completion
|
||||
|
||||
import com.intellij.testFramework.fixtures.BasePlatformTestCase
|
||||
import net.sergeych.lyng.idea.util.DocsBootstrap
|
||||
import net.sergeych.lyng.miniast.BuiltinDocRegistry
|
||||
import net.sergeych.lyng.miniast.DocLookupUtils
|
||||
import net.sergeych.lyng.miniast.MiniClassDecl
|
||||
|
||||
class LyngCompletionMemberTest : BasePlatformTestCase() {
|
||||
|
||||
override fun getTestDataPath(): String = ""
|
||||
|
||||
private fun complete(code: String): List<String> {
|
||||
myFixture.configureByText("test.lyng", code)
|
||||
val items = myFixture.completeBasic()
|
||||
return myFixture.lookupElementStrings ?: emptyList()
|
||||
}
|
||||
|
||||
private fun ensureDocs(imports: List<String>) {
|
||||
// Make sure external/bundled docs like lyng.io.fs are registered
|
||||
DocsBootstrap.ensure()
|
||||
// Touch modules to force stdlib lazy load and optional modules
|
||||
for (m in imports) BuiltinDocRegistry.docsForModule(m)
|
||||
}
|
||||
|
||||
private fun aggregateMemberNames(className: String, imported: List<String>): Set<String> {
|
||||
val classes = DocLookupUtils.aggregateClasses(imported)
|
||||
val visited = mutableSetOf<String>()
|
||||
val result = linkedSetOf<String>()
|
||||
fun dfs(name: String) {
|
||||
val cls: MiniClassDecl = classes[name] ?: return
|
||||
for (m in cls.members) result.add(m.name)
|
||||
if (!visited.add(name)) return
|
||||
for (b in cls.bases) dfs(b)
|
||||
}
|
||||
dfs(className)
|
||||
// Conservative supplementation mirroring contributor behavior
|
||||
when (className) {
|
||||
"List" -> listOf("Collection", "Iterable").forEach { dfs(it) }
|
||||
"Array" -> listOf("Collection", "Iterable").forEach { dfs(it) }
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
fun test_NoGlobalsAfterDot_IteratorFromLines() {
|
||||
val code = """
|
||||
import lyng.io.fs
|
||||
import lyng.stdlib
|
||||
|
||||
val files = Path("../..").lines().<caret>
|
||||
""".trimIndent()
|
||||
val imported = listOf("lyng.io.fs", "lyng.stdlib")
|
||||
ensureDocs(imported)
|
||||
|
||||
val items = complete(code)
|
||||
// Must not propose globals after dot
|
||||
assertFalse(items.contains("Path"))
|
||||
assertFalse(items.contains("Array"))
|
||||
assertFalse(items.contains("String"))
|
||||
|
||||
// Should contain a reasonable subset of Iterator members
|
||||
val expected = aggregateMemberNames("Iterator", imported)
|
||||
// At least one expected member must appear
|
||||
val intersection = expected.intersect(items.toSet())
|
||||
assertTrue("Expected Iterator members, but got: $items", intersection.isNotEmpty())
|
||||
}
|
||||
|
||||
fun test_IteratorAfterLines_WithPrefix() {
|
||||
val code = """
|
||||
import lyng.io.fs
|
||||
import lyng.stdlib
|
||||
|
||||
val files = Path("../..").lines().t<caret>
|
||||
""".trimIndent()
|
||||
val imported = listOf("lyng.io.fs", "lyng.stdlib")
|
||||
ensureDocs(imported)
|
||||
|
||||
val items = complete(code)
|
||||
// Must not propose globals after dot even with prefix
|
||||
assertFalse(items.contains("Path"))
|
||||
assertFalse(items.contains("Array"))
|
||||
assertFalse(items.contains("String"))
|
||||
|
||||
// All suggestions should start with the typed prefix (case-insensitive)
|
||||
assertTrue(items.all { it.startsWith("t", ignoreCase = true) })
|
||||
|
||||
// Some Iterator member starting with 't' should be present (e.g., toList)
|
||||
val expected = aggregateMemberNames("Iterator", imported).filter { it.startsWith("t", true) }.toSet()
|
||||
if (expected.isNotEmpty()) {
|
||||
val intersection = expected.intersect(items.toSet())
|
||||
assertTrue("Expected Iterator members with prefix 't', got: $items", intersection.isNotEmpty())
|
||||
} else {
|
||||
// If registry has no 't*' members, at least suggestions should not be empty
|
||||
assertTrue(items.isNotEmpty())
|
||||
}
|
||||
}
|
||||
|
||||
fun test_ListLiteral_MembersWithInherited() {
|
||||
val code = """
|
||||
import lyng.stdlib
|
||||
|
||||
val x = [1,2,3].<caret>
|
||||
""".trimIndent()
|
||||
val imported = listOf("lyng.stdlib")
|
||||
ensureDocs(imported)
|
||||
|
||||
val items = complete(code)
|
||||
// Must not propose globals after dot
|
||||
assertFalse(items.contains("Array"))
|
||||
assertFalse(items.contains("String"))
|
||||
assertFalse(items.contains("Path"))
|
||||
|
||||
// Expect members from List plus parents (Collection/Iterable)
|
||||
val expected = aggregateMemberNames("List", imported)
|
||||
val intersection = expected.intersect(items.toSet())
|
||||
assertTrue("Expected List/Collection/Iterable members, got: $items", intersection.isNotEmpty())
|
||||
|
||||
// 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)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
hello from cli test
|
||||
@@ -20,6 +20,7 @@ package net.sergeych
|
||||
import com.github.ajalt.clikt.core.CliktCommand
|
||||
import com.github.ajalt.clikt.core.Context
|
||||
import com.github.ajalt.clikt.core.main
|
||||
import com.github.ajalt.clikt.core.subcommands
|
||||
import com.github.ajalt.clikt.parameters.arguments.argument
|
||||
import com.github.ajalt.clikt.parameters.arguments.multiple
|
||||
import com.github.ajalt.clikt.parameters.arguments.optional
|
||||
@@ -71,69 +72,81 @@ val baseScopeDefer = globalDefer {
|
||||
}
|
||||
|
||||
fun runMain(args: Array<String>) {
|
||||
if(args.isNotEmpty()) {
|
||||
// CLI formatter: lyng fmt [--check] [--in-place] <files...>
|
||||
if (args[0] == "fmt") {
|
||||
formatCli(args.drop(1))
|
||||
return
|
||||
}
|
||||
if( args.size >= 2 && args[0] == "--" ) {
|
||||
// -- -file.lyng <args>
|
||||
// Fast paths for legacy/positional script execution that should work without requiring explicit options
|
||||
if (args.isNotEmpty()) {
|
||||
// Support: jyng -- -file.lyng <args>
|
||||
if (args.size >= 2 && args[0] == "--") {
|
||||
executeFileWithArgs(args[1], args.drop(2))
|
||||
return
|
||||
} else if( args[0][0] != '-') {
|
||||
// file.lyng <args>
|
||||
}
|
||||
// Support: jyng script.lyng <args> (when first token is not an option and not a subcommand name)
|
||||
if (!args[0].startsWith('-') && args[0] != "fmt") {
|
||||
executeFileWithArgs(args[0], args.drop(1))
|
||||
return
|
||||
}
|
||||
}
|
||||
// normal processing
|
||||
Lyng { runBlocking { it() } }.main(args)
|
||||
|
||||
// Delegate all other parsing and dispatching to Clikt with proper subcommands.
|
||||
Lyng { runBlocking { it() } }
|
||||
.subcommands(Fmt())
|
||||
.main(args)
|
||||
}
|
||||
|
||||
private fun formatCli(args: List<String>) {
|
||||
var checkOnly = false
|
||||
var inPlace = true
|
||||
var enableSpacing = false
|
||||
var enableWrapping = false
|
||||
val files = mutableListOf<String>()
|
||||
for (a in args) {
|
||||
when (a) {
|
||||
"--check" -> { checkOnly = true; inPlace = false }
|
||||
"--in-place", "-i" -> inPlace = true
|
||||
"--spacing" -> enableSpacing = true
|
||||
"--wrap", "--wrapping" -> enableWrapping = true
|
||||
else -> files += a
|
||||
private class Fmt : CliktCommand(name = "fmt") {
|
||||
private val checkOnly by option("--check", help = "Check only; print files that would change").flag()
|
||||
private val inPlace by option("-i", "--in-place", help = "Write changes back to files").flag()
|
||||
private val enableSpacing by option("--spacing", help = "Apply spacing normalization").flag()
|
||||
private val enableWrapping by option("--wrap", "--wrapping", help = "Enable line wrapping").flag()
|
||||
private val files by argument(help = "One or more .lyng files to format").multiple()
|
||||
|
||||
override fun help(context: Context): String = "Format Lyng source files"
|
||||
|
||||
override fun run() {
|
||||
// Validate inputs
|
||||
if (files.isEmpty()) {
|
||||
println("Error: no files specified. See --help for usage.")
|
||||
exit(1)
|
||||
}
|
||||
}
|
||||
if (files.isEmpty()) {
|
||||
println("Usage: lyng fmt [--check] [--in-place|-i] [--spacing] [--wrap] <file1.lyng> [file2.lyng ...]")
|
||||
exit(1)
|
||||
return
|
||||
}
|
||||
var changed = false
|
||||
val cfg = net.sergeych.lyng.format.LyngFormatConfig(
|
||||
applySpacing = enableSpacing,
|
||||
applyWrapping = enableWrapping,
|
||||
)
|
||||
for (path in files) {
|
||||
val p = path.toPath()
|
||||
val original = FileSystem.SYSTEM.source(p).use { it.buffer().use { bs -> bs.readUtf8() } }
|
||||
val formatted = net.sergeych.lyng.format.LyngFormatter.format(original, cfg)
|
||||
if (formatted != original) {
|
||||
changed = true
|
||||
if (checkOnly && inPlace) {
|
||||
println("Error: --check and --in-place cannot be used together")
|
||||
exit(1)
|
||||
}
|
||||
|
||||
val cfg = net.sergeych.lyng.format.LyngFormatConfig(
|
||||
applySpacing = enableSpacing,
|
||||
applyWrapping = enableWrapping,
|
||||
)
|
||||
|
||||
var anyChanged = false
|
||||
val multiFile = files.size > 1
|
||||
|
||||
for (path in files) {
|
||||
val p = path.toPath()
|
||||
val original = FileSystem.SYSTEM.source(p).use { it.buffer().use { bs -> bs.readUtf8() } }
|
||||
val formatted = net.sergeych.lyng.format.LyngFormatter.format(original, cfg)
|
||||
val changed = formatted != original
|
||||
if (checkOnly) {
|
||||
println(path)
|
||||
if (changed) {
|
||||
println(path)
|
||||
anyChanged = true
|
||||
}
|
||||
} else if (inPlace) {
|
||||
FileSystem.SYSTEM.write(p) { writeUtf8(formatted) }
|
||||
// Write back regardless, but only touch file if content differs
|
||||
if (changed) {
|
||||
FileSystem.SYSTEM.write(p) { writeUtf8(formatted) }
|
||||
}
|
||||
} else {
|
||||
// default to stdout if not in-place and not --check
|
||||
println("--- $path (formatted) ---\n$formatted")
|
||||
// Default: stdout output
|
||||
if (multiFile) {
|
||||
println("--- $path ---")
|
||||
}
|
||||
println(formatted)
|
||||
}
|
||||
}
|
||||
}
|
||||
if (checkOnly) {
|
||||
exit(if (changed) 2 else 0)
|
||||
|
||||
if (checkOnly) {
|
||||
exit(if (anyChanged) 2 else 0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -162,6 +175,10 @@ private class Lyng(val launcher: (suspend () -> Unit) -> Unit) : CliktCommand()
|
||||
""".trimIndent()
|
||||
|
||||
override fun run() {
|
||||
// If a subcommand (like `fmt`) was invoked, do nothing in the root command.
|
||||
// This prevents the root from printing help before the subcommand runs.
|
||||
if (currentContext.invokedSubcommand != null) return
|
||||
|
||||
runBlocking {
|
||||
val baseScope = baseScopeDefer.await()
|
||||
when {
|
||||
@@ -188,13 +205,7 @@ private class Lyng(val launcher: (suspend () -> Unit) -> Unit) : CliktCommand()
|
||||
|
||||
else -> {
|
||||
if (script == null) {
|
||||
println(
|
||||
"""
|
||||
|
||||
Error: no script specified.
|
||||
|
||||
""".trimIndent()
|
||||
)
|
||||
println("Error: no script specified.\n")
|
||||
echoFormattedHelp()
|
||||
} else {
|
||||
baseScope.addConst("ARGV", ObjList(args.map { ObjString(it) }.toMutableList()))
|
||||
|
||||
@@ -19,6 +19,11 @@ package net.sergeych
|
||||
|
||||
import kotlin.system.exitProcess
|
||||
|
||||
// Allow tests to override JVM exit behavior without terminating the whole VM.
|
||||
// In production, this points to exitProcess; tests can replace it to throw.
|
||||
@PublishedApi
|
||||
internal var jvmExitImpl: (Int) -> Nothing = { code -> exitProcess(code) }
|
||||
|
||||
actual fun exit(code: Int) {
|
||||
exitProcess(code)
|
||||
jvmExitImpl(code)
|
||||
}
|
||||
@@ -0,0 +1,134 @@
|
||||
/*
|
||||
* Copyright 2025 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_cli
|
||||
|
||||
import net.sergeych.jvmExitImpl
|
||||
import net.sergeych.runMain
|
||||
import org.junit.After
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import java.io.ByteArrayOutputStream
|
||||
import java.io.PrintStream
|
||||
import java.nio.file.Files
|
||||
import java.nio.file.Path
|
||||
|
||||
class CliFmtJvmTest {
|
||||
private val originalOut: PrintStream = System.out
|
||||
private val originalErr: PrintStream = System.err
|
||||
|
||||
private class TestExit(val code: Int) : RuntimeException()
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
// Make exit() throw in tests so we can assert the code
|
||||
jvmExitImpl = { code -> throw TestExit(code) }
|
||||
}
|
||||
|
||||
@After
|
||||
fun tearDown() {
|
||||
System.setOut(originalOut)
|
||||
System.setErr(originalErr)
|
||||
// restore default exit behavior for safety
|
||||
jvmExitImpl = { code -> kotlin.system.exitProcess(code) }
|
||||
}
|
||||
|
||||
private data class CliResult(val out: String, val err: String, val exitCode: Int?)
|
||||
|
||||
private fun runCli(vararg args: String): CliResult {
|
||||
val outBuf = ByteArrayOutputStream()
|
||||
val errBuf = ByteArrayOutputStream()
|
||||
System.setOut(PrintStream(outBuf, true, Charsets.UTF_8))
|
||||
System.setErr(PrintStream(errBuf, true, Charsets.UTF_8))
|
||||
|
||||
var exitCode: Int? = null
|
||||
try {
|
||||
runMain(arrayOf(*args))
|
||||
} catch (e: TestExit) {
|
||||
exitCode = e.code
|
||||
} finally {
|
||||
System.out.flush()
|
||||
System.err.flush()
|
||||
}
|
||||
return CliResult(outBuf.toString("UTF-8"), errBuf.toString("UTF-8"), exitCode)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun fmtDoesNotPrintRootHelp() {
|
||||
val tmp: Path = Files.createTempFile("lyng_fmt_", ".lyng")
|
||||
try {
|
||||
Files.writeString(tmp, "println(1)\n")
|
||||
val r = runCli("fmt", tmp.toString())
|
||||
// Root help banner should not appear
|
||||
assertFalse(r.out.contains("The Lyng script language interpreter"))
|
||||
// Should output formatted content (stdout default)
|
||||
assertTrue("Expected some output", r.out.isNotBlank())
|
||||
} finally {
|
||||
Files.deleteIfExists(tmp)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun fmtCheckAndInPlaceAreMutuallyExclusive() {
|
||||
val r = runCli("fmt", "--check", "--in-place", "nonexistent.lyng")
|
||||
// Should exit with code 1 and print an error
|
||||
assertTrue("Expected exit code 1", r.exitCode == 1)
|
||||
assertTrue(r.out.contains("cannot be used together"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun fmtMultipleFilesPrintsHeaders() {
|
||||
val tmp1: Path = Files.createTempFile("lyng_fmt_", ".lyng")
|
||||
val tmp2: Path = Files.createTempFile("lyng_fmt_", ".lyng")
|
||||
try {
|
||||
Files.writeString(tmp1, "println(1)\n")
|
||||
Files.writeString(tmp2, "println(2)\n")
|
||||
val r = runCli("fmt", tmp1.toString(), tmp2.toString())
|
||||
assertTrue(r.out.contains("--- ${tmp1.toString()} ---"))
|
||||
assertTrue(r.out.contains("--- ${tmp2.toString()} ---"))
|
||||
} finally {
|
||||
Files.deleteIfExists(tmp1)
|
||||
Files.deleteIfExists(tmp2)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun legacyPositionalScriptExecutes() {
|
||||
// Create a tiny script and ensure it runs when passed positionally
|
||||
val tmp: Path = Files.createTempFile("lyng_script_", ".lyng")
|
||||
try {
|
||||
Files.writeString(tmp, "println(\"OK\")\n")
|
||||
val r = runCli(tmp.toString())
|
||||
assertTrue(r.out.contains("OK"))
|
||||
} finally {
|
||||
Files.deleteIfExists(tmp)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun legacyDoubleDashStopsParsingAndExecutesScript() {
|
||||
val tmp: Path = Files.createTempFile("lyng_script_", ".lyng")
|
||||
try {
|
||||
Files.writeString(tmp, "println(\"DASH\")\n")
|
||||
val r = runCli("--", tmp.toString())
|
||||
assertTrue(r.out.contains("DASH"))
|
||||
} finally {
|
||||
Files.deleteIfExists(tmp)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -38,7 +38,7 @@ kotlin {
|
||||
publishLibraryVariants("release")
|
||||
@OptIn(ExperimentalKotlinGradlePluginApi::class)
|
||||
compilerOptions {
|
||||
jvmTarget.set(JvmTarget.JVM_11)
|
||||
jvmTarget.set(JvmTarget.JVM_17)
|
||||
}
|
||||
}
|
||||
iosX64()
|
||||
@@ -71,11 +71,7 @@ kotlin {
|
||||
sourceSets {
|
||||
all {
|
||||
languageSettings.optIn("kotlin.ExperimentalUnsignedTypes")
|
||||
// languageSettings.optIn("kotlinx.coroutines.ExperimentalCoroutinesApi")
|
||||
// Correct opt-in markers for coroutines
|
||||
// languageSettings.optIn("kotlinx.coroutines.DelicateCoroutinesApi")
|
||||
// languageSettings.optIn("kotlin.contracts.ExperimentalContracts")
|
||||
// languageSettings.optIn("kotlinx.coroutines.FlowPreview")
|
||||
languageSettings.optIn("kotlin.time.ExperimentalTime")
|
||||
}
|
||||
val commonMain by getting {
|
||||
dependencies {
|
||||
@@ -115,8 +111,8 @@ android {
|
||||
minSdk = libs.versions.android.minSdk.get().toInt()
|
||||
}
|
||||
compileOptions {
|
||||
sourceCompatibility = JavaVersion.VERSION_11
|
||||
targetCompatibility = JavaVersion.VERSION_11
|
||||
sourceCompatibility = JavaVersion.VERSION_17
|
||||
targetCompatibility = JavaVersion.VERSION_17
|
||||
}
|
||||
lint {
|
||||
// Prevent Android Lint from failing the build due to Kotlin toolchain
|
||||
|
||||
@@ -163,7 +163,7 @@ private suspend fun buildFsModule(module: ModuleScope, policy: FsAccessPolicy) {
|
||||
fsGuard {
|
||||
val self = this.thisObj as ObjPath
|
||||
val m = self.ensureMetadata()
|
||||
m.createdAtMillis?.let { ObjInstant(kotlinx.datetime.Instant.fromEpochMilliseconds(it)) } ?: ObjNull
|
||||
m.createdAtMillis?.let { ObjInstant(kotlin.time.Instant.fromEpochMilliseconds(it)) } ?: ObjNull
|
||||
}
|
||||
}
|
||||
// createdAtMillis(): Int? — milliseconds since epoch or null
|
||||
@@ -322,7 +322,7 @@ private suspend fun buildFsModule(module: ModuleScope, policy: FsAccessPolicy) {
|
||||
// mkdirs(mustCreate: Bool=false)
|
||||
addFnDoc(
|
||||
name = "mkdirs",
|
||||
doc = "Create directories (like `mkdir -p`). Optional `mustCreate` enforces error if target exists.",
|
||||
doc = "Create directories (like `mkdir -p`). If `mustCreate` is true and the path already exists, the call fails. Otherwise it is a no‑op when the directory exists.",
|
||||
params = listOf(ParamDoc("mustCreate", type("lyng.Bool"))),
|
||||
moduleName = module.packageName
|
||||
) {
|
||||
@@ -336,7 +336,7 @@ private suspend fun buildFsModule(module: ModuleScope, policy: FsAccessPolicy) {
|
||||
// move(to: Path|String, overwrite: Bool=false)
|
||||
addFnDoc(
|
||||
name = "move",
|
||||
doc = "Move this path to a new location. `to` may be a `Path` or `String`. Use `overwrite` to replace existing target.",
|
||||
doc = "Move this path to a new location. `to` may be a `Path` or `String`. When `overwrite` is false and the target exists, the operation fails (provider may throw `AccessDeniedException`).",
|
||||
// types vary; keep generic description in doc
|
||||
params = listOf(ParamDoc("to"), ParamDoc("overwrite", type("lyng.Bool"))),
|
||||
moduleName = module.packageName
|
||||
@@ -352,7 +352,7 @@ private suspend fun buildFsModule(module: ModuleScope, policy: FsAccessPolicy) {
|
||||
// delete(mustExist: Bool=false, recursively: Bool=false)
|
||||
addFnDoc(
|
||||
name = "delete",
|
||||
doc = "Delete this path. Optional flags: `mustExist` and `recursively`.",
|
||||
doc = "Delete this path. `mustExist=true` causes failure if the path does not exist. `recursively=true` removes directories with their contents. Providers can throw `AccessDeniedException` on policy violations.",
|
||||
params = listOf(ParamDoc("mustExist", type("lyng.Bool")), ParamDoc("recursively", type("lyng.Bool"))),
|
||||
moduleName = module.packageName
|
||||
) {
|
||||
@@ -367,7 +367,7 @@ private suspend fun buildFsModule(module: ModuleScope, policy: FsAccessPolicy) {
|
||||
// copy(to: Path|String, overwrite: Bool=false)
|
||||
addFnDoc(
|
||||
name = "copy",
|
||||
doc = "Copy this path to a new location. `to` may be a `Path` or `String`. Use `overwrite` to replace existing target.",
|
||||
doc = "Copy this path to a new location. `to` may be a `Path` or `String`. When `overwrite` is false and the target exists, the operation fails (provider may throw `AccessDeniedException`).",
|
||||
params = listOf(ParamDoc("to"), ParamDoc("overwrite", type("lyng.Bool"))),
|
||||
moduleName = module.packageName
|
||||
) {
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
* Filesystem module builtin docs registration, located in lyngio so core library
|
||||
* does not depend on external packages. The IDEA plugin (and any other tooling)
|
||||
* may reflectively call FsBuiltinDocs.ensure() to make sure docs are registered.
|
||||
*/
|
||||
package net.sergeych.lyngio.docs
|
||||
|
||||
import net.sergeych.lyng.miniast.BuiltinDocRegistry
|
||||
import net.sergeych.lyng.miniast.ParamDoc
|
||||
import net.sergeych.lyng.miniast.TypeGenericDoc
|
||||
import net.sergeych.lyng.miniast.type
|
||||
|
||||
object FsBuiltinDocs {
|
||||
private var registered = false
|
||||
|
||||
fun ensure() {
|
||||
if (registered) return
|
||||
// Register docs immediately (not lazy) so tooling can see them without executing module builders
|
||||
BuiltinDocRegistry.module("lyng.io.fs") {
|
||||
// Class Path with a short summary
|
||||
classDoc(
|
||||
name = "Path",
|
||||
doc = "Filesystem path class. Construct with a string: `Path(\"/tmp\")`."
|
||||
) {
|
||||
// Common instance methods (subset sufficient for Quick Docs)
|
||||
method(
|
||||
name = "exists",
|
||||
doc = "Whether the path exists on the filesystem.",
|
||||
returns = type("lyng.Bool")
|
||||
)
|
||||
method(
|
||||
name = "isFile",
|
||||
doc = "Whether the path exists and is a file.",
|
||||
returns = type("lyng.Bool")
|
||||
)
|
||||
method(
|
||||
name = "isDir",
|
||||
doc = "Whether the path exists and is a directory.",
|
||||
returns = type("lyng.Bool")
|
||||
)
|
||||
method(
|
||||
name = "readUtf8",
|
||||
doc = "Read the entire file as UTF-8 string.",
|
||||
returns = type("lyng.String")
|
||||
)
|
||||
method(
|
||||
name = "writeUtf8",
|
||||
doc = "Write UTF-8 string to the file (overwrite).",
|
||||
params = listOf(ParamDoc("text", type("lyng.String")))
|
||||
)
|
||||
method(
|
||||
name = "bytes",
|
||||
doc = "Iterate file content as `Buffer` chunks.",
|
||||
params = listOf(ParamDoc("size", type("lyng.Int"))),
|
||||
returns = TypeGenericDoc(type("lyng.Iterator"), listOf(type("lyng.Buffer")))
|
||||
)
|
||||
method(
|
||||
name = "lines",
|
||||
doc = "Iterate file as lines of text.",
|
||||
returns = TypeGenericDoc(type("lyng.Iterator"), listOf(type("lyng.String")))
|
||||
)
|
||||
}
|
||||
|
||||
// Top-level exported constants
|
||||
valDoc(
|
||||
name = "Path",
|
||||
doc = "Filesystem path class. Construct with a string: `Path(\"/tmp\")`.",
|
||||
type = type("Path")
|
||||
)
|
||||
valDoc(
|
||||
name = "Paths",
|
||||
doc = "Alias of `Path` for those who prefer plural form.",
|
||||
type = type("Path")
|
||||
)
|
||||
}
|
||||
registered = true
|
||||
}
|
||||
}
|
||||
+128
-4
@@ -21,7 +21,7 @@ import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
|
||||
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
|
||||
|
||||
group = "net.sergeych"
|
||||
version = "1.0.4-SNAPSHOT"
|
||||
version = "1.1.0-SNAPSHOT"
|
||||
|
||||
// Removed legacy buildscript classpath declarations; plugins are applied via the plugins DSL below
|
||||
|
||||
@@ -51,7 +51,7 @@ kotlin {
|
||||
publishLibraryVariants("release")
|
||||
@OptIn(ExperimentalKotlinGradlePluginApi::class)
|
||||
compilerOptions {
|
||||
jvmTarget.set(JvmTarget.JVM_11)
|
||||
jvmTarget.set(JvmTarget.JVM_17)
|
||||
}
|
||||
}
|
||||
iosX64()
|
||||
@@ -89,16 +89,16 @@ kotlin {
|
||||
languageSettings.optIn("kotlinx.coroutines.DelicateCoroutinesApi")
|
||||
languageSettings.optIn("kotlin.contracts.ExperimentalContracts")
|
||||
languageSettings.optIn("kotlinx.coroutines.FlowPreview")
|
||||
languageSettings.optIn("kotlin.time.ExperimentalTime")
|
||||
}
|
||||
|
||||
val commonMain by getting {
|
||||
kotlin.srcDir("$buildDir/generated/buildConfig/commonMain/kotlin")
|
||||
dependencies {
|
||||
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.8.1")
|
||||
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.9.0")
|
||||
//put your multiplatform dependencies here
|
||||
api(libs.kotlinx.coroutines.core)
|
||||
api(libs.mp.bintools)
|
||||
api("net.sergeych:mp_stools:1.5.2")
|
||||
}
|
||||
}
|
||||
val commonTest by getting {
|
||||
@@ -107,9 +107,133 @@ kotlin {
|
||||
implementation(libs.kotlinx.coroutines.test)
|
||||
}
|
||||
}
|
||||
val jvmTest by getting {
|
||||
dependencies {
|
||||
// Allow tests to load external docs like lyng.io.fs via registrar
|
||||
implementation(project(":lyngio"))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
android {
|
||||
namespace = "net.sergeych.lynglib"
|
||||
compileSdk = libs.versions.android.compileSdk.get().toInt()
|
||||
defaultConfig {
|
||||
minSdk = libs.versions.android.minSdk.get().toInt()
|
||||
}
|
||||
compileOptions {
|
||||
sourceCompatibility = JavaVersion.VERSION_17
|
||||
targetCompatibility = JavaVersion.VERSION_17
|
||||
}
|
||||
}
|
||||
|
||||
// ---- Build-time generation of stdlib text from .lyng files into a Kotlin constant ----
|
||||
// Implemented as a proper task type compatible with Gradle Configuration Cache
|
||||
|
||||
abstract class GenerateLyngStdlib : DefaultTask() {
|
||||
@get:InputDirectory
|
||||
@get:PathSensitive(PathSensitivity.RELATIVE)
|
||||
abstract val sourceDir: DirectoryProperty
|
||||
|
||||
@get:OutputDirectory
|
||||
abstract val outputDir: DirectoryProperty
|
||||
|
||||
@TaskAction
|
||||
fun generate() {
|
||||
val targetPkg = "net.sergeych.lyng.stdlib_included"
|
||||
val pkgPath = targetPkg.replace('.', '/')
|
||||
val outBase = outputDir.get().asFile
|
||||
val targetDir = outBase.resolve(pkgPath)
|
||||
targetDir.mkdirs()
|
||||
|
||||
val srcDir = sourceDir.get().asFile
|
||||
val files = srcDir.walkTopDown()
|
||||
.filter { it.isFile && it.extension == "lyng" }
|
||||
.sortedBy { it.name }
|
||||
.toList()
|
||||
|
||||
val content = if (files.isEmpty()) "" else buildString {
|
||||
files.forEachIndexed { idx, f ->
|
||||
val text = f.readText()
|
||||
if (idx > 0) append("\n\n")
|
||||
append(text)
|
||||
}
|
||||
}
|
||||
|
||||
fun escapeForQuoted(s: String): String = buildString {
|
||||
for (ch in s) when (ch) {
|
||||
'\\' -> append("\\\\")
|
||||
'"' -> append("\\\"")
|
||||
'\n' -> append("\\n")
|
||||
'\r' -> {}
|
||||
'\t' -> append("\\t")
|
||||
else -> append(ch)
|
||||
}
|
||||
}
|
||||
val body = escapeForQuoted(content)
|
||||
|
||||
val sb = StringBuilder()
|
||||
sb.append("package ").append(targetPkg).append("\n\n")
|
||||
sb.append("@Suppress(\"Unused\", \"MemberVisibilityCanBePrivate\")\n")
|
||||
sb.append("internal val rootLyng = \"")
|
||||
sb.append(body)
|
||||
sb.append("\"\n")
|
||||
|
||||
targetDir.resolve("root_lyng.generated.kt").writeText(sb.toString())
|
||||
}
|
||||
}
|
||||
|
||||
// The .lyng source of the stdlib lives here (module-relative path):
|
||||
val lyngStdlibDir = layout.projectDirectory.dir("stdlib/lyng")
|
||||
// The generated Kotlin source will be placed here and added to commonMain sources:
|
||||
val generatedLyngStdlibDir = layout.buildDirectory.dir("generated/source/lyngStdlib/commonMain/kotlin")
|
||||
|
||||
val generateLyngStdlib by tasks.registering(GenerateLyngStdlib::class) {
|
||||
group = "build"
|
||||
description = "Generate Kotlin source with embedded lyng stdlib text"
|
||||
sourceDir.set(lyngStdlibDir)
|
||||
outputDir.set(generatedLyngStdlibDir)
|
||||
}
|
||||
|
||||
// Add the generated directory to commonMain sources
|
||||
kotlin.sourceSets.named("commonMain") {
|
||||
kotlin.srcDir(generatedLyngStdlibDir)
|
||||
}
|
||||
|
||||
// Ensure ALL Kotlin compilations (all targets/variants) depend on the generator
|
||||
kotlin.targets.configureEach {
|
||||
compilations.configureEach {
|
||||
compileTaskProvider.configure {
|
||||
dependsOn(generateLyngStdlib)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Ensure any SourcesJar tasks (for all targets/variants) are properly wired to the generator
|
||||
tasks.withType<Jar>().configureEach {
|
||||
if (name == "sourcesJar" || name.endsWith("SourcesJar")) {
|
||||
// Declare both dependency and inputs to satisfy Gradle validation and up-to-date checks
|
||||
dependsOn(generateLyngStdlib)
|
||||
inputs.dir(generatedLyngStdlibDir)
|
||||
}
|
||||
}
|
||||
|
||||
// Extra safety: in case the SourcesJar task is not of type Jar (AGP/MPP variations),
|
||||
// wire it up by name as well. This guarantees the dependency even if the concrete type differs.
|
||||
tasks.configureEach {
|
||||
if (name == "androidReleaseSourcesJar" || name == "sourcesJar" || name.endsWith("SourcesJar")) {
|
||||
dependsOn(generateLyngStdlib)
|
||||
inputs.dir(generatedLyngStdlibDir)
|
||||
}
|
||||
}
|
||||
|
||||
// Be explicit for the aggregate metadata sources task too
|
||||
tasks.named("sourcesJar").configure {
|
||||
dependsOn(generateLyngStdlib)
|
||||
inputs.dir(generatedLyngStdlibDir)
|
||||
}
|
||||
|
||||
android {
|
||||
namespace = "org.jetbrains.kotlinx.multiplatform.library.template"
|
||||
compileSdk = libs.versions.android.compileSdk.get().toInt()
|
||||
|
||||
@@ -39,12 +39,18 @@ actual object ScopePool {
|
||||
actual fun borrow(parent: Scope, args: Arguments, pos: Pos, thisObj: Obj): Scope {
|
||||
val pool = pool()
|
||||
val s = if (pool.isNotEmpty()) pool.removeLast() else Scope(parent, args, pos, thisObj)
|
||||
if (s.parent !== parent || s.args !== args || s.pos !== pos || s.thisObj !== thisObj) {
|
||||
s.resetForReuse(parent, args, pos, thisObj)
|
||||
} else {
|
||||
s.frameId = nextFrameId()
|
||||
return try {
|
||||
if (s.parent !== parent || s.args !== args || s.pos !== pos || s.thisObj !== thisObj) {
|
||||
s.resetForReuse(parent, args, pos, thisObj)
|
||||
} else {
|
||||
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 s
|
||||
}
|
||||
|
||||
actual fun release(scope: Scope) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
|
||||
* 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.
|
||||
@@ -49,12 +49,14 @@ data class ArgsDeclaration(val params: List<Item>, val endTokenType: Token.Type)
|
||||
arguments: Arguments = scope.args,
|
||||
defaultAccessType: AccessType = AccessType.Var,
|
||||
defaultVisibility: Visibility = Visibility.Public,
|
||||
declaringClass: net.sergeych.lyng.obj.ObjClass? = scope.currentClassCtx
|
||||
) {
|
||||
fun assign(a: Item, value: Obj) {
|
||||
scope.addItem(a.name, (a.accessType ?: defaultAccessType).isMutable,
|
||||
value.byValueCopy(),
|
||||
a.visibility ?: defaultVisibility,
|
||||
recordType = ObjRecord.Type.Argument)
|
||||
recordType = ObjRecord.Type.Argument,
|
||||
declaringClass = declaringClass)
|
||||
}
|
||||
|
||||
// Prepare positional args and parameter count, handle tail-block binding
|
||||
@@ -136,7 +138,7 @@ data class ArgsDeclaration(val params: List<Item>, val endTokenType: Token.Type)
|
||||
} else {
|
||||
val value = if (hp < callArgs.size) callArgs[hp++]
|
||||
else a.defaultValue?.execute(scope)
|
||||
?: scope.raiseIllegalArgument("too few arguments for the call")
|
||||
?: scope.raiseIllegalArgument("too few arguments for the call (${arguments.list})")
|
||||
assign(a, value)
|
||||
}
|
||||
i++
|
||||
|
||||
@@ -38,35 +38,88 @@ class ClosureScope(val callScope: Scope, val closureScope: Scope) :
|
||||
}
|
||||
|
||||
override fun get(name: String): ObjRecord? {
|
||||
// Fast-path built-ins
|
||||
if (name == "this") return thisObj.asReadonly
|
||||
|
||||
// Priority:
|
||||
// 1) Locals and arguments declared in this lambda frame (including values defined before suspension)
|
||||
// 2) Instance/class members of the captured receiver (`closureScope.thisObj`), e.g., fields like `coll`, `factor`
|
||||
// 3) Symbols from the captured closure scope (its locals and parents)
|
||||
// 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) Fallback to the standard chain (this frame -> parent (callScope) -> class members)
|
||||
// 5) Symbols from the caller chain (locals/parents), ignoring nested ClosureScope overrides
|
||||
// 6) Special fallback for module pseudo-symbols (e.g., __PACKAGE__)
|
||||
|
||||
// First, prefer locals/arguments bound in this frame
|
||||
// 1) Locals/arguments in this closure frame
|
||||
super.objects[name]?.let { return it }
|
||||
super.localBindings[name]?.let { return it }
|
||||
|
||||
// Prefer instance fields/methods declared on the captured receiver:
|
||||
// First, resolve real instance fields stored in the instance scope (constructor vars like `coll`, `factor`)
|
||||
// 2) Members on the captured receiver instance
|
||||
(closureScope.thisObj as? net.sergeych.lyng.obj.ObjInstance)
|
||||
?.instanceScope
|
||||
?.objects
|
||||
?.get(name)
|
||||
?.let { return it }
|
||||
?.let { rec ->
|
||||
if (canAccessMember(rec.visibility, rec.declaringClass, currentClassCtx)) return rec
|
||||
}
|
||||
findExtension(closureScope.thisObj.objClass, name)?.let { return it }
|
||||
closureScope.thisObj.objClass.getInstanceMemberOrNull(name)?.let { rec ->
|
||||
if (canAccessMember(rec.visibility, rec.declaringClass, currentClassCtx)) return rec
|
||||
}
|
||||
|
||||
// Then, try class-declared members (methods/properties declared in the class body)
|
||||
closureScope.thisObj.objClass.getInstanceMemberOrNull(name)?.let { return it }
|
||||
// 3) Closure scope chain (locals/parents + members), ignore ClosureScope overrides to prevent recursion
|
||||
closureScope.chainLookupWithMembers(name, currentClassCtx)?.let { return it }
|
||||
|
||||
// Then delegate to the full closure scope chain (locals, parents, etc.)
|
||||
closureScope.get(name)?.let { return it }
|
||||
// 4) Caller `this` members
|
||||
findExtension(callScope.thisObj.objClass, name)?.let { return it }
|
||||
callScope.thisObj.objClass.getInstanceMemberOrNull(name)?.let { rec ->
|
||||
if (canAccessMember(rec.visibility, rec.declaringClass, currentClassCtx)) return rec
|
||||
}
|
||||
|
||||
// Allow resolving instance members of the caller's `this` (e.g., FlowBuilder.emit)
|
||||
callScope.thisObj.objClass.getInstanceMemberOrNull(name)?.let { return it }
|
||||
// 5) Caller chain (locals/parents + members)
|
||||
callScope.chainLookupWithMembers(name, currentClassCtx)?.let { return it }
|
||||
|
||||
// Fallback to the standard lookup chain: this frame -> parent (callScope) -> class members
|
||||
return super.get(name)
|
||||
// 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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
|
||||
* 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.
|
||||
@@ -17,7 +17,6 @@
|
||||
|
||||
package net.sergeych.lyng
|
||||
|
||||
import ObjEnumClass
|
||||
import net.sergeych.lyng.Compiler.Companion.compile
|
||||
import net.sergeych.lyng.miniast.*
|
||||
import net.sergeych.lyng.obj.*
|
||||
@@ -674,12 +673,19 @@ class Compiler(
|
||||
Token.Type.RBRACKET -> return entries
|
||||
Token.Type.ELLIPSIS -> {
|
||||
parseExpressionLevel()?.let { entries += ListEntry.Spread(it) }
|
||||
?: throw ScriptError(t.pos, "spread element must have an expression")
|
||||
}
|
||||
|
||||
else -> {
|
||||
cc.previous()
|
||||
parseExpressionLevel()?.let { entries += ListEntry.Element(it) }
|
||||
?: throw ScriptError(t.pos, "invalid list literal: expecting expression")
|
||||
parseExpressionLevel()?.let { expr ->
|
||||
if (cc.current().type == Token.Type.ELLIPSIS) {
|
||||
cc.next()
|
||||
entries += ListEntry.Spread(expr)
|
||||
} else {
|
||||
entries += ListEntry.Element(expr)
|
||||
}
|
||||
} ?: throw ScriptError(t.pos, "invalid list literal: expecting expression")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -832,6 +838,7 @@ class Compiler(
|
||||
}
|
||||
|
||||
Token.Type.NEWLINE -> {}
|
||||
Token.Type.MULTILINE_COMMENT, Token.Type.SINLGE_LINE_COMMENT -> {}
|
||||
|
||||
Token.Type.ID -> {
|
||||
// visibility
|
||||
@@ -1028,6 +1035,12 @@ class Compiler(
|
||||
if (t2.type == Token.Type.COLON) {
|
||||
// name: expr
|
||||
val name = t1.value
|
||||
// Check for shorthand: name: (comma or rparen)
|
||||
val next = cc.peekNextNonWhitespace()
|
||||
if (next.type == Token.Type.COMMA || next.type == Token.Type.RPAREN) {
|
||||
val localVar = LocalVarRef(name, t1.pos)
|
||||
return ParsedArgument(statement(t1.pos) { localVar.get(it).value }, t1.pos, isSplat = false, name = name)
|
||||
}
|
||||
val rhs = parseExpression() ?: t2.raiseSyntax("expected expression after named argument '${name}:'")
|
||||
return ParsedArgument(rhs, t1.pos, isSplat = false, name = name)
|
||||
}
|
||||
@@ -1043,7 +1056,7 @@ class Compiler(
|
||||
}
|
||||
|
||||
Token.Type.ELLIPSIS -> {
|
||||
parseStatement()?.let { args += ParsedArgument(it, t.pos, isSplat = true) }
|
||||
parseExpression()?.let { args += ParsedArgument(it, t.pos, isSplat = true) }
|
||||
?: throw ScriptError(t.pos, "Expecting arguments list")
|
||||
}
|
||||
|
||||
@@ -1094,6 +1107,12 @@ class Compiler(
|
||||
val t2 = cc.next()
|
||||
if (t2.type == Token.Type.COLON) {
|
||||
val name = t1.value
|
||||
// Check for shorthand: name: (comma or rparen)
|
||||
val next = cc.peekNextNonWhitespace()
|
||||
if (next.type == Token.Type.COMMA || next.type == Token.Type.RPAREN) {
|
||||
val localVar = LocalVarRef(name, t1.pos)
|
||||
return ParsedArgument(statement(t1.pos) { localVar.get(it).value }, t1.pos, isSplat = false, name = name)
|
||||
}
|
||||
val rhs = parseExpression() ?: t2.raiseSyntax("expected expression after named argument '${name}:'")
|
||||
return ParsedArgument(rhs, t1.pos, isSplat = false, name = name)
|
||||
}
|
||||
@@ -1109,7 +1128,7 @@ class Compiler(
|
||||
}
|
||||
|
||||
Token.Type.ELLIPSIS -> {
|
||||
parseStatement()?.let { args += ParsedArgument(it, t.pos, isSplat = true) }
|
||||
parseExpression()?.let { args += ParsedArgument(it, t.pos, isSplat = true) }
|
||||
?: throw ScriptError(t.pos, "Expecting arguments list")
|
||||
}
|
||||
|
||||
@@ -1215,12 +1234,12 @@ class Compiler(
|
||||
return when (t.type) {
|
||||
Token.Type.INT, Token.Type.HEX -> {
|
||||
val n = t.value.replace("_", "").toLong(if (t.type == Token.Type.HEX) 16 else 10)
|
||||
if (isPlus) ObjInt(n) else ObjInt(-n)
|
||||
if (isPlus) ObjInt.of(n) else ObjInt.of(-n)
|
||||
}
|
||||
|
||||
Token.Type.REAL -> {
|
||||
val d = t.value.toDouble()
|
||||
if (isPlus) ObjReal(d) else ObjReal(-d)
|
||||
if (isPlus) ObjReal.of(d) else ObjReal.of(-d)
|
||||
}
|
||||
|
||||
else -> {
|
||||
@@ -1352,6 +1371,23 @@ class Compiler(
|
||||
parseClassDeclaration()
|
||||
}
|
||||
|
||||
"init" -> {
|
||||
if (codeContexts.lastOrNull() is CodeContext.ClassBody) {
|
||||
val block = parseBlock()
|
||||
lastParsedBlockRange?.let { range ->
|
||||
miniSink?.onInitDecl(MiniInitDecl(MiniRange(id.pos, range.end), id.pos))
|
||||
}
|
||||
val initStmt = statement(id.pos) { scp ->
|
||||
block.execute(scp)
|
||||
ObjVoid
|
||||
}
|
||||
statement {
|
||||
currentClassCtx?.instanceInitializers?.add(initStmt)
|
||||
ObjVoid
|
||||
}
|
||||
} else null
|
||||
}
|
||||
|
||||
"enum" -> {
|
||||
pendingDeclStart = id.pos
|
||||
pendingDeclDoc = consumePendingDoc()
|
||||
@@ -1713,6 +1749,9 @@ class Compiler(
|
||||
}
|
||||
|
||||
private fun parseEnumDeclaration(): Statement {
|
||||
val startPos = pendingDeclStart ?: cc.currentPos()
|
||||
val doc = pendingDeclDoc ?: consumePendingDoc()
|
||||
pendingDeclDoc = null
|
||||
val nameToken = cc.requireToken(Token.Type.ID)
|
||||
// so far only simplest enums:
|
||||
val names = mutableListOf<String>()
|
||||
@@ -1740,6 +1779,16 @@ class Compiler(
|
||||
}
|
||||
} while (true)
|
||||
|
||||
miniSink?.onEnumDecl(
|
||||
MiniEnumDecl(
|
||||
range = MiniRange(startPos, cc.currentPos()),
|
||||
name = nameToken.value,
|
||||
entries = names,
|
||||
doc = doc,
|
||||
nameStart = nameToken.pos
|
||||
)
|
||||
)
|
||||
|
||||
return statement {
|
||||
ObjEnumClass.createSimpleEnum(nameToken.value, names).also {
|
||||
addItem(nameToken.value, false, it, recordType = ObjRecord.Type.Enum)
|
||||
@@ -1753,7 +1802,7 @@ class Compiler(
|
||||
val constructorArgsDeclaration =
|
||||
if (cc.skipTokenOfType(Token.Type.LPAREN, isOptional = true))
|
||||
parseArgsDeclaration(isClassDeclaration = true)
|
||||
else null
|
||||
else ArgsDeclaration(emptyList(), Token.Type.RPAREN)
|
||||
|
||||
if (constructorArgsDeclaration != null && constructorArgsDeclaration.endTokenType != Token.Type.RPAREN)
|
||||
throw ScriptError(
|
||||
@@ -1790,7 +1839,9 @@ class Compiler(
|
||||
if (next.type == Token.Type.LBRACE) {
|
||||
// parse body
|
||||
val bodyStart = next.pos
|
||||
val st = parseScript()
|
||||
val st = withLocalNames(constructorArgsDeclaration?.params?.map { it.name }?.toSet() ?: emptySet()) {
|
||||
parseScript()
|
||||
}
|
||||
val rbTok = cc.next()
|
||||
if (rbTok.type != Token.Type.RBRACE) throw ScriptError(rbTok.pos, "unbalanced braces in class body")
|
||||
classBodyRange = MiniRange(bodyStart, rbTok.pos)
|
||||
@@ -1840,7 +1891,7 @@ class Compiler(
|
||||
// create class
|
||||
val className = nameToken.value
|
||||
|
||||
// @Suppress("UNUSED_VARIABLE") val defaultAccess = if (isStruct) AccessType.Var else AccessType.Initialization
|
||||
// @Suppress("UNUSED_VARIABLE") val defaultAccess = if (isStruct) AccessType.Variable else AccessType.Initialization
|
||||
// @Suppress("UNUSED_VARIABLE") val defaultVisibility = Visibility.Public
|
||||
|
||||
// create instance constructor
|
||||
@@ -1917,7 +1968,7 @@ class Compiler(
|
||||
while (cc.hasPrevious() && cnt < maxDepth) {
|
||||
val t = cc.previous()
|
||||
cnt++
|
||||
if (t.type == Token.Type.LABEL) {
|
||||
if (t.type == Token.Type.LABEL || t.type == Token.Type.ATLABEL) {
|
||||
found = t.value
|
||||
break
|
||||
}
|
||||
@@ -1948,7 +1999,8 @@ class Compiler(
|
||||
val namesForLoop = (currentLocalNames?.toSet() ?: emptySet()) + tVar.value
|
||||
val (canBreak, body, elseStatement) = withLocalNames(namesForLoop) {
|
||||
val loopParsed = cc.parseLoop {
|
||||
parseStatement() ?: throw ScriptError(start, "Bad for statement: expected loop body")
|
||||
if (cc.current().type == Token.Type.LBRACE) parseBlock()
|
||||
else parseStatement() ?: throw ScriptError(start, "Bad for statement: expected loop body")
|
||||
}
|
||||
// possible else clause
|
||||
cc.skipTokenOfType(Token.Type.NEWLINE, isOptional = true)
|
||||
@@ -2000,7 +2052,7 @@ class Compiler(
|
||||
var breakCaught = false
|
||||
|
||||
if (size > 0) {
|
||||
var current = runCatching { sourceObj.getAt(forContext, ObjInt(0)) }
|
||||
var current = runCatching { sourceObj.getAt(forContext, ObjInt.of(0)) }
|
||||
.getOrElse {
|
||||
throw ScriptError(
|
||||
tOp.pos,
|
||||
@@ -2011,23 +2063,21 @@ class Compiler(
|
||||
var index = 0
|
||||
while (true) {
|
||||
loopSO.value = current
|
||||
if (canBreak) {
|
||||
try {
|
||||
result = body.execute(forContext)
|
||||
} catch (lbe: LoopBreakContinueException) {
|
||||
if (lbe.label == label || lbe.label == null) {
|
||||
breakCaught = true
|
||||
if (lbe.doContinue) continue
|
||||
else {
|
||||
result = lbe.result
|
||||
break
|
||||
}
|
||||
} else
|
||||
throw lbe
|
||||
}
|
||||
} else result = body.execute(forContext)
|
||||
try {
|
||||
result = body.execute(forContext)
|
||||
} catch (lbe: LoopBreakContinueException) {
|
||||
if (lbe.label == label || lbe.label == null) {
|
||||
breakCaught = true
|
||||
if (lbe.doContinue) continue
|
||||
else {
|
||||
result = lbe.result
|
||||
break
|
||||
}
|
||||
} else
|
||||
throw lbe
|
||||
}
|
||||
if (++index >= size) break
|
||||
current = sourceObj.getAt(forContext, ObjInt(index.toLong()))
|
||||
current = sourceObj.getAt(forContext, ObjInt.of(index.toLong()))
|
||||
}
|
||||
}
|
||||
if (!breakCaught && elseStatement != null) {
|
||||
@@ -2047,11 +2097,9 @@ class Compiler(
|
||||
body: Statement, elseStatement: Statement?, label: String?, catchBreak: Boolean
|
||||
): Obj {
|
||||
var result: Obj = ObjVoid
|
||||
val iVar = ObjInt(0)
|
||||
loopVar.value = iVar
|
||||
if (catchBreak) {
|
||||
for (i in start..<end) {
|
||||
iVar.value = i//.toLong()
|
||||
loopVar.value = ObjInt.of(i)
|
||||
try {
|
||||
result = body.execute(forScope)
|
||||
} catch (lbe: LoopBreakContinueException) {
|
||||
@@ -2064,7 +2112,7 @@ class Compiler(
|
||||
}
|
||||
} else {
|
||||
for (i in start..<end) {
|
||||
iVar.value = i
|
||||
loopVar.value = ObjInt.of(i)
|
||||
result = body.execute(forScope)
|
||||
}
|
||||
}
|
||||
@@ -2076,57 +2124,50 @@ class Compiler(
|
||||
body: Statement, elseStatement: Statement?, label: String?,
|
||||
catchBreak: Boolean
|
||||
): Obj {
|
||||
val iterObj = sourceObj.invokeInstanceMethod(forScope, "iterator")
|
||||
var result: Obj = ObjVoid
|
||||
var completedNaturally = false
|
||||
try {
|
||||
while (iterObj.invokeInstanceMethod(forScope, "hasNext").toBool()) {
|
||||
if (catchBreak)
|
||||
try {
|
||||
loopVar.value = iterObj.invokeInstanceMethod(forScope, "next")
|
||||
result = body.execute(forScope)
|
||||
} catch (lbe: LoopBreakContinueException) {
|
||||
if (lbe.label == label || lbe.label == null) {
|
||||
if (lbe.doContinue) continue
|
||||
// premature finish, will trigger cancel in finally
|
||||
return lbe.result
|
||||
}
|
||||
throw lbe
|
||||
}
|
||||
else {
|
||||
loopVar.value = iterObj.invokeInstanceMethod(forScope, "next")
|
||||
var breakCaught = false
|
||||
sourceObj.enumerate(forScope) { item ->
|
||||
loopVar.value = item
|
||||
if (catchBreak) {
|
||||
try {
|
||||
result = body.execute(forScope)
|
||||
true
|
||||
} catch (lbe: LoopBreakContinueException) {
|
||||
if (lbe.label == label || lbe.label == null) {
|
||||
if (lbe.doContinue) true
|
||||
else {
|
||||
result = lbe.result
|
||||
breakCaught = true
|
||||
false
|
||||
}
|
||||
} else
|
||||
throw lbe
|
||||
}
|
||||
}
|
||||
completedNaturally = true
|
||||
return elseStatement?.execute(forScope) ?: result
|
||||
} finally {
|
||||
if (!completedNaturally) {
|
||||
// Best-effort cancellation on premature termination
|
||||
runCatching {
|
||||
iterObj.invokeInstanceMethod(forScope, "cancelIteration") { ObjVoid }
|
||||
}
|
||||
} else {
|
||||
result = body.execute(forScope)
|
||||
true
|
||||
}
|
||||
}
|
||||
return if (!breakCaught && elseStatement != null) {
|
||||
elseStatement.execute(forScope)
|
||||
} else result
|
||||
}
|
||||
|
||||
@Suppress("UNUSED_VARIABLE")
|
||||
private suspend fun parseDoWhileStatement(): Statement {
|
||||
val label = getLabel()?.also { cc.labels += it }
|
||||
val (breakFound, body) = cc.parseLoop {
|
||||
parseStatement() ?: throw ScriptError(cc.currentPos(), "Bad while statement: expected statement")
|
||||
val (canBreak, body) = cc.parseLoop {
|
||||
parseStatement() ?: throw ScriptError(cc.currentPos(), "Bad do-while statement: expected body statement")
|
||||
}
|
||||
label?.also { cc.labels -= it }
|
||||
|
||||
cc.skipTokens(Token.Type.NEWLINE)
|
||||
cc.skipWsTokens()
|
||||
val tWhile = cc.next()
|
||||
if (tWhile.type != Token.Type.ID || tWhile.value != "while")
|
||||
throw ScriptError(tWhile.pos, "Expected 'while' after do body")
|
||||
|
||||
val t = cc.next()
|
||||
if (t.type != Token.Type.ID && t.value != "while")
|
||||
cc.skipTokenOfType(Token.Type.LPAREN, "expected '(' here")
|
||||
|
||||
val conditionStart = ensureLparen()
|
||||
val condition =
|
||||
parseExpression() ?: throw ScriptError(conditionStart, "Bad while statement: expected expression")
|
||||
ensureLparen()
|
||||
val condition = parseExpression() ?: throw ScriptError(cc.currentPos(), "Expected condition after 'while'")
|
||||
ensureRparen()
|
||||
|
||||
cc.skipTokenOfType(Token.Type.NEWLINE, isOptional = true)
|
||||
@@ -2137,13 +2178,11 @@ class Compiler(
|
||||
null
|
||||
}
|
||||
|
||||
|
||||
return statement(body.pos) {
|
||||
var wasBroken = false
|
||||
var result: Obj = ObjVoid
|
||||
lateinit var doScope: Scope
|
||||
while (true) {
|
||||
doScope = it.createChildScope().apply { skipScopeCreation = true }
|
||||
val doScope = it.createChildScope().apply { skipScopeCreation = true }
|
||||
try {
|
||||
result = body.execute(doScope)
|
||||
} catch (e: LoopBreakContinueException) {
|
||||
@@ -2155,11 +2194,12 @@ class Compiler(
|
||||
}
|
||||
// for continue: just fall through to condition check below
|
||||
} else {
|
||||
// Not our label, let outer loops handle it
|
||||
throw e
|
||||
}
|
||||
}
|
||||
if (!condition.execute(doScope).toBool()) break
|
||||
if (!condition.execute(doScope).toBool()) {
|
||||
break
|
||||
}
|
||||
}
|
||||
if (!wasBroken) elseStatement?.let { s -> result = s.execute(it) }
|
||||
result
|
||||
@@ -2173,7 +2213,10 @@ class Compiler(
|
||||
parseExpression() ?: throw ScriptError(start, "Bad while statement: expected expression")
|
||||
ensureRparen()
|
||||
|
||||
val body = parseStatement() ?: throw ScriptError(start, "Bad while statement: expected statement")
|
||||
val (canBreak, body) = cc.parseLoop {
|
||||
if (cc.current().type == Token.Type.LBRACE) parseBlock()
|
||||
else parseStatement() ?: throw ScriptError(start, "Bad while statement: expected statement")
|
||||
}
|
||||
label?.also { cc.labels -= it }
|
||||
|
||||
cc.skipTokenOfType(Token.Type.NEWLINE, isOptional = true)
|
||||
@@ -2187,21 +2230,23 @@ class Compiler(
|
||||
var result: Obj = ObjVoid
|
||||
var wasBroken = false
|
||||
while (condition.execute(it).toBool()) {
|
||||
try {
|
||||
// we don't need to create new context here: if body is a block,
|
||||
// parse block will do it, otherwise single statement doesn't need it:
|
||||
result = body.execute(it)
|
||||
} catch (lbe: LoopBreakContinueException) {
|
||||
if (lbe.label == label || lbe.label == null) {
|
||||
if (lbe.doContinue) continue
|
||||
else {
|
||||
result = lbe.result
|
||||
wasBroken = true
|
||||
break
|
||||
}
|
||||
} else
|
||||
throw lbe
|
||||
}
|
||||
val loopScope = it.createChildScope()
|
||||
if (canBreak) {
|
||||
try {
|
||||
result = body.execute(loopScope)
|
||||
} catch (lbe: LoopBreakContinueException) {
|
||||
if (lbe.label == label || lbe.label == null) {
|
||||
if (lbe.doContinue) continue
|
||||
else {
|
||||
result = lbe.result
|
||||
wasBroken = true
|
||||
break
|
||||
}
|
||||
} else
|
||||
throw lbe
|
||||
}
|
||||
} else
|
||||
result = body.execute(loopScope)
|
||||
}
|
||||
if (!wasBroken) elseStatement?.let { s -> result = s.execute(it) }
|
||||
result
|
||||
@@ -2226,7 +2271,12 @@ class Compiler(
|
||||
cc.previous()
|
||||
val resultExpr = if (t.pos.line == start.line && (!t.isComment &&
|
||||
t.type != Token.Type.SEMICOLON &&
|
||||
t.type != Token.Type.NEWLINE)
|
||||
t.type != Token.Type.NEWLINE &&
|
||||
t.type != Token.Type.RBRACE &&
|
||||
t.type != Token.Type.RPAREN &&
|
||||
t.type != Token.Type.RBRACKET &&
|
||||
t.type != Token.Type.COMMA &&
|
||||
t.type != Token.Type.EOF)
|
||||
) {
|
||||
// we have something on this line, could be expression
|
||||
parseStatement()
|
||||
@@ -2328,6 +2378,7 @@ class Compiler(
|
||||
throw ScriptError(t.pos, "Expected identifier after 'fun'")
|
||||
else t.value
|
||||
var nameStartPos: Pos = t.pos
|
||||
var receiverMini: MiniTypeRef? = null
|
||||
|
||||
val annotation = lastAnnotation
|
||||
val parentContext = codeContexts.last()
|
||||
@@ -2336,6 +2387,12 @@ class Compiler(
|
||||
// Is extension?
|
||||
if (t.type == Token.Type.DOT) {
|
||||
extTypeName = name
|
||||
val receiverEnd = Pos(start.source, start.line, start.column + name.length)
|
||||
receiverMini = MiniTypeName(
|
||||
range = MiniRange(start, receiverEnd),
|
||||
segments = listOf(MiniTypeName.Segment(name, MiniRange(start, receiverEnd))),
|
||||
nullable = false
|
||||
)
|
||||
t = cc.next()
|
||||
if (t.type != Token.Type.ID)
|
||||
throw ScriptError(t.pos, "illegal extension format: expected function name")
|
||||
@@ -2379,7 +2436,8 @@ class Compiler(
|
||||
returnType = returnTypeMini,
|
||||
body = null,
|
||||
doc = declDocLocal,
|
||||
nameStart = nameStartPos
|
||||
nameStart = nameStartPos,
|
||||
receiver = receiverMini
|
||||
)
|
||||
miniSink?.onFunDecl(node)
|
||||
pendingDeclDoc = null
|
||||
@@ -2395,7 +2453,20 @@ class Compiler(
|
||||
val fnStatements = if (isExtern)
|
||||
statement { raiseError("extern function not provided: $name") }
|
||||
else
|
||||
withLocalNames(paramNames) { parseBlock() }
|
||||
withLocalNames(paramNames) {
|
||||
val next = cc.peekNextNonWhitespace()
|
||||
if (next.type == Token.Type.ASSIGN) {
|
||||
cc.skipWsTokens()
|
||||
cc.next() // consume '='
|
||||
val expr = parseExpression() ?: throw ScriptError(cc.current().pos, "Expected function body expression")
|
||||
// Shorthand function returns the expression value
|
||||
statement(expr.pos) { scope ->
|
||||
expr.execute(scope)
|
||||
}
|
||||
} else {
|
||||
parseBlock()
|
||||
}
|
||||
}
|
||||
// Capture and pop the local declarations count for this function
|
||||
val fnLocalDecls = localDeclCountStack.removeLastOrNull() ?: 0
|
||||
|
||||
@@ -2435,7 +2506,7 @@ class Compiler(
|
||||
// class extension method
|
||||
val type = context[typeName]?.value ?: context.raiseSymbolNotFound("class $typeName not found")
|
||||
if (type !is ObjClass) context.raiseClassCastError("$typeName is not the class instance")
|
||||
type.addFn(name, isOpen = true, visibility = visibility) {
|
||||
val stmt = statement {
|
||||
// ObjInstance has a fixed instance scope, so we need to build a closure
|
||||
(thisObj as? ObjInstance)?.let { i ->
|
||||
annotatedFnBody.execute(ClosureScope(this, i.instanceScope))
|
||||
@@ -2443,6 +2514,7 @@ class Compiler(
|
||||
// other classes can create one-time scope for this rare case:
|
||||
?: annotatedFnBody.execute(thisObj.autoInstanceScope(this))
|
||||
}
|
||||
context.addExtension(type, name, ObjRecord(stmt, isMutable = false, visibility = visibility, declaringClass = null))
|
||||
}
|
||||
// regular function/method
|
||||
?: run {
|
||||
@@ -2531,45 +2603,139 @@ class Compiler(
|
||||
@Suppress("UNUSED_PARAMETER") isOpen: Boolean = false,
|
||||
isStatic: Boolean = false
|
||||
): Statement {
|
||||
val nameToken = cc.next()
|
||||
val start = nameToken.pos
|
||||
if (nameToken.type != Token.Type.ID)
|
||||
throw ScriptError(nameToken.pos, "Expected identifier here")
|
||||
val name = nameToken.value
|
||||
val nextToken = cc.next()
|
||||
val start = nextToken.pos
|
||||
|
||||
if (nextToken.type == Token.Type.LBRACKET) {
|
||||
// Destructuring
|
||||
if (isStatic) throw ScriptError(start, "static destructuring is not supported")
|
||||
|
||||
val entries = parseArrayLiteral()
|
||||
val pattern = ListLiteralRef(entries)
|
||||
|
||||
// Register all names in the pattern
|
||||
pattern.forEachVariableWithPos { name, namePos ->
|
||||
declareLocalName(name)
|
||||
val declRange = MiniRange(namePos, namePos)
|
||||
val node = MiniValDecl(
|
||||
range = declRange,
|
||||
name = name,
|
||||
mutable = isMutable,
|
||||
type = null,
|
||||
initRange = null,
|
||||
doc = pendingDeclDoc,
|
||||
nameStart = namePos
|
||||
)
|
||||
miniSink?.onValDecl(node)
|
||||
}
|
||||
pendingDeclDoc = null
|
||||
|
||||
val eqToken = cc.next()
|
||||
if (eqToken.type != Token.Type.ASSIGN)
|
||||
throw ScriptError(eqToken.pos, "destructuring declaration must be initialized")
|
||||
|
||||
val initialExpression = parseStatement(true)
|
||||
?: throw ScriptError(eqToken.pos, "Expected initializer expression")
|
||||
|
||||
val names = mutableListOf<String>()
|
||||
pattern.forEachVariable { names.add(it) }
|
||||
|
||||
return statement(start) { context ->
|
||||
val value = initialExpression.execute(context)
|
||||
for (name in names) {
|
||||
context.addItem(name, true, ObjVoid, visibility)
|
||||
}
|
||||
pattern.setAt(start, context, value)
|
||||
if (!isMutable) {
|
||||
for (name in names) {
|
||||
val rec = context.objects[name]!!
|
||||
val immutableRec = rec.copy(isMutable = false)
|
||||
context.objects[name] = immutableRec
|
||||
context.localBindings[name] = immutableRec
|
||||
context.updateSlotFor(name, immutableRec)
|
||||
}
|
||||
}
|
||||
ObjVoid
|
||||
}
|
||||
}
|
||||
|
||||
if (nextToken.type != Token.Type.ID)
|
||||
throw ScriptError(nextToken.pos, "Expected identifier or [ here")
|
||||
var name = nextToken.value
|
||||
var extTypeName: String? = null
|
||||
var nameStartPos: Pos = nextToken.pos
|
||||
var receiverMini: MiniTypeRef? = null
|
||||
|
||||
if (cc.peekNextNonWhitespace().type == Token.Type.DOT) {
|
||||
cc.skipWsTokens()
|
||||
cc.next() // consume dot
|
||||
extTypeName = name
|
||||
val receiverEnd = Pos(nextToken.pos.source, nextToken.pos.line, nextToken.pos.column + name.length)
|
||||
receiverMini = MiniTypeName(
|
||||
range = MiniRange(nextToken.pos, receiverEnd),
|
||||
segments = listOf(MiniTypeName.Segment(name, MiniRange(nextToken.pos, receiverEnd))),
|
||||
nullable = false
|
||||
)
|
||||
val nameToken = cc.next()
|
||||
if (nameToken.type != Token.Type.ID)
|
||||
throw ScriptError(nameToken.pos, "Expected identifier after dot in extension declaration")
|
||||
name = nameToken.value
|
||||
nameStartPos = nameToken.pos
|
||||
}
|
||||
|
||||
// Optional explicit type annotation
|
||||
val varTypeMini: MiniTypeRef? = if (cc.current().type == Token.Type.COLON) {
|
||||
parseTypeDeclarationWithMini().second
|
||||
} else null
|
||||
|
||||
val markBeforeEq = cc.savePos()
|
||||
cc.skipWsTokens()
|
||||
val eqToken = cc.next()
|
||||
var setNull = false
|
||||
var isProperty = false
|
||||
|
||||
val declaringClassNameCaptured = (codeContexts.lastOrNull() as? CodeContext.ClassBody)?.name
|
||||
|
||||
if (declaringClassNameCaptured != null || extTypeName != null) {
|
||||
val mark = cc.savePos()
|
||||
cc.restorePos(markBeforeEq)
|
||||
cc.skipWsTokens()
|
||||
val next = cc.peekNextNonWhitespace()
|
||||
if (next.isId("get") || next.isId("set")) {
|
||||
isProperty = true
|
||||
cc.restorePos(markBeforeEq)
|
||||
cc.skipWsTokens()
|
||||
} else {
|
||||
cc.restorePos(mark)
|
||||
}
|
||||
}
|
||||
|
||||
// Register the local name at compile time so that subsequent identifiers can be emitted as fast locals
|
||||
if (!isStatic) declareLocalName(name)
|
||||
|
||||
val isDelegate = if (eqToken.isId("by")) {
|
||||
val isDelegate = if (!isProperty && eqToken.isId("by")) {
|
||||
true
|
||||
} else {
|
||||
if (eqToken.type != Token.Type.ASSIGN) {
|
||||
if (!isMutable)
|
||||
if (!isProperty && eqToken.type != Token.Type.ASSIGN) {
|
||||
if (!isMutable && (declaringClassNameCaptured == null) && (extTypeName == null))
|
||||
throw ScriptError(start, "val must be initialized")
|
||||
else {
|
||||
cc.previous()
|
||||
cc.restorePos(markBeforeEq)
|
||||
cc.skipWsTokens()
|
||||
setNull = true
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
val initialExpression = if (setNull) null
|
||||
val initialExpression = if (setNull || isProperty) null
|
||||
else parseStatement(true)
|
||||
?: throw ScriptError(eqToken.pos, "Expected initializer expression")
|
||||
|
||||
// Emit MiniValDecl for this declaration (before execution wiring), attach doc if any
|
||||
run {
|
||||
val declRange = MiniRange(pendingDeclStart ?: start, cc.currentPos())
|
||||
val initR = if (setNull) null else MiniRange(eqToken.pos, cc.currentPos())
|
||||
val initR = if (setNull || isProperty) null else MiniRange(eqToken.pos, cc.currentPos())
|
||||
val node = MiniValDecl(
|
||||
range = declRange,
|
||||
name = name,
|
||||
@@ -2577,7 +2743,8 @@ class Compiler(
|
||||
type = varTypeMini,
|
||||
initRange = initR,
|
||||
doc = pendingDeclDoc,
|
||||
nameStart = nameToken.pos
|
||||
nameStart = nameStartPos,
|
||||
receiver = receiverMini
|
||||
)
|
||||
miniSink?.onValDecl(node)
|
||||
pendingDeclDoc = null
|
||||
@@ -2598,17 +2765,95 @@ class Compiler(
|
||||
return NopStatement
|
||||
}
|
||||
|
||||
// Determine declaring class (if inside class body) at compile time, capture it in the closure
|
||||
val declaringClassNameCaptured = (codeContexts.lastOrNull() as? CodeContext.ClassBody)?.name
|
||||
// Check for accessors if it is a class member
|
||||
var getter: Statement? = null
|
||||
var setter: Statement? = null
|
||||
if (declaringClassNameCaptured != null || extTypeName != null) {
|
||||
while (true) {
|
||||
val t = cc.peekNextNonWhitespace()
|
||||
if (t.isId("get")) {
|
||||
cc.skipWsTokens()
|
||||
cc.next() // consume 'get'
|
||||
cc.requireToken(Token.Type.LPAREN)
|
||||
cc.requireToken(Token.Type.RPAREN)
|
||||
getter = if (cc.peekNextNonWhitespace().type == Token.Type.LBRACE) {
|
||||
cc.skipWsTokens()
|
||||
parseBlock()
|
||||
} else if (cc.peekNextNonWhitespace().type == Token.Type.ASSIGN) {
|
||||
cc.skipWsTokens()
|
||||
cc.next() // consume '='
|
||||
val expr = parseExpression() ?: throw ScriptError(cc.current().pos, "Expected getter expression")
|
||||
(expr as? Statement) ?: statement(expr.pos) { s -> expr.execute(s) }
|
||||
} else {
|
||||
throw ScriptError(cc.current().pos, "Expected { or = after get()")
|
||||
}
|
||||
} else if (t.isId("set")) {
|
||||
cc.skipWsTokens()
|
||||
cc.next() // consume 'set'
|
||||
cc.requireToken(Token.Type.LPAREN)
|
||||
val setArg = cc.requireToken(Token.Type.ID, "Expected setter argument name")
|
||||
cc.requireToken(Token.Type.RPAREN)
|
||||
setter = if (cc.peekNextNonWhitespace().type == Token.Type.LBRACE) {
|
||||
cc.skipWsTokens()
|
||||
val body = parseBlock()
|
||||
statement(body.pos) { scope ->
|
||||
val value = scope.args.list.firstOrNull() ?: ObjNull
|
||||
scope.addItem(setArg.value, true, value, recordType = ObjRecord.Type.Argument)
|
||||
body.execute(scope)
|
||||
}
|
||||
} else if (cc.peekNextNonWhitespace().type == Token.Type.ASSIGN) {
|
||||
cc.skipWsTokens()
|
||||
cc.next() // consume '='
|
||||
val expr = parseExpression() ?: throw ScriptError(cc.current().pos, "Expected setter expression")
|
||||
val st = (expr as? Statement) ?: statement(expr.pos) { s -> expr.execute(s) }
|
||||
statement(st.pos) { scope ->
|
||||
val value = scope.args.list.firstOrNull() ?: ObjNull
|
||||
scope.addItem(setArg.value, true, value, recordType = ObjRecord.Type.Argument)
|
||||
st.execute(scope)
|
||||
}
|
||||
} else {
|
||||
throw ScriptError(cc.current().pos, "Expected { or = after set(...)")
|
||||
}
|
||||
} else break
|
||||
}
|
||||
if (getter != null || setter != null) {
|
||||
if (isMutable) {
|
||||
if (getter == null || setter == null) {
|
||||
throw ScriptError(start, "var property must have both get() and set()")
|
||||
}
|
||||
} else {
|
||||
if (setter != null)
|
||||
throw ScriptError(start, "val property cannot have a setter (name: $name)")
|
||||
if (getter == null)
|
||||
throw ScriptError(start, "val property with accessors must have a getter (name: $name)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return statement(nameToken.pos) { context ->
|
||||
return statement(start) { context ->
|
||||
if (extTypeName != null) {
|
||||
val prop = if (getter != null || setter != null) {
|
||||
ObjProperty(name, getter, setter)
|
||||
} else {
|
||||
// Simple val extension with initializer
|
||||
val initExpr = initialExpression ?: throw ScriptError(start, "Extension val must be initialized")
|
||||
ObjProperty(name, statement(initExpr.pos) { scp -> initExpr.execute(scp) }, null)
|
||||
}
|
||||
|
||||
val type = context[extTypeName]?.value ?: context.raiseSymbolNotFound("class $extTypeName not found")
|
||||
if (type !is ObjClass) context.raiseClassCastError("$extTypeName is not the class instance")
|
||||
|
||||
context.addExtension(type, name, ObjRecord(prop, isMutable = false, visibility = visibility, declaringClass = null, type = ObjRecord.Type.Property))
|
||||
|
||||
return@statement prop
|
||||
}
|
||||
// In true class bodies (not inside a function), store fields under a class-qualified key to support MI collisions
|
||||
// Do NOT infer declaring class from runtime thisObj here; only the compile-time captured
|
||||
// ClassBody qualifies for class-field storage. Otherwise, this is a plain local.
|
||||
val declaringClassName = declaringClassNameCaptured
|
||||
if (declaringClassName == null) {
|
||||
if (context.containsLocal(name))
|
||||
throw ScriptError(nameToken.pos, "Variable $name is already defined")
|
||||
throw ScriptError(start, "Variable $name is already defined")
|
||||
}
|
||||
|
||||
// Register the local name so subsequent identifiers can be emitted as fast locals
|
||||
@@ -2616,23 +2861,32 @@ class Compiler(
|
||||
|
||||
if (isDelegate) {
|
||||
TODO()
|
||||
// println("initial expr = $initialExpression")
|
||||
// val initValue =
|
||||
// (initialExpression?.execute(context.copy(Arguments(ObjString(name)))) as? Statement)
|
||||
// ?.execute(context.copy(Arguments(ObjString(name))))
|
||||
// ?: context.raiseError("delegate initialization required")
|
||||
// println("delegate init: $initValue")
|
||||
// if (!initValue.isInstanceOf(ObjArray))
|
||||
// context.raiseIllegalArgument("delegate initialized must be an array")
|
||||
// val s = initValue.getAt(context, 1)
|
||||
// val setter = if (s == ObjNull) statement { raiseNotImplemented("setter is not provided") }
|
||||
// else (s as? Statement) ?: context.raiseClassCastError("setter must be a callable")
|
||||
// ObjDelegate(
|
||||
// (initValue.getAt(context, 0) as? Statement)
|
||||
// ?: context.raiseClassCastError("getter must be a callable"), setter
|
||||
// ).also {
|
||||
// context.addItem(name, isMutable, it, visibility, recordType = ObjRecord.Type.Field)
|
||||
// }
|
||||
} else if (getter != null || setter != null) {
|
||||
val declaringClassName = declaringClassNameCaptured!!
|
||||
val storageName = "$declaringClassName::$name"
|
||||
val prop = ObjProperty(name, getter, setter)
|
||||
|
||||
// If we are in class scope now (defining instance field), defer initialization to instance time
|
||||
val isClassScope = context.thisObj is ObjClass && (context.thisObj !is ObjInstance)
|
||||
if (isClassScope) {
|
||||
val cls = context.thisObj as ObjClass
|
||||
// Register the property
|
||||
cls.instanceInitializers += statement(start) { scp ->
|
||||
scp.addItem(
|
||||
storageName,
|
||||
isMutable,
|
||||
prop,
|
||||
visibility,
|
||||
recordType = ObjRecord.Type.Property
|
||||
)
|
||||
ObjVoid
|
||||
}
|
||||
ObjVoid
|
||||
} else {
|
||||
// We are in instance scope already: perform initialization immediately
|
||||
context.addItem(storageName, isMutable, prop, visibility, recordType = ObjRecord.Type.Property)
|
||||
prop
|
||||
}
|
||||
} else {
|
||||
if (declaringClassName != null && !isStatic) {
|
||||
val storageName = "$declaringClassName::$name"
|
||||
@@ -2641,9 +2895,10 @@ class Compiler(
|
||||
if (isClassScope) {
|
||||
val cls = context.thisObj as ObjClass
|
||||
// Defer: at instance construction, evaluate initializer in instance scope and store under mangled name
|
||||
val initStmt = statement(nameToken.pos) { scp ->
|
||||
val initStmt = statement(start) { scp ->
|
||||
val initValue = initialExpression?.execute(scp)?.byValueCopy() ?: ObjNull
|
||||
scp.addOrUpdateItem(storageName, initValue, visibility, recordType = ObjRecord.Type.Field)
|
||||
// Preserve mutability of declaration: do NOT use addOrUpdateItem here, as it creates mutable records
|
||||
scp.addItem(storageName, isMutable, initValue, visibility, recordType = ObjRecord.Type.Field)
|
||||
ObjVoid
|
||||
}
|
||||
cls.instanceInitializers += initStmt
|
||||
@@ -2651,7 +2906,8 @@ class Compiler(
|
||||
} else {
|
||||
// We are in instance scope already: perform initialization immediately
|
||||
val initValue = initialExpression?.execute(context)?.byValueCopy() ?: ObjNull
|
||||
context.addOrUpdateItem(storageName, initValue, visibility, recordType = ObjRecord.Type.Field)
|
||||
// Preserve mutability of declaration: create record with correct mutability
|
||||
context.addItem(storageName, isMutable, initValue, visibility, recordType = ObjRecord.Type.Field)
|
||||
initValue
|
||||
}
|
||||
} else {
|
||||
@@ -2759,22 +3015,22 @@ class Compiler(
|
||||
|
||||
// Arithmetic for ints only (keep semantics simple at compile time)
|
||||
BinOp.PLUS -> when {
|
||||
a is ObjInt && b is ObjInt -> ObjInt(a.value + b.value)
|
||||
a is ObjInt && b is ObjInt -> ObjInt.of(a.value + b.value)
|
||||
a is ObjString && b is ObjString -> ObjString(a.value + b.value)
|
||||
else -> null
|
||||
}
|
||||
|
||||
BinOp.MINUS -> if (a is ObjInt && b is ObjInt) ObjInt(a.value - b.value) else null
|
||||
BinOp.STAR -> if (a is ObjInt && b is ObjInt) ObjInt(a.value * b.value) else null
|
||||
BinOp.SLASH -> if (a is ObjInt && b is ObjInt && b.value != 0L) ObjInt(a.value / b.value) else null
|
||||
BinOp.PERCENT -> if (a is ObjInt && b is ObjInt && b.value != 0L) ObjInt(a.value % b.value) else null
|
||||
BinOp.MINUS -> if (a is ObjInt && b is ObjInt) ObjInt.of(a.value - b.value) else null
|
||||
BinOp.STAR -> if (a is ObjInt && b is ObjInt) ObjInt.of(a.value * b.value) else null
|
||||
BinOp.SLASH -> if (a is ObjInt && b is ObjInt && b.value != 0L) ObjInt.of(a.value / b.value) else null
|
||||
BinOp.PERCENT -> if (a is ObjInt && b is ObjInt && b.value != 0L) ObjInt.of(a.value % b.value) else null
|
||||
|
||||
// Bitwise for ints
|
||||
BinOp.BAND -> if (a is ObjInt && b is ObjInt) ObjInt(a.value and b.value) else null
|
||||
BinOp.BXOR -> if (a is ObjInt && b is ObjInt) ObjInt(a.value xor b.value) else null
|
||||
BinOp.BOR -> if (a is ObjInt && b is ObjInt) ObjInt(a.value or b.value) else null
|
||||
BinOp.SHL -> if (a is ObjInt && b is ObjInt) ObjInt(a.value shl (b.value.toInt() and 63)) else null
|
||||
BinOp.SHR -> if (a is ObjInt && b is ObjInt) ObjInt(a.value shr (b.value.toInt() and 63)) else null
|
||||
BinOp.BAND -> if (a is ObjInt && b is ObjInt) ObjInt.of(a.value and b.value) else null
|
||||
BinOp.BXOR -> if (a is ObjInt && b is ObjInt) ObjInt.of(a.value xor b.value) else null
|
||||
BinOp.BOR -> if (a is ObjInt && b is ObjInt) ObjInt.of(a.value or b.value) else null
|
||||
BinOp.SHL -> if (a is ObjInt && b is ObjInt) ObjInt.of(a.value shl (b.value.toInt() and 63)) else null
|
||||
BinOp.SHR -> if (a is ObjInt && b is ObjInt) ObjInt.of(a.value shr (b.value.toInt() and 63)) else null
|
||||
|
||||
// Non-folded / side-effecting or type-dependent ops
|
||||
BinOp.EQARROW, BinOp.REF_EQ, BinOp.REF_NEQ, BinOp.MATCH, BinOp.NOTMATCH,
|
||||
@@ -2787,12 +3043,12 @@ class Compiler(
|
||||
return when (op) {
|
||||
UnaryOp.NOT -> if (a is ObjBool) if (!a.value) ObjTrue else ObjFalse else null
|
||||
UnaryOp.NEGATE -> when (a) {
|
||||
is ObjInt -> ObjInt(-a.value)
|
||||
is ObjReal -> ObjReal(-a.value)
|
||||
is ObjInt -> ObjInt.of(-a.value)
|
||||
is ObjReal -> ObjReal.of(-a.value)
|
||||
else -> null
|
||||
}
|
||||
|
||||
UnaryOp.BITNOT -> if (a is ObjInt) ObjInt(a.value.inv()) else null
|
||||
UnaryOp.BITNOT -> if (a is ObjInt) ObjInt.of(a.value.inv()) else null
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -25,11 +25,12 @@ class CompilerContext(val tokens: List<Token>) {
|
||||
var loopLevel = 0
|
||||
|
||||
inline fun <T> parseLoop(f: () -> T): Pair<Boolean, T> {
|
||||
if (++loopLevel == 0) breakFound = false
|
||||
val oldBreakFound = breakFound
|
||||
breakFound = false
|
||||
val result = f()
|
||||
return Pair(breakFound, result).also {
|
||||
--loopLevel
|
||||
}
|
||||
val currentBreakFound = breakFound
|
||||
breakFound = oldBreakFound || currentBreakFound
|
||||
return Pair(currentBreakFound, result)
|
||||
}
|
||||
|
||||
var currentIndex = 0
|
||||
@@ -108,7 +109,6 @@ class CompilerContext(val tokens: List<Token>) {
|
||||
val t = next()
|
||||
return if (t.type != tokenType) {
|
||||
if (!isOptional) {
|
||||
println("unexpected: $t (needed $tokenType)")
|
||||
throw ScriptError(t.pos, errorMessage)
|
||||
} else {
|
||||
previous()
|
||||
|
||||
@@ -45,19 +45,36 @@ class ModuleScope(
|
||||
if (record.visibility.isPublic) {
|
||||
val newName = symbols?.let { ss: Map<String, String> ->
|
||||
ss[symbol]
|
||||
?.also { symbolsToImport!!.remove(it) }
|
||||
?: scope.raiseError("internal error: symbol $symbol not found though the module is cached")
|
||||
} ?: symbol
|
||||
val existing = scope.objects[newName]
|
||||
if (existing != null ) {
|
||||
if (existing.importedFrom != record.importedFrom)
|
||||
scope.raiseError("symbol ${existing.importedFrom?.packageName}.$newName already exists, redefinition on import is not allowed")
|
||||
// already imported
|
||||
?.also { symbolsToImport!!.remove(symbol) }
|
||||
?: return@let null
|
||||
} ?: if (symbols == null) symbol else null
|
||||
|
||||
if (newName != null) {
|
||||
val existing = scope.objects[newName]
|
||||
if (existing != null) {
|
||||
if (existing.importedFrom != record.importedFrom)
|
||||
scope.raiseError("symbol ${existing.importedFrom?.packageName}.$newName already exists, redefinition on import is not allowed")
|
||||
// already imported
|
||||
} else {
|
||||
// when importing records, we keep track of its package (not otherwise needed)
|
||||
if (record.importedFrom == null) record.importedFrom = this
|
||||
scope.objects[newName] = record
|
||||
}
|
||||
}
|
||||
else {
|
||||
// when importing records, we keep track of its package (not otherwise needed)
|
||||
if (record.importedFrom == null) record.importedFrom = this
|
||||
scope.objects[newName] = record
|
||||
}
|
||||
}
|
||||
for ((cls, map) in this.extensions) {
|
||||
for ((symbol, record) in map) {
|
||||
if (record.visibility.isPublic) {
|
||||
val newName = symbols?.let { ss: Map<String, String> ->
|
||||
ss[symbol]
|
||||
?.also { symbolsToImport!!.remove(symbol) }
|
||||
?: return@let null
|
||||
} ?: if (symbols == null) symbol else null
|
||||
|
||||
if (newName != null) {
|
||||
scope.addExtension(cls, newName, record)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -213,25 +213,40 @@ private class Parser(fromPos: Pos) {
|
||||
|
||||
'!' -> {
|
||||
if (currentChar == 'i') {
|
||||
// Potentially !in / !is, but only if a word boundary follows
|
||||
pos.advance()
|
||||
when (currentChar) {
|
||||
'n' -> {
|
||||
pos.advance()
|
||||
Token("!in", from, Token.Type.NOTIN)
|
||||
// if next char continues an identifier, it's actually '!'+identifier starting with "in..."
|
||||
if (idNextChars(currentChar)) {
|
||||
// backtrack to right after '!'
|
||||
pos.back()
|
||||
pos.back()
|
||||
Token("!", from, Token.Type.NOT)
|
||||
} else
|
||||
Token("!in", from, Token.Type.NOTIN)
|
||||
}
|
||||
|
||||
's' -> {
|
||||
pos.advance()
|
||||
Token("!is", from, Token.Type.NOTIS)
|
||||
// if next char continues an identifier, it's actually '!'+identifier starting with "is..."
|
||||
if (idNextChars(currentChar)) {
|
||||
// backtrack to right after '!'
|
||||
pos.back()
|
||||
pos.back()
|
||||
Token("!", from, Token.Type.NOT)
|
||||
} else
|
||||
Token("!is", from, Token.Type.NOTIS)
|
||||
}
|
||||
|
||||
else -> {
|
||||
// it was just '!i' followed by something else; revert one step and return '!'
|
||||
pos.back()
|
||||
Token("!", from, Token.Type.NOT)
|
||||
}
|
||||
}
|
||||
} else
|
||||
if (currentChar == '=') {
|
||||
} else if (currentChar == '=') {
|
||||
pos.advance()
|
||||
if (currentChar == '=') {
|
||||
pos.advance()
|
||||
|
||||
@@ -62,6 +62,153 @@ open class Scope(
|
||||
*/
|
||||
internal val localBindings: MutableMap<String, ObjRecord> = mutableMapOf()
|
||||
|
||||
internal val extensions: MutableMap<ObjClass, MutableMap<String, ObjRecord>> = mutableMapOf()
|
||||
|
||||
fun addExtension(cls: ObjClass, name: String, record: ObjRecord) {
|
||||
extensions.getOrPut(cls) { mutableMapOf() }[name] = record
|
||||
}
|
||||
|
||||
internal fun findExtension(receiverClass: ObjClass, name: String): ObjRecord? {
|
||||
var s: Scope? = this
|
||||
val visited = HashSet<Long>(4)
|
||||
while (s != null) {
|
||||
if (!visited.add(s.frameId)) break
|
||||
// Proximity rule: check all extensions in the current scope before going to parent.
|
||||
// Priority within scope: more specific class in MRO wins.
|
||||
for (cls in receiverClass.mro) {
|
||||
s.extensions[cls]?.get(name)?.let { return it }
|
||||
}
|
||||
if (s is ClosureScope) {
|
||||
s.closureScope.findExtension(receiverClass, name)?.let { return it }
|
||||
}
|
||||
s = s.parent
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/** Debug helper: ensure assigning [candidateParent] does not create a structural cycle. */
|
||||
private fun ensureNoCycle(candidateParent: Scope?) {
|
||||
if (candidateParent == null) return
|
||||
var s: Scope? = candidateParent
|
||||
var hops = 0
|
||||
while (s != null && hops++ < 1024) {
|
||||
if (s === this) {
|
||||
// In production we silently ignore; for debugging throw an error to signal misuse
|
||||
throw IllegalStateException("cycle detected in scope parent chain assignment")
|
||||
}
|
||||
s = s.parent
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Internal lookup helpers that deliberately avoid invoking overridden `get` implementations
|
||||
* (notably in ClosureScope) to prevent accidental ping-pong and infinite recursion across
|
||||
* 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.
|
||||
*/
|
||||
private fun tryGetLocalRecord(s: Scope, name: String, caller: net.sergeych.lyng.obj.ObjClass?): ObjRecord? {
|
||||
s.objects[name]?.let { rec ->
|
||||
if (rec.declaringClass == null || canAccessMember(rec.visibility, rec.declaringClass, caller)) return rec
|
||||
}
|
||||
s.localBindings[name]?.let { rec ->
|
||||
if (rec.declaringClass == null || canAccessMember(rec.visibility, rec.declaringClass, caller)) return rec
|
||||
}
|
||||
s.getSlotIndexOf(name)?.let { idx ->
|
||||
val rec = s.getSlotRecord(idx)
|
||||
if (rec.declaringClass == null || canAccessMember(rec.visibility, rec.declaringClass, caller)) return rec
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
internal fun chainLookupIgnoreClosure(name: String): ObjRecord? {
|
||||
var s: Scope? = this
|
||||
// use frameId to detect unexpected structural cycles in the parent chain
|
||||
val visited = HashSet<Long>(4)
|
||||
while (s != null) {
|
||||
if (!visited.add(s.frameId)) return null
|
||||
tryGetLocalRecord(s, name, currentClassCtx)?.let { return it }
|
||||
s = s.parent
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform base Scope.get semantics for this frame without delegating into parent.get
|
||||
* virtual dispatch. This checks:
|
||||
* - locals/bindings in this frame
|
||||
* - walks raw parent chain for locals/bindings (ignoring ClosureScope-specific overrides)
|
||||
* - finally falls back to this frame's `thisObj` instance/class members
|
||||
*/
|
||||
internal fun baseGetIgnoreClosure(name: String): ObjRecord? {
|
||||
// 1) locals/bindings in this frame
|
||||
tryGetLocalRecord(this, name, currentClassCtx)?.let { return it }
|
||||
// 2) walk parents for plain locals/bindings only
|
||||
var s = parent
|
||||
val visited = HashSet<Long>(4)
|
||||
while (s != null) {
|
||||
if (!visited.add(s.frameId)) return null
|
||||
tryGetLocalRecord(s, name, currentClassCtx)?.let { return it }
|
||||
s = s.parent
|
||||
}
|
||||
// 3) fallback to instance/class members of this frame's thisObj
|
||||
for (cls in thisObj.objClass.mro) {
|
||||
this.extensions[cls]?.get(name)?.let { return it }
|
||||
}
|
||||
return thisObj.objClass.getInstanceMemberOrNull(name)?.let { rec ->
|
||||
if (canAccessMember(rec.visibility, rec.declaringClass, currentClassCtx)) rec else null
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Walk the ancestry starting from this scope and try to resolve [name] against:
|
||||
* - locals/bindings of each frame
|
||||
* - then instance/class members of each frame's `thisObj`.
|
||||
* This completely avoids invoking overridden `get` implementations, preventing
|
||||
* ping-pong recursion between `ClosureScope` frames.
|
||||
*/
|
||||
internal fun chainLookupWithMembers(name: String, caller: net.sergeych.lyng.obj.ObjClass? = currentClassCtx): ObjRecord? {
|
||||
var s: Scope? = this
|
||||
val visited = HashSet<Long>(4)
|
||||
while (s != null) {
|
||||
if (!visited.add(s.frameId)) return null
|
||||
tryGetLocalRecord(s, name, caller)?.let { return it }
|
||||
for (cls in s.thisObj.objClass.mro) {
|
||||
s.extensions[cls]?.get(name)?.let { return it }
|
||||
}
|
||||
s.thisObj.objClass.getInstanceMemberOrNull(name)?.let { rec ->
|
||||
if (canAccessMember(rec.visibility, rec.declaringClass, caller)) return rec
|
||||
}
|
||||
s = s.parent
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a non-pooled snapshot of this scope suitable for capturing as a closure environment.
|
||||
* Copies locals, slots, and localBindings; preserves parent chain and class context.
|
||||
*/
|
||||
fun snapshotForClosure(): Scope {
|
||||
val snap = Scope(parent, args, pos, thisObj)
|
||||
snap.currentClassCtx = this.currentClassCtx
|
||||
// copy locals and bindings
|
||||
snap.objects.putAll(this.objects)
|
||||
snap.localBindings.putAll(this.localBindings)
|
||||
// copy extensions
|
||||
for ((cls, map) in extensions) {
|
||||
snap.extensions[cls] = map.toMutableMap()
|
||||
}
|
||||
// copy slots map preserving indices
|
||||
if (this.slotCount() > 0) {
|
||||
var i = 0
|
||||
while (i < this.slotCount()) {
|
||||
val rec = this.getSlotRecord(i)
|
||||
snap.allocateSlotFor(this.nameToSlot.entries.firstOrNull { it.value == i }?.key ?: "slot${'$'}i", rec)
|
||||
i++
|
||||
}
|
||||
}
|
||||
return snap
|
||||
}
|
||||
|
||||
/**
|
||||
* Hint internal collections to reduce reallocations for upcoming parameter/local assignments.
|
||||
* Only effective for ArrayList-backed slots; maps are left as-is (rehashed lazily by JVM).
|
||||
@@ -171,13 +318,19 @@ open class Scope(
|
||||
if (name == "this") thisObj.asReadonly
|
||||
else {
|
||||
// Prefer direct locals/bindings declared in this frame
|
||||
(objects[name]
|
||||
(objects[name]?.let { rec ->
|
||||
if (rec.declaringClass == null || canAccessMember(rec.visibility, rec.declaringClass, currentClassCtx)) rec else null
|
||||
}
|
||||
// Then, check known local bindings in this frame (helps after suspension)
|
||||
?: localBindings[name]
|
||||
?: localBindings[name]?.let { rec ->
|
||||
if (rec.declaringClass == null || canAccessMember(rec.visibility, rec.declaringClass, currentClassCtx)) rec else null
|
||||
}
|
||||
// Walk up ancestry
|
||||
?: parent?.get(name)
|
||||
// Finally, fallback to class members on thisObj
|
||||
?: thisObj.objClass.getInstanceMemberOrNull(name)
|
||||
?: thisObj.objClass.getInstanceMemberOrNull(name)?.let { rec ->
|
||||
if (canAccessMember(rec.visibility, rec.declaringClass, currentClassCtx)) rec else null
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
@@ -195,15 +348,18 @@ open class Scope(
|
||||
return idx
|
||||
}
|
||||
|
||||
fun updateSlotFor(name: String, record: ObjRecord) {
|
||||
nameToSlot[name]?.let { slots[it] = record }
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
fun resetForReuse(parent: Scope?, args: Arguments, pos: Pos, thisObj: Obj) {
|
||||
this.parent = parent
|
||||
this.args = args
|
||||
this.pos = pos
|
||||
this.thisObj = thisObj
|
||||
// Fully detach from any previous chain/state first to avoid residual ancestry
|
||||
// that could interact badly with the new parent and produce a cycle.
|
||||
this.parent = null
|
||||
this.skipScopeCreation = false
|
||||
// fresh identity for PIC caches
|
||||
this.frameId = nextFrameId()
|
||||
@@ -212,6 +368,13 @@ open class Scope(
|
||||
slots.clear()
|
||||
nameToSlot.clear()
|
||||
localBindings.clear()
|
||||
extensions.clear()
|
||||
// Now safe to validate and re-parent
|
||||
ensureNoCycle(parent)
|
||||
this.parent = parent
|
||||
this.args = args
|
||||
this.pos = pos
|
||||
this.thisObj = thisObj
|
||||
// Pre-size local slots for upcoming parameter assignment where possible
|
||||
reserveLocalCapacity(args.list.size + 4)
|
||||
}
|
||||
@@ -220,7 +383,10 @@ open class Scope(
|
||||
* Creates a new child scope using the provided arguments and optional `thisObj`.
|
||||
*/
|
||||
fun createChildScope(pos: Pos, args: Arguments = Arguments.EMPTY, newThisObj: Obj? = null): Scope =
|
||||
Scope(this, args, pos, newThisObj ?: thisObj).also { it.reserveLocalCapacity(args.list.size + 4) }
|
||||
Scope(this, args, pos, newThisObj ?: thisObj).also {
|
||||
it.ensureNoCycle(it.parent)
|
||||
it.reserveLocalCapacity(args.list.size + 4)
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a block inside a child frame. Guarded for future pooling via [PerfFlags.SCOPE_POOL].
|
||||
@@ -249,7 +415,7 @@ open class Scope(
|
||||
* @return A new instance of [Scope] initialized with the specified arguments and `thisObj`.
|
||||
*/
|
||||
fun createChildScope(args: Arguments = Arguments.EMPTY, newThisObj: Obj? = null): Scope =
|
||||
Scope(this, args, pos, newThisObj ?: thisObj)
|
||||
Scope(this, args, pos, newThisObj ?: thisObj).also { it.ensureNoCycle(it.parent) }
|
||||
|
||||
/**
|
||||
* @return A child scope with the same arguments, position and [thisObj]
|
||||
@@ -286,9 +452,10 @@ open class Scope(
|
||||
isMutable: Boolean,
|
||||
value: Obj,
|
||||
visibility: Visibility = Visibility.Public,
|
||||
recordType: ObjRecord.Type = ObjRecord.Type.Other
|
||||
recordType: ObjRecord.Type = ObjRecord.Type.Other,
|
||||
declaringClass: net.sergeych.lyng.obj.ObjClass? = currentClassCtx
|
||||
): ObjRecord {
|
||||
val rec = ObjRecord(value, isMutable, visibility, declaringClass = currentClassCtx, type = recordType)
|
||||
val rec = ObjRecord(value, isMutable, visibility, declaringClass = declaringClass, type = recordType)
|
||||
objects[name] = rec
|
||||
// Index this binding within the current frame to help resolve locals across suspension
|
||||
localBindings[name] = rec
|
||||
@@ -305,9 +472,12 @@ open class Scope(
|
||||
callScope.allocateSlotFor(name, rec)
|
||||
}
|
||||
}
|
||||
// Map to a slot for fast local access (if not already mapped)
|
||||
if (getSlotIndexOf(name) == null) {
|
||||
// Map to a slot for fast local access (ensure consistency)
|
||||
val idx = getSlotIndexOf(name)
|
||||
if (idx == null) {
|
||||
allocateSlotFor(name, rec)
|
||||
} else {
|
||||
slots[idx] = rec
|
||||
}
|
||||
return rec
|
||||
}
|
||||
@@ -318,13 +488,13 @@ open class Scope(
|
||||
}
|
||||
|
||||
inline fun addVoidFn(vararg names: String, crossinline fn: suspend Scope.() -> Unit) {
|
||||
addFn<ObjVoid>(*names) {
|
||||
addFn(*names) {
|
||||
fn(this)
|
||||
ObjVoid
|
||||
}
|
||||
}
|
||||
|
||||
inline fun <reified T : Obj> addFn(vararg names: String, crossinline fn: suspend Scope.() -> T) {
|
||||
fun addFn(vararg names: String, fn: suspend Scope.() -> Obj) {
|
||||
val newFn = object : Statement() {
|
||||
override val pos: Pos = Pos.builtIn
|
||||
|
||||
@@ -406,6 +576,23 @@ open class Scope(
|
||||
|
||||
open fun applyClosure(closure: Scope): Scope = ClosureScope(this, closure)
|
||||
|
||||
/**
|
||||
* Resolve and evaluate a qualified identifier exactly as compiled code would.
|
||||
* For input like `A.B.C`, it builds the same ObjRef chain the compiler emits:
|
||||
* `LocalVarRef("A", Pos.builtIn)` followed by `FieldRef` for each segment, then evaluates it.
|
||||
* This mirrors `eval("A.B.C")` resolution semantics without invoking the compiler.
|
||||
*/
|
||||
suspend fun resolveQualifiedIdentifier(qualifiedName: String): Obj {
|
||||
val trimmed = qualifiedName.trim()
|
||||
if (trimmed.isEmpty()) raiseSymbolNotFound("empty identifier")
|
||||
val parts = trimmed.split('.')
|
||||
var ref: ObjRef = LocalVarRef(parts[0], Pos.builtIn)
|
||||
for (i in 1 until parts.size) {
|
||||
ref = FieldRef(ref, parts[i], false)
|
||||
}
|
||||
return ref.evalValue(this)
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
fun new(): Scope =
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
|
||||
* 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.
|
||||
@@ -20,6 +20,9 @@ package net.sergeych.lyng
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.yield
|
||||
import net.sergeych.lyng.Script.Companion.defaultImportManager
|
||||
import net.sergeych.lyng.miniast.addConstDoc
|
||||
import net.sergeych.lyng.miniast.addVoidFnDoc
|
||||
import net.sergeych.lyng.miniast.type
|
||||
import net.sergeych.lyng.obj.*
|
||||
import net.sergeych.lyng.pacman.ImportManager
|
||||
import net.sergeych.lyng.stdlib_included.rootLyng
|
||||
@@ -152,64 +155,116 @@ class Script(
|
||||
sqrt(args.firstAndOnly().toDouble())
|
||||
)
|
||||
}
|
||||
addFn( "abs" ) {
|
||||
addFn("abs") {
|
||||
val x = args.firstAndOnly()
|
||||
if( x is ObjInt) ObjInt( x.value.absoluteValue ) else ObjReal( x.toDouble().absoluteValue )
|
||||
if (x is ObjInt) ObjInt(x.value.absoluteValue) else ObjReal(x.toDouble().absoluteValue)
|
||||
}
|
||||
|
||||
addVoidFn("assert") {
|
||||
val cond = requiredArg<ObjBool>(0)
|
||||
val message = if( args.size > 1 )
|
||||
val message = if (args.size > 1)
|
||||
": " + (args[1] as Statement).execute(this).toString(this).value
|
||||
else ""
|
||||
if( !cond.value == true )
|
||||
if (!cond.value == true)
|
||||
raiseError(ObjAssertionFailedException(this, "Assertion failed$message"))
|
||||
}
|
||||
|
||||
addVoidFn("assertEquals") {
|
||||
val a = requiredArg<Obj>(0)
|
||||
val b = requiredArg<Obj>(1)
|
||||
if( a.compareTo(this, b) != 0 )
|
||||
raiseError(ObjAssertionFailedException(this,"Assertion failed: ${a.inspect(this)} == ${b.inspect(this)}"))
|
||||
if (a.compareTo(this, b) != 0)
|
||||
raiseError(
|
||||
ObjAssertionFailedException(
|
||||
this,
|
||||
"Assertion failed: ${a.inspect(this)} == ${b.inspect(this)}"
|
||||
)
|
||||
)
|
||||
}
|
||||
// alias used in tests
|
||||
addVoidFn("assertEqual") {
|
||||
val a = requiredArg<Obj>(0)
|
||||
val b = requiredArg<Obj>(1)
|
||||
if (a.compareTo(this, b) != 0)
|
||||
raiseError(
|
||||
ObjAssertionFailedException(
|
||||
this,
|
||||
"Assertion failed: ${a.inspect(this)} == ${b.inspect(this)}"
|
||||
)
|
||||
)
|
||||
}
|
||||
addVoidFn("assertNotEquals") {
|
||||
val a = requiredArg<Obj>(0)
|
||||
val b = requiredArg<Obj>(1)
|
||||
if( a.compareTo(this, b) == 0 )
|
||||
raiseError(ObjAssertionFailedException(this,"Assertion failed: ${a.inspect(this)} != ${b.inspect(this)}"))
|
||||
if (a.compareTo(this, b) == 0)
|
||||
raiseError(
|
||||
ObjAssertionFailedException(
|
||||
this,
|
||||
"Assertion failed: ${a.inspect(this)} != ${b.inspect(this)}"
|
||||
)
|
||||
)
|
||||
}
|
||||
addFn("assertThrows") {
|
||||
val code = requireOnlyArg<Statement>()
|
||||
val result =try {
|
||||
val code: Statement
|
||||
val expectedClass: ObjClass?
|
||||
when(args.size) {
|
||||
1 -> {
|
||||
code = requiredArg<Statement>(0)
|
||||
expectedClass = null
|
||||
}
|
||||
2 -> {
|
||||
code = requiredArg<Statement>(1)
|
||||
expectedClass = requiredArg<ObjClass>(0)
|
||||
}
|
||||
else -> raiseIllegalArgument("Expected 1 or 2 arguments, got ${args.size}")
|
||||
}
|
||||
val result = try {
|
||||
code.execute(this)
|
||||
null
|
||||
}
|
||||
catch( e: ExecutionError ) {
|
||||
} catch (e: ExecutionError) {
|
||||
e.errorObject
|
||||
}
|
||||
catch (_: ScriptError) {
|
||||
} catch (_: ScriptError) {
|
||||
ObjNull
|
||||
}
|
||||
result ?: raiseError(ObjAssertionFailedException(this,"Expected exception but nothing was thrown"))
|
||||
if( result == null ) raiseError(ObjAssertionFailedException(this, "Expected exception but nothing was thrown"))
|
||||
expectedClass?.let {
|
||||
if( result !is ObjException)
|
||||
raiseError("Expected $expectedClass, got $result")
|
||||
if (result.exceptionClass != expectedClass) {
|
||||
raiseError("Expected $expectedClass, got ${result.exceptionClass}")
|
||||
}
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
addFn("dynamic") {
|
||||
ObjDynamic.create(this, requireOnlyArg())
|
||||
}
|
||||
|
||||
val root = this
|
||||
val mathClass = ObjClass("Math").apply {
|
||||
addFn("sqrt") {
|
||||
ObjReal(sqrt(args.firstAndOnly().toDouble()))
|
||||
}
|
||||
}
|
||||
addItem("Math", false, ObjInstance(mathClass).apply {
|
||||
instanceScope = Scope(root, thisObj = this)
|
||||
})
|
||||
|
||||
addFn("require") {
|
||||
val condition = requiredArg<ObjBool>(0)
|
||||
if( !condition.value ) {
|
||||
val message = args.list.getOrNull(1)?.toString() ?: "requirement not met"
|
||||
raiseIllegalArgument(message)
|
||||
if (!condition.value) {
|
||||
var message = args.list.getOrNull(1)
|
||||
if( message is Statement ) message = message.execute(this)
|
||||
raiseIllegalArgument(message?.toString() ?: "requirement not met")
|
||||
}
|
||||
ObjVoid
|
||||
}
|
||||
addFn("check") {
|
||||
val condition = requiredArg<ObjBool>(0)
|
||||
if( !condition.value ) {
|
||||
val message = args.list.getOrNull(1)?.toString() ?: "check failed"
|
||||
raiseIllegalState(message)
|
||||
if (!condition.value) {
|
||||
var message = args.list.getOrNull(1)
|
||||
if( message is Statement ) message = message.execute(this)
|
||||
raiseIllegalState(message?.toString() ?: "check failed")
|
||||
}
|
||||
ObjVoid
|
||||
}
|
||||
@@ -247,6 +302,7 @@ class Script(
|
||||
addConst("Deferred", ObjDeferred.type)
|
||||
addConst("CompletableDeferred", ObjCompletableDeferred.type)
|
||||
addConst("Mutex", ObjMutex.type)
|
||||
addConst("Flow", ObjFlow.type)
|
||||
|
||||
addConst("Regex", ObjRegex.type)
|
||||
|
||||
@@ -269,9 +325,19 @@ class Script(
|
||||
}
|
||||
|
||||
val pi = ObjReal(PI)
|
||||
addConst("π", pi)
|
||||
addConstDoc(
|
||||
name = "π",
|
||||
value = pi,
|
||||
doc = "The mathematical constant pi (π).",
|
||||
type = type("lyng.Real")
|
||||
)
|
||||
getOrCreateNamespace("Math").apply {
|
||||
addConst("PI", pi)
|
||||
addConstDoc(
|
||||
name = "PI",
|
||||
value = pi,
|
||||
doc = "The mathematical constant pi (π) in the Math namespace.",
|
||||
type = type("lyng.Real")
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -281,24 +347,51 @@ class Script(
|
||||
rootLyng
|
||||
)
|
||||
addPackage("lyng.buffer") {
|
||||
it.addConst("Buffer", ObjBuffer.type)
|
||||
it.addConst("MutableBuffer", ObjMutableBuffer.type)
|
||||
it.addConstDoc(
|
||||
name = "Buffer",
|
||||
value = ObjBuffer.type,
|
||||
doc = "Immutable sequence of bytes. Use for binary data and IO.",
|
||||
type = type("lyng.Class")
|
||||
)
|
||||
it.addConstDoc(
|
||||
name = "MutableBuffer",
|
||||
value = ObjMutableBuffer.type,
|
||||
doc = "Mutable byte buffer. Supports in-place modifications.",
|
||||
type = type("lyng.Class")
|
||||
)
|
||||
}
|
||||
addPackage("lyng.serialization") {
|
||||
it.addConst("Lynon", ObjLynonClass)
|
||||
it.addConstDoc(
|
||||
name = "Lynon",
|
||||
value = ObjLynonClass,
|
||||
doc = "Lynon serialization utilities: encode/decode data structures to a portable binary/text form.",
|
||||
type = type("lyng.Class")
|
||||
)
|
||||
}
|
||||
addPackage("lyng.time") {
|
||||
it.addConst("Instant", ObjInstant.type)
|
||||
it.addConst("Duration", ObjDuration.type)
|
||||
it.addFn("delay") {
|
||||
it.addConstDoc(
|
||||
name = "Instant",
|
||||
value = ObjInstant.type,
|
||||
doc = "Point in time (epoch-based).",
|
||||
type = type("lyng.Class")
|
||||
)
|
||||
it.addConstDoc(
|
||||
name = "Duration",
|
||||
value = ObjDuration.type,
|
||||
doc = "Time duration with millisecond precision.",
|
||||
type = type("lyng.Class")
|
||||
)
|
||||
it.addVoidFnDoc(
|
||||
"delay",
|
||||
doc = "Suspend for the given time. Accepts Duration, Int seconds, or Real seconds."
|
||||
) {
|
||||
val a = args.firstAndOnly()
|
||||
when(a) {
|
||||
when (a) {
|
||||
is ObjInt -> delay(a.value * 1000)
|
||||
is ObjReal -> delay((a.value * 1000).roundToLong())
|
||||
is ObjDuration -> delay(a.duration)
|
||||
else -> raiseIllegalArgument("Expected Duration, Int or Real, got ${a.inspect(this)}")
|
||||
}
|
||||
ObjVoid
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ enum class Visibility {
|
||||
|
||||
/** MI-aware visibility check: whether [caller] can access a member declared in [decl] with [visibility]. */
|
||||
fun canAccessMember(visibility: Visibility, decl: net.sergeych.lyng.obj.ObjClass?, caller: net.sergeych.lyng.obj.ObjClass?): Boolean {
|
||||
return when (visibility) {
|
||||
val res = when (visibility) {
|
||||
Visibility.Public -> true
|
||||
Visibility.Private -> (decl != null && caller === decl)
|
||||
Visibility.Protected -> when {
|
||||
@@ -36,4 +36,5 @@ fun canAccessMember(visibility: Visibility, decl: net.sergeych.lyng.obj.ObjClass
|
||||
else -> (caller.allParentsSet.contains(decl))
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
|
||||
* 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.
|
||||
@@ -25,12 +25,9 @@ import net.sergeych.lyng.Source
|
||||
import net.sergeych.lyng.highlight.HighlightKind
|
||||
import net.sergeych.lyng.highlight.SimpleLyngHighlighter
|
||||
import net.sergeych.lyng.highlight.offsetOf
|
||||
import net.sergeych.lyng.miniast.MiniClassDecl
|
||||
import net.sergeych.lyng.miniast.MiniFunDecl
|
||||
import net.sergeych.lyng.miniast.MiniScript
|
||||
import net.sergeych.lyng.miniast.MiniValDecl
|
||||
import net.sergeych.lyng.miniast.*
|
||||
|
||||
enum class SymbolKind { Class, Enum, Function, Val, Var, Param }
|
||||
enum class SymbolKind { Class, Enum, Function, Value, Variable, Parameter }
|
||||
|
||||
data class Symbol(
|
||||
val id: Int,
|
||||
@@ -111,7 +108,7 @@ object Binder {
|
||||
for (cf in d.ctorFields) {
|
||||
val fs = source.offsetOf(cf.nameStart)
|
||||
val fe = fs + cf.name.length
|
||||
val kind = if (cf.mutable) SymbolKind.Var else SymbolKind.Val
|
||||
val kind = if (cf.mutable) SymbolKind.Variable else SymbolKind.Value
|
||||
val fieldSym = Symbol(nextId++, cf.name, kind, fs, fe, containerId = sym.id)
|
||||
symbols += fieldSym
|
||||
classes.last().fields += fieldSym.id
|
||||
@@ -138,7 +135,7 @@ object Binder {
|
||||
for (p in d.params) {
|
||||
val ps = source.offsetOf(p.nameStart)
|
||||
val pe = ps + p.name.length
|
||||
val pk = SymbolKind.Param
|
||||
val pk = SymbolKind.Parameter
|
||||
val paramSym = Symbol(nextId++, p.name, pk, ps, pe, containerId = sym.id)
|
||||
fnScope.locals += paramSym.id
|
||||
symbols += paramSym
|
||||
@@ -147,7 +144,7 @@ object Binder {
|
||||
}
|
||||
is MiniValDecl -> {
|
||||
val (s, e) = nameOffsets(d.nameStart, d.name)
|
||||
val kind = if (d.mutable) SymbolKind.Var else SymbolKind.Val
|
||||
val kind = if (d.mutable) SymbolKind.Variable else SymbolKind.Value
|
||||
val ownerClass = classContaining(s)
|
||||
if (ownerClass != null) {
|
||||
// class field
|
||||
@@ -160,6 +157,12 @@ object Binder {
|
||||
topLevelByName.getOrPut(d.name) { mutableListOf() }.add(sym.id)
|
||||
}
|
||||
}
|
||||
is MiniEnumDecl -> {
|
||||
val (s, e) = nameOffsets(d.nameStart, d.name)
|
||||
val sym = Symbol(nextId++, d.name, SymbolKind.Enum, s, e, containerId = null)
|
||||
symbols += sym
|
||||
topLevelByName.getOrPut(d.name) { mutableListOf() }.add(sym.id)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -191,7 +194,7 @@ object Binder {
|
||||
.maxByOrNull { it.rangeEnd - it.rangeStart }
|
||||
if (containerFn != null) {
|
||||
val fnSymId = containerFn.id
|
||||
val kind = if (d.mutable) SymbolKind.Var else SymbolKind.Val
|
||||
val kind = if (d.mutable) SymbolKind.Variable else SymbolKind.Value
|
||||
val localSym = Symbol(nextId++, d.name, kind, s, e, containerId = fnSymId)
|
||||
symbols += localSym
|
||||
containerFn.locals += localSym.id
|
||||
@@ -231,7 +234,7 @@ object Binder {
|
||||
val inFn = functions.asSequence()
|
||||
.filter { it.rangeEnd > it.rangeStart && nameStart >= it.rangeStart && nameStart <= it.rangeEnd }
|
||||
.maxByOrNull { it.rangeEnd - it.rangeStart }
|
||||
val kind = if (kw.equals("var", true)) SymbolKind.Var else SymbolKind.Val
|
||||
val kind = if (kw.equals("var", true)) SymbolKind.Variable else SymbolKind.Value
|
||||
if (inFn != null) {
|
||||
val localSym = Symbol(nextId++, text.substring(nameStart, nameEnd), kind, nameStart, nameEnd, containerId = inFn.id)
|
||||
symbols += localSym
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
|
||||
* 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.
|
||||
@@ -34,11 +34,18 @@ object LyngFormatter {
|
||||
var parenBalance = 0
|
||||
var bracketBalance = 0
|
||||
var prevBracketContinuation = false
|
||||
var inBlockComment = false
|
||||
// We don't keep per-"[" base alignment; continuation rules define alignment.
|
||||
|
||||
fun codePart(s: String): String {
|
||||
val idx = s.indexOf("//")
|
||||
return if (idx >= 0) s.substring(0, idx) else s
|
||||
fun updateBalances(ch: Char) {
|
||||
when (ch) {
|
||||
'{' -> blockLevel++
|
||||
'}' -> if (blockLevel > 0) blockLevel--
|
||||
'(' -> parenBalance++
|
||||
')' -> if (parenBalance > 0) parenBalance--
|
||||
'[' -> bracketBalance++
|
||||
']' -> if (bracketBalance > 0) bracketBalance--
|
||||
}
|
||||
}
|
||||
|
||||
fun indentOf(level: Int, continuation: Int): String =
|
||||
@@ -57,13 +64,20 @@ object LyngFormatter {
|
||||
}
|
||||
|
||||
for ((i, rawLine) in lines.withIndex()) {
|
||||
val line = rawLine
|
||||
val code = codePart(line)
|
||||
val (parts, nextInBlockComment) = splitIntoParts(rawLine, inBlockComment)
|
||||
val code = parts.filter { it.type == PartType.Code }.joinToString("") { it.text }
|
||||
val trimmedStart = code.dropWhile { it == ' ' || it == '\t' }
|
||||
val trimmedLine = rawLine.trim()
|
||||
|
||||
// Compute effective indent level for this line
|
||||
var effectiveLevel = blockLevel
|
||||
if (trimmedStart.startsWith("}")) effectiveLevel = (effectiveLevel - 1).coerceAtLeast(0)
|
||||
if (inBlockComment) {
|
||||
if (!trimmedLine.startsWith("*/")) {
|
||||
effectiveLevel += 1
|
||||
}
|
||||
} else if (trimmedStart.startsWith("}")) {
|
||||
effectiveLevel = (effectiveLevel - 1).coerceAtLeast(0)
|
||||
}
|
||||
// else/catch/finally should align with the parent block level; no extra dedent here,
|
||||
// because the preceding '}' has already reduced [blockLevel] appropriately.
|
||||
|
||||
@@ -74,15 +88,7 @@ object LyngFormatter {
|
||||
if (applyAwaiting) effectiveLevel += 1
|
||||
|
||||
val firstChar = trimmedStart.firstOrNull()
|
||||
// Do not apply continuation on a line that starts with a closer ')' or ']'
|
||||
val startsWithCloser = firstChar == ')' || firstChar == ']'
|
||||
// Kotlin-like rule: continuation persists while inside parentheses; it applies
|
||||
// even on the line that starts with ')'. For brackets, do not apply on the ']' line itself.
|
||||
// Continuation rules:
|
||||
// - For brackets: one-shot continuation for first element after '[', and while inside brackets
|
||||
// (except on the ']' line) continuation equals one unit.
|
||||
// - For parentheses: continuation depth scales with nested level; e.g., inside two nested
|
||||
// parentheses lines get 2 * continuationIndentSize. No continuation on a line that starts with ')'.
|
||||
// While inside parentheses, continuation applies scaled by nesting level
|
||||
val parenContLevels = if (parenBalance > 0 && firstChar != ')') parenBalance else 0
|
||||
val continuation = when {
|
||||
// One-shot continuation when previous line ended with '[' to align first element
|
||||
@@ -91,6 +97,7 @@ object LyngFormatter {
|
||||
bracketBalance > 0 && firstChar != ']' -> config.continuationIndentSize
|
||||
// While inside parentheses, continuation applies scaled by nesting level
|
||||
parenContLevels > 0 -> config.continuationIndentSize * parenContLevels
|
||||
trimmedStart.startsWith(".") -> config.indentSize
|
||||
else -> 0
|
||||
}
|
||||
|
||||
@@ -100,8 +107,8 @@ object LyngFormatter {
|
||||
}
|
||||
|
||||
// Replace leading whitespace with the exact target indent; but keep fully blank lines truly empty
|
||||
val contentStart = line.indexOfFirst { it != ' ' && it != '\t' }.let { if (it < 0) line.length else it }
|
||||
var content = line.substring(contentStart)
|
||||
val contentStart = rawLine.indexOfFirst { it != ' ' && it != '\t' }.let { if (it < 0) rawLine.length else it }
|
||||
var content = rawLine.substring(contentStart)
|
||||
// Collapse spaces right after an opening '[' to avoid "[ 1"; make it "[1"
|
||||
if (content.startsWith("[")) {
|
||||
content = "[" + content.drop(1).trimStart()
|
||||
@@ -115,11 +122,7 @@ object LyngFormatter {
|
||||
}
|
||||
// Determine base indent using structural level and continuation only (spaces only)
|
||||
val indentString = indentOf(effectiveLevel, continuation)
|
||||
if (content.isEmpty()) {
|
||||
// preserve truly blank line as empty to avoid trailing spaces on empty lines
|
||||
// (also keeps continuation blocks visually clean)
|
||||
// do nothing, just append nothing; newline will be appended below if needed
|
||||
} else {
|
||||
if (content.isNotEmpty()) {
|
||||
sb.append(indentString).append(content)
|
||||
}
|
||||
|
||||
@@ -127,14 +130,12 @@ object LyngFormatter {
|
||||
if (i < lines.lastIndex) sb.append('\n')
|
||||
|
||||
// Update balances using this line's code content
|
||||
for (ch in code) when (ch) {
|
||||
'{' -> blockLevel++
|
||||
'}' -> if (blockLevel > 0) blockLevel--
|
||||
'(' -> parenBalance++
|
||||
')' -> if (parenBalance > 0) parenBalance--
|
||||
'[' -> bracketBalance++
|
||||
']' -> if (bracketBalance > 0) bracketBalance--
|
||||
for (part in parts) {
|
||||
if (part.type == PartType.Code) {
|
||||
for (ch in part.text) updateBalances(ch)
|
||||
}
|
||||
}
|
||||
inBlockComment = nextInBlockComment
|
||||
|
||||
// Update awaitingSingleIndent based on current line
|
||||
if (applyAwaiting && trimmedStart.isNotEmpty()) {
|
||||
@@ -149,70 +150,41 @@ object LyngFormatter {
|
||||
}
|
||||
|
||||
// Prepare one-shot bracket continuation if the current line ends with '['
|
||||
// (first element line gets continuation even before balances update propagate).
|
||||
val endsWithBracket = code.trimEnd().endsWith("[")
|
||||
// Reset one-shot flag after we used it on this line
|
||||
if (prevBracketContinuation) prevBracketContinuation = false
|
||||
// Set for the next iteration if current line ends with '['
|
||||
// Record whether THIS line ends with an opening '[' so the NEXT line gets a one-shot
|
||||
// continuation indent for the first element.
|
||||
if (endsWithBracket) {
|
||||
// One-shot continuation for the very next line
|
||||
prevBracketContinuation = true
|
||||
} else {
|
||||
// Reset the one-shot flag if the previous line didn't end with '['
|
||||
// Reset the one-shot flag if it was used or if line doesn't end with '['
|
||||
prevBracketContinuation = false
|
||||
}
|
||||
}
|
||||
return sb.toString()
|
||||
}
|
||||
|
||||
/** Full format. Currently performs indentation only; spacing/wrapping can be added later. */
|
||||
fun format(text: String, config: LyngFormatConfig = LyngFormatConfig()): String {
|
||||
// Phase 1: indentation
|
||||
val indented = reindent(text, config)
|
||||
if (!config.applySpacing && !config.applyWrapping) return indented
|
||||
|
||||
// Phase 2: minimal, safe spacing (PSI-free). Skip block comments completely and
|
||||
// only apply spacing to the part before '//' on each line.
|
||||
// Phase 2: minimal, safe spacing (PSI-free).
|
||||
val lines = indented.split('\n')
|
||||
val out = StringBuilder(indented.length)
|
||||
var inBlockComment = false
|
||||
for ((i, rawLine) in lines.withIndex()) {
|
||||
var line = rawLine
|
||||
if (config.applySpacing) {
|
||||
if (inBlockComment) {
|
||||
// Pass-through until we see the end of the block comment on some line
|
||||
val end = line.indexOf("*/")
|
||||
if (end >= 0) {
|
||||
inBlockComment = false
|
||||
}
|
||||
} else {
|
||||
// If this line opens a block comment, apply spacing only before the opener
|
||||
val startIdx = line.indexOf("/*")
|
||||
val endIdx = line.indexOf("*/")
|
||||
if (startIdx >= 0 && (endIdx < 0 || endIdx < startIdx)) {
|
||||
val before = line.substring(0, startIdx)
|
||||
val after = line.substring(startIdx)
|
||||
val commentIdx = before.indexOf("//")
|
||||
val code = if (commentIdx >= 0) before.substring(0, commentIdx) else before
|
||||
val tail = if (commentIdx >= 0) before.substring(commentIdx) else ""
|
||||
val spaced = applyMinimalSpacing(code)
|
||||
line = (spaced + tail) + after
|
||||
inBlockComment = true
|
||||
val (parts, nextInBlockComment) = splitIntoParts(rawLine, inBlockComment)
|
||||
val sb = StringBuilder()
|
||||
for (part in parts) {
|
||||
if (part.type == PartType.Code) {
|
||||
sb.append(applyMinimalSpacingRules(part.text))
|
||||
} else {
|
||||
// Normal code line: respect single-line comments
|
||||
val commentIdx = line.indexOf("//")
|
||||
if (commentIdx >= 0) {
|
||||
val code = line.substring(0, commentIdx)
|
||||
val tail = line.substring(commentIdx)
|
||||
val spaced = applyMinimalSpacing(code)
|
||||
line = spaced + tail
|
||||
} else {
|
||||
line = applyMinimalSpacing(line)
|
||||
}
|
||||
sb.append(part.text)
|
||||
}
|
||||
}
|
||||
line = sb.toString()
|
||||
inBlockComment = nextInBlockComment
|
||||
}
|
||||
out.append(line.trimEnd())
|
||||
if (i < lines.lastIndex) out.append('\n')
|
||||
@@ -369,7 +341,80 @@ object LyngFormatter {
|
||||
}
|
||||
}
|
||||
|
||||
private fun applyMinimalSpacing(code: String): String {
|
||||
private enum class PartType { Code, StringLiteral, BlockComment, LineComment }
|
||||
private data class Part(val text: String, val type: PartType)
|
||||
|
||||
/**
|
||||
* Split a line into parts: code, string literals, and comments.
|
||||
* Tracks [inBlockComment] state across lines.
|
||||
*/
|
||||
private fun splitIntoParts(
|
||||
text: String,
|
||||
inBlockCommentInitial: Boolean
|
||||
): Pair<List<Part>, Boolean> {
|
||||
val result = mutableListOf<Part>()
|
||||
var i = 0
|
||||
var last = 0
|
||||
var inBlockComment = inBlockCommentInitial
|
||||
var inString = false
|
||||
var quoteChar = ' '
|
||||
|
||||
while (i < text.length) {
|
||||
if (inBlockComment) {
|
||||
if (text.startsWith("*/", i)) {
|
||||
result.add(Part(text.substring(last, i + 2), PartType.BlockComment))
|
||||
inBlockComment = false
|
||||
i += 2
|
||||
last = i
|
||||
} else {
|
||||
i++
|
||||
}
|
||||
} else if (inString) {
|
||||
if (text[i] == quoteChar) {
|
||||
var escapeCount = 0
|
||||
var j = i - 1
|
||||
while (j >= 0 && text[j] == '\\') {
|
||||
escapeCount++
|
||||
j--
|
||||
}
|
||||
if (escapeCount % 2 == 0) {
|
||||
inString = false
|
||||
result.add(Part(text.substring(last, i + 1), PartType.StringLiteral))
|
||||
last = i + 1
|
||||
}
|
||||
}
|
||||
i++
|
||||
} else {
|
||||
if (text.startsWith("//", i)) {
|
||||
if (i > last) result.add(Part(text.substring(last, i), PartType.Code))
|
||||
result.add(Part(text.substring(i), PartType.LineComment))
|
||||
last = text.length
|
||||
break
|
||||
} else if (text.startsWith("/*", i)) {
|
||||
if (i > last) result.add(Part(text.substring(last, i), PartType.Code))
|
||||
inBlockComment = true
|
||||
last = i
|
||||
i += 2
|
||||
} else if (text[i] == '"' || text[i] == '\'') {
|
||||
if (i > last) result.add(Part(text.substring(last, i), PartType.Code))
|
||||
inString = true
|
||||
quoteChar = text[i]
|
||||
last = i
|
||||
i++
|
||||
} else {
|
||||
i++
|
||||
}
|
||||
}
|
||||
}
|
||||
if (last < text.length) {
|
||||
val leftover = text.substring(last)
|
||||
val type = if (inBlockComment) PartType.BlockComment else PartType.Code
|
||||
result.add(Part(leftover, type))
|
||||
}
|
||||
return result to inBlockComment
|
||||
}
|
||||
|
||||
private fun applyMinimalSpacingRules(code: String): String {
|
||||
var s = code
|
||||
// Ensure space before '(' for control-flow keywords
|
||||
s = s.replace(Regex("\\b(if|for|while)\\("), "$1 (")
|
||||
@@ -408,15 +453,16 @@ private fun applyMinimalSpacing(code: String): String {
|
||||
s = s.replace(Regex(", \\)"), ",)")
|
||||
s = s.replace(Regex(", \\]"), ",]")
|
||||
// Equality/boolean operators: ensure spaces around
|
||||
s = s.replace(Regex("\\s*(==|!=|<=|>=|&&|\\|\\|)\\s*"), " $1 ")
|
||||
// Assignment '=' (not part of '==', '!=', '<=', '>=', '=>'): collapse whitespace to single spaces around '='
|
||||
s = s.replace(Regex("(?<![=!<>])\\s*=\\s*(?![=>])"), " = ")
|
||||
// Multiply/Divide/Mod as binary: spaces around
|
||||
s = s.replace(Regex("\\s*([*/%])\\s*"), " $1 ")
|
||||
s = s.replace(Regex("\\s*(==|!=|<=|>=|&&|\\|\\||\\+=|-=|\\*=|/=|=>|->)\\s*"), " $1 ")
|
||||
// Assignment '=' (not part of '==', '!=', '<=', '>=', '=>', '+=', '-=', '*=', '/=', '->'): collapse whitespace to single spaces around '='
|
||||
s = s.replace(Regex("(?<![=!<>+\\-*/])\\s*=\\s*(?![=>])"), " = ")
|
||||
// Multiply/Divide/Mod as binary: spaces around. Avoid '*=', '/='
|
||||
s = s.replace(Regex("(?<![*])\\s*([*/%])\\s*(?![=])"), " $1 ")
|
||||
// Addition as binary: spaces around '+'. We try not to break '++' and '+=' here.
|
||||
s = s.replace(Regex("(?<![+])\\s*\\+\\s*(?![+=])"), " + ")
|
||||
// Subtraction as binary: add spaces when it looks binary. Keep unary '-' tight (after start or ( [ { = : ,)
|
||||
s = s.replace(Regex("(?<=[^\\s(\\[\\{=:,])-(?=[^=])"), " - ")
|
||||
// and avoid splitting '-=', '--', '->'
|
||||
s = s.replace(Regex("(?<=[^\\s(\\[\\{=:,])\\s*-\\s*(?=[^=\\->])"), " - ")
|
||||
// Colon in types/extends: remove spaces before, ensure one space after (keep '::' intact)
|
||||
s = s.replace(Regex("(?<!:)\\s*:(?!:)\\s*"), ": ")
|
||||
return s
|
||||
|
||||
@@ -40,6 +40,8 @@ enum class HighlightKind {
|
||||
Label,
|
||||
Directive,
|
||||
Error,
|
||||
/** Enum constant (both declaration and usage). */
|
||||
EnumConstant,
|
||||
}
|
||||
|
||||
/** A highlighted span: character range and its semantic/lexical kind. */
|
||||
|
||||
@@ -159,8 +159,10 @@ class SimpleLyngHighlighter : LyngHighlighter {
|
||||
}
|
||||
if (range.endExclusive > range.start) raw += HighlightSpan(range, k)
|
||||
}
|
||||
// Heuristics: mark enum constants in declaration blocks and on qualified usages Foo.BAR
|
||||
val overridden = applyEnumConstantHeuristics(text, src, tokens, raw)
|
||||
// Adjust single-line comment spans to extend till EOL to compensate for lexer offset/length quirks
|
||||
val adjusted = extendSingleLineCommentsToEol(text, raw)
|
||||
val adjusted = extendSingleLineCommentsToEol(text, overridden)
|
||||
// Spans are in order; merge adjacent of the same kind for compactness
|
||||
return mergeAdjacent(adjusted)
|
||||
}
|
||||
@@ -202,3 +204,81 @@ private fun extendSingleLineCommentsToEol(
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
/**
|
||||
* Detect enum constants both in enum declarations and in qualified usages (TypeName.CONST)
|
||||
* and override corresponding identifier spans with EnumConstant kind.
|
||||
*/
|
||||
private fun applyEnumConstantHeuristics(
|
||||
text: String,
|
||||
src: Source,
|
||||
tokens: List<net.sergeych.lyng.Token>,
|
||||
spans: MutableList<HighlightSpan>
|
||||
): MutableList<HighlightSpan> {
|
||||
if (tokens.isEmpty() || spans.isEmpty()) return spans
|
||||
|
||||
// Build quick lookup from range start to span index for identifiers only
|
||||
val byStart = HashMap<Int, Int>(spans.size * 2)
|
||||
for (i in spans.indices) {
|
||||
val s = spans[i]
|
||||
if (s.kind == HighlightKind.Identifier) byStart[s.range.start] = i
|
||||
}
|
||||
|
||||
fun overrideIdAtToken(idx: Int) {
|
||||
val t = tokens[idx]
|
||||
if (t.type != Type.ID) return
|
||||
val start = src.offsetOf(t.pos)
|
||||
val spanIndex = byStart[start] ?: return
|
||||
spans[spanIndex] = HighlightSpan(spans[spanIndex].range, HighlightKind.EnumConstant)
|
||||
}
|
||||
|
||||
// 1) Enum declarations: enum Name { CONST1, CONST2 }
|
||||
var i = 0
|
||||
while (i < tokens.size) {
|
||||
val t = tokens[i]
|
||||
if (t.type == Type.ID && t.value.equals("enum", ignoreCase = true)) {
|
||||
// expect: ID(enum) ID(name) LBRACE (ID (COMMA ID)* ) RBRACE
|
||||
var j = i + 1
|
||||
// skip optional whitespace/newlines tokens are separate types, so we just check IDs and braces
|
||||
if (j < tokens.size && tokens[j].type == Type.ID) j++ else { i++; continue }
|
||||
if (j < tokens.size && tokens[j].type == Type.LBRACE) {
|
||||
j++
|
||||
while (j < tokens.size) {
|
||||
val tk = tokens[j]
|
||||
if (tk.type == Type.RBRACE) { j++; break }
|
||||
if (tk.type == Type.ID) {
|
||||
// enum entry declaration
|
||||
overrideIdAtToken(j)
|
||||
j++
|
||||
// optional comma
|
||||
if (j < tokens.size && tokens[j].type == Type.COMMA) { j++ ; continue }
|
||||
continue
|
||||
}
|
||||
// Any unexpected token ends enum entries scan
|
||||
break
|
||||
}
|
||||
i = j
|
||||
continue
|
||||
}
|
||||
}
|
||||
i++
|
||||
}
|
||||
|
||||
// 2) Qualified usages: Something.CONST where CONST is ALL_UPPERCASE (with digits/underscores)
|
||||
fun isAllUpperCase(name: String): Boolean = name.isNotEmpty() && name.all { it == '_' || it.isDigit() || (it.isLetter() && it.isUpperCase()) }
|
||||
i = 1
|
||||
while (i + 0 < tokens.size) {
|
||||
val dotTok = tokens[i]
|
||||
if (dotTok.type == Type.DOT && i + 1 < tokens.size) {
|
||||
val next = tokens[i + 1]
|
||||
if (next.type == Type.ID && isAllUpperCase(next.value)) {
|
||||
overrideIdAtToken(i + 1)
|
||||
i += 2
|
||||
continue
|
||||
}
|
||||
}
|
||||
i++
|
||||
}
|
||||
|
||||
return spans
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
|
||||
* 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.
|
||||
@@ -22,6 +22,7 @@
|
||||
package net.sergeych.lyng.miniast
|
||||
|
||||
import net.sergeych.lyng.Pos
|
||||
import net.sergeych.lyng.stdlib_included.rootLyng
|
||||
|
||||
// ---------------- Types DSL ----------------
|
||||
|
||||
@@ -67,8 +68,14 @@ object BuiltinDocRegistry : BuiltinDocSource {
|
||||
}
|
||||
|
||||
override fun docsForModule(moduleName: String): List<MiniDecl> {
|
||||
modules[moduleName]?.let { return it }
|
||||
// Try lazy supplier once
|
||||
// If module already present but we also have a lazy supplier for it, merge supplier once
|
||||
modules[moduleName]?.let { existing ->
|
||||
lazySuppliers.remove(moduleName)?.invoke()?.let { built ->
|
||||
existing += built
|
||||
}
|
||||
return existing
|
||||
}
|
||||
// Try lazy supplier once when module is not present
|
||||
val built = lazySuppliers.remove(moduleName)?.invoke()
|
||||
if (built != null) {
|
||||
val list = modules.getOrPut(moduleName) { mutableListOf() }
|
||||
@@ -96,6 +103,26 @@ object BuiltinDocRegistry : BuiltinDocSource {
|
||||
init {
|
||||
registerLazy("lyng.stdlib") { buildStdlibDocs() }
|
||||
}
|
||||
|
||||
/**
|
||||
* List names of extension members defined for [className] in the stdlib text (`root.lyng`).
|
||||
* We do a lightweight regex scan like: `fun ClassName.methodName(` or `val ClassName.propName`
|
||||
* and collect distinct names.
|
||||
*/
|
||||
fun extensionMemberNamesFor(className: String): List<String> {
|
||||
val src = try { rootLyng } catch (_: Throwable) { null } ?: return emptyList()
|
||||
val out = LinkedHashSet<String>()
|
||||
// Match lines like: fun String.trim(...) or val Int.isEven = ...
|
||||
val re = Regex("^\\s*(?:fun|val|var)\\s+${className}\\.([A-Za-z_][A-Za-z0-9_]*)\\b", RegexOption.MULTILINE)
|
||||
re.findAll(src).forEach { m ->
|
||||
val name = m.groupValues.getOrNull(1)?.trim()
|
||||
if (!name.isNullOrEmpty()) out.add(name)
|
||||
}
|
||||
return out.toList()
|
||||
}
|
||||
|
||||
@Deprecated("Use extensionMemberNamesFor", ReplaceWith("extensionMemberNamesFor(className)"))
|
||||
fun extensionMethodNamesFor(className: String): List<String> = extensionMemberNamesFor(className)
|
||||
}
|
||||
|
||||
// ---------------- Builders ----------------
|
||||
@@ -262,7 +289,125 @@ internal fun TypeDoc.toMiniTypeRef(): MiniTypeRef = when (this) {
|
||||
|
||||
// ---------------- Built-in module doc seeds ----------------
|
||||
|
||||
// Seed docs for lyng.stdlib lazily to avoid init-order coupling.
|
||||
// ---------------- Inline docs support (.lyng source) ----------------
|
||||
|
||||
/**
|
||||
* Lightweight, single-pass scanner that extracts inline doc comments from the stdlib .lyng source
|
||||
* and associates them with declarations (top-level functions, classes, and class methods).
|
||||
* It is intentionally conservative and only recognizes simple patterns present in stdlib sources.
|
||||
*
|
||||
* The scan is cached; performed at most once per process.
|
||||
*/
|
||||
private object StdlibInlineDocIndex {
|
||||
private var built = false
|
||||
|
||||
// Keys for matching declaration docs
|
||||
private sealed interface Key {
|
||||
data class TopFun(val name: String) : Key
|
||||
data class Clazz(val name: String) : Key
|
||||
data class Method(val className: String, val name: String) : Key
|
||||
}
|
||||
|
||||
private val docs: MutableMap<Key, String> = mutableMapOf()
|
||||
|
||||
private fun putIfAbsent(k: Key, doc: String) {
|
||||
if (doc.isBlank()) return
|
||||
if (!docs.containsKey(k)) docs[k] = doc.trim()
|
||||
}
|
||||
|
||||
private fun buildOnce() {
|
||||
if (built) return
|
||||
built = true
|
||||
val text = try { rootLyng } catch (_: Throwable) { null } ?: return
|
||||
|
||||
// Simple line-based scan. Collect a doc block immediately preceding a declaration.
|
||||
val lines = text.lines()
|
||||
val buf = mutableListOf<String>()
|
||||
var inBlock = false
|
||||
var prevWasComment = false
|
||||
|
||||
fun flushTo(key: Key) {
|
||||
if (buf.isNotEmpty()) {
|
||||
val raw = buf.joinToString("\n").trimEnd()
|
||||
putIfAbsent(key, raw)
|
||||
buf.clear()
|
||||
}
|
||||
}
|
||||
|
||||
for (rawLine in lines) {
|
||||
val line = rawLine.trim()
|
||||
when {
|
||||
// Multiline block comment begin/end
|
||||
line.startsWith("/*") && !inBlock -> {
|
||||
inBlock = true
|
||||
val inner = line.removePrefix("/*").let { l -> if (l.endsWith("*/")) l.removeSuffix("*/") else l }
|
||||
buf += inner
|
||||
prevWasComment = true
|
||||
if (line.endsWith("*/")) inBlock = false
|
||||
continue
|
||||
}
|
||||
inBlock -> {
|
||||
val content = if (line.endsWith("*/")) {
|
||||
inBlock = false
|
||||
line.removeSuffix("*/")
|
||||
} else line
|
||||
// Trim leading '*' like Javadoc style
|
||||
val t = content.trimStart()
|
||||
buf += if (t.startsWith("*")) t.removePrefix("*").trimStart() else content
|
||||
prevWasComment = true
|
||||
continue
|
||||
}
|
||||
line.startsWith("//") -> {
|
||||
buf += line.removePrefix("//")
|
||||
prevWasComment = true
|
||||
continue
|
||||
}
|
||||
line.isBlank() -> {
|
||||
// Blank line breaks doc association unless it immediately follows a comment
|
||||
if (!prevWasComment) buf.clear()
|
||||
prevWasComment = false
|
||||
continue
|
||||
}
|
||||
else -> {
|
||||
// Non-comment, non-blank: try to match a declaration just after comments
|
||||
if (buf.isNotEmpty()) {
|
||||
// fun/val/var Class.name( ... )
|
||||
val mExt = Regex("^(?:fun|val|var)\\s+([A-Za-z_][A-Za-z0-9_]*)\\.([A-Za-z_][A-Za-z0-9_]*)\\b").find(line)
|
||||
if (mExt != null) {
|
||||
val (cls, name) = mExt.destructured
|
||||
flushTo(Key.Method(cls, name))
|
||||
} else {
|
||||
// fun name( ... )
|
||||
val mTop = Regex("^fun\\s+([A-Za-z_][A-Za-z0-9_]*)\\s*\\(").find(line)
|
||||
if (mTop != null) {
|
||||
val (name) = mTop.destructured
|
||||
flushTo(Key.TopFun(name))
|
||||
} else {
|
||||
// class Name
|
||||
val mClass = Regex("^class\\s+([A-Za-z_][A-Za-z0-9_]*)\\b").find(line)
|
||||
if (mClass != null) {
|
||||
val (name) = mClass.destructured
|
||||
flushTo(Key.Clazz(name))
|
||||
} else {
|
||||
// Unrecognized line – drop buffer to avoid leaking to unrelated code
|
||||
buf.clear()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
prevWasComment = false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Public API: fetch doc text if present
|
||||
fun topFunDoc(name: String): String? { buildOnce(); return docs[Key.TopFun(name)] }
|
||||
fun classDoc(name: String): String? { buildOnce(); return docs[Key.Clazz(name)] }
|
||||
fun methodDoc(className: String, name: String): String? { buildOnce(); return docs[Key.Method(className, name)] }
|
||||
}
|
||||
|
||||
// Seed docs for lyng.stdlib lazily to avoid init-order coupling and prefer inline docs where present.
|
||||
private fun buildStdlibDocs(): List<MiniDecl> {
|
||||
val decls = mutableListOf<MiniDecl>()
|
||||
// Use the same DSL builders to construct decls
|
||||
@@ -270,7 +415,7 @@ private fun buildStdlibDocs(): List<MiniDecl> {
|
||||
// Printing
|
||||
mod.funDoc(
|
||||
name = "print",
|
||||
doc = """
|
||||
doc = StdlibInlineDocIndex.topFunDoc("print") ?: """
|
||||
Print values to the standard output without a trailing newline.
|
||||
Accepts any number of arguments and prints them separated by a space.
|
||||
""".trimIndent(),
|
||||
@@ -279,7 +424,7 @@ private fun buildStdlibDocs(): List<MiniDecl> {
|
||||
)
|
||||
mod.funDoc(
|
||||
name = "println",
|
||||
doc = """
|
||||
doc = StdlibInlineDocIndex.topFunDoc("println") ?: """
|
||||
Print values to the standard output and append a newline.
|
||||
Accepts any number of arguments and prints them separated by a space.
|
||||
""".trimIndent(),
|
||||
@@ -288,7 +433,7 @@ private fun buildStdlibDocs(): List<MiniDecl> {
|
||||
// Caching helper
|
||||
mod.funDoc(
|
||||
name = "cached",
|
||||
doc = """
|
||||
doc = StdlibInlineDocIndex.topFunDoc("cached") ?: """
|
||||
Wrap a `builder` into a zero-argument thunk that computes once and caches the result.
|
||||
The first call invokes `builder()` and stores the value; subsequent calls return the cached value.
|
||||
""".trimIndent(),
|
||||
@@ -298,44 +443,44 @@ private fun buildStdlibDocs(): List<MiniDecl> {
|
||||
// Math helpers (scalar versions)
|
||||
fun math1(name: String) = mod.funDoc(
|
||||
name = name,
|
||||
doc = "Compute $name(x).",
|
||||
doc = StdlibInlineDocIndex.topFunDoc(name) ?: "Compute $name(x).",
|
||||
params = listOf(ParamDoc("x", type("lyng.Number")))
|
||||
)
|
||||
math1("sin"); math1("cos"); math1("tan"); math1("asin"); math1("acos"); math1("atan")
|
||||
mod.funDoc(name = "floor", doc = "Round down the number to the nearest integer.", params = listOf(ParamDoc("x", type("lyng.Number"))))
|
||||
mod.funDoc(name = "ceil", doc = "Round up the number to the nearest integer.", params = listOf(ParamDoc("x", type("lyng.Number"))))
|
||||
mod.funDoc(name = "round", doc = "Round the number to the nearest integer.", params = listOf(ParamDoc("x", type("lyng.Number"))))
|
||||
mod.funDoc(name = "floor", doc = StdlibInlineDocIndex.topFunDoc("floor") ?: "Round down the number to the nearest integer.", params = listOf(ParamDoc("x", type("lyng.Number"))))
|
||||
mod.funDoc(name = "ceil", doc = StdlibInlineDocIndex.topFunDoc("ceil") ?: "Round up the number to the nearest integer.", params = listOf(ParamDoc("x", type("lyng.Number"))))
|
||||
mod.funDoc(name = "round", doc = StdlibInlineDocIndex.topFunDoc("round") ?: "Round the number to the nearest integer.", params = listOf(ParamDoc("x", type("lyng.Number"))))
|
||||
|
||||
// Hyperbolic and inverse hyperbolic
|
||||
math1("sinh"); math1("cosh"); math1("tanh"); math1("asinh"); math1("acosh"); math1("atanh")
|
||||
|
||||
// Exponentials and logarithms
|
||||
mod.funDoc(name = "exp", doc = "Euler's exponential e^x.", params = listOf(ParamDoc("x", type("lyng.Number"))))
|
||||
mod.funDoc(name = "ln", doc = "Natural logarithm (base e).", params = listOf(ParamDoc("x", type("lyng.Number"))))
|
||||
mod.funDoc(name = "log10", doc = "Logarithm base 10.", params = listOf(ParamDoc("x", type("lyng.Number"))))
|
||||
mod.funDoc(name = "log2", doc = "Logarithm base 2.", params = listOf(ParamDoc("x", type("lyng.Number"))))
|
||||
mod.funDoc(name = "exp", doc = StdlibInlineDocIndex.topFunDoc("exp") ?: "Euler's exponential e^x.", params = listOf(ParamDoc("x", type("lyng.Number"))))
|
||||
mod.funDoc(name = "ln", doc = StdlibInlineDocIndex.topFunDoc("ln") ?: "Natural logarithm (base e).", params = listOf(ParamDoc("x", type("lyng.Number"))))
|
||||
mod.funDoc(name = "log10", doc = StdlibInlineDocIndex.topFunDoc("log10") ?: "Logarithm base 10.", params = listOf(ParamDoc("x", type("lyng.Number"))))
|
||||
mod.funDoc(name = "log2", doc = StdlibInlineDocIndex.topFunDoc("log2") ?: "Logarithm base 2.", params = listOf(ParamDoc("x", type("lyng.Number"))))
|
||||
|
||||
// Power/roots and absolute value
|
||||
mod.funDoc(
|
||||
name = "pow",
|
||||
doc = "Raise `x` to the power `y`.",
|
||||
doc = StdlibInlineDocIndex.topFunDoc("pow") ?: "Raise `x` to the power `y`.",
|
||||
params = listOf(ParamDoc("x", type("lyng.Number")), ParamDoc("y", type("lyng.Number")))
|
||||
)
|
||||
mod.funDoc(
|
||||
name = "sqrt",
|
||||
doc = "Square root of `x`.",
|
||||
doc = StdlibInlineDocIndex.topFunDoc("sqrt") ?: "Square root of `x`.",
|
||||
params = listOf(ParamDoc("x", type("lyng.Number")))
|
||||
)
|
||||
mod.funDoc(
|
||||
name = "abs",
|
||||
doc = "Absolute value of a number (works for Int and Real).",
|
||||
doc = StdlibInlineDocIndex.topFunDoc("abs") ?: "Absolute value of a number (works for Int and Real).",
|
||||
params = listOf(ParamDoc("x", type("lyng.Number")))
|
||||
)
|
||||
|
||||
// Assertions and checks
|
||||
mod.funDoc(
|
||||
name = "assert",
|
||||
doc = """
|
||||
doc = StdlibInlineDocIndex.topFunDoc("assert") ?: """
|
||||
Assert that `cond` is true, otherwise throw an `AssertionFailedException`.
|
||||
Optionally provide a `message`.
|
||||
""".trimIndent(),
|
||||
@@ -343,17 +488,17 @@ private fun buildStdlibDocs(): List<MiniDecl> {
|
||||
)
|
||||
mod.funDoc(
|
||||
name = "assertEquals",
|
||||
doc = "Assert that `a == b`, otherwise throw an assertion error.",
|
||||
doc = StdlibInlineDocIndex.topFunDoc("assertEquals") ?: "Assert that `a == b`, otherwise throw an assertion error.",
|
||||
params = listOf(ParamDoc("a"), ParamDoc("b"))
|
||||
)
|
||||
mod.funDoc(
|
||||
name = "assertNotEquals",
|
||||
doc = "Assert that `a != b`, otherwise throw an assertion error.",
|
||||
doc = StdlibInlineDocIndex.topFunDoc("assertNotEquals") ?: "Assert that `a != b`, otherwise throw an assertion error.",
|
||||
params = listOf(ParamDoc("a"), ParamDoc("b"))
|
||||
)
|
||||
mod.funDoc(
|
||||
name = "assertThrows",
|
||||
doc = """
|
||||
doc = StdlibInlineDocIndex.topFunDoc("assertThrows") ?: """
|
||||
Execute `code` and return the thrown `Exception` object.
|
||||
If nothing is thrown, an assertion error is raised.
|
||||
""".trimIndent(),
|
||||
@@ -364,123 +509,134 @@ private fun buildStdlibDocs(): List<MiniDecl> {
|
||||
// Utilities
|
||||
mod.funDoc(
|
||||
name = "dynamic",
|
||||
doc = "Wrap a value into a dynamic object that defers resolution to runtime.",
|
||||
doc = StdlibInlineDocIndex.topFunDoc("dynamic") ?: "Wrap a value into a dynamic object that defers resolution to runtime.",
|
||||
params = listOf(ParamDoc("value"))
|
||||
)
|
||||
mod.funDoc(
|
||||
name = "require",
|
||||
doc = "Require `cond` to be true, else throw `IllegalArgumentException` with optional `message`.",
|
||||
doc = StdlibInlineDocIndex.topFunDoc("require") ?: "Require `cond` to be true, else throw `IllegalArgumentException` with optional `message`.",
|
||||
params = listOf(ParamDoc("cond", type("lyng.Bool")), ParamDoc("message"))
|
||||
)
|
||||
mod.funDoc(
|
||||
name = "check",
|
||||
doc = "Check `cond` is true, else throw `IllegalStateException` with optional `message`.",
|
||||
doc = StdlibInlineDocIndex.topFunDoc("check") ?: "Check `cond` is true, else throw `IllegalStateException` with optional `message`.",
|
||||
params = listOf(ParamDoc("cond", type("lyng.Bool")), ParamDoc("message"))
|
||||
)
|
||||
mod.funDoc(
|
||||
name = "traceScope",
|
||||
doc = "Print a debug trace of the current scope chain with an optional label.",
|
||||
doc = StdlibInlineDocIndex.topFunDoc("traceScope") ?: "Print a debug trace of the current scope chain with an optional label.",
|
||||
params = listOf(ParamDoc("label", type("lyng.String")))
|
||||
)
|
||||
mod.funDoc(
|
||||
name = "delay",
|
||||
doc = "Suspend for the specified number of milliseconds.",
|
||||
doc = StdlibInlineDocIndex.topFunDoc("delay") ?: "Suspend for the specified number of milliseconds.",
|
||||
params = listOf(ParamDoc("ms", type("lyng.Number")))
|
||||
)
|
||||
|
||||
// Concurrency helpers
|
||||
mod.funDoc(
|
||||
name = "launch",
|
||||
doc = "Launch an asynchronous task and return a `Deferred`.",
|
||||
doc = StdlibInlineDocIndex.topFunDoc("launch") ?: "Launch an asynchronous task and return a `Deferred`.",
|
||||
params = listOf(ParamDoc("code")),
|
||||
returns = type("lyng.Deferred")
|
||||
)
|
||||
mod.funDoc(
|
||||
name = "yield",
|
||||
doc = "Yield to the scheduler, allowing other tasks to run."
|
||||
doc = StdlibInlineDocIndex.topFunDoc("yield") ?: "Yield to the scheduler, allowing other tasks to run."
|
||||
)
|
||||
mod.funDoc(
|
||||
name = "flow",
|
||||
doc = "Create a lazy iterable stream using the provided `builder`.",
|
||||
doc = StdlibInlineDocIndex.topFunDoc("flow") ?: "Create a lazy iterable stream using the provided `builder`.",
|
||||
params = listOf(ParamDoc("builder")),
|
||||
returns = type("lyng.Iterable")
|
||||
)
|
||||
|
||||
// Common Iterable helpers (document top-level extension-like APIs as class members)
|
||||
mod.classDoc(name = "Iterable", doc = "Helper operations for iterable collections.") {
|
||||
method(name = "filter", doc = "Filter elements by predicate.", params = listOf(ParamDoc("predicate")), returns = type("lyng.Iterable"))
|
||||
method(name = "drop", doc = "Skip the first N elements.", params = listOf(ParamDoc("n", type("lyng.Int"))), returns = type("lyng.Iterable"))
|
||||
method(name = "first", doc = "Return the first element or throw if empty.")
|
||||
method(name = "last", doc = "Return the last element or throw if empty.")
|
||||
method(name = "dropLast", doc = "Drop the last N elements.", params = listOf(ParamDoc("n", type("lyng.Int"))), returns = type("lyng.Iterable"))
|
||||
method(name = "takeLast", doc = "Take the last N elements.", params = listOf(ParamDoc("n", type("lyng.Int"))), returns = type("lyng.List"))
|
||||
method(name = "joinToString", doc = "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 = "Return true if any element matches the predicate.", params = listOf(ParamDoc("predicate")), returns = type("lyng.Bool"))
|
||||
method(name = "all", doc = "Return true if all elements match the predicate.", params = listOf(ParamDoc("predicate")), returns = type("lyng.Bool"))
|
||||
method(name = "sum", doc = "Sum all elements; returns null for empty collections.", returns = type("lyng.Number", nullable = true))
|
||||
method(name = "sumOf", doc = "Sum mapped values of elements; returns null for empty collections.", params = listOf(ParamDoc("f")))
|
||||
method(name = "minOf", doc = "Minimum of mapped values.", params = listOf(ParamDoc("lambda")))
|
||||
method(name = "maxOf", doc = "Maximum of mapped values.", params = listOf(ParamDoc("lambda")))
|
||||
method(name = "sorted", doc = "Return elements sorted by natural order.", returns = type("lyng.Iterable"))
|
||||
method(name = "sortedBy", doc = "Return elements sorted by the key selector.", params = listOf(ParamDoc("predicate")), returns = type("lyng.Iterable"))
|
||||
method(name = "shuffled", doc = "Return a shuffled copy as a list.", returns = type("lyng.List"))
|
||||
method(name = "map", doc = "Transform elements by applying `transform`.", params = listOf(ParamDoc("transform")), returns = type("lyng.Iterable"))
|
||||
method(name = "toList", doc = "Collect elements of this iterable into a new list.", returns = type("lyng.List"))
|
||||
mod.classDoc(name = "Iterable", doc = StdlibInlineDocIndex.classDoc("Iterable") ?: "Helper operations for iterable collections.") {
|
||||
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 = "drop", doc = md("drop", "Skip the first N elements."), params = listOf(ParamDoc("n", type("lyng.Int"))), returns = type("lyng.Iterable"))
|
||||
method(name = "first", doc = md("first", "Return the first element or throw if empty."))
|
||||
method(name = "last", doc = md("last", "Return the last element or throw if empty."))
|
||||
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 = "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 = "all", doc = md("all", "Return true if all elements match the predicate."), params = listOf(ParamDoc("predicate")), returns = type("lyng.Bool"))
|
||||
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 = "minOf", doc = md("minOf", "Minimum of mapped values."), params = listOf(ParamDoc("lambda")))
|
||||
method(name = "maxOf", doc = md("maxOf", "Maximum of mapped values."), params = listOf(ParamDoc("lambda")))
|
||||
method(name = "sorted", doc = md("sorted", "Return elements sorted by natural order."), returns = type("lyng.Iterable"))
|
||||
method(name = "sortedBy", doc = md("sortedBy", "Return elements sorted by the key selector."), params = listOf(ParamDoc("predicate")), returns = type("lyng.Iterable"))
|
||||
method(name = "shuffled", doc = md("shuffled", "Return a shuffled copy as a list."), returns = type("lyng.List"))
|
||||
method(name = "map", doc = md("map", "Transform elements by applying `transform`."), params = listOf(ParamDoc("transform")), returns = type("lyng.Iterable"))
|
||||
method(name = "toList", doc = md("toList", "Collect elements of this iterable into a new list."), returns = type("lyng.List"))
|
||||
}
|
||||
|
||||
// List helpers
|
||||
mod.classDoc(name = "List", doc = "List-specific operations.", bases = listOf(type("Collection"), type("Iterable"))) {
|
||||
method(name = "toString", doc = "Return string representation like [a,b,c].", returns = type("lyng.String"))
|
||||
method(name = "sortBy", doc = "Sort list in-place by key selector.", params = listOf(ParamDoc("predicate")))
|
||||
method(name = "sort", doc = "Sort list in-place by natural order.")
|
||||
method(name = "toList", doc = "Return a shallow copy of this list (new list with the same elements).", returns = type("lyng.List"))
|
||||
mod.classDoc(name = "List", doc = StdlibInlineDocIndex.classDoc("List") ?: "List-specific operations.", bases = listOf(type("Collection"), type("Iterable"))) {
|
||||
fun md(name: String, fallback: String) = StdlibInlineDocIndex.methodDoc("List", name) ?: fallback
|
||||
method(name = "toString", doc = md("toString", "Return string representation like [a,b,c]."), returns = type("lyng.String"))
|
||||
method(name = "sortBy", doc = md("sortBy", "Sort list in-place by key selector."), params = listOf(ParamDoc("predicate")))
|
||||
method(name = "sort", doc = md("sort", "Sort list in-place by natural order."))
|
||||
method(name = "toList", doc = md("toList", "Return a shallow copy of this list (new list with the same elements)."), returns = type("lyng.List"))
|
||||
}
|
||||
|
||||
// Collection helpers (supertype for sized collections)
|
||||
mod.classDoc(name = "Collection", doc = "Collection operations common to sized collections.", bases = listOf(type("Iterable"))) {
|
||||
method(name = "size", doc = "Number of elements in the collection.", returns = type("lyng.Int"))
|
||||
method(name = "toList", doc = "Collect elements into a new list.", returns = type("lyng.List"))
|
||||
mod.classDoc(name = "Collection", doc = StdlibInlineDocIndex.classDoc("Collection") ?: "Collection operations common to sized collections.", bases = listOf(type("Iterable"))) {
|
||||
fun md(name: String, fallback: String) = StdlibInlineDocIndex.methodDoc("Collection", name) ?: fallback
|
||||
method(name = "size", doc = md("size", "Number of elements in the collection."), returns = type("lyng.Int"))
|
||||
method(name = "toList", doc = md("toList", "Collect elements into a new list."), returns = type("lyng.List"))
|
||||
}
|
||||
|
||||
// Iterator helpers
|
||||
mod.classDoc(name = "Iterator", doc = "Iterator protocol for sequential access.") {
|
||||
method(name = "hasNext", doc = "Whether another element is available.", returns = type("lyng.Bool"))
|
||||
method(name = "next", doc = "Return the next element.")
|
||||
method(name = "cancelIteration", doc = "Stop the iteration early.")
|
||||
method(name = "toList", doc = "Consume this iterator and collect elements into a list.", returns = type("lyng.List"))
|
||||
mod.classDoc(name = "Iterator", doc = StdlibInlineDocIndex.classDoc("Iterator") ?: "Iterator protocol for sequential access.") {
|
||||
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 = "next", doc = md("next", "Return the next element."))
|
||||
method(name = "cancelIteration", doc = md("cancelIteration", "Stop the iteration early."))
|
||||
method(name = "toList", doc = md("toList", "Consume this iterator and collect elements into a list."), returns = type("lyng.List"))
|
||||
}
|
||||
|
||||
// Exceptions and utilities
|
||||
mod.classDoc(name = "Exception", doc = "Exception helpers.") {
|
||||
method(name = "printStackTrace", doc = "Print this exception and its stack trace to standard output.")
|
||||
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.")
|
||||
}
|
||||
|
||||
mod.classDoc(name = "String", doc = "String helpers.") {
|
||||
method(name = "re", doc = "Compile this string into a regular expression.", returns = type("lyng.Regex"))
|
||||
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 = "ordinal", doc = "Returns the ordinal of this enum constant.", returns = type("lyng.Int"))
|
||||
}
|
||||
|
||||
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.
|
||||
method(name = "re", doc = StdlibInlineDocIndex.methodDoc("String", "re") ?: "Compile this string into a regular expression.", returns = type("lyng.Regex"))
|
||||
}
|
||||
|
||||
// StackTraceEntry structure
|
||||
mod.classDoc(name = "StackTraceEntry", doc = "Represents a single stack trace element.") {
|
||||
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.
|
||||
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 = "column", doc = "Column number (0-based).", type = type("lyng.Int"))
|
||||
field(name = "sourceString", doc = "The source line text.", type = type("lyng.String"))
|
||||
method(name = "toString", doc = "Formatted representation: source:line:column: text.", returns = type("lyng.String"))
|
||||
method(name = "toString", doc = StdlibInlineDocIndex.methodDoc("StackTraceEntry", "toString") ?: "Formatted representation: source:line:column: text.", returns = type("lyng.String"))
|
||||
}
|
||||
|
||||
// Constants and namespaces
|
||||
mod.valDoc(
|
||||
name = "π",
|
||||
doc = "The mathematical constant pi.",
|
||||
doc = StdlibInlineDocIndex.topFunDoc("π") ?: "The mathematical constant pi.",
|
||||
type = type("lyng.Real"),
|
||||
mutable = false
|
||||
)
|
||||
mod.classDoc(name = "Math", doc = "Mathematical constants and helpers.") {
|
||||
field(name = "PI", doc = "The mathematical constant pi.", type = type("lyng.Real"), isStatic = true)
|
||||
field(name = "PI", doc = StdlibInlineDocIndex.methodDoc("Math", "PI") ?: "The mathematical constant pi.", type = type("lyng.Real"), isStatic = true)
|
||||
}
|
||||
|
||||
decls += mod.build()
|
||||
return decls
|
||||
}
|
||||
|
||||
// (Registration is triggered from BuiltinDocRegistry.init)
|
||||
// (Registration for external modules is provided by their own libraries)
|
||||
@@ -0,0 +1,274 @@
|
||||
/*
|
||||
* 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.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* Pure-Kotlin, PSI-free completion engine used for isolated tests and non-IDE harnesses.
|
||||
* Mirrors the IntelliJ MVP logic: MiniAst + BuiltinDocRegistry + lenient imports.
|
||||
*/
|
||||
package net.sergeych.lyng.miniast
|
||||
import net.sergeych.lyng.Compiler
|
||||
import net.sergeych.lyng.Script
|
||||
import net.sergeych.lyng.Source
|
||||
import net.sergeych.lyng.pacman.ImportProvider
|
||||
|
||||
/** Minimal completion item description (IDE-agnostic). */
|
||||
data class CompletionItem(
|
||||
val name: String,
|
||||
val kind: Kind,
|
||||
val tailText: String? = null,
|
||||
val typeText: String? = null,
|
||||
)
|
||||
|
||||
enum class Kind { Function, Class_, Enum, Value, Method, Field }
|
||||
|
||||
/**
|
||||
* Platform-free, lenient import provider that never fails on unknown packages.
|
||||
* Used to allow MiniAst parsing even when external modules are not present at runtime.
|
||||
*/
|
||||
class LenientImportProvider private constructor(root: net.sergeych.lyng.Scope) : ImportProvider(root) {
|
||||
override suspend fun createModuleScope(pos: net.sergeych.lyng.Pos, packageName: String): net.sergeych.lyng.ModuleScope =
|
||||
net.sergeych.lyng.ModuleScope(this, pos, packageName)
|
||||
|
||||
companion object {
|
||||
fun create(): LenientImportProvider = LenientImportProvider(Script.defaultImportManager.rootScope)
|
||||
}
|
||||
}
|
||||
|
||||
object CompletionEngineLight {
|
||||
|
||||
suspend fun completeAtMarkerSuspend(textWithCaret: String, marker: String = "<caret>"): List<CompletionItem> {
|
||||
val idx = textWithCaret.indexOf(marker)
|
||||
require(idx >= 0) { "Caret marker '$marker' not found" }
|
||||
val text = textWithCaret.replace(marker, "")
|
||||
return completeSuspend(text, idx)
|
||||
}
|
||||
|
||||
suspend fun completeSuspend(text: String, caret: Int): List<CompletionItem> {
|
||||
// Ensure stdlib Obj*-defined docs (e.g., String methods) are initialized before registry lookup
|
||||
StdlibDocsBootstrap.ensure()
|
||||
val prefix = prefixAt(text, caret)
|
||||
val mini = buildMiniAst(text)
|
||||
val imported: List<String> = DocLookupUtils.canonicalImportedModules(mini ?: return emptyList(), text)
|
||||
|
||||
val cap = 200
|
||||
val out = ArrayList<CompletionItem>(64)
|
||||
|
||||
// Member context detection: dot immediately before caret or before current word start
|
||||
val word = DocLookupUtils.wordRangeAt(text, caret)
|
||||
val memberDot = DocLookupUtils.findDotLeft(text, word?.first ?: caret)
|
||||
if (memberDot != null) {
|
||||
// 0) Try chained member call return type inference
|
||||
DocLookupUtils.guessReturnClassFromMemberCallBefore(text, memberDot, imported, mini)?.let { cls ->
|
||||
offerMembersAdd(out, prefix, imported, cls, mini)
|
||||
return out
|
||||
}
|
||||
// 0a) Top-level call before dot
|
||||
DocLookupUtils.guessReturnClassFromTopLevelCallBefore(text, memberDot, imported, mini)?.let { cls ->
|
||||
offerMembersAdd(out, prefix, imported, cls, mini)
|
||||
return out
|
||||
}
|
||||
// 0b) Across-known-callees (Iterable/Iterator/List preference)
|
||||
DocLookupUtils.guessReturnClassAcrossKnownCallees(text, memberDot, imported, mini)?.let { cls ->
|
||||
offerMembersAdd(out, prefix, imported, cls, mini)
|
||||
return out
|
||||
}
|
||||
// 1) Receiver inference fallback
|
||||
(DocLookupUtils.guessReceiverClassViaMini(mini, text, memberDot, imported) ?: DocLookupUtils.guessReceiverClass(text, memberDot, imported, mini))?.let { cls ->
|
||||
offerMembersAdd(out, prefix, imported, cls, mini)
|
||||
return out
|
||||
}
|
||||
// In member context and unknown receiver/return type: show nothing (no globals after dot)
|
||||
return out
|
||||
}
|
||||
|
||||
// Global identifiers: params > local decls > imported > stdlib; Functions > Classes > Values; alphabetical
|
||||
mini?.let { m ->
|
||||
val decls = m.declarations
|
||||
val funs = decls.filterIsInstance<MiniFunDecl>().sortedBy { it.name.lowercase() }
|
||||
val classes = decls.filterIsInstance<MiniClassDecl>().sortedBy { it.name.lowercase() }
|
||||
val enums = decls.filterIsInstance<MiniEnumDecl>().sortedBy { it.name.lowercase() }
|
||||
val vals = decls.filterIsInstance<MiniValDecl>().sortedBy { it.name.lowercase() }
|
||||
funs.forEach { offerDeclAdd(out, prefix, it) }
|
||||
classes.forEach { offerDeclAdd(out, prefix, it) }
|
||||
enums.forEach { offerDeclAdd(out, prefix, it) }
|
||||
vals.forEach { offerDeclAdd(out, prefix, it) }
|
||||
}
|
||||
|
||||
// Imported and builtin
|
||||
val (nonStd, std) = imported.partition { it != "lyng.stdlib" }
|
||||
val order = nonStd + std
|
||||
val emptyPrefixThrottle = prefix.isBlank()
|
||||
var externalAdded = 0
|
||||
val budget = if (emptyPrefixThrottle) 100 else Int.MAX_VALUE
|
||||
for (mod in order) {
|
||||
val decls = BuiltinDocRegistry.docsForModule(mod)
|
||||
val funs = decls.filterIsInstance<MiniFunDecl>().sortedBy { it.name.lowercase() }
|
||||
val classes = decls.filterIsInstance<MiniClassDecl>().sortedBy { it.name.lowercase() }
|
||||
val enums = decls.filterIsInstance<MiniEnumDecl>().sortedBy { it.name.lowercase() }
|
||||
val vals = decls.filterIsInstance<MiniValDecl>().sortedBy { it.name.lowercase() }
|
||||
funs.forEach { if (externalAdded < budget) { offerDeclAdd(out, prefix, it); externalAdded++ } }
|
||||
classes.forEach { if (externalAdded < budget) { offerDeclAdd(out, prefix, it); externalAdded++ } }
|
||||
enums.forEach { if (externalAdded < budget) { offerDeclAdd(out, prefix, it); externalAdded++ } }
|
||||
vals.forEach { if (externalAdded < budget) { offerDeclAdd(out, prefix, it); externalAdded++ } }
|
||||
if (out.size >= cap || externalAdded >= budget) break
|
||||
}
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
// --- Emission helpers ---
|
||||
|
||||
private fun offerDeclAdd(out: MutableList<CompletionItem>, prefix: String, d: MiniDecl) {
|
||||
fun add(ci: CompletionItem) { if (ci.name.startsWith(prefix, true)) out += ci }
|
||||
when (d) {
|
||||
is MiniFunDecl -> {
|
||||
val params = d.params.joinToString(", ") { it.name }
|
||||
val tail = "(${params})"
|
||||
add(CompletionItem(d.name, Kind.Function, tailText = tail, typeText = typeOf(d.returnType)))
|
||||
}
|
||||
is MiniClassDecl -> add(CompletionItem(d.name, Kind.Class_))
|
||||
is MiniEnumDecl -> add(CompletionItem(d.name, Kind.Enum))
|
||||
is MiniValDecl -> add(CompletionItem(d.name, Kind.Value, typeText = typeOf(d.type)))
|
||||
// else -> add(CompletionItem(d.name, Kind.Value))
|
||||
}
|
||||
}
|
||||
|
||||
private fun offerMembersAdd(out: MutableList<CompletionItem>, prefix: String, imported: List<String>, className: String, mini: MiniScript? = null) {
|
||||
val classes = DocLookupUtils.aggregateClasses(imported, mini)
|
||||
val visited = mutableSetOf<String>()
|
||||
val directMap = LinkedHashMap<String, MutableList<MiniMemberDecl>>()
|
||||
val inheritedMap = LinkedHashMap<String, MutableList<MiniMemberDecl>>()
|
||||
|
||||
fun addMembersOf(name: String, direct: Boolean) {
|
||||
val cls = classes[name] ?: return
|
||||
val target = if (direct) directMap else inheritedMap
|
||||
for (m in cls.members) target.getOrPut(m.name) { mutableListOf() }.add(m)
|
||||
for (b in cls.bases) if (visited.add(b)) addMembersOf(b, false)
|
||||
}
|
||||
|
||||
visited.add(className)
|
||||
addMembersOf(className, true)
|
||||
|
||||
// Conservative supplements for containers
|
||||
when (className) {
|
||||
"List" -> 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>>) {
|
||||
for (name in map.keys.sortedBy { it.lowercase() }) {
|
||||
val variants = map[name] ?: continue
|
||||
// Prefer a method with a known return type; else any method; else first variant
|
||||
val rep =
|
||||
variants.asSequence()
|
||||
.filterIsInstance<MiniMemberFunDecl>()
|
||||
.firstOrNull { it.returnType != null }
|
||||
?: variants.firstOrNull { it is MiniMemberFunDecl }
|
||||
?: variants.first()
|
||||
when (rep) {
|
||||
is MiniMemberFunDecl -> {
|
||||
val params = rep.params.joinToString(", ") { it.name }
|
||||
val extra = variants.count { it is MiniMemberFunDecl } - 1
|
||||
val ov = if (extra > 0) " (+$extra overloads)" else ""
|
||||
val ci = CompletionItem(name, Kind.Method, tailText = "(${params})$ov", typeText = typeOf(rep.returnType))
|
||||
if (ci.name.startsWith(prefix, true)) out += ci
|
||||
}
|
||||
is MiniMemberValDecl -> {
|
||||
// Prefer a field variant with known type if available
|
||||
val chosen = variants.asSequence()
|
||||
.filterIsInstance<MiniMemberValDecl>()
|
||||
.firstOrNull { it.type != null } ?: rep
|
||||
val ci = CompletionItem(name, Kind.Field, typeText = typeOf((chosen as MiniMemberValDecl).type))
|
||||
if (ci.name.startsWith(prefix, true)) out += ci
|
||||
}
|
||||
is MiniInitDecl -> {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
emitGroup(directMap)
|
||||
emitGroup(inheritedMap)
|
||||
|
||||
// Supplement with stdlib extension members defined in root.lyng (e.g., fun String.re(...))
|
||||
run {
|
||||
val already = (directMap.keys + inheritedMap.keys).toMutableSet()
|
||||
val ext = BuiltinDocRegistry.extensionMemberNamesFor(className)
|
||||
for (name in ext) {
|
||||
if (already.contains(name)) continue
|
||||
val resolved = DocLookupUtils.resolveMemberWithInheritance(imported, className, name)
|
||||
if (resolved != null) {
|
||||
when (val member = resolved.second) {
|
||||
is MiniMemberFunDecl -> {
|
||||
val params = member.params.joinToString(", ") { it.name }
|
||||
val ci = CompletionItem(name, Kind.Method, tailText = "(${params})", typeText = typeOf(member.returnType))
|
||||
if (ci.name.startsWith(prefix, true)) out += ci
|
||||
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 {
|
||||
// Fallback: emit simple method name without detailed types
|
||||
val ci = CompletionItem(name, Kind.Method, tailText = "()", typeText = null)
|
||||
if (ci.name.startsWith(prefix, true)) out += ci
|
||||
already.add(name)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// --- Inference helpers (text-only, PSI-free) ---
|
||||
|
||||
private suspend fun buildMiniAst(text: String): MiniScript? {
|
||||
val sink = MiniAstBuilder()
|
||||
return try {
|
||||
val src = Source("<engine>", text)
|
||||
val provider = LenientImportProvider.create()
|
||||
Compiler.compileWithMini(src, provider, sink)
|
||||
sink.build()
|
||||
} catch (_: Throwable) {
|
||||
sink.build()
|
||||
}
|
||||
}
|
||||
|
||||
private fun typeOf(t: MiniTypeRef?): String = when (t) {
|
||||
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}"
|
||||
}
|
||||
|
||||
// Note: we intentionally skip "params in scope" in the isolated engine to avoid PSI/offset mapping.
|
||||
|
||||
// Text helpers
|
||||
private fun prefixAt(text: String, offset: Int): String {
|
||||
val off = offset.coerceIn(0, text.length)
|
||||
var i = (off - 1).coerceAtLeast(0)
|
||||
while (i >= 0 && DocLookupUtils.isIdentChar(text[i])) i--
|
||||
val start = i + 1
|
||||
return if (start in 0..text.length && start <= off) text.substring(start, off) else ""
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,688 @@
|
||||
/*
|
||||
* 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.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* Shared QuickDoc lookup helpers reusable outside the IDEA plugin.
|
||||
*/
|
||||
package net.sergeych.lyng.miniast
|
||||
|
||||
import net.sergeych.lyng.binding.BindingSnapshot
|
||||
import net.sergeych.lyng.highlight.offsetOf
|
||||
|
||||
object DocLookupUtils {
|
||||
fun findDeclarationAt(mini: MiniScript, offset: Int, name: String): Pair<String, String>? {
|
||||
val src = mini.range.start.source
|
||||
fun matches(p: net.sergeych.lyng.Pos, len: Int) = src.offsetOf(p).let { s -> offset >= s && offset < s + len }
|
||||
|
||||
for (d in mini.declarations) {
|
||||
if (matches(d.nameStart, d.name.length)) {
|
||||
val kind = when (d) {
|
||||
is MiniFunDecl -> "Function"
|
||||
is MiniClassDecl -> "Class"
|
||||
is MiniEnumDecl -> "Enum"
|
||||
is MiniValDecl -> if (d.mutable) "Variable" else "Value"
|
||||
}
|
||||
return d.name to kind
|
||||
}
|
||||
|
||||
val members = when (d) {
|
||||
is MiniFunDecl -> {
|
||||
for (p in d.params) {
|
||||
if (matches(p.nameStart, p.name.length)) return p.name to "Parameter"
|
||||
}
|
||||
emptyList()
|
||||
}
|
||||
|
||||
is MiniEnumDecl -> {
|
||||
// Enum entries don't have explicit nameStart yet, but we can check their text range if we had it.
|
||||
// For now, heuristic: if offset is within enum range and matches an entry name.
|
||||
// To be more precise, we should check that we are NOT at the 'enum' or enum name.
|
||||
if (offset >= src.offsetOf(d.range.start) && offset <= src.offsetOf(d.range.end)) {
|
||||
if (d.entries.contains(name) && !matches(d.nameStart, d.name.length)) {
|
||||
// verify we are actually at the entry word in text
|
||||
val off = src.offsetOf(d.range.start)
|
||||
val end = src.offsetOf(d.range.end)
|
||||
val text = src.text.substring(off, end)
|
||||
// This is still a bit loose but better
|
||||
return name to "EnumConstant"
|
||||
}
|
||||
}
|
||||
emptyList()
|
||||
}
|
||||
|
||||
is MiniClassDecl -> {
|
||||
for (cf in d.ctorFields) {
|
||||
if (matches(cf.nameStart, cf.name.length)) return cf.name to (if (cf.mutable) "Variable" else "Value")
|
||||
}
|
||||
for (cf in d.classFields) {
|
||||
if (matches(cf.nameStart, cf.name.length)) return cf.name to (if (cf.mutable) "Variable" else "Value")
|
||||
}
|
||||
d.members
|
||||
}
|
||||
|
||||
else -> emptyList()
|
||||
}
|
||||
|
||||
for (m in members) {
|
||||
if (matches(m.nameStart, m.name.length)) {
|
||||
val kind = when (m) {
|
||||
is MiniMemberFunDecl -> "Function"
|
||||
is MiniMemberValDecl -> if (m.isStatic) "Value" else (if (m.mutable) "Variable" else "Value")
|
||||
is MiniInitDecl -> "Initializer"
|
||||
}
|
||||
return m.name to kind
|
||||
}
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
fun findTypeByRange(mini: MiniScript?, name: String, startOffset: Int): MiniTypeRef? {
|
||||
if (mini == null) return null
|
||||
val src = mini.range.start.source
|
||||
|
||||
for (d in mini.declarations) {
|
||||
if (d.name == name && src.offsetOf(d.nameStart) == startOffset) {
|
||||
return when (d) {
|
||||
is MiniValDecl -> d.type
|
||||
is MiniFunDecl -> d.returnType
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
|
||||
if (d is MiniFunDecl) {
|
||||
for (p in d.params) {
|
||||
if (p.name == name && src.offsetOf(p.nameStart) == startOffset) return p.type
|
||||
}
|
||||
}
|
||||
|
||||
if (d is MiniClassDecl) {
|
||||
for (m in d.members) {
|
||||
if (m.name == name && src.offsetOf(m.nameStart) == startOffset) {
|
||||
return when (m) {
|
||||
is MiniMemberFunDecl -> m.returnType
|
||||
is MiniMemberValDecl -> m.type
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
}
|
||||
for (cf in d.ctorFields) {
|
||||
if (cf.name == name && src.offsetOf(cf.nameStart) == startOffset) return cf.type
|
||||
}
|
||||
for (cf in d.classFields) {
|
||||
if (cf.name == name && src.offsetOf(cf.nameStart) == startOffset) return cf.type
|
||||
}
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert MiniAst imports to fully-qualified module names expected by BuiltinDocRegistry.
|
||||
* Heuristics:
|
||||
* - If an import does not start with "lyng.", prefix it with "lyng." (e.g., "io.fs" -> "lyng.io.fs").
|
||||
* - Keep original if it already starts with "lyng.".
|
||||
* - Always include "lyng.stdlib" to make builtins available for docs.
|
||||
* - If [sourceText] is provided, uses textual extraction as a fallback if MiniAst has no imports.
|
||||
*/
|
||||
fun canonicalImportedModules(mini: MiniScript, sourceText: String? = null): List<String> {
|
||||
val raw = mini.imports.map { it.segments.joinToString(".") { s -> s.name } }
|
||||
val result = LinkedHashSet<String>()
|
||||
for (name in raw) {
|
||||
val canon = if (name.startsWith("lyng.")) name else "lyng.$name"
|
||||
result.add(canon)
|
||||
}
|
||||
|
||||
if (result.isEmpty() && sourceText != null) {
|
||||
result.addAll(extractImportsFromText(sourceText))
|
||||
}
|
||||
|
||||
// Always make stdlib available as a fallback context for common types,
|
||||
// even when there are no explicit imports in the file
|
||||
result.add("lyng.stdlib")
|
||||
return result.toList()
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
|
||||
fun aggregateClasses(importedModules: List<String>, localMini: MiniScript? = null): Map<String, MiniClassDecl> {
|
||||
// Collect all class decls by name across modules, then merge duplicates by unioning members and bases.
|
||||
val buckets = LinkedHashMap<String, MutableList<MiniClassDecl>>()
|
||||
for (mod in importedModules) {
|
||||
val docs = BuiltinDocRegistry.docsForModule(mod)
|
||||
for (cls in docs.filterIsInstance<MiniClassDecl>()) {
|
||||
buckets.getOrPut(cls.name) { mutableListOf() }.add(cls)
|
||||
}
|
||||
for (en in docs.filterIsInstance<MiniEnumDecl>()) {
|
||||
buckets.getOrPut(en.name) { mutableListOf() }.add(enumToSyntheticClass(en))
|
||||
}
|
||||
}
|
||||
|
||||
// Add local declarations
|
||||
localMini?.declarations?.forEach { d ->
|
||||
when (d) {
|
||||
is MiniClassDecl -> {
|
||||
buckets.getOrPut(d.name) { mutableListOf() }.add(d)
|
||||
}
|
||||
is MiniEnumDecl -> {
|
||||
val syn = enumToSyntheticClass(d)
|
||||
buckets.getOrPut(d.name) { mutableListOf() }.add(syn)
|
||||
}
|
||||
else -> {}
|
||||
}
|
||||
}
|
||||
|
||||
fun mergeClassDecls(name: String, list: List<MiniClassDecl>): MiniClassDecl {
|
||||
if (list.isEmpty()) throw IllegalArgumentException("empty class list for $name")
|
||||
if (list.size == 1) return list.first()
|
||||
// Choose a representative for non-merge fields (range/nameStart/bodyRange): take the first
|
||||
val rep = list.first()
|
||||
val bases = LinkedHashSet<String>()
|
||||
val members = LinkedHashMap<String, MutableList<MiniMemberDecl>>()
|
||||
var doc: MiniDoc? = null
|
||||
for (c in list) {
|
||||
bases.addAll(c.bases)
|
||||
if (doc == null && c.doc != null && c.doc.raw.isNotBlank()) doc = c.doc
|
||||
for (m in c.members) {
|
||||
// Group by name to keep overloads together
|
||||
members.getOrPut(m.name) { mutableListOf() }.add(m)
|
||||
}
|
||||
}
|
||||
// Flatten grouped members back to a list; keep stable name order
|
||||
val mergedMembers = members.keys.sortedBy { it.lowercase() }.flatMap { members[it] ?: emptyList() }
|
||||
return MiniClassDecl(
|
||||
range = rep.range,
|
||||
name = rep.name,
|
||||
bases = bases.toList(),
|
||||
bodyRange = rep.bodyRange,
|
||||
ctorFields = rep.ctorFields,
|
||||
classFields = rep.classFields,
|
||||
doc = doc,
|
||||
nameStart = rep.nameStart,
|
||||
members = mergedMembers
|
||||
)
|
||||
}
|
||||
|
||||
val result = LinkedHashMap<String, MiniClassDecl>()
|
||||
for ((name, list) in buckets) {
|
||||
result[name] = mergeClassDecls(name, list)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
fun resolveMemberWithInheritance(importedModules: List<String>, className: String, member: String, localMini: MiniScript? = null): Pair<String, MiniMemberDecl>? {
|
||||
val classes = aggregateClasses(importedModules, localMini)
|
||||
fun dfs(name: String, visited: MutableSet<String>): Pair<String, MiniMemberDecl>? {
|
||||
val cls = classes[name] ?: return null
|
||||
cls.members.firstOrNull { it.name == member }?.let { return name to it }
|
||||
if (!visited.add(name)) return null
|
||||
for (baseName in cls.bases) {
|
||||
dfs(baseName, visited)?.let { return it }
|
||||
}
|
||||
return null
|
||||
}
|
||||
return dfs(className, mutableSetOf())
|
||||
}
|
||||
|
||||
fun findMemberAcrossClasses(importedModules: List<String>, member: String, localMini: MiniScript? = null): Pair<String, MiniMemberDecl>? {
|
||||
val classes = aggregateClasses(importedModules, localMini)
|
||||
// Preferred order for ambiguous common ops
|
||||
val preference = listOf("Iterable", "Iterator", "List")
|
||||
for (name in preference) {
|
||||
resolveMemberWithInheritance(importedModules, name, member, localMini)?.let { return it }
|
||||
}
|
||||
for ((name, cls) in classes) {
|
||||
cls.members.firstOrNull { it.name == member }?.let { return name to it }
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* Try to guess a class name of the receiver when the receiver is a call like `ClassName(...)`.
|
||||
* We walk left from the dot, find a matching `)` and then the identifier immediately before the `(`.
|
||||
* If that identifier matches a known class in any of the imported modules, return it.
|
||||
*/
|
||||
fun guessClassFromCallBefore(text: String, dotPos: Int, importedModules: List<String>, localMini: MiniScript? = null): String? {
|
||||
var i = (dotPos - 1).coerceAtLeast(0)
|
||||
while (i >= 0 && text[i].isWhitespace()) i--
|
||||
if (i < 0 || text[i] != ')') return null
|
||||
// Walk back to matching '(' accounting nested parentheses
|
||||
var depth = 0
|
||||
i--
|
||||
while (i >= 0) {
|
||||
val ch = text[i]
|
||||
when (ch) {
|
||||
')' -> depth++
|
||||
'(' -> if (depth == 0) break else depth--
|
||||
'\n' -> {}
|
||||
}
|
||||
i--
|
||||
}
|
||||
if (i < 0 || text[i] != '(') return null
|
||||
// Now find identifier immediately before '('
|
||||
var j = i - 1
|
||||
while (j >= 0 && text[j].isWhitespace()) j--
|
||||
val end = j + 1
|
||||
while (j >= 0 && isIdentChar(text[j])) j--
|
||||
val start = j + 1
|
||||
if (start >= end) return null
|
||||
val name = text.substring(start, end)
|
||||
// Validate against imported classes
|
||||
val classes = aggregateClasses(importedModules, localMini)
|
||||
return if (classes.containsKey(name)) name else null
|
||||
}
|
||||
|
||||
fun guessReceiverClassViaMini(mini: MiniScript?, text: String, dotPos: Int, imported: List<String>, binding: BindingSnapshot? = null): String? {
|
||||
if (mini == null) return null
|
||||
val i = prevNonWs(text, dotPos - 1)
|
||||
if (i < 0) return null
|
||||
val wordRange = wordRangeAt(text, i + 1) ?: return null
|
||||
val ident = text.substring(wordRange.first, wordRange.second)
|
||||
|
||||
// 0) Use binding if available for precise resolution
|
||||
if (binding != null) {
|
||||
val ref = binding.references.firstOrNull { wordRange.first >= it.start && wordRange.first < it.end }
|
||||
if (ref != null) {
|
||||
val sym = binding.symbols.firstOrNull { it.id == ref.symbolId }
|
||||
if (sym != null) {
|
||||
val type = findTypeByRange(mini, sym.name, sym.declStart)
|
||||
simpleClassNameOf(type)?.let { return it }
|
||||
}
|
||||
} else {
|
||||
// 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 }
|
||||
if (sym != null) {
|
||||
val type = findTypeByRange(mini, sym.name, sym.declStart)
|
||||
simpleClassNameOf(type)?.let { return it }
|
||||
// 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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 1) Global declarations in current file (val/var/fun/class/enum)
|
||||
val d = mini.declarations.firstOrNull { it.name == ident }
|
||||
if (d != null) {
|
||||
return when (d) {
|
||||
is MiniClassDecl -> d.name
|
||||
is MiniEnumDecl -> d.name
|
||||
is MiniValDecl -> simpleClassNameOf(d.type)
|
||||
is MiniFunDecl -> simpleClassNameOf(d.returnType)
|
||||
}
|
||||
}
|
||||
|
||||
// 2) Parameters in any function (best-effort fallback without scope mapping)
|
||||
for (fd in mini.declarations.filterIsInstance<MiniFunDecl>()) {
|
||||
for (p in fd.params) {
|
||||
if (p.name == ident) return simpleClassNameOf(p.type)
|
||||
}
|
||||
}
|
||||
|
||||
// 3) Recursive chaining: Base.ident.
|
||||
val dotBefore = findDotLeft(text, wordRange.first)
|
||||
if (dotBefore != null) {
|
||||
val receiverClass = guessReceiverClassViaMini(mini, text, dotBefore, imported, binding)
|
||||
?: guessReceiverClass(text, dotBefore, imported, mini)
|
||||
if (receiverClass != null) {
|
||||
val resolved = resolveMemberWithInheritance(imported, receiverClass, ident, mini)
|
||||
if (resolved != null) {
|
||||
val rt = when (val m = resolved.second) {
|
||||
is MiniMemberFunDecl -> m.returnType
|
||||
is MiniMemberValDecl -> m.type
|
||||
else -> null
|
||||
}
|
||||
return simpleClassNameOf(rt)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 4) Check if it's a known class (static access)
|
||||
val classes = aggregateClasses(imported, mini)
|
||||
if (classes.containsKey(ident)) return ident
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
fun guessReturnClassFromMemberCallBeforeMini(mini: MiniScript?, text: String, dotPos: Int, imported: List<String>, binding: BindingSnapshot? = null): String? {
|
||||
if (mini == null) return null
|
||||
var i = prevNonWs(text, dotPos - 1)
|
||||
if (i < 0 || text[i] != ')') return null
|
||||
// back to matching '('
|
||||
i--
|
||||
var depth = 0
|
||||
while (i >= 0) {
|
||||
when (text[i]) {
|
||||
')' -> depth++
|
||||
'(' -> if (depth == 0) break else depth--
|
||||
}
|
||||
i--
|
||||
}
|
||||
if (i < 0 || text[i] != '(') return null
|
||||
var j = i - 1
|
||||
while (j >= 0 && text[j].isWhitespace()) j--
|
||||
val end = j + 1
|
||||
while (j >= 0 && isIdentChar(text[j])) j--
|
||||
val start = j + 1
|
||||
if (start >= end) return null
|
||||
val callee = text.substring(start, end)
|
||||
// Ensure member call: dot before callee
|
||||
var k = start - 1
|
||||
while (k >= 0 && text[k].isWhitespace()) k--
|
||||
if (k < 0 || text[k] != '.') return null
|
||||
val prevDot = k
|
||||
// Resolve receiver class via MiniAst (ident like `x`)
|
||||
val receiverClass = guessReceiverClassViaMini(mini, text, prevDot, imported, binding) ?: return null
|
||||
// If receiver class is a locally declared class, resolve member on it
|
||||
val localClass = mini.declarations.filterIsInstance<MiniClassDecl>().firstOrNull { it.name == receiverClass }
|
||||
if (localClass != null) {
|
||||
val mm = localClass.members.firstOrNull { it.name == callee }
|
||||
if (mm != null) {
|
||||
val rt = when (mm) {
|
||||
is MiniMemberFunDecl -> mm.returnType
|
||||
is MiniMemberValDecl -> mm.type
|
||||
else -> null
|
||||
}
|
||||
return simpleClassNameOf(rt)
|
||||
} else {
|
||||
// Try to scan class body text for method signature and extract return type
|
||||
val sigs = scanLocalClassMembersFromText(mini, text, localClass)
|
||||
val sig = sigs[callee]
|
||||
if (sig != null && sig.typeText != null) return sig.typeText
|
||||
}
|
||||
}
|
||||
// Else fallback to registry-based resolution (covers imported classes)
|
||||
return resolveMemberWithInheritance(imported, receiverClass, callee, mini)?.second?.let { m ->
|
||||
val rt = when (m) {
|
||||
is MiniMemberFunDecl -> m.returnType
|
||||
is MiniMemberValDecl -> m.type
|
||||
is MiniInitDecl -> null
|
||||
}
|
||||
simpleClassNameOf(rt)
|
||||
}
|
||||
}
|
||||
|
||||
data class ScannedSig(val kind: String, val params: List<String>?, val typeText: String?)
|
||||
|
||||
fun scanLocalClassMembersFromText(mini: MiniScript, text: String, cls: MiniClassDecl): Map<String, ScannedSig> {
|
||||
val src = mini.range.start.source
|
||||
val start = src.offsetOf(cls.bodyRange?.start ?: cls.range.start)
|
||||
val end = src.offsetOf(cls.bodyRange?.end ?: cls.range.end).coerceAtMost(text.length)
|
||||
if (start !in 0..end) return emptyMap()
|
||||
val body = text.substring(start, end)
|
||||
val map = LinkedHashMap<String, ScannedSig>()
|
||||
// fun name(params): Type
|
||||
val funRe = Regex("^\\s*fun\\s+([A-Za-z_][A-Za-z0-9_]*)\\s*\\(([^)]*)\\)\\s*(?::\\s*([A-Za-z_][A-Za-z0-9_]*))?", RegexOption.MULTILINE)
|
||||
for (m in funRe.findAll(body)) {
|
||||
val name = m.groupValues.getOrNull(1) ?: continue
|
||||
val params = m.groupValues.getOrNull(2)?.split(',')?.mapNotNull { it.trim().takeIf { it.isNotEmpty() } } ?: emptyList()
|
||||
val type = m.groupValues.getOrNull(3)?.takeIf { it.isNotBlank() }
|
||||
map[name] = ScannedSig("fun", params, type)
|
||||
}
|
||||
// val/var name: Type
|
||||
val valRe = Regex("^\\s*(val|var)\\s+([A-Za-z_][A-Za-z0-9_]*)\\s*(?::\\s*([A-Za-z_][A-Za-z0-9_]*))?", RegexOption.MULTILINE)
|
||||
for (m in valRe.findAll(body)) {
|
||||
val kind = m.groupValues.getOrNull(1) ?: continue
|
||||
val name = m.groupValues.getOrNull(2) ?: continue
|
||||
val type = m.groupValues.getOrNull(3)?.takeIf { it.isNotBlank() }
|
||||
if (!map.containsKey(name)) map[name] = ScannedSig(kind, null, type)
|
||||
}
|
||||
return map
|
||||
}
|
||||
|
||||
fun guessReceiverClass(text: String, dotPos: Int, imported: List<String>, mini: MiniScript? = null): String? {
|
||||
guessClassFromCallBefore(text, dotPos, imported, mini)?.let { return it }
|
||||
var i = prevNonWs(text, dotPos - 1)
|
||||
if (i >= 0) {
|
||||
when (text[i]) {
|
||||
'"' -> return "String"
|
||||
']' -> return "List"
|
||||
'}' -> return "Dict"
|
||||
')' -> {
|
||||
// Parenthesized expression: walk back to matching '(' and inspect the inner expression
|
||||
var j = i - 1
|
||||
var depth = 0
|
||||
while (j >= 0) {
|
||||
when (text[j]) {
|
||||
')' -> depth++
|
||||
'(' -> if (depth == 0) break else depth--
|
||||
}
|
||||
j--
|
||||
}
|
||||
if (j >= 0 && text[j] == '(') {
|
||||
val innerS = (j + 1).coerceAtLeast(0)
|
||||
val innerE = i.coerceAtMost(text.length)
|
||||
if (innerS < innerE) {
|
||||
val inner = text.substring(innerS, innerE).trim()
|
||||
if (inner.startsWith('"') && inner.endsWith('"')) return "String"
|
||||
if (inner.startsWith('[') && inner.endsWith(']')) return "List"
|
||||
if (inner.startsWith('{') && inner.endsWith('}')) return "Dict"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Numeric literal: decimal/int/hex/scientific
|
||||
var j = i
|
||||
var hasDigits = false
|
||||
var hasDot = false
|
||||
var hasExp = false
|
||||
while (j >= 0) {
|
||||
val ch = text[j]
|
||||
if (ch.isDigit()) {
|
||||
hasDigits = true; j--; continue
|
||||
}
|
||||
if (ch == '.') {
|
||||
hasDot = true; j--; continue
|
||||
}
|
||||
if (ch == 'e' || ch == 'E') {
|
||||
hasExp = true; j--; if (j >= 0 && (text[j] == '+' || text[j] == '-')) j--; continue
|
||||
}
|
||||
if (ch in listOf('x', 'X', 'a', 'b', 'c', 'd', 'f', 'A', 'B', 'C', 'D', 'F')) {
|
||||
j--; continue
|
||||
}
|
||||
break
|
||||
}
|
||||
if (hasDigits) return if (hasDot || hasExp) "Real" else "Int"
|
||||
|
||||
// 3) this@Type or as Type
|
||||
val identRange = wordRangeAt(text, i + 1)
|
||||
if (identRange != null) {
|
||||
val ident = text.substring(identRange.first, identRange.second)
|
||||
// if it's "as Type", we want Type
|
||||
var k = prevNonWs(text, identRange.first - 1)
|
||||
if (k >= 1 && text[k] == 's' && text[k - 1] == 'a' && (k - 1 == 0 || !text[k - 2].isLetterOrDigit())) {
|
||||
return ident
|
||||
}
|
||||
// if it's "this@Type", we want Type
|
||||
if (k >= 0 && text[k] == '@') {
|
||||
val k2 = prevNonWs(text, k - 1)
|
||||
if (k2 >= 3 && text.substring(k2 - 3, k2 + 1) == "this") {
|
||||
return ident
|
||||
}
|
||||
}
|
||||
|
||||
// 4) Check if it's a known class (static access)
|
||||
val classes = aggregateClasses(imported, mini)
|
||||
if (classes.containsKey(ident)) return ident
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
fun guessReturnClassFromMemberCallBefore(text: String, dotPos: Int, imported: List<String>, mini: MiniScript? = null): String? {
|
||||
var i = prevNonWs(text, dotPos - 1)
|
||||
if (i < 0 || text[i] != ')') return null
|
||||
i--
|
||||
var depth = 0
|
||||
while (i >= 0) {
|
||||
when (text[i]) {
|
||||
')' -> depth++
|
||||
'(' -> if (depth == 0) break else depth--
|
||||
}
|
||||
i--
|
||||
}
|
||||
if (i < 0 || text[i] != '(') return null
|
||||
var j = i - 1
|
||||
while (j >= 0 && text[j].isWhitespace()) j--
|
||||
val end = j + 1
|
||||
while (j >= 0 && isIdentChar(text[j])) j--
|
||||
val start = j + 1
|
||||
if (start >= end) return null
|
||||
val callee = text.substring(start, end)
|
||||
var k = start - 1
|
||||
while (k >= 0 && text[k].isWhitespace()) k--
|
||||
if (k < 0 || text[k] != '.') return null
|
||||
val prevDot = k
|
||||
val receiverClass = guessReceiverClass(text, prevDot, imported, mini) ?: return null
|
||||
val resolved = resolveMemberWithInheritance(imported, receiverClass, callee, mini) ?: return null
|
||||
val member = resolved.second
|
||||
val ret = when (member) {
|
||||
is MiniMemberFunDecl -> member.returnType
|
||||
is MiniMemberValDecl -> member.type
|
||||
is MiniInitDecl -> null
|
||||
}
|
||||
return simpleClassNameOf(ret)
|
||||
}
|
||||
|
||||
fun guessReturnClassFromTopLevelCallBefore(text: String, dotPos: Int, imported: List<String>, mini: MiniScript? = null): String? {
|
||||
var i = prevNonWs(text, dotPos - 1)
|
||||
if (i < 0 || text[i] != ')') return null
|
||||
i--
|
||||
var depth = 0
|
||||
while (i >= 0) {
|
||||
when (text[i]) {
|
||||
')' -> depth++
|
||||
'(' -> if (depth == 0) break else depth--
|
||||
}
|
||||
i--
|
||||
}
|
||||
if (i < 0 || text[i] != '(') return null
|
||||
var j = i - 1
|
||||
while (j >= 0 && text[j].isWhitespace()) j--
|
||||
val end = j + 1
|
||||
while (j >= 0 && isIdentChar(text[j])) j--
|
||||
val start = j + 1
|
||||
if (start >= end) return null
|
||||
val callee = text.substring(start, end)
|
||||
var k = start - 1
|
||||
while (k >= 0 && text[k].isWhitespace()) k--
|
||||
if (k >= 0 && text[k] == '.') return null // was a member call
|
||||
for (mod in imported) {
|
||||
val decls = BuiltinDocRegistry.docsForModule(mod)
|
||||
val fn = decls.asSequence().filterIsInstance<MiniFunDecl>().firstOrNull { it.name == callee }
|
||||
if (fn != null) return simpleClassNameOf(fn.returnType)
|
||||
}
|
||||
// Also check local declarations
|
||||
mini?.declarations?.filterIsInstance<MiniFunDecl>()?.firstOrNull { it.name == callee }?.let { return simpleClassNameOf(it.returnType) }
|
||||
return null
|
||||
}
|
||||
|
||||
fun guessReturnClassAcrossKnownCallees(text: String, dotPos: Int, imported: List<String>, mini: MiniScript? = null): String? {
|
||||
var i = prevNonWs(text, dotPos - 1)
|
||||
if (i < 0 || text[i] != ')') return null
|
||||
i--
|
||||
var depth = 0
|
||||
while (i >= 0) {
|
||||
when (text[i]) {
|
||||
')' -> depth++
|
||||
'(' -> if (depth == 0) break else depth--
|
||||
}
|
||||
i--
|
||||
}
|
||||
if (i < 0 || text[i] != '(') return null
|
||||
var j = i - 1
|
||||
while (j >= 0 && text[j].isWhitespace()) j--
|
||||
val end = j + 1
|
||||
while (j >= 0 && isIdentChar(text[j])) j--
|
||||
val start = j + 1
|
||||
if (start >= end) return null
|
||||
val callee = text.substring(start, end)
|
||||
val resolved = findMemberAcrossClasses(imported, callee, mini) ?: return null
|
||||
val member = resolved.second
|
||||
val ret = when (member) {
|
||||
is MiniMemberFunDecl -> member.returnType
|
||||
is MiniMemberValDecl -> member.type
|
||||
is MiniInitDecl -> null
|
||||
}
|
||||
return simpleClassNameOf(ret)
|
||||
}
|
||||
|
||||
fun simpleClassNameOf(t: MiniTypeRef?): String? = when (t) {
|
||||
null -> null
|
||||
is MiniTypeName -> t.segments.lastOrNull()?.name
|
||||
is MiniGenericType -> simpleClassNameOf(t.base)
|
||||
is MiniFunctionType -> null
|
||||
is MiniTypeVar -> null
|
||||
}
|
||||
|
||||
fun findDotLeft(text: String, offset: Int): Int? {
|
||||
var i = (offset - 1).coerceAtLeast(0)
|
||||
while (i >= 0 && text[i].isWhitespace()) i--
|
||||
return if (i >= 0 && text[i] == '.') i else null
|
||||
}
|
||||
|
||||
fun prevNonWs(text: String, start: Int): Int {
|
||||
var i = start.coerceAtMost(text.length - 1)
|
||||
while (i >= 0 && text[i].isWhitespace()) i--
|
||||
return i
|
||||
}
|
||||
|
||||
fun wordRangeAt(text: String, offset: Int): Pair<Int, Int>? {
|
||||
if (text.isEmpty()) return null
|
||||
val off = offset.coerceIn(0, text.length)
|
||||
var s = off
|
||||
var e = off
|
||||
while (s > 0 && isIdentChar(text[s - 1])) s--
|
||||
while (e < text.length && isIdentChar(text[e])) e++
|
||||
return if (s < e) s to e else null
|
||||
}
|
||||
|
||||
fun isIdentChar(c: Char): Boolean = c == '_' || c.isLetterOrDigit()
|
||||
|
||||
fun enumToSyntheticClass(en: MiniEnumDecl): MiniClassDecl {
|
||||
val staticMembers = mutableListOf<MiniMemberDecl>()
|
||||
// entries: List
|
||||
staticMembers.add(MiniMemberValDecl(en.range, "entries", false, null, null, en.nameStart, isStatic = true))
|
||||
// valueOf(name: String): Enum
|
||||
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)
|
||||
for (entry in en.entries) {
|
||||
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(
|
||||
range = en.range,
|
||||
name = en.name,
|
||||
bases = listOf("Enum"),
|
||||
bodyRange = null,
|
||||
doc = en.doc,
|
||||
nameStart = en.nameStart,
|
||||
members = staticMembers
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -100,7 +100,7 @@ fun ObjClass.addFnDoc(
|
||||
code: suspend Scope.() -> Obj
|
||||
) {
|
||||
// Register runtime method
|
||||
addFn(name, isOpen, visibility, code)
|
||||
addFn(name, isOpen, visibility, code = code)
|
||||
// Register docs for the member under this class
|
||||
BuiltinDocRegistry.module(moduleName ?: ownerModuleNameFromClassOrUnknown()) {
|
||||
classDoc(this@addFnDoc.className, doc = "") {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
|
||||
* 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.
|
||||
@@ -109,7 +109,8 @@ data class MiniFunDecl(
|
||||
val returnType: MiniTypeRef?,
|
||||
val body: MiniBlock?,
|
||||
override val doc: MiniDoc?,
|
||||
override val nameStart: Pos
|
||||
override val nameStart: Pos,
|
||||
val receiver: MiniTypeRef? = null
|
||||
) : MiniDecl
|
||||
|
||||
data class MiniValDecl(
|
||||
@@ -119,7 +120,8 @@ data class MiniValDecl(
|
||||
val type: MiniTypeRef?,
|
||||
val initRange: MiniRange?,
|
||||
override val doc: MiniDoc?,
|
||||
override val nameStart: Pos
|
||||
override val nameStart: Pos,
|
||||
val receiver: MiniTypeRef? = null
|
||||
) : MiniDecl
|
||||
|
||||
data class MiniClassDecl(
|
||||
@@ -135,6 +137,14 @@ data class MiniClassDecl(
|
||||
val members: List<MiniMemberDecl> = emptyList()
|
||||
) : MiniDecl
|
||||
|
||||
data class MiniEnumDecl(
|
||||
override val range: MiniRange,
|
||||
override val name: String,
|
||||
val entries: List<String>,
|
||||
override val doc: MiniDoc?,
|
||||
override val nameStart: Pos
|
||||
) : MiniDecl
|
||||
|
||||
data class MiniCtorField(
|
||||
val name: String,
|
||||
val mutable: Boolean,
|
||||
@@ -183,6 +193,15 @@ data class MiniMemberValDecl(
|
||||
override val isStatic: Boolean = false,
|
||||
) : MiniMemberDecl
|
||||
|
||||
data class MiniInitDecl(
|
||||
override val range: MiniRange,
|
||||
override val nameStart: Pos,
|
||||
) : MiniMemberDecl {
|
||||
override val name: String get() = "init"
|
||||
override val doc: MiniDoc? get() = null
|
||||
override val isStatic: Boolean get() = false
|
||||
}
|
||||
|
||||
// Streaming sink to collect mini-AST during parsing. Implementations may assemble a tree or process events.
|
||||
interface MiniAstSink {
|
||||
fun onScriptStart(start: Pos) {}
|
||||
@@ -193,7 +212,9 @@ interface MiniAstSink {
|
||||
fun onImport(node: MiniImport) {}
|
||||
fun onFunDecl(node: MiniFunDecl) {}
|
||||
fun onValDecl(node: MiniValDecl) {}
|
||||
fun onInitDecl(node: MiniInitDecl) {}
|
||||
fun onClassDecl(node: MiniClassDecl) {}
|
||||
fun onEnumDecl(node: MiniEnumDecl) {}
|
||||
|
||||
fun onBlock(node: MiniBlock) {}
|
||||
fun onIdentifier(node: MiniIdentifier) {}
|
||||
@@ -263,6 +284,12 @@ class MiniAstBuilder : MiniAstSink {
|
||||
lastDoc = null
|
||||
}
|
||||
|
||||
override fun onEnumDecl(node: MiniEnumDecl) {
|
||||
val attach = node.copy(doc = node.doc ?: lastDoc)
|
||||
currentScript?.declarations?.add(attach)
|
||||
lastDoc = null
|
||||
}
|
||||
|
||||
override fun onBlock(node: MiniBlock) {
|
||||
blocks.addLast(node)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* Ensure stdlib Obj*-defined docs (like String methods added via ObjString.addFnDoc)
|
||||
* are initialized before registry lookups for completion/quick docs.
|
||||
*/
|
||||
package net.sergeych.lyng.miniast
|
||||
|
||||
import net.sergeych.lyng.obj.ObjString
|
||||
|
||||
object StdlibDocsBootstrap {
|
||||
// Simple idempotent guard; races are harmless as initializer side-effects are idempotent
|
||||
private var ensured = false
|
||||
|
||||
fun ensure() {
|
||||
if (ensured) return
|
||||
try {
|
||||
// Touch core Obj* types whose docs are registered via addFnDoc/addConstDoc
|
||||
// Accessing .type forces their static initializers to run and register docs.
|
||||
@Suppress("UNUSED_VARIABLE")
|
||||
val _string = ObjString.type
|
||||
} catch (_: Throwable) {
|
||||
// Best-effort; absence should not break consumers
|
||||
} finally {
|
||||
ensured = true
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -17,8 +17,13 @@
|
||||
|
||||
package net.sergeych.lyng.obj
|
||||
|
||||
import kotlinx.serialization.DeserializationStrategy
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlinx.serialization.json.Json
|
||||
import kotlinx.serialization.json.JsonElement
|
||||
import kotlinx.serialization.json.JsonNull
|
||||
import kotlinx.serialization.serializer
|
||||
import net.sergeych.lyng.*
|
||||
import net.sergeych.lynon.LynonDecoder
|
||||
import net.sergeych.lynon.LynonEncoder
|
||||
@@ -78,23 +83,52 @@ open class Obj {
|
||||
scope: Scope,
|
||||
name: String,
|
||||
args: Arguments = Arguments.EMPTY,
|
||||
onNotFoundResult: (() -> Obj?)? = null
|
||||
onNotFoundResult: (suspend () -> Obj?)? = null
|
||||
): Obj {
|
||||
val rec = objClass.getInstanceMemberOrNull(name)
|
||||
if (rec != null) {
|
||||
val decl = rec.declaringClass ?: objClass.findDeclaringClassOf(name)
|
||||
val caller = scope.currentClassCtx
|
||||
if (!canAccessMember(rec.visibility, decl, caller))
|
||||
scope.raiseError(ObjAccessException(scope, "can't invoke ${name}: not visible (declared in ${decl?.className ?: "?"}, caller ${caller?.className ?: "?"})"))
|
||||
// Propagate declaring class as current class context during method execution
|
||||
val saved = scope.currentClassCtx
|
||||
scope.currentClassCtx = decl
|
||||
try {
|
||||
return rec.value.invoke(scope, this, args)
|
||||
} finally {
|
||||
scope.currentClassCtx = saved
|
||||
// 1. Hierarchy members (excluding root fallback)
|
||||
for (cls in objClass.mro) {
|
||||
if (cls.className == "Obj") break
|
||||
val rec = cls.members[name] ?: cls.classScope?.objects?.get(name)
|
||||
if (rec != null) {
|
||||
val decl = rec.declaringClass ?: cls
|
||||
val caller = scope.currentClassCtx
|
||||
if (!canAccessMember(rec.visibility, decl, caller))
|
||||
scope.raiseError(ObjAccessException(scope, "can't invoke ${name}: not visible (declared in ${decl.className}, caller ${caller?.className ?: "?"})"))
|
||||
val saved = scope.currentClassCtx
|
||||
scope.currentClassCtx = decl
|
||||
try {
|
||||
return rec.value.invoke(scope, this, args)
|
||||
} finally {
|
||||
scope.currentClassCtx = saved
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Extensions in scope
|
||||
val extension = scope.findExtension(objClass, name)
|
||||
if (extension != null) {
|
||||
return extension.value.invoke(scope, this, args)
|
||||
}
|
||||
|
||||
// 3. Root object fallback
|
||||
for (cls in objClass.mro) {
|
||||
if (cls.className == "Obj") {
|
||||
cls.members[name]?.let { rec ->
|
||||
val decl = rec.declaringClass ?: cls
|
||||
val caller = scope.currentClassCtx
|
||||
if (!canAccessMember(rec.visibility, decl, caller))
|
||||
scope.raiseError(ObjAccessException(scope, "can't invoke ${name}: not visible (declared in ${decl.className}, caller ${caller?.className ?: "?"})"))
|
||||
val saved = scope.currentClassCtx
|
||||
scope.currentClassCtx = decl
|
||||
try {
|
||||
return rec.value.invoke(scope, this, args)
|
||||
} finally {
|
||||
scope.currentClassCtx = saved
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return onNotFoundResult?.invoke()
|
||||
?: scope.raiseError(
|
||||
"no such member: $name on ${objClass.className}. Considered order: ${objClass.renderLinearization(true)}. " +
|
||||
@@ -122,6 +156,40 @@ open class Obj {
|
||||
return invokeInstanceMethod(scope, "contains", other).toBool()
|
||||
}
|
||||
|
||||
/**
|
||||
* Call [callback] for each element of this obj considering it provides [Iterator]
|
||||
* methods `hasNext` and `next`.
|
||||
*
|
||||
* IF callback returns false, iteration is stopped.
|
||||
*/
|
||||
open suspend fun enumerate(scope: Scope, callback: suspend (Obj) -> Boolean) {
|
||||
val iterator = invokeInstanceMethod(scope, "iterator")
|
||||
val hasNext = iterator.getInstanceMethod(scope, "hasNext")
|
||||
val next = iterator.getInstanceMethod(scope, "next")
|
||||
var closeIt = false
|
||||
try {
|
||||
while (hasNext.invoke(scope, iterator).toBool()) {
|
||||
val nextValue = next.invoke(scope, iterator)
|
||||
val shouldContinue = try {
|
||||
callback(nextValue)
|
||||
} catch (e: Exception) {
|
||||
// iteration aborted due to exception in callback
|
||||
closeIt = true
|
||||
throw e
|
||||
}
|
||||
if (!shouldContinue) {
|
||||
closeIt = true
|
||||
break
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
if (closeIt) {
|
||||
// Best-effort cancel on premature termination
|
||||
iterator.invokeInstanceMethod(scope, "cancelIteration") { ObjVoid }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Default toString implementation:
|
||||
*
|
||||
@@ -137,15 +205,19 @@ open class Obj {
|
||||
* @param calledFromLyng true if called from Lyng's `toString`. Normally this parameter should be ignored,
|
||||
* but it is used to avoid endless recursion in [Obj.toString] base implementation
|
||||
*/
|
||||
open suspend fun toString(scope: Scope,calledFromLyng: Boolean=false): ObjString {
|
||||
return if (this is ObjString) this
|
||||
else if( !calledFromLyng ) {
|
||||
invokeInstanceMethod(scope, "toString") {
|
||||
ObjString(this.toString())
|
||||
open suspend fun toString(scope: Scope, calledFromLyng: Boolean = false): ObjString {
|
||||
if (this is ObjString) return this
|
||||
return if (!calledFromLyng) {
|
||||
invokeInstanceMethod(scope, "toString", Arguments.EMPTY) {
|
||||
defaultToString(scope)
|
||||
} as ObjString
|
||||
} else { ObjString(this.toString()) }
|
||||
} else {
|
||||
defaultToString(scope)
|
||||
}
|
||||
}
|
||||
|
||||
open suspend fun defaultToString(scope: Scope): ObjString = ObjString(this.toString())
|
||||
|
||||
/**
|
||||
* Class of the object: definition of member functions (top-level), etc.
|
||||
* Note that using lazy allows to avoid endless recursion here
|
||||
@@ -223,7 +295,7 @@ open class Obj {
|
||||
|
||||
open suspend fun assign(scope: Scope, other: Obj): Obj? = null
|
||||
|
||||
open fun getValue(scope: Scope) = this
|
||||
open suspend fun getValue(scope: Scope) = this
|
||||
|
||||
/**
|
||||
* a += b
|
||||
@@ -270,34 +342,83 @@ open class Obj {
|
||||
// suspend fun <T> sync(block: () -> T): T = monitor.withLock { block() }
|
||||
|
||||
open suspend fun readField(scope: Scope, name: String): ObjRecord {
|
||||
// could be property or class field:
|
||||
val obj = objClass.getInstanceMemberOrNull(name) ?: scope.raiseError(
|
||||
// 1. Hierarchy members (excluding root fallback)
|
||||
for (cls in objClass.mro) {
|
||||
if (cls.className == "Obj") break
|
||||
cls.members[name]?.let { return resolveRecord(scope, it, name, it.declaringClass) }
|
||||
cls.classScope?.objects?.get(name)?.let { return resolveRecord(scope, it, name, it.declaringClass) }
|
||||
}
|
||||
|
||||
// 2. Extensions
|
||||
val extension = scope.findExtension(objClass, name)
|
||||
if (extension != null) {
|
||||
return resolveRecord(scope, extension, name, extension.declaringClass)
|
||||
}
|
||||
|
||||
// 3. Root fallback
|
||||
for (cls in objClass.mro) {
|
||||
if (cls.className == "Obj") {
|
||||
cls.members[name]?.let {
|
||||
val decl = it.declaringClass ?: cls
|
||||
return resolveRecord(scope, it, name, decl)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
scope.raiseError(
|
||||
"no such field: $name on ${objClass.className}. Considered order: ${objClass.renderLinearization(true)}"
|
||||
)
|
||||
val decl = obj.declaringClass ?: objClass.findDeclaringClassOf(name)
|
||||
}
|
||||
|
||||
protected suspend fun resolveRecord(scope: Scope, obj: ObjRecord, name: String, decl: ObjClass?): ObjRecord {
|
||||
val value = obj.value
|
||||
if (value is ObjProperty) {
|
||||
return ObjRecord(value.callGetter(scope, this, decl), obj.isMutable)
|
||||
}
|
||||
if (value is Statement && decl != null) {
|
||||
return ObjRecord(value.execute(scope.createChildScope(scope.pos, newThisObj = this)), obj.isMutable)
|
||||
}
|
||||
val caller = scope.currentClassCtx
|
||||
// Check visibility for non-property members here if they weren't checked before
|
||||
if (!canAccessMember(obj.visibility, decl, caller))
|
||||
scope.raiseError(ObjAccessException(scope, "can't access field ${name}: not visible (declared in ${decl?.className ?: "?"}, caller ${caller?.className ?: "?"})"))
|
||||
return when (val value = obj.value) {
|
||||
is Statement -> {
|
||||
ObjRecord(value.execute(scope.createChildScope(scope.pos, newThisObj = this)), obj.isMutable)
|
||||
}
|
||||
// could be writable property naturally
|
||||
// null -> ObjNull.asReadonly
|
||||
else -> obj
|
||||
}
|
||||
return obj
|
||||
}
|
||||
|
||||
open suspend fun writeField(scope: Scope, name: String, newValue: Obj) {
|
||||
willMutate(scope)
|
||||
val field = objClass.getInstanceMemberOrNull(name) ?: scope.raiseError(
|
||||
var field: ObjRecord? = null
|
||||
// 1. Hierarchy members (excluding root fallback)
|
||||
for (cls in objClass.mro) {
|
||||
if (cls.className == "Obj") break
|
||||
field = cls.members[name] ?: cls.classScope?.objects?.get(name)
|
||||
if (field != null) break
|
||||
}
|
||||
// 2. Extensions
|
||||
if (field == null) {
|
||||
field = scope.findExtension(objClass, name)
|
||||
}
|
||||
// 3. Root fallback
|
||||
if (field == null) {
|
||||
for (cls in objClass.mro) {
|
||||
if (cls.className == "Obj") {
|
||||
field = cls.members[name]
|
||||
if (field != null) break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (field == null) scope.raiseError(
|
||||
"no such field: $name on ${objClass.className}. Considered order: ${objClass.renderLinearization(true)}"
|
||||
)
|
||||
val decl = field.declaringClass ?: objClass.findDeclaringClassOf(name)
|
||||
|
||||
val decl = field.declaringClass
|
||||
val caller = scope.currentClassCtx
|
||||
if (!canAccessMember(field.visibility, decl, caller))
|
||||
scope.raiseError(ObjAccessException(scope, "can't assign field ${name}: not visible (declared in ${decl?.className ?: "?"}, caller ${caller?.className ?: "?"})"))
|
||||
if (field.isMutable) field.value = newValue else scope.raiseError("can't assign to read-only field: $name")
|
||||
if (field.value is ObjProperty) {
|
||||
(field.value as ObjProperty).callSetter(scope, this, newValue, decl)
|
||||
} else if (field.isMutable) field.value = newValue else scope.raiseError("can't assign to read-only field: $name")
|
||||
}
|
||||
|
||||
open suspend fun getAt(scope: Scope, index: Obj): Obj {
|
||||
@@ -376,6 +497,10 @@ open class Obj {
|
||||
return null
|
||||
}
|
||||
|
||||
open suspend fun toJson(scope: Scope = Scope()): JsonElement {
|
||||
scope.raiseNotImplemented("toJson for ${objClass.className}")
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
val rootObjectType = ObjClass("Obj").apply {
|
||||
@@ -418,7 +543,9 @@ open class Obj {
|
||||
thisObj.putAt(this, requiredArg<Obj>(0), newValue)
|
||||
newValue
|
||||
}
|
||||
|
||||
addFn("toJsonString") {
|
||||
thisObj.toJson(this).toString().toObj()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -429,8 +556,8 @@ open class Obj {
|
||||
is Obj -> obj
|
||||
is Double -> ObjReal(obj)
|
||||
is Float -> ObjReal(obj.toDouble())
|
||||
is Int -> ObjInt(obj.toLong())
|
||||
is Long -> ObjInt(obj)
|
||||
is Int -> ObjInt.of(obj.toLong())
|
||||
is Long -> ObjInt.of(obj)
|
||||
is String -> ObjString(obj)
|
||||
is CharSequence -> ObjString(obj.toString())
|
||||
is Boolean -> ObjBool(obj)
|
||||
@@ -520,6 +647,10 @@ object ObjNull : Obj() {
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun toJson(scope: Scope): JsonElement {
|
||||
return JsonNull
|
||||
}
|
||||
|
||||
override val objClass: ObjClass by lazy {
|
||||
object : ObjClass("Null") {
|
||||
override suspend fun deserialize(scope: Scope, decoder: LynonDecoder, lynonType: LynonType?): Obj {
|
||||
@@ -572,4 +703,25 @@ data class ObjNamespace(val name: String) : Obj() {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Decodes the current object into a deserialized form using the provided deserialization strategy.
|
||||
* It is based on [Obj.toJson] and uses existing Kotlin Json serialization, without string representation
|
||||
* (only `JsonElement` to carry information between Kotlin and Lyng serialization worlds), thus efficient.
|
||||
*
|
||||
* @param strategy The deserialization strategy that defines how the object should be decoded.
|
||||
* @param scope An optional scope used during deserialization to define the context. Defaults to a new instance of Scope.
|
||||
* @return The deserialized object of type T.
|
||||
*/
|
||||
suspend fun <T>Obj.decodeSerializableWith(strategy: DeserializationStrategy<T>, scope: Scope = Scope()): T =
|
||||
Json.decodeFromJsonElement(strategy,toJson(scope))
|
||||
|
||||
/**
|
||||
* Decodes a serializable object of type [T] using the provided decoding scope. The deserialization uses
|
||||
* [Obj.toJson] and existing Json based serialization ithout using actual string representation, thus
|
||||
* efficient.
|
||||
*
|
||||
* @param T The type of the object to be decoded. Must be a reified type.
|
||||
* @param scope The scope used during decoding. Defaults to a new instance of [Scope].
|
||||
*/
|
||||
suspend inline fun <reified T>Obj.decodeSerializable(scope: Scope= Scope()) =
|
||||
decodeSerializableWith<T>(serializer<T>(), scope)
|
||||
@@ -17,6 +17,11 @@
|
||||
|
||||
package net.sergeych.lyng.obj
|
||||
|
||||
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 {
|
||||
|
||||
/**
|
||||
@@ -25,19 +30,34 @@ val ObjArray by lazy {
|
||||
ObjClass("Array", ObjCollection).apply {
|
||||
// we can create iterators using size/getat:
|
||||
|
||||
addFn("iterator") {
|
||||
ObjArrayIterator(thisObj).also { it.init(this) }
|
||||
}
|
||||
addFnDoc(
|
||||
name = "iterator",
|
||||
doc = "Iterator over elements of this array using its indexer.",
|
||||
returns = TypeGenericDoc(type("lyng.Iterator"), listOf(type("lyng.Any"))),
|
||||
moduleName = "lyng.stdlib"
|
||||
) { ObjArrayIterator(thisObj).also { it.init(this) } }
|
||||
|
||||
addFn("contains", isOpen = true) {
|
||||
addFnDoc(
|
||||
name = "contains",
|
||||
doc = "Whether the array contains the given element (by equality).",
|
||||
params = listOf(ParamDoc("element")),
|
||||
returns = type("lyng.Bool"),
|
||||
isOpen = true,
|
||||
moduleName = "lyng.stdlib"
|
||||
) {
|
||||
val obj = args.firstAndOnly()
|
||||
for (i in 0..<thisObj.invokeInstanceMethod(this, "size").toInt()) {
|
||||
if (thisObj.getAt(this, ObjInt(i.toLong())).compareTo(this, obj) == 0) return@addFn ObjTrue
|
||||
if (thisObj.getAt(this, ObjInt(i.toLong())).compareTo(this, obj) == 0) return@addFnDoc ObjTrue
|
||||
}
|
||||
ObjFalse
|
||||
}
|
||||
|
||||
addFn("last") {
|
||||
addFnDoc(
|
||||
name = "last",
|
||||
doc = "The last element of this array.",
|
||||
returns = type("lyng.Any"),
|
||||
moduleName = "lyng.stdlib"
|
||||
) {
|
||||
thisObj.invokeInstanceMethod(
|
||||
this,
|
||||
"getAt",
|
||||
@@ -45,13 +65,27 @@ val ObjArray by lazy {
|
||||
)
|
||||
}
|
||||
|
||||
addFn("lastIndex") { (thisObj.invokeInstanceMethod(this, "size").toInt() - 1).toObj() }
|
||||
addFnDoc(
|
||||
name = "lastIndex",
|
||||
doc = "Index of the last element (size - 1).",
|
||||
returns = type("lyng.Int"),
|
||||
moduleName = "lyng.stdlib"
|
||||
) { (thisObj.invokeInstanceMethod(this, "size").toInt() - 1).toObj() }
|
||||
|
||||
addFn("indices") {
|
||||
ObjRange(0.toObj(), thisObj.invokeInstanceMethod(this, "size"), false)
|
||||
}
|
||||
addFnDoc(
|
||||
name = "indices",
|
||||
doc = "Range of valid indices for this array.",
|
||||
returns = type("lyng.Range"),
|
||||
moduleName = "lyng.stdlib"
|
||||
) { ObjRange(0.toObj(), thisObj.invokeInstanceMethod(this, "size"), false) }
|
||||
|
||||
addFn("binarySearch") {
|
||||
addFnDoc(
|
||||
name = "binarySearch",
|
||||
doc = "Binary search for a target in a sorted array. Returns index or negative insertion point - 1.",
|
||||
params = listOf(ParamDoc("target")),
|
||||
returns = type("lyng.Int"),
|
||||
moduleName = "lyng.stdlib"
|
||||
) {
|
||||
val target = args.firstAndOnly()
|
||||
var low = 0
|
||||
var high = thisObj.invokeInstanceMethod(this, "size").toInt() - 1
|
||||
@@ -62,13 +96,12 @@ val ObjArray by lazy {
|
||||
|
||||
val cmp = midVal.compareTo(this, target)
|
||||
when {
|
||||
cmp == 0 -> return@addFn (mid).toObj()
|
||||
cmp == 0 -> return@addFnDoc (mid).toObj()
|
||||
cmp > 0 -> high = mid - 1
|
||||
else -> low = mid + 1
|
||||
}
|
||||
}
|
||||
|
||||
// Элемент не найден, возвращаем -(точка вставки) - 1
|
||||
(-low - 1).toObj()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,8 @@
|
||||
|
||||
package net.sergeych.lyng.obj
|
||||
|
||||
import kotlinx.serialization.json.JsonElement
|
||||
import kotlinx.serialization.json.JsonPrimitive
|
||||
import net.sergeych.lyng.Scope
|
||||
import net.sergeych.lynon.LynonDecoder
|
||||
import net.sergeych.lynon.LynonEncoder
|
||||
@@ -62,6 +64,10 @@ data class ObjBool(val value: Boolean) : Obj() {
|
||||
return value == other.value
|
||||
}
|
||||
|
||||
override suspend fun toJson(scope: Scope): JsonElement {
|
||||
return JsonPrimitive(value)
|
||||
}
|
||||
|
||||
companion object {
|
||||
val type = object : ObjClass("Bool") {
|
||||
override suspend fun deserialize(scope: Scope, decoder: LynonDecoder,lynonType: LynonType?): Obj {
|
||||
|
||||
@@ -18,6 +18,8 @@
|
||||
package net.sergeych.lyng.obj
|
||||
|
||||
import net.sergeych.lyng.Scope
|
||||
import net.sergeych.lyng.miniast.addFnDoc
|
||||
import net.sergeych.lyng.miniast.type
|
||||
|
||||
class ObjChar(val value: Char): Obj() {
|
||||
|
||||
@@ -45,7 +47,18 @@ class ObjChar(val value: Char): Obj() {
|
||||
|
||||
companion object {
|
||||
val type = ObjClass("Char").apply {
|
||||
addFn("code") { ObjInt(thisAs<ObjChar>().value.code.toLong()) }
|
||||
addFnDoc(
|
||||
name = "code",
|
||||
doc = "Unicode code point (UTF-16 code unit) of this character.",
|
||||
returns = type("lyng.Int"),
|
||||
moduleName = "lyng.stdlib"
|
||||
) { ObjInt(thisAs<ObjChar>().value.code.toLong()) }
|
||||
addFn("isDigit") {
|
||||
thisAs<ObjChar>().value.isDigit().toObj()
|
||||
}
|
||||
addFn("isSpace") {
|
||||
thisAs<ObjChar>().value.isWhitespace().toObj()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
|
||||
* 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.
|
||||
@@ -18,13 +18,73 @@
|
||||
package net.sergeych.lyng.obj
|
||||
|
||||
import net.sergeych.lyng.*
|
||||
import net.sergeych.lyng.miniast.ParamDoc
|
||||
import net.sergeych.lyng.miniast.TypeGenericDoc
|
||||
import net.sergeych.lyng.miniast.addFnDoc
|
||||
import net.sergeych.lyng.miniast.type
|
||||
import net.sergeych.lynon.LynonDecoder
|
||||
import net.sergeych.lynon.LynonType
|
||||
|
||||
// Simple id generator for class identities (not thread-safe; fine for scripts)
|
||||
private object ClassIdGen { var c: Long = 1L; fun nextId(): Long = c++ }
|
||||
|
||||
val ObjClassType by lazy { ObjClass("Class") }
|
||||
val ObjClassType by lazy {
|
||||
ObjClass("Class").apply {
|
||||
addFnDoc(
|
||||
name = "name",
|
||||
doc = "Simple name of this class (without package).",
|
||||
returns = type("lyng.String"),
|
||||
moduleName = "lyng.stdlib"
|
||||
) { thisAs<ObjClass>().classNameObj }
|
||||
|
||||
addFnDoc(
|
||||
name = "fields",
|
||||
doc = "Declared instance fields of this class and its ancestors (C3 order), without duplicates.",
|
||||
returns = TypeGenericDoc(type("lyng.List"), listOf(type("lyng.String"))),
|
||||
moduleName = "lyng.stdlib"
|
||||
) {
|
||||
val cls = thisAs<ObjClass>()
|
||||
val seen = hashSetOf<String>()
|
||||
val names = mutableListOf<Obj>()
|
||||
for (c in cls.mro) {
|
||||
for ((n, rec) in c.members) {
|
||||
if (rec.value !is Statement && seen.add(n)) names += ObjString(n)
|
||||
}
|
||||
}
|
||||
ObjList(names.toMutableList())
|
||||
}
|
||||
|
||||
addFnDoc(
|
||||
name = "methods",
|
||||
doc = "Declared instance methods of this class and its ancestors (C3 order), without duplicates.",
|
||||
returns = TypeGenericDoc(type("lyng.List"), listOf(type("lyng.String"))),
|
||||
moduleName = "lyng.stdlib"
|
||||
) {
|
||||
val cls = thisAs<ObjClass>()
|
||||
val seen = hashSetOf<String>()
|
||||
val names = mutableListOf<Obj>()
|
||||
for (c in cls.mro) {
|
||||
for ((n, rec) in c.members) {
|
||||
if (rec.value is Statement && seen.add(n)) names += ObjString(n)
|
||||
}
|
||||
}
|
||||
ObjList(names.toMutableList())
|
||||
}
|
||||
|
||||
addFnDoc(
|
||||
name = "get",
|
||||
doc = "Lookup a member by name in this class (including ancestors) and return it, or null if absent.",
|
||||
params = listOf(ParamDoc("name", type("lyng.String"))),
|
||||
returns = type("lyng.Any", nullable = true),
|
||||
moduleName = "lyng.stdlib"
|
||||
) {
|
||||
val cls = thisAs<ObjClass>()
|
||||
val name = requiredArg<ObjString>(0).value
|
||||
val rec = cls.getInstanceMemberOrNull(name)
|
||||
rec?.value ?: ObjNull
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
open class ObjClass(
|
||||
val className: String,
|
||||
@@ -115,7 +175,11 @@ open class ObjClass(
|
||||
}
|
||||
|
||||
/** Full C3 MRO including this class at index 0. */
|
||||
val mro: List<ObjClass> by lazy { c3Linearize(this, mutableMapOf()) }
|
||||
val mro: List<ObjClass> by lazy {
|
||||
val base = c3Linearize(this, mutableMapOf())
|
||||
if (this.className == "Obj" || base.any { it.className == "Obj" }) base
|
||||
else base + rootObjectType
|
||||
}
|
||||
|
||||
/** Parents in C3 order (no self). */
|
||||
val mroParents: List<ObjClass> by lazy { mro.drop(1) }
|
||||
@@ -140,12 +204,23 @@ open class ObjClass(
|
||||
override suspend fun compareTo(scope: Scope, other: Obj): Int = if (other === this) 0 else -1
|
||||
|
||||
override suspend fun callOn(scope: Scope): Obj {
|
||||
val instance = createInstance(scope)
|
||||
initializeInstance(instance, scope.args, runConstructors = true)
|
||||
return instance
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an instance of this class and initialize its [ObjInstance.instanceScope] with
|
||||
* methods. Does NOT run initializers or constructors.
|
||||
*/
|
||||
internal fun createInstance(scope: Scope): ObjInstance {
|
||||
val instance = ObjInstance(this)
|
||||
// Avoid capturing a transient (pooled) call frame as the parent of the instance scope.
|
||||
// Bind instance scope to the caller's parent chain directly so name resolution (e.g., stdlib like sqrt)
|
||||
// remains stable even when call frames are pooled and reused.
|
||||
val stableParent = scope.parent
|
||||
val stableParent = classScope ?: scope.parent
|
||||
instance.instanceScope = Scope(stableParent, scope.args, scope.pos, instance)
|
||||
instance.instanceScope.currentClassCtx = null
|
||||
// Expose instance methods (and other callable members) directly in the instance scope for fast lookup
|
||||
// This mirrors Obj.autoInstanceScope behavior for ad-hoc scopes and makes fb.method() resolution robust
|
||||
// 1) members-defined methods
|
||||
@@ -163,75 +238,101 @@ open class ObjClass(
|
||||
}
|
||||
}
|
||||
}
|
||||
// Constructor chaining MVP: initialize base classes left-to-right, then this class.
|
||||
// Ensure each ancestor is initialized at most once (diamond-safe).
|
||||
val visited = hashSetOf<ObjClass>()
|
||||
return instance
|
||||
}
|
||||
|
||||
suspend fun initClass(c: ObjClass, argsForThis: Arguments?, isRoot: Boolean = false) {
|
||||
if (!visited.add(c)) return
|
||||
// For the most-derived class, bind its constructor params BEFORE parents so base arg thunks can see them
|
||||
if (isRoot) {
|
||||
c.constructorMeta?.let { meta ->
|
||||
val argsHere = argsForThis ?: Arguments.EMPTY
|
||||
// Assign constructor params into instance scope (unmangled)
|
||||
meta.assignToContext(instance.instanceScope, argsHere)
|
||||
// Also expose them under MI-mangled storage keys `${Class}::name` so qualified views can access them
|
||||
// and so that base-class casts like `(obj as Base).field` work.
|
||||
for (p in meta.params) {
|
||||
val rec = instance.instanceScope.objects[p.name]
|
||||
if (rec != null) {
|
||||
val mangled = "${c.className}::${p.name}"
|
||||
// Always point the mangled name to the current record to keep writes consistent
|
||||
// across re-bindings (e.g., second pass before ctor)
|
||||
instance.instanceScope.objects[mangled] = rec
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Run initializers and optionally constructors for the given [instance].
|
||||
* Handles Multiple Inheritance correctly (diamond-safe).
|
||||
*/
|
||||
internal suspend fun initializeInstance(
|
||||
instance: ObjInstance,
|
||||
args: Arguments?,
|
||||
runConstructors: Boolean
|
||||
) {
|
||||
val visited = hashSetOf<ObjClass>()
|
||||
initClassInternal(instance, visited, this, args, isRoot = true, runConstructors = runConstructors)
|
||||
}
|
||||
|
||||
private suspend fun initClassInternal(
|
||||
instance: ObjInstance,
|
||||
visited: MutableSet<ObjClass>,
|
||||
c: ObjClass,
|
||||
argsForThis: Arguments?,
|
||||
@Suppress("UNUSED_PARAMETER") isRoot: Boolean = false,
|
||||
runConstructors: Boolean = true
|
||||
) {
|
||||
if (!visited.add(c)) return
|
||||
|
||||
// Bind constructor parameters (both mangled and unmangled)
|
||||
// These are needed for:
|
||||
// 1) base constructor argument evaluation (if called from a derived class)
|
||||
// 2) this class's field initializers and `init` blocks
|
||||
// 3) this class's constructor body
|
||||
// 4) `compareTo` and other structural operations
|
||||
c.constructorMeta?.let { meta ->
|
||||
val argsHere = argsForThis ?: Arguments.EMPTY
|
||||
// Assign constructor params into instance scope (unmangled)
|
||||
meta.assignToContext(instance.instanceScope, argsHere, declaringClass = c)
|
||||
// Also expose them under MI-mangled storage keys `${Class}::name` so qualified views can access them
|
||||
// and so that base-class casts like `(obj as Base).field` work.
|
||||
for (p in meta.params) {
|
||||
val rec = instance.instanceScope.objects[p.name]
|
||||
if (rec != null) {
|
||||
val mangled = "${c.className}::${p.name}"
|
||||
// Always point the mangled name to the current record to keep writes consistent
|
||||
// across re-bindings
|
||||
instance.instanceScope.objects[mangled] = rec
|
||||
}
|
||||
}
|
||||
// Initialize direct parents first, in order
|
||||
for (p in c.directParents) {
|
||||
val raw = c.directParentArgs[p]?.toArguments(instance.instanceScope, false)
|
||||
val limited = if (raw != null) {
|
||||
val need = p.constructorMeta?.params?.size ?: 0
|
||||
if (need == 0) Arguments.EMPTY else Arguments(raw.list.take(need), tailBlockMode = false)
|
||||
} else Arguments.EMPTY
|
||||
initClass(p, limited)
|
||||
}
|
||||
// Execute per-instance initializers collected from class body for this class
|
||||
if (c.instanceInitializers.isNotEmpty()) {
|
||||
val savedCtx = instance.instanceScope.currentClassCtx
|
||||
instance.instanceScope.currentClassCtx = c
|
||||
try {
|
||||
for (initStmt in c.instanceInitializers) {
|
||||
initStmt.execute(instance.instanceScope)
|
||||
}
|
||||
} finally {
|
||||
instance.instanceScope.currentClassCtx = savedCtx
|
||||
}
|
||||
}
|
||||
// Then run this class' constructor, if any
|
||||
c.instanceConstructor?.let { ctor ->
|
||||
// Bind this class's constructor parameters into the instance scope now, right before ctor
|
||||
c.constructorMeta?.let { meta ->
|
||||
val argsHere = argsForThis ?: Arguments.EMPTY
|
||||
meta.assignToContext(instance.instanceScope, argsHere)
|
||||
// Ensure mangled aliases exist for qualified access starting from this class
|
||||
for (p in meta.params) {
|
||||
val rec = instance.instanceScope.objects[p.name]
|
||||
if (rec != null) {
|
||||
val mangled = "${c.className}::${p.name}"
|
||||
// Overwrite to ensure alias refers to the latest ObjRecord after re-binding
|
||||
instance.instanceScope.objects[mangled] = rec
|
||||
}
|
||||
}
|
||||
}
|
||||
val execScope = instance.instanceScope.createChildScope(args = argsForThis ?: Arguments.EMPTY, newThisObj = instance)
|
||||
ctor.execute(execScope)
|
||||
}
|
||||
}
|
||||
|
||||
initClass(this, instance.instanceScope.args, isRoot = true)
|
||||
return instance
|
||||
// Initialize direct parents first, in order
|
||||
for (p in c.directParents) {
|
||||
val raw = c.directParentArgs[p]?.toArguments(instance.instanceScope, false)
|
||||
val limited = if (raw != null) {
|
||||
val need = p.constructorMeta?.params?.size ?: 0
|
||||
if (need == 0) Arguments.EMPTY else Arguments(raw.list.take(need), tailBlockMode = false)
|
||||
} else Arguments.EMPTY
|
||||
initClassInternal(instance, visited, p, limited, false, runConstructors)
|
||||
}
|
||||
|
||||
// Re-bind this class's parameters right before running its initializers and constructor.
|
||||
// This ensures that unmangled names in the instance scope correctly refer to THIS class's
|
||||
// parameters even if they were shadowed/overwritten by parent class initialization.
|
||||
c.constructorMeta?.let { meta ->
|
||||
val argsHere = argsForThis ?: Arguments.EMPTY
|
||||
meta.assignToContext(instance.instanceScope, argsHere, declaringClass = c)
|
||||
// Re-sync mangled names to point to the fresh records to keep them consistent
|
||||
for (p in meta.params) {
|
||||
val rec = instance.instanceScope.objects[p.name]
|
||||
if (rec != null) {
|
||||
val mangled = "${c.className}::${p.name}"
|
||||
instance.instanceScope.objects[mangled] = rec
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Execute per-instance initializers collected from class body for this class
|
||||
if (c.instanceInitializers.isNotEmpty()) {
|
||||
val savedCtx = instance.instanceScope.currentClassCtx
|
||||
instance.instanceScope.currentClassCtx = c
|
||||
try {
|
||||
for (initStmt in c.instanceInitializers) {
|
||||
initStmt.execute(instance.instanceScope)
|
||||
}
|
||||
} finally {
|
||||
instance.instanceScope.currentClassCtx = savedCtx
|
||||
}
|
||||
}
|
||||
// Then run this class' constructor, if any
|
||||
if (runConstructors) {
|
||||
c.instanceConstructor?.let { ctor ->
|
||||
val execScope =
|
||||
instance.instanceScope.createChildScope(args = argsForThis ?: Arguments.EMPTY, newThisObj = instance)
|
||||
ctor.execute(execScope)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun callWithArgs(scope: Scope, vararg plainArgs: Obj): Obj {
|
||||
@@ -244,14 +345,15 @@ open class ObjClass(
|
||||
initialValue: Obj,
|
||||
isMutable: Boolean = false,
|
||||
visibility: Visibility = Visibility.Public,
|
||||
pos: Pos = Pos.builtIn
|
||||
pos: Pos = Pos.builtIn,
|
||||
declaringClass: ObjClass? = this
|
||||
) {
|
||||
// Allow overriding ancestors: only prevent redefinition if THIS class already defines an immutable member
|
||||
val existingInSelf = members[name]
|
||||
if (existingInSelf != null && existingInSelf.isMutable == false)
|
||||
throw ScriptError(pos, "$name is already defined in $objClass")
|
||||
// Install/override in this class
|
||||
members[name] = ObjRecord(initialValue, isMutable, visibility, declaringClass = this)
|
||||
members[name] = ObjRecord(initialValue, isMutable, visibility, declaringClass = declaringClass)
|
||||
// Structural change: bump layout version for PIC invalidation
|
||||
layoutVersion += 1
|
||||
}
|
||||
@@ -281,13 +383,29 @@ open class ObjClass(
|
||||
name: String,
|
||||
isOpen: Boolean = false,
|
||||
visibility: Visibility = Visibility.Public,
|
||||
declaringClass: ObjClass? = this,
|
||||
code: suspend Scope.() -> Obj
|
||||
) {
|
||||
val stmt = statement { code() }
|
||||
createField(name, stmt, isOpen, visibility)
|
||||
createField(name, stmt, isOpen, visibility, Pos.builtIn, declaringClass)
|
||||
}
|
||||
|
||||
fun addConst(name: String, value: Obj) = createField(name, value, isMutable = false)
|
||||
|
||||
fun addProperty(
|
||||
name: String,
|
||||
getter: (suspend Scope.() -> Obj)? = null,
|
||||
setter: (suspend Scope.(Obj) -> Unit)? = null,
|
||||
visibility: Visibility = Visibility.Public,
|
||||
declaringClass: ObjClass? = this
|
||||
) {
|
||||
val g = getter?.let { statement { it() } }
|
||||
val s = setter?.let { statement { it(requiredArg(0)); ObjVoid } }
|
||||
val prop = ObjProperty(name, g, s)
|
||||
members[name] = ObjRecord(prop, false, visibility, declaringClass, type = ObjRecord.Type.Property)
|
||||
layoutVersion += 1
|
||||
}
|
||||
|
||||
fun addClassConst(name: String, value: Obj) = createClassField(name, value)
|
||||
fun addClassFn(name: String, isOpen: Boolean = false, code: suspend Scope.() -> Obj) {
|
||||
createClassField(name, statement { code() }, isOpen)
|
||||
@@ -355,7 +473,7 @@ open class ObjClass(
|
||||
|
||||
override suspend fun invokeInstanceMethod(
|
||||
scope: Scope, name: String, args: Arguments,
|
||||
onNotFoundResult: (() -> Obj?)?
|
||||
onNotFoundResult: (suspend () -> Obj?)?
|
||||
): Obj {
|
||||
return classScope?.objects?.get(name)?.value?.invoke(scope, this, args)
|
||||
?: super.invokeInstanceMethod(scope, name, args, onNotFoundResult)
|
||||
|
||||
@@ -19,6 +19,9 @@ package net.sergeych.lyng.obj
|
||||
|
||||
import kotlinx.coroutines.CompletableDeferred
|
||||
import net.sergeych.lyng.Scope
|
||||
import net.sergeych.lyng.miniast.ParamDoc
|
||||
import net.sergeych.lyng.miniast.addFnDoc
|
||||
import net.sergeych.lyng.miniast.type
|
||||
|
||||
class ObjCompletableDeferred(val completableDeferred: CompletableDeferred<Obj>): ObjDeferred(completableDeferred) {
|
||||
|
||||
@@ -30,7 +33,13 @@ class ObjCompletableDeferred(val completableDeferred: CompletableDeferred<Obj>):
|
||||
return ObjCompletableDeferred(CompletableDeferred())
|
||||
}
|
||||
}.apply {
|
||||
addFn("complete") {
|
||||
addFnDoc(
|
||||
name = "complete",
|
||||
doc = "Complete this deferred with the given value. Subsequent calls have no effect.",
|
||||
params = listOf(ParamDoc("value")),
|
||||
returns = type("lyng.Void"),
|
||||
moduleName = "lyng.stdlib"
|
||||
) {
|
||||
thisAs<ObjCompletableDeferred>().completableDeferred.complete(args.firstAndOnly())
|
||||
ObjVoid
|
||||
}
|
||||
|
||||
@@ -19,6 +19,8 @@ package net.sergeych.lyng.obj
|
||||
|
||||
import kotlinx.coroutines.Deferred
|
||||
import net.sergeych.lyng.Scope
|
||||
import net.sergeych.lyng.miniast.addFnDoc
|
||||
import net.sergeych.lyng.miniast.type
|
||||
|
||||
open class ObjDeferred(val deferred: Deferred<Obj>): Obj() {
|
||||
|
||||
@@ -30,20 +32,33 @@ open class ObjDeferred(val deferred: Deferred<Obj>): Obj() {
|
||||
scope.raiseError("Deferred constructor is not directly callable")
|
||||
}
|
||||
}.apply {
|
||||
addFn("await") {
|
||||
thisAs<ObjDeferred>().deferred.await()
|
||||
}
|
||||
addFn("isCompleted") {
|
||||
thisAs<ObjDeferred>().deferred.isCompleted.toObj()
|
||||
}
|
||||
addFn("isActive") {
|
||||
addFnDoc(
|
||||
name = "await",
|
||||
doc = "Suspend until completion and return the result value (or throw if failed).",
|
||||
returns = type("lyng.Any"),
|
||||
moduleName = "lyng.stdlib"
|
||||
) { thisAs<ObjDeferred>().deferred.await() }
|
||||
addFnDoc(
|
||||
name = "isCompleted",
|
||||
doc = "Whether this deferred has completed (successfully or with an error).",
|
||||
returns = type("lyng.Bool"),
|
||||
moduleName = "lyng.stdlib"
|
||||
) { thisAs<ObjDeferred>().deferred.isCompleted.toObj() }
|
||||
addFnDoc(
|
||||
name = "isActive",
|
||||
doc = "Whether this deferred is currently active (not completed and not cancelled).",
|
||||
returns = type("lyng.Bool"),
|
||||
moduleName = "lyng.stdlib"
|
||||
) {
|
||||
val d = thisAs<ObjDeferred>().deferred
|
||||
// Cross-engine tolerant: prefer Deferred.isActive; otherwise treat any not-yet-completed and not-cancelled as active
|
||||
(d.isActive || (!d.isCompleted && !d.isCancelled)).toObj()
|
||||
}
|
||||
addFn("isCancelled") {
|
||||
thisAs<ObjDeferred>().deferred.isCancelled.toObj()
|
||||
}
|
||||
addFnDoc(
|
||||
name = "isCancelled",
|
||||
doc = "Whether this deferred was cancelled.",
|
||||
returns = type("lyng.Bool"),
|
||||
moduleName = "lyng.stdlib"
|
||||
) { thisAs<ObjDeferred>().deferred.isCancelled.toObj() }
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +18,8 @@
|
||||
package net.sergeych.lyng.obj
|
||||
|
||||
import net.sergeych.lyng.Scope
|
||||
import net.sergeych.lyng.miniast.addFnDoc
|
||||
import net.sergeych.lyng.miniast.type
|
||||
import kotlin.time.Duration
|
||||
import kotlin.time.Duration.Companion.days
|
||||
import kotlin.time.Duration.Companion.hours
|
||||
@@ -72,91 +74,169 @@ class ObjDuration(val duration: Duration) : Obj() {
|
||||
)
|
||||
}
|
||||
}.apply {
|
||||
addFn("days") {
|
||||
thisAs<ObjDuration>().duration.toDouble(DurationUnit.DAYS).toObj()
|
||||
}
|
||||
addFn("hours") {
|
||||
thisAs<ObjDuration>().duration.toDouble(DurationUnit.HOURS).toObj()
|
||||
}
|
||||
addFn("minutes") {
|
||||
thisAs<ObjDuration>().duration.toDouble(DurationUnit.MINUTES).toObj()
|
||||
}
|
||||
addFn("seconds") {
|
||||
thisAs<ObjDuration>().duration.toDouble(DurationUnit.SECONDS).toObj()
|
||||
}
|
||||
addFn("milliseconds") {
|
||||
thisAs<ObjDuration>().duration.toDouble(DurationUnit.MILLISECONDS).toObj()
|
||||
}
|
||||
addFn("microseconds") {
|
||||
thisAs<ObjDuration>().duration.toDouble(DurationUnit.MICROSECONDS).toObj()
|
||||
}
|
||||
addFnDoc(
|
||||
name = "days",
|
||||
doc = "Return this duration as a real number of days.",
|
||||
returns = type("lyng.Real"),
|
||||
moduleName = "lyng.time"
|
||||
) { thisAs<ObjDuration>().duration.toDouble(DurationUnit.DAYS).toObj() }
|
||||
addFnDoc(
|
||||
name = "hours",
|
||||
doc = "Return this duration as a real number of hours.",
|
||||
returns = type("lyng.Real"),
|
||||
moduleName = "lyng.time"
|
||||
) { thisAs<ObjDuration>().duration.toDouble(DurationUnit.HOURS).toObj() }
|
||||
addFnDoc(
|
||||
name = "minutes",
|
||||
doc = "Return this duration as a real number of minutes.",
|
||||
returns = type("lyng.Real"),
|
||||
moduleName = "lyng.time"
|
||||
) { thisAs<ObjDuration>().duration.toDouble(DurationUnit.MINUTES).toObj() }
|
||||
addFnDoc(
|
||||
name = "seconds",
|
||||
doc = "Return this duration as a real number of seconds.",
|
||||
returns = type("lyng.Real"),
|
||||
moduleName = "lyng.time"
|
||||
) { thisAs<ObjDuration>().duration.toDouble(DurationUnit.SECONDS).toObj() }
|
||||
addFnDoc(
|
||||
name = "milliseconds",
|
||||
doc = "Return this duration as a real number of milliseconds.",
|
||||
returns = type("lyng.Real"),
|
||||
moduleName = "lyng.time"
|
||||
) { thisAs<ObjDuration>().duration.toDouble(DurationUnit.MILLISECONDS).toObj() }
|
||||
addFnDoc(
|
||||
name = "microseconds",
|
||||
doc = "Return this duration as a real number of microseconds.",
|
||||
returns = type("lyng.Real"),
|
||||
moduleName = "lyng.time"
|
||||
) { thisAs<ObjDuration>().duration.toDouble(DurationUnit.MICROSECONDS).toObj() }
|
||||
// extensions
|
||||
|
||||
ObjInt.type.addFn("seconds") {
|
||||
ObjDuration(thisAs<ObjInt>().value.seconds)
|
||||
}
|
||||
ObjInt.type.addFnDoc(
|
||||
name = "seconds",
|
||||
doc = "Construct a `Duration` equal to this integer number of seconds.",
|
||||
returns = type("lyng.Duration"),
|
||||
moduleName = "lyng.time"
|
||||
) { ObjDuration(thisAs<ObjInt>().value.seconds) }
|
||||
|
||||
ObjInt.type.addFn("second") {
|
||||
ObjDuration(thisAs<ObjInt>().value.seconds)
|
||||
}
|
||||
ObjInt.type.addFn("milliseconds") {
|
||||
ObjDuration(thisAs<ObjInt>().value.milliseconds)
|
||||
}
|
||||
ObjInt.type.addFnDoc(
|
||||
name = "second",
|
||||
doc = "Construct a `Duration` equal to this integer number of seconds.",
|
||||
returns = type("lyng.Duration"),
|
||||
moduleName = "lyng.time"
|
||||
) { ObjDuration(thisAs<ObjInt>().value.seconds) }
|
||||
ObjInt.type.addFnDoc(
|
||||
name = "milliseconds",
|
||||
doc = "Construct a `Duration` equal to this integer number of milliseconds.",
|
||||
returns = type("lyng.Duration"),
|
||||
moduleName = "lyng.time"
|
||||
) { ObjDuration(thisAs<ObjInt>().value.milliseconds) }
|
||||
|
||||
ObjInt.type.addFn("millisecond") {
|
||||
ObjDuration(thisAs<ObjInt>().value.milliseconds)
|
||||
}
|
||||
ObjReal.type.addFn("seconds") {
|
||||
ObjDuration(thisAs<ObjReal>().value.seconds)
|
||||
}
|
||||
ObjInt.type.addFnDoc(
|
||||
name = "millisecond",
|
||||
doc = "Construct a `Duration` equal to this integer number of milliseconds.",
|
||||
returns = type("lyng.Duration"),
|
||||
moduleName = "lyng.time"
|
||||
) { ObjDuration(thisAs<ObjInt>().value.milliseconds) }
|
||||
ObjReal.type.addFnDoc(
|
||||
name = "seconds",
|
||||
doc = "Construct a `Duration` equal to this real number of seconds.",
|
||||
returns = type("lyng.Duration"),
|
||||
moduleName = "lyng.time"
|
||||
) { ObjDuration(thisAs<ObjReal>().value.seconds) }
|
||||
|
||||
ObjReal.type.addFn("second") {
|
||||
ObjDuration(thisAs<ObjReal>().value.seconds)
|
||||
}
|
||||
ObjReal.type.addFnDoc(
|
||||
name = "second",
|
||||
doc = "Construct a `Duration` equal to this real number of seconds.",
|
||||
returns = type("lyng.Duration"),
|
||||
moduleName = "lyng.time"
|
||||
) { ObjDuration(thisAs<ObjReal>().value.seconds) }
|
||||
|
||||
ObjReal.type.addFn("milliseconds") {
|
||||
ObjDuration(thisAs<ObjReal>().value.milliseconds)
|
||||
}
|
||||
ObjReal.type.addFn("millisecond") {
|
||||
ObjDuration(thisAs<ObjReal>().value.milliseconds)
|
||||
}
|
||||
ObjReal.type.addFnDoc(
|
||||
name = "milliseconds",
|
||||
doc = "Construct a `Duration` equal to this real number of milliseconds.",
|
||||
returns = type("lyng.Duration"),
|
||||
moduleName = "lyng.time"
|
||||
) { ObjDuration(thisAs<ObjReal>().value.milliseconds) }
|
||||
ObjReal.type.addFnDoc(
|
||||
name = "millisecond",
|
||||
doc = "Construct a `Duration` equal to this real number of milliseconds.",
|
||||
returns = type("lyng.Duration"),
|
||||
moduleName = "lyng.time"
|
||||
) { ObjDuration(thisAs<ObjReal>().value.milliseconds) }
|
||||
|
||||
ObjInt.type.addFn("minutes") {
|
||||
ObjDuration(thisAs<ObjInt>().value.minutes)
|
||||
}
|
||||
ObjReal.type.addFn("minutes") {
|
||||
ObjDuration(thisAs<ObjReal>().value.minutes)
|
||||
}
|
||||
ObjInt.type.addFn("minute") {
|
||||
ObjDuration(thisAs<ObjInt>().value.minutes)
|
||||
}
|
||||
ObjReal.type.addFn("minute") {
|
||||
ObjDuration(thisAs<ObjReal>().value.minutes)
|
||||
}
|
||||
ObjInt.type.addFn("hours") {
|
||||
ObjDuration(thisAs<ObjInt>().value.hours)
|
||||
}
|
||||
ObjReal.type.addFn("hours") {
|
||||
ObjDuration(thisAs<ObjReal>().value.hours)
|
||||
}
|
||||
ObjInt.type.addFn("hour") {
|
||||
ObjDuration(thisAs<ObjInt>().value.hours)
|
||||
}
|
||||
ObjReal.type.addFn("hour") {
|
||||
ObjDuration(thisAs<ObjReal>().value.hours)
|
||||
}
|
||||
ObjInt.type.addFn("days") {
|
||||
ObjDuration(thisAs<ObjInt>().value.days)
|
||||
}
|
||||
ObjReal.type.addFn("days") {
|
||||
ObjDuration(thisAs<ObjReal>().value.days)
|
||||
}
|
||||
ObjInt.type.addFn("day") {
|
||||
ObjDuration(thisAs<ObjInt>().value.days)
|
||||
}
|
||||
ObjReal.type.addFn("day") {
|
||||
ObjDuration(thisAs<ObjReal>().value.days)
|
||||
}
|
||||
ObjInt.type.addFnDoc(
|
||||
name = "minutes",
|
||||
doc = "Construct a `Duration` equal to this integer number of minutes.",
|
||||
returns = type("lyng.Duration"),
|
||||
moduleName = "lyng.time"
|
||||
) { ObjDuration(thisAs<ObjInt>().value.minutes) }
|
||||
ObjReal.type.addFnDoc(
|
||||
name = "minutes",
|
||||
doc = "Construct a `Duration` equal to this real number of minutes.",
|
||||
returns = type("lyng.Duration"),
|
||||
moduleName = "lyng.time"
|
||||
) { ObjDuration(thisAs<ObjReal>().value.minutes) }
|
||||
ObjInt.type.addFnDoc(
|
||||
name = "minute",
|
||||
doc = "Construct a `Duration` equal to this integer number of minutes.",
|
||||
returns = type("lyng.Duration"),
|
||||
moduleName = "lyng.time"
|
||||
) { ObjDuration(thisAs<ObjInt>().value.minutes) }
|
||||
ObjReal.type.addFnDoc(
|
||||
name = "minute",
|
||||
doc = "Construct a `Duration` equal to this real number of minutes.",
|
||||
returns = type("lyng.Duration"),
|
||||
moduleName = "lyng.time"
|
||||
) { ObjDuration(thisAs<ObjReal>().value.minutes) }
|
||||
ObjInt.type.addFnDoc(
|
||||
name = "hours",
|
||||
doc = "Construct a `Duration` equal to this integer number of hours.",
|
||||
returns = type("lyng.Duration"),
|
||||
moduleName = "lyng.time"
|
||||
) { ObjDuration(thisAs<ObjInt>().value.hours) }
|
||||
ObjReal.type.addFnDoc(
|
||||
name = "hours",
|
||||
doc = "Construct a `Duration` equal to this real number of hours.",
|
||||
returns = type("lyng.Duration"),
|
||||
moduleName = "lyng.time"
|
||||
) { ObjDuration(thisAs<ObjReal>().value.hours) }
|
||||
ObjInt.type.addFnDoc(
|
||||
name = "hour",
|
||||
doc = "Construct a `Duration` equal to this integer number of hours.",
|
||||
returns = type("lyng.Duration"),
|
||||
moduleName = "lyng.time"
|
||||
) { ObjDuration(thisAs<ObjInt>().value.hours) }
|
||||
ObjReal.type.addFnDoc(
|
||||
name = "hour",
|
||||
doc = "Construct a `Duration` equal to this real number of hours.",
|
||||
returns = type("lyng.Duration"),
|
||||
moduleName = "lyng.time"
|
||||
) { ObjDuration(thisAs<ObjReal>().value.hours) }
|
||||
ObjInt.type.addFnDoc(
|
||||
name = "days",
|
||||
doc = "Construct a `Duration` equal to this integer number of days.",
|
||||
returns = type("lyng.Duration"),
|
||||
moduleName = "lyng.time"
|
||||
) { ObjDuration(thisAs<ObjInt>().value.days) }
|
||||
ObjReal.type.addFnDoc(
|
||||
name = "days",
|
||||
doc = "Construct a `Duration` equal to this real number of days.",
|
||||
returns = type("lyng.Duration"),
|
||||
moduleName = "lyng.time"
|
||||
) { ObjDuration(thisAs<ObjReal>().value.days) }
|
||||
ObjInt.type.addFnDoc(
|
||||
name = "day",
|
||||
doc = "Construct a `Duration` equal to this integer number of days.",
|
||||
returns = type("lyng.Duration"),
|
||||
moduleName = "lyng.time"
|
||||
) { ObjDuration(thisAs<ObjInt>().value.days) }
|
||||
ObjReal.type.addFnDoc(
|
||||
name = "day",
|
||||
doc = "Construct a `Duration` equal to this real number of days.",
|
||||
returns = type("lyng.Duration"),
|
||||
moduleName = "lyng.time"
|
||||
) { ObjDuration(thisAs<ObjReal>().value.days) }
|
||||
|
||||
|
||||
// addFn("epochSeconds") {
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
package net.sergeych.lyng.obj
|
||||
|
||||
import net.sergeych.lyng.Arguments
|
||||
import net.sergeych.lyng.ClosureScope
|
||||
import net.sergeych.lyng.Scope
|
||||
import net.sergeych.lyng.Statement
|
||||
|
||||
@@ -54,13 +55,16 @@ class ObjDynamicContext(val delegate: ObjDynamic) : Obj() {
|
||||
open class ObjDynamic(var readCallback: Statement? = null, var writeCallback: Statement? = null) : Obj() {
|
||||
|
||||
override val objClass: ObjClass = type
|
||||
// Capture the lexical scope used to build this dynamic so callbacks can see outer locals
|
||||
internal var builderScope: Scope? = null
|
||||
|
||||
/**
|
||||
* Use read callback to dynamically resolve the field name. Note that it does not work
|
||||
* with method invocation which is implemented separately in [invokeInstanceMethod] below.
|
||||
*/
|
||||
override suspend fun readField(scope: Scope, name: String): ObjRecord {
|
||||
return readCallback?.execute(scope.createChildScope(Arguments(ObjString(name))))?.let {
|
||||
val execBase = builderScope?.let { ClosureScope(scope, it) } ?: scope
|
||||
return readCallback?.execute(execBase.createChildScope(Arguments(ObjString(name))))?.let {
|
||||
if (writeCallback != null)
|
||||
it.asMutable
|
||||
else
|
||||
@@ -77,30 +81,29 @@ open class ObjDynamic(var readCallback: Statement? = null, var writeCallback: St
|
||||
scope: Scope,
|
||||
name: String,
|
||||
args: Arguments,
|
||||
onNotFoundResult: (() -> Obj?)?
|
||||
onNotFoundResult: (suspend () -> Obj?)?
|
||||
): Obj {
|
||||
val over = readCallback?.execute(
|
||||
scope.createChildScope(
|
||||
Arguments(ObjString(name)
|
||||
)
|
||||
)
|
||||
)
|
||||
val execBase = builderScope?.let { ClosureScope(scope, it) } ?: scope
|
||||
val over = readCallback?.execute(execBase.createChildScope(Arguments(ObjString(name))))
|
||||
return over?.invoke(scope, scope.thisObj, args)
|
||||
?: super.invokeInstanceMethod(scope, name, args, onNotFoundResult)
|
||||
}
|
||||
|
||||
override suspend fun writeField(scope: Scope, name: String, newValue: Obj) {
|
||||
writeCallback?.execute(scope.createChildScope(Arguments(ObjString(name), newValue)))
|
||||
val execBase = builderScope?.let { ClosureScope(scope, it) } ?: scope
|
||||
writeCallback?.execute(execBase.createChildScope(Arguments(ObjString(name), newValue)))
|
||||
?: super.writeField(scope, name, newValue)
|
||||
}
|
||||
|
||||
override suspend fun getAt(scope: Scope, index: Obj): Obj {
|
||||
return readCallback?.execute(scope.createChildScope(Arguments(index)))
|
||||
val execBase = builderScope?.let { ClosureScope(scope, it) } ?: scope
|
||||
return readCallback?.execute(execBase.createChildScope(Arguments(index)))
|
||||
?: super.getAt(scope, index)
|
||||
}
|
||||
|
||||
override suspend fun putAt(scope: Scope, index: Obj, newValue: Obj) {
|
||||
writeCallback?.execute(scope.createChildScope(Arguments(index, newValue)))
|
||||
val execBase = builderScope?.let { ClosureScope(scope, it) } ?: scope
|
||||
writeCallback?.execute(execBase.createChildScope(Arguments(index, newValue)))
|
||||
?: super.putAt(scope, index, newValue)
|
||||
}
|
||||
|
||||
@@ -109,7 +112,12 @@ open class ObjDynamic(var readCallback: Statement? = null, var writeCallback: St
|
||||
suspend fun create(scope: Scope, builder: Statement): ObjDynamic {
|
||||
val delegate = ObjDynamic()
|
||||
val context = ObjDynamicContext(delegate)
|
||||
builder.execute(scope.createChildScope(newThisObj = context))
|
||||
// Capture the function's lexical scope (scope) so callbacks can see outer locals like parameters.
|
||||
// Build the dynamic in a child scope purely to set `this` to context, but keep captured closure at parent.
|
||||
val buildScope = scope.createChildScope(newThisObj = context)
|
||||
// Snapshot the caller scope to capture locals/args even if the runtime pools/reuses frames
|
||||
delegate.builderScope = scope.snapshotForClosure()
|
||||
builder.execute(buildScope)
|
||||
return delegate
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,21 @@
|
||||
/*
|
||||
* 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.obj/*
|
||||
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
@@ -15,8 +32,9 @@
|
||||
*
|
||||
*/
|
||||
|
||||
import kotlinx.serialization.json.JsonElement
|
||||
import kotlinx.serialization.json.JsonPrimitive
|
||||
import net.sergeych.lyng.Scope
|
||||
import net.sergeych.lyng.obj.*
|
||||
import net.sergeych.lynon.LynonDecoder
|
||||
import net.sergeych.lynon.LynonEncoder
|
||||
import net.sergeych.lynon.LynonType
|
||||
@@ -38,6 +56,10 @@ open class ObjEnumEntry(enumClass: ObjEnumClass, val name: ObjString, val ordina
|
||||
return ordinal.compareTo(scope, other.ordinal)
|
||||
}
|
||||
|
||||
override suspend fun toJson(scope: Scope): JsonElement {
|
||||
return JsonPrimitive(name.value)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
object EnumBase : ObjClass("Enum") {
|
||||
|
||||
Loaded 100 of 161 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user