Skip to content

Commit ab675cd

Browse files
committed
move pat_vim2py table to pycompiler.vim
1 parent fda5e96 commit ab675cd

File tree

2 files changed

+98
-73
lines changed

2 files changed

+98
-73
lines changed

py/pycompiler.vim

Lines changed: 95 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ let s:opprec[s:NODE_ENV] = 9
6969
let s:opprec[s:NODE_REG] = 9
7070
" vint: +ProhibitUsingUndeclaredVariable
7171

72+
function! s:Err(msg, pos) abort
73+
return printf('vimlparser: pycompiler: %s: line %d col %d', a:msg, a:pos.lnum, a:pos.col)
74+
endfunction
75+
7276
" Reserved Python keywords (dict for faster lookup).
7377
let s:reserved_keywords = {
7478
\ 'False': 1,
@@ -673,27 +677,33 @@ function s:PythonCompiler.compile_sequalcs(node)
673677
endfunction
674678

675679
function s:PythonCompiler.compile_match(node)
676-
return printf('viml_eqreg(%s, %s)', self.compile(a:node.left), self.compile(a:node.right))
680+
let right = s:compile_regpat_node(a:node.right)
681+
return printf('viml_eqreg(%s, %s)', self.compile(a:node.right), right)
677682
endfunction
678683

679684
function s:PythonCompiler.compile_matchci(node)
680-
return printf('viml_eqregq(%s, %s)', self.compile(a:node.left), self.compile(a:node.right))
685+
let right = s:compile_regpat_node(a:node.right)
686+
return printf('viml_eqregq(%s, %s)', self.compile(a:node.right), right)
681687
endfunction
682688

683689
function s:PythonCompiler.compile_matchcs(node)
684-
return printf('viml_eqregh(%s, %s)', self.compile(a:node.left), self.compile(a:node.right))
690+
let right = s:compile_regpat_node(a:node.right)
691+
return printf('viml_eqregh(%s, %s)', self.compile(a:node.right), right)
685692
endfunction
686693

687694
function s:PythonCompiler.compile_nomatch(node)
688-
return printf('not viml_eqreg(%s, %s)', self.compile(a:node.left), self.compile(a:node.right))
695+
let right = s:compile_regpat_node(a:node.right)
696+
return printf('not viml_eqreg(%s, %s)', self.compile(a:node.right), right)
689697
endfunction
690698

691699
function s:PythonCompiler.compile_nomatchci(node)
692-
return printf('not viml_eqregq(%s, %s, flags=re.IGNORECASE)', self.compile(a:node.left), self.compile(a:node.right))
700+
let right = s:compile_regpat_node(a:node.right)
701+
return printf('not viml_eqregq(%s, %s, flags=re.IGNORECASE)', self.compile(a:node.right), right)
693702
endfunction
694703

695704
function s:PythonCompiler.compile_nomatchcs(node)
696-
return printf('not viml_eqregh(%s, %s)', self.compile(a:node.left), self.compile(a:node.right))
705+
let right = s:compile_regpat_node(a:node.right)
706+
return printf('not viml_eqregh(%s, %s)', self.compile(a:node.right), right)
697707
endfunction
698708

699709
function s:PythonCompiler.compile_is(node)
@@ -879,6 +889,85 @@ function s:PythonCompiler.compile_op2(node, op)
879889
endfunction
880890

881891

892+
let s:pat_vim2py = {
893+
\ '[0-9a-zA-Z]': '[0-9a-zA-Z]',
894+
\ '[@*!=><&~#]': '[@*!=><&~#]',
895+
\ '\\<ARGOPT\\>': '\\bARGOPT\\b',
896+
\ '\\<BANG\\>': '\\bBANG\\b',
897+
\ '\\<EDITCMD\\>': '\\bEDITCMD\\b',
898+
\ '\\<NOTRLCOM\\>': '\\bNOTRLCOM\\b',
899+
\ '\\<TRLBAR\\>': '\\bTRLBAR\\b',
900+
\ '\\<USECTRLV\\>': '\\bUSECTRLV\\b',
901+
\ '\\<USERCMD\\>': '\\bUSERCMD\\b',
902+
\ '\\<\\(XFILE\\|FILES\\|FILE1\\)\\>': '\\b(XFILE|FILES|FILE1)\\b',
903+
\ '\\S': '\\S',
904+
\ '\\a': '[A-Za-z]',
905+
\ '\\d': '\\d',
906+
\ '\\h': '[A-Za-z_]',
907+
\ '\\s': '\\s',
908+
\ '\\v^d%[elete][lp]$': '^d(elete|elet|ele|el|e)[lp]$',
909+
\ '\\v^s%(c[^sr][^i][^p]|g|i[^mlg]|I|r[^e])':
910+
\ '^s(c[^sr][^i][^p]|g|i[^mlg]|I|r[^e])',
911+
\ '\\w': '[0-9A-Za-z_]',
912+
\ '\\w\\|[:#]': '[0-9A-Za-z_]|[:#]',
913+
\ '\\x': '[0-9A-Fa-f]',
914+
\ '^++': '^\+\+',
915+
\ '^++bad=\\(keep\\|drop\\|.\\)\\>': '^\\+\\+bad=(keep|drop|.)\\b',
916+
\ '^++bad=drop': '^\\+\\+bad=drop',
917+
\ '^++bad=keep': '^\\+\\+bad=keep',
918+
\ '^++bin\\>': '^\\+\\+bin\\b',
919+
\ '^++edit\\>': '^\\+\\+edit\\b',
920+
\ '^++enc=\\S': '^\\+\\+enc=\\S',
921+
\ '^++encoding=\\S': '^\\+\\+encoding=\\S',
922+
\ '^++ff=\\(dos\\|unix\\|mac\\)\\>': '^\\+\\+ff=(dos|unix|mac)\\b',
923+
\ '^++fileformat=\\(dos\\|unix\\|mac\\)\\>':
924+
\ '^\\+\\+fileformat=(dos|unix|mac)\\b',
925+
\ '^++nobin\\>': '^\\+\\+nobin\\b',
926+
\ '^[A-Z]': '^[A-Z]',
927+
\ '^\\$\\w\\+': '^\\$[0-9A-Za-z_]+',
928+
\ '^\\(!\\|global\\|vglobal\\)$': '^(!|global|vglobal)$',
929+
\ '^\\(WHILE\\|FOR\\)$': '^(WHILE|FOR)$',
930+
\ '^\\(vimgrep\\|vimgrepadd\\|lvimgrep\\|lvimgrepadd\\)$':
931+
\ '^(vimgrep|vimgrepadd|lvimgrep|lvimgrepadd)$',
932+
\ '^\\d': '^\\d',
933+
\ '^\\h': '^[A-Za-z_]',
934+
\ '^\\s': '^\\s',
935+
\ '^\\s*\\\\': '^\\s*\\\\',
936+
\ '^[ \\t]$': '^[ \\t]$',
937+
\ '^[A-Za-z]$': '^[A-Za-z]$',
938+
\ '^[0-9A-Za-z]$': '^[0-9A-Za-z]$',
939+
\ '^[0-9]$': '^[0-9]$',
940+
\ '^[0-9A-Fa-f]$': '^[0-9A-Fa-f]$',
941+
\ '^[0-9A-Za-z_]$': '^[0-9A-Za-z_]$',
942+
\ '^[A-Za-z_]$': '^[A-Za-z_]$',
943+
\ '^[0-9A-Za-z_:#]$': '^[0-9A-Za-z_:#]$',
944+
\ '^[A-Za-z_][0-9A-Za-z_]*$': '^[A-Za-z_][0-9A-Za-z_]*$',
945+
\ '^[A-Z]$': '^[A-Z]$',
946+
\ '^[a-z]$': '^[a-z]$',
947+
\ '^[vgslabwt]:$\\|^\\([vgslabwt]:\\)\\?[A-Za-z_][0-9A-Za-z_#]*$':
948+
\ '^[vgslabwt]:$|^([vgslabwt]:)?[A-Za-z_][0-9A-Za-z_#]*$',
949+
\ '^[0-7]$': '^[0-7]$',
950+
\ '^[0-9A-Fa-f][0-9A-Fa-f]$': '^[0-9A-Fa-f][0-9A-Fa-f]$',
951+
\ '^\.[0-9A-Fa-f]$': '^\.[0-9A-Fa-f]$',
952+
\ '^[0-9A-Fa-f][^0-9A-Fa-f]$': '^[0-9A-Fa-f][^0-9A-Fa-f]$',
953+
\}
954+
955+
function! s:compile_regpat_node(node) abort
956+
" vint: -ProhibitUsingUndeclaredVariable
957+
if a:node.type !=# s:NODE_STRING
958+
" vint: +ProhibitUsingUndeclaredVariable
959+
throw s:Err(printf('expected regexp string node, but got node.type = %d', a:node.type), a:node.pos)
960+
endif
961+
if a:node.value[0] !=# "'"
962+
throw s:Err('must use single quote', a:node.pos)
963+
endif
964+
let vimpat = substitute(a:node.value[1:-2], "''", "'", 'g')
965+
if !has_key(s:pat_vim2py, vimpat)
966+
throw s:Err(printf('the pattern does not exist: %s', vimpat), a:node.pos)
967+
endif
968+
return s:pat_vim2py[vimpat]
969+
endfunction
970+
882971
let s:viml_builtin_functions = map(copy(s:VimLParser.builtin_functions), 'v:val.name')
883972

884973
let s:script_dir = expand('<sfile>:h')

py/vimlfunc.py

Lines changed: 3 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -29,70 +29,6 @@ class AttributeDict(dict):
2929
__delattr__ = dict.__delitem__
3030

3131

32-
pat_vim2py = {
33-
"[0-9a-zA-Z]": "[0-9a-zA-Z]",
34-
"[@*!=><&~#]": "[@*!=><&~#]",
35-
"\\<ARGOPT\\>": "\\bARGOPT\\b",
36-
"\\<BANG\\>": "\\bBANG\\b",
37-
"\\<EDITCMD\\>": "\\bEDITCMD\\b",
38-
"\\<NOTRLCOM\\>": "\\bNOTRLCOM\\b",
39-
"\\<TRLBAR\\>": "\\bTRLBAR\\b",
40-
"\\<USECTRLV\\>": "\\bUSECTRLV\\b",
41-
"\\<USERCMD\\>": "\\bUSERCMD\\b",
42-
"\\<\\(XFILE\\|FILES\\|FILE1\\)\\>": "\\b(XFILE|FILES|FILE1)\\b",
43-
"\\S": "\\S",
44-
"\\a": "[A-Za-z]",
45-
"\\d": "\\d",
46-
"\\h": "[A-Za-z_]",
47-
"\\s": "\\s",
48-
"\\v^d%[elete][lp]$": "^d(elete|elet|ele|el|e)[lp]$",
49-
"\\v^s%(c[^sr][^i][^p]|g|i[^mlg]|I|r[^e])":
50-
"^s(c[^sr][^i][^p]|g|i[^mlg]|I|r[^e])",
51-
"\\w": "[0-9A-Za-z_]",
52-
"\\w\\|[:#]": "[0-9A-Za-z_]|[:#]",
53-
"\\x": "[0-9A-Fa-f]",
54-
"^++": r"^\+\+",
55-
"^++bad=\\(keep\\|drop\\|.\\)\\>": "^\\+\\+bad=(keep|drop|.)\\b",
56-
"^++bad=drop": "^\\+\\+bad=drop",
57-
"^++bad=keep": "^\\+\\+bad=keep",
58-
"^++bin\\>": "^\\+\\+bin\\b",
59-
"^++edit\\>": "^\\+\\+edit\\b",
60-
"^++enc=\\S": "^\\+\\+enc=\\S",
61-
"^++encoding=\\S": "^\\+\\+encoding=\\S",
62-
"^++ff=\\(dos\\|unix\\|mac\\)\\>": "^\\+\\+ff=(dos|unix|mac)\\b",
63-
"^++fileformat=\\(dos\\|unix\\|mac\\)\\>":
64-
"^\\+\\+fileformat=(dos|unix|mac)\\b",
65-
"^++nobin\\>": "^\\+\\+nobin\\b",
66-
"^[A-Z]": "^[A-Z]",
67-
"^\\$\\w\\+": "^\\$[0-9A-Za-z_]+",
68-
"^\\(!\\|global\\|vglobal\\)$": "^(!|global|vglobal)$",
69-
"^\\(WHILE\\|FOR\\)$": "^(WHILE|FOR)$",
70-
"^\\(vimgrep\\|vimgrepadd\\|lvimgrep\\|lvimgrepadd\\)$":
71-
"^(vimgrep|vimgrepadd|lvimgrep|lvimgrepadd)$",
72-
"^\\d": "^\\d",
73-
"^\\h": "^[A-Za-z_]",
74-
"^\\s": "^\\s",
75-
"^\\s*\\\\": "^\\s*\\\\",
76-
"^[ \\t]$": "^[ \\t]$",
77-
"^[A-Za-z]$": "^[A-Za-z]$",
78-
"^[0-9A-Za-z]$": "^[0-9A-Za-z]$",
79-
"^[0-9]$": "^[0-9]$",
80-
"^[0-9A-Fa-f]$": "^[0-9A-Fa-f]$",
81-
"^[0-9A-Za-z_]$": "^[0-9A-Za-z_]$",
82-
"^[A-Za-z_]$": "^[A-Za-z_]$",
83-
"^[0-9A-Za-z_:#]$": "^[0-9A-Za-z_:#]$",
84-
"^[A-Za-z_][0-9A-Za-z_]*$": "^[A-Za-z_][0-9A-Za-z_]*$",
85-
"^[A-Z]$": "^[A-Z]$",
86-
"^[a-z]$": "^[a-z]$",
87-
"^[vgslabwt]:$\\|^\\([vgslabwt]:\\)\\?[A-Za-z_][0-9A-Za-z_#]*$":
88-
"^[vgslabwt]:$|^([vgslabwt]:)?[A-Za-z_][0-9A-Za-z_#]*$",
89-
"^[0-7]$": "^[0-7]$",
90-
"^[0-9A-Fa-f][0-9A-Fa-f]$": "^[0-9A-Fa-f][0-9A-Fa-f]$",
91-
r"^\.[0-9A-Fa-f]$": r"^\.[0-9A-Fa-f]$",
92-
"^[0-9A-Fa-f][^0-9A-Fa-f]$": "^[0-9A-Fa-f][^0-9A-Fa-f]$",
93-
}
94-
95-
9632
def viml_add(lst, item):
9733
lst.append(item)
9834

@@ -114,15 +50,15 @@ def viml_equalci(a, b):
11450

11551

11652
def viml_eqreg(s, reg):
117-
return re.search(pat_vim2py[reg], s, re.IGNORECASE)
53+
return re.search(reg, s, re.IGNORECASE)
11854

11955

12056
def viml_eqregh(s, reg):
121-
return re.search(pat_vim2py[reg], s)
57+
return re.search(reg, s)
12258

12359

12460
def viml_eqregq(s, reg):
125-
return re.search(pat_vim2py[reg], s, re.IGNORECASE)
61+
return re.search(reg, s, re.IGNORECASE)
12662

12763

12864
def viml_escape(s, chars):

0 commit comments

Comments
 (0)