]> git.donarmstrong.com Git - lilypond.git/blob - lily/keyword.cc
patch::: 0.1.18.jcn2: patsje
[lilypond.git] / lily / keyword.cc
1 /*
2   keyword.cc -- keywords and identifiers
3  */
4
5 #include <stdlib.h>
6
7 #include "my-lily-lexer.hh"
8 #include "keyword.hh"
9
10 /* for qsort */
11 int
12       tabcmp (void const * p1, void const * p2)
13 {
14   return strcmp (((Keyword_ent const *) p1)->name,
15                 ((Keyword_ent const *) p2)->name);
16 }
17
18 Keyword_table::Keyword_table (Keyword_ent *tab)
19 {
20   table = tab;
21
22   /* count keywords */
23   for (maxkey = 0; table[maxkey].name; maxkey++);
24
25   /* sort them */
26   qsort (table, maxkey, sizeof (Keyword_ent), tabcmp);
27 }
28
29 /*
30   lookup with binsearch, return tokencode.
31 */
32 int
33 Keyword_table::lookup (char const *s) const
34 {
35   int     lo,
36           hi,
37           cmp,
38           result;
39   lo = 0;
40   hi = maxkey;
41
42   /* binary search */
43   do
44   {
45       cmp = (lo + hi) / 2;
46
47       result = strcmp (s, table[cmp].name);
48
49       if (result < 0)
50           hi = cmp;
51       else
52           lo = cmp;
53     }
54   while (hi - lo > 1);
55   if (!strcmp (s, table[lo].name))
56   {
57       return table[lo].tokcode;
58     }
59   else
60       return -1;              /* not found */
61 }
62