RenderStmt.java 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package templates;
  7. import common.RegistroBase;
  8. import exception.CompileException;
  9. import org.antlr.v4.runtime.ANTLRInputStream;
  10. import org.antlr.v4.runtime.CommonTokenStream;
  11. import org.antlr.v4.runtime.TokenStream;
  12. import org.antlr.v4.runtime.tree.ParseTreeWalker;
  13. /**
  14. *
  15. * @author EUGENIO CARVALHO
  16. */
  17. public class RenderStmt {
  18. protected ParseTreeWalker walker;
  19. protected TemplateParser.InitContext tree;
  20. public final String ID;
  21. private final String Format;
  22. RenderStmt(String id, String format) {
  23. ID = id;
  24. Format = format;
  25. ANTLRInputStream input = new ANTLRInputStream(Format);
  26. TemplateLexer lexer = new TemplateLexer(input);
  27. lexer.removeErrorListeners();
  28. CommonTokenStream tokens = new CommonTokenStream(lexer);
  29. TemplateParser parser = new TemplateParser((TokenStream) tokens);
  30. parser.removeErrorListeners();
  31. tree = parser.init();
  32. walker = new ParseTreeWalker();
  33. }
  34. public String Render(RegistroBase ctx) throws CompileException {
  35. // System.out.println("ctx:" + ctx);
  36. // throw new Exception("assssssssssss");
  37. TListener listener = new TListener(ctx);
  38. walker.walk(listener, tree);
  39. if (listener.hasError()) {
  40. // System.out.println("ctx:" + ctx);
  41. throw new CompileException(
  42. "Render: %s\nFormat: %s)\nError: %s",
  43. ID,
  44. Format,
  45. // ctx.Get("block.position"),
  46. listener.GetError()
  47. );
  48. }
  49. return listener.toString();
  50. }
  51. }