CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-rapydscript-ng

Pythonic JavaScript that doesn't suck - a Python-to-JavaScript transpiler with clean syntax and performance

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

compilation.mddocs/

Compilation and Parsing

Core compilation functionality for transforming RapydScript source code (.pyj files) into optimized JavaScript. The compilation system provides complete parsing, AST manipulation, and code generation capabilities.

Capabilities

Compiler Factory

Creates a compiler instance with all compilation facilities in an isolated VM context.

/**
 * Creates a compiler instance with all compilation facilities
 * @returns Compiler instance with parsing and output capabilities
 */
function create_compiler(): CompilerInstance;

interface CompilerInstance {
  parse: (code: string, options: ParseOptions) => AST_Toplevel;
  OutputStream: new (options: OutputOptions) => OutputStream;
  DefaultsError: typeof Error;
  SyntaxError: typeof Error;
  ImportError: typeof Error;
  string_template: (template: string, vars: object) => string;
  tokenizer: (code: string, options: object) => TokenStream;
  ALL_KEYWORDS: string[];
  IDENTIFIER_PAT: RegExp;
  NATIVE_CLASSES: string[];
}

Usage Example:

const { create_compiler } = require("rapydscript-ng");
const RapydScript = create_compiler();

// Access all compiler facilities
const ast = RapydScript.parse(code, options);
const output = new RapydScript.OutputStream({ beautify: true });

Parse Function

Main parsing function that converts RapydScript source code into an Abstract Syntax Tree.

/**
 * Parse RapydScript source code into an AST
 * @param code - RapydScript source code string
 * @param options - Parsing configuration options
 * @returns AST_Toplevel root node
 * @throws SyntaxError on parsing failures
 */
function parse(code: string, options: ParseOptions): AST_Toplevel;

interface ParseOptions {
  /** Source filename for error reporting */
  filename?: string;
  /** Existing toplevel AST to extend */
  toplevel?: AST_Toplevel;
  /** Base directory for relative imports */
  basedir?: string;
  /** Standard library directory path */
  libdir?: string;
  /** Additional import search directories */
  import_dirs?: string[];
  /** Remove assert statements during parsing */
  discard_asserts?: boolean;
  /** Cache directory for compiled modules */
  module_cache_dir?: string;
}

Usage Examples:

// Basic parsing
const ast = RapydScript.parse(`
def hello(name):
    print(f"Hello, {name}!")
`, { filename: 'hello.pyj' });

// Advanced parsing with options
const ast = RapydScript.parse(sourceCode, {
  filename: 'main.pyj',
  basedir: '/path/to/project',
  libdir: '/path/to/rapydscript/lib',
  import_dirs: ['/custom/modules'],
  discard_asserts: true
});

Output Stream

Output stream class that manages JavaScript code generation with formatting and optimization options.

/**
 * Creates an output stream for JavaScript code generation
 * @param options - Output formatting and optimization options
 */
class OutputStream {
  constructor(options: OutputOptions);
  
  /** Print text to the output stream */
  print(text: string): void;
  
  /** Execute function with increased indentation */
  with_indent(body: () => void): void;
  
  /** Get the complete generated output */
  get(): string;
  
  /** Get the complete generated output (alias for get) */
  toString(): string;
}

interface OutputOptions {
  /** Generate formatted, readable JavaScript */
  beautify?: boolean;
  /** Wrap output in private scope (function wrapper) */
  private_scope?: boolean;
  /** Exclude base library functions from output */
  omit_baselib?: boolean;
  /** Target JavaScript version (5 or 6) */
  js_version?: number;
  /** Preserve docstrings as __doc__ attributes */
  keep_docstrings?: boolean;
  /** Remove assert statements from output */
  discard_asserts?: boolean;
  /** Cache directory for compiled modules */
  module_cache_dir?: string;
  /** Pre-loaded base library code */
  baselib_plain?: string;
  /** Comment preservation strategy */
  comments?: boolean | Function;
}

Usage Examples:

// Basic output generation
const output = new RapydScript.OutputStream({ 
  beautify: true,
  private_scope: true 
});
ast.print(output);
const javascript = output.get();

// Minified output for production
const minified = new RapydScript.OutputStream({ 
  beautify: false,
  omit_baselib: true,
  js_version: 6 
});
ast.print(minified);
const compactJs = minified.get();

Error Classes

Exception classes used during compilation for different types of errors.

/**
 * Thrown when invalid compiler options are provided
 */
class DefaultsError extends Error {
  constructor(message: string);
}

/**
 * Thrown when parsing fails due to syntax errors
 */
class SyntaxError extends Error {
  constructor(message: string, filename?: string, line?: number, col?: number);
}

/**
 * Thrown when module imports cannot be resolved
 */
class ImportError extends Error {
  constructor(message: string, filename?: string);
}

Utility Functions

Helper functions for string templating and lexical analysis.

/**
 * Simple string templating with variable substitution
 * @param template - Template string with {variable} placeholders
 * @param vars - Object containing variable values
 * @returns Interpolated string
 */
function string_template(template: string, vars: object): string;

/**
 * Tokenize RapydScript source code into a token stream
 * @param code - Source code to tokenize
 * @param options - Tokenization options
 * @returns Token stream for parsing
 */
function tokenizer(code: string, options: object): TokenStream;

/**
 * Access to compile-time decorators registry
 * @returns Object containing available compile-time decorators
 */
const compile_time_decorators: object;

Parser Constants

Constants used by the parser for keyword recognition and identifier validation.

/** Array of all reserved keywords in RapydScript */
const ALL_KEYWORDS: string[];

/** Regular expression pattern for valid identifiers */
const IDENTIFIER_PAT: RegExp;

/** List of built-in JavaScript classes recognized by the compiler */
const NATIVE_CLASSES: string[];

Usage Examples:

// Check if a word is a reserved keyword
const isKeyword = RapydScript.ALL_KEYWORDS.includes('def'); // true

// Validate identifier names
const isValidId = RapydScript.IDENTIFIER_PAT.test('my_variable'); // true

// Check for native JavaScript classes
const isNative = RapydScript.NATIVE_CLASSES.includes('Array'); // true

Complete Compilation Workflow

const { create_compiler } = require("rapydscript-ng");
const fs = require("fs");

// Create compiler instance
const RapydScript = create_compiler();

// Read source file
const sourceCode = fs.readFileSync("app.pyj", "utf-8");

try {
  // Parse into AST
  const ast = RapydScript.parse(sourceCode, {
    filename: "app.pyj",
    basedir: __dirname,
    libdir: "/usr/local/lib/rapydscript/lib"
  });
  
  // Generate JavaScript
  const output = new RapydScript.OutputStream({
    beautify: true,
    private_scope: false,
    js_version: 5
  });
  
  ast.print(output);
  const javascript = output.get();
  
  // Write output
  fs.writeFileSync("app.js", javascript);
  
} catch (error) {
  if (error instanceof RapydScript.SyntaxError) {
    console.error("Syntax error:", error.message);
  } else if (error instanceof RapydScript.ImportError) {
    console.error("Import error:", error.message);
  } else {
    throw error;
  }
}

Install with Tessl CLI

npx tessl i tessl/npm-rapydscript-ng

docs

ast.md

cli.md

compilation.md

embedded.md

index.md

libraries.md

output.md

tile.json