OpenUILang (openui_lang v1.0.0)

Copy Markdown View Source

Elixir implementation of the OpenUI 1.0 Language specification (openui-lang).

Provides a zero-dependency lexer, Pratt expression parser, schema-aware materializer, incremental streaming parser, runtime evaluator, statement patch merger, serializer, form validation rules, message protocol parser, and system prompt generator.

Summary

Functions

Auto-closes unclosed strings and brackets in a partial openui-lang string.

Builds an OpenUI wire message with ]]>openui: section markers.

Compiles a decoded LibrarySpec (or JSON Schema map) into a compiled catalog (cat) for use with parse/3, stream_parser/2, evaluate_result/2, and json_to_openui/3.

Decodes a JSON string while preserving object key insertion order in "__keys__". Required when loading a LibrarySpec JSON file so positional argument order matches the JSON Schema property order.

Demultiplexes mixed conversational Markdown prose and openui-lang code blocks. Returns %{prose: String.t(), code: String.t()}.

Evaluates a single AST node with the given evaluation context.

Evaluates a ParseResult and returns the canonical OpenUI result map (root, tree, errors, errorObjects, unresolved, orphans, incomplete, state).

Generates a system prompt for an LLM from a LibrarySpec map.

Serializes an ElementNode tree back to openui-lang source text.

Merges an existing openui-lang program with a statement patch and garbage-collects statements unreachable from root_id.

Parses a complete or partial openui-lang source string in one batch pass.

Parses a token list into an AST expression node.

Parses an OpenUI wire message with ]]>openui: section markers.

Parses an openui-lang source string (complete or mid-stream partial) into a flat symbol table map %{"root" => {"Panel", [...]}, "$var" => "default", ...} for TUI forward-reference resolution, 80-column layout clamping, and live $live_* bindings.

Pushes a streamed chunk into the streaming parser and returns {parse_result, updated_stream_parser}.

Creates a new stateful OpenUILang.Parser streaming parser struct.

Tokenizes an openui-lang source string into a flat token list.

Validates a form value against structured validation rules. Returns nil when valid, or an error string when invalid.

Functions

auto_close(input)

@spec auto_close(String.t()) :: %{text: String.t(), was_incomplete: boolean()}

Auto-closes unclosed strings and brackets in a partial openui-lang string.

build_message(input)

@spec build_message(map()) :: String.t()

Builds an OpenUI wire message with ]]>openui: section markers.

compile_schema(schema)

@spec compile_schema(map() | nil) :: map()

Compiles a decoded LibrarySpec (or JSON Schema map) into a compiled catalog (cat) for use with parse/3, stream_parser/2, evaluate_result/2, and json_to_openui/3.

decode_json!(json_bin)

@spec decode_json!(String.t()) :: term()

Decodes a JSON string while preserving object key insertion order in "__keys__". Required when loading a LibrarySpec JSON file so positional argument order matches the JSON Schema property order.

demux(input)

@spec demux(String.t()) :: %{prose: String.t(), code: String.t()}

Demultiplexes mixed conversational Markdown prose and openui-lang code blocks. Returns %{prose: String.t(), code: String.t()}.

evaluate(ast_node, context, schema_ctx \\ nil)

@spec evaluate(map(), map(), map() | nil) :: term()

Evaluates a single AST node with the given evaluation context.

evaluate_result(parse_result, opts \\ %{})

@spec evaluate_result(map(), map()) :: map()

Evaluates a ParseResult and returns the canonical OpenUI result map (root, tree, errors, errorObjects, unresolved, orphans, incomplete, state).

generate_system_prompt(library_spec, opts \\ [])

@spec generate_system_prompt(map(), keyword()) :: String.t()

Generates a system prompt for an LLM from a LibrarySpec map.

json_to_openui(element_node, compiled_schema, options \\ %{})

@spec json_to_openui(map(), map(), map()) :: String.t()

Serializes an ElementNode tree back to openui-lang source text.

merge_statements(existing, patch, root_id \\ "root")

@spec merge_statements(String.t(), String.t(), String.t()) :: String.t()

Merges an existing openui-lang program with a statement patch and garbage-collects statements unreachable from root_id.

parse(input, compiled_schema \\ nil, root_name \\ nil)

@spec parse(String.t(), map() | nil, String.t() | nil) :: map()

Parses a complete or partial openui-lang source string in one batch pass.

parse_expression(tokens)

@spec parse_expression([map()]) :: map()

Parses a token list into an AST expression node.

parse_message(raw, opts \\ [])

@spec parse_message(String.t(), keyword()) :: map()

Parses an OpenUI wire message with ]]>openui: section markers.

parse_symbols(input)

@spec parse_symbols(String.t()) :: %{optional(String.t()) => term()}

Parses an openui-lang source string (complete or mid-stream partial) into a flat symbol table map %{"root" => {"Panel", [...]}, "$var" => "default", ...} for TUI forward-reference resolution, 80-column layout clamping, and live $live_* bindings.

push_chunk(stream_parser, chunk, complete? \\ false)

@spec push_chunk(OpenUILang.Parser.t(), String.t(), boolean()) ::
  {map(), OpenUILang.Parser.t()}

Pushes a streamed chunk into the streaming parser and returns {parse_result, updated_stream_parser}.

stream_parser(compiled_schema \\ nil, root_name \\ nil)

@spec stream_parser(map() | nil, String.t() | nil) :: OpenUILang.Parser.t()

Creates a new stateful OpenUILang.Parser streaming parser struct.

tokenize(src)

@spec tokenize(String.t()) :: [map()]

Tokenizes an openui-lang source string into a flat token list.

validate_rules(value, rules, custom_validators \\ %{})

@spec validate_rules(term(), map() | [String.t()], map()) :: String.t() | nil

Validates a form value against structured validation rules. Returns nil when valid, or an error string when invalid.