void StereotypeDefinitionParser::parseToolbar() { Token token = d->m_scanner->read(); if (token.type() != Token::TokenIdentifier && token.type() != Token::TokenKeyword) throw StereotypeDefinitionParserError(QStringLiteral("Expected identifier."), token.sourcePos()); QString id = token.text(); Toolbar toolbar; toolbar.setId(id); parseToolbarProperties(&toolbar); token = readNextToken(); if (token.type() != Token::TokenKeyword || token.subtype() != KEYWORD_BEGIN) throw StereotypeDefinitionParserError(QStringLiteral("Expected token 'begin'."), token.sourcePos()); parseToolbarCommands(&toolbar); token = readNextToken(); if (token.type() != Token::TokenKeyword || token.subtype() != KEYWORD_END) throw StereotypeDefinitionParserError(QStringLiteral("Expected token 'end'."), token.sourcePos()); emit toolbarParsed(toolbar); }
int fillRedirectionOperands(char **input, char **output, int (*get_next_byte) (void *), void *get_next_byte_argument) { int len = 0; TOKENTYPE type = 0; while (1) { len = 0; type = getTokenType(get_next_byte(get_next_byte_argument)); switch (type) { case REDIRECTION1: type = readNextToken(input, &len, get_next_byte, get_next_byte_argument); if (!(*input) || !strlen(*input)) { goto err; } break; case REDIRECTION2: type = readNextToken(output, &len, get_next_byte, get_next_byte_argument); if (!(*output) || !strlen(*output)) { goto err; } break; default: _rewind(get_next_byte_argument, -1); goto out; } if (type == REDIRECTION2 || type == REDIRECTION1) { continue; } else { break; } } out: return type; err: printErr(); return -1; }
void Tokenizer::getTokens( const char * prog,tokenList_t & tokens) throw (Exception *) { int cursor=0; int line=1; size_t startPos = std::string::npos; Token * token; do { token=readNextToken(prog,cursor,line,startPos,tokens); if (token!=NULL) { token->setLine(line); token->setStaringPos(startPos); tokens.push_back(token); } } while (! Token::isA<TEndScript>(token) ); }
void Tokenizer::getTokens( const char * prog,tokenList & tokens) throw (Exception *) { int cursor=0; int line=1; Token * obj; do { obj=readNextToken(prog,cursor,line,tokens); if (obj!=NULL) { Token * t=dynamic_cast<Token*>(obj); if (t) t->setLine(line); tokens.push_back(obj); Token::addReference(obj); } } while (!dynamic_cast<TEndScript *>(obj)); }
void StereotypeDefinitionParser::parseToolbarCommands(Toolbar *toolbar) { Token token; QList<Toolbar::Tool> tools; bool loop = true; while (loop) { token = readNextToken(); if (token.type() != Token::TokenKeyword) { loop = false; } else { switch (token.subtype()) { case KEYWORD_TOOL: { QString toolName = parseStringExpression(); expectComma(); QString element = parseIdentifierExpression(); static QSet<QString> elementNames = QSet<QString>() << QStringLiteral("package") << QStringLiteral("component") << QStringLiteral("class") << QStringLiteral("item") << QStringLiteral("annotation") << QStringLiteral("boundary"); QString elementName = element.toLower(); if (!elementNames.contains(elementName)) throw StereotypeDefinitionParserError(QString(QStringLiteral("Unexpected value \"%1\" for element.")).arg(element), token.sourcePos()); QString stereotype; if (nextIsComma()) stereotype = parseStringExpression(); tools.append(Toolbar::Tool(toolName, element, stereotype)); expectSemicolonOrEndOfLine(); break; } case KEYWORD_SEPARATOR: tools.append(Toolbar::Tool()); expectSemicolonOrEndOfLine(); break; default: loop = false; break; } } } toolbar->setTools(tools); d->m_scanner->unread(token); }
void StereotypeDefinitionParser::parseFile() { for (;;) { Token token = readNextToken(); if (token.type() == Token::TokenEndOfInput) break; else if (token.type() != Token::TokenKeyword || token.subtype() == KEYWORD_ICON) parseIcon(); else if (token.type() != Token::TokenKeyword || token.subtype() == KEYWORD_TOOLBAR) parseToolbar(); else throw StereotypeDefinitionParserError(QStringLiteral("Expected 'icon' or 'toolbar'."), token.sourcePos()); token = d->m_scanner->read(); if (token.type() == Token::TokenOperator && token.subtype() == OPERATOR_PERIOD) break; else if (token.type() != Token::TokenOperator || token.subtype() != OPERATOR_SEMICOLON) d->m_scanner->unread(token); } }
QString PlSqlFormatter::format(const QString &code) { scanner = new PlSqlScanner(new StringReader(code)); QScopedPointer<PlSqlScanner> scopedScanner(scanner); //const static int selectKeyword = PlSqlParsingTable::getInstance()->getKeywordIx("SELECT"); do{ readNextToken(); if(!formatGenericConstruct()){ formatDefaultToken(); } //indents.clear(); }while(token != PLS_E_O_F); return result; }
void StereotypeDefinitionParser::parseToolbarProperties(Toolbar *toolbar) { Token token; bool loop = true; while (loop) { token = readNextToken(); if (token.type() != Token::TokenKeyword) { loop = false; } else { switch (token.subtype()) { case KEYWORD_PRIORITY: toolbar->setPriority(parseIntProperty()); break; default: loop = false; break; } } } d->m_scanner->unread(token); }
command_t makeCommandStreamUtil(int (*get_next_byte) (void *), void *get_next_byte_argument, STATE *state) { char **tokenPTR = checked_malloc(sizeof(char**)); char *token = NULL; int len = 0; TOKENTYPE type; command_t command = NULL; char *input = NULL, *output = NULL; type = readNextToken(tokenPTR, &len, get_next_byte, get_next_byte_argument); if (type == NOT_DEFINED) { free(tokenPTR); return NULL; } else if (type == O_PAR) { token = "("; } else { token = *tokenPTR; } command = AllocateCommand(); if (!command) { return NULL; } if (!strncmp(token, "then", 4)) { if (!(pop() == IF)) { printErr(); } push(THEN); *state = THEN; goto ret_null; } else if (!strncmp(token, "done", 4)) { if (!(pop() == DO)) { printErr(); } *state = DONE; CScount--; goto ret_null; } else if (!strncmp(token, "do", 4)) { STATE tmp = pop(); if (!((tmp == WHILE) || (tmp == UNTIL))) { printErr(); } push(DO); *state = DO; goto ret_null; } else if (!strncmp(token, "else", 4)) { if (!(pop() == THEN)) { printErr(); } push(ELSE); *state = ELSE; goto ret_null; } else if (!strncmp(token, "fi", 4)) { STATE tmp = pop(); if (!((tmp == THEN) || (tmp == ELSE))) { printErr(); } CScount--; *state = FI; goto ret_null; } else if (!strncmp(token, ")", 1)) { CScount--; *state = CLOSE_PAR; goto ret_null; } else if (!strncmp(token, "if", 2)) { push(IF); CScount++; command = makeCommand(command, NULL, IF_COMMAND, input, output); free(tokenPTR); command->u.command[0] = makeCommandStreamUtil(get_next_byte, get_next_byte_argument, state); while (*state != THEN) { if (!makeCommandStreamUtil(get_next_byte, get_next_byte_argument, state)) { type = NOT_DEFINED; break; } } if (type == NOT_DEFINED && *state != THEN) { return NULL; } command->u.command[1] = makeCommandStreamUtil(get_next_byte, get_next_byte_argument, state); if (*state != ELSE && *state != FI) { // HANDLE error; ; } else if (*state == ELSE || (*state == FI && CScount)) { command->u.command[2] = makeCommandStreamUtil(get_next_byte, get_next_byte_argument, state); } else { command->u.command[2] = NULL; } } else if (!strncmp(token, "while", 5)) { push(WHILE); (CScount)++; command = makeCommand(command, NULL, WHILE_COMMAND, input, output); free(tokenPTR); command->u.command[0] = makeCommandStreamUtil(get_next_byte, get_next_byte_argument, state); if (*state != DO) { // Handle Error ; } command->u.command[1] = makeCommandStreamUtil(get_next_byte, get_next_byte_argument, state); if (*state != DONE) { // HANDLE error; ; } else if (*state == DONE) { if (checkRedirection(get_next_byte, get_next_byte_argument)) { fillRedirectionOperands(&command->input, &command->output, get_next_byte, get_next_byte_argument); } command->u.command[2] = makeCommandStreamUtil(get_next_byte, get_next_byte_argument, state); /* if (command->u.command[2]) { */ /* command_t newCommand = makeCommand(NULL, NULL, SEQUENCE_COMMAND, */ /* NULL, NULL); */ /* newCommand->u.command[0] = command->u.command[1]; */ /* newCommand->u.command[1] = command->u.command[2]; */ /* command->u.command[1] = newCommand; */ /* command->u.command[2] = NULL; */ /* } */ } else { command->u.command[2] = NULL; } } else if (!strncmp(token, "until", 5)) { push(UNTIL); (CScount)++; command = makeCommand(command, NULL, UNTIL_COMMAND, input, output); free(tokenPTR); command->u.command[0] = makeCommandStreamUtil(get_next_byte, get_next_byte_argument, state); if (*state != DO) { // Handle Error ; } command->u.command[1] = makeCommandStreamUtil(get_next_byte, get_next_byte_argument, state); if (*state != DONE) { // HANDLE error; ; } else if (*state == DONE) { if (checkRedirection(get_next_byte, get_next_byte_argument)) { fillRedirectionOperands(&command->input, &command->output, get_next_byte, get_next_byte_argument); } command->u.command[2] = makeCommandStreamUtil(get_next_byte, get_next_byte_argument, state); /* if (command->u.command[2]) { */ /* command_t newCommand = makeCommand(NULL, NULL, SEQUENCE_COMMAND, */ /* NULL, NULL); */ /* newCommand->u.command[0] = command->u.command[1]; */ /* newCommand->u.command[1] = command->u.command[2]; */ /* command->u.command[1] = newCommand; */ /* command->u.command[2] = NULL; */ /* } */ } else { command->u.command[2] = NULL; } } else if (!strncmp(token, "(", 1)) { CScount++; command = makeCommand(command, NULL, SUBSHELL_COMMAND, input, output); free(tokenPTR); command->u.command[0] = makeCommandStreamUtil(get_next_byte, get_next_byte_argument, state); if (*state != CLOSE_PAR) { // Handle Error } else if (*state == CLOSE_PAR && CScount) { command->u.command[0] = makeCommandStreamUtil(get_next_byte, get_next_byte_argument, state); } } else { // SIMPLE_COMMAND while (1) { STATE prevState = *state; if (isKeyWordUpdate(token, state) && (prevState == COMMAND)) { removeWhiteSpace(token); command = makeSimpleCommand(command, tokenPTR, input, output); break; } if (type == REDIRECTION1 || type == REDIRECTION2) { type = fillRedirectionOperands(&input, &output, get_next_byte, get_next_byte_argument); // command = makeSimpleCommand(command, tokenPTR, input, output); // break; // type = readNextToken(tokenPTR, &len, get_next_byte, get_next_byte_argument); } else if (type == SPACE) { appendChar(token, ' '); type = readNextToken(tokenPTR, &len, get_next_byte, get_next_byte_argument); } else if (type == NEWLINE && !CScount) { command = makeSimpleCommand(command, tokenPTR, input, output); break; } else if (type == PIPE || type == SEMICOLON || type == NEWLINE) { removeWhiteSpace(token); if (((type == PIPE) || (type == SEMICOLON)) && !strlen(token)) { printErr(); } command = makeCommand(command, tokenPTR, type == PIPE ? PIPE_COMMAND : SEQUENCE_COMMAND, input, output); command->u.command[1] = makeCommandStreamUtil(get_next_byte, get_next_byte_argument, state); if (!command->u.command[1]) { command = convertToSimple(command); } break; } *state = COMMAND; } } return command; ret_null: free(command); free(tokenPTR); return NULL; }
void StereotypeDefinitionParser::parseIconCommands(StereotypeIcon *stereotypeIcon) { Token token; bool loop = true; IconShape iconShape; QList<ShapeValueF> parameters; typedef QList<IconCommandParameter> Parameters; static const IconCommandParameter SCALED(ShapeValueF::UnitScaled); static const IconCommandParameter FIX(ShapeValueF::UnitRelative); static const IconCommandParameter ABSOLUTE(ShapeValueF::UnitAbsolute); while (loop) { token = readNextToken(); if (token.type() != Token::TokenKeyword) { loop = false; } else { switch (token.subtype()) { case KEYWORD_CIRCLE: parameters = parseIconCommandParameters(Parameters() << SCALED << SCALED << SCALED); iconShape.addCircle(ShapePointF(parameters.at(0), parameters.at(1)), parameters.at(2)); expectSemicolonOrEndOfLine(); break; case KEYWORD_ELLIPSE: parameters = parseIconCommandParameters(Parameters() << SCALED << SCALED << SCALED << SCALED); iconShape.addEllipse(ShapePointF(parameters.at(0), parameters.at(1)), ShapeSizeF(parameters.at(2), parameters.at(3))); expectSemicolonOrEndOfLine(); break; case KEYWORD_LINE: parameters = parseIconCommandParameters(Parameters() << SCALED << SCALED << SCALED << SCALED); iconShape.addLine(ShapePointF(parameters.at(0), parameters.at(1)), ShapePointF(parameters.at(2), parameters.at(3))); expectSemicolonOrEndOfLine(); break; case KEYWORD_RECT: parameters = parseIconCommandParameters(Parameters() << SCALED << SCALED << SCALED << SCALED); iconShape.addRect(ShapePointF(parameters.at(0), parameters.at(1)), ShapeSizeF(parameters.at(2), parameters.at(3))); expectSemicolonOrEndOfLine(); break; case KEYWORD_ROUNDEDRECT: parameters = parseIconCommandParameters(Parameters() << SCALED << SCALED << SCALED << SCALED << FIX); iconShape.addRoundedRect(ShapePointF(parameters.at(0), parameters.at(1)), ShapeSizeF(parameters.at(2), parameters.at(3)), parameters.at(4)); expectSemicolonOrEndOfLine(); break; case KEYWORD_ARC: { parameters = parseIconCommandParameters( Parameters() << SCALED << SCALED << SCALED << SCALED << ABSOLUTE << ABSOLUTE); qreal startAngle = expectAbsoluteValue(parameters.at(4), d->m_scanner->sourcePos()); qreal spanAngle = expectAbsoluteValue(parameters.at(5), d->m_scanner->sourcePos()); iconShape.addArc(ShapePointF(parameters.at(0), parameters.at(1)), ShapeSizeF(parameters.at(2), parameters.at(3)), startAngle, spanAngle); expectSemicolonOrEndOfLine(); break; } case KEYWORD_MOVETO: parameters = parseIconCommandParameters(Parameters() << SCALED << SCALED); iconShape.moveTo(ShapePointF(parameters.at(0), parameters.at(1))); expectSemicolonOrEndOfLine(); break; case KEYWORD_LINETO: parameters = parseIconCommandParameters(Parameters() << SCALED << SCALED); iconShape.lineTo(ShapePointF(parameters.at(0), parameters.at(1))); expectSemicolonOrEndOfLine(); break; case KEYWORD_ARCMOVETO: { parameters = parseIconCommandParameters( Parameters() << SCALED << SCALED << SCALED << SCALED << ABSOLUTE); qreal angle = expectAbsoluteValue(parameters.at(4), d->m_scanner->sourcePos()); iconShape.arcMoveTo(ShapePointF(parameters.at(0), parameters.at(1)), ShapeSizeF(parameters.at(2), parameters.at(3)), angle); expectSemicolonOrEndOfLine(); break; } case KEYWORD_ARCTO: { parameters = parseIconCommandParameters( Parameters() << SCALED << SCALED << SCALED << SCALED << ABSOLUTE << ABSOLUTE); qreal startAngle = expectAbsoluteValue(parameters.at(4), d->m_scanner->sourcePos()); qreal sweepLength = expectAbsoluteValue(parameters.at(5), d->m_scanner->sourcePos()); iconShape.arcTo(ShapePointF(parameters.at(0), parameters.at(1)), ShapeSizeF(parameters.at(2), parameters.at(3)), startAngle, sweepLength); expectSemicolonOrEndOfLine(); break; } case KEYWORD_CLOSE: iconShape.closePath(); expectSemicolonOrEndOfLine(); break; default: loop = false; break; } } } stereotypeIcon->setIconShape(iconShape); d->m_scanner->unread(token); }
void StereotypeDefinitionParser::parseIconProperties(StereotypeIcon *stereotypeIcon) { Token token; bool loop = true; QSet<StereotypeIcon::Element> elements; QSet<QString> stereotypes; while (loop) { token = readNextToken(); if (token.type() != Token::TokenKeyword) { loop = false; } else { switch (token.subtype()) { case KEYWORD_TITLE: stereotypeIcon->setTitle(parseStringProperty()); expectSemicolonOrEndOfLine(); break; case KEYWORD_ELEMENTS: { QList<QString> identifiers = parseIdentifierListProperty(); foreach (const QString &identifier, identifiers) { static QHash<QString, StereotypeIcon::Element> elementNames = QHash<QString, StereotypeIcon::Element>() << qMakePair(QString(QStringLiteral("package")), StereotypeIcon::ElementPackage) << qMakePair(QString(QStringLiteral("component")), StereotypeIcon::ElementComponent) << qMakePair(QString(QStringLiteral("class")), StereotypeIcon::ElementClass) << qMakePair(QString(QStringLiteral("diagram")), StereotypeIcon::ElementDiagram) << qMakePair(QString(QStringLiteral("item")), StereotypeIcon::ElementItem); QString elementName = identifier.toLower(); if (!elementNames.contains(elementName)) throw StereotypeDefinitionParserError(QString(QStringLiteral("Unexpected value \"%1\" for element.")).arg(identifier), token.sourcePos()); elements.insert(elementNames.value(elementName)); } expectSemicolonOrEndOfLine(); break; } case KEYWORD_STEREOTYPE: stereotypes.insert(parseStringProperty()); expectSemicolonOrEndOfLine(); break; case KEYWORD_WIDTH: stereotypeIcon->setWidth(parseFloatProperty()); expectSemicolonOrEndOfLine(); break; case KEYWORD_HEIGHT: stereotypeIcon->setHeight(parseFloatProperty()); expectSemicolonOrEndOfLine(); break; case KEYWORD_MINWIDTH: stereotypeIcon->setMinWidth(parseFloatProperty()); expectSemicolonOrEndOfLine(); break; case KEYWORD_MINHEIGHT: stereotypeIcon->setMinHeight(parseFloatProperty()); expectSemicolonOrEndOfLine(); break; case KEYWORD_LOCK_SIZE: { QString lockValue = parseIdentifierProperty(); QString lockName = lockValue.toLower(); static QHash<QString, StereotypeIcon::SizeLock> lockNames = QHash<QString, StereotypeIcon::SizeLock>() << qMakePair(QString(QStringLiteral("none")), StereotypeIcon::LockNone) << qMakePair(QString(QStringLiteral("width")), StereotypeIcon::LockWidth) << qMakePair(QString(QStringLiteral("height")), StereotypeIcon::LockHeight) << qMakePair(QString(QStringLiteral("size")), StereotypeIcon::LockSize) << qMakePair(QString(QStringLiteral("ratio")), StereotypeIcon::LockRatio); if (lockNames.contains(lockName)) { StereotypeIcon::SizeLock sizeLock = lockNames.value(lockName); stereotypeIcon->setSizeLock(sizeLock); } else { throw StereotypeDefinitionParserError(QString(QStringLiteral("Unexpected value \"%1\" for size lock.")).arg(lockValue), token.sourcePos()); } break; } case KEYWORD_DISPLAY: { QString displayValue = parseIdentifierProperty(); QString displayName = displayValue.toLower(); static QHash<QString, StereotypeIcon::Display> displayNames = QHash<QString, StereotypeIcon::Display>() << qMakePair(QString(QStringLiteral("none")), StereotypeIcon::DisplayNone) << qMakePair(QString(QStringLiteral("label")), StereotypeIcon::DisplayLabel) << qMakePair(QString(QStringLiteral("decoration")), StereotypeIcon::DisplayDecoration) << qMakePair(QString(QStringLiteral("icon")), StereotypeIcon::DisplayIcon) << qMakePair(QString(QStringLiteral("smart")), StereotypeIcon::DisplaySmart); if (displayNames.contains(displayName)) { StereotypeIcon::Display display = displayNames.value(displayName); stereotypeIcon->setDisplay(display); } else { throw StereotypeDefinitionParserError(QString(QStringLiteral("Unexpected value \"%1\" for stereotype display.")).arg(displayValue), token.sourcePos()); } break; } case KEYWORD_TEXTALIGN: { QString alignValue = parseIdentifierProperty(); QString alignName = alignValue.toLower(); static QHash<QString, StereotypeIcon::TextAlignment> alignNames = QHash<QString, StereotypeIcon::TextAlignment>() << qMakePair(QString(QStringLiteral("below")), StereotypeIcon::TextalignBelow) << qMakePair(QString(QStringLiteral("center")), StereotypeIcon::TextalignCenter) << qMakePair(QString(QStringLiteral("none")), StereotypeIcon::TextalignNone); if (alignNames.contains(alignName)) { StereotypeIcon::TextAlignment textAlignment = alignNames.value(alignName); stereotypeIcon->setTextAlignment(textAlignment); } else { throw StereotypeDefinitionParserError(QString(QStringLiteral("Unexpected value \"%1\" for text alignment.")).arg(alignValue), token.sourcePos()); } break; } case KEYWORD_BASECOLOR: stereotypeIcon->setBaseColor(parseColorProperty()); expectSemicolonOrEndOfLine(); break; default: loop = false; break; } } } stereotypeIcon->setElements(elements); stereotypeIcon->setStereotypes(stereotypes); d->m_scanner->unread(token); }