Interactive Disambiguation of Meta Programs with Concrete Object - - PowerPoint PPT Presentation

interactive disambiguation of meta programs with concrete
SMART_READER_LITE
LIVE PREVIEW

Interactive Disambiguation of Meta Programs with Concrete Object - - PowerPoint PPT Presentation

Interactive Disambiguation of Meta Programs with Concrete Object Syntax Lennart Kats (TUDelft) (KolibriFX) Karl T. Kalleberg (TUDelft) Eelco Visser Meta-programming Meta-programming with Template Engines <<FOREACH ... AS


slide-1
SLIDE 1

Interactive Disambiguation of Meta Programs with Concrete Object Syntax

Lennart Kats Karl T. Kalleberg Eelco Visser (TUDelft) (KolibriFX) (TUDelft)

slide-2
SLIDE 2

Meta-programming

slide-3
SLIDE 3

Meta-programming with Template Engines

<<FOREACH ... AS class>> <<FILE "samples/petclinic/" + class.name + ".java">> package samples.petclinic; publc class <<class.name>> { private long id; public void setId(long id) { this.id = id; } public long getId() { return id; } } <<ENDFILE>> <<ENDFOREACH>>

Works for any language! Easy to read, Easy to write

Lack of compositionality

Works for any lanugage!

Only code generation

slide-4
SLIDE 4

for (Output c : ...) { CompilationUnit u = newCompilationUnit( "samples/petclinic/" + c.name + ".java"); u.setPackageName("samples.petclinic"); ClassDec cd = u.newClassDec(c.name); cd.newField(PRIVATE, long.class, "id"); MethodDec m = cd.newMethod(PUBLIC, void.class, "setId"); m.newParameter(long.class, "id"); m.newStatement(newAssign(newFieldAccess(THIS, "id"), newVariableAccess("id"))); m = cd.newMethod(PUBLIC, long.class, "getId"); m.newStatement(newReturn(newVariableAccess("id"))); ... }

Hard to read, Hard to write

Always well- formed! Further transformation still possible

Meta-programming with Abstract Syntax

Highly verbose

slide-5
SLIDE 5

Any meta language (here: Java)

for (Output c : ...) { CompilationUnit u = |[ package samples.petclinic; publc class $[c.name] { private long id; public void setId(long id) { this.id = id; } public long getId() { return id; } } ]| ... }

Any object language (here: Java)

Meta-programming with Concrete Object Syntax

[Visser, GPCE’02]

Always well- formed!

slide-6
SLIDE 6

Any meta language (here: Java)

for (Output c : ...) { CompilationUnit u = |[ package samples.petclinic; public class $[c.name] { private long id; public void setId(long id) { this.id = id; } public long getId() { return id; } } ]| ... }

Any object language (here: Java)

for (Output c : ...) { CompilationUnit u = newCompilationUnit( "samples/petclinic/" + c.name + ".java"); u.setPackageName("samples.petclinic"); ClassDec cd = u.newClassDec(c.name); cd.newField(PRIVATE, long.class, "id"); MethodDec m = cd.newMethod(PUBLIC, void.class, "setId"); m.newParameter(long.class, "id"); m.newStatement(newAssign(newFieldAccess (THIS, "id"), newVariableAccess("id")));

Transform

Meta-programming with Concrete Object Syntax

[Visser, GPCE’02]

Further transformation still possible Easy to read, Easy to write(?)

slide-7
SLIDE 7

Prerequisites

Parser for meta language + object language created using grammar composition:

  • meta language grammar
  • object language grammar
  • mixin grammar for quotations and anti-

quotations

slide-8
SLIDE 8

Example SDF Mixin Grammar

module Stratego-Java imports Stratego Java exports context-free syntax "|[" ClassDec "]|" -> Term "|[" CompUnit "]|" -> Term "|[" BlockStm "]|" -> Term "$[" Term "]" -> ClassDec "$[" Term "]" -> BlockStm "$[" Term "]" -> CompUnit {cons("ToMetaExpr")} {cons("ToMetaExpr")} {cons("ToMetaExpr")} {cons("FromMetaExpr")} {cons("FromMetaExpr")} {cons("FromMetaExpr")}

slide-9
SLIDE 9

Problem: Ambiguity

|[ class X { } ]|

Which is it?

exports context-free syntax "|[" ClassDec "]|" -> Term {cons("ToMetaExpr")} "|[" CompUnit "]|" -> Term {cons("ToMetaExpr")} "|[" BlockStm "]|" -> Term {cons("ToMetaExpr")}

slide-10
SLIDE 10

Problem: Ambiguity

class X { } class Y { }

Which is it? Just another class?

exports context-free syntax "|[" ClassDec "]|" -> Term {cons("ToMetaExpr")} "|[" CompUnit "]|" -> Term {cons("ToMetaExpr")} "|[" BlockStm "]|" -> Term {cons("ToMetaExpr")}

slide-11
SLIDE 11

Problem: Ambiguity

class X { }

Which is it? Just another class? A compilation unit?

exports context-free syntax "|[" ClassDec "]|" -> Term {cons("ToMetaExpr")} "|[" CompUnit "]|" -> Term {cons("ToMetaExpr")} "|[" BlockStm "]|" -> Term {cons("ToMetaExpr")}

slide-12
SLIDE 12

Problem: Ambiguity

void foo () { class X { } }

Which is it? Just another class? A compilation unit? Or a class declaration statement?

exports context-free syntax "|[" ClassDec "]|" -> Term {cons("ToMetaExpr")} "|[" CompUnit "]|" -> Term {cons("ToMetaExpr")} "|[" BlockStm "]|" -> Term {cons("ToMetaExpr")}

slide-13
SLIDE 13

Problem: Ambiguity

Which is it? Just another class? A compilation unit? Or a class declaration statement?

exports context-free syntax "|[" ClassDec "]|" -> Term {cons("ToMetaExpr")} "|[" CompUnit "]|" -> Term {cons("ToMetaExpr")} "|[" BlockStm "]|" -> Term {cons("ToMetaExpr")} |[ class X { } ]|

slide-14
SLIDE 14

Disambiguation

slide-15
SLIDE 15

"|[" ClassDec "]|" -> Term {cons("ToMetaExpr")} "|[" CompUnit "]|" -> Term {cons("ToMetaExpr")} "|[" BlockStm "]|" -> Term {cons("ToMetaExpr")} "ClassDec" "CompUnit" "BlockStm"

Verbose

Simple, generic

Need to know the tag names

CompUnit |[ class Foo {} ]|

Tag-based Disambiguation

slide-16
SLIDE 16

Type-based Disambiguation

CompUnit c = |[ class Foo {} ]|;

More concise!

Need to know the type names Often relies on heuristics Requires special type checker

fooMethod(|[ class Foo {} ]|); [Bravenboer et al. ’05, Vinju ’05]

slide-17
SLIDE 17

Interactive Disambiguation

Addresses discovery issue Complementary approach

slide-18
SLIDE 18
  • 1. User writes meta program

,

slide-19
SLIDE 19

"|[" ConstantDec "]|" -> Term {cons("ToMetaExpr")} "|[" LocalVarDecStm "]|" -> Term {cons("ToMetaExpr")} "|[" FieldDec "]|" -> Term {cons("ToMetaExpr")}

Parsing with Ambiguity

Constant Dec LocalVar DecStm FieldDec ToMetaExpr ToMetaExpr ToMetaExpr amb Constant Dec LocalVar DecStm FieldDec

Parse forest from generalized parser (SGLR):

,

slide-20
SLIDE 20
  • 2. IDE detects ambiguity
slide-21
SLIDE 21
  • 3. User selects intention
slide-22
SLIDE 22
  • 4. IDE explicitly disambiguates

meta program

Simple, generic No heuristics Addresses name discovery

slide-23
SLIDE 23

Mixin Grammar for Interactive Disambiguation

"|[" ClassDec "]|" -> Term {cons("ToMetaExpr")} "|[" CompUnit "]|" -> Term {cons("ToMetaExpr")} "|[" BlockStm "]|" -> Term {cons("ToMetaExpr")} "ClassDec" "|[" ClassDec "]|" -> Term {cons("ToMetaExpr")} "CompUnit" "|[" CompUnit "]|" -> Term {cons("ToMetaExpr")} "BlockStm" "|[" BlockStm "]|" -> Term {cons("ToMetaExpr")}

Discovery Disambiguation

slide-24
SLIDE 24

Perspective

Here, applied with tag-based disambiguation Also useful with type-based disambiguation

  • Get rid of heuristics
  • Different transformations needed
slide-25
SLIDE 25

Perspective

Link to projectional editing

slide-26
SLIDE 26

Conclusion

  • Leverage IDE
  • Useful for meta programs. Programs also?
  • Addresses discovery issue
  • Avoids heuristics
  • www.spoofax.org