コード例 #1
0
ファイル: ParserMain.c プロジェクト: klenin/CATS-EasyGen
static ParserObjectAttrT* parseAttr(ParserObjectRecordT* curSeq)
{
    ParserObjectAttrT* res;
    int i, j, k;

    if (!curToken || curToken->type != ttAttrName) return 0;
    res = AllocateBuffer(sizeof(ParserObjectAttrT));
    memset(res,0,sizeof(ParserObjectAttrT));
    res->attrName = AllocateBuffer(strlen(curToken->str) + 1);
    strcpy(res->attrName, curToken->str);
    moveToNextToken();
    if (!chkCurToken('=')) {attrDestructor(res); return 0;}
    moveToNextToken();

    i = attrFind(res->attrName);
    if (i < 0) {
        genParseError(E_UNKNOWN_ATTRIBUTE);
        attrDestructor(res);
        return NULL;
    }

    if (!curToken) return NULL;
    switch (g_ParserObjectKind2AttrValueKind[i]) {
        case PARSER_OBJECT_ATTR_VALUE_IDENTIFIER:
            res->strVal = parseSingleToken(ttIdentifier);
            if (!res->strVal) {attrDestructor(res); return NULL;}
            break;
        case PARSER_OBJECT_ATTR_VALUE_INTEGER:
            res->exVal1 = parseExpression(curSeq);
            if (!res->exVal1) {attrDestructor(res); return NULL;}
            break;
        case PARSER_OBJECT_ATTR_VALUE_INTEGER_RANGE:
            parseIntRange(&res->exVal1, &res->exVal2, curSeq);
            if (!res->exVal1) {attrDestructor(res); return NULL;}
            break;
        case PARSER_OBJECT_ATTR_VALUE_CHARSET:
            res->strVal = parseSingleToken(ttCharSet);
            if (!res->strVal) {attrDestructor(res); return NULL;}
            if (isInCharSet(0, res->strVal) < 0) {
                genParseError(E_INVALID_CHARSET);
                attrDestructor(res); return NULL;
            }
            res->charNum = 0;
            memset(res->valid, 0, sizeof(res->valid));
            for (j = 0; j < charNumber; j++) {
                res->valid[j] = isInCharSet(j, res->strVal);
                if (res->valid[j]) res->charNum++;
            }
            res->charSetStr = AllocateBuffer(res->charNum+1);
            res->charSetStr[res->charNum] = 0; k = 0;
            for (j = 0; j < charNumber; j++)
                if (res->valid[j]) res->charSetStr[k++] = j;
            break;
        default:
            genParseError(B_ARRAY_ATTR_TYPE_BY_KIND);
            attrDestructor(res); return NULL;
    }
    if (ParserIsErrorRaised()) {attrDestructor(res); return NULL;}
    return res;
}
コード例 #2
0
ファイル: ParserMain.c プロジェクト: klenin/CATS-EasyGen
static ParserObjectAttrT* parseAttrList(
    ParserObjectKindT objKind,
    ParserObjectRecordT* curSeq
)
{
    //it should eat semicolons too
    ParserObjectAttrT *res = 0;
    ParserObjectAttrT *curAttr;
    int bad = 0, i, n = 0;
    int vis[PARSER_OBJECT_ATTRIBUTE_KINDS_COUNT];

    res = AllocateArray(PARSER_OBJECT_ATTRIBUTE_KINDS_COUNT,
        sizeof(ParserObjectAttrT));
    memset(vis, 0, sizeof(vis));

    while ((curAttr = parseAttr(curSeq))) {
        i = attrFind(curAttr->attrName);
        if (vis[i]) {
            genParseError(E_ATTRIBUTE_ALREADY_DEFINED);
            attrDestructor(curAttr); curAttr = 0;
            bad = 1; break;
        }
        if (!g_ParserObjectAttrKindIsValidForObjectKind[objKind][i]) {
            genParseError(E_INVALID_ATTRIBUTE);
            attrDestructor(curAttr); curAttr = 0;
            bad = 1; break;
        }
        vis[i] = 1; n++;
        copyAttrToAttr(&(res[i]), curAttr);
        attrDestructor(curAttr);

        if (chkCurToken(',')) {
            bad = 1; moveToNextToken();
        } else bad = 0;
    }

    if (ParserIsErrorRaised()) {attrListDestructor(res); return NULL;}
    if (n < getAttrCount(objKind)) bad = 1;
    if (bad) {
        attrListDestructor(res);
        genParseError(E_INVALID_ATTRIBUTE_LIST);
        return NULL;
    }
    if (!curToken || chkCurToken(';')) {
        if (curToken) moveToNextToken();
        return res;
    } else {attrListDestructor(res); return NULL;}
}
コード例 #3
0
ファイル: ParserMain.c プロジェクト: klenin/CATS-EasyGen
//-----------------------------initialize & finalize --------------------------
void initialize(const char* buf1)
{
    lastError = 0;
    buf = buf1;
    bufPos = pos = 0; line = 1;
    bufSize = strlen(buf);
    moveToNextToken();
}
コード例 #4
0
ファイル: ParserMain.c プロジェクト: klenin/CATS-EasyGen
static void parseIntRange(struct expr** ex1, struct expr** ex2, ParserObjectRecordT* curSeq)
{
    if (chkCurToken('[')) {
        moveToNextToken();
        *ex1 = parseExpression(curSeq);
        if (!*ex1 || !chkCurToken(',')) {
            exprDestructor(*ex1); *ex1 = *ex2 = 0; return;
        }
        moveToNextToken();
        *ex2 = parseExpression(curSeq);
        if (!*ex2 || !chkCurToken(']')) {
            exprDestructor(*ex1); exprDestructor(*ex2);
            *ex1 = *ex2 = 0; return;
        }
        moveToNextToken();
    } else {
        *ex1 = parseExpression(curSeq); *ex2 = 0;
    }
}
コード例 #5
0
ファイル: ParserMain.c プロジェクト: klenin/CATS-EasyGen
static char* parseSingleToken(int tokenType)
{
    char* res = NULL;
    if (curToken && curToken->type == tokenType) {
        res = AllocateBuffer(strlen(curToken->str) + 1);
        strcpy(res, curToken->str);
        moveToNextToken();
    } else {
        genParseError(E_UNEXPECTED_TOKEN);
    }
    return res;
}
コード例 #6
0
ファイル: ParserMain.c プロジェクト: klenin/CATS-EasyGen
static ParserObjectT* parseNextObject(ParserObjectRecordT* curSeq)
{
    ParserObjectKindT objKind;
    ParserObjectT* res;
    ParserObjectAttrT* attrList;
    char* name;
    ParserObjectRecordWithDataT tmp;

    if (!curToken) return NULL;
    if (curToken->type != ttObject) {
        genParseError(E_OBJECT_KIND_EXPECTED);
        return NULL;
    }
    objKind = curToken->value;
    moveToNextToken();

    attrList = parseAttrList(objKind, curSeq);

    if (attrList) {
        name = attrList[PARSER_OBJECT_ATTR_NAME].strVal;
        if (name) { // if name doesn't exist, than it's unnamed objKind
            tmp.pointerToData = 0; tmp.recPart = curSeq;
            if (ParserFindObject(name, tmp, 0).objPart) {
                genParseError(E_DUPLICATE_OBJECT);
                attrListDestructor(attrList);
                return NULL;
            }
        }
    }

    if (attrList ||
        objKind == PARSER_OBJECT_KIND_END ||
        objKind == PARSER_OBJECT_KIND_NEWLINE ||
        objKind == PARSER_OBJECT_KIND_SOFTLINE)
    {
            res = AllocateBuffer(sizeof(ParserObjectT));
            res->attrList = attrList; res->objKind = objKind;
            if (objKind == PARSER_OBJECT_KIND_SEQUENCE) {
                res->rec = parseObjRecord1(curSeq);
                if (!res->rec) {objDestructor(res); return 0;}
            } else res->rec = 0;
            if (ParserIsErrorRaised()) {objDestructor(res); return 0;}
            //res->parent = curSeq;  <- curSeq will reallocate anyway
            return res;
    }

    genParseError(E_INVALID_ATTRIBUTE_LIST);
    return NULL;
}
コード例 #7
0
void ScCodeEditor::keyPressEvent( QKeyEvent *e )
{
    hideMouseCursor(e);

    QTextCursor cursor( textCursor() );
    bool cursorMoved = true;

    if (e == QKeySequence::MoveToNextWord)
        moveToNextToken( cursor, QTextCursor::MoveAnchor );
    else if (e == QKeySequence::MoveToPreviousWord)
        moveToPreviousToken( cursor, QTextCursor::MoveAnchor );
    else if (e == QKeySequence::SelectNextWord)
        moveToNextToken( cursor, QTextCursor::KeepAnchor );
    else if (e == QKeySequence::SelectPreviousWord)
        moveToPreviousToken( cursor, QTextCursor::KeepAnchor );
    else
        cursorMoved = false;

    if (cursorMoved) {
        setTextCursor( cursor );
        return;
    }

    switch (e->key()) {
    case Qt::Key_Home:
    {
        Qt::KeyboardModifiers mods(e->modifiers());
        if (mods && mods != Qt::ShiftModifier) {
            GenericCodeEditor::keyPressEvent(e);
            return;
        }

        QTextCursor::MoveMode mode =
            mods & Qt::ShiftModifier ? QTextCursor::KeepAnchor : QTextCursor::MoveAnchor;

        QTextCursor c(textCursor());
        QTextBlock b(c.block());

        int pos = indentedStartOfLine(b);
        pos += b.position();

        if (c.position() == pos)
            c.movePosition(QTextCursor::StartOfLine, mode);
        else
            c.setPosition(pos, mode);

        setTextCursor(c);

        return;
    }

    case Qt::Key_Backtab:
    {
        QTextCursor cursor = textCursor();
        insertSpaceToNextTabStop( cursor );
        ensureCursorVisible();
        return;
    }
    case Qt::Key_Backspace:
        if (mInsertMatchingTokens && !overwriteMode() && e->modifiers() == 0)
            if (removeMatchingTokens())
                break;
        GenericCodeEditor::keyPressEvent(e);
        break;
    case Qt::Key_Enter:
    case Qt::Key_Return:
    {
        QTextBlock cursorBlock = cursor.block();
        int cursorPosInBlock = cursor.position() - cursorBlock.position();
        TokenIterator nextToken = TokenIterator::rightOf(cursorBlock, cursorPosInBlock);
        if ( nextToken.block() == cursorBlock && nextToken.type() == Token::ClosingBracket )
        {
            cursor.beginEditBlock();
            cursor.insertBlock();
            cursor.insertBlock();
            cursor.endEditBlock();
            cursor.movePosition( QTextCursor::PreviousBlock, QTextCursor::KeepAnchor );
            indent(cursor, JoinEditBlock);
            cursor.movePosition( QTextCursor::EndOfBlock );
        }
        else {
            cursor.beginEditBlock();
            cursor.insertBlock();
            cursor.endEditBlock();
            indent(cursor, JoinEditBlock);
        }
        cursor.setVerticalMovementX(-1);
        setTextCursor(cursor);
        break;
    }
    default:
        if (!overwriteMode() && insertMatchingTokens(e->text()))
            break;
        GenericCodeEditor::keyPressEvent(e);
    }

    mAutoCompleter->keyPress(e);
}
コード例 #8
0
ファイル: ParserMain.c プロジェクト: klenin/CATS-EasyGen
static struct expr* parseExpression1(int priority, int isFirst,
    ParserObjectRecordT* curSeq)
{
    struct expr *res, *op;
    ParserObjectT* varObj;
    ParserObjectRecordWithDataT tmp;
    char ch;
    if (priority > priorityNumber) {
        switch (curToken->type) {
            case ttIdentifier:
                res = AllocateBuffer(sizeof(struct expr));
                res->op1 = res->op2 = 0; res->opCode = 0;
                res->varName = AllocateBuffer(strlen(curToken->str) + 1);
                strcpy(res->varName, curToken->str);

                tmp.pointerToData = 0; tmp.recPart = curSeq;
                varObj = ParserFindObject(res->varName, tmp, 1).objPart;
                if (!varObj) genParseError(E_UNKNOWN_OBJECT);
                if (!ParserIsErrorRaised() && varObj->objKind != PARSER_OBJECT_KIND_INTEGER) genParseError(E_INTEGER_OBJECT_EXPECTED);
                if (ParserIsErrorRaised()) {exprDestructor(res); return 0;}
                moveToNextToken();
                if (ParserIsErrorRaised()) {exprDestructor(res); return 0;}
                return res;
            case ttInteger:
                res = AllocateBuffer(sizeof(struct expr));
                res->op1 = res->op2 = 0; res->opCode = 0; res->varName = 0;
                res->intConst = curToken->value;
                moveToNextToken();
                if (ParserIsErrorRaised()) {exprDestructor(res); return 0;}
                return res;
            case ttDelim:
                if (curToken->value == '(') {
                    moveToNextToken();
                    res = parseExpression1(1, 1, curSeq);
                    if (res && !chkCurToken(')')) {
                        genParseError(E_RB_EXPECTED);
                        exprDestructor(res);
                        return NULL;
                    }
                    moveToNextToken();
                    return res;
                }
                // don't need "break" here
            default:
                return NULL;
        }
    } else {
        op = parseExpression1(priority + 1, 1, curSeq);

        if (ParserIsErrorRaised()) {exprDestructor(op); return NULL;}
        if (!op) {
            if (priority == getOpPriority('+') && isFirst &&
                (chkCurToken('+') || chkCurToken('-'))) { // unary operations
                    op = AllocateBuffer(sizeof(struct expr));
                    op->op1 = op->op2 = 0; op->opCode = 0; op->varName = 0;
                    op->intConst = 0;
            } else {
                if (priority == getOpPriority('+')) {
                    genParseError(E_OPERAND_EXPECTED);
                }
                return NULL;
            }
        }

        if (chkCurToken('^')) {
            if (priority < getOpPriority('^')) {
                genParseError(B_UNEXPECTED_CAP);
                exprDestructor(op);
                return NULL;
            }
            res = AllocateBuffer(sizeof(struct expr));
            res->op1 = op; res->opCode = '^';
            res->varName = 0; res->intConst = 0;
            moveToNextToken();
            res->op2 = parseExpression1(priority, 0, curSeq);
            if (!res->op2) {exprDestructor(res->op2); return NULL;}
            op = res;
        } else {
            while ((chkCurToken('+') || chkCurToken('-') ||
                chkCurToken('*') || chkCurToken('/')) &&
                getOpPriority((int)curToken->value) == priority) {
                    ch = (char)curToken->value;
                    res = AllocateBuffer(sizeof(struct expr));
                    res->op1 = op; res->opCode = ch;
                    res->varName = 0; res->intConst = 0;
                    moveToNextToken();
                    res->op2 = parseExpression1(priority, 0, curSeq);
                    if (!res->op2) {exprDestructor(res); return NULL;}
                    op = res;
            }
        }
        if (ParserIsErrorRaised()) {exprDestructor(op); return NULL;}
        return op;
    }
}
コード例 #9
0
void ScCodeEditor::keyPressEvent( QKeyEvent *e )
{
    QTextCursor cursor( textCursor() );
    bool cursorMoved = true;
    if (e == QKeySequence::MoveToNextWord)
        moveToNextToken( cursor, QTextCursor::MoveAnchor );
    else if (e == QKeySequence::MoveToPreviousWord)
        moveToPreviousToken( cursor, QTextCursor::MoveAnchor );
    else if (e == QKeySequence::SelectNextWord)
        moveToNextToken( cursor, QTextCursor::KeepAnchor );
    else if (e == QKeySequence::SelectPreviousWord)
        moveToPreviousToken( cursor, QTextCursor::KeepAnchor );
    else
        cursorMoved = false;

    if (cursorMoved) {
        setTextCursor( cursor );
        return;
    }

    switch (e->key()) {
    case Qt::Key_Home:
    {
        Qt::KeyboardModifiers mods(e->modifiers());
        if (mods && mods != Qt::ShiftModifier) {
            GenericCodeEditor::keyPressEvent(e);
            return;
        }

        hideMouseCursor();

        QTextCursor::MoveMode mode =
            mods & Qt::ShiftModifier ? QTextCursor::KeepAnchor : QTextCursor::MoveAnchor;

        QTextCursor c(textCursor());
        QTextBlock b(c.block());

        int pos = indentedStartOfLine(b);
        pos += b.position();

        if (c.position() == pos)
            c.movePosition(QTextCursor::StartOfLine, mode);
        else
            c.setPosition(pos, mode);

        setTextCursor(c);

        return;
    }

    case Qt::Key_Backtab:
    {
        hideMouseCursor();
        QTextCursor cursor = textCursor();
        insertSpaceToNextTabStop( cursor );
        ensureCursorVisible();
        return;
    }
    default:
    {
        static const QString closingBrackets(")]}");

        bool indentCurrentLine =
                e->key() == Qt::Key_Enter ||
                e->key() == Qt::Key_Return ||
                ( !e->text().isEmpty() && closingBrackets.contains(e->text()) );

        if (indentCurrentLine)
            cursor.beginEditBlock();

        GenericCodeEditor::keyPressEvent(e);

        if (indentCurrentLine) {
            cursor.endEditBlock();
            cursor.joinPreviousEditBlock();
            indent();
            cursor.endEditBlock();
        }
    }
    }

    mAutoCompleter->keyPress(e);
}
コード例 #10
0
ファイル: sc_editor.cpp プロジェクト: acamari/supercollider
void ScCodeEditor::keyPressEvent( QKeyEvent *e )
{
    QTextCursor cursor( textCursor() );
    bool cursorMoved = true;
    if (e == QKeySequence::MoveToNextWord)
        moveToNextToken( cursor, QTextCursor::MoveAnchor );
    else if (e == QKeySequence::MoveToPreviousWord)
        moveToPreviousToken( cursor, QTextCursor::MoveAnchor );
    else if (e == QKeySequence::SelectNextWord)
        moveToNextToken( cursor, QTextCursor::KeepAnchor );
    else if (e == QKeySequence::SelectPreviousWord)
        moveToPreviousToken( cursor, QTextCursor::KeepAnchor );
    else
        cursorMoved = false;

    if (cursorMoved) {
        setTextCursor( cursor );
        return;
    }

    switch (e->key()) {
    case Qt::Key_Home:
    {
        Qt::KeyboardModifiers mods(e->modifiers());
        if (mods && mods != Qt::ShiftModifier) {
            GenericCodeEditor::keyPressEvent(e);
            return;
        }

        hideMouseCursor();

        QTextCursor::MoveMode mode =
            mods & Qt::ShiftModifier ? QTextCursor::KeepAnchor : QTextCursor::MoveAnchor;

        QTextCursor c(textCursor());
        QTextBlock b(c.block());

        int pos = indentedStartOfLine(b);
        pos += b.position();

        if (c.position() == pos)
            c.movePosition(QTextCursor::StartOfLine, mode);
        else
            c.setPosition(pos, mode);

        setTextCursor(c);

        return;
    }

    case Qt::Key_Backtab:
    {
        hideMouseCursor();
        QTextCursor cursor = textCursor();
        insertSpaceToNextTabStop( cursor );
        ensureCursorVisible();
        return;
    }

    case Qt::Key_Enter:
    case Qt::Key_Return:
    case Qt::Key_BraceRight:
    case Qt::Key_BracketRight:
    case Qt::Key_ParenRight: {
        hideMouseCursor();

        // Wrap superclass' implementation into an edit block,
        // so it can be joined with indentation later:

        QTextCursor cursor = textCursor();

        cursor.beginEditBlock();
        GenericCodeEditor::keyPressEvent(e);
        cursor.endEditBlock();

        cursor.joinPreviousEditBlock();
        indent();
        cursor.endEditBlock();

        cursor.setVerticalMovementX(-1);
        setTextCursor(cursor);

        break;
    }
    default:
        GenericCodeEditor::keyPressEvent(e);
    }

    mAutoCompleter->keyPress(e);
}
コード例 #11
0
ファイル: goodies.c プロジェクト: Oliviers-OSS/dbgflags
time_t parseDuration(const char *string) {
    /*
     * Allowed format:
     * jj hh:mn:ss
     *  hh:mn:ss
     *  nn
     *  nn s
     *  nn mn
     *  nn h
     *  nn j
     */

    durationParserStates state = init;
    time_t duration = 0;
    register const char *cursor = string;
    unsigned int n;
    while(*cursor != '\0') {
        cursor = parseInteger(cursor,&n);
        cursor = moveToNextToken(cursor);
        switch(*cursor) {
            case '\0':
                duration = duration * 60 + n;
                break;            
            case 's':
                duration += n;
                cursor++;
                break;
            case 'm':
                cursor++;
                if ('n' == *cursor) {
                    duration += n*60;
                    cursor++;
                } else {
                    WARNING_MSG("unknow time unit at m%s", cursor);
                    duration = 0;
                    cursor = moveToEnd(cursor);
                }                
                break;
            case 'h':
                duration += n*3600;
                cursor++;
                break;
            case 'j':
                duration += n*24*3600;
                cursor++;
                break;
            case ':':
                /* hh:mn:ss */
                switch(state) {
                    case init:
                        duration = n;
                        state = hours; /* hours read */
                        break;
                    case days:
                        duration = duration * 24  + n;
                        state = hours; /* hours read */
                        break;
                    case hours:
                        duration = duration * 60 + n;
                        state = minutes; /* minutes read */
                        break;
                    case minutes:
                        duration = (duration + n) * 60;
                        state = seconds; /* seconds read */
                    case seconds:
                        WARNING_MSG("bad string time format %s",string);
                        duration = 0;
                        cursor = moveToEnd(cursor) - 1;
                        break;
                } /* switch(state) */
                cursor++;
                break;
            default:
                if (isdigit(*cursor)) {
                    /* jj hh:mn:ss */
                    state = days; /* days read */
                    duration = n;
                } else {
                    WARNING_MSG("bad string format %s",string);
                    duration = 0;
                    cursor = moveToEnd(cursor) - 1;
                }
        } /* switch(*cursor) */
    } /*(cursor != '\0')*/

    return duration;
}