parser: Provide nextnext for token rules
Because the indentation rule sometimes needs to look two tokens forward (in case of anchors for instance).
This commit is contained in:
@@ -39,7 +39,8 @@ class IndentationStackTestCase(RuleTestCase):
|
|||||||
context = {}
|
context = {}
|
||||||
output = ''
|
output = ''
|
||||||
for elem in token_generator(source):
|
for elem in token_generator(source):
|
||||||
list(check(conf, elem.curr, elem.prev, elem.next, context))
|
list(check(conf, elem.curr, elem.prev, elem.next, elem.nextnext,
|
||||||
|
context))
|
||||||
|
|
||||||
token_type = (elem.curr.__class__.__name__
|
token_type = (elem.curr.__class__.__name__
|
||||||
.replace('Token', '')
|
.replace('Token', '')
|
||||||
|
|||||||
@@ -68,6 +68,7 @@ def get_costemic_problems(buffer, conf):
|
|||||||
rule_conf = conf.rules[rule.ID]
|
rule_conf = conf.rules[rule.ID]
|
||||||
for problem in rule.check(rule_conf,
|
for problem in rule.check(rule_conf,
|
||||||
elem.curr, elem.prev, elem.next,
|
elem.curr, elem.prev, elem.next,
|
||||||
|
elem.nextnext,
|
||||||
context[rule.ID]):
|
context[rule.ID]):
|
||||||
problem.rule = rule.ID
|
problem.rule = rule.ID
|
||||||
problem.level = rule_conf['level']
|
problem.level = rule_conf['level']
|
||||||
|
|||||||
@@ -30,11 +30,12 @@ class Line(object):
|
|||||||
|
|
||||||
|
|
||||||
class Token(object):
|
class Token(object):
|
||||||
def __init__(self, line_no, curr, prev, next):
|
def __init__(self, line_no, curr, prev, next, nextnext):
|
||||||
self.line_no = line_no
|
self.line_no = line_no
|
||||||
self.curr = curr
|
self.curr = curr
|
||||||
self.prev = prev
|
self.prev = prev
|
||||||
self.next = next
|
self.next = next
|
||||||
|
self.nextnext = nextnext
|
||||||
|
|
||||||
|
|
||||||
def line_generator(buffer):
|
def line_generator(buffer):
|
||||||
@@ -55,14 +56,16 @@ def token_generator(buffer):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
prev = None
|
prev = None
|
||||||
next = yaml_loader.peek_token()
|
curr = yaml_loader.get_token()
|
||||||
while next is not None:
|
while curr is not None:
|
||||||
curr = yaml_loader.get_token()
|
next = yaml_loader.get_token()
|
||||||
next = yaml_loader.peek_token()
|
nextnext = yaml_loader.peek_token()
|
||||||
|
|
||||||
yield Token(curr.start_mark.line + 1, curr, prev, next)
|
yield Token(curr.start_mark.line + 1, curr, prev, next, nextnext)
|
||||||
|
|
||||||
prev = curr
|
prev = curr
|
||||||
|
curr = next
|
||||||
|
|
||||||
except yaml.scanner.ScannerError:
|
except yaml.scanner.ScannerError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|||||||
@@ -73,7 +73,7 @@ CONF = {'min-spaces-inside': int,
|
|||||||
'max-spaces-inside': int}
|
'max-spaces-inside': int}
|
||||||
|
|
||||||
|
|
||||||
def check(conf, token, prev, next, context):
|
def check(conf, token, prev, next, nextnext, context):
|
||||||
if isinstance(token, yaml.FlowMappingStartToken):
|
if isinstance(token, yaml.FlowMappingStartToken):
|
||||||
problem = spaces_after(token, prev, next,
|
problem = spaces_after(token, prev, next,
|
||||||
min=conf['min-spaces-inside'],
|
min=conf['min-spaces-inside'],
|
||||||
|
|||||||
@@ -74,7 +74,7 @@ CONF = {'min-spaces-inside': int,
|
|||||||
'max-spaces-inside': int}
|
'max-spaces-inside': int}
|
||||||
|
|
||||||
|
|
||||||
def check(conf, token, prev, next, context):
|
def check(conf, token, prev, next, nextnext, context):
|
||||||
if isinstance(token, yaml.FlowSequenceStartToken):
|
if isinstance(token, yaml.FlowSequenceStartToken):
|
||||||
problem = spaces_after(token, prev, next,
|
problem = spaces_after(token, prev, next,
|
||||||
min=conf['min-spaces-inside'],
|
min=conf['min-spaces-inside'],
|
||||||
|
|||||||
@@ -81,7 +81,7 @@ CONF = {'max-spaces-before': int,
|
|||||||
'max-spaces-after': int}
|
'max-spaces-after': int}
|
||||||
|
|
||||||
|
|
||||||
def check(conf, token, prev, next, context):
|
def check(conf, token, prev, next, nextnext, context):
|
||||||
if isinstance(token, yaml.ValueToken):
|
if isinstance(token, yaml.ValueToken):
|
||||||
problem = spaces_before(token, prev, next,
|
problem = spaces_before(token, prev, next,
|
||||||
max=conf['max-spaces-before'],
|
max=conf['max-spaces-before'],
|
||||||
|
|||||||
@@ -105,7 +105,7 @@ CONF = {'max-spaces-before': int,
|
|||||||
'max-spaces-after': int}
|
'max-spaces-after': int}
|
||||||
|
|
||||||
|
|
||||||
def check(conf, token, prev, next, context):
|
def check(conf, token, prev, next, nextnext, context):
|
||||||
if isinstance(token, yaml.FlowEntryToken):
|
if isinstance(token, yaml.FlowEntryToken):
|
||||||
if (prev is not None and conf['max-spaces-before'] != -1 and
|
if (prev is not None and conf['max-spaces-before'] != -1 and
|
||||||
prev.end_mark.line < token.start_mark.line):
|
prev.end_mark.line < token.start_mark.line):
|
||||||
|
|||||||
@@ -67,7 +67,7 @@ CONF = {'require-starting-space': bool,
|
|||||||
'min-spaces-from-content': int}
|
'min-spaces-from-content': int}
|
||||||
|
|
||||||
|
|
||||||
def check(conf, token, prev, next, context):
|
def check(conf, token, prev, next, nextnext, context):
|
||||||
for comment in get_comments_between_tokens(token, next):
|
for comment in get_comments_between_tokens(token, next):
|
||||||
if (conf['min-spaces-from-content'] != -1 and
|
if (conf['min-spaces-from-content'] != -1 and
|
||||||
not isinstance(token, yaml.StreamStartToken) and
|
not isinstance(token, yaml.StreamStartToken) and
|
||||||
|
|||||||
@@ -98,7 +98,7 @@ TYPE = 'token'
|
|||||||
# # commented line 2
|
# # commented line 2
|
||||||
# current: line
|
# current: line
|
||||||
|
|
||||||
def check(conf, token, prev, next, context):
|
def check(conf, token, prev, next, nextnext, context):
|
||||||
if prev is None:
|
if prev is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|||||||
@@ -84,7 +84,7 @@ TYPE = 'token'
|
|||||||
CONF = {'present': bool}
|
CONF = {'present': bool}
|
||||||
|
|
||||||
|
|
||||||
def check(conf, token, prev, next, context):
|
def check(conf, token, prev, next, nextnext, context):
|
||||||
if conf['present']:
|
if conf['present']:
|
||||||
if (isinstance(token, yaml.StreamEndToken) and
|
if (isinstance(token, yaml.StreamEndToken) and
|
||||||
not (isinstance(prev, yaml.DocumentEndToken) or
|
not (isinstance(prev, yaml.DocumentEndToken) or
|
||||||
|
|||||||
@@ -74,7 +74,7 @@ TYPE = 'token'
|
|||||||
CONF = {'present': bool}
|
CONF = {'present': bool}
|
||||||
|
|
||||||
|
|
||||||
def check(conf, token, prev, next, context):
|
def check(conf, token, prev, next, nextnext, context):
|
||||||
if conf['present']:
|
if conf['present']:
|
||||||
if (isinstance(prev, (yaml.StreamStartToken,
|
if (isinstance(prev, (yaml.StreamStartToken,
|
||||||
yaml.DocumentEndToken,
|
yaml.DocumentEndToken,
|
||||||
|
|||||||
@@ -78,7 +78,7 @@ TYPE = 'token'
|
|||||||
CONF = {'max-spaces-after': int}
|
CONF = {'max-spaces-after': int}
|
||||||
|
|
||||||
|
|
||||||
def check(conf, token, prev, next, context):
|
def check(conf, token, prev, next, nextnext, context):
|
||||||
if isinstance(token, yaml.BlockEntryToken):
|
if isinstance(token, yaml.BlockEntryToken):
|
||||||
problem = spaces_after(token, prev, next,
|
problem = spaces_after(token, prev, next,
|
||||||
max=conf['max-spaces-after'],
|
max=conf['max-spaces-after'],
|
||||||
|
|||||||
@@ -236,7 +236,7 @@ def check_scalar_indentation(conf, token, context):
|
|||||||
(expected_indent, indent))
|
(expected_indent, indent))
|
||||||
|
|
||||||
|
|
||||||
def check(conf, token, prev, next, context):
|
def check(conf, token, prev, next, nextnext, context):
|
||||||
if 'stack' not in context:
|
if 'stack' not in context:
|
||||||
context['stack'] = [Parent(ROOT, 0)]
|
context['stack'] = [Parent(ROOT, 0)]
|
||||||
context['cur_line'] = -1
|
context['cur_line'] = -1
|
||||||
|
|||||||
@@ -72,7 +72,7 @@ class Parent(object):
|
|||||||
self.keys = []
|
self.keys = []
|
||||||
|
|
||||||
|
|
||||||
def check(conf, token, prev, next, context):
|
def check(conf, token, prev, next, nextnext, context):
|
||||||
if 'stack' not in context:
|
if 'stack' not in context:
|
||||||
context['stack'] = []
|
context['stack'] = []
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user