コード例 #1
0
def match_to_block(match, cleaned_content, content):
  assert cleaned_content[match.end() - 1] == "(", "Block starts with %s" % (cleaned_content[match.end() - 1],)
  condition_end, condition_buffer = find_block(cleaned_content, match.end() - 1, "(", ")")
  block_content = content[match.end():condition_end - 1]
  brace_match = BRACE_PATTERN.match(cleaned_content[condition_end:])
  line = len(content[:match.end()].splitlines())
  ifelse_block = IfElseStatement(match.start(), line, content=block_content)
  if not brace_match:
    ifelse_block.ignored = True
    return ifelse_block
  
  buffer_start = condition_end + brace_match.end() - 1
  alternatives = [] 
  try:
    while True:
      buffer_end, block_buffer = find_block(cleaned_content, buffer_start, "{", "}")
      
      alternatives.append(IfElseAlternative(ifelse_block.start,
                                            buffer_start + 1,
                                            buffer_end,
                                            content[buffer_start + 1:buffer_end - 1]))
      
      ifelse_match = IFELSE_START_PATTERN.match(cleaned_content[buffer_end:])
      else_match = ELSE_START_PATTERN.match(cleaned_content[buffer_end:])

      if ifelse_match != None:
        condition_end, unused = find_block(cleaned_content, buffer_end + ifelse_match.end() - 1, "(", ")")
        brace_match = BRACE_PATTERN.match(cleaned_content[condition_end:])
        if not brace_match:
          raise BrokenStatement()
        buffer_start = condition_end + brace_match.end() - 1
      elif else_match != None:
        buffer_start = buffer_end + else_match.end() - 1
      else:
        break
  except BrokenStatement:
    ifelse_block.ignored = True
  else:
    ifelse_block.ignored = False
    for a in alternatives:
      ifelse_block.alternatives.append(a)
  finally:
    return ifelse_block
コード例 #2
0
def match_to_block(match, content):
  label = eval(content[match.start('label'):match.end('label')].strip())
  maybe_block = MaybeStatement(MaybeStatement.BLOCK, match.start(), label)
  maybe_block.line = len(content[:match.start()].splitlines()) + 1
  buffer_start = match.end() - 1
  
  value = 0
  while True:
    buffer_end, block_buffer = find_block(content, buffer_start, "{", "}")
    maybe_block.alternatives.append(MaybeAlternative(value,
                                                     maybe_block.start,
                                                     buffer_start + 1,
                                                     buffer_end,
                                                     block_buffer[1:-1]))
    value += 1
    or_match = ALTERNATIVE_START_PATTERN.match(content[buffer_end:])
    if or_match != None:
      buffer_start = buffer_end + or_match.end() - 1
    else:
      break
  maybe_block.end = buffer_end
  maybe_block.content = content[maybe_block.start:maybe_block.end]
  return maybe_block