Show Menu

Rascal Cheat Sheet by

This is the cheatsheat for a Rascal workshop
development     programming     metaprogramming     rascal

Default datatypes

String
str
Boolean
bool
Array
list[­T]
Location
loc
Map / set
map[T, T]
Integer
int
Set (unique array)
set[T]
T = type. For example: list[str] for a list of strings

Imports

String
import String
Boolean
import Boolean
List
import List
Location
import Location
Map
import Map
Integer
import util::Math
Set
import Set
M3 AST
import analys­is:­:m3­::AST
IO
import IO

Read java project

createAstsFromDirectory(loc project, true);
This method is located in the M3 AST library
return: set[De­cla­ration]

Debugging

Print to console (one line)
printl­n(v­alue)
Print to console (forma­tted)
iprint­ln(­value)
This methods are located in the IO library

Location

File
|file:­///­hom­e/r­asc­al/­ras­cal.rsc|
Project
|proje­ct:­//a­ndr­oid­-pr­oject/|
URL
|http:­//w­ww.g­oo­gle.nl|
Folder
|file:­///­hom­e/r­ascal/|
 

Data manipu­lation 1

String - append
str : "­str­" + "­str­"
String - compare
bool : "str == "­str­"
String - interp­olation
"­value: <st­r>"
List - assignment
list[int] : [3,2,1];
List - append
list[int] : [3,2] + 1;
Set - assignment
set[int]: {3,2,1}
Set - append
set[int]: {3,2} + 1;
All values are immutable

Data manipu­lation 2

// data Game = game(list[Player] players);
// Game game = getGame();

// Access players:
game.players

// Overwrite players:
game.players = []

// Add player:
game.players = game.players + player

If else

if("str" == "str") {
    iprintln("true");
} else {
    iprintln("false");
}

For loop (array)

list[int] numbers = [1, 2, 3, 4, 5];

for (number <- numbers) {
    iprintln(number);
}

For loop (map)

map[str, int] numbers = ["one" : 1, "two" : 2,
"three" : 3, "four" : 4, "five" : 5];

for (numberText <- numbers) {
    int numberValue = numbers[numberText];
    iprint­ln(­num­berValue);
}

File manipu­lation

Read file
readFi­le(­loc­ation)
Write file
writeF­ile­(lo­cation, value)

Method definition

public str getString() {
    return "String";
}
The method test() is reserved
 

Visit pattern - Matches

wildcard
_
Value check
"­exact value"
Assignment
method
Block assignment
block:

Visit pattern - Example 1

visit (ast) {
    case \import(importName): {
        iprintln(importName);
    }
}

Visit pattern - Example 2

visit (ast) {
    case \impor­t(_): {
        iprint­ln(­"­Found an import­");
    }
}

Visit pattern - Example 3

visit (ast) {
    case \impor­t("I­mpo­rte­dCl­ass­"): {
        iprint­ln(­"­found Import­edC­las­s");
    }
}

Visit pattern - Example 4

visit (ast) {
    case \field(simpleType(simpleName(name)), _): {
        iprint­ln(­"­found a field: <name>");
    }
}

Syntax keywords

layout
The layout of the syntax. Is the syntax separated by spaces or new lines.
lexical
Definition for a block of the input, which is separated by the layout.
start syntax
The global syntax definition of the input
syntax
The global syntax can be separated into smaller pieces, which can be defined with a syntax.

Download the Rascal Cheat Sheet

2 Pages
//media.cheatography.com/storage/thumb/rubene_rascal.750.jpg

PDF (recommended)

Alternative Downloads

Share This Cheat Sheet!

 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Regular Expressions Cheat Sheet
          PHP Cheat Sheet