org.antlr.v4.runtime.tree.TerminalNodeImpl Java Examples

The following examples show how to use org.antlr.v4.runtime.tree.TerminalNodeImpl. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SqlParser.java    From crate with Apache License 2.0 6 votes vote down vote up
@Override
public void exitNonReserved(SqlBaseParser.NonReservedContext context) {
    // replace nonReserved words with IDENT tokens
    context.getParent().removeLastChild();

    Token token = (Token) context.getChild(0).getPayload();
    context.getParent().addChild(new TerminalNodeImpl(
        new CommonToken(
            new Pair<>(token.getTokenSource(), token.getInputStream()),
            SqlBaseLexer.IDENTIFIER,
            token.getChannel(),
            token.getStartIndex(),
            token.getStopIndex())
        )
    );
}
 
Example #2
Source File: Python3MethodParser.java    From Siamese with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Recursive depth first traversal that updates the current class node and extracts methods.
 * @param ruleContext RuleContext
 * @param methods List of Method extracted
 * @param classNode Current TerminalNodeImpl with class name that is the class
 *                  containing all methods traversed onwards from current iteration
 */
private void traverse(RuleContext ruleContext, ArrayList<Method> methods, TerminalNodeImpl classNode) {
    if (ruleContext.getRuleIndex() == Python3Parser.RULE_classdef) {
        classNode = getClassNode(ruleContext);
    }

    if (ruleContext.getRuleIndex() == Python3Parser.RULE_funcdef) {
        List<TerminalNodeImpl> terminalNodes = new ArrayList<>();
        // Add terminal nodes to list in place
        extract(ruleContext, methods, classNode, terminalNodes);
        Method method = createMethod(classNode, terminalNodes);
        methods.add(method);
    } else {
        for (int i = 0; i < ruleContext.getChildCount(); i++) {
            ParseTree parseTree = ruleContext.getChild(i);
            if (parseTree instanceof RuleContext) {
                traverse((RuleContext) parseTree, methods, classNode);
            }
        }
    }
}
 
Example #3
Source File: CustomChecksTest.java    From sonar-tsql-plugin with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testCheckEndingWIthSemicolon2() throws Throwable {
	Rule r = new Rule();
	RuleImplementation rImpl = new RuleImplementation();
	rImpl.getNames().getTextItem().add(Select_statementContext.class.getSimpleName());
	rImpl.setRuleMatchType(RuleMatchType.CLASS_ONLY);
	r.setRuleImplementation(rImpl);
	RuleImplementation child = new RuleImplementation();
	child.setDistance(1);
	child.setIndex(-1);
	child.setDistanceCheckType(RuleDistanceIndexMatchType.EQUALS);
	child.setIndexCheckType(RuleDistanceIndexMatchType.EQUALS);
	child.getTextToFind().getTextItem().add(";");
	child.setTextCheckType(TextCheckType.STRICT);
	child.setRuleMatchType(RuleMatchType.CLASS_ONLY);
	child.setRuleResultType(RuleResultType.FAIL_IF_NOT_FOUND);
	child.getNames().getTextItem().add(TerminalNodeImpl.class.getSimpleName());
	rImpl.getChildrenRules().getRuleImplementation().add(child);
	String s = "SELECT * from dbo.test where name like '%test%'";
	TsqlIssue[] issues = AntlrUtils.verify(r, s);
	Assert.assertEquals(1, issues.length);

}
 
Example #4
Source File: Python3MethodParser.java    From Siamese with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Reformats the input list of TerminalNodeImpl to form the source code String, where lexing the String
 * would return the tokens from the list. This includes the indentation structure which is recreated.
 * @param terminalNodes List of TerminalNodeImpl for a method
 * @return Source code of a method formed from the input list
 */
private String reformat(List<TerminalNodeImpl> terminalNodes) {
    StringBuilder stringBuilder = new StringBuilder();
    int indents = 0;
    int i = 0;
    Token prevToken = null;
    while (i < terminalNodes.size()) {
        TerminalNodeImpl terminalNodeImpl = terminalNodes.get(i);
        Token token = terminalNodeImpl.getSymbol();
        if (token.getType() == Python3Parser.NEWLINE) {
            stringBuilder.append(NEWLINE);
            while (i + 1 < terminalNodes.size()) {
                Token next = terminalNodes.get(i + 1).getSymbol();
                if (next.getType() == Python3Parser.INDENT) {
                    indents++;
                } else if (next.getType() == Python3Parser.DEDENT) {
                    indents--;
                } else {
                    break;
                }
                i++;
            }
            stringBuilder.append(StringUtils.repeat(TAB, indents));
        } else {
            if (prevToken == null ||
                prevToken.getType() == Python3Parser.NEWLINE ||
                prevToken.getType() == Python3Parser.INDENT ||
                prevToken.getType() == Python3Parser.DEDENT) {
                stringBuilder.append(token.getText());
            } else {
                stringBuilder.append(WHITESPACE).append(token.getText());
            }
        }
        i++;
        prevToken = token;
    }
    return stringBuilder.toString();
}
 
Example #5
Source File: HiveQLAnalyzer.java    From incubator-tajo with Apache License 2.0 5 votes vote down vote up
@Override
public Expr visitWhenExpression(HiveQLParser.WhenExpressionContext ctx) {
  CaseWhenPredicate caseWhen = new CaseWhenPredicate();
  Expr condition = null, result = null;
  for (int i = 1; i < ctx.getChildCount(); i++) {
    if (ctx.getChild(i) instanceof TerminalNodeImpl) {
      if (((TerminalNodeImpl) ctx.getChild(i)).getSymbol().getType() == HiveQLLexer.KW_WHEN) {
        condition = null;
        result = null;

        if (ctx.getChild(i + 1) instanceof HiveQLParser.ExpressionContext) {
          condition = visitExpression((HiveQLParser.ExpressionContext) ctx.getChild(i + 1));
        }

        if (ctx.getChild(i + 3) instanceof HiveQLParser.ExpressionContext) {
          result = visitExpression((HiveQLParser.ExpressionContext) ctx.getChild(i + 3));
        }

        if (condition != null && result != null) {
          caseWhen.addWhen(condition, result);
        }
      } else if (((TerminalNodeImpl) ctx.getChild(i)).getSymbol().getType() == HiveQLLexer.KW_ELSE) {
        result = visitExpression((HiveQLParser.ExpressionContext) ctx.getChild(i + 1));
        caseWhen.setElseResult(result);
      }
    }
  }

  return caseWhen;
}
 
Example #6
Source File: HiveQLAnalyzer.java    From incubator-tajo with Apache License 2.0 5 votes vote down vote up
@Override
public Expr visitCaseExpression(HiveQLParser.CaseExpressionContext ctx) {
  CaseWhenPredicate caseWhen = new CaseWhenPredicate();
  Expr condition = null, result = null;
  for (int i = 1; i < ctx.getChildCount(); i++) {
    if (ctx.getChild(i) instanceof TerminalNodeImpl) {
      if (((TerminalNodeImpl) ctx.getChild(i)).getSymbol().getType() == HiveQLLexer.KW_WHEN) {
        condition = null;
        result = null;

        if (ctx.getChild(i + 1) instanceof HiveQLParser.ExpressionContext) {
          condition = visitExpression((HiveQLParser.ExpressionContext) ctx.getChild(i + 1));
        }

        if (ctx.getChild(i + 3) instanceof HiveQLParser.ExpressionContext) {
          result = visitExpression((HiveQLParser.ExpressionContext) ctx.getChild(i + 3));
        }

        if (condition != null && result != null) {
          caseWhen.addWhen(condition, result);
        }
      } else if (((TerminalNodeImpl) ctx.getChild(i)).getSymbol().getType() == HiveQLLexer.KW_ELSE) {
        result = visitExpression((HiveQLParser.ExpressionContext) ctx.getChild(i + 1));
        caseWhen.setElseResult(result);
      }
    }
  }

  return caseWhen;
}
 
Example #7
Source File: AbstractJavaListener.java    From jd-gui with GNU General Public License v3.0 5 votes vote down vote up
protected int countDimension(List<ParseTree> children) {
    int dimension = 0;

    for (ParseTree child : children) {
        if (child instanceof TerminalNodeImpl) {
            if (((TerminalNodeImpl)child).getSymbol().getType() == JavaParser.LBRACK)
                dimension++;
        }
    }

    return dimension;
}
 
Example #8
Source File: CustomChecksTest.java    From sonar-tsql-plugin with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testCheckEndingWithSemicolon() throws Throwable {
	Rule r = new Rule();
	r.setKey("Example1");
	r.setInternalKey("Example1");
	r.setDescription("Select statement should end with semicolon");
	r.setName("Select statement should end with semicolon");
	RuleImplementation rImpl = new RuleImplementation();
	rImpl.getNames().getTextItem().add(Select_statementContext.class.getSimpleName());
	rImpl.setRuleMatchType(RuleMatchType.CLASS_ONLY);
	r.setRuleImplementation(rImpl);
	RuleImplementation child = new RuleImplementation();
	child.setDistance(1);
	child.setIndex(-1);
	child.setDistanceCheckType(RuleDistanceIndexMatchType.EQUALS);
	child.setIndexCheckType(RuleDistanceIndexMatchType.EQUALS);
	child.getTextToFind().getTextItem().add(";");
	child.setTextCheckType(TextCheckType.STRICT);
	child.setRuleMatchType(RuleMatchType.TEXT_AND_CLASS);
	child.setRuleResultType(RuleResultType.FAIL_IF_NOT_FOUND);
	child.getNames().getTextItem().add(TerminalNodeImpl.class.getSimpleName());
	rImpl.getChildrenRules().getRuleImplementation().add(child);
	rImpl.getCompliantRulesCodeExamples().getRuleCodeExample()
			.add("SELECT * from dbo.test where name like '%test%';");
	rImpl.getViolatingRulesCodeExamples().getRuleCodeExample()
			.add("SELECT * from dbo.test where name like '%test%'");

	String s = "SELECT * from dbo.test where name like '%test%';";
	TsqlIssue[] issues = AntlrUtils.verify(r, s);

	Assert.assertEquals(0, issues.length);

}
 
Example #9
Source File: CustomPluginChecks.java    From sonar-tsql-plugin with GNU General Public License v3.0 5 votes vote down vote up
public static Rule getOrderByRuleWithoutAscDesc() {
	Rule rule = new Rule();
	rule.setKey("C017");
	rule.setInternalKey("C017");
	rule.setName("ORDER BY clause does not contain order (ASC/DESC)");
	rule.setDescription("<h2>Description</h2><p>It is advisable to specidy order how rows should be ordered.</p>");
	rule.setTag("understanding");
	rule.setSeverity("MINOR");
	rule.setRemediationFunction("LINEAR");
	rule.setDebtRemediationFunctionCoefficient("1min");
	RuleImplementation child2 = new RuleImplementation();
	child2.getNames().getTextItem().add(TerminalNodeImpl.class.getSimpleName());
	child2.getTextToFind().getTextItem().add("asc");
	child2.getTextToFind().getTextItem().add("desc");
	child2.setTextCheckType(TextCheckType.STRICT);
	child2.setRuleResultType(RuleResultType.FAIL_IF_NOT_FOUND);
	child2.setRuleMatchType(RuleMatchType.TEXT_AND_CLASS);

	RuleImplementation impl = new RuleImplementation();

	impl.getChildrenRules().getRuleImplementation().add(child2);
	impl.getNames().getTextItem().add(Order_by_expressionContext.class.getSimpleName());
	impl.setRuleMatchType(RuleMatchType.CLASS_ONLY);
	impl.setRuleResultType(RuleResultType.DEFAULT);
	impl.setRuleViolationMessage("No ASC/DESC order defined in an ORDER BY clause.");
	impl.getViolatingRulesCodeExamples().getRuleCodeExample()
			.add("SELECT name, surname from dbo.test order by name, surname asc;");
	impl.getCompliantRulesCodeExamples().getRuleCodeExample()
			.add("SELECT name, surname from dbo.test order by name desc, surname asc;");

	rule.setRuleImplementation(impl);
	return rule;
}
 
Example #10
Source File: CustomPluginChecks.java    From sonar-tsql-plugin with GNU General Public License v3.0 5 votes vote down vote up
public static Rule getUnionVsUnionALLRule() {
	Rule r = new Rule();
	r.setKey("C015");
	r.setInternalKey("C015");
	r.setDescription(
			"<h2>Description</h2><p>It is  advisable to consider using UNION ALL operator instead of UNION.</p>");
	r.setSeverity("MAJOR");
	r.setTag("performance");
	r.setRemediationFunction("LINEAR");
	r.setDebtRemediationFunctionCoefficient("3min");
	r.setName("UNION operator is used");

	RuleImplementation rImpl = new RuleImplementation();
	rImpl.getNames().getTextItem().add(Sql_unionContext.class.getSimpleName());
	rImpl.setRuleViolationMessage(
			"Consider using UNION ALL instead of UNION operator for the performance reasons.");
	rImpl.setRuleMatchType(RuleMatchType.CLASS_ONLY);
	r.setRuleImplementation(rImpl);
	RuleImplementation child = new RuleImplementation();
	child.getTextToFind().getTextItem().add("all");
	child.setTextCheckType(TextCheckType.STRICT);
	child.setRuleMatchType(RuleMatchType.TEXT_AND_CLASS);
	child.setRuleResultType(RuleResultType.FAIL_IF_NOT_FOUND);
	child.getNames().getTextItem().add(TerminalNodeImpl.class.getSimpleName());

	rImpl.getChildrenRules().getRuleImplementation().add(child);
	rImpl.getCompliantRulesCodeExamples().getRuleCodeExample()
			.add("SELECT name, surname, count from dbo.test union all SELECT name, surname, count from dbo.test2;");
	rImpl.getViolatingRulesCodeExamples().getRuleCodeExample()
			.add("SELECT name, surname, count from dbo.test union SELECT name, surname, count from dbo.test2;");
	r.setSource("https://dzone.com/articles/sql-handbook-and-best-practices-performance-tuning");
	return r;
}
 
Example #11
Source File: CustomPluginChecks.java    From sonar-tsql-plugin with GNU General Public License v3.0 5 votes vote down vote up
public static Rule getWhereWithOrVsUnionRule() {
	Rule r = new Rule();
	r.setKey("C014");
	r.setInternalKey("C014");
	r.setDescription(
			"<h2>Description</h2><p>It is  advisable to consider using UNION/UNION ALL operator instead of OR verb in the WHERE clause.</p>");
	r.setSeverity("MAJOR");
	r.setTag("performance");
	r.setRemediationFunction("LINEAR");
	r.setDebtRemediationFunctionCoefficient("3min");
	r.setName("OR verb is used in a WHERE clause");
	RuleImplementation rImpl = new RuleImplementation();
	rImpl.getNames().getTextItem().add(Search_conditionContext.class.getSimpleName());
	rImpl.setRuleMatchType(RuleMatchType.CLASS_ONLY);
	rImpl.setRuleViolationMessage("Consider using UNION ALL instead of OR in a WHERE clause.");
	r.setRuleImplementation(rImpl);
	RuleImplementation child = new RuleImplementation();
	child.getTextToFind().getTextItem().add("or");
	child.setTextCheckType(TextCheckType.STRICT);
	child.setRuleMatchType(RuleMatchType.TEXT_AND_CLASS);
	child.setRuleResultType(RuleResultType.FAIL_IF_FOUND);
	child.getNames().getTextItem().add(TerminalNodeImpl.class.getSimpleName());

	rImpl.getChildrenRules().getRuleImplementation().add(child);
	rImpl.getCompliantRulesCodeExamples().getRuleCodeExample()
			.add("SELECT name, surname, count from dbo.test where name = 'or' and surname = 'TestOR';");
	rImpl.getViolatingRulesCodeExamples().getRuleCodeExample()
			.add("SELECT name, surname, count from dbo.test where name = 'Test' OR surname = 'Testor';");
	r.setSource("https://dzone.com/articles/sql-handbook-and-best-practices-performance-tuning");
	return r;
}
 
Example #12
Source File: CustomPluginChecks.java    From sonar-tsql-plugin with GNU General Public License v3.0 5 votes vote down vote up
public static Rule getSemicolonRule() {
	Rule r = new Rule();
	r.setKey("Example1");
	r.setInternalKey("Example1");
	r.setDescription("Select statement should end with semicolon");
	r.setName("Select statement should end with semicolon");
	RuleImplementation rImpl = new RuleImplementation();
	rImpl.getNames().getTextItem().add(Select_statementContext.class.getSimpleName());
	rImpl.setRuleMatchType(RuleMatchType.CLASS_ONLY);
	r.setRuleImplementation(rImpl);
	RuleImplementation child = new RuleImplementation();
	child.setDistance(1);
	child.setIndex(-1);
	child.setDistanceCheckType(RuleDistanceIndexMatchType.EQUALS);
	child.setIndexCheckType(RuleDistanceIndexMatchType.EQUALS);
	child.getTextToFind().getTextItem().add(";");
	child.setTextCheckType(TextCheckType.STRICT);
	child.setRuleMatchType(RuleMatchType.TEXT_AND_CLASS);
	child.setRuleResultType(RuleResultType.FAIL_IF_NOT_FOUND);
	child.getNames().getTextItem().add(TerminalNodeImpl.class.getSimpleName());
	rImpl.getChildrenRules().getRuleImplementation().add(child);
	rImpl.getCompliantRulesCodeExamples().getRuleCodeExample()
			.add("SELECT * from dbo.test where name like '%test%';");
	rImpl.getViolatingRulesCodeExamples().getRuleCodeExample()
			.add("SELECT * from dbo.test where name like '%test%'");
	return r;
}
 
Example #13
Source File: Select_or_valuesGenerator.java    From antsdb with GNU Lesser General Public License v3.0 5 votes vote down vote up
private static RuleContext createColumnName_(Expr_functionContext rule, OutputField field) {
    Column_name_Context column_name_ = new Column_name_Context(rule.getParent(), rule.invokingState);
    Column_nameContext column_name = new Column_nameContext(column_name_.getParent(), rule.invokingState);
    IdentifierContext identifier = new IdentifierContext(column_name, rule.invokingState);
    CommonToken token = CommonTokenFactory.DEFAULT.create(
            MysqlParser.BACKTICK_QUOTED_IDENTIFIER, 
            '`' + field.name + '`' );
    TerminalNode term = new TerminalNodeImpl(token);
    identifier.addChild(term);
    column_name.addChild(identifier);
    column_name_.addChild(column_name);
    return column_name_;
}
 
Example #14
Source File: SimpleDatasetRepositoryConnection.java    From mobi with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void enterConstructQuery(Sparql11Parser.ConstructQueryContext ctx) {
    // Handle short CONSTRUCT WHERE {...} case
    if (ctx.constructTemplate() == null) {
        constructWhere = true;
        // If there is no dataset clause, insert
        if (ctx.datasetClause() == null || ctx.datasetClause().size() == 0) {
            rewriter.insertBefore(((TerminalNodeImpl) ctx.getChild(1)).getSymbol(), getDatasetClause());
        }
    }
}
 
Example #15
Source File: Python3MethodParser.java    From Siamese with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Iterative depth first traversal of the root RuleContext to find all TerminalNodeImpl
 * (in the order of ascending file and token position as in the original source code)
 * which are then used to create a file-level method.
 * @param ruleContext Root RuleContext of the parsed file
 * @return {@link crest.siamese.document.Method}
 */
private Method createFileMethod(RuleContext ruleContext) {
    List<TerminalNodeImpl> terminalNodes = new ArrayList<>();

    Stack<ParseTree> stack = new Stack<>();
    stack.push(ruleContext);

    while (!stack.isEmpty()) {
        ParseTree curr = stack.pop();
        if (curr instanceof RuleContext) {
            // First child would be at top of stack
            for (int i = curr.getChildCount() - 1; i >= 0; i--) {
                stack.push(curr.getChild(i));
            }
        } else if (curr instanceof TerminalNodeImpl) {
            terminalNodes.add((TerminalNodeImpl) curr);
        }
    }

    int startLine = terminalNodes.get(0).getSymbol().getLine();
    int endLine = terminalNodes.get(terminalNodes.size() - 1).getSymbol().getLine();
    List<Parameter> params = new ArrayList<>();
    String src = reformat(terminalNodes);

    return new Method(FILE_PATH, StringUtils.EMPTY, StringUtils.EMPTY, StringUtils.EMPTY,
            StringUtils.EMPTY, src, startLine, endLine, params, StringUtils.EMPTY);
}
 
Example #16
Source File: Python3MethodParser.java    From Siamese with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Creates a Method given the class node (null for global methods) and the list of TerminalNodeImpl for that
 * function. Loops through terminalNodes to find function name, function parameters, function headers.
 * @param classNode TerminalNodeImpl of the class name
 * @param terminalNodes List of TerminalNodeImpl of a method
 * @return {@link crest.siamese.document.Method}
 */
private Method createMethod(TerminalNodeImpl classNode, List<TerminalNodeImpl> terminalNodes) {
    String className = classNode == null ? StringUtils.EMPTY : classNode.getText();
    String functionName = null;
    int startLine = terminalNodes.get(0).getSymbol().getLine();
    int endLine = terminalNodes.get(terminalNodes.size() - 1).getSymbol().getLine();
    List<Parameter> params = new ArrayList<>();
    StringBuilder header = new StringBuilder();
    for (TerminalNodeImpl terminalNodeImpl: terminalNodes) {
        Token token = terminalNodeImpl.getSymbol();
        RuleContext parent = (RuleContext) terminalNodeImpl.getParent();

        // Function name
        if (functionName == null && token.getType() == Python3Parser.NAME) {
            functionName = token.getText();
        }

        // Function parameters
        if (token.getType() == Python3Parser.NAME && parent.getRuleIndex() == Python3Parser.RULE_tfpdef) {
            params.add(new Parameter(StringUtils.EMPTY, token.getText()));
        }

        // Function header, append space after "def" keyword
        header.append(header.length() == 0 ? token.getText() + WHITESPACE : token.getText());

        // Outside of function header, no further iteration required
        if (token.getType() == Python3Parser.COLON && parent.getRuleIndex() == Python3Parser.RULE_funcdef) {
            break;
        }
    }
    String src = reformat(terminalNodes);
    return new Method(FILE_PATH, StringUtils.EMPTY, className, functionName,
            StringUtils.EMPTY, src, startLine, endLine, params, header.toString());
}
 
Example #17
Source File: Python3MethodParser.java    From Siamese with GNU General Public License v3.0 5 votes vote down vote up
/**
 * The method is called when the traverse method encounters a function definition RuleContext. Extracts the list of
 * TerminalNodeImpl of the input RuleContext. If an inner function definition is found, that RuleContext will be
 * traversed and extracted as a separate method. Method extraction excludes the async keyword (for asynchronous
 * function definition) and function decorators.
 * @param ruleContext Starting RuleContext of a function definition
 * @param methods List of Method extracted
 * @param classNode Current TerminalNodeImpl with class name that is the class
 *                  containing all methods traversed onwards from current iteration
 * @param terminalNodes List of TerminalNodeImpl of a method being extracted
 */
private void extract(RuleContext ruleContext, ArrayList<Method> methods,
                     TerminalNodeImpl classNode, List<TerminalNodeImpl> terminalNodes) {
    for (int i = 0; i < ruleContext.getChildCount(); i++) {
        ParseTree parseTree = ruleContext.getChild(i);
        if (parseTree instanceof RuleContext) {
            RuleContext childRuleContext = (RuleContext) parseTree;
            if (childRuleContext.getRuleIndex() == Python3Parser.RULE_funcdef) {
                // Inner function definition within a function
                traverse(childRuleContext, methods, classNode);
                // Remove async keyword if inner function is asynchronous
                RuleContext parent = (RuleContext) terminalNodes.get(terminalNodes.size() - 1).getParent();
                if (parent.getRuleIndex() == Python3Parser.RULE_async_funcdef
                        || parent.getRuleIndex() == Python3Parser.RULE_async_stmt) {
                    terminalNodes.remove(terminalNodes.size() - 1);
                }
            } else {
                // Continue extracting terminal nodes from current function
                // Ignore function decorators of inner function
                if (childRuleContext.getRuleIndex() != Python3Parser.RULE_decorators) {
                    extract(childRuleContext, methods, classNode, terminalNodes);
                }
            }
        } else if (parseTree instanceof TerminalNodeImpl) {
            TerminalNodeImpl terminalNode = (TerminalNodeImpl) parseTree;
            terminalNodes.add(terminalNode);
        }
    }
}
 
Example #18
Source File: CustomPluginChecks.java    From sonar-tsql-plugin with GNU General Public License v3.0 4 votes vote down vote up
public static Rule getIndexNamingRule() {
	RuleImplementation ruleImpl = new RuleImplementation();
	ruleImpl.getNames().getTextItem().add(Create_indexContext.class.getSimpleName());
	ruleImpl.setRuleMatchType(RuleMatchType.CLASS_ONLY);

	RuleImplementation child1 = new RuleImplementation();
	child1.getTextToFind().getTextItem().add("INDEX");
	child1.getNames().getTextItem().add(TerminalNodeImpl.class.getSimpleName());
	child1.setRuleResultType(RuleResultType.SKIP_IF_NOT_FOUND);
	child1.setRuleMatchType(RuleMatchType.TEXT_AND_CLASS);

	ruleImpl.getChildrenRules().getRuleImplementation().add(child1);
	ruleImpl.setRuleMatchType(RuleMatchType.TEXT_AND_CLASS);

	RuleImplementation main = new RuleImplementation();
	main.getNames().getTextItem().add(IdContext.class.getSimpleName());
	main.setTextCheckType(TextCheckType.CONTAINS);
	main.getTextToFind().getTextItem().add("IX_");
	main.setRuleMatchType(RuleMatchType.TEXT_AND_CLASS);
	main.setRuleResultType(RuleResultType.FAIL_IF_NOT_FOUND);
	main.setRuleViolationMessage("Index name is not defined using suggested naming convention to start with IX_.");
	main.setDistanceCheckType(RuleDistanceIndexMatchType.EQUALS);

	ruleImpl.getChildrenRules().getRuleImplementation().add(main);
	ruleImpl.getViolatingRulesCodeExamples().getRuleCodeExample()
			.add("CREATE UNIQUE INDEX Test_Name on dbo.test (Name);");
	ruleImpl.getCompliantRulesCodeExamples().getRuleCodeExample()
			.add("CREATE UNIQUE INDEX IX_Test_Name on dbo.test (Name);");

	Rule rule = new Rule();
	rule.setKey("C013");
	rule.setInternalKey("C013");
	rule.setStatus("BETA");
	rule.setName("Defined index name is not using recommended naming convention");
	rule.setDescription(
			"<h2>Description</h2><p>Defined index name is not using recommended naming convention to start with IX_.</p>");
	rule.setTag("naming");
	rule.setSeverity("MINOR");
	rule.setRemediationFunction("LINEAR");
	rule.setDebtRemediationFunctionCoefficient("3min");
	rule.setRuleImplementation(ruleImpl);

	return rule;
}
 
Example #19
Source File: CustomPluginChecks.java    From sonar-tsql-plugin with GNU General Public License v3.0 4 votes vote down vote up
public static Rule getFKRule() {
	RuleImplementation ruleImpl = new RuleImplementation();
	ruleImpl.getNames().getTextItem().add(Table_constraintContext.class.getSimpleName());
	ruleImpl.setRuleMatchType(RuleMatchType.CLASS_ONLY);

	RuleImplementation child1 = new RuleImplementation();
	child1.getTextToFind().getTextItem().add("FOREIGN");
	child1.getNames().getTextItem().add(TerminalNodeImpl.class.getSimpleName());
	child1.setRuleResultType(RuleResultType.SKIP_IF_NOT_FOUND);
	child1.setRuleMatchType(RuleMatchType.TEXT_AND_CLASS);

	RuleImplementation child2 = new RuleImplementation();
	child2.getTextToFind().getTextItem().add("KEY");
	child2.getNames().getTextItem().add(TerminalNodeImpl.class.getSimpleName());
	child2.setRuleResultType(RuleResultType.SKIP_IF_NOT_FOUND);
	child2.setRuleMatchType(RuleMatchType.TEXT_AND_CLASS);
	ruleImpl.getChildrenRules().getRuleImplementation().add(child1);
	ruleImpl.getChildrenRules().getRuleImplementation().add(child2);
	ruleImpl.setRuleMatchType(RuleMatchType.TEXT_AND_CLASS);

	RuleImplementation main = new RuleImplementation();
	main.getNames().getTextItem().add(IdContext.class.getSimpleName());
	main.setTextCheckType(TextCheckType.CONTAINS);
	main.getTextToFind().getTextItem().add("FK_");
	main.setRuleMatchType(RuleMatchType.TEXT_AND_CLASS);
	main.setRuleResultType(RuleResultType.FAIL_IF_NOT_FOUND);
	main.setRuleViolationMessage("Foreign key is not defined using suggested naming convention to start with FK_.");
	main.setDistanceCheckType(RuleDistanceIndexMatchType.EQUALS);

	ruleImpl.getChildrenRules().getRuleImplementation().add(main);
	ruleImpl.getViolatingRulesCodeExamples().getRuleCodeExample().add(
			"ALTER TABLE dbo.Orders ADD CONSTRAINT ClientId FOREIGN KEY (ClientId) REFERENCES dbo.Clients(Id);  ");
	ruleImpl.getCompliantRulesCodeExamples().getRuleCodeExample().add(
			"ALTER TABLE dbo.Orders ADD CONSTRAINT FK_ClientId FOREIGN KEY (ClientId) REFERENCES dbo.Clients(Id); ");

	Rule rule = new Rule();
	rule.setKey("C011");
	rule.setInternalKey("C011");
	rule.setStatus("BETA");
	rule.setName("Defined foreign key is not using recommended naming convention");
	rule.setDescription(
			"<h2>Description</h2><p>Defined foreign key is not using recommended naming convention to start with FK_.</p>");
	rule.setTag("naming");
	rule.setSeverity("MINOR");
	rule.setRemediationFunction("LINEAR");
	rule.setDebtRemediationFunctionCoefficient("3min");
	rule.setRuleImplementation(ruleImpl);

	return rule;
}
 
Example #20
Source File: CustomPluginChecks.java    From sonar-tsql-plugin with GNU General Public License v3.0 4 votes vote down vote up
public static Rule getExistsVsInRule() {
	Rule r = new Rule();
	r.setKey("C016");
	r.setInternalKey("C016");
	r.setDescription(
			"<h2>Description</h2><p>Consider using EXISTS/NOT EXISTS operator instead of IN for a subquery.</p>");
	r.setSeverity("MAJOR");
	r.setTag("performance");
	r.setRemediationFunction("LINEAR");
	r.setDebtRemediationFunctionCoefficient("5min");
	r.setName("IN/NOT IN is used for a subquery");
	RuleImplementation rImpl = new RuleImplementation();
	rImpl.getNames().getTextItem().add(PredicateContext.class.getSimpleName());
	rImpl.setRuleMatchType(RuleMatchType.CLASS_ONLY);
	rImpl.setRuleViolationMessage("Consider using EXISTS/NOT EXISTS instead of IN/NOT IN in a clause.");
	r.setRuleImplementation(rImpl);

	RuleImplementation child = new RuleImplementation();
	child.getTextToFind().getTextItem().add("in");
	child.setTextCheckType(TextCheckType.STRICT);
	child.setRuleMatchType(RuleMatchType.TEXT_AND_CLASS);
	child.setRuleResultType(RuleResultType.FAIL_IF_FOUND);
	child.getNames().getTextItem().add(TerminalNodeImpl.class.getSimpleName());

	RuleImplementation childSubqueryContext = new RuleImplementation();
	childSubqueryContext.setRuleMatchType(RuleMatchType.CLASS_ONLY);
	childSubqueryContext.setRuleResultType(RuleResultType.SKIP_IF_NOT_FOUND);
	childSubqueryContext.getNames().getTextItem().add(SubqueryContext.class.getSimpleName());

	rImpl.getChildrenRules().getRuleImplementation().add(child);
	rImpl.getChildrenRules().getRuleImplementation().add(childSubqueryContext);
	rImpl.getCompliantRulesCodeExamples().getRuleCodeExample()
			.add("SELECT name, surname, count from dbo.test where locationID in (1,2,3);");
	rImpl.getCompliantRulesCodeExamples().getRuleCodeExample()
			.add("SELECT name, surname, count from dbo.test where locationID not in (1,2,3);");

	rImpl.getCompliantRulesCodeExamples().getRuleCodeExample().add(
			"SELECT name, surname, count from dbo.test where exists (select 1 from dbo.locations where id = locationID);");
	rImpl.getViolatingRulesCodeExamples().getRuleCodeExample()
			.add("SELECT name, surname, count from dbo.test where locationID in (select id from dbo.locations);");
	rImpl.getViolatingRulesCodeExamples().getRuleCodeExample().add(
			"SELECT name, surname, count from dbo.test where locationID not in (select id from dbo.locations);");

	r.setSource("https://dzone.com/articles/sql-handbook-and-best-practices-performance-tuning");
	return r;
}
 
Example #21
Source File: CustomPluginChecks.java    From sonar-tsql-plugin with GNU General Public License v3.0 4 votes vote down vote up
public static Rule getPKRule() {
	RuleImplementation ruleImpl = new RuleImplementation();
	ruleImpl.getNames().getTextItem().add(Table_constraintContext.class.getSimpleName());
	ruleImpl.setRuleMatchType(RuleMatchType.CLASS_ONLY);

	RuleImplementation child1 = new RuleImplementation();
	child1.getTextToFind().getTextItem().add("PRIMARY");
	child1.getNames().getTextItem().add(TerminalNodeImpl.class.getSimpleName());
	child1.setRuleResultType(RuleResultType.SKIP_IF_NOT_FOUND);
	child1.setRuleMatchType(RuleMatchType.TEXT_AND_CLASS);

	RuleImplementation child2 = new RuleImplementation();
	child2.getTextToFind().getTextItem().add("KEY");
	child2.getNames().getTextItem().add(TerminalNodeImpl.class.getSimpleName());
	child2.setRuleResultType(RuleResultType.SKIP_IF_NOT_FOUND);
	child2.setRuleMatchType(RuleMatchType.TEXT_AND_CLASS);
	ruleImpl.getChildrenRules().getRuleImplementation().add(child1);
	ruleImpl.getChildrenRules().getRuleImplementation().add(child2);
	ruleImpl.setRuleMatchType(RuleMatchType.TEXT_AND_CLASS);

	RuleImplementation main = new RuleImplementation();
	main.getNames().getTextItem().add(IdContext.class.getSimpleName());
	main.setTextCheckType(TextCheckType.CONTAINS);
	main.getTextToFind().getTextItem().add("PK_");
	main.setRuleMatchType(RuleMatchType.TEXT_AND_CLASS);
	main.setRuleResultType(RuleResultType.FAIL_IF_NOT_FOUND);
	main.setRuleViolationMessage("Primary key is not defined using suggested naming convention to start with PK_.");
	main.setDistanceCheckType(RuleDistanceIndexMatchType.EQUALS);

	ruleImpl.getChildrenRules().getRuleImplementation().add(main);
	ruleImpl.getViolatingRulesCodeExamples().getRuleCodeExample().add(
			"CREATE TABLE dbo.Orders\r\n(\r\nId int NOT NULL,\r\nCONSTRAINT OrderID PRIMARY KEY CLUSTERED (Id) \r\n);  ");
	ruleImpl.getCompliantRulesCodeExamples().getRuleCodeExample()
			.add("ALTER TABLE dbo.Orders ADD CONSTRAINT PK_OrderId PRIMARY KEY CLUSTERED (Id);");
	ruleImpl.getViolatingRulesCodeExamples().getRuleCodeExample()
			.add("CREATE TABLE dbo.Orders\r\n(\r\nId int NOT NULL,  \r\nPRIMARY KEY (Id)\r\n);  ");

	Rule rule = new Rule();
	rule.setKey("C010");
	rule.setInternalKey("C010");
	rule.setStatus("BETA");
	rule.setName("Defined primary key is not using recommended naming convention");
	rule.setDescription(
			"<h2>Description</h2><p>Defined primary key is not using recommended naming convention to start with PK_.</p>");
	rule.setTag("naming");
	rule.setSeverity("MINOR");
	rule.setRemediationFunction("LINEAR");
	rule.setDebtRemediationFunctionCoefficient("3min");
	rule.setRuleImplementation(ruleImpl);

	return rule;
}
 
Example #22
Source File: CustomPluginChecks.java    From sonar-tsql-plugin with GNU General Public License v3.0 4 votes vote down vote up
public static Rule getSargRule() {
	Rule rule = new Rule();
	rule.setKey("C009");
	rule.setInternalKey("C009");
	rule.setStatus("BETA");
	rule.setName("Non-sargable statement used");
	rule.setDescription(
			"<h2>Description</h2><p>Use of non-sargeable arguments might cause performance problems.</p>");
	rule.setTag("performance");
	rule.setSeverity("MAJOR");
	rule.setRemediationFunction("LINEAR");
	rule.setDebtRemediationFunctionCoefficient("5min");
	RuleImplementation functionCallContainsColRef = new RuleImplementation();
	functionCallContainsColRef.getNames().getTextItem().add(Full_column_nameContext.class.getSimpleName());
	functionCallContainsColRef.setRuleMatchType(RuleMatchType.CLASS_ONLY);
	functionCallContainsColRef.setRuleResultType(RuleResultType.FAIL_IF_FOUND);
	functionCallContainsColRef
			.setRuleViolationMessage("Non-sargeable argument found - column referenced in a function.");

	RuleImplementation ruleFunctionCall = new RuleImplementation();
	ruleFunctionCall.getNames().getTextItem().add(SCALAR_FUNCTIONContext.class.getSimpleName());
	ruleFunctionCall.getNames().getTextItem().add(Function_callContext.class.getSimpleName());
	ruleFunctionCall.setRuleMatchType(RuleMatchType.CLASS_ONLY);
	ruleFunctionCall.setRuleResultType(RuleResultType.DEFAULT);
	ruleFunctionCall.getChildrenRules().getRuleImplementation().add(functionCallContainsColRef);

	RuleImplementation predicateContextContainsLike = new RuleImplementation();
	predicateContextContainsLike.getTextToFind().getTextItem().add("LIKE");
	predicateContextContainsLike.setTextCheckType(TextCheckType.CONTAINS);
	predicateContextContainsLike.getNames().getTextItem().add(PredicateContext.class.getSimpleName());
	predicateContextContainsLike.setRuleMatchType(RuleMatchType.TEXT_AND_CLASS);
	predicateContextContainsLike.setRuleResultType(RuleResultType.DEFAULT);

	RuleImplementation functionCallContainsLikeWildcard = new RuleImplementation();
	functionCallContainsLikeWildcard.getTextToFind().getTextItem().add("N?[',�]%(.*?)'");
	functionCallContainsLikeWildcard.setTextCheckType(TextCheckType.REGEXP);
	functionCallContainsLikeWildcard.getNames().getTextItem().add(TerminalNodeImpl.class.getSimpleName());
	functionCallContainsLikeWildcard.setRuleMatchType(RuleMatchType.TEXT_AND_CLASS);
	functionCallContainsLikeWildcard.setRuleResultType(RuleResultType.FAIL_IF_FOUND);
	functionCallContainsLikeWildcard
			.setRuleViolationMessage("Non-sargeable argument found - predicate starts with wildcard. %");

	predicateContextContainsLike.getChildrenRules().getRuleImplementation().add(functionCallContainsLikeWildcard);

	RuleImplementation impl = new RuleImplementation();
	impl.getChildrenRules().getRuleImplementation().add(ruleFunctionCall);
	impl.getChildrenRules().getRuleImplementation().add(predicateContextContainsLike);

	impl.getNames().getTextItem().add(Search_conditionContext.class.getSimpleName());
	impl.setRuleMatchType(RuleMatchType.CLASS_ONLY);
	impl.setRuleResultType(RuleResultType.DEFAULT);
	impl.getCompliantRulesCodeExamples().getRuleCodeExample()
			.add("SELECT MAX(RateChangeDate)  FROM HumanResources.EmployeePayHistory WHERE BusinessEntityID = 1");
	impl.getViolatingRulesCodeExamples().getRuleCodeExample()
			.add("SELECT name, surname from dbo.test where year(date) > 2008");
	impl.getViolatingRulesCodeExamples().getRuleCodeExample()
			.add("SELECT name, surname from dbo.test where name like '%red' ");
	impl.getCompliantRulesCodeExamples().getRuleCodeExample()
			.add("SELECT name, surname from dbo.test where date between 2008-10-10 and 2010-10-10;");
	impl.getCompliantRulesCodeExamples().getRuleCodeExample().add("SELECT max(price) from dbo.items;");

	rule.setSource("http://sqlmag.com/t-sql/t-sql-best-practices-part-1");
	rule.setRuleImplementation(impl);

	return rule;
}
 
Example #23
Source File: HiveQLAnalyzer.java    From incubator-tajo with Apache License 2.0 4 votes vote down vote up
/**
 * This method parse operators for equals expressions as follows:
 * =, <>, !=, >=, >, <=, <, IN, NOT IN, LIKE, REGEXP, RLIKE
 * <p/>
 * In this case, this make RuntimeException>
 *
 * @param ctx
 * @return
 */
@Override
public Expr visitPrecedenceEqualExpression(HiveQLParser.PrecedenceEqualExpressionContext ctx) {
  Expr current = null, left = null, right = null, min = null, max = null;
  OpType type = null;
  boolean isNot = false, isIn = false;
  for (int i = 0; i < ctx.getChildCount(); i++) {
    if (ctx.getChild(i) instanceof HiveQLParser.PrecedenceBitwiseOrExpressionContext) {
      if (i == 0) {
        left = visitPrecedenceBitwiseOrExpression((HiveQLParser.PrecedenceBitwiseOrExpressionContext) ctx.getChild(i));
      } else {
        right = visitPrecedenceBitwiseOrExpression((HiveQLParser.PrecedenceBitwiseOrExpressionContext) ctx.getChild(i));
      }
    } else if (ctx.getChild(i) instanceof HiveQLParser.ExpressionsContext) {
      right = visitExpressions((HiveQLParser.ExpressionsContext) ctx.getChild(i));
    } else if (ctx.getChild(i) instanceof TerminalNodeImpl) {
      int symbolType = ((TerminalNodeImpl) ctx.getChild(i)).getSymbol().getType();
      switch (symbolType) {
        case HiveQLLexer.KW_NOT:
          isNot = true;
          break;
        case HiveQLLexer.KW_IN:
          isIn = true;
          break;
        default:
          break;
      }
    } else if (ctx.getChild(i) instanceof HiveQLParser.PrecedenceEqualOperatorContext
        || ctx.getChild(i) instanceof HiveQLParser.PrecedenceEqualNegatableOperatorContext) {
      String keyword = ctx.getChild(i).getText().toUpperCase();

      if (keyword.equals(">")) {
        type = OpType.GreaterThan;
      } else if (keyword.equals("<=>")) {
        throw new RuntimeException("Unexpected operator : <=>");
      } else if (keyword.equals("=")) {
        type = OpType.Equals;
      } else if (keyword.equals("<=")) {
        type = OpType.LessThanOrEquals;
      } else if (keyword.equals("<")) {
        type = OpType.LessThan;
      } else if (keyword.equals(">=")) {
        type = OpType.GreaterThanOrEquals;
      } else if (keyword.equals("<>")) {
        type = OpType.NotEquals;
      } else if (keyword.equals("!=")) {
        type = OpType.NotEquals;
      } else if (keyword.equals("REGEXP")) {
        type = OpType.Regexp;
      } else if (keyword.equals("RLIKE")) {
        type = OpType.Regexp;
      } else if (keyword.equals("LIKE")) {
        type = OpType.LikePredicate;
      }
    }
  }

  if (type != null && right != null) {
    if (type.equals(OpType.LikePredicate)) {
      PatternMatchPredicate like = new PatternMatchPredicate(OpType.LikePredicate,
          isNot, left, right);
      current = like;
    } else if (type.equals(OpType.Regexp)) {
      PatternMatchPredicate regex = new PatternMatchPredicate(OpType.Regexp, isNot, left, right);
      current = regex;
    } else {
      BinaryOperator binaryOperator = new BinaryOperator(type, left, right);
      current = binaryOperator;
    }
  } else if (isIn) {
    InPredicate inPredicate = new InPredicate(left, right, isNot);
    current = inPredicate;
  } else {
    current = left;
  }

  return current;
}
 
Example #24
Source File: HiveQLAnalyzer.java    From incubator-tajo with Apache License 2.0 4 votes vote down vote up
@Override
public Expr visitFunction(HiveQLParser.FunctionContext ctx) {
  Expr current = null;
  String signature = ctx.functionName().getText();

  boolean isDistinct = false;
  if (ctx.getChild(2) != null) {
    if (ctx.getChild(2) instanceof TerminalNodeImpl && ctx.getChild(2).getText().equalsIgnoreCase("DISTINCT")) {
      isDistinct = true;
    }
  }

  if (signature.equalsIgnoreCase("MIN")
      || signature.equalsIgnoreCase("MAX")
      || signature.equalsIgnoreCase("SUM")
      || signature.equalsIgnoreCase("AVG")
      || signature.equalsIgnoreCase("COUNT")
      ) {
    if (ctx.selectExpression().size() > 1) {
      throw new RuntimeException("Exactly expected one argument.");
    }

    if (ctx.selectExpression().size() == 0) {
      CountRowsFunctionExpr countRowsFunctionExpr = new CountRowsFunctionExpr();
      current = countRowsFunctionExpr;
    } else {
      GeneralSetFunctionExpr setFunctionExpr = new GeneralSetFunctionExpr(signature, isDistinct,
          visitSelectExpression(ctx.selectExpression(0)));
      current = setFunctionExpr;
    }
  } else {
    FunctionExpr functionExpr = new FunctionExpr(signature);
    Expr[] params = new Expr[ctx.selectExpression().size()];
    for (int i = 0; i < ctx.selectExpression().size(); i++) {
      params[i] = visitSelectExpression(ctx.selectExpression(i));
    }
    functionExpr.setParams(params);
    current = functionExpr;
  }


  return current;
}
 
Example #25
Source File: Hierarchy.java    From batfish with Apache License 2.0 4 votes vote down vote up
/**
 * Populate the specified setLine's children with the supplied path and return the generated set
 * statement
 */
private static StatementContext setLineHelper(
    Set_lineContext setLine,
    HierarchyPath path,
    int overrideLine,
    boolean markWildcards,
    Map<Token, String> tokenInputs) {
  StringBuilder sb = new StringBuilder();
  for (HierarchyChildNode pathNode : path._nodes) {
    sb.append(pathNode._text);
    sb.append(" ");
  }
  String newStatementText = sb.toString();
  // Get rid of last " ", which matters for tokens where whitespace is not ignored
  newStatementText =
      "set " + newStatementText.substring(0, newStatementText.length() - 1) + "\n";
  TerminalNode set = new TerminalNodeImpl(new CommonToken(FlatJuniperLexer.SET, "set"));
  Set_line_tailContext setLineTail = new Set_line_tailContext(setLine, -1);
  TerminalNode newline = new TerminalNodeImpl(new CommonToken(FlatJuniperLexer.NEWLINE, "\n"));
  setLine.children = new ArrayList<>();
  setLine.children.add(set);
  setLine.children.add(setLineTail);
  setLine.children.add(newline);
  FlatJuniperCombinedParser parser =
      new FlatJuniperCombinedParser(newStatementText, parserSettings());
  // Use the supplied line number for the constructed nodes
  parser.getLexer().setOverrideTokenStartLine(overrideLine);
  if (markWildcards) {
    parser.setMarkWildcards(true);
  }
  Flat_juniper_configurationContext newConfiguration =
      parser.getParser().flat_juniper_configuration();
  markTokenInputs(newConfiguration, newStatementText, tokenInputs, parser);
  if (markWildcards) {
    parser.setMarkWildcards(false);
  }
  StatementContext newStatement = newConfiguration.set_line(0).set_line_tail().statement();
  newStatement.parent = setLineTail;

  setLineTail.children = new ArrayList<>();
  setLineTail.children.add(newStatement);
  return newStatement;
}
 
Example #26
Source File: PreviewPanel.java    From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private void showError(String message) {
	setParseTree(Collections.emptyList(), new TerminalNodeImpl(new CommonToken(Token.INVALID_TYPE, message)));
}