SWRTJ Eclipse Programmer's Guide

  1. Getting Started
    1. How to create your first Swrtj project
    2. How to create your first Swrtj file
    3. How to generate java files
  2. Concepts
    1. Swrtj Project
    2. SWRTJ Language
      1. Interfaces
      2. Records
      3. Traits
      4. Classes
      5. Programs
      6. Imports
    3. Swrtj User Interface
      1. Syntax Highlighting
      2. Code completion
      3. Outline view
      4. Validation
      5. Code generation


Getting Started

How to create your first Swrtj project

First of all open your Eclipse IDE and make you sure that the Swrtj plugin is present.
The plugin makes available two wizards. If you choose from the 'file' menu, New > Other you can open the wizard list.
Now choose Swrtj > Swrtj Project and click on the next button.

A new page will appear. Type your project name and then click on the 'Finish' button and a new Swrtj project will be created. Now open the new folder created on the package explorer and you can start to edit your files.
The project is created with two example files.

How to create your first Swrtj file

The Swrtj plugin makes available a file wizard that creates a new swrtj file with an example included.
After creating a new swrtj project, that we call 'MySwrtjProject', you can open the file wizard from the 'file' menu choosing New > Other and then Swrtj/Swrtj file.
Click on the 'Next' button and then insert the container name(i.e. the folder in which you want to create the file) and the file name.

Please insert your files in the 'src' folder of your project if you want to generate java files from it.

How to generate java files

With the Swrtj plugin you can also generate java files.
When you have created a new project you can choose a file and generate it. Right click on the selected file to open the pop-up menu and then choose Generate > Java Files.

After generating the files, a message will notify the outcome of the operation.
The generated java files will appear under the 'src-gen' directory.

Concepts

Swrtj Project

An Swrtj project is a project with the java and xtext nature, due to the need to run java files.

The Swrtj project structure is the following:

SWRTJ Language

Interfaces

The interface syntax and use is equivalent to java one but without modifier (like public, private, etc.).
Every interface implicitly extends the 'IObject' interface. Every interface is subtype of IObject.
Here's an example


interface IAccount {
  void update(int price);
}

Multiple inheritance is allowed too.


interface IPostalAccount extends IAccount, IIDentificableAccount {
  void update(int price);
  int getAccountID();
}

Records

A record is a set of fields. Here's an example of basic record declaration.


record RAccount is {
  int money;
}

Records can be composed by other records by using one or more of the following record operations.

Record sum (commutative and associative)

Creates a record that contains all the fields of the sum record elements(two or more).
If there is a field conflict(fields with the same name in different records) please use the exclude or rename operation.


record RSyncAccount is RAccount + RSynchronizer

Record exclusion

Excludes a field from a record.


record RWindow is RDecoredWindow[exclude windowDecoration]

Record renaming

Renames a field of a record. The new name must not exist in the target record.


record RErrorMessage is RMessage[description renameTo errorDescription]

Traits

A trait is a set of methods. Here's an example of basic trait declaration.


record TAccount is {
  int money;

  void commit();
  void update(int money) {
    ...
  }
}

The field money is just an indicator that the class that will use this trait has to provide this field. At the same way the method declaration of commit just indicates that class has to provide such method.
The method update contains a method implementation that is provided to every class that use this trait.

Traits can be composed by other traits by using one or more of the following trait operations.

Trait sum (commutative and associative)

Creates a trait that contains all the methods of the sum trait elements(two or more).
If there is a method conflict(method with the same name in different traits) please use the exclude or rename operation.


trait TSyncAccount is TAccount + TSynchronizer

Trait exclusion

Excludes a method (required or provided) from a trait.


trait TWindow is TDecoredWindow[exclude setDecoration]

Trait aliasing

Makes the aliasing of a provided method (creating the method copy).
The original method is not cancelled.


trait TErrorMessage is TMessage[getDescription aliasAs getErrorDescription]

Trait method renaming

Renames a method of a trait. The new name must not exist in the target trait.


record TErrorMessage is TMessage[getDescription renameTo getErrorDescription]

Trait field renaming

Renames a required field of a trait.


trait TErrorMessage is TMessage[description renameFieldTo errorDescription]

Classes

Classes contain only constructors and they must to implements interfaces using traits and fields given by records.
Here the inheritance is not applicable and classes are not types.
Follows a class definition example.


class CAccount implements IAccount by RAccount and TAccount {
  CAccount() {
    this.init();
  }
}

You can also use record and trait expressions.
Follows an example.


class CSyncAccount implements IAccount, ISyncAccount by RSyncAccount and
      TAccount + {...} + TSynchronized {
  CSyncAccount() {
    this.init();
  }
}

Programs

The program construct is used to create a runnable file. It corresponds to the java Main class, i.e., a class that contains the 'main' method.
Note that you can create more programs within a single file.
Here's the 'Hello world' example.


program Main {
  out.println("Hello World");
}

Swrtj language makes available three system constants for the input output.

Imports

The import instruction in a file, imports an swrtj file (that can contain one more elements like classes, interfaces, etc.) into the file so that every element of the imported file can be used within the importing file.
For instance, suppose to have a file 'Account.swrtj' that provides the interface IAccount and the class CAccount, you can use it within a file 'Main.swrtj'.


import "Account.swrtj"

program Main {
  IAccount myAccount = new CAccount();
  ...
}

When you import a file it will be added to files to generate. In the example above if you generate 'Main.swrtj' even 'Account.swrtj' will be generated into a different java package.

Swrtj User Interface

Syntax Highlighting

The syntax highlighting provides an help for the keywords that are highlighted in order to makes them more visible.
Furthermore, fields are in italic style while methods are in normal style.

Code completion

The code completion provides an help using a drop-down window that suggests all the code elements that can be used in a specific context.
For instance when you type 'this.' a window automatically appears and shows all the field an methods invokable on the 'this' parameter.

You can open the proposal window in every par of the code editor with CTRL + SPACE and to press ENTER to choose an element.

The code completion window automatically appears every time that the '.' or '+' character is typed.

Outline view

The outline is a view (located at the right of the editor) that shows your swrtj file. The structure shown is a tree where the root is the file that has interfaces, classes, records, traits, programs and imports as children.
Here's an example.

The red circle indicates a required field (like stream in TSyncWriter2), the green one indicates a provided field (like in classes).
Trait TSyncWriter2 contains the unsyncWrite method with the red icon and the write method with the blue icon because the first one is a request (i.e., the trait TSyncWriter2 has not the implementation of this method) while the second one is a provided method.

Validation

The user interface performs the code validation when you type something in the editor.
The validation includes:

The picture below shows the validation errors in the editor and in the bottom window.

Code generation

The code generation involves the creation of the java files equivalent to the Swrtj ones.
If you try to compile an swrtj file 'File.swrtj' in the 'src' folder, every element in the file will be translated to a java class or interface and it will be created within the 'src-gen' folder in the 'packages.file' package. Since the package name is taken from the file name please use just characters and/or letters in an swrtj file name if you want to generate it.

Every program element (see Programs) is translated as a java executable file so that, if you right click on it and choose Run As > Java Application you can run it.