lilypond-0.0.62
[lilypond.git] / lily / keyword.cc
blob965630964e1997aef969216c281cde2bf921b4e4
1 /*
2 keyword.cc -- keywords and identifiers
3 */
5 #include <stdlib.h>
7 #include "glob.hh"
8 #include "my-lily-lexer.hh"
9 //#include "mudobs.hh"
10 //#include "gram.hh"
12 /* for the keyword table */
13 struct Keyword_ent
15 char const *name;
16 int tokcode;
19 struct Keyword_table
21 Keyword_ent *table;
22 int maxkey;
23 Keyword_table(Keyword_ent *);
24 int lookup(char const *s) const;
28 /* for qsort */
29 int
30 tabcmp(void const * p1, void const * p2)
32 return strcmp(((Keyword_ent const *) p1)->name,
33 ((Keyword_ent const *) p2)->name);
36 Keyword_table::Keyword_table(Keyword_ent *tab)
38 table = tab;
40 /* count keywords */
41 for (maxkey = 0; table[maxkey].name; maxkey++);
43 /* sort them */
44 qsort(table, maxkey, sizeof(Keyword_ent), tabcmp);
48 lookup with binsearch, return tokencode.
50 int
51 Keyword_table::lookup(char const *s)const
53 int lo,
54 hi,
55 cmp,
56 result;
57 lo = 0;
58 hi = maxkey;
60 /* binary search */
63 cmp = (lo + hi) / 2;
65 result = strcmp(s, table[cmp].name);
67 if (result < 0)
68 hi = cmp;
69 else
70 lo = cmp;
72 while (hi - lo > 1);
73 if (!strcmp(s, table[lo].name))
75 return table[lo].tokcode;
76 } else
77 return -1; /* not found */