Navigation
Synopsis Concrete patterns.
Syntax
` Token1 Token2 ... Tokenn `
(Symbol) ` Token1 Token2 ... Tokenn `
<Type Var>
<Var>
Description Suppose we want to manipulate text written in some hypothetical language LANG. Then first the concrete syntax of LANG has to be defined by importing a module that declares the non-terminals and syntax rules for LANG. Next LANG programs have to be parsed. LANG programs made come from text files or they may be included in the Rascal program as literals. In both cases the text is parsed according to the defined syntax and the result is a parse tree in the form of a value of type Tree. Concrete patterns operate on these trees.

A concrete pattern is a quoted concrete syntax fragment that may contain variables. The syntax that is used to parse the concrete pattern may come from any module that has been imported in the module in which the concrete pattern occurs.

A concrete pattern may have the following forms:
  • Quoted pattern
    ` Token1 Token2 ... Tokenn `
    
    Inside a quoted pattern arbitrary lexical tokens may occur. Quoted patterns may contain variable declaration patterns and variable patterns (see below).
  • A typed quoted pattern
    (Symbol) ` Token1 Token2 ... Tokenn `
    
    is a quoted pattern that is preceded by a nonterminal symbol to define its desired syntactic type.
  • A typed variable pattern
    <Type Var>
    
  • A variable pattern
    <Var>
    
Inside concrete syntax patterns, layout is ignored.
Examples Examples (in a context where an appropriate concrete syntax has been defined):
  • Quoted syntax pattern with two pattern variable declarations:
    ` while <EXP Exp> do <{STATEMENT ";"}* Stats> od `
    
    Two observations can be made about this example:
    • The non-terminals EXP and {STATEMENT ";"}* are declared in the imported module and can be used as types in the Rascal program.
    • When this pattern is matched successfully against a subject, the variables Exp and Stats will be bound.
  • Quoted syntax pattern with two pattern variable uses (Exp and Stats should already have a value):
    ` while <Exp> do <Stats> od `
    
  • Identical to the previous example, but with a declaration of the desired syntactic type:
    STATEMENT ` while <Exp> do <Stats> od `
    
A full example of concrete patterns can be found in Recipes:Languages/Exp/Concrete/WithLayout.
Is this page unclear, or have you spotted an error? Please add a comment below and help us to improve it. For all other questions and remarks, visit ask.rascal-mpl.org.