Compare commits
9 Commits
ab12212df2
...
0574629904
| Author | SHA1 | Date | |
|---|---|---|---|
| 0574629904 | |||
| 26723b83ff | |||
| 44b30d2339 | |||
| 3aa75843ac | |||
| 061452a334 | |||
| 4494cbce91 | |||
| 47d1ea1a0b | |||
| 7645efc4f9 | |||
| 47c3fda4c8 |
4
.gitignore
vendored
4
.gitignore
vendored
|
|
@ -34,4 +34,6 @@ report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
|
|||
.DS_Store
|
||||
|
||||
/tmp
|
||||
/docs
|
||||
/docs
|
||||
|
||||
*.vsix
|
||||
|
|
@ -200,7 +200,7 @@ function parseExpression(input: string) {
|
|||
- **Not in scope** → Parses as `Word("obj.prop")` → compiles to `PUSH 'obj.prop'` (treated as file path/string)
|
||||
|
||||
Implementation files:
|
||||
- **src/parser/scopeTracker.ts**: ContextTracker that maintains immutable scope chain
|
||||
- **src/parser/parserScopeContext.ts**: ContextTracker that maintains immutable scope chain
|
||||
- **src/parser/tokenizer.ts**: External tokenizer checks `stack.context` to decide if dot creates DotGet or Word
|
||||
- Scope tracking: Captures variables from assignments (`x = 5`) and function parameters (`fn x:`)
|
||||
- See `src/parser/tests/dot-get.test.ts` for comprehensive examples
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
@external propSource highlighting from "./highlight"
|
||||
|
||||
@context trackScope from "./scopeTracker"
|
||||
@context trackScope from "./parserScopeContext"
|
||||
|
||||
@skip { space | Comment }
|
||||
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
import {LRParser, LocalTokenGroup} from "@lezer/lr"
|
||||
import {operatorTokenizer} from "./operatorTokenizer"
|
||||
import {tokenizer, specializeKeyword} from "./tokenizer"
|
||||
import {trackScope} from "./scopeTracker"
|
||||
import {trackScope} from "./parserScopeContext"
|
||||
import {highlighting} from "./highlight"
|
||||
const spec_Identifier = {__proto__:null,if:66, null:94, catch:100, finally:106, end:108, else:116, while:130, try:136, throw:140}
|
||||
export const parser = LRParser.deserialize({
|
||||
|
|
|
|||
4
vscode-extension/.gitignore
vendored
4
vscode-extension/.gitignore
vendored
|
|
@ -1,4 +0,0 @@
|
|||
node_modules
|
||||
client/dist
|
||||
server/dist
|
||||
*.vsix
|
||||
|
|
@ -1,12 +1,12 @@
|
|||
import { TextDocument, Position } from 'vscode-languageserver-textdocument'
|
||||
import { Diagnostic, DiagnosticSeverity } from 'vscode-languageserver/node'
|
||||
import { parser } from '../../../src/parser/shrimp'
|
||||
import { Tree } from '@lezer/common'
|
||||
import { Compiler } from '../../../src/compiler/compiler'
|
||||
import { CompilerError } from '../../../src/compiler/compilerError'
|
||||
|
||||
export const buildDiagnostics = (textDocument: TextDocument): Diagnostic[] => {
|
||||
export const buildDiagnostics = (textDocument: TextDocument, tree: Tree): Diagnostic[] => {
|
||||
const text = textDocument.getText()
|
||||
const diagnostics = getParseErrors(textDocument)
|
||||
const diagnostics = getParseErrors(textDocument, tree)
|
||||
|
||||
if (diagnostics.length > 0) {
|
||||
return diagnostics
|
||||
|
|
@ -59,9 +59,7 @@ const unknownDiagnostic = (message: string): Diagnostic => {
|
|||
return diagnostic
|
||||
}
|
||||
|
||||
const getParseErrors = (textDocument: TextDocument): Diagnostic[] => {
|
||||
const tree = parser.parse(textDocument.getText())
|
||||
|
||||
const getParseErrors = (textDocument: TextDocument, tree: Tree): Diagnostic[] => {
|
||||
const ranges: { start: Position; end: Position }[] = []
|
||||
tree.iterate({
|
||||
enter(n) {
|
||||
|
|
|
|||
|
|
@ -1,10 +1,10 @@
|
|||
import { test, expect, describe } from 'bun:test'
|
||||
import { ScopeTracker } from './scopeTracker'
|
||||
import { EditorScopeAnalyzer } from './editorScopeAnalyzer'
|
||||
import { TextDocument } from 'vscode-languageserver-textdocument'
|
||||
import { parser } from '../../../src/parser/shrimp'
|
||||
import * as Terms from '../../../src/parser/shrimp.terms'
|
||||
|
||||
describe('ScopeTracker', () => {
|
||||
describe('EditorScopeAnalyzer', () => {
|
||||
test('top-level assignment is in scope', () => {
|
||||
const code = 'x = 5\necho x'
|
||||
const { tree, tracker } = parseAndGetScope(code)
|
||||
|
|
@ -146,6 +146,6 @@ end`
|
|||
const parseAndGetScope = (code: string) => {
|
||||
const document = TextDocument.create('test://test.sh', 'shrimp', 1, code)
|
||||
const tree = parser.parse(code)
|
||||
const tracker = new ScopeTracker(document)
|
||||
const tracker = new EditorScopeAnalyzer(document)
|
||||
return { document, tree, tracker }
|
||||
}
|
||||
|
|
@ -7,7 +7,7 @@ import { globals } from '../../../src/prelude'
|
|||
* Tracks variables in scope at a given position in the parse tree.
|
||||
* Used to distinguish identifiers (in scope) from words (not in scope).
|
||||
*/
|
||||
export class ScopeTracker {
|
||||
export class EditorScopeAnalyzer {
|
||||
private document: TextDocument
|
||||
private scopeCache = new Map<number, Set<string>>()
|
||||
|
||||
|
|
@ -1,13 +1,13 @@
|
|||
import { parser } from '../../../src/parser/shrimp'
|
||||
import * as Terms from '../../../src/parser/shrimp.terms'
|
||||
import { SyntaxNode } from '@lezer/common'
|
||||
import { SyntaxNode, Tree } from '@lezer/common'
|
||||
import { TextDocument } from 'vscode-languageserver-textdocument'
|
||||
import {
|
||||
SemanticTokensBuilder,
|
||||
SemanticTokenTypes,
|
||||
SemanticTokenModifiers,
|
||||
} from 'vscode-languageserver/node'
|
||||
import { ScopeTracker } from './scopeTracker'
|
||||
import { EditorScopeAnalyzer } from './editorScopeAnalyzer'
|
||||
|
||||
export const TOKEN_TYPES = [
|
||||
SemanticTokenTypes.function,
|
||||
|
|
@ -28,11 +28,9 @@ export const TOKEN_MODIFIERS = [
|
|||
SemanticTokenModifiers.readonly,
|
||||
]
|
||||
|
||||
export function buildSemanticTokens(document: TextDocument): number[] {
|
||||
const text = document.getText()
|
||||
const tree = parser.parse(text)
|
||||
export function buildSemanticTokens(document: TextDocument, tree: Tree): number[] {
|
||||
const builder = new SemanticTokensBuilder()
|
||||
const scopeTracker = new ScopeTracker(document)
|
||||
const scopeTracker = new EditorScopeAnalyzer(document)
|
||||
|
||||
walkTree(tree.topNode, document, builder, scopeTracker)
|
||||
|
||||
|
|
@ -77,7 +75,7 @@ function walkTree(
|
|||
node: SyntaxNode,
|
||||
document: TextDocument,
|
||||
builder: SemanticTokensBuilder,
|
||||
scopeTracker: ScopeTracker
|
||||
scopeTracker: EditorScopeAnalyzer
|
||||
) {
|
||||
// Special handling for NamedArgPrefix to split "name=" into two tokens
|
||||
if (node.type.id === Terms.NamedArgPrefix) {
|
||||
|
|
@ -104,7 +102,7 @@ type TokenInfo = { type: number; modifiers: number } | undefined
|
|||
function getTokenType(
|
||||
node: SyntaxNode,
|
||||
document: TextDocument,
|
||||
scopeTracker: ScopeTracker
|
||||
scopeTracker: EditorScopeAnalyzer
|
||||
): TokenInfo {
|
||||
const nodeTypeId = node.type.id
|
||||
const parentTypeId = node.parent?.type.id
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ import { buildDiagnostics } from './diagnostics'
|
|||
import { buildSemanticTokens, TOKEN_MODIFIERS, TOKEN_TYPES } from './semanticTokens'
|
||||
import { parser } from '../../../src/parser/shrimp'
|
||||
import { Compiler } from '../../../src/compiler/compiler'
|
||||
import { Tree } from '@lezer/common'
|
||||
import {
|
||||
InitializeResult,
|
||||
TextDocuments,
|
||||
|
|
@ -10,18 +11,23 @@ import {
|
|||
createConnection,
|
||||
ProposedFeatures,
|
||||
CompletionItemKind,
|
||||
TextDocumentChangeEvent,
|
||||
} from 'vscode-languageserver/node'
|
||||
|
||||
const connection = createConnection(ProposedFeatures.all)
|
||||
const documents = new TextDocuments(TextDocument)
|
||||
documents.listen(connection)
|
||||
|
||||
const documentTrees = new Map<string, Tree>()
|
||||
|
||||
// Server capabilities
|
||||
connection.onInitialize(handleInitialize)
|
||||
|
||||
// Language features
|
||||
connection.languages.semanticTokens.on(handleSemanticTokens)
|
||||
documents.onDidOpen(handleDocumentOpen)
|
||||
documents.onDidChangeContent(handleDocumentChange)
|
||||
documents.onDidClose(handleDocumentClose)
|
||||
connection.onCompletion(handleCompletion)
|
||||
|
||||
// Debug commands
|
||||
|
|
@ -31,10 +37,7 @@ connection.onRequest('shrimp/bytecode', handleBytecode)
|
|||
// Start listening
|
||||
connection.listen()
|
||||
|
||||
// ============================================================================
|
||||
// Handler implementations
|
||||
// ============================================================================
|
||||
|
||||
function handleInitialize(): InitializeResult {
|
||||
connection.console.log('🦐 Server initialized with capabilities')
|
||||
const result: InitializeResult = {
|
||||
|
|
@ -56,21 +59,40 @@ function handleInitialize(): InitializeResult {
|
|||
return result
|
||||
}
|
||||
|
||||
function handleDocumentOpen(event: TextDocumentChangeEvent<TextDocument>) {
|
||||
const document = event.document
|
||||
const tree = parser.parse(document.getText())
|
||||
documentTrees.set(document.uri, tree)
|
||||
}
|
||||
|
||||
function handleSemanticTokens(params: any) {
|
||||
const document = documents.get(params.textDocument.uri)
|
||||
if (!document) return { data: [] }
|
||||
|
||||
const data = buildSemanticTokens(document)
|
||||
const tree = documentTrees.get(params.textDocument.uri)
|
||||
if (!tree) return { data: [] }
|
||||
|
||||
const data = buildSemanticTokens(document, tree)
|
||||
return { data }
|
||||
}
|
||||
|
||||
function handleDocumentChange(change: any) {
|
||||
const textDocument = change.document
|
||||
const diagnostics = buildDiagnostics(textDocument)
|
||||
connection.sendDiagnostics({ uri: textDocument.uri, diagnostics })
|
||||
function handleDocumentChange(change: TextDocumentChangeEvent<TextDocument>) {
|
||||
const document = change.document
|
||||
|
||||
// Parse and cache
|
||||
const tree = parser.parse(document.getText())
|
||||
documentTrees.set(document.uri, tree)
|
||||
|
||||
// Build diagnostics using cached tree
|
||||
const diagnostics = buildDiagnostics(document, tree)
|
||||
connection.sendDiagnostics({ uri: document.uri, diagnostics })
|
||||
}
|
||||
|
||||
function handleCompletion(params: any) {
|
||||
function handleDocumentClose(event: TextDocumentChangeEvent<TextDocument>) {
|
||||
documentTrees.delete(event.document.uri)
|
||||
}
|
||||
|
||||
function handleCompletion() {
|
||||
const keywords = ['if', 'else', 'do', 'end', 'and', 'or', 'true', 'false', 'null']
|
||||
|
||||
return keywords.map((keyword) => ({
|
||||
|
|
@ -84,8 +106,13 @@ function handleParseTree(params: { uri: string }) {
|
|||
const document = documents.get(params.uri)
|
||||
if (!document) return 'Document not found'
|
||||
|
||||
const tree = documentTrees.get(params.uri)
|
||||
if (!tree) {
|
||||
connection.console.error(`🦐 No cached tree for ${params.uri}`)
|
||||
return 'No cached parse tree available'
|
||||
}
|
||||
|
||||
const text = document.getText()
|
||||
const tree = parser.parse(text)
|
||||
const cursor = tree.cursor()
|
||||
|
||||
let formatted = ''
|
||||
|
|
|
|||
Binary file not shown.
Loading…
Reference in New Issue
Block a user