Package avrora.core

Examples of avrora.core.Instr$ST


    // 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);
      if (altActionST.impl.formalArguments.containsKey("isListLabel")) {
        altActionST.add("isListLabel", altInfo.isListLabel);
      }
      else if (altInfo.isListLabel) {
        delegate.getGenerator().tool.errMgr.toolError(ErrorType.CODE_TEMPLATE_ARG_ISSUE, templateName, "isListLabel");
      }
      Action altAction =
View Full Code Here


    // CREATE TEMPLATE FOR THIS OUTPUT OBJECT
    Class<? extends OutputModelObject> cl = omo.getClass();
    String templateName = cl.getSimpleName();
    if ( templateName == null ) {
      tool.errMgr.toolError(ErrorType.NO_MODEL_TO_TEMPLATE_MAPPING, cl.getSimpleName());
      return new ST("["+templateName+" invalid]");
    }
    ST st = templates.getInstanceOf(templateName);
    if ( st == null ) {
      tool.errMgr.toolError(ErrorType.CODE_GEN_TEMPLATES_INCOMPLETE, templateName);
      return new ST("["+templateName+" invalid]");
    }
    if ( st.impl.formalArguments == null ) {
      tool.errMgr.toolError(ErrorType.CODE_TEMPLATE_ARG_ISSUE, templateName, "<none>");
      return st;
    }

    Map<String,FormalArgument> formalArgs = st.impl.formalArguments;

    // PASS IN OUTPUT MODEL OBJECT TO TEMPLATE AS FIRST ARG
    Set<String> argNames = formalArgs.keySet();
    Iterator<String> arg_it = argNames.iterator();
    String modelArgName = arg_it.next(); // ordered so this is first arg
    st.add(modelArgName, omo);

    // COMPUTE STs FOR EACH NESTED MODEL OBJECT MARKED WITH @ModelElement AND MAKE ST ATTRIBUTE
    Set<String> usedFieldNames = new HashSet<String>();
    Field fields[] = cl.getFields();
    for (Field fi : fields) {
      ModelElement annotation = fi.getAnnotation(ModelElement.class);
      if (annotation == null) {
        continue;
      }

      String fieldName = fi.getName();

      if (!usedFieldNames.add(fieldName)) {
        tool.errMgr.toolError(ErrorType.INTERNAL_ERROR, "Model object " + omo.getClass().getSimpleName() + " has multiple fields named '" + fieldName + "'");
        continue;
      }

      // Just don't set @ModelElement fields w/o formal arg in target ST
      if ( formalArgs.get(fieldName)==null ) continue;

      try {
        Object o = fi.get(omo);
        if ( o instanceof OutputModelObject ) {  // SINGLE MODEL OBJECT?
          OutputModelObject nestedOmo = (OutputModelObject)o;
          ST nestedST = walk(nestedOmo);
//          System.out.println("set ModelElement "+fieldName+"="+nestedST+" in "+templateName);
          st.add(fieldName, nestedST);
        }
        else if ( o instanceof Collection || o instanceof OutputModelObject[] ) {
          // LIST OF MODEL OBJECTS?
          if ( o instanceof OutputModelObject[] ) {
            o = Arrays.asList((OutputModelObject[])o);
          }
          Collection<?> nestedOmos = (Collection<?>)o;
          for (Object nestedOmo : nestedOmos) {
            if ( nestedOmo==null ) continue;
            ST nestedST = walk((OutputModelObject)nestedOmo);
//            System.out.println("set ModelElement "+fieldName+"="+nestedST+" in "+templateName);
            st.add(fieldName, nestedST);
          }
        }
        else if ( o instanceof Map ) {
          Map<?, ?> nestedOmoMap = (Map<?, ?>)o;
          Map<Object, ST> m = new LinkedHashMap<Object, ST>();
          for (Map.Entry<?, ?> entry : nestedOmoMap.entrySet()) {
            ST nestedST = walk((OutputModelObject)entry.getValue());
//            System.out.println("set ModelElement "+fieldName+"="+nestedST+" in "+templateName);
            m.put(entry.getKey(), nestedST);
          }
          st.add(fieldName, m);
        }
View Full Code Here

  }

  public String getDOT(DFA dfa, boolean isLexer) {
    if ( dfa.s0==null return null;

    ST dot = stlib.getInstanceOf("dfa");
    dot.add("name", "DFA"+dfa.decision);
    dot.add("startState", dfa.s0.stateNumber);
//    dot.add("useBox", Tool.internalOption_ShowATNConfigsInDFA);
    dot.add("rankdir", rankdir);

    // define stop states first; seems to be a bug in DOT where doublecircle
    for (DFAState d : dfa.states.keySet()) {
      if ( !d.isAcceptState ) continue;
      ST st = stlib.getInstanceOf("stopstate");
      st.add("name", "s"+d.stateNumber);
      st.add("label", getStateLabel(d));
      dot.add("states", st);
    }

    for (DFAState d : dfa.states.keySet()) {
      if ( d.isAcceptState ) continue;
      if ( d.stateNumber == Integer.MAX_VALUE ) continue;
      ST st = stlib.getInstanceOf("state");
      st.add("name", "s"+d.stateNumber);
      st.add("label", getStateLabel(d));
      dot.add("states", st);
    }

    for (DFAState d : dfa.states.keySet()) {
      if ( d.edges!=null ) {
        for (int i = 0; i < d.edges.length; i++) {
          DFAState target = d.edges[i];
          if ( target==null) continue;
          if ( target.stateNumber == Integer.MAX_VALUE ) continue;
          int ttype = i-1; // we shift up for EOF as -1 for parser
          String label = String.valueOf(ttype);
          if ( isLexer ) label = "'"+getEdgeLabel(String.valueOf((char) i))+"'";
          else if ( grammar!=null ) label = grammar.getTokenDisplayName(ttype);
          ST st = stlib.getInstanceOf("edge");
          st.add("label", label);
          st.add("src", "s"+d.stateNumber);
          st.add("target", "s"+target.stateNumber);
          st.add("arrowhead", arrowhead);
          dot.add("edges", st);
        }
      }
    }
View Full Code Here

  public String getLoopCounter(GrammarAST ast) {
    return "cnt"+ ast.token.getTokenIndex();
  }

  public String getListLabel(String label) {
    ST st = getTemplates().getInstanceOf("ListLabelName");
    st.add("label", label);
    return st.render();
  }
View Full Code Here

  }

  // should be same for all refs to same token like ctx.ID within single rule function
  // for literals like 'while', we gen _s<ttype>
  public String getImplicitTokenLabel(String tokenName) {
    ST st = getTemplates().getInstanceOf("ImplicitTokenLabel");
    int ttype = getCodeGenerator().g.getTokenType(tokenName);
    if ( tokenName.startsWith("'") ) {
      return "s"+ttype;
    }
    String text = getTokenTypeAsTargetLabel(getCodeGenerator().g, ttype);
    st.add("tokenName", text);
    return st.render();
  }
View Full Code Here

    return st.render();
  }

  // x=(A|B)
  public String getImplicitSetLabel(String id) {
    ST st = getTemplates().getInstanceOf("ImplicitSetLabel");
    st.add("id", id);
    return st.render();
  }
View Full Code Here

  public String getDOT(ATNState startState, String[] ruleNames, boolean isLexer) {
    if ( startState==null return null;

    // The output DOT graph for visualization
    Set<ATNState> markedStates = new HashSet<ATNState>();
    ST dot = stlib.getInstanceOf("atn");
    dot.add("startState", startState.stateNumber);
    dot.add("rankdir", rankdir);

    List<ATNState> work = new LinkedList<ATNState>();

    work.add(startState);
    while ( !work.isEmpty() ) {
      ATNState s = work.get(0);
      if ( markedStates.contains(s) ) { work.remove(0); continue; }
      markedStates.add(s);

      // don't go past end of rule node to the follow states
      if ( s instanceof RuleStopState) continue;

      // special case: if decision point, then line up the alt start states
      // unless it's an end of block
//      if ( s instanceof BlockStartState ) {
//        ST rankST = stlib.getInstanceOf("decision-rank");
//        DecisionState alt = (DecisionState)s;
//        for (int i=0; i<alt.getNumberOfTransitions(); i++) {
//          ATNState target = alt.transition(i).target;
//          if ( target!=null ) {
//            rankST.add("states", target.stateNumber);
//          }
//        }
//        dot.add("decisionRanks", rankST);
//      }

      // make a DOT edge for each transition
      ST edgeST;
      for (int i = 0; i < s.getNumberOfTransitions(); i++) {
        Transition edge = s.transition(i);
        if ( edge instanceof RuleTransition ) {
          RuleTransition rr = ((RuleTransition)edge);
          // don't jump to other rules, but display edge to follow node
          edgeST = stlib.getInstanceOf("edge");

          String label = "<" + ruleNames[rr.ruleIndex];
          if (((RuleStartState)rr.target).isPrecedenceRule) {
            label += "[" + rr.precedence + "]";
          }
          label += ">";

          edgeST.add("label", label);
          edgeST.add("src", "s"+s.stateNumber);
          edgeST.add("target", "s"+rr.followState.stateNumber);
          edgeST.add("arrowhead", arrowhead);
          dot.add("edges", edgeST);
          work.add(rr.followState);
          continue;
        }
        if ( edge instanceof ActionTransition) {
          edgeST = stlib.getInstanceOf("action-edge");
          edgeST.add("label", getEdgeLabel(edge.toString()));
        }
        else if ( edge instanceof AbstractPredicateTransition ) {
          edgeST = stlib.getInstanceOf("edge");
          edgeST.add("label", getEdgeLabel(edge.toString()));
        }
        else if ( edge.isEpsilon() ) {
          edgeST = stlib.getInstanceOf("epsilon-edge");
          edgeST.add("label", getEdgeLabel(edge.toString()));
          boolean loopback = false;
          if (edge.target instanceof PlusBlockStartState) {
            loopback = s.equals(((PlusBlockStartState)edge.target).loopBackState);
          }
          else if (edge.target instanceof StarLoopEntryState) {
            loopback = s.equals(((StarLoopEntryState)edge.target).loopBackState);
          }
          edgeST.add("loopback", loopback);
        }
        else if ( edge instanceof AtomTransition ) {
          edgeST = stlib.getInstanceOf("edge");
          AtomTransition atom = (AtomTransition)edge;
          String label = String.valueOf(atom.label);
          if ( isLexer ) label = "'"+getEdgeLabel(String.valueOf((char)atom.label))+"'";
          else if ( grammar!=null ) label = grammar.getTokenDisplayName(atom.label);
          edgeST.add("label", getEdgeLabel(label));
        }
        else if ( edge instanceof SetTransition ) {
          edgeST = stlib.getInstanceOf("edge");
          SetTransition set = (SetTransition)edge;
          String label = set.label().toString();
          if ( isLexer ) label = set.label().toString(true);
          else if ( grammar!=null ) label = set.label().toString(grammar.getVocabulary());
          if ( edge instanceof NotSetTransition ) label = "~"+label;
          edgeST.add("label", getEdgeLabel(label));
        }
        else if ( edge instanceof RangeTransition ) {
          edgeST = stlib.getInstanceOf("edge");
          RangeTransition range = (RangeTransition)edge;
          String label = range.label().toString();
          if ( isLexer ) label = range.toString();
          else if ( grammar!=null ) label = range.label().toString(grammar.getVocabulary());
          edgeST.add("label", getEdgeLabel(label));
        }
        else {
          edgeST = stlib.getInstanceOf("edge");
          edgeST.add("label", getEdgeLabel(edge.toString()));
        }
        edgeST.add("src", "s"+s.stateNumber);
        edgeST.add("target", "s"+edge.target.stateNumber);
        edgeST.add("arrowhead", arrowhead);
        if (s.getNumberOfTransitions() > 1) {
          edgeST.add("transitionIndex", i);
        } else {
          edgeST.add("transitionIndex", false);
        }
        dot.add("edges", edgeST);
        work.add(edge.target);
      }
    }

    // define nodes we visited (they will appear first in DOT output)
    // this is an example of ST's lazy eval :)
    // define stop state first; seems to be a bug in DOT where doublecircle
    // shape only works if we define them first. weird.
//    ATNState stopState = startState.atn.ruleToStopState.get(startState.rule);
//    if ( stopState!=null ) {
//      ST st = stlib.getInstanceOf("stopstate");
//      st.add("name", "s"+stopState.stateNumber);
//      st.add("label", getStateLabel(stopState));
//      dot.add("states", st);
//    }
    for (ATNState s : markedStates) {
      if ( !(s instanceof RuleStopState) ) continue;
      ST st = stlib.getInstanceOf("stopstate");
      st.add("name", "s"+s.stateNumber);
      st.add("label", getStateLabel(s));
      dot.add("states", st);
    }

    for (ATNState s : markedStates) {
      if ( s instanceof RuleStopState ) continue;
      ST st = stlib.getInstanceOf("state");
      st.add("name", "s"+s.stateNumber);
      st.add("label", getStateLabel(s));
      st.add("transitions", s.getTransitions());
      dot.add("states", st);
    }

    return dot.render();
  }
View Full Code Here

    st.add("id", id);
    return st.render();
  }

  public String getImplicitRuleLabel(String ruleName) {
    ST st = getTemplates().getInstanceOf("ImplicitRuleLabel");
    st.add("ruleName", ruleName);
    return st.render();
  }
View Full Code Here

    st.add("ruleName", ruleName);
    return st.render();
  }

  public String getElementListName(String name) {
    ST st = getTemplates().getInstanceOf("ElementListName");
    st.add("elemName", getElementName(name));
    return st.render();
  }
View Full Code Here

      return this.event == ((Wrapper)o).event;
    }

    @Override
    public String toString() {
      ST st = event.scope.st;
      if ( st.isAnonSubtemplate() ) return "{...}";
      if ( st.debugState!=null && st.debugState.newSTEvent!=null ) {
        String label = st.toString()+" @ "+st.debugState.newSTEvent.getFileName()+":"+
             st.debugState.newSTEvent.getLine();
        return "<html><b>" + StringRenderer.escapeHTML(label) + "</b></html>";
      }
      else {
        return st.toString();
      }
    }
View Full Code Here

TOP

Related Classes of avrora.core.Instr$ST

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.