@@ -69,6 +69,10 @@ let s:opprec[s:NODE_ENV] = 9
6969let 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).
7377let s: reserved_keywords = {
7478 \ ' False' : 1 ,
@@ -673,27 +677,33 @@ function s:PythonCompiler.compile_sequalcs(node)
673677endfunction
674678
675679function 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 )
677682endfunction
678683
679684function 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 )
681687endfunction
682688
683689function 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 )
685692endfunction
686693
687694function 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 )
689697endfunction
690698
691699function 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 )
693702endfunction
694703
695704function 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 )
697707endfunction
698708
699709function s: PythonCompiler .compile_is (node)
@@ -879,6 +889,85 @@ function s:PythonCompiler.compile_op2(node, op)
879889endfunction
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+
882971let s: viml_builtin_functions = map (copy (s: VimLParser .builtin_functions), ' v:val.name' )
883972
884973let s: script_dir = expand (' <sfile>:h' )
0 commit comments