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