~ubuntu-branches/ubuntu/vivid/vim-ultisnips/vivid

« back to all changes in this revision

Viewing changes to pythonx/UltiSnips/snippet/parsing/ultisnips.py

  • Committer: Package Import Robot
  • Author(s): Michael Fladischer
  • Date: 2014-10-12 18:11:54 UTC
  • Revision ID: package-import@ubuntu.com-20141012181154-1jeoj467dh2l5f2e
Tags: upstream-3.0
ImportĀ upstreamĀ versionĀ 3.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
# encoding: utf-8
 
3
 
 
4
"""Parses a UltiSnips snippet definition and launches it into Vim."""
 
5
 
 
6
from UltiSnips.snippet.parsing._base import tokenize_snippet_text, finalize
 
7
from UltiSnips.snippet.parsing._lexer import EscapeCharToken, \
 
8
    VisualToken, TransformationToken, TabStopToken, MirrorToken, \
 
9
    PythonCodeToken, VimLCodeToken, ShellCodeToken
 
10
from UltiSnips.text_objects import EscapedChar, Mirror, PythonCode, \
 
11
        ShellCode, TabStop, Transformation, VimLCode, Visual
 
12
 
 
13
_TOKEN_TO_TEXTOBJECT = {
 
14
    EscapeCharToken: EscapedChar,
 
15
    VisualToken: Visual,
 
16
    ShellCodeToken: ShellCode,
 
17
    PythonCodeToken: PythonCode,
 
18
    VimLCodeToken: VimLCode,
 
19
}
 
20
 
 
21
__ALLOWED_TOKENS = [
 
22
    EscapeCharToken, VisualToken, TransformationToken, TabStopToken,
 
23
    MirrorToken, PythonCodeToken, VimLCodeToken, ShellCodeToken
 
24
]
 
25
 
 
26
def _resolve_ambiguity(all_tokens, seen_ts):
 
27
    """$1 could be a Mirror or a TabStop. This figures this out."""
 
28
    for parent, token in all_tokens:
 
29
        if isinstance(token, MirrorToken):
 
30
            if token.number not in seen_ts:
 
31
                seen_ts[token.number] = TabStop(parent, token)
 
32
            else:
 
33
                Mirror(parent, seen_ts[token.number], token)
 
34
 
 
35
def _create_transformations(all_tokens, seen_ts):
 
36
    """Create the objects that need to know about tabstops."""
 
37
    for parent, token in all_tokens:
 
38
        if isinstance(token, TransformationToken):
 
39
            if token.number not in seen_ts:
 
40
                raise RuntimeError(
 
41
                    "Tabstop %i is not known but is used by a Transformation"
 
42
                    % token.number)
 
43
            Transformation(parent, seen_ts[token.number], token)
 
44
 
 
45
 
 
46
def parse_and_instantiate(parent_to, text, indent):
 
47
    """Parses a snippet definition in UltiSnips format from 'text' assuming the
 
48
    current 'indent'. Will instantiate all the objects and link them as
 
49
    children to parent_to. Will also put the initial text into Vim."""
 
50
    all_tokens, seen_ts = tokenize_snippet_text(parent_to, text, indent,
 
51
            __ALLOWED_TOKENS, __ALLOWED_TOKENS, _TOKEN_TO_TEXTOBJECT)
 
52
    _resolve_ambiguity(all_tokens, seen_ts)
 
53
    _create_transformations(all_tokens, seen_ts)
 
54
    finalize(all_tokens, seen_ts, parent_to)