@Test
 public void testSizeZeroOnLineWithMultipleExpr() throws Exception {
   ST t = new ST("begin\n" + "  <name>\n" + "	<users><users>\n" + "end\n");
   String expecting = "begin" + newline + "end" + newline;
   String result = t.render();
   assertEquals(expecting, result);
 }
Example #2
0
  @Override
  public void render(PetriNet pn, Writer writer) throws RenderException, IOException {
    verifyNet(pn);

    STGroup group = new STGroupFile("uniol/apt/io/renderer/impl/BagginsPN.stg", '$', '$');
    ST pnTemplate = group.getInstanceOf("pn");
    pnTemplate.add("name", pn.getName());

    // Handle places
    pnTemplate.add("places", pn.getPlaces());

    List<Place> placeList = new ArrayList<>(pn.getPlaces());

    // Handle transitions (and arcs)
    for (Transition t : pn.getTransitions()) {
      List<IdWeightPair> preset = new ArrayList<>();
      List<IdWeightPair> postset = new ArrayList<>();
      for (Flow e : t.getPresetEdges()) {
        preset.add(new IdWeightPair(placeList.indexOf(e.getSource()), -e.getWeight()));
      }
      for (Flow e : t.getPostsetEdges()) {
        postset.add(new IdWeightPair(placeList.indexOf(e.getTarget()), e.getWeight()));
      }

      pnTemplate.addAggr("transitions.{transition, preset, postset}", t, preset, postset);
    }

    pnTemplate.write(new AutoIndentWriter(writer), new ThrowingErrorListener());
  }
 @Test
 public void testEmptyLine() throws Exception {
   ST t = new ST("begin\n" + "\n" + "end\n");
   String expecting = "begin" + newline + newline + "end" + newline;
   String result = t.render();
   assertEquals(expecting, result);
 }
 @Test
 public void testTemplatesInDictionary() throws Exception {
   String templates =
       "auxMap ::= [\n"
           + "   \"E\": {electric <field>},\n"
           + "   \"I\": {in <field> between},\n"
           + "   \"F\": {<field> force},\n"
           + "   default: {<field>}\n"
           + "]\n"
           + "\n"
           + "makeTmpl(type, field) ::= <<\n"
           + "<auxMap.(type)>\n"
           + ">>\n"
           + "\n"
           + "top() ::= <<\n"
           + "  <makeTmpl(\"E\", \"foo\")>\n"
           + "  <makeTmpl(\"F\", \"foo\")>\n"
           + "  <makeTmpl(\"I\", \"foo\")>\n"
           + ">>\n";
   writeFile(tmpdir, "t.stg", templates);
   STGroup group = new STGroupFile(tmpdir + File.separatorChar + "t.stg");
   ST st = group.getInstanceOf("top");
   Assert.assertNotNull(st);
   String expecting = "  electric foo" + newline + "  foo force" + newline + "  in foo between";
   Assert.assertEquals(expecting, st.render());
 }
 @Test
 public void testSizeZeroOnLineByItselfGetsNoOutput() throws Exception {
   ST t = new ST("begin\n" + "<name>\n" + "<users>\n" + "<users>\n" + "end\n");
   String expecting = "begin\nend\n";
   String result = t.render();
   assertEquals(expecting, result);
 }
 @Test
 public void testIFElseExprOnSingleLine() throws Exception {
   ST t = new ST("begin\n" + "<if(users)><else><endif>\n" + "end\n");
   String expecting = "begin" + newline + "end" + newline;
   String result = t.render();
   assertEquals(expecting, result);
 }
Example #7
0
  @Override
  public Handle lexerCallCommand(GrammarAST ID, GrammarAST arg) {
    LexerAction lexerAction = createLexerAction(ID, arg);
    if (lexerAction != null) {
      return action(ID, lexerAction);
    }

    // fall back to standard action generation for the command
    ST cmdST =
        codegenTemplates.getInstanceOf("Lexer" + CharSupport.capitalize(ID.getText()) + "Command");
    if (cmdST == null) {
      g.tool.errMgr.grammarError(
          ErrorType.INVALID_LEXER_COMMAND, g.fileName, ID.token, ID.getText());
      return epsilon(ID);
    }

    if (cmdST.impl.formalArguments == null || !cmdST.impl.formalArguments.containsKey("arg")) {
      g.tool.errMgr.grammarError(
          ErrorType.UNWANTED_LEXER_COMMAND_ARGUMENT, g.fileName, ID.token, ID.getText());
      return epsilon(ID);
    }

    cmdST.add("arg", arg.getText());
    cmdST.add("grammar", arg.g);
    return action(cmdST.render());
  }
 @Override
 public ST codegen() {
   ST expr = CodeGen.expr();
   String castExpr = "(" + type.getSimpleName() + ")" + codeStr(arg);
   expr.add("expr", castExpr);
   return expr;
 }
 @Test
 public void testCommentOnlyLineGivesNoOutput2() throws Exception {
   ST t = new org.stringtemplate.v4.ST("begin\n" + "    <! ignore !>\n" + "end\n");
   String expecting = "begin" + newline + "end" + newline;
   String result = t.render();
   assertEquals(expecting, result);
 }
Example #10
0
  public void testActions(String templates, String actionName, String action, String expected)
      throws org.antlr.runtime.RecognitionException {
    int lp = templates.indexOf('(');
    String name = templates.substring(0, lp);
    STGroup group = new STGroupString(templates);
    ST st = group.getInstanceOf(name);
    st.add(actionName, action);
    String grammar = st.render();
    ErrorQueue equeue = new ErrorQueue();
    Grammar g = new Grammar(grammar, equeue);
    if (g.ast != null && !g.ast.hasErrors) {
      SemanticPipeline sem = new SemanticPipeline(g);
      sem.process();

      ATNFactory factory = new ParserATNFactory(g);
      if (g.isLexer()) factory = new LexerATNFactory((LexerGrammar) g);
      g.atn = factory.createATN();

      CodeGenerator gen = new CodeGenerator(g);
      ST outputFileST = gen.generateParser();
      String output = outputFileST.render();
      // System.out.println(output);
      String b = "#" + actionName + "#";
      int start = output.indexOf(b);
      String e = "#end-" + actionName + "#";
      int end = output.indexOf(e);
      String snippet = output.substring(start + b.length(), end);
      assertEquals(expected, snippet);
    }
    if (equeue.size() > 0) {
      System.err.println(equeue.toString());
    }
  }
Example #11
0
 @Test
 public void testEmptyExprAsFirstLineGetsNoOutput() throws Exception {
   ST t = new ST("<users>\n" + "end\n");
   String expecting = "end" + newline;
   String result = t.render();
   assertEquals(expecting, result);
 }
Example #12
0
 @Test
 public void testIndentedIFExpr() throws Exception {
   ST t = new ST("begin\n" + "    <if(x)><endif>\n" + "end\n");
   String expecting = "begin" + newline + "end" + newline;
   String result = t.render();
   assertEquals(expecting, result);
 }
  @Override
  public void enterProgramHeading(pascalParser.ProgramHeadingContext ctx) {

    programName = ctx.getChild(1).getText();
    ST st = stg.getInstanceOf("enterProgram");
    st.add("v", programName);
    System.out.println(st.render());
  }
Example #14
0
 @Test
 public void testElseIFOnMultipleLines() throws Exception {
   ST t =
       new ST(
           "begin\n" + "<if(a)>\n" + "foo\n" + "<elseif(b)>\n" + "bar\n" + "<endif>\n" + "end\n");
   String expecting = "begin" + newline + "end" + newline;
   String result = t.render();
   assertEquals(expecting, result);
 }
Example #15
0
 @Test
 public void testNewlineNormalizationInTemplateStringPC() throws Exception {
   ST st = new ST("Foo\r\n" + "Bar\n");
   StringWriter sw = new StringWriter();
   st.write(new AutoIndentWriter(sw, "\r\n")); // force \r\n as newline
   String result = sw.toString();
   String expecting = "Foo\r\nBar\r\n"; // expect \r\n in output
   assertEquals(expecting, result);
 }
Example #16
0
 @Test
 public void testTrimmedNewlinesBeforeAfterInTemplate() throws Exception {
   String templates = "a(x) ::= <<" + newline + "foo" + newline + ">>" + newline;
   STGroupString group = new STGroupString(templates);
   ST st = group.getInstanceOf("a");
   String expected = "foo";
   String result = st.render();
   assertEquals(expected, result);
 }
Example #17
0
 @Test
 public void testDontTrimJustSpaceBeforeAfterInTemplate() throws Exception {
   String templates = "a(x) ::= << foo >>\n";
   STGroupString group = new STGroupString(templates);
   ST st = group.getInstanceOf("a");
   String expected = " foo ";
   String result = st.render();
   assertEquals(expected, result);
 }
Example #18
0
 @Test
 public void testLineBreakNoWhiteSpace() throws Exception {
   ST st = new ST("Foo <\\\\>" + newline + "bar\n");
   StringWriter sw = new StringWriter();
   st.write(new AutoIndentWriter(sw, "\n")); // force \n as newline
   String result = sw.toString();
   String expecting = "Foo bar\n";
   assertEquals(expecting, result);
 }
  @Override
  public void enterAssignmentStatement(pascalParser.AssignmentStatementContext ctx) {

    ST st = stg.getInstanceOf("assignment");
    st.add("ident", ctx.getChild(0).getText());
    st.add("value", ctx.getChild(2).getText());

    System.out.print(st.render().trim() + "\n");
  }
Example #20
0
 @Test
 public void testNewlineNormalizationInAttribute() throws Exception {
   ST st = new ST("Foo\r\n" + "<name>\n");
   st.add("name", "a\nb\r\nc");
   StringWriter sw = new StringWriter();
   st.write(new AutoIndentWriter(sw, "\n")); // force \n as newline
   String result = sw.toString();
   String expecting = "Foo\na\nb\nc\n"; // expect \n in output
   assertEquals(expecting, result);
 }
Example #21
0
 @Test
 public void testTrimmedSubtemplatesNoArgs() throws Exception {
   STGroup group = new STGroup();
   group.defineTemplate("test", "[<foo({ foo })>]");
   group.defineTemplate("foo", "x", "<x>");
   ST st = group.getInstanceOf("test");
   String expected = "[ foo ]";
   String result = st.render();
   assertEquals(expected, result);
 }
Example #22
0
 public static String renderTemplate(
     String templateStr, Map<String, ? extends Object> templateValues) {
   ST template = new ST(templateStr, '$', '$');
   if (templateValues != null) {
     for (String key : templateValues.keySet()) {
       template.add(key, templateValues.get(key));
     }
   }
   return template.render();
 }
Example #23
0
  public static String GetTemplate(Config config, String name, IRequestContext ctx)
      throws Exception {
    InputStream is =
        ctx.getServletContext().getResourceAsStream(String.format("%s%s", templatesPath, name));

    ST template = new ST(StringUtils.getStreamContents(is), '$', '$');
    template.add("cfg", config);
    String result = template.render();
    return result;
  }
  @Override
  public void enterConstantDefinitionPart(pascalParser.ConstantDefinitionPartContext ctx) {
    List<ParseTree> values = ctx.children;
    ArrayList<String> valueNames = new ArrayList<String>();

    for (int i = 1; i < ctx.getChildCount(); i += 3) valueNames.add(values.get(i).getText());

    ST st = stg.getInstanceOf("VAR");
    st.add("values", valueNames);
    System.out.println(st.render());
  }
 /** Test that a map can return a <b>string</b> with the word: default. */
 @Test
 public void testDictDefaultIsDefaultString() throws Exception {
   String templates =
       "map ::= [default: \"default\"] " + newline + "t() ::= << <map.(\"1\")> >>" + newline;
   writeFile(tmpdir, "test.stg", templates);
   STGroup group = new STGroupFile(tmpdir + "/" + "test.stg");
   ST st = group.getInstanceOf("t");
   String expecting = " default ";
   String result = st.render();
   assertEquals(expecting, result);
 }
Example #26
0
 @Test
 public void testLeaveNewlineOnEndInSubtemplates() throws Exception {
   STGroup group = new STGroup();
   group.defineTemplate("test", "names", "<names:{n |\n" + "<n>\n" + "}>!");
   ST st = group.getInstanceOf("test");
   st.add("names", "Ter");
   st.add("names", "Tom");
   st.add("names", "Sumana");
   String expected = "Ter" + newline + "Tom" + newline + "Sumana" + newline + "!";
   String result = st.render();
   assertEquals(expected, result);
 }
Example #27
0
 @Test
 public void testTrimJustOneWSInSubtemplates() throws Exception {
   STGroup group = new STGroup();
   group.defineTemplate("test", "names", "<names:{n |  <n> }>!");
   ST st = group.getInstanceOf("test");
   st.add("names", "Ter");
   st.add("names", "Tom");
   st.add("names", "Sumana");
   String expected = " Ter  Tom  Sumana !";
   String result = st.render();
   assertEquals(expected, result);
 }
Example #28
0
 @Test
 public void testTrimmedSubtemplatesArgs() throws Exception {
   STGroup group = new STGroup();
   group.defineTemplate("test", "names", "<names:{x|  foo }>");
   ST st = group.getInstanceOf("test");
   st.add("names", "Ter");
   st.add("names", "Tom");
   st.add("names", "Sumana");
   String expected = " foo  foo  foo ";
   String result = st.render();
   assertEquals(expected, result);
 }
  /**
   * This is a regression test for antlr/stringtemplate4#114. "dictionary value using <% %> is
   * broken" Before the fix the following test would return %hi%
   * https://github.com/antlr/stringtemplate4/issues/114
   */
  @Test
  public void testDictionaryBehaviorNoNewlineTemplate() throws Exception {
    String templates =
        "d ::= [\n" + "	\"x\" : <%hi%>\n" + "]\n" + "\n" + "t() ::= <<\n" + "<d.x>\n" + ">>\n";

    writeFile(tmpdir, "t.stg", templates);
    STGroup group = new STGroupFile(tmpdir + File.separatorChar + "t.stg");
    ST st = group.getInstanceOf("t");
    String expected = "hi";
    String result = st.render();
    assertEquals(expected, result);
  }
 public void add(MethodDecl decl) {
   if (addedDecls.contains(decl)) return;
   ST visit;
   if (visitorImpl) visit = tmplGroup.getInstanceOf("visit");
   else visit = tmplGroup.getInstanceOf("declVisit");
   for (Column c : decl.columns) {
     visit.add("columns", c);
   }
   if (decl.isLastColumn()) visit.add("outmost", true);
   visitorBase.add("methodDecls", visit);
   addedDecls.add(decl);
 }