Moved completers to new dir; improved completers and added word completion
This commit is contained in:
3
plugins/completers/snippets_completer/cson/__init__.py
Normal file
3
plugins/completers/snippets_completer/cson/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from .parser import load, loads
|
||||
from .writer import dump, dumps
|
||||
from .speg import ParseError
|
||||
295
plugins/completers/snippets_completer/cson/parser.py
Normal file
295
plugins/completers/snippets_completer/cson/parser.py
Normal file
@@ -0,0 +1,295 @@
|
||||
from .speg import peg
|
||||
import re, sys
|
||||
|
||||
if sys.version_info[0] == 2:
|
||||
_chr = unichr
|
||||
else:
|
||||
_chr = chr
|
||||
|
||||
def load(fin):
|
||||
return loads(fin.read())
|
||||
|
||||
def loads(s):
|
||||
if isinstance(s, bytes):
|
||||
s = s.decode('utf-8')
|
||||
if s.startswith(u'\ufeff'):
|
||||
s = s[1:]
|
||||
return peg(s.replace('\r\n', '\n'), _p_root)
|
||||
|
||||
def _p_ws(p):
|
||||
p('[ \t]*')
|
||||
|
||||
def _p_nl(p):
|
||||
p(r'([ \t]*(?:#[^\n]*)?\r?\n)+')
|
||||
|
||||
def _p_ews(p):
|
||||
with p:
|
||||
p(_p_nl)
|
||||
p(_p_ws)
|
||||
|
||||
def _p_id(p):
|
||||
return p(r'[$a-zA-Z_][$0-9a-zA-Z_]*')
|
||||
|
||||
_escape_table = {
|
||||
'r': '\r',
|
||||
'n': '\n',
|
||||
't': '\t',
|
||||
'f': '\f',
|
||||
'b': '\b',
|
||||
}
|
||||
def _p_unescape(p):
|
||||
esc = p('\\\\(?:u[0-9a-fA-F]{4}|[^\n])')
|
||||
if esc[1] == 'u':
|
||||
return _chr(int(esc[2:], 16))
|
||||
return _escape_table.get(esc[1:], esc[1:])
|
||||
|
||||
_re_indent = re.compile(r'[ \t]*')
|
||||
def _p_block_str(p, c):
|
||||
p(r'{c}{c}{c}'.format(c=c))
|
||||
lines = [['']]
|
||||
with p:
|
||||
while True:
|
||||
s = p(r'(?:{c}(?!{c}{c})|[^{c}\\])*'.format(c=c))
|
||||
l = s.split('\n')
|
||||
lines[-1].append(l[0])
|
||||
lines.extend([x] for x in l[1:])
|
||||
if p(r'(?:\\\n[ \t]*)*'):
|
||||
continue
|
||||
p.commit()
|
||||
lines[-1].append(p(_p_unescape))
|
||||
p(r'{c}{c}{c}'.format(c=c))
|
||||
|
||||
lines = [''.join(l) for l in lines]
|
||||
strip_ws = len(lines) > 1
|
||||
if strip_ws and all(c in ' \t' for c in lines[-1]):
|
||||
lines.pop()
|
||||
|
||||
indent = None
|
||||
for line in lines[1:]:
|
||||
if not line:
|
||||
continue
|
||||
if indent is None:
|
||||
indent = _re_indent.match(line).group(0)
|
||||
continue
|
||||
for i, (c1, c2) in enumerate(zip(indent, line)):
|
||||
if c1 != c2:
|
||||
indent = indent[:i]
|
||||
break
|
||||
|
||||
ind_len = len(indent or '')
|
||||
if strip_ws and all(c in ' \t' for c in lines[0]):
|
||||
lines = [line[ind_len:] for line in lines[1:]]
|
||||
else:
|
||||
lines[1:] = [line[ind_len:] for line in lines[1:]]
|
||||
|
||||
return '\n'.join(lines)
|
||||
|
||||
_re_mstr_nl = re.compile(r'(?:[ \t]*\n)+[ \t]*')
|
||||
_re_mstr_trailing_nl = re.compile(_re_mstr_nl.pattern + r'\Z')
|
||||
def _p_multiline_str(p, c):
|
||||
p('{c}(?!{c}{c})(?:[ \t]*\n[ \t]*)?'.format(c=c))
|
||||
string_parts = []
|
||||
with p:
|
||||
while True:
|
||||
string_parts.append(p(r'[^{c}\\]*'.format(c=c)))
|
||||
if p(r'(?:\\\n[ \t]*)*'):
|
||||
string_parts.append('')
|
||||
continue
|
||||
p.commit()
|
||||
string_parts.append(p(_p_unescape))
|
||||
p(c)
|
||||
string_parts[-1] = _re_mstr_trailing_nl.sub('', string_parts[-1])
|
||||
string_parts[::2] = [_re_mstr_nl.sub(' ', part) for part in string_parts[::2]]
|
||||
return ''.join(string_parts)
|
||||
|
||||
def _p_string(p):
|
||||
with p:
|
||||
return p(_p_block_str, '"')
|
||||
with p:
|
||||
return p(_p_block_str, "'")
|
||||
with p:
|
||||
return p(_p_multiline_str, '"')
|
||||
return p(_p_multiline_str, "'")
|
||||
|
||||
def _p_array_value(p):
|
||||
with p:
|
||||
p(_p_nl)
|
||||
return p(_p_object)
|
||||
with p:
|
||||
p(_p_ws)
|
||||
return p(_p_line_object)
|
||||
p(_p_ews)
|
||||
return p(_p_simple_value)
|
||||
|
||||
def _p_key(p):
|
||||
with p:
|
||||
return p(_p_id)
|
||||
return p(_p_string)
|
||||
|
||||
def _p_flow_kv(p):
|
||||
k = p(_p_key)
|
||||
p(_p_ews)
|
||||
p(':')
|
||||
with p:
|
||||
p(_p_nl)
|
||||
return k, p(_p_object)
|
||||
with p:
|
||||
p(_p_ws)
|
||||
return k, p(_p_line_object)
|
||||
p(_p_ews)
|
||||
return k, p(_p_simple_value)
|
||||
|
||||
def _p_flow_obj_sep(p):
|
||||
with p:
|
||||
p(_p_ews)
|
||||
p(',')
|
||||
p(_p_ews)
|
||||
return
|
||||
|
||||
p(_p_nl)
|
||||
p(_p_ws)
|
||||
|
||||
def _p_simple_value(p):
|
||||
with p:
|
||||
p('null')
|
||||
return None
|
||||
|
||||
with p:
|
||||
p('false')
|
||||
return False
|
||||
with p:
|
||||
p('true')
|
||||
return True
|
||||
|
||||
with p:
|
||||
return int(p('0b[01]+')[2:], 2)
|
||||
with p:
|
||||
return int(p('0o[0-7]+')[2:], 8)
|
||||
with p:
|
||||
return int(p('0x[0-9a-fA-F]+')[2:], 16)
|
||||
with p:
|
||||
return float(p(r'-?(?:[1-9][0-9]*|0)?\.[0-9]+(?:[Ee][\+-]?[0-9]+)?|(?:[1-9][0-9]*|0)(?:\.[0-9]+)?[Ee][\+-]?[0-9]+'))
|
||||
with p:
|
||||
return int(p('-?[1-9][0-9]*|0'), 10)
|
||||
|
||||
with p:
|
||||
return p(_p_string)
|
||||
|
||||
with p:
|
||||
p(r'\[')
|
||||
r = []
|
||||
with p:
|
||||
p.set('I', '')
|
||||
r.append(p(_p_array_value))
|
||||
with p:
|
||||
while True:
|
||||
with p:
|
||||
p(_p_ews)
|
||||
p(',')
|
||||
rr = p(_p_array_value)
|
||||
if not p:
|
||||
p(_p_nl)
|
||||
with p:
|
||||
rr = p(_p_object)
|
||||
if not p:
|
||||
p(_p_ews)
|
||||
rr = p(_p_simple_value)
|
||||
r.append(rr)
|
||||
p.commit()
|
||||
with p:
|
||||
p(_p_ews)
|
||||
p(',')
|
||||
p(_p_ews)
|
||||
p(r'\]')
|
||||
return r
|
||||
|
||||
p(r'\{')
|
||||
|
||||
r = {}
|
||||
p(_p_ews)
|
||||
with p:
|
||||
p.set('I', '')
|
||||
k, v = p(_p_flow_kv)
|
||||
r[k] = v
|
||||
with p:
|
||||
while True:
|
||||
p(_p_flow_obj_sep)
|
||||
k, v = p(_p_flow_kv)
|
||||
r[k] = v
|
||||
p.commit()
|
||||
p(_p_ews)
|
||||
with p:
|
||||
p(',')
|
||||
p(_p_ews)
|
||||
p(r'\}')
|
||||
return r
|
||||
|
||||
def _p_line_kv(p):
|
||||
k = p(_p_key)
|
||||
p(_p_ws)
|
||||
p(':')
|
||||
p(_p_ws)
|
||||
with p:
|
||||
p(_p_nl)
|
||||
p(p.get('I'))
|
||||
return k, p(_p_indented_object)
|
||||
with p:
|
||||
return k, p(_p_line_object)
|
||||
with p:
|
||||
return k, p(_p_simple_value)
|
||||
p(_p_nl)
|
||||
p(p.get('I'))
|
||||
p('[ \t]')
|
||||
p(_p_ws)
|
||||
return k, p(_p_simple_value)
|
||||
|
||||
def _p_line_object(p):
|
||||
k, v = p(_p_line_kv)
|
||||
r = { k: v }
|
||||
with p:
|
||||
while True:
|
||||
p(_p_ws)
|
||||
p(',')
|
||||
p(_p_ws)
|
||||
k, v = p(_p_line_kv)
|
||||
r[k] = v # uniqueness
|
||||
p.commit()
|
||||
return r
|
||||
|
||||
def _p_object(p):
|
||||
p.set('I', p.get('I') + p('[ \t]*'))
|
||||
r = p(_p_line_object)
|
||||
with p:
|
||||
while True:
|
||||
p(_p_ws)
|
||||
with p:
|
||||
p(',')
|
||||
p(_p_nl)
|
||||
p(p.get('I'))
|
||||
rr = p(_p_line_object)
|
||||
r.update(rr) # unqueness
|
||||
p.commit()
|
||||
return r
|
||||
|
||||
def _p_indented_object(p):
|
||||
p.set('I', p.get('I') + p('[ \t]'))
|
||||
return p(_p_object)
|
||||
|
||||
def _p_root(p):
|
||||
with p:
|
||||
p(_p_nl)
|
||||
|
||||
with p:
|
||||
p.set('I', '')
|
||||
r = p(_p_object)
|
||||
p(_p_ws)
|
||||
with p:
|
||||
p(',')
|
||||
|
||||
if not p:
|
||||
p(_p_ws)
|
||||
r = p(_p_simple_value)
|
||||
|
||||
p(_p_ews)
|
||||
p(p.eof)
|
||||
return r
|
||||
@@ -0,0 +1 @@
|
||||
from .peg import peg, ParseError
|
||||
147
plugins/completers/snippets_completer/cson/speg/peg.py
Normal file
147
plugins/completers/snippets_completer/cson/speg/peg.py
Normal file
@@ -0,0 +1,147 @@
|
||||
import sys, re
|
||||
|
||||
class ParseError(Exception):
|
||||
def __init__(self, msg, text, offset, line, col):
|
||||
self.msg = msg
|
||||
self.text = text
|
||||
self.offset = offset
|
||||
self.line = line
|
||||
self.col = col
|
||||
super(ParseError, self).__init__(msg, offset, line, col)
|
||||
|
||||
if sys.version_info[0] == 2:
|
||||
_basestr = basestring
|
||||
else:
|
||||
_basestr = str
|
||||
|
||||
def peg(s, r):
|
||||
p = _Peg(s)
|
||||
try:
|
||||
return p(r)
|
||||
except _UnexpectedError as e:
|
||||
offset = max(p._errors)
|
||||
err = p._errors[offset]
|
||||
raise ParseError(err.msg, s, offset, err.line, err.col)
|
||||
|
||||
class _UnexpectedError(RuntimeError):
|
||||
def __init__(self, state, expr):
|
||||
self.state = state
|
||||
self.expr = expr
|
||||
|
||||
class _PegState:
|
||||
def __init__(self, pos, line, col):
|
||||
self.pos = pos
|
||||
self.line = line
|
||||
self.col = col
|
||||
self.vars = {}
|
||||
self.commited = False
|
||||
|
||||
class _PegError:
|
||||
def __init__(self, msg, line, col):
|
||||
self.msg = msg
|
||||
self.line = line
|
||||
self.col = col
|
||||
|
||||
class _Peg:
|
||||
def __init__(self, s):
|
||||
self._s = s
|
||||
self._states = [_PegState(0, 1, 1)]
|
||||
self._errors = {}
|
||||
self._re_cache = {}
|
||||
|
||||
def __call__(self, r, *args, **kw):
|
||||
if isinstance(r, _basestr):
|
||||
compiled = self._re_cache.get(r)
|
||||
if not compiled:
|
||||
compiled = re.compile(r)
|
||||
self._re_cache[r] = compiled
|
||||
st = self._states[-1]
|
||||
m = compiled.match(self._s[st.pos:])
|
||||
if not m:
|
||||
self.error(expr=r, err=kw.get('err'))
|
||||
|
||||
ms = m.group(0)
|
||||
st.pos += len(ms)
|
||||
nl_pos = ms.rfind('\n')
|
||||
if nl_pos < 0:
|
||||
st.col += len(ms)
|
||||
else:
|
||||
st.col = len(ms) - nl_pos
|
||||
st.line += ms[:nl_pos].count('\n') + 1
|
||||
return ms
|
||||
else:
|
||||
kw.pop('err', None)
|
||||
return r(self, *args, **kw)
|
||||
|
||||
def __repr__(self):
|
||||
pos = self._states[-1].pos
|
||||
vars = {}
|
||||
for st in self._states:
|
||||
vars.update(st.vars)
|
||||
return '_Peg(%r, %r)' % (self._s[:pos] + '*' + self._s[pos:], vars)
|
||||
|
||||
@staticmethod
|
||||
def eof(p):
|
||||
if p._states[-1].pos != len(p._s):
|
||||
p.error()
|
||||
|
||||
def error(self, err=None, expr=None):
|
||||
st = self._states[-1]
|
||||
if err is None:
|
||||
err = 'expected {!r}, found {!r}'.format(expr, self._s[st.pos:st.pos+4])
|
||||
self._errors[st.pos] = _PegError(err, st.line, st.col)
|
||||
raise _UnexpectedError(st, expr)
|
||||
|
||||
def get(self, key, default=None):
|
||||
for state in self._states[::-1]:
|
||||
if key in state.vars:
|
||||
return state.vars[key][0]
|
||||
return default
|
||||
|
||||
def set(self, key, value):
|
||||
self._states[-1].vars[key] = value, False
|
||||
|
||||
def set_global(self, key, value):
|
||||
self._states[-1].vars[key] = value, True
|
||||
|
||||
def opt(self, *args, **kw):
|
||||
with self:
|
||||
return self(*args, **kw)
|
||||
|
||||
def not_(self, s, *args, **kw):
|
||||
with self:
|
||||
self(s)
|
||||
self.error()
|
||||
|
||||
def __enter__(self):
|
||||
self._states[-1].committed = False
|
||||
self._states.append(_PegState(self._states[-1].pos, self._states[-1].line, self._states[-1].col))
|
||||
|
||||
def __exit__(self, type, value, traceback):
|
||||
if type is None:
|
||||
self.commit()
|
||||
self._states.pop()
|
||||
return type == _UnexpectedError
|
||||
|
||||
def commit(self):
|
||||
cur = self._states[-1]
|
||||
prev = self._states[-2]
|
||||
|
||||
for key in cur.vars:
|
||||
val, g = cur.vars[key]
|
||||
if not g:
|
||||
continue
|
||||
if key in prev.vars:
|
||||
prev.vars[key] = val, prev.vars[key][1]
|
||||
else:
|
||||
prev.vars[key] = val, True
|
||||
|
||||
prev.pos = cur.pos
|
||||
prev.line = cur.line
|
||||
prev.col = cur.col
|
||||
prev.committed = True
|
||||
|
||||
def __nonzero__(self):
|
||||
return self._states[-1].committed
|
||||
|
||||
__bool__ = __nonzero__
|
||||
191
plugins/completers/snippets_completer/cson/writer.py
Normal file
191
plugins/completers/snippets_completer/cson/writer.py
Normal file
@@ -0,0 +1,191 @@
|
||||
import re, json, sys
|
||||
|
||||
if sys.version_info[0] == 2:
|
||||
def _is_num(o):
|
||||
return isinstance(o, int) or isinstance(o, long) or isinstance(o, float)
|
||||
def _stringify(o):
|
||||
if isinstance(o, str):
|
||||
return unicode(o)
|
||||
if isinstance(o, unicode):
|
||||
return o
|
||||
return None
|
||||
else:
|
||||
def _is_num(o):
|
||||
return isinstance(o, int) or isinstance(o, float)
|
||||
def _stringify(o):
|
||||
if isinstance(o, bytes):
|
||||
return o.decode()
|
||||
if isinstance(o, str):
|
||||
return o
|
||||
return None
|
||||
|
||||
_id_re = re.compile(r'[$a-zA-Z_][$0-9a-zA-Z_]*\Z')
|
||||
|
||||
class CSONEncoder:
|
||||
def __init__(self, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False,
|
||||
indent=None, default=None):
|
||||
self._skipkeys = skipkeys
|
||||
self._ensure_ascii = ensure_ascii
|
||||
self._allow_nan = allow_nan
|
||||
self._sort_keys = sort_keys
|
||||
self._indent = ' ' * (indent or 4)
|
||||
self._default = default
|
||||
if check_circular:
|
||||
self._obj_stack = set()
|
||||
else:
|
||||
self._obj_stack = None
|
||||
|
||||
def _format_simple_val(self, o):
|
||||
if o is None:
|
||||
return 'null'
|
||||
if isinstance(o, bool):
|
||||
return 'true' if o else 'false'
|
||||
if _is_num(o):
|
||||
return str(o)
|
||||
s = _stringify(o)
|
||||
if s is not None:
|
||||
return self._escape_string(s)
|
||||
return None
|
||||
|
||||
def _escape_string(self, s):
|
||||
r = json.dumps(s, ensure_ascii=self._ensure_ascii)
|
||||
return u"'{}'".format(r[1:-1].replace("'", r"\'"))
|
||||
|
||||
def _escape_key(self, s):
|
||||
if s is None or isinstance(s, bool) or _is_num(s):
|
||||
s = str(s)
|
||||
s = _stringify(s)
|
||||
if s is None:
|
||||
if self._skipkeys:
|
||||
return None
|
||||
raise TypeError('keys must be a string')
|
||||
if not _id_re.match(s):
|
||||
return self._escape_string(s)
|
||||
return s
|
||||
|
||||
def _push_obj(self, o):
|
||||
if self._obj_stack is not None:
|
||||
if id(o) in self._obj_stack:
|
||||
raise ValueError('Circular reference detected')
|
||||
self._obj_stack.add(id(o))
|
||||
|
||||
def _pop_obj(self, o):
|
||||
if self._obj_stack is not None:
|
||||
self._obj_stack.remove(id(o))
|
||||
|
||||
def _encode(self, o, obj_val=False, indent='', force_flow=False):
|
||||
if isinstance(o, list):
|
||||
if not o:
|
||||
if obj_val:
|
||||
yield ' []\n'
|
||||
else:
|
||||
yield indent
|
||||
yield '[]\n'
|
||||
else:
|
||||
if obj_val:
|
||||
yield ' [\n'
|
||||
else:
|
||||
yield indent
|
||||
yield '[\n'
|
||||
indent = indent + self._indent
|
||||
self._push_obj(o)
|
||||
for v in o:
|
||||
for chunk in self._encode(v, obj_val=False, indent=indent, force_flow=True):
|
||||
yield chunk
|
||||
self._pop_obj(o)
|
||||
yield indent[:-len(self._indent)]
|
||||
yield ']\n'
|
||||
elif isinstance(o, dict):
|
||||
items = [(self._escape_key(k), v) for k, v in o.items()]
|
||||
if self._skipkeys:
|
||||
items = [(k, v) for k, v in items if k is not None]
|
||||
if self._sort_keys:
|
||||
items.sort()
|
||||
if force_flow or not items:
|
||||
if not items:
|
||||
if obj_val:
|
||||
yield ' {}\n'
|
||||
else:
|
||||
yield indent
|
||||
yield '{}\n'
|
||||
else:
|
||||
if obj_val:
|
||||
yield ' {\n'
|
||||
else:
|
||||
yield indent
|
||||
yield '{\n'
|
||||
indent = indent + self._indent
|
||||
self._push_obj(o)
|
||||
for k, v in items:
|
||||
yield indent
|
||||
yield k
|
||||
yield ':'
|
||||
for chunk in self._encode(v, obj_val=True, indent=indent + self._indent, force_flow=False):
|
||||
yield chunk
|
||||
self._pop_obj(o)
|
||||
yield indent[:-len(self._indent)]
|
||||
yield '}\n'
|
||||
else:
|
||||
if obj_val:
|
||||
yield '\n'
|
||||
self._push_obj(o)
|
||||
for k, v in items:
|
||||
yield indent
|
||||
yield k
|
||||
yield ':'
|
||||
for chunk in self._encode(v, obj_val=True, indent=indent + self._indent, force_flow=False):
|
||||
yield chunk
|
||||
self._pop_obj(o)
|
||||
else:
|
||||
v = self._format_simple_val(o)
|
||||
if v is None:
|
||||
self._push_obj(o)
|
||||
v = self.default(o)
|
||||
for chunk in self._encode(v, obj_val=obj_val, indent=indent, force_flow=force_flow):
|
||||
yield chunk
|
||||
self._pop_obj(o)
|
||||
else:
|
||||
if obj_val:
|
||||
yield ' '
|
||||
else:
|
||||
yield indent
|
||||
yield v
|
||||
yield '\n'
|
||||
|
||||
def iterencode(self, o):
|
||||
return self._encode(o)
|
||||
|
||||
def encode(self, o):
|
||||
return ''.join(self.iterencode(o))
|
||||
|
||||
def default(self, o):
|
||||
if self._default is None:
|
||||
raise TypeError('Cannot serialize an object of type {}'.format(type(o).__name__))
|
||||
return self._default(o)
|
||||
|
||||
def dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None,
|
||||
indent=None, default=None, sort_keys=False, **kw):
|
||||
if indent is None and cls is None:
|
||||
return json.dump(obj, fp, skipkeys=skipkeys, ensure_ascii=ensure_ascii, check_circular=check_circular,
|
||||
allow_nan=allow_nan, default=default, sort_keys=sort_keys, separators=(',', ':'))
|
||||
|
||||
if cls is None:
|
||||
cls = CSONEncoder
|
||||
encoder = cls(skipkeys=skipkeys, ensure_ascii=ensure_ascii, check_circular=check_circular,
|
||||
allow_nan=allow_nan, sort_keys=sort_keys, indent=indent, default=default, **kw)
|
||||
|
||||
for chunk in encoder.iterencode(obj):
|
||||
fp.write(chunk)
|
||||
|
||||
def dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None,
|
||||
default=None, sort_keys=False, **kw):
|
||||
if indent is None and cls is None:
|
||||
return json.dumps(obj, skipkeys=skipkeys, ensure_ascii=ensure_ascii, check_circular=check_circular,
|
||||
allow_nan=allow_nan, default=default, sort_keys=sort_keys, separators=(',', ':'))
|
||||
|
||||
if cls is None:
|
||||
cls = CSONEncoder
|
||||
encoder = cls(skipkeys=skipkeys, ensure_ascii=ensure_ascii, check_circular=check_circular,
|
||||
allow_nan=allow_nan, sort_keys=sort_keys, indent=indent, default=default, **kw)
|
||||
|
||||
return encoder.encode(obj)
|
||||
Reference in New Issue
Block a user