rune-langium / core/src / parse
Function: parse()
ts
function parse(input, uri?): Promise<ParseResult>;Defined in: packages/core/src/api/parse.ts:111
Parse a Rosetta DSL source string into a typed AST.
Parameters
| Parameter | Type | Description |
|---|---|---|
input | string | The Rosetta DSL source text. |
uri? | string | Optional URI for the document (defaults to inmemory:///model.rosetta). |
Returns
Promise<ParseResult>
A ParseResult with the root RosettaModel node and any errors.
Remarks
Services are lazily initialized on first call and reused across subsequent calls. The services singleton is module-level — concurrent calls to parse() in tests that re-import the module may share state unexpectedly. Use createRuneDslServices() directly in long-running servers.
Use When
- Validating a single
.rosettafile or snippet in memory - Building a parse pipeline in a Node.js script
- Unit-testing grammar rules in isolation
Avoid When
- Parsing files that have cross-references to other documents — unresolved references will have
ref === undefined. UseparseWorkspace()instead. - Running inside a Langium LSP server — the DocumentBuilder is already managed by the server lifecycle; calling
parse()creates a second services instance and wastes memory.
Pitfalls
- Do NOT call
parse()on a file whose type references live in other.rosettafiles — cross-file references will be unresolved (undefined). Provide all documents toparseWorkspace()for resolved cross-references. - Do NOT mutate nodes in the returned
value— Langium's index tracks AST node identity; mutations bypass incremental reparse and corrupt the scope graph.
Example
ts
import { parse } from '@rune-langium/core';
const result = await parse(`
namespace com.example
version "1.0.0"
type Trade:
quantity number (1..1)
`);
if (result.hasErrors) {
console.error(result.lexerErrors, result.parserErrors);
} else {
console.log(result.value.elements.length, 'elements');
}