parser: Provide nextnext for token rules

Because the indentation rule sometimes needs to look two tokens forward
(in case of anchors for instance).
pull/4/head
Adrien Vergé 9 years ago
parent 62fa4cbe39
commit 48c7d65c54

@ -39,7 +39,8 @@ class IndentationStackTestCase(RuleTestCase):
context = {}
output = ''
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__
.replace('Token', '')

@ -68,6 +68,7 @@ def get_costemic_problems(buffer, conf):
rule_conf = conf.rules[rule.ID]
for problem in rule.check(rule_conf,
elem.curr, elem.prev, elem.next,
elem.nextnext,
context[rule.ID]):
problem.rule = rule.ID
problem.level = rule_conf['level']

@ -30,11 +30,12 @@ class Line(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.curr = curr
self.prev = prev
self.next = next
self.nextnext = nextnext
def line_generator(buffer):
@ -55,14 +56,16 @@ def token_generator(buffer):
try:
prev = None
next = yaml_loader.peek_token()
while next is not None:
curr = yaml_loader.get_token()
next = yaml_loader.peek_token()
curr = yaml_loader.get_token()
while curr is not None:
next = yaml_loader.get_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
curr = next
except yaml.scanner.ScannerError:
pass

@ -73,7 +73,7 @@ CONF = {'min-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):
problem = spaces_after(token, prev, next,
min=conf['min-spaces-inside'],

@ -74,7 +74,7 @@ CONF = {'min-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):
problem = spaces_after(token, prev, next,
min=conf['min-spaces-inside'],

@ -81,7 +81,7 @@ CONF = {'max-spaces-before': 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):
problem = spaces_before(token, prev, next,
max=conf['max-spaces-before'],

@ -105,7 +105,7 @@ CONF = {'max-spaces-before': 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 (prev is not None and conf['max-spaces-before'] != -1 and
prev.end_mark.line < token.start_mark.line):

@ -67,7 +67,7 @@ CONF = {'require-starting-space': bool,
'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):
if (conf['min-spaces-from-content'] != -1 and
not isinstance(token, yaml.StreamStartToken) and

@ -98,7 +98,7 @@ TYPE = 'token'
# # commented line 2
# current: line
def check(conf, token, prev, next, context):
def check(conf, token, prev, next, nextnext, context):
if prev is None:
return

@ -84,7 +84,7 @@ TYPE = 'token'
CONF = {'present': bool}
def check(conf, token, prev, next, context):
def check(conf, token, prev, next, nextnext, context):
if conf['present']:
if (isinstance(token, yaml.StreamEndToken) and
not (isinstance(prev, yaml.DocumentEndToken) or

@ -74,7 +74,7 @@ TYPE = 'token'
CONF = {'present': bool}
def check(conf, token, prev, next, context):
def check(conf, token, prev, next, nextnext, context):
if conf['present']:
if (isinstance(prev, (yaml.StreamStartToken,
yaml.DocumentEndToken,

@ -78,7 +78,7 @@ TYPE = 'token'
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):
problem = spaces_after(token, prev, next,
max=conf['max-spaces-after'],

@ -236,7 +236,7 @@ def check_scalar_indentation(conf, token, context):
(expected_indent, indent))
def check(conf, token, prev, next, context):
def check(conf, token, prev, next, nextnext, context):
if 'stack' not in context:
context['stack'] = [Parent(ROOT, 0)]
context['cur_line'] = -1

@ -72,7 +72,7 @@ class Parent(object):
self.keys = []
def check(conf, token, prev, next, context):
def check(conf, token, prev, next, nextnext, context):
if 'stack' not in context:
context['stack'] = []

Loading…
Cancel
Save