Пример #1
0
void ruby_write_node_for_local_from_n_to_m(node_t*n, state_t*s)
{
    strf(s, "(");
    write_node(s, n->child[0]);
    strf(s, ").upto((");
    write_node(s, n->child[1]);
    strf(s, ")-1) do\n");
    indent(s);write_node(s, n->child[2]);dedent(s);
    strf(s, "\nend");
}
Пример #2
0
void c_write_node_for_local_from_n_to_m(node_t*n, state_t*s)
{
    strf(s, "for(v%d=", n->value.i);
    write_node(s, n->child[0]);
    strf(s, ";v%d<", n->value.i);
    write_node(s, n->child[1]);
    strf(s, ";v%d++) {\n", n->value.i);
    indent(s);write_node(s, n->child[2]);dedent(s);
    strf(s, "\n}\n");
}
Пример #3
0
void c_write_node_if(node_t*n, state_t*s)
{
    if(!node_terminates(n) && node_has_consumer_parent(n)) {
        strf(s, "(");
        write_node(s, n->child[0]);
        strf(s, ")?(");
        write_node(s, n->child[1]);
        strf(s, "):(");
        write_node(s, n->child[2]);
        strf(s, ")");
    } else {
        strf(s, "if(");
        write_node(s, n->child[0]);
        strf(s, ")\n");
        indent(s);write_node(s, n->child[1]);
        if(!node_is_missing(n->child[2])) {
            strf(s, ";");dedent(s);
            strf(s, "\nelse\n");
            indent(s);write_node(s, n->child[2]);
        }
        dedent(s);
    }
}
Пример #4
0
void ruby_write_node_if(node_t*n, state_t*s)
{
    if(!node_terminates(n) && node_has_consumer_parent(n)) {
        strf(s, "if ");
        write_node(s, n->child[0]);
        strf(s, " then ");
        indent(s);write_node(s, n->child[1]);dedent(s);
        if(!node_is_missing(n->child[2])) {
            strf(s, " else ");
            indent(s);write_node(s, n->child[2]);dedent(s);
        }
        strf(s, " end");
    } else {
        strf(s, "if ");
        write_node(s, n->child[0]);
        strf(s, " then\n");
        indent(s);write_node(s, n->child[1]);dedent(s);
        if(!node_is_missing(n->child[2])) {
            strf(s, "\nelse\n");
            indent(s);write_node(s, n->child[2]);dedent(s);
        }
        strf(s, "\nend");
    }
}
Пример #5
0
void c_write_node_block(node_t*n, state_t*s)
{
    int t;
    if(n->parent) {
        strf(s, "{\n");
        indent(s);
    }
    for(t=0;t<n->num_children;t++) {
        if(t)
            strf(s, "\n");
        write_node(s, n->child[t]);
        strf(s, ";");
    }
    if(n->parent) {
        dedent(s);
        strf(s, "\n}\n");
    }
}
Пример #6
0
void Token::print() const {
	if( eol() ) std::cout << "NEWLINE" ;
	else if( eof() ) std::cout << "ENDMARKER" ;
	else if( indent() ) std::cout << "INDENT";
	else if( dedent() ) std::cout << "DEDENT";
	else if( isOpenBrace() ) std::cout << " { ";
	else if( isCloseBrace() ) std::cout << " } ";
	else if( isComma() ) std::cout << " , ";
	else if( isPeriod()) std::cout<< ".";
	else if( isEqual() ) std::cout << " == ";
	else if( isNotEqual() ) std::cout << " != ";
	else if( isLessThan() ) std::cout << " < ";
	else if( isGreaterThan() ) std::cout << " > ";
	else if( isLessThanEqual() ) std::cout << " <= ";
	else if( isGreaterThanEqual() ) std::cout << " >= ";
	else if( isOpenParen() )  std::cout << " ( " ;
	else if( isCloseParen() )  std::cout << " ) " ;
	else if( isAssignmentOperator() )  std::cout << " = ";
	else if( isColon() )  std::cout << " : " ;
	else if( isMultiplicationOperator() )  std::cout << " * " ;
	else if( isAdditionOperator() )  std::cout << " + ";
	else if( isSubtractionOperator() )  std::cout << " - ";
	else if( isModuloOperator() )  std::cout << " % ";
	else if( isDivisionOperator() )  std::cout << " / ";
	else if( isFloorDivision() ) std::cout << " // ";
	else if( isOpenBrack() ) std::cout<< "[";
	else if( isCloseBrack() ) std::cout<< "]";
	else if( isName() )  std::cout << getName();
	else if( isKeyword() ) std::cout << getKeyword();
	else if( isWholeNumber() ) std::cout << getWholeNumber();
	else if( isFloat() ) std::cout << getFloat();
	else if( isString() ) std::cout << getString();
	else if( isCall() ) std::cout << "CALL " << getName();
	else if( isSub() ) std::cout << "ARRAY SUB " << getName();
	else if( isAppend() ) std::cout << "ARRAY APPEND " << getName();
	else if( isPop() ) std::cout << "ARRAY POP " << getName();
	else std::cout << "Uninitialized token.\n";
}
Пример #7
0
static void write_close(Context* ctx, NextChar next_char) {
  if (ctx->next_char != NEXT_CHAR_FORCE_NEWLINE)
    ctx->next_char = NEXT_CHAR_NONE;
  dedent(ctx);
  write_puts(ctx, ")", next_char);
}
Пример #8
0
void ruby_write_footer(model_t*model, state_t*s)
{
    dedent(s);
    strf(s, "\nend\n");
}
Пример #9
0
	StringTemplate(const std::string &format)
	: _fmt(dedent(format))
	{

	}
Пример #10
0
	StringTemplate(const char *format)
	: _fmt(dedent(format)) { }
Пример #11
0
void c_write_footer(model_t*model, state_t*s)
{
    node_t*root = model->code;
    dedent(s);
    strf(s, "\n}\n");
}
Пример #12
0
void EditorView::keyPressEvent(QKeyEvent * e)
{
    QTextCursor cursor = textCursor();

    if (e->modifiers() & Qt::ControlModifier)
    {
        switch (e->key())
        {
            case Qt::Key_Home:
                cursor.movePosition(QTextCursor::Start);
                setTextCursor(cursor);
                break;
            case Qt::Key_End:
                cursor.movePosition(QTextCursor::End);
                setTextCursor(cursor);
                break;

            default:
                QPlainTextEdit::keyPressEvent(e);
        }
    }
    else
    {
        switch (e->key())
        {
            case Qt::Key_Enter:
            case Qt::Key_Return:
                if (smartIndent)
                {
                    if(autoIndent() == 0)
                    {
                        QPlainTextEdit::keyPressEvent(e);
                    }
                }
                else
                    QPlainTextEdit::keyPressEvent(e);

                break;
            case Qt::Key_Period:
                if(!handleAutoComplete('.'))
                    QPlainTextEdit::keyPressEvent(e);
                break;
            case Qt::Key_NumberSign:
                if(!handleAutoComplete('#'))
                    QPlainTextEdit::keyPressEvent(e);
                break;
            case Qt::Key_Tab:
                tabOn = true;
                if (textCursor().hasSelection())
                    tabBlockShift();
                else
                    indent();
                break;
            case Qt::Key_Backtab:
                tabOn = true;
                tabBlockShift();
                break;
            case Qt::Key_Backspace:
                if (tabOn)
                    dedent();
                else
                    QPlainTextEdit::keyPressEvent(e);
                break;
            case Qt::Key_Space:
                tabOn = false;
                QPlainTextEdit::keyPressEvent(e);
                break;

            default:
                QPlainTextEdit::keyPressEvent(e);
        }
    }
}
Пример #13
0
size_t Matcher::match(Method method)
{
  DBGLOG("BEGIN Matcher::match()");
  if (pos_ < end_)
    buf_[pos_] = chr_;
scan:
  txt_ = buf_ + cur_;
  len_ = 0;
  bool bob = at_bob();
  if (hit_end() && ded_ == 0 && tab_.empty())
  {
    if (method == Const::SPLIT && !bob && cap_ != 0 && cap_ != Const::EMPTY)
    {
      cap_ = Const::EMPTY;
      buf_[pos_] = '\0';
      DBGLOG("Split empty at end, cap = %zu", cap_);
      DBGLOG("END Matcher::match()");
      return cap_;
    }
    cap_ = 0;
    DBGLOG("END Matcher::match()");
    return 0;
  }
  bool bol = bob || at_bol();
  bool bow;
  bool eow;
  int c1 = got_;
  if (isword(c1))
  {
    bow = false;
    eow = !isword(peek());
  }
  else
  {
    bow = isword(peek());
    eow = false;
  }
  ind_ = pos_; // ind scans input in buf[] in newline() up to pos - 1
  size_t col = 0; // count columns from BOL
  if (pat_->fsm_)
  {
    fsm_.bob = bob;
    fsm_.bow = bow;
    fsm_.eow = eow;
    fsm_.col = col;
    fsm_.c1 = c1;
  }
redo:
  cap_ = 0;
  lap_.resize(0);
  bool nul = method == Const::MATCH;
  if (pat_->fsm_)
  {
    DBGLOG("FSM code %p", pat_->fsm_);
    fsm_.bol = bol;
    fsm_.nul = nul;
    pat_->fsm_(*this);
    col = fsm_.col;
    nul = fsm_.nul;
    c1 = fsm_.c1;
  }
  else if (pat_->opc_)
  {
    const Pattern::Opcode *pc = pat_->opc_;
    while (true)
    {
      Pattern::Opcode opcode = *pc;
      DBGLOG("Fetch: code[%zu] = 0x%08X", pc - pat_->opc_, opcode);
      Pattern::Index index;
      switch (opcode >> 16)
      {
        case 0x00ff: // check if HALT
          if (Pattern::is_opcode_halt(opcode))
            goto done;
          break;
        case 0xff00: // TAKE
          cap_ = Pattern::index_of(opcode);
          DBGLOG("Take: cap = %zu", cap_);
          cur_ = pos_;
          ++pc;
          continue;
        case 0xff7e: // TAIL
          index = Pattern::index_of(opcode);
          DBGLOG("Tail: %u", index);
          if (lap_.size() > index && lap_[index] >= 0)
            cur_ = txt_ - buf_ + static_cast<size_t>(lap_[index]); // mind the (new) gap
          ++pc;
          continue;
        case 0xff7f: // HEAD
          index = Pattern::index_of(opcode);
          DBGLOG("Head: lookahead[%u] = %zu", index, pos_ - (txt_ - buf_));
          if (lap_.size() <= index)
            lap_.resize(index + 1, -1);
          lap_[index] = static_cast<int>(pos_ - (txt_ - buf_)); // mind the gap
          ++pc;
          continue;
        case 0xff00 | Pattern::META_DED:
          if (ded_ > 0)
          {
            index = Pattern::index_of(opcode);
            DBGLOG("Dedent ded = %zu", ded_); // unconditional dedent matching \j
            nul = true;
            pc = pat_->opc_ + index;
            continue;
          }
      }
      int c0 = c1;
      if (c0 == EOF)
        break;
      c1 = get();
      DBGLOG("Get: c1 = %d", c1);
      index = Pattern::IMAX;
      Pattern::Index back = Pattern::IMAX; // where to jump back to (backtrack on meta transitions)
      Pattern::Index la;
      while (true)
      {
        if (index == Pattern::IMAX || back == Pattern::IMAX) // we no longer have to pass through all if index and back are set
        {
          switch (opcode >> 16)
          {
            case 0xff00: // TAKE
              cap_ = Pattern::index_of(opcode);
              DBGLOG("Take: cap = %zu", cap_);
              cur_ = pos_;
              if (c1 != EOF)
                --cur_; // Must unget one char
              opcode = *++pc;
              continue;
            case 0xff7e: // TAIL
              la = Pattern::index_of(opcode);
              DBGLOG("Tail: %u", la);
              if (lap_.size() > la && lap_[la] >= 0)
                cur_ = txt_ - buf_ + static_cast<size_t>(lap_[la]); // mind the (new) gap
              opcode = *++pc;
              continue;
            case 0xff7f: // HEAD
              opcode = *++pc;
              continue;
            case 0xff00 | Pattern::META_DED:
              DBGLOG("DED? %d", c1);
              if (index == Pattern::IMAX && bol && dedent(col))
                index = Pattern::index_of(opcode);
              opcode = *++pc;
              continue;
            case 0xff00 | Pattern::META_IND:
              DBGLOG("IND? %d", c1);
              if (index == Pattern::IMAX && bol && indent(col))
                index = Pattern::index_of(opcode);
              opcode = *++pc;
              continue;
            case 0xff00 | Pattern::META_EOB:
              DBGLOG("EOB? %d", c1);
              if (index == Pattern::IMAX && c1 == EOF)
                index = Pattern::index_of(opcode);
              opcode = *++pc;
              continue;
            case 0xff00 | Pattern::META_BOB:
              DBGLOG("BOB? %d", bob);
              if (index == Pattern::IMAX && bob)
                index = Pattern::index_of(opcode);
              opcode = *++pc;
              continue;
            case 0xff00 | Pattern::META_EOL:
              DBGLOG("EOL? %d", c1);
              if (index == Pattern::IMAX && (c1 == EOF || c1 == '\n'))
                index = Pattern::index_of(opcode);
              opcode = *++pc;
              continue;
            case 0xff00 | Pattern::META_BOL:
              DBGLOG("BOL? %d", bol);
              if (index == Pattern::IMAX && bol)
                index = Pattern::index_of(opcode);
              opcode = *++pc;
              continue;
            case 0xff00 | Pattern::META_EWE:
              DBGLOG("EWE? %d %d %d", c0, c1, isword(c0) && !isword(c1));
              if (index == Pattern::IMAX && isword(c0) && !isword(c1))
                index = Pattern::index_of(opcode);
              opcode = *++pc;
              continue;
            case 0xff00 | Pattern::META_BWE:
              DBGLOG("BWE? %d %d %d", c0, c1, !isword(c0) && isword(c1));
              if (index == Pattern::IMAX && !isword(c0) && isword(c1))
                index = Pattern::index_of(opcode);
              opcode = *++pc;
              continue;
            case 0xff00 | Pattern::META_EWB:
              DBGLOG("EWB? %d", eow);
              if (index == Pattern::IMAX && eow)
                index = Pattern::index_of(opcode);
              opcode = *++pc;
              continue;
            case 0xff00 | Pattern::META_BWB:
              DBGLOG("BWB? %d", bow);
              if (index == Pattern::IMAX && bow)
                index = Pattern::index_of(opcode);
              opcode = *++pc;
              continue;
            case 0xff00 | Pattern::META_NWE:
              DBGLOG("NWE? %d %d %d", c0, c1, isword(c0) == isword(c1));
              if (index == Pattern::IMAX && isword(c0) == isword(c1))
                index = Pattern::index_of(opcode);
              opcode = *++pc;
              continue;
            case 0xff00 | Pattern::META_NWB:
              DBGLOG("NWE? %d %d", bow, eow);
              if (index == Pattern::IMAX && !bow && !eow)
                index = Pattern::index_of(opcode);
              opcode = *++pc;
              continue;
          }
        }
        if (index != Pattern::IMAX)
        {
          DBGLOG("Backtrack: pc = %u", index);
          if (back == Pattern::IMAX)
            back = static_cast<Pattern::Index>(pc - pat_->opc_);
          pc = pat_->opc_ + index;
          opcode = *pc;
          index = Pattern::IMAX;
        }
        else
        {
          if (back != Pattern::IMAX)
          {
            pc = pat_->opc_ + back;
            opcode = *pc;
          }
          break;
        }
      }
      if (c1 == EOF)
        break;
      Pattern::Opcode lo = c1 << 24;
      Pattern::Opcode hi = lo | 0x00ffffff;
      while (hi < opcode || lo > (opcode << 8))
        opcode = *++pc;
      index = Pattern::index_of(opcode);
      if (index == Pattern::IMAX)
        break;
      pc = pat_->opc_ + index;
    }
  }
done:
  if (bol && cap_ != Const::EMPTY)
  {
    if (col > 0 && (tab_.empty() || tab_.back() < col))
    {
      DBGLOG("Set new stop: tab_[%zu] = %zu", tab_.size(), col);
      tab_.push_back(col);
    }
    else if (!tab_.empty() && tab_.back() > col)
    {
      size_t n;
      for (n = tab_.size() - 1; n > 0; --n)
        if (tab_.at(n - 1) <= col)
          break;
      ded_ += tab_.size() - n;
      DBGLOG("Dedents: ded = %zu tab_ = %zu", ded_, tab_.size());
      tab_.resize(n);
      if (n > 0)
        tab_.back() = col; // adjust stop when indents are not aligned (Python would give an error)
    }
  }
  if (ded_ > 0)
  {
    DBGLOG("Dedents: ded = %zu", ded_);
    if (col == 0 && bol)
    {
      ded_ += tab_.size();
      tab_.resize(0);
      DBGLOG("Rescan for pending dedents: ded = %zu", ded_);
      pos_ = ind_;
      bol = false; // avoid looping, match \j exactly
      goto redo;
    }
    --ded_;
  }
  if (method == Const::SPLIT)
  {
    DBGLOG("Split: len = %zu cap = %zu cur = %zu pos = %zu end = %zu txt-buf = %zu eob = %d", len_, cap_, cur_, pos_, end_, txt_-buf_, (int)eof_);
    if (cap_ == 0 || (cur_ == static_cast<size_t>(txt_ - buf_) && !bob))
    {
      if (!hit_end())
      {
        ++len_;
        DBGLOG("Split continue: len = %zu", len_);
        set_current(++cur_);
        goto redo;
      }
      cap_ = Const::EMPTY;
      set_current(pos_); // chr_ = static_cast<unsigned char>(buf_[pos_]);
      buf_[pos_] = '\0';
      DBGLOG("Split at eof: cap = %zu txt = '%s' len = %zu", cap_, txt_, len_);
      DBGLOG("END Matcher::match()");
      return cap_;
    }
    if (bob && cur_ == 0 && hit_end())
      cap_ = Const::EMPTY;
    set_current(cur_);
    buf_[txt_ - buf_ + len_] = '\0';
    DBGLOG("Split: txt = '%s' len = %zu", txt_, len_);
    DBGLOG("END Matcher::match()");
    return cap_;
  }
  len_ = cur_ - (txt_ - buf_);
  if (len_ == 0 && !nul)
  {
    DBGLOG("Empty match cur = %zu pos = %zu end = %zu", cur_, pos_, end_);
    pos_ = cur_;
    if (hit_end())
    {
      set_current(cur_);
      DBGLOG("Reject empty match at EOF");
      cap_ = 0;
    }
    else if (method == Const::FIND)
    {
      set_current(++cur_); // skip one unrecognized char
      DBGLOG("Reject and continue?");
      if (cap_ == 0 || !opt_.N)
        goto scan;
      DBGLOG("Accept empty match");
      buf_[pos_ - 1] = '\0';
    }
    else
    {
      set_current(cur_);
      DBGLOG("Reject empty match");
      cap_ = 0;
    }
  }
  else
  {
    if (cur_ == end_ && len_ == 0)
    {
      DBGLOG("Hit end: got = %d", got_);
      if (cap_ == Const::EMPTY && !opt_.A)
        cap_ = 0; // cannot goto scan?
    }
    else
    {
      set_current(cur_);
      if (len_ > 0)
      {
        if (cap_ == Const::EMPTY && !opt_.A)
        {
          DBGLOG("Ignore accept and continue: len = %zu", len_);
          if (method != Const::MATCH)
            goto scan;
          cap_ = 0;
        }
      }
    }
  }
  buf_[pos_] = '\0';
  DBGLOG("Return: cap = %zu txt = '%s' len = %zu got = %d", cap_, txt_, len_, got_);
  DBGLOG("END match()");
  return cap_;
}