Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to Expression Languages with Xtext

Introduction to Expression Languages with Xtext

Basics and patterns to create languages with expressions.
Slides from presentation at EclipseCon Europe 2017
https://www.eclipsecon.org/europe2017/session/introduction-expression-languages-xtext

Karsten Thoms

December 18, 2018
Tweet

More Decks by Karsten Thoms

Other Decks in Programming

Transcript

  1. EXPRESSIONS Additive + - Multiplicative * / % Boolean &&

    || Relational > < >= <= == != Unary !x Postfix x++ Atomic „Hello“
  2. base SHIPPED WITH XTEXT FULL FEATURED EXPRESSION LANGUAGE STRONG TYPED

    JAVA INTEGRATION EMBEDDABLE PRODUCTION READY
  3. LEFT RECURSION A rule is left-recursive when the first symbol

    is non-terminal and refers to the rule itself Expression : Expression '+' Expression | '(' Expression ')' | Number; Number : value = INT; 2 + 3
  4. !21 Addition returns Expression: Number ({Sum.left = current} '+' right=Number)*;

    Number: value = INT; 1 2 + 3 Sum 2 3 1 EXPRESSIONS Example: Example: ! left right
  5. PRECEDENCE ExprPrioN: ExprPrioM =>({ExprPrioN.someRef = current} ..... )*; ExprPrioM: ExprPrioN

    =>({ExprPrioM.someRef = current} ..... )*; ExprPrioO: Atomic =>({ExprPrioO.someRef = current} ..... )*; Atomic: {Literal} value=<TERMINAL>; PriorityN PriorityM Atomic < < PriorityO < Addition Multiplication Literal < < Exponentiation <
  6. SYNTACTIC PREDICATES • Expressed by => or -> in front

    of a grammar element • Give the parser hint to follow certain paths in case of ambiguity
 
 „if you see these tokens, then follow this path“ • Recommended instead of using backtracking
  7. DANGLING ELSE To which if element does the else part

    belong? if (condition1) if (condition2) x = 1 else x = 2 if (condition1) if (condition2) x = 1 else x = 2
  8. DANGLING ELSE IfStatement returns Expression: 'if' '(' condition=BoolExpression ')' thenPart=Expression

    (=>'else' elsePart=Expression)? ; if if expr expr expr condition thenPart condition elsePart
  9. IMPLEMENTING A TYPE SYSTEM 1.What is the Actual Type of

    an expression ?
 2.What is the Expected Type of an expression ?
 3.Is some type conformant with another type ?
  10. IMPLEMENTING A TYPE SYSTEM 1.What is the Actual Type of

    an expression ?
 2.What is the Expected Type of an expression ?
 3.Is some type conformant with another type ? T getActualType (Expression expr) T getExpectedType (Expression expr) boolean isConformant (T expected, T actual)
  11. TYPE VALIDATION class TypeValidator extends AbstractMyDSLValidator { @Inject ITypeComputer typeComputer

    @Check def void checkType(Expression expr) { val actualType = typeComputer.getActualType(expr) val expectedType = typeComputer.getExpectedType(expr) if (expectedType === null || actualType === null) return; // nothing to check if (!typeComputer.isConformant(expectedType, actualType)) { error("Incompatible types. Expected '" + expectedType.name 
 + "' but was '" + actualType.name + "'" , null, IssueCodes.INCOMPATIBLE_TYPES); } } } class MyDSLRuntimeModule extends AbstractMyDSLRuntimeModule { ... @SingletonBinding(eager=true) def bindTypeValidator () { TypeValidator } }
  12. number x; string s; s = x + 1; Actual:

    NUMBER Expected: STRING
  13. EXPECTED TYPE • Depends on context • Variable initialisation: type

    of the declared variable • Assignment: right-hand side must be of type of left-hand side • If condition: boolean • Argument of a method call: type of the corresponding parameter of the method