示例#1
0
 def process(self, stack, stmt):
     if isinstance(stmt, grouping.Statement):
         self._curr_stmt = stmt
     self._process(stmt)
     if isinstance(stmt, grouping.Statement):
         if self._last_stmt is not None:
             if self._last_stmt.to_unicode().endswith('\n'):
                 nl = '\n'
             else:
                 nl = '\n\n'
             stmt.tokens.insert(0, grouping.Token(T.Whitespace, nl))
         if self._last_stmt != stmt:
             self._last_stmt = stmt
示例#2
0
 def _process(self, tlist):
     idx = 0
     clss = set([x.__class__ for x in tlist.tokens])
     while grouping.Comment in clss:
         token = tlist.token_next_by_instance(0, grouping.Comment)
         tidx = tlist.token_index(token)
         prev = tlist.token_prev(tidx, False)
         next_ = tlist.token_next(tidx, False)
         # Replace by whitespace if prev and next exist and if they're not
         # whitespaces. This doesn't apply if prev or next is a paranthesis.
         if (prev is not None and next_ is not None
                 and not prev.is_whitespace() and not next_.is_whitespace()
                 and not (prev.match(T.Punctuation, '(')
                          or next_.match(T.Punctuation, ')'))):
             tlist.tokens[tidx] = grouping.Token(T.Whitespace, ' ')
         else:
             tlist.tokens.pop(tidx)
         clss = set([x.__class__ for x in tlist.tokens])
示例#3
0
 def _process(self, stack, group, stream):
     for token in stream:
         if token.is_whitespace() and "\n" in token.value:
             if token.value.endswith("\n"):
                 self.line = ""
             else:
                 self.line = token.value.splitlines()[-1]
         elif token.is_group() and not token.__class__ in self.keep_together:
             token.tokens = self._process(stack, token, token.tokens)
         else:
             val = token.to_unicode()
             if len(self.line) + len(val) > self.width:
                 match = re.search("^ +", self.line)
                 if match is not None:
                     indent = match.group()
                 else:
                     indent = ""
                 yield grouping.Token(T.Whitespace, "\n%s" % indent)
                 self.line = indent
             self.line += val
         yield token
示例#4
0
 def _process(self, stream, varname):
     if self.count > 1:
         yield grouping.Token(T.Whitespace, '\n')
     yield grouping.Token(T.Name, varname)
     yield grouping.Token(T.Whitespace, ' ')
     yield grouping.Token(T.Operator, '=')
     yield grouping.Token(T.Whitespace, ' ')
     yield grouping.Token(T.Text, '"')
     cnt = 0
     for token in stream:
         if token.is_whitespace() and '\n' in token.value:
             #                cnt += 1
             #                if cnt == 1:
             #                    continue
             after_lb = token.value.split('\n', 1)[1]
             yield grouping.Token(T.Text, ' "')
             yield grouping.Token(T.Operator, ';')
             yield grouping.Token(T.Whitespace, '\n')
             yield grouping.Token(T.Name, varname)
             yield grouping.Token(T.Whitespace, ' ')
             yield grouping.Token(T.Punctuation, '.')
             yield grouping.Token(T.Operator, '=')
             yield grouping.Token(T.Whitespace, ' ')
             yield grouping.Token(T.Text, '"')
             if after_lb:
                 yield grouping.Token(T.Text, after_lb)
             continue
         elif '"' in token.value:
             token.value = token.value.replace('"', '\\"')
         yield grouping.Token(T.Text, token.value)
     yield grouping.Token(T.Text, '"')
     yield grouping.Token(T.Punctuation, ';')
示例#5
0
 def _process(self, stream, varname, count, has_nl):
     if count > 1:
         yield grouping.Token(T.Whitespace, '\n')
     yield grouping.Token(T.Name, varname)
     yield grouping.Token(T.Whitespace, ' ')
     yield grouping.Token(T.Operator, '=')
     yield grouping.Token(T.Whitespace, ' ')
     if has_nl:
         yield grouping.Token(T.Operator, '(')
     yield grouping.Token(T.Text, "'")
     cnt = 0
     for token in stream:
         cnt += 1
         if token.is_whitespace() and '\n' in token.value:
             if cnt == 1:
                 continue
             after_lb = token.value.split('\n', 1)[1]
             yield grouping.Token(T.Text, " '")
             yield grouping.Token(T.Whitespace, '\n')
             for i in range(len(varname) + 4):
                 yield grouping.Token(T.Whitespace, ' ')
             yield grouping.Token(T.Text, "'")
             if after_lb:  # it's the indendation
                 yield grouping.Token(T.Whitespace, after_lb)
             continue
         elif token.value and "'" in token.value:
             token.value = token.value.replace("'", "\\'")
         yield grouping.Token(T.Text, token.value or '')
     yield grouping.Token(T.Text, "'")
     if has_nl:
         yield grouping.Token(T.Operator, ')')
示例#6
0
 def nl(self):
     # TODO: newline character should be configurable
     ws = '\n' + (self.char * ((self.indent * self.width) + self.offset))
     return grouping.Token(T.Whitespace, ws)