I'll help you create a VS Code extension to preview ETRICE ROOM files with behavior and structure views. Here's a complete implementation:
Project Structure
etrice-room-viewer/
├── package.json
├── tsconfig.json
├── src/
│ ├── extension.ts
│ ├── roomParser.ts
│ ├── previewProvider.ts
│ ├── webview/
│ │ ├── main.js
│ │ └── style.css
│ └── types/
│ └── room.ts
└── README.md
1. package.json
{
"name": "etrice-room-viewer",
"displayName": "ETRICE ROOM Viewer",
"description": "Preview ETRICE ROOM files as behavior and structure diagrams",
"version": "0.1.0",
"publisher": "your-publisher-name",
"engines": {
"vscode": "^1.74.0"
},
"categories": ["Visualization", "Other"],
"keywords": ["etrice", "room", "modeling", "actor", "state-machine"],
"activationEvents": [],
"main": "./out/extension.js",
"contributes": {
"languages": [
{
"id": "room",
"aliases": ["ROOM", "room"],
"extensions": [".room"],
"configuration": "./language-configuration.json"
}
],
"commands": [
{
"command": "etrice.openPreview",
"title": "Open ROOM Preview",
"icon": "$(preview)"
},
{
"command": "etrice.showStructure",
"title": "Show Structure View",
"icon": "$(type-hierarchy)"
},
{
"command": "etrice.showBehavior",
"title": "Show Behavior View",
"icon": "$(git-branch)"
}
],
"menus": {
"editor/title": [
{
"command": "etrice.openPreview",
"when": "resourceExtname == .room",
"group": "navigation"
}
],
"explorer/context": [
{
"command": "etrice.openPreview",
"when": "resourceExtname == .room"
}
]
},
"configuration": {
"title": "ETRICE ROOM Viewer",
"properties": {
"etrice.defaultView": {
"type": "string",
"enum": ["structure", "behavior", "both"],
"default": "both",
"description": "Default view to show when opening ROOM files"
}
}
}
},
"scripts": {
"vscode:prepublish": "npm run compile",
"compile": "tsc -p ./",
"watch": "tsc -watch -p ./",
"pretest": "npm run compile && npm run lint",
"lint": "eslint src --ext ts"
},
"devDependencies": {
"@types/vscode": "^1.74.0",
"@types/node": "16.x",
"typescript": "^4.9.4",
"@typescript-eslint/eslint-plugin": "^5.45.0",
"@typescript-eslint/parser": "^5.45.0",
"eslint": "^8.28.0"
}
}
2. tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"target": "ES2020",
"outDir": "out",
"lib": ["ES2020"],
"sourceMap": true,
"rootDir": "src",
"strict": true
},
"exclude": ["node_modules", ".vscode-test"]
}
3. src/types/room.ts
export interface RoomModel {
modelName: string;
imports: string[];
actorClasses: ActorClass[];
protocolClasses: ProtocolClass[];
dataClasses: DataClass[];
}
export interface ActorClass {
name: string;
base?: string;
documentation?: string;
interfaces: Interface[];
structure?: Structure;
behavior?: Behavior;
}
export interface Interface {
name: string;
protocol: string;
conjugated: boolean;
multiplicity?: number;
}
export interface Structure {
actors: ActorRef[];
bindings: Binding[];
layerConnections: LayerConnection[];
serviceProvisionPoints: ServiceProvisionPoint[];
servicAccessPoints: ServiceAccessPoint[];
}
export interface ActorRef {
name: string;
type: string;
multiplicity?: number;
optional?: boolean;
}
export interface Binding {
endpoint1: string;
endpoint2: string;
}
export interface LayerConnection {
from: string;
to: string;
satisfied: string;
}
export interface ServiceProvisionPoint {
name: string;
protocol: string;
}
export interface ServiceAccessPoint {
name: string;
protocol: string;
}
export interface Behavior {
stateMachine: StateMachine;
}
export interface StateMachine {
states: State[];
transitions: Transition[];
initialState?: string;
}
export interface State {
name: string;
type: 'simple' | 'composite' | 'choice' | 'initial' | 'exit';
entry?: string;
exit?: string;
substates?: State[];
documentation?: string;
}
export interface Transition {
from: string;
to: string;
trigger?: Trigger;
guard?: string;
action?: string;
documentation?: string;
}
export interface Trigger {
msgFromIf: string;
message: string;
}
export interface ProtocolClass {
name: string;
documentation?: string;
incoming: Message[];
outgoing: Message[];
}
export interface Message {
name: string;
data?: DataType;
documentation?: string;
}
export interface DataType {
name: string;
type: string;
}
export interface DataClass {
name: string;
documentation?: string;
attributes: Attribute[];
operations: Operation[];
}
export interface Attribute {
name: string;
type: string;
defaultValue?: string;
}
export interface Operation {
name: string;
returnType: string;
parameters: Parameter[];
documentation?: string;
}
export interface Parameter {
name: string;
type: string;
}
4. src/roomParser.ts
import { RoomModel, ActorClass, ProtocolClass, DataClass, Structure, Behavior, StateMachine, State, Transition, Interface, ActorRef, Binding, Message, Trigger } from './types/room';
export class RoomParser {
private lines: string[] = [];
private currentLine = 0;
parse(content: string): RoomModel {
this.lines = content.split('\n').map(line => line.trim());
this.currentLine = 0;
const model: RoomModel = {
modelName: '',
imports: [],
actorClasses: [],
protocolClasses: [],
dataClasses: []
};
while (this.currentLine < this.lines.length) {
const line = this.getCurrentLine();
if (line.startsWith('RoomModel')) {
model.modelName = this.parseModelName(line);
} else if (line.startsWith('import')) {
model.imports.push(this.parseImport(line));
} else if (line.includes('ActorClass')) {
const actorClass = this.parseActorClass();
if (actorClass) {
model.actorClasses.push(actorClass);
}
} else if (line.includes('ProtocolClass')) {
const protocolClass = this.parseProtocolClass();
if (protocolClass) {
model.protocolClasses.push(protocolClass);
}
} else if (line.includes('DataClass')) {
const dataClass = this.parseDataClass();
if (dataClass) {
model.dataClasses.push(dataClass);
}
}
this.currentLine++;
}
return model;
}
private getCurrentLine(): string {
return this.currentLine < this.lines.length ? this.lines[this.currentLine] : '';
}
private parseModelName(line: string): string {
const match = line.match(/RoomModel\s+(\w+)/);
return match ? match[1] : '';
}
private parseImport(line: string): string {
const match = line.match(/import\s+(.+?)(?:\s+from\s+".+")?/);
return match ? match[1].replace(/"/g, '') : '';
}
private parseActorClass(): ActorClass | null {
const line = this.getCurrentLine();
const match = line.match(/ActorClass\s+(\w+)(?:\s+extends\s+(\w+))?/);
if (!match) return null;
const actorClass: ActorClass = {
name: match[1],
base: match[2],
interfaces: []
};
this.currentLine++; // Move past the ActorClass line
// Find the opening brace
while (this.currentLine < this.lines.length && !this.getCurrentLine().includes('{')) {
this.currentLine++;
}
let braceLevel = 0;
let inSection = '';
while (this.currentLine < this.lines.length) {
const currentLine = this.getCurrentLine();
// Count braces
braceLevel += (currentLine.match(/{/g) || []).length;
braceLevel -= (currentLine.match(/}/g) || []).length;
if (braceLevel === 0) break;
// Parse different sections
if (currentLine.includes('Interface')) {
const iface = this.parseInterface(currentLine);
if (iface) {
actorClass.interfaces.push(iface);
}
} else if (currentLine.includes('Structure')) {
inSection = 'structure';
actorClass.structure = this.parseStructure();
} else if (currentLine.includes('Behavior')) {
inSection = 'behavior';
actorClass.behavior = this.parseBehavior();
}
this.currentLine++;
}
return actorClass;
}
private parseInterface(line: string): Interface | null {
const match = line.match(/Interface\s+(\w+)\s*:\s*(\w+)(?:\s+(conjugated))?(?:\s*\[(\d+)\])?/);
if (!match) return null;
return {
name: match[1],
protocol: match[2],
conjugated: !!match[3],
multiplicity: match[4] ? parseInt(match[4]) : undefined
};
}
private parseStructure(): Structure {
const structure: Structure = {
actors: [],
bindings: [],
layerConnections: [],
serviceProvisionPoints: [],
servicAccessPoints: []
};
let braceLevel = 0;
this.currentLine++; // Move past Structure line
while (this.currentLine < this.lines.length) {
const line = this.getCurrentLine();
braceLevel += (line.match(/{/g) || []).length;
braceLevel -= (line.match(/}/g) || []).length;
if (braceLevel < 0) break;
if (line.includes('ActorRef')) {
const actorRef = this.parseActorRef(line);
if (actorRef) {
structure.actors.push(actorRef);
}
} else if (line.includes('Binding')) {
const binding = this.parseBinding(line);
if (binding) {
structure.bindings.push(binding);
}
}
this.currentLine++;
}
this.currentLine--; // Step back one line
return structure;
}
private parseActorRef(line: string): ActorRef | null {
const match = line.match(/ActorRef\s+(\w+)\s*:\s*(\w+)(?:\s*\[(\d+)\])?/);
if (!match) return null;
return {
name: match[1],
type: match[2],
multiplicity: match[3] ? parseInt(match[3]) : undefined
};
}
private parseBinding(line: string): Binding | null {
const match = line.match(/Binding\s+(.+?)\s+and\s+(.+)/);
if (!match) return null;
return {
endpoint1: match[1].trim(),
endpoint2: match[2].trim()
};
}
private parseBehavior(): Behavior {
const behavior: Behavior = {
stateMachine: {
states: [],
transitions: []
}
};
let braceLevel = 0;
this.currentLine++; // Move past Behavior line
while (this.currentLine < this.lines.length) {
const line = this.getCurrentLine();
braceLevel += (line.match(/{/g) || []).length;
braceLevel -= (line.match(/}/g) || []).length;
if (braceLevel < 0) break;
if (line.includes('StateMachine')) {
behavior.stateMachine = this.parseStateMachine();
}
this.currentLine++;
}
this.currentLine--; // Step back one line
return behavior;
}
private parseStateMachine(): StateMachine {
const stateMachine: StateMachine = {
states: [],
transitions: []
};
let braceLevel = 0;
this.currentLine++; // Move past StateMachine line
while (this.currentLine < this.lines.length) {
const line = this.getCurrentLine();
braceLevel += (line.match(/{/g) || []).length;
braceLevel -= (line.match(/}/g) || []).length;
if (braceLevel < 0) break;
if (line.includes('State')) {
const state = this.parseState(line);
if (state) {
stateMachine.states.push(state);
if (line.includes('initial')) {
stateMachine.initialState = state.name;
}
}
} else if (line.includes('Transition')) {
const transition = this.parseTransition(line);
if (transition) {
stateMachine.transitions.push(transition);
}
}
this.currentLine++;
}
this.currentLine--; // Step back one line
return stateMachine;
}
private parseState(line: string): State | null {
const match = line.match(/State\s+(\w+)/);
if (!match) return null;
const state: State = {
name: match[1],
type: 'simple'
};
if (line.includes('initial')) {
state.type = 'initial';
}
return state;
}
private parseTransition(line: string): Transition | null {
// Parse: Transition from_state -> to_state { triggers { trigger_info } }
const match = line.match(/Transition\s+(.+?)\s*->\s*(.+?)(?:\s*\{(.+?)\})?/);
if (!match) return null;
const transition: Transition = {
from: match[1].trim(),
to: match[2].trim()
};
if (match[3]) {
const triggerMatch = match[3].match(/triggers\s*\{\s*<(.+?)