Example #1
0
    def layout_handler_newline_optional(self, dispatcher, node, before, after,
                                        prev):
        # simply render the newline with an implicit sourcemap line/col, if
        # not already preceded or followed by a newline
        idx = len(dispatcher.newline_str)

        def fc(s):
            return '' if s is None else s[:idx]

        def lc(s):
            return '' if s is None else s[-idx:]

        # include standard ones plus whatever else that was provided, i.e.
        # the typical <CR><LF>
        newline_strs = {'\r', '\n', dispatcher.newline_str}

        if (before and lc(before) in '\r\n'):
            # not needed since this is the beginning
            return
        # if no new lines in any of the checked characters
        if not newline_strs & {lc(before), fc(after), lc(prev)}:
            yield StreamFragment(dispatcher.newline_str, 0, 0, None, None)

        for chunk in self._generate_indents(dispatcher):
            yield chunk
Example #2
0
def token_handler_str_default(token,
                              dispatcher,
                              node,
                              subnode,
                              sourcepath_stack=(None, )):
    """
    Standard token handler that will return the value, ignoring any
    tokens or strings that have been remapped.
    """

    if isinstance(token.pos, int):
        _, lineno, colno = node.getpos(subnode, token.pos)
    else:
        lineno, colno = None, None
    yield StreamFragment(subnode, lineno, colno, None, sourcepath_stack[-1])
Example #3
0
def token_handler_unobfuscate(token,
                              dispatcher,
                              node,
                              subnode,
                              sourcepath_stack=(None, )):
    """
    A token handler that will resolve and return the original identifier
    value.
    """

    original = (node.value if isinstance(node, Identifier)
                and node.value != subnode else None)

    if isinstance(token.pos, int):
        _, lineno, colno = node.getpos(original or subnode, token.pos)
    else:
        lineno, colno = None, None

    yield StreamFragment(subnode, lineno, colno, original,
                         sourcepath_stack[-1])
Example #4
0
def layout_handler_semicolon(dispatcher, node, before, after, prev):
    # required layout handler for the EndStatement Format rule.
    _, lineno, colno = node.getpos(';', 0)
    yield StreamFragment(';', lineno, colno, None, None)
Example #5
0
def layout_handler_newline_simple(dispatcher, node, before, after, prev):
    # simply render the newline with an implicit sourcemap line/col
    yield StreamFragment(dispatcher.newline_str, 0, 0, None, None)
Example #6
0
def layout_handler_closebrace(dispatcher, node, before, after, prev):
    # required layout handler for the CloseBlock Format rule.
    _, lineno, colno = node.getpos('}', 0)
    yield StreamFragment('}', lineno, colno, None, None)
Example #7
0
def layout_handler_semicolon_optional(dispatcher, node, before, after, prev):
    # only yield if there is something after
    if after:
        _, lineno, colno = node.getpos(';', 0)
        yield StreamFragment(';', lineno, colno, None, None)
Example #8
0
    Indent,
    Dedent,
    LineComment as RuleTypeLineComment,
    BlockComment as RuleTypeBlockComment,
)
from calmjs.parse.lexers.es5 import PATT_LINE_CONTINUATION

required_space = re.compile(r'^(?:\w\w|\+\+|\-\-|\w\$|\$\w)$')

# the various assignments symbols; for dealing with pretty spacing
assignment_tokens = {
    '*=', '/=', '%=', '+=', '-=', '<<=', '>>=', '>>>=', '&=', '^=', '|=', '='
}
# other symbols
optional_rhs_space_tokens = {';', ')', None}
space_imply = StreamFragment(' ', 0, 0, None, None)
space_drop = StreamFragment(' ', None, None, None, None)


def rule_handler_noop(*a, **kw):
    # a no op for layouts
    return
    yield  # pragma: no cover


def token_handler_str_default(token,
                              dispatcher,
                              node,
                              subnode,
                              sourcepath_stack=(None, )):
    """
Example #9
0
 def layout_handler_newline(self, dispatcher, node, before, after, prev):
     # simply render the newline with an implicit sourcemap line/col
     yield StreamFragment(dispatcher.newline_str, 0, 0, None, None)
     for chunk in self._generate_indents(dispatcher):
         yield chunk
Example #10
0
 def _generate_indents(self, dispatcher):
     s = self.indent_str if self.indent_str else dispatcher.indent_str
     indents = s * self._level
     if indents:
         yield StreamFragment(indents, None, None, None, None)