or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

ast-browser.mdconsole-application.mdindex.mdobject-inspector.mdsystem-integration.mdtext-editing.md

ast-browser.mddocs/

0

# AST Browser

1

2

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

3

4

## Main AST Browser

5

6

```groovy { .api }

7

class AstBrowser {

8

// Constructor

9

AstBrowser(inputArea, rootElement, classLoader, config = null)

10

11

// Core Operations

12

void refresh()

13

static void main(String[] args)

14

}

15

```

16

17

### Parameters

18

19

**Constructor Parameters:**

20

- `inputArea`: Input text area containing the source code to analyze

21

- `rootElement`: Root element for the AST tree display

22

- `classLoader`: ClassLoader for compilation and class loading

23

- `config` (CompilerConfiguration, optional): Compiler configuration, defaults to null

24

25

**main() Parameters:**

26

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

27

28

### Returns

29

30

- **refresh()**: void - Refreshes the AST display with current input

31

- **main()**: void - Launches standalone AST browser application

32

33

## Compilation Phase Analysis

34

35

```groovy { .api }

36

enum CompilePhaseAdapter {

37

INITIALIZATION,

38

PARSING,

39

CONVERSION,

40

SEMANTIC_ANALYSIS,

41

CANONICALIZATION,

42

INSTRUCTION_SELECTION,

43

CLASS_GENERATION,

44

OUTPUT,

45

FINALIZATION

46

}

47

```

48

49

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

50

51

## AST Tree Components

52

53

### Tree Node Management

54

55

```groovy { .api }

56

class TreeNodeWithProperties extends DefaultMutableTreeNode {

57

List<List<String>> properties

58

59

// Constructor

60

TreeNodeWithProperties(Object userObject)

61

TreeNodeWithProperties(Object userObject, List<List<String>> properties)

62

63

// Property Management

64

List<List<String>> getProperties()

65

void setProperties(List<List<String>> properties)

66

}

67

68

interface AstBrowserNodeMaker<T> {

69

// Interface for creating different types of tree nodes

70

}

71

72

class SwingTreeNodeMaker implements AstBrowserNodeMaker<DefaultMutableTreeNode> {

73

// Creates Swing tree nodes for AST display

74

}

75

```

76

77

### AST to Script Conversion

78

79

```groovy { .api }

80

class AstNodeToScriptAdapter {

81

static void main(String[] args)

82

// Converts AST nodes back to script text

83

}

84

85

class AstNodeToScriptVisitor implements GroovyCodeVisitor, GroovyClassVisitor {

86

// Visitor pattern implementation for AST traversal and conversion

87

}

88

```

89

90

### Script to Tree Conversion

91

92

```groovy { .api }

93

class ScriptToTreeNodeAdapter {

94

// Converts Groovy scripts to tree node representations

95

}

96

97

class TreeNodeBuildingNodeOperation extends PrimaryClassNodeOperation {

98

// Operation for building tree nodes during compilation

99

}

100

101

class TreeNodeBuildingVisitor extends CodeVisitorSupport {

102

// Visitor for building tree structures from AST

103

}

104

```

105

106

## Bytecode Analysis

107

108

```groovy { .api }

109

class BytecodeCollector extends ClassCollector {

110

// Collects bytecode information during compilation

111

}

112

113

class GeneratedBytecodeAwareGroovyClassLoader extends GroovyClassLoader {

114

// ClassLoader that tracks generated bytecode for analysis

115

}

116

```

117

118

## UI Preferences

119

120

```groovy { .api }

121

class AstBrowserUiPreferences {

122

// UI configuration and preferences for AST browser

123

}

124

```

125

126

## Usage Examples

127

128

### Standalone AST Browser

129

130

```groovy

131

// Launch standalone AST browser

132

groovy.console.ui.AstBrowser.main([] as String[])

133

134

// Or from legacy package

135

groovy.inspect.swingui.AstBrowser.main([] as String[])

136

```

137

138

### Programmatic AST Analysis

139

140

```groovy

141

def sourceCode = '''

142

class Example {

143

def method() {

144

println "Hello World"

145

}

146

}

147

'''

148

149

def inputArea = new JTextArea(sourceCode)

150

def rootElement = new DefaultMutableTreeNode("Root")

151

def classLoader = new GroovyClassLoader()

152

153

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

154

astBrowser.refresh()

155

```

156

157

### AST Node to Script Conversion

158

159

```groovy

160

// Convert AST nodes back to script text

161

groovy.console.ui.AstNodeToScriptAdapter.main([

162

"--source", "MyScript.groovy"

163

] as String[])

164

```

165

166

## Integration with Console

167

168

The AST browser integrates with the main Console application:

169

170

### Console Integration

171

172

From the Console, access AST browser via:

173

- **inspectAst()** method - Opens AST browser for current script

174

- **inspectTokens()** method - Shows token analysis

175

- Menu options for AST analysis

176

177

### Real-time Analysis

178

179

The AST browser provides:

180

- Real-time AST updates as code changes

181

- Compilation phase visualization

182

- Bytecode generation tracking

183

- Error location highlighting

184

185

## Text-based AST Analysis

186

187

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

188

189

```groovy { .api }

190

class TextNode {

191

String text

192

List<TextNode> children

193

194

// Constructors

195

TextNode(String text)

196

TextNode(String text, List<TextNode> children)

197

198

// Tree Navigation

199

String getText()

200

void setText(String text)

201

List<TextNode> getChildren()

202

void setChildren(List<TextNode> children)

203

void addChild(TextNode child)

204

205

// String Representation

206

String toString()

207

}

208

209

class TextTreeNodeMaker implements AstBrowserNodeMaker<TextNode> {

210

// Creates text-based tree nodes for console output

211

TextNode makeNode(Object userObject)

212

TextNode makeNodeWithProperties(Object userObject, List<List<String>> properties)

213

}

214

```

215

216

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.

217

218

## Legacy API

219

220

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

221

222

```groovy

223

// Legacy imports (same API)

224

import groovy.inspect.swingui.AstBrowser

225

import groovy.inspect.swingui.AstNodeToScriptAdapter

226

import groovy.inspect.swingui.ScriptToTreeNodeAdapter

227

```

228

229

## Advanced Features

230

231

### Compilation Phase Analysis

232

233

View AST at different compilation phases:

234

1. **PARSING** - Initial syntax tree from source

235

2. **CONVERSION** - Converted to Groovy AST nodes

236

3. **SEMANTIC_ANALYSIS** - Type checking and resolution

237

4. **CANONICALIZATION** - Simplified and optimized AST

238

5. **INSTRUCTION_SELECTION** - Bytecode instruction planning

239

6. **CLASS_GENERATION** - Final bytecode generation

240

241

### Bytecode Inspection

242

243

The AST browser can display:

244

- Generated Java bytecode

245

- Bytecode instruction mapping to source lines

246

- Class structure and metadata

247

- Method signatures and implementations

248

249

### Tree Navigation

250

251

Navigate the AST with:

252

- Expandable tree structure

253

- Property panels for node details

254

- Search and filter capabilities

255

- Export options for analysis results