Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring to use Syntax Tree #18

Merged
merged 6 commits into from
Oct 19, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
update
  • Loading branch information
SatoshiMatsuzaki authored and SatoshiMatsuzaki committed Oct 19, 2019
commit 83aa748ba83d51f4cdca24fe7edf1b9fd24e219f
40 changes: 20 additions & 20 deletions src/checker/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def _check(tree: SyntaxTree, indent_steps: int) -> List[Violation]:
for leaf in tree.leaves:
if leaf.indent % indent_steps != 0:
v = violation.IndentStepsViolation(
line=leaf.node.line_num,
tree=leaf,
pos=1,
expected=indent_steps,
actual=leaf.indent)
Expand Down Expand Up @@ -76,7 +76,7 @@ def _check(tree: SyntaxTree, keyword_style: str) -> List[Violation]:
params = {'style': keyword_style, 'actual': word, 'expected': expected}

v = violation.KeywordStyleViolation(
line=leaf.node.line_num,
tree=leaf,
pos=pos,
**params)
violation_list.append(v)
Expand Down Expand Up @@ -149,7 +149,7 @@ def _check_position(tree: SyntaxTree, comma_position: str) -> List[Violation]:
pos = leaf.get_position(lindex+idx)
violation_list.append(
violation.CommaPositionViolation(
line=leaf.node.line_num,
tree=leaf,
pos=pos,
comma_position=comma_position))

Expand Down Expand Up @@ -208,7 +208,7 @@ def _check_multiple(tree: SyntaxTree) -> List[Violation]:

if length > 1:
pos = leaf.get_position(idx)
v = violation.MultiSpacesViolation(leaf.node.line_num, pos)
v = violation.MultiSpacesViolation(leaf, pos)
violation_list.append(v)

violation_list.extend(WhitespaceChecker._check_multiple(leaf))
Expand All @@ -233,7 +233,7 @@ def _check_comma(tree: SyntaxTree) -> List[Violation]:
'position': 'before'}
violation_list.append(
violation.WhitespaceViolation(
line=leaf.node.line_num,
tree=leaf,
pos=pos,
**params))

Expand All @@ -245,7 +245,7 @@ def _check_comma(tree: SyntaxTree) -> List[Violation]:
'target': f'{token.word}{leaf.tokens[idx+1].word}'}
violation_list.append(
violation.WhitespaceViolation(
line=leaf.node.line_num,
tree=leaf,
pos=pos,
**params))

Expand All @@ -269,7 +269,7 @@ def _check_bracket(tree: SyntaxTree) -> List[Violation]:
'position': 'after',
'target': f'{token.word}{leaf.tokens[idx+1].word}'}
violation_list.append(
violation.WhitespaceViolation(line=leaf.node.line_num, pos=pos, **params))
violation.WhitespaceViolation(tree=leaf, pos=pos, **params))

# Checks whether a whitespace does not exist before right-bracket " )".
if token.kind == Token.BRACKET_RIGHT \
Expand All @@ -281,7 +281,7 @@ def _check_bracket(tree: SyntaxTree) -> List[Violation]:
'target': f'{leaf.tokens[idx-1].word}{token.word}'}
violation_list.append(
violation.WhitespaceViolation(
line=leaf.node.line_num,
tree=leaf,
pos=pos,
**params))

Expand All @@ -308,7 +308,7 @@ def _check_operator(tree: SyntaxTree) -> List[Violation]:
'position': 'before',
'target': f'{leaf.tokens[idx-1].word}{token.word}'}
violation_list.append(
violation.WhitespaceViolation(line=leaf.node.line_num, pos=pos, **params))
violation.WhitespaceViolation(tree=leaf, pos=pos, **params))

# Checks whether a whitespace exists after operator.
if leaf.tokens[idx + 1].kind != Token.WHITESPACE:
Expand All @@ -318,7 +318,7 @@ def _check_operator(tree: SyntaxTree) -> List[Violation]:
'position': 'after',
'target': f'{token.word}{leaf.tokens[idx + 1].word}'}
violation_list.append(
violation.WhitespaceViolation(line=leaf.node.line_num, pos=pos, **params))
violation.WhitespaceViolation(tree=leaf, pos=pos, **params))

violation_list.extend(
WhitespaceChecker._check_operator(leaf))
Expand Down Expand Up @@ -392,7 +392,7 @@ def _check_table_name(tree: SyntaxTree) -> List[Violation]:
------
"""
pos = leaf.get_position(idx)
v = violation.JoinTableViolation(line=leaf.node.line_num, pos=pos)
v = violation.JoinTableViolation(tree=leaf, pos=pos)
violation_list.append(v)

violation_list.extend(JoinChecker._check_table_name(leaf))
Expand Down Expand Up @@ -431,7 +431,7 @@ def _check_context(tree: SyntaxTree, expected_list: Dict[str, str]) -> List[Viol
if join_context_str.upper() not in ['INNER JOIN', 'RIGHT OUTER JOIN', 'LEFT OUTER JOIN', 'CROSS JOIN']:
pos = leaf.get_position(idx)
params = {'actual': join_context_str, 'expected': expected}
v = violation.JoinContextViolation(line=leaf.node.line_num, pos=pos, **params)
v = violation.JoinContextViolation(tree=leaf, pos=pos, **params)
violation_list.append(v)

violation_list.extend(JoinChecker._check_context(leaf, expected_list))
Expand Down Expand Up @@ -463,9 +463,9 @@ def check(tree: SyntaxTree, config: ConfigLoader) -> List[Violation]:
result: List[Violation] = []

# 1. Checks whether two or more blank lines exist.
v_list, blank_count, last_node = LineChecker._check_blank_line(tree, blank_count=0)
v_list, blank_count, last_tree = LineChecker._check_blank_line(tree, blank_count=0)
if blank_count >= 2:
v_list.append(violation.MultiBlankLineViolation(last_node.line_num, pos=1))
v_list.append(violation.MultiBlankLineViolation(last_tree, pos=1))
result.extend(v_list)

# 2. Checks whether breaking line after specified keywords.
Expand All @@ -474,25 +474,25 @@ def check(tree: SyntaxTree, config: ConfigLoader) -> List[Violation]:
return result

@staticmethod
def _check_blank_line(tree: SyntaxTree, blank_count: int) -> Tuple[List[Violation], int, Node]:
def _check_blank_line(tree: SyntaxTree, blank_count: int) -> Tuple[List[Violation], int, SyntaxTree]:
violation_list: List[Violation] = []
last_node = tree.node
last_tree = tree

for leaf in tree.leaves:
count = len(leaf.node)
is_blank = (count == 0)

if count == 1 and leaf.tokens[0].kind == Token.WHITESPACE:
violation_list.append(violation.OnlyWhitespaceViolation(leaf.node.line_num, pos=1))
violation_list.append(violation.OnlyWhitespaceViolation(tree=leaf, pos=1))

# If this line is not blank and 2 or more previous lines are blank, stack violation.
if is_blank:
blank_count += 1
elif blank_count >= 2:
violation_list.append(violation.MultiBlankLineViolation(leaf.node.line_num-1, pos=1))
violation_list.append(violation.MultiBlankLineViolation(tree=leaf, pos=1))
blank_count = 0

v_list, blank_count, last_node = LineChecker._check_blank_line(leaf, blank_count)
v_list, blank_count, last_tree = LineChecker._check_blank_line(leaf, blank_count)
violation_list.extend(v_list)

return violation_list, blank_count, last_node
return violation_list, blank_count, last_tree
47 changes: 24 additions & 23 deletions src/checker/violation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from enum import Enum

from src.parser.token import Token
from src.parser.syntax_tree import SyntaxTree


class Code(Enum):
Expand Down Expand Up @@ -58,8 +59,8 @@ def template(self, value: str):


class Violation:
def __init__(self, line: int, pos: int, code: Code, **kwargs):
self.line: int = line
def __init__(self, tree: SyntaxTree, pos: int, code: Code, **kwargs):
self.tree: SyntaxTree = tree
self.pos: int = pos
self.code: Code = code
self.params: Dict = kwargs
Expand All @@ -68,18 +69,18 @@ def get_message(self):
_template = '(L{line}, {pos}): ' + self.code.template

return _template.format(
line=self.line,
line=self.tree.node.line_num,
pos=self.pos,
**self.params)


class IndentStepsViolation(Violation):
def __init__(self, line: int, pos: int, **kwargs):
super().__init__(line, pos, Code.INDENT_STEPS, **kwargs)
def __init__(self, tree: SyntaxTree, pos: int, **kwargs):
super().__init__(tree, pos, Code.INDENT_STEPS, **kwargs)


class KeywordStyleViolation(Violation):
def __init__(self, line: int, pos: int, **kwargs):
def __init__(self, tree: SyntaxTree, pos: int, **kwargs):
if 'style' not in kwargs:
raise KeyError(f'style must be passed.')

Expand All @@ -94,11 +95,11 @@ def __init__(self, line: int, pos: int, **kwargs):
else:
raise ValueError('keyword style must be in [upper-all, upper-head, lower]')

super().__init__(line, pos, _code, **kwargs)
super().__init__(tree, pos, _code, **kwargs)


class CommaPositionViolation(Violation):
def __init__(self, line: int, pos: int, comma_position: str, **kwargs):
def __init__(self, tree: SyntaxTree, pos: int, comma_position: str, **kwargs):
self.comma_position = comma_position

if comma_position == 'head':
Expand All @@ -108,16 +109,16 @@ def __init__(self, line: int, pos: int, comma_position: str, **kwargs):
else:
raise ValueError('position must be in [head, end]')

super().__init__(line, pos, _code, **kwargs)
super().__init__(tree, pos, _code, **kwargs)


class MultiSpacesViolation(Violation):
def __init__(self, line: int, pos: int, **kwargs):
super().__init__(line, pos, Code.WHITESPACE_MULTIPLE, **kwargs)
def __init__(self, tree: SyntaxTree, pos: int, **kwargs):
super().__init__(tree, pos, Code.WHITESPACE_MULTIPLE, **kwargs)


class WhitespaceViolation(Violation):
def __init__(self, line: int, pos: int, **kwargs):
def __init__(self, tree: SyntaxTree, pos: int, **kwargs):
if 'token' not in kwargs:
raise KeyError(f'token must be passed.')
if 'position' not in kwargs:
Expand Down Expand Up @@ -149,36 +150,36 @@ def __init__(self, line: int, pos: int, **kwargs):
raise ValueError('token kind must be in [{}, {}, {}, {}]'.format(
Token.COMMA, Token.BRACKET_LEFT, Token.BRACKET_RIGHT, Token.OPERATOR))

super().__init__(line, pos, _code, **kwargs)
super().__init__(tree, pos, _code, **kwargs)


class JoinTableViolation(Violation):
def __init__(self, line: int, pos: int, **kwargs):
super().__init__(line, pos, Code.JOIN_TABLE, **kwargs)
def __init__(self, tree: SyntaxTree, pos: int, **kwargs):
super().__init__(tree, pos, Code.JOIN_TABLE, **kwargs)


class JoinContextViolation(Violation):
def __init__(self, line: int, pos: int, **kwargs):
super().__init__(line, pos, Code.JOIN_CONTEXT, **kwargs)
def __init__(self, tree: SyntaxTree, pos: int, **kwargs):
super().__init__(tree, pos, Code.JOIN_CONTEXT, **kwargs)


class MultiBlankLineViolation(Violation):
def __init__(self, line: int, pos: int, **kwargs):
super().__init__(line, pos, Code.LINE_BlANK_MULTIPLE, **kwargs)
def __init__(self, tree: SyntaxTree, pos: int, **kwargs):
super().__init__(tree, pos, Code.LINE_BlANK_MULTIPLE, **kwargs)


class OnlyWhitespaceViolation(Violation):
def __init__(self, line: int, pos: int, **kwargs):
super().__init__(line, pos, Code.LINE_ONLY_WHITESPACE, **kwargs)
def __init__(self, tree: SyntaxTree, pos: int, **kwargs):
super().__init__(tree, pos, Code.LINE_ONLY_WHITESPACE, **kwargs)


class BreakingLineViolation(Violation):
def __init__(self, line: int, pos: int, position: int, **kwargs):
def __init__(self, tree: SyntaxTree, pos: int, position: int, **kwargs):
if position == 'before':
_code = Code.BREAK_LINE_BEFORE
elif position == 'after':
_code = Code.BREAK_LINE_AFTER
else:
raise ValueError('position must be in [before, after]')

super().__init__(line, pos, _code, **kwargs)
super().__init__(tree, pos, _code, **kwargs)