Stone IDL Compiler

3.3.9 · active · verified Sat Apr 11

Stone is an interface description language (IDL) and a suite of tools for defining APIs and generating client/server code in various languages. It is primarily used by Dropbox for its API development. The current version is 3.3.9, and it maintains an active release cadence with frequent patch updates and occasional minor feature additions.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to programmatically parse a simple Stone IDL specification using the `stone` library, accessing its Intermediate Representation (IR). It creates a temporary '.stone' file, parses it, and prints details about the defined namespace, structs, and routes. It does not perform actual code generation to keep the example concise and runnable without side effects.

import os
from stone.frontend.stone_parser import parse_stone_grammar
from stone.ir import Api

# 1. Define a simple Stone spec content
stone_spec_content = """
namespace example

struct User {
    id String
    name String
}

route greet(name: String): User
    "Greets a user."
"""

# Create a temporary .stone file for parsing
temp_stone_file = "temp_spec.stone"
with open(temp_stone_file, "w") as f:
    f.write(stone_spec_content)

try:
    # 2. Parse the Stone spec file(s)
    # parse_stone_grammar expects a list of file paths
    api = parse_stone_grammar([temp_stone_file])

    # 3. Access the parsed Intermediate Representation (IR)
    example_namespace = api.namespaces.get('example')
    if example_namespace:
        print("Parsed API Namespace:", example_namespace.name)
        print("Parsed API Structs:", [s.name for s in example_namespace.data_types if s.is_struct])
        print("Parsed API Routes:", [r.name for r in example_namespace.routes])
    else:
        print("Namespace 'example' not found in parsed API.")

    # For full code generation, you would then instantiate a backend
    # (e.g., PythonResourceBackend) with the 'api' object and call its 'generate' method.

except Exception as e:
    print(f"Error during Stone processing: {e}")
finally:
    # Clean up temporary file
    if os.path.exists(temp_stone_file):
        os.remove(temp_stone_file)

view raw JSON →