Exemple #1
0
def _ex_route_print(state):
    command = TokenCommand('print')
    command.addressable = True
    command.cooperates_with_global = True

    # TODO [review] count param looks unused.
    params = {'count': '', 'flags': []}

    while True:
        c = state.consume()

        state.skip(' ')
        state.ignore()

        if c == state.EOF:
            command.params = params

            return None, [command, TokenEof()]

        if c.isdigit():
            state.match(r'\d*')
            params['count'] = state.emit()
            continue

        m = state.expect_match(r'[l#p]+')
        params['flags'] = list(m.group(0))
        state.ignore()
        state.expect_eof()
        break

    command.params = params

    return None, [command, TokenEof()]
Exemple #2
0
def _ex_route_delete(state):
    command = TokenCommand('delete')
    command.addressable = True

    params = {'register': '"', 'count': None}

    state.skip(' ')
    state.ignore()

    c = state.consume()
    if c == state.EOF:
        command.params = params

        return None, [command, TokenEof()]

    state.backup()
    state.skip(' ')
    state.ignore()

    m = state.expect_match(
        r'(?P<register>[a-zA-Z0-9"])(?:\s+(?P<count>\d+))?\s*$')

    params.update(m.groupdict())
    if params['count']:
        raise NotImplementedError('parameter not implemented')

    command.params = params

    return None, [command, TokenEof()]
Exemple #3
0
def _ex_route_wqall(state):
    command = TokenCommand('wqall')
    command.addressable = True
    params = {'++': ''}

    state.skip(' ')
    state.ignore()

    c = state.consume()
    if c == '+':
        state.expect('+')
        state.ignore()
        # TODO: expect_match should work with emit()
        # https://vimhelp.appspot.com/editing.txt.html#[++opt]
        m = state.expect_match(
            r'(?:f(?:ile)?f(?:ormat)?|(?:file)?enc(?:oding)?|(?:no)?bin(?:ary)?|bad|edit)(?=\s|$)',
            lambda: Exception("E474: Invalid argument"))

        name = m.group(0)

        plus_plus_translations = {
            'ff': 'fileformat',
            'bin': 'binary',
            'enc': 'fileencoding',
            'nobin': 'nobinary'
        }

        params['++'] = plus_plus_translations.get(name, name)
        state.ignore()

    state.expect_eof()

    command.params = params

    return None, [command, TokenEof()]
Exemple #4
0
def _ex_route_read(state):
    command = TokenCommand('read')

    params = {}

    state.skip(' ')
    state.ignore()

    c = state.consume()

    if c == '+':
        raise NotImplementedError('parameter not implemented')

    elif c == '!':
        m = state.match(r'(?P<cmd>.+)')
        params.update(m.groupdict())

    else:
        state.backup()
        m = state.match(r'(?P<file_name>.+)$')
        params.update(m.groupdict())

    state.expect_eof()

    command.params = params

    return command
Exemple #5
0
def _literal_route(state, name, forcable=False, **kwargs):
    command = TokenCommand(name, **kwargs)

    if forcable and state.match('!'):
        command.forced = True

    return command
Exemple #6
0
def _ex_route_print(state):
    command = TokenCommand('print')
    command.addressable = True
    command.cooperates_with_global = True

    while True:
        c = state.consume()

        state.skip(' ')
        state.ignore()

        if c == state.EOF:
            break

        c = state.consume()
        if c.isdigit():
            state.match(r'\d*')
            command.params['count'] = state.emit()
            continue

        state.backup()

        m = state.expect_match(r'[l#p]+')
        command.params['flags'] = list(m.group(0))
        state.ignore()
        state.expect_eof()
        break

    return command
Exemple #7
0
def _ex_route_edit(state):
    command = TokenCommand('edit')

    params = {'file_name': None}

    c = state.consume()
    if c == state.EOF:
        command.params = params

        return command

    bang = c == '!'
    if not bang:
        state.backup()

    while True:
        c = state.consume()
        if c == state.EOF:
            command.params = params
            command.forced = bang

            return command

        if c in ('+', '#'):
            raise NotImplementedError('parameter not implemented')

        if c != ' ':
            state.match(r'.*')
            params['file_name'] = state.emit().strip()

            state.skip(' ')
            state.ignore()
def _ex_route_setlocal(state) -> TokenCommand:
    command = TokenCommand('setlocal')
    command.params.update(
        state.expect_match(
            r'\s*(?P<option>.+?)\s*(?:=\s*(?P<value>.*))?$').groupdict())

    return command
Exemple #9
0
def _create_map_route(state, name):
    command = TokenCommand(name)

    m = state.match(r'\s*(?P<lhs>.+?)\s+(?P<rhs>.+?)\s*$')
    if m:
        command.params.update(m.groupdict())

    return command
Exemple #10
0
def _ex_route_quit(state):
    command = TokenCommand('quit')
    bang = state.consume() == '!'

    state.expect_eof()

    command.forced = bang

    return None, [command, TokenEof()]
Exemple #11
0
def _ex_route_tabprevious(state):
    command = TokenCommand('tabprevious')
    c = state.consume()
    if c == state.EOF:
        return None, [command, TokenEof()]

    command.forced = c == '!'

    return None, [command, TokenEof()]
Exemple #12
0
def _ex_route_wq(state):
    command = TokenCommand('wq')
    # TODO [review] None of the prams looks used
    params = {
        '++': None,
        'file': None,
    }

    c = state.consume()
    if c == state.EOF:
        command.params = params

        return None, [command, TokenEof()]

    bang = True if c == '!' else False
    if not bang:
        state.backup()

    c = state.consume()
    if c == '+':
        state.expect('+')
        state.ignore()

        # TODO: expect_match should work with emit()
        # https://vimhelp.appspot.com/editing.txt.html#[++opt]
        m = state.expect_match(
            r'(?:f(?:ile)?f(?:ormat)?|(?:file)?enc(?:oding)?|(?:no)?bin(?:ary)?|bad|edit)(?=\s|$)',
            lambda: Exception("E474: Invalid argument"))

        name = m.group(0)

        plus_plus_translations = {
            'ff': 'fileformat',
            'bin': 'binary',
            'enc': 'fileencoding',
            'nobin': 'nobinary',
        }

        params['++'] = plus_plus_translations.get(name, name)

        state.ignore()
        raise NotImplementedError('param not implemented')

    if c == state.EOF:
        command.params = params
        command.forced = bang

        return None, [command, TokenEof()]

    m = state.expect_match(r'.+$')
    params['file'] = m.group(0).strip()

    command.params = params
    command.forced = bang

    return None, [command, TokenEof()]
Exemple #13
0
def _ex_route_help(state):
    command = TokenCommand('help')
    match = state.expect_match(r'(?P<bang>!)?\s*(?P<subject>.+)?$').groupdict()
    params = {'subject': match['subject']}
    bang = bool(match['bang'])

    command.params = params
    command.forced = bang

    return None, [command, TokenEof()]
Exemple #14
0
def _ex_route_unabbreviate(state):
    command = TokenCommand('unabbreviate')
    params = {'lhs': None}

    m = state.expect_match(r'\s+(?P<lhs>.+?)\s*$')
    params.update(m.groupdict())

    command.params = params

    return None, [command, TokenEof()]
def _create_route(state,
                  name: str,
                  forcable: bool = False,
                  **kwargs) -> TokenCommand:
    command = TokenCommand(name, **kwargs)

    if forcable and state.match('!'):
        command.forced = True

    return command
Exemple #16
0
def _ex_route_buffers(state):
    command = TokenCommand('buffers')

    try:
        state.expect_eof()
    except ValueError:
        # TODO Use a special domain exception for exceptions raised in scans.
        raise Exception("E488: Trailing characters")

    return None, [command, TokenEof()]
Exemple #17
0
def _ex_route_shell_out(state):
    command = TokenCommand('!', target='shell_out')
    command.addressable = True
    params = {'cmd': None}

    m = state.expect_match(r'(?P<cmd>.+)$')
    params.update(m.groupdict())

    command.params = params

    return None, [command, TokenEof()]
Exemple #18
0
def _ex_route_sunmap(state):
    command = TokenCommand('sunmap')
    params = {'keys': None}

    m = state.match(r'\s*(?P<keys>.+?)\s*$')
    if m:
        params.update(m.groupdict())

    command.params = params

    return None, [command, TokenEof()]
Exemple #19
0
def _ex_route_copy(state):
    command = TokenCommand('copy')
    command.addressable = True

    params = {'address': None}
    m = state.expect_match(r'\s*(?P<address>.+?)\s*$')
    params.update(m.groupdict())

    command.params = params

    return None, [command, TokenEof()]
Exemple #20
0
def _ex_route_move(state):
    command = TokenCommand('move')
    command.addressable = True

    state.skip(' ')
    state.ignore()

    m = state.match(r'(?P<address>.*$)')
    if m:
        command.params['address'] = m.group(0).strip() or '.'

    return command
Exemple #21
0
def _ex_route_setlocal(state):
    command = TokenCommand('setlocal')
    params = {'option': None, 'value': None}

    state.skip(' ')
    state.ignore()

    m = state.expect_match(r'(?P<option>.+?)(?:[:=](?P<value>.+?))?$')
    params.update(m.groupdict())

    command.params = params

    return None, [command, TokenEof()]
Exemple #22
0
def _ex_route_let(state):
    command = TokenCommand('let')
    params = {'name': None, 'value': None}

    m = state.expect_match(
        r'(?P<name>.+?)\s*=\s*(?P<value>.+?)\s*$',
        on_error=lambda: Exception("E121: Undefined variable"))

    params.update(m.groupdict())

    command.params = params

    return None, [command, TokenEof()]
Exemple #23
0
def _ex_route_file(state):
    command = TokenCommand('file')
    bang = state.consume()
    if bang == state.EOF:
        return None, [command, TokenEof()]

    bang = bang == '!'
    if not bang:
        raise Exception("E488: Trailing characters")

    state.expect_eof(on_error=lambda: Exception("E488: Trailing characters"))

    command.forced = bang

    return None, [command, TokenEof()]
Exemple #24
0
def _ex_route_read(state):
    command = TokenCommand('read')
    params = {
        'cmd': None,
        '++': [],
        'file_name': None,
    }

    state.skip(' ')
    state.ignore()

    c = state.consume()

    if c == '+':
        state.expect('+')
        state.ignore()
        # TODO: expect_match should work with emit()
        # https://vimhelp.appspot.com/editing.txt.html#[++opt]
        m = state.expect_match(
            r'(?:f(?:ile)?f(?:ormat)?|(?:file)?enc(?:oding)?|(?:no)?bin(?:ary)?|bad|edit)(?=\s|$)',
            lambda: Exception("E474: Invalid argument"))
        name = m.group(0)

        plus_plus_translations = {
            'ff': 'fileformat',
            'bin': 'binary',
            'enc': 'fileencoding',
            'nobin': 'nobinary',
        }

        params['++'] = plus_plus_translations.get(name, name)
        state.ignore()
        raise NotImplementedError('++opt not implemented')

    elif c == '!':
        m = state.match(r'(?P<cmd>.+)')
        params.update(m.groupdict())

    else:
        state.backup()
        m = state.match(r'(?P<file_name>.+)$')
        params.update(m.groupdict())

    state.expect_eof()

    command.params = params

    return None, [command, TokenEof()]
Exemple #25
0
def _ex_route_cdd(state):
    command = TokenCommand('cdd')

    c = state.consume()
    if c == state.EOF:
        return None, [command, TokenEof()]

    bang = c == '!'
    if not bang:
        state.backup()

    state.expect_eof()

    command.forced = bang

    return None, [command, TokenEof()]
Exemple #26
0
def _ex_route_only(state):
    command = TokenCommand('only')

    bang = state.consume()
    if bang == '!':
        state.ignore()
        state.expect_eof()

        command.forced = True

        return None, [command, TokenEof()]

    # TODO [refactor] and remove assertion
    assert bang == state.EOF, 'trailing characters'

    return None, [command, TokenEof()]
Exemple #27
0
def _ex_route_move(state):
    command = TokenCommand('move')
    command.addressable = True
    params = {'address': None}

    state.skip(' ')
    state.ignore()

    m = state.match(r'(?P<address>.*$)')
    if m:
        address_command_line = m.group(0).strip() or '.'
        params['address'] = address_command_line

    command.params = params

    return None, [command, TokenEof()]
Exemple #28
0
def _ex_route_double_ampersand(state):
    command = TokenCommand('&&', target='double_ampersand')
    command.addressable = True

    params = {'flags': [], 'count': ''}

    m = state.match(r'\s*([cgr])*\s*(\d*)\s*$')

    params['flags'] = list(m.group(1)) if m.group(1) else []
    params['count'] = m.group(2) or ''

    state.expect_eof()

    command.params = params

    return None, [command, TokenEof()]
Exemple #29
0
def _ex_route_browse(state):
    command = TokenCommand('browse')
    # TODO [review] "cmd" param looks unused.
    params = {'cmd': None}

    state.skip(' ')
    state.ignore()

    m = state.match(r'(?P<cmd>.*)$')

    params.update(m.groupdict())
    if params['cmd']:
        raise NotImplementedError('parameter not implemented')

    command.params = params

    return None, [command, TokenEof()]
Exemple #30
0
def _ex_route_registers(state):
    command = TokenCommand('registers')
    # TODO [review] "names" param looks unused by ex_registers
    params = {'names': []}

    state.skip(' ')
    state.ignore()

    while True:
        c = state.consume()
        if c == state.EOF:
            command.params = params

            return None, [command, TokenEof()]
        elif c.isalpha() or c.isdigit():
            params['names'].append(c)
        else:
            raise ValueError('wrong arguments')