interactive disambiguation of meta programs with concrete
play

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


  1. Interactive Disambiguation of Meta Programs with Concrete Object Syntax Lennart Kats (TUDelft) (KolibriFX) Karl T. Kalleberg (TUDelft) Eelco Visser

  2. Meta-programming

  3. Meta-programming with Template Engines <<FOREACH ... AS class>> <<FILE "samples/petclinic/" + class.name + ".java">> package samples.petclinic; publc class <<class.name>> { Easy to read, private long id; public void setId(long id) { Easy to write this.id = id; } public long getId() { return id; Works for any } } language! <<ENDFILE>> <<ENDFOREACH>> Works for any Only code Lack of compositionality lanugage! generation

  4. Meta-programming with Abstract Syntax for (Output c : ...) { Always well- CompilationUnit u = newCompilationUnit( formed! "samples/petclinic/" + c.name + ".java"); u.setPackageName("samples.petclinic"); ClassDec cd = u.newClassDec(c.name); cd.newField(PRIVATE, long.class, "id"); Further MethodDec m = cd.newMethod(PUBLIC, void.class, "setId"); transformation m.newParameter(long.class, "id"); m.newStatement(newAssign(newFieldAccess(THIS, "id"), still possible newVariableAccess("id"))); m = cd.newMethod(PUBLIC, long.class, "getId"); m.newStatement(newReturn(newVariableAccess("id"))); ... Hard to read, } Hard to write Highly verbose

  5. Meta-programming with Concrete Object Syntax [Visser, GPCE’02] 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) } ]| ... } Always well- formed!

  6. Meta-programming with Concrete Object Syntax [Visser, GPCE’02] Any meta language (here: Java) for (Output c : ...) { CompilationUnit u = |[ package samples.petclinic; Easy to read, public class $[c.name] { Easy to write(?) private long id; public void setId(long id) { this.id = id; } for (Output c : ...) { public long getId() { CompilationUnit u = Further return id; newCompilationUnit( Transform } "samples/petclinic/" + c.name + transformation Any object language (here: Java) } ".java"); ]| u.setPackageName("samples.petclinic"); still possible ... 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")));

  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

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

  9. Problem: Ambiguity Which is it? |[ class X { } ]| exports context-free syntax "|[" ClassDec "]|" -> Term {cons("ToMetaExpr")} "|[" CompUnit "]|" -> Term {cons("ToMetaExpr")} "|[" BlockStm "]|" -> Term {cons("ToMetaExpr")}

  10. Problem: Ambiguity Which is it? class X { Just another class? } class Y { } exports context-free syntax "|[" ClassDec "]|" -> Term {cons("ToMetaExpr")} "|[" CompUnit "]|" -> Term {cons("ToMetaExpr")} "|[" BlockStm "]|" -> Term {cons("ToMetaExpr")}

  11. Problem: Ambiguity Which is it? class X { Just another class? } A compilation unit? exports context-free syntax "|[" ClassDec "]|" -> Term {cons("ToMetaExpr")} "|[" CompUnit "]|" -> Term {cons("ToMetaExpr")} "|[" BlockStm "]|" -> Term {cons("ToMetaExpr")}

  12. Problem: Ambiguity Which is it? void foo () { class X { 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")}

  13. Problem: Ambiguity Which is it? |[ class X { 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")}

  14. Disambiguation

  15. Tag-based Disambiguation "ClassDec" "|[" ClassDec "]|" -> Term {cons("ToMetaExpr")} "CompUnit" "|[" CompUnit "]|" -> Term {cons("ToMetaExpr")} "BlockStm" "|[" BlockStm "]|" -> Term {cons("ToMetaExpr")} Simple, generic CompUnit |[ class Foo {} ]| Verbose Need to know the tag names

  16. Type-based Disambiguation [Bravenboer et al. ’05, Vinju ’05] More concise! Need to know the type names CompUnit c = |[ class Foo {} ]|; fooMethod(|[ class Foo {} ]|); Requires special type checker Often relies on heuristics

  17. Interactive Disambiguation Addresses discovery issue Complementary approach

  18. 1. User writes meta program ,

  19. Parsing with Ambiguity , "|[" ConstantDec "]|" -> Term {cons("ToMetaExpr")} "|[" LocalVarDecStm "]|" -> Term {cons("ToMetaExpr")} "|[" FieldDec "]|" -> Term {cons("ToMetaExpr")} Parse forest from generalized parser (SGLR): amb ToMetaExpr ToMetaExpr ToMetaExpr Constant LocalVar Constant LocalVar FieldDec FieldDec Dec DecStm Dec DecStm

  20. 2. IDE detects ambiguity

  21. 3. User selects intention

  22. 4. IDE explicitly disambiguates meta program Simple, generic Addresses name discovery No heuristics

  23. Mixin Grammar for Interactive Disambiguation Discovery "|[" 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")} Disambiguation

  24. Perspective Here, applied with tag-based disambiguation Also useful with type-based disambiguation • Get rid of heuristics • Different transformations needed

  25. Perspective Link to projectional editing

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

Recommend


More recommend