Package org.stringtemplate.v4

Examples of org.stringtemplate.v4.STGroup


    setFileName(fileName);
    // ensure we have the composite set to something
    if ( composite.delegateGrammarTreeRoot==null ) {
      composite.setDelegationRoot(this);
    }
    STGroup lexerGrammarSTG = new STGroupString(lexerGrammarTemplate);
    lexerGrammarST = lexerGrammarSTG.getInstanceOf("grammar");
    target = CodeGenerator.loadLanguageTarget((String) getOption("language"));
  }
View Full Code Here


  public Grammar(Tool tool) {
    setTool(tool);
    builtFromString = true;
    composite = new CompositeGrammar(this);
    STGroup lexerGrammarSTG = new STGroupString(lexerGrammarTemplate);
    lexerGrammarST = lexerGrammarSTG.getInstanceOf("grammar");
    target = CodeGenerator.loadLanguageTarget((String)getOption("language"));
  }
View Full Code Here

        // new output dir for each test
        tmpdir = new File(System.getProperty("java.io.tmpdir"),
              "antlr-"+getClass().getName()+"-"+
              System.currentTimeMillis()).getAbsolutePath();
        ErrorManager.resetErrorState();
        STGroup.defaultGroup = new STGroup();
    }
View Full Code Here

    return getBadWords().contains(idNode.getText());
  }

  @Override
  protected STGroup loadTemplates() {
    STGroup result = targetTemplates.get();
    if (result == null) {
      result = super.loadTemplates();
      result.registerRenderer(String.class, new JavaStringRenderer(), true);
      targetTemplates.set(result);
    }

    return result;
  }
View Full Code Here

  }

  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 ) {
View Full Code Here

                Map<String, Map<String, Object>> actions = grammar.getActions();
                actions.put( "all", sharedActionBlocks.get( grammar.name ) );
            }
        }

        STGroup group = generator.getTemplates();
        RubyRenderer renderer = new RubyRenderer();
        try {
            group.registerRenderer( Class.forName( "java.lang.String" ), renderer );
        } catch ( ClassNotFoundException e ) {
            // this shouldn't happen
            System.err.println( "ClassNotFoundException: " + e.getMessage() );
            e.printStackTrace( System.err );
        }
View Full Code Here

  public void buildLeftRecursiveRuleFunction(LeftRecursiveRule r, LeftRecursiveRuleFunction function) {
    buildNormalRuleFunction(r, function);

    // now inject code to start alts
    CodeGenerator gen = delegate.getGenerator();
    STGroup codegenTemplates = gen.getTemplates();

    // pick out alt(s) for primaries
    CodeBlockForOuterMostAlt outerAlt = (CodeBlockForOuterMostAlt)function.code.get(0);
    List<CodeBlockForAlt> primaryAltsCode = new ArrayList<CodeBlockForAlt>();
    SrcOp primaryStuff = outerAlt.ops.get(0);
    if ( primaryStuff instanceof Choice ) {
      Choice primaryAltBlock = (Choice) primaryStuff;
      primaryAltsCode.addAll(primaryAltBlock.alts);
    }
    else { // just a single alt I guess; no block
      primaryAltsCode.add((CodeBlockForAlt)primaryStuff);
    }

    // pick out alt(s) for op alts
    StarBlock opAltStarBlock = (StarBlock)outerAlt.ops.get(1);
    CodeBlockForAlt altForOpAltBlock = opAltStarBlock.alts.get(0);
    List<CodeBlockForAlt> opAltsCode = new ArrayList<CodeBlockForAlt>();
    SrcOp opStuff = altForOpAltBlock.ops.get(0);
    if ( opStuff instanceof AltBlock ) {
      AltBlock opAltBlock = (AltBlock)opStuff;
      opAltsCode.addAll(opAltBlock.alts);
    }
    else { // just a single alt I guess; no block
      opAltsCode.add((CodeBlockForAlt)opStuff);
    }

    // Insert code in front of each primary alt to create specialized ctx if there was a label
    for (int i = 0; i < primaryAltsCode.size(); i++) {
      LeftRecursiveRuleAltInfo altInfo = r.recPrimaryAlts.get(i);
      if ( altInfo.altLabel==null ) continue;
      ST altActionST = codegenTemplates.getInstanceOf("recRuleReplaceContext");
      altActionST.add("ctxName", Utils.capitalize(altInfo.altLabel));
      Action altAction =
        new Action(delegate, function.altLabelCtxs.get(altInfo.altLabel), altActionST);
      CodeBlockForAlt alt = primaryAltsCode.get(i);
      alt.insertOp(0, altAction);
    }

    // Insert code to set ctx.stop after primary block and before op * loop
    ST setStopTokenAST = codegenTemplates.getInstanceOf("recRuleSetStopToken");
    Action setStopTokenAction = new Action(delegate, function.ruleCtx, setStopTokenAST);
    outerAlt.insertOp(1, setStopTokenAction);

    // Insert code to set _prevctx at start of * loop
    ST setPrevCtx = codegenTemplates.getInstanceOf("recRuleSetPrevCtx");
    Action setPrevCtxAction = new Action(delegate, function.ruleCtx, setPrevCtx);
    opAltStarBlock.addIterationOp(setPrevCtxAction);

    // Insert code in front of each op alt to create specialized ctx if there was an alt label
    for (int i = 0; i < opAltsCode.size(); i++) {
      ST altActionST;
      LeftRecursiveRuleAltInfo altInfo = r.recOpAlts.getElement(i);
      String templateName;
      if ( altInfo.altLabel!=null ) {
        templateName = "recRuleLabeledAltStartAction";
        altActionST = codegenTemplates.getInstanceOf(templateName);
        altActionST.add("currentAltLabel", altInfo.altLabel);
      }
      else {
        templateName = "recRuleAltStartAction";
        altActionST = codegenTemplates.getInstanceOf(templateName);
        altActionST.add("ctxName", Utils.capitalize(r.name));
      }
      altActionST.add("ruleName", r.name);
      // add label of any lr ref we deleted
      altActionST.add("label", altInfo.leftRecursiveRuleRefLabel);
View Full Code Here

  protected abstract boolean visibleGrammarSymbolCausesIssueInGeneratedCode(GrammarAST idNode);

  public boolean templatesExist() {
    String groupFileName = CodeGenerator.TEMPLATE_ROOT + "/" + getLanguage() + "/" + getLanguage() + STGroup.GROUP_FILE_EXTENSION;
    STGroup result = null;
    try {
      result = new STGroupFile(groupFileName);
    }
    catch (IllegalArgumentException iae) {
      result = null;
View Full Code Here

  }

  @Nullable
  protected STGroup loadTemplates() {
    String groupFileName = CodeGenerator.TEMPLATE_ROOT + "/" + getLanguage() + "/" + getLanguage() + STGroup.GROUP_FILE_EXTENSION;
    STGroup result = null;
    try {
      result = new STGroupFile(groupFileName);
    }
    catch (IllegalArgumentException iae) {
      gen.tool.errMgr.toolError(ErrorType.MISSING_CODE_GEN_TEMPLATES,
             iae,
             language);
    }
    if ( result==null ) return null;
    result.registerRenderer(Integer.class, new NumberRenderer());
    result.registerRenderer(String.class, new StringRenderer());
    result.setListener(new STErrorListener() {
      @Override
      public void compileTimeError(STMessage msg) {
        reportError(msg);
      }
View Full Code Here

  public void time2Args(int reps) {
    String templates =
      "t(x,y) ::= \"<x><y>\"\n";
    Misc.writeFile(tmpdir, "t.stg", templates);
    STGroup group = new STGroupFile(tmpdir+"/"+"t.stg");
    ST st = group.getInstanceOf("t");
    st.add("x", 1);
    st.add("y", 2);

    for (int i = 0; i < reps; i++) {
      st.render();
View Full Code Here

TOP

Related Classes of org.stringtemplate.v4.STGroup

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.