Commits

ZyX_I committed 3a84cfd

@/fwc: Added haskey check

  • Participants
  • Parent commits eb10722

Comments (0)

Files changed (4)

File plugin/frawor/fwc/compiler.vim

             \             'callable function reference, but got %s',
             \     'nreg': 'string `%s'' is not a valid regular expression (%s)',
             \   'nmatch': "string `%s' does not match regular expression `%s'",
+            \  'keysmis': 'the following required keys are missing: %s',
+            \   'keymis': 'key `%s'' is missing',
         \}, '"Error while processing check %u for %s: ".v:val'))
 let s:_messages._types=['number', 'string', 'function reference', 'list',
             \           'dictionary']
                 " TODO
             elseif desc[0] is 'key'
                 " TODO
-            elseif desc[0] is 'hkey'
-                " TODO
+            elseif desc[0] is 'haskey'
+                let addedcond=1
+                call self.addtypecond([type({})], idx)
+                if len(desc[1])>1
+                    let keys='['.join(map(copy(desc[1]),
+                                \         'type(v:val)==type("")?'.
+                                \               's:F.string(v:val):'.
+                                \               'self.getvar(v:val)'),
+                                \', ').']'
+                    let absentkeys='filter('.keys.', '.
+                                \          string('!has_key('.curargstr.', '.
+                                \                           'v:val)').')'
+                    call self.nextcond('!empty('.absentkeys.')')
+                                \.addthrow('keysmis', idx, '@#@',
+                                \          'join('.absentkeys.', ", ")')
+                                \.up()
+                elseif !empty(desc[1])
+                    let keystr=((type(desc[1][0])==type(""))?
+                                \       (s:F.string(desc[1][0])):
+                                \       (self.getvar(desc[1][0])))
+                    call self.nextcond('!has_key('.curargstr.', '.keystr.')')
+                                \.addthrow('keymis', idx, '@#@', keystr)
+                                \.up()
+                endif
             elseif desc[0] is 'isfunc'
                 let addedcond=1
                 let frefpref='string('.curargstr.')[10:11]'

File plugin/frawor/fwc/parser.vim

 "▶3 check.in
 " Checks whether {argument} is inside list {var}. Matcher is ignored
 let s:args.check.in=['var', '?omtchr']
-"▶3 check.hkey
+"▶3 check.haskey
 " Checks whether {argument} is a dictionary with given keys
-let s:args.check.hkey=['*string']
+let s:args.check.haskey=['*string']
 "▶3 check.isfunc
 " Checks whether {argument} is a callable function reference. Additional 
 " argument determines whether strings should not be accepted

File test/fwccheck.ok

 plugin/frawor/fwc/compiler:nbool
 plugin/frawor/fwc/compiler:typefail
 plugin/frawor/fwc/compiler:nreg
-::: Section <Checks/isfunc>
+::: Section <Checks/Built-in checks/haskey>
+plugin/frawor/fwc/compiler:typefail
+plugin/frawor/fwc/compiler:keymis
+plugin/frawor/fwc/compiler:keysmis
+plugin/frawor/fwc/compiler:keysmis
+::: Section <Checks/Built-in checks/isfunc>
 plugin/frawor/fwc/compiler:nsfunc
 plugin/frawor/fwc/compiler:nsfunc
 plugin/frawor/fwc/compiler:typefail
 plugin/frawor/fwc/compiler:nfunc
-::: Section <Checks/isfunc/Match>
+::: Section <Checks/Built-in checks/Match>
 plugin/frawor/fwc/compiler:nmatch
 plugin/frawor/fwc/compiler:typefail
 plugin/frawor/fwc/compiler:nmatch
-::: Section <Checks/isfunc/Types>
+::: Section <Checks/Built-in checks/Types>
 plugin/frawor/fwc/compiler:typefail
 plugin/frawor/fwc/compiler:typefail
 plugin/frawor/fwc/compiler:typefail

File test/fwctests.dat

 ['isreg',           'check'], ['\(abc'],            0
 ['_',               'check'], [[[[]]]],             1
 ['any',             'check'], [[[[]]]],             1
-#▶2 isfunc
+#▶3 haskey
+['haskey abc',      'check'], [""],                 0
+['haskey abc',      'check'], [{}],                 0
+['haskey abc, def', 'check'], [{}],                 0
+['haskey abc, def', 'check'], [{'abc': 1}],         0
+['haskey abc, def', 'check'], [{'abc':1, 'def':1}], 1
+['haskey abc',      'check'], [{'abc': 1}],         1
+#▶3 isfunc
 ['isfunc',          'check'], [function("tr")],     1
 ['isfunc',          'check'], [function("s:Eval")], 0
 ['isfunc',          'check'], ["tr"],               1
 ['isfunc',          'check'], ["s:Eval"],           0
+['isfunc 1',        'check'], [function("tr")],     1
 ['isfunc 1',        'check'], ["tr"],               0
 ['isfunc 1',        'check'], [function("s:Eval")], 0
 #▶3 Match