Esempio n. 1
0
  /**
   * @param sql
   * @param alterTableDef
   * @return
   * @throws SqlJetException
   */
  private String getTableAlteredSql(String sql, SqlJetAlterTableDef alterTableDef)
      throws SqlJetException {

    final RuleReturnScope parsedSQL = parseTable(sql);
    final CommonTree ast = (CommonTree) parsedSQL.getTree();
    final CommonToken nameToken = (CommonToken) ((CommonTree) ast.getChild(1)).getToken();
    final CharStream inputStream = nameToken.getInputStream();
    final CommonToken stopToken = (CommonToken) parsedSQL.getStop();

    final StringBuilder b = new StringBuilder();

    if (alterTableDef.getNewTableName() != null) {
      b.append(inputStream.substring(0, nameToken.getStartIndex() - 1));
      b.append(getAlterTableName(alterTableDef));
      b.append(inputStream.substring(nameToken.getStopIndex() + 1, stopToken.getStopIndex()));
    } else if (alterTableDef.getNewColumnDef() != null) {
      b.append(inputStream.substring(0, stopToken.getStartIndex() - 1));
      b.append(",").append(getAlterTableName(alterTableDef));
      b.append(inputStream.substring(stopToken.getStartIndex(), stopToken.getStopIndex()));
    } else {
      throw new SqlJetException("Wrong ALTER TABLE statement");
    }

    return b.toString();
  }
Esempio n. 2
0
 private CommonToken getNextNonHiddenToken(int offset, int end) {
   int index = getTokenIndexAtCharacter(tokens, offset);
   if (index < 0) index = 1 - index;
   int size = getTokens().size();
   for (; index < size; index++) {
     CommonToken token = getTokens().get(index);
     if (token.getStartIndex() >= end) {
       return null;
     }
     if (token.getChannel() != HIDDEN_CHANNEL && token.getStartIndex() >= offset) {
       return token;
     }
   }
   return null;
 }
  private int getElementOffset(CommonTree tree) {
    switch (tree.getType()) {
      case ANTLRParser.MODE:
      case ANTLRParser.ASSIGN:
      case ANTLRParser.RULE:
        if (tree.getChildCount() > 0 && tree.getChild(0) instanceof CommonTree) {
          CommonTree child = (CommonTree) tree.getChild(0);
          if (child.getToken() instanceof CommonToken) {
            CommonToken token = (CommonToken) child.getToken();
            return token.getStartIndex();
          }
        }

        break;

      case ANTLRParser.ID:
        break;

      default:
        throw new UnsupportedOperationException();
    }

    if (tree.getToken() instanceof CommonToken) {
      return ((CommonToken) tree.getToken()).getStartIndex();
    }

    return 0;
  }
Esempio n. 4
0
 private int getStringOrCommentIndent(int offset) {
   CommonToken tokenContainingOffset = getTokenStrictlyContainingOffset(offset);
   CommonToken token = getStartOfStringToken(tokenContainingOffset);
   if (token != null) {
     int type = token.getType();
     int start = token.getCharPositionInLine();
     if (token.getStartIndex() < offset) {
       switch (type) {
         case STRING_LITERAL:
         case STRING_START:
         case ASTRING_LITERAL:
           return start + 1;
         case STRING_MID:
         case STRING_END:
         case MULTI_COMMENT:
           return start + 1;
           // uncomment to get a bigger indent
           //                    return start+3;
         case VERBATIM_STRING:
         case AVERBATIM_STRING:
           return start + 3;
       }
     }
   }
   return -1;
 }
 public void assertNonSmoking(final CharSequence input) throws Exception {
   final String string = input.toString();
   final List<CommonToken> tokenList = CollectionLiterals.<CommonToken>newArrayList();
   {
     final Lexer lexer = this.lexerProvider.get();
     ANTLRStringStream _aNTLRStringStream = new ANTLRStringStream(string);
     lexer.setCharStream(_aNTLRStringStream);
     Token token = lexer.nextToken();
     boolean _notEquals = (!Objects.equal(token, Token.EOF_TOKEN));
     boolean _while = _notEquals;
     while (_while) {
       {
         tokenList.add(((CommonToken) token));
         Token _nextToken = lexer.nextToken();
         token = _nextToken;
       }
       boolean _notEquals_1 = (!Objects.equal(token, Token.EOF_TOKEN));
       _while = _notEquals_1;
     }
   }
   for (final CommonToken token : tokenList) {
     {
       final int start = token.getStartIndex();
       String _text = token.getText();
       final int length = _text.length();
       String _substring = string.substring(0, start);
       String _substring_1 = string.substring((start + length));
       String _plus = (_substring + _substring_1);
       this.processFile(_plus);
     }
   }
 }
Esempio n. 6
0
 private String getCoreSQL(RuleReturnScope parsedSQL) {
   final CommonTree ast = (CommonTree) parsedSQL.getTree();
   final CommonToken nameToken = (CommonToken) ((CommonTree) ast.getChild(1)).getToken();
   final CharStream inputStream = nameToken.getInputStream();
   final CommonToken stopToken = (CommonToken) parsedSQL.getStop();
   return inputStream.substring(nameToken.getStartIndex(), stopToken.getStopIndex());
 }
Esempio n. 7
0
 /**
  * @param alterTableDef
  * @return
  */
 private String getAlterTableName(SqlJetAlterTableDef alterTableDef) {
   final ParserRuleReturnScope parsedSql = alterTableDef.getParsedSql();
   final CommonTree ast = (CommonTree) parsedSql.getTree();
   final CommonToken stopToken = (CommonToken) parsedSql.getStop();
   final CommonToken nameToken =
       (CommonToken) ((CommonTree) ast.getChild(ast.getChildCount() - 1)).getToken();
   final CharStream inputStream = nameToken.getInputStream();
   return inputStream.substring(nameToken.getStartIndex(), stopToken.getStopIndex());
 }
Esempio n. 8
0
 private int count(String token, int startIndex, int stopIndex) {
   int count = 0;
   List<CommonToken> tokens = getTokens();
   for (CommonToken tok : tokens) {
     if (tok.getStartIndex() >= startIndex
         && tok.getStopIndex() < stopIndex
         && tok.getText().equals(token)) {
       count++;
     }
   }
   return count;
 }
Esempio n. 9
0
 /**
  * @param sql
  * @param alterTableName
  * @return
  * @throws SqlJetException
  */
 private String getAlteredIndexSql(String sql, String alterTableName) throws SqlJetException {
   final RuleReturnScope parsedSQL = parseIndex(sql);
   final CommonTree ast = (CommonTree) parsedSQL.getTree();
   final CommonToken nameToken = (CommonToken) ((CommonTree) ast.getChild(2)).getToken();
   final CharStream inputStream = nameToken.getInputStream();
   final CommonToken stopToken = (CommonToken) parsedSQL.getStop();
   final StringBuilder b = new StringBuilder();
   b.append(inputStream.substring(0, nameToken.getStartIndex() - 1));
   b.append(alterTableName);
   b.append(inputStream.substring(nameToken.getStopIndex() + 1, stopToken.getStopIndex()));
   return b.toString();
 }
  /** Called 3rd */
  @Override
  public void apply(
      @NotNull PsiFile file,
      List<ANTLRv4ExternalAnnotator.Issue> issues,
      @NotNull AnnotationHolder holder) {
    for (int i = 0; i < issues.size(); i++) {
      Issue issue = issues.get(i);
      for (int j = 0; j < issue.offendingTokens.size(); j++) {
        Token t = issue.offendingTokens.get(j);
        if (t instanceof CommonToken) {
          CommonToken ct = (CommonToken) t;
          int startIndex = ct.getStartIndex();
          int stopIndex = ct.getStopIndex();
          TextRange range = new TextRange(startIndex, stopIndex + 1);
          ErrorSeverity severity = ErrorSeverity.INFO;
          if (issue.msg.getErrorType() != null) {
            severity = issue.msg.getErrorType().severity;
          }
          switch (severity) {
            case ERROR:
            case ERROR_ONE_OFF:
            case FATAL:
              holder.createErrorAnnotation(range, issue.annotation);
              break;

            case WARNING:
              holder.createWarningAnnotation(range, issue.annotation);
              break;

            case WARNING_ONE_OFF:
            case INFO:
              holder.createWeakWarningAnnotation(range, issue.annotation);

            default:
              break;
          }
        }
      }
    }
    super.apply(file, issues, holder);
  }
Esempio n. 11
0
 private void adjustStringOrCommentIndentation() throws BadLocationException {
   CommonToken tok = getTokenStrictlyContainingOffset(getEndOfCurrentLine());
   if (tok != null) {
     int len = command.length;
     String text = command.text;
     if (isQuoteOrCommentToken(tok.getType())
         && text != null
         && text.length() < len) { // reduced indent of a quoted or commented token
       String indent = document.get(command.offset, len);
       int line = document.getLineOfOffset(tok.getStartIndex()) + 1;
       int lastLine = document.getLineOfOffset(tok.getStopIndex());
       while (line <= lastLine) {
         int offset = document.getLineOffset(line);
         if (document.get(offset, len).equals(indent)) {
           document.replace(offset, len, text);
         }
         line++;
       }
     }
   }
 }
 private int[] start() {
   final CommonToken t = (CommonToken) input.LT(1);
   if (t == null) return new int[] {0, 0};
   return new int[] {t.getStartIndex(), t.getLine()};
 }