lilypond-1.1.49
[lilypond.git] / src / keyword.cc
blob14903cfb61ae5a59d1274e2d08ef8ebe721b35a5
1 /*
2 keyword.cc -- keywords and identifiers
3 */
5 #include <stdlib.h>
7 #include "glob.hh"
8 #include "lexer.hh"
9 //#include "mudobs.hh"
10 //#include "gram.hh"
12 /* for the keyword table */
13 struct Keyword_ent
15 const char *name;
16 int tokcode;
19 struct Keyword_table
21 Keyword_ent *table;
22 int maxkey;
23 Keyword_table(Keyword_ent *);
24 int lookup(const char *s) const;
28 /* for qsort */
29 int
30 tabcmp(const void * p1, const void * p2)
32 return strcmp(((const Keyword_ent *) p1)->name,
33 ((const Keyword_ent *) 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(const char *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 */