summaryrefslogtreecommitdiff
path: root/runtime/plugin/logiPat.vim
blob: 47fabe954da055bb0cc04d713b268e94569c93ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
" LogiPat: Boolean logical pattern matcher
"   Author:  Charles E. Campbell
"   Date:    Apr 04, 2016
"   Version: 4
"   Purpose: to do Boolean-logic based regular expression pattern matching
" Copyright:    Copyright (C) 1999-2011 Charles E. Campbell {{{1
"               Permission is hereby granted to use and distribute this code,
"               with or without modifications, provided that this copyright
"               notice is copied with it. Like most anything else that's free,
"               LogiPat.vim is provided *as is* and comes with no warranty
"               of any kind, either expressed or implied. By using this
"               plugin, you agree that in no event will the copyright
"               holder be liable for any damages resulting from the use
"               of this software.
"
"   Usage: {{{1
"       :LogiPat ...
"
"         Boolean logic supported:
"            () grouping operators
"            !  not the following pattern
"            |  logical or
"            &  logical and
"            "..pattern.."
"	Example: {{{1
"		:LogiPat !("january"|"february")
"		  would match all strings not containing the strings january
"		  or february
"	GetLatestVimScripts: 1290 1 :AutoInstall: LogiPat.vim
"
"  Behold, you will conceive in your womb, and bring forth a son, {{{1
"  and will call his name Jesus. He will be great, and will be
"  called the Son of the Most High. The Lord God will give him the
"  throne of his father, David, and he will reign over the house of
"  Jacob forever. There will be no end to his kingdom. (Luke 1:31-33 WEB)

" ---------------------------------------------------------------------
" Load Once: {{{1
if &cp || exists("loaded_logiPat")
 finish
endif
let g:loaded_logiPat = "v4"
let s:keepcpo        = &cpo
set cpo&vim
"DechoRemOn

" ---------------------------------------------------------------------
" Public Interface: {{{1
com!     -nargs=* LogiPat		call   LogiPat(<q-args>,1)
sil! com -nargs=* LP			call   LogiPat(<q-args>,1)
sil! com -nargs=* LPR			call   LogiPat(<q-args>,1,"r")
com!     -nargs=+ LPE			echomsg LogiPat(<q-args>)
com!     -nargs=+ LogiPatFlags	let  s:LogiPatFlags="<args>"
sil! com -nargs=+ LPF			let  s:LogiPatFlags="<args>"

" =====================================================================
" Functions: {{{1

" ---------------------------------------------------------------------
" LogiPat: this function interprets the boolean-logic pattern {{{2
fun! LogiPat(pat,...)
"  call Dfunc("LogiPat(pat<".a:pat.">)")

  " LogiPat(pat,dosearch)
  if a:0 > 0
   let dosearch= a:1
  else
   let dosearch= 0
  endif
  if a:0 >= 3
   let s:LogiPatFlags= a:3
  endif

  let s:npatstack = 0
  let s:nopstack  = 0
  let s:preclvl   = 0
  let expr        = a:pat

  " Lexer/Parser
  while expr != ""
"   call Decho("expr<".expr.">")

   if expr =~ '^"'
	" push a Pattern; accept "" as a single " in the pattern
    let expr = substitute(expr,'^\s*"','','')
    let pat  = substitute(expr,'^\(\%([^"]\|\"\"\)\{-}\)"\([^"].*$\|$\)','\1','')
	let pat  = substitute(pat,'""','"','g')
    let expr = substitute(expr,'^\(\%([^"]\|\"\"\)\{-}\)"\([^"].*$\|$\)','\2','')
    let expr = substitute(expr,'^\s*','','')
"    call Decho("pat<".pat."> expr<".expr.">")

    call s:LP_PatPush('.*'.pat.'.*')

   elseif expr =~ '^[!()|&]'
    " push an operator
    let op   = strpart(expr,0,1)
    let expr = strpart(expr,strlen(op))
	" allow for those who can't resist doubling their and/or operators
	if op =~ '[|&]' && expr[0] == op
     let expr = strpart(expr,strlen(op))
	endif
    call s:LP_OpPush(op)

   elseif expr =~ '^\s'
    " skip whitespace
    let expr= strpart(expr,1)

   else
    echoerr "operator<".strpart(expr,0,1)."> not supported (yet)"
    let expr= strpart(expr,1)
   endif

  endwhile

  " Final Execution
  call s:LP_OpPush('Z')

  let result= s:LP_PatPop(1)
"  call Decho("result=".result)

  " sanity checks and cleanup
  if s:npatstack > 0
   echoerr s:npatstack." patterns left on stack!"
   let s:npatstack= 0
  endif
  if s:nopstack > 0
   echoerr s:nopstack." operators left on stack!"
   let s:nopstack= 0
  endif

  " perform the indicated search
  if dosearch
   if exists("s:LogiPatFlags") && s:LogiPatFlags != ""
"  call Decho("search(result<".result."> LogiPatFlags<".s:LogiPatFlags.">)")
    call search(result,s:LogiPatFlags)
   else
"  call Decho("search(result<".result.">)")
    call search(result)
   endif
   let @/= result
  endif

"  call Dret("LogiPat ".result)
  return result
endfun

" ---------------------------------------------------------------------
" s:String: Vim6.4 doesn't have string() {{{2
func! s:String(str)
  return "'".escape(a:str, '"')."'"
endfunc

" ---------------------------------------------------------------------
" LP_PatPush: {{{2
fun! s:LP_PatPush(pat)
"  call Dfunc("LP_PatPush(pat<".a:pat.">)")
  let s:npatstack              = s:npatstack + 1
  let s:patstack_{s:npatstack} = a:pat
"  call s:StackLook("patpush") "Decho
"  call Dret("LP_PatPush : npatstack=".s:npatstack)
endfun

" ---------------------------------------------------------------------
" LP_PatPop: pop a number/variable from LogiPat's pattern stack {{{2
fun! s:LP_PatPop(lookup)
"  call Dfunc("LP_PatPop(lookup=".a:lookup.")")
  if s:npatstack > 0
   let ret         = s:patstack_{s:npatstack}
   let s:npatstack = s:npatstack - 1
  else
   let ret= "---error---"
   echoerr "(LogiPat) invalid expression"
  endif
"  call s:StackLook("patpop") "Decho
"  call Dret("LP_PatPop ".ret)
  return ret
endfun

" ---------------------------------------------------------------------
" LP_OpPush: {{{2
fun! s:LP_OpPush(op)
"  call Dfunc("LP_OpPush(op<".a:op.">)")

  " determine new operator's precedence level
  if a:op == '('
  	let s:preclvl= s:preclvl + 10
	let preclvl  = s:preclvl
  elseif a:op == ')'
  	let s:preclvl= s:preclvl - 10
   if s:preclvl < 0
    let s:preclvl= 0
    echoerr "too many )s"
   endif
   let preclvl= s:preclvl
  elseif a:op =~ '|'
   let preclvl= s:preclvl + 2
  elseif a:op =~ '&'
   let preclvl= s:preclvl + 4
  elseif a:op == '!'
   let preclvl= s:preclvl + 6
  elseif a:op == 'Z'
   let preclvl= -1
  else
   echoerr "expr<".expr."> not supported (yet)"
   let preclvl= s:preclvl
  endif
"  call Decho("new operator<".a:op."> preclvl=".preclvl)

  " execute higher-precdence operators
"  call Decho("execute higher-precedence operators")
  call s:LP_Execute(preclvl)

  " push new operator onto operator-stack
"  call Decho("push new operator<".a:op."> onto stack with preclvl=".preclvl." at nopstack=".(s:nopstack+1))
  if a:op =~ '!'
   let s:nopstack             = s:nopstack + 1
   let s:opprec_{s:nopstack}  = preclvl
   let s:opstack_{s:nopstack} = a:op
  elseif a:op =~ '|'
   let s:nopstack             = s:nopstack + 1
   let s:opprec_{s:nopstack}  = preclvl
   let s:opstack_{s:nopstack} = a:op
  elseif a:op == '&'
   let s:nopstack             = s:nopstack + 1
   let s:opprec_{s:nopstack}  = preclvl
   let s:opstack_{s:nopstack} = a:op
  endif

"  call s:StackLook("oppush") "Decho
"  call Dret("LP_OpPush : s:preclvl=".s:preclvl)
endfun

" ---------------------------------------------------------------------
" LP_Execute: execute operators from opstack using pattern stack {{{2
fun! s:LP_Execute(preclvl)
"  call Dfunc("LP_Execute(preclvl=".a:preclvl.") npatstack=".s:npatstack." nopstack=".s:nopstack)

  " execute all higher precedence operators
  while s:nopstack > 0 && a:preclvl < s:opprec_{s:nopstack}
   let op= s:opstack_{s:nopstack}
"   call Decho("op<".op."> nop=".s:nopstack." [preclvl=".a:preclvl."] < [opprec_".s:nopstack."=".s:opprec_{s:nopstack}."]")

   let s:nopstack = s:nopstack - 1
 
   if     op == '!'
    let n1= s:LP_PatPop(1)
	call s:LP_PatPush(s:LP_Not(n1))
 
   elseif op == '|'
    let n1= s:LP_PatPop(1)
    let n2= s:LP_PatPop(1)
    call s:LP_PatPush(s:LP_Or(n2,n1))
 
   elseif op =~ '&'
    let n1= s:LP_PatPop(1)
    let n2= s:LP_PatPop(1)
    call s:LP_PatPush(s:LP_And(n2,n1))
   endif
 
"   call s:StackLook("execute") "Decho
  endwhile

"  call Dret("LP_Execute")
endfun

" ---------------------------------------------------------------------
" LP_Not: writes a logical-not for a pattern {{{2
fun! s:LP_Not(pat)
"  call Dfunc("LP_Not(pat<".a:pat.">)")
  if a:pat =~ '^\.\*' && a:pat =~ '\.\*$'
   let pat= substitute(a:pat,'^\.\*\(.*\)\.\*$','\1','')
   let ret= '^\%(\%('.pat.'\)\@!.\)*$'
  else
   let ret= '^\%(\%('.a:pat.'\)\@!.\)*$'
  endif
"  call Dret("LP_Not ".ret)
  return ret
endfun

" ---------------------------------------------------------------------
" LP_Or: writes a logical-or branch using two patterns {{{2
fun! s:LP_Or(pat1,pat2)
"  call Dfunc("LP_Or(pat1<".a:pat1."> pat2<".a:pat2.">)")
  let ret= '\%('.a:pat1.'\|'.a:pat2.'\)'
"  call Dret("LP_Or ".ret)
  return ret
endfun

" ---------------------------------------------------------------------
" LP_And: writes a logical-and concat using two patterns {{{2
fun! s:LP_And(pat1,pat2)
"  call Dfunc("LP_And(pat1<".a:pat1."> pat2<".a:pat2.">)")
  let ret= '\%('.a:pat1.'\&'.a:pat2.'\)'
"  call Dret("LP_And ".ret)
  return ret
endfun

" ---------------------------------------------------------------------
" StackLook: {{{2
fun! s:StackLook(description)
"  call Dfunc("StackLook(description<".a:description.">)")
  let iop = 1
  let ifp = 1
"  call Decho("Pattern                       Operator")

  " print both pattern and operator
  while ifp <= s:npatstack && iop <= s:nopstack
   let fp = s:patstack_{ifp}
   let op = s:opstack_{iop}." (P".s:opprec_{s:nopstack}.')'
   let fplen= strlen(fp)
   if fplen < 30
   	let fp= fp.strpart("                              ",1,30-fplen)
   endif
"   call Decho(fp.op)
   let ifp = ifp + 1
   let iop = iop + 1
  endwhile

  " print just pattern
  while ifp <= s:npatstack
   let fp  = s:patstack_{ifp}
"   call Decho(fp)
   let ifp = ifp + 1
  endwhile

  " print just operator
  while iop <= s:nopstack
   let op  = s:opstack_{iop}." (P".s:opprec_{s:nopstack}.')'
"   call Decho("                              ".op)
   let iop = iop + 1
  endwhile
"  call Dret("StackLook")
endfun

" ---------------------------------------------------------------------
"  Cleanup And Modeline: {{{1
let &cpo= s:keepcpo
unlet s:keepcpo
" vim: ts=4 fdm=marker