CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-codehaus-groovy--groovy-console

A Swing-based interactive console for evaluating Groovy expressions and scripts with syntax highlighting, code completion, and an integrated AST browser.

Pending
Overview
Eval results
Files

ast-browser.mddocs/

AST Browser

The AST Browser provides powerful Abstract Syntax Tree visualization and analysis tools for understanding Groovy code structure, compilation phases, and bytecode generation.

Main AST Browser

class AstBrowser {
    // Constructor
    AstBrowser(inputArea, rootElement, classLoader, config = null)
    
    // Core Operations
    void refresh()
    static void main(String[] args)
}

Parameters

Constructor Parameters:

  • inputArea: Input text area containing the source code to analyze
  • rootElement: Root element for the AST tree display
  • classLoader: ClassLoader for compilation and class loading
  • config (CompilerConfiguration, optional): Compiler configuration, defaults to null

main() Parameters:

  • args (String[]): Command line arguments for standalone AST browser

Returns

  • refresh(): void - Refreshes the AST display with current input
  • main(): void - Launches standalone AST browser application

Compilation Phase Analysis

enum CompilePhaseAdapter {
    INITIALIZATION,
    PARSING,
    CONVERSION, 
    SEMANTIC_ANALYSIS,
    CANONICALIZATION,
    INSTRUCTION_SELECTION,
    CLASS_GENERATION,
    OUTPUT,
    FINALIZATION
}

The CompilePhaseAdapter enum represents the different phases of Groovy compilation that can be analyzed in the AST browser.

AST Tree Components

Tree Node Management

class TreeNodeWithProperties extends DefaultMutableTreeNode {
    List<List<String>> properties
    
    // Constructor
    TreeNodeWithProperties(Object userObject)
    TreeNodeWithProperties(Object userObject, List<List<String>> properties)
    
    // Property Management
    List<List<String>> getProperties()
    void setProperties(List<List<String>> properties)
}

interface AstBrowserNodeMaker<T> {
    // Interface for creating different types of tree nodes
}

class SwingTreeNodeMaker implements AstBrowserNodeMaker<DefaultMutableTreeNode> {
    // Creates Swing tree nodes for AST display
}

AST to Script Conversion

class AstNodeToScriptAdapter {
    static void main(String[] args)
    // Converts AST nodes back to script text
}

class AstNodeToScriptVisitor implements GroovyCodeVisitor, GroovyClassVisitor {
    // Visitor pattern implementation for AST traversal and conversion
}

Script to Tree Conversion

class ScriptToTreeNodeAdapter {
    // Converts Groovy scripts to tree node representations
}

class TreeNodeBuildingNodeOperation extends PrimaryClassNodeOperation {
    // Operation for building tree nodes during compilation
}

class TreeNodeBuildingVisitor extends CodeVisitorSupport {
    // Visitor for building tree structures from AST
}

Bytecode Analysis

class BytecodeCollector extends ClassCollector {
    // Collects bytecode information during compilation
}

class GeneratedBytecodeAwareGroovyClassLoader extends GroovyClassLoader {
    // ClassLoader that tracks generated bytecode for analysis
}

UI Preferences

class AstBrowserUiPreferences {
    // UI configuration and preferences for AST browser
}

Usage Examples

Standalone AST Browser

// Launch standalone AST browser
groovy.console.ui.AstBrowser.main([] as String[])

// Or from legacy package
groovy.inspect.swingui.AstBrowser.main([] as String[])

Programmatic AST Analysis

def sourceCode = '''
class Example {
    def method() {
        println "Hello World"
    }
}
'''

def inputArea = new JTextArea(sourceCode)
def rootElement = new DefaultMutableTreeNode("Root")
def classLoader = new GroovyClassLoader()

def astBrowser = new groovy.console.ui.AstBrowser(inputArea, rootElement, classLoader)
astBrowser.refresh()

AST Node to Script Conversion

// Convert AST nodes back to script text
groovy.console.ui.AstNodeToScriptAdapter.main([
    "--source", "MyScript.groovy"
] as String[])

Integration with Console

The AST browser integrates with the main Console application:

Console Integration

From the Console, access AST browser via:

  • inspectAst() method - Opens AST browser for current script
  • inspectTokens() method - Shows token analysis
  • Menu options for AST analysis

Real-time Analysis

The AST browser provides:

  • Real-time AST updates as code changes
  • Compilation phase visualization
  • Bytecode generation tracking
  • Error location highlighting

Text-based AST Analysis

For non-GUI environments, text-based AST analysis is available:

class TextNode {
    String text
    List<TextNode> children
    
    // Constructors
    TextNode(String text)
    TextNode(String text, List<TextNode> children)
    
    // Tree Navigation
    String getText()
    void setText(String text)
    List<TextNode> getChildren()
    void setChildren(List<TextNode> children)
    void addChild(TextNode child)
    
    // String Representation
    String toString()
}

class TextTreeNodeMaker implements AstBrowserNodeMaker<TextNode> {
    // Creates text-based tree nodes for console output
    TextNode makeNode(Object userObject)
    TextNode makeNodeWithProperties(Object userObject, List<List<String>> properties)
}

The TextNode and TextTreeNodeMaker classes provide text-based AST representation for command-line environments or when GUI components are not available. This enables AST analysis in headless environments or for programmatic processing of syntax trees.

Legacy API

The same AST browser functionality is available in the legacy packages:

// Legacy imports (same API)
import groovy.inspect.swingui.AstBrowser
import groovy.inspect.swingui.AstNodeToScriptAdapter
import groovy.inspect.swingui.ScriptToTreeNodeAdapter

Advanced Features

Compilation Phase Analysis

View AST at different compilation phases:

  1. PARSING - Initial syntax tree from source
  2. CONVERSION - Converted to Groovy AST nodes
  3. SEMANTIC_ANALYSIS - Type checking and resolution
  4. CANONICALIZATION - Simplified and optimized AST
  5. INSTRUCTION_SELECTION - Bytecode instruction planning
  6. CLASS_GENERATION - Final bytecode generation

Bytecode Inspection

The AST browser can display:

  • Generated Java bytecode
  • Bytecode instruction mapping to source lines
  • Class structure and metadata
  • Method signatures and implementations

Tree Navigation

Navigate the AST with:

  • Expandable tree structure
  • Property panels for node details
  • Search and filter capabilities
  • Export options for analysis results

Install with Tessl CLI

npx tessl i tessl/maven-org-codehaus-groovy--groovy-console

docs

ast-browser.md

console-application.md

index.md

object-inspector.md

system-integration.md

text-editing.md

tile.json