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