]> git.donarmstrong.com Git - lilypond.git/blob - src/mylexer.cc
release: 0.0.22
[lilypond.git] / src / mylexer.cc
1 #include "interval.hh"
2 #include "identparent.hh"
3 #include "associter.hh"
4 #include "lexer.hh"
5 #include "parser.hh"
6 #include "keyword.hh"
7 #include "assoc.hh"
8 #include "lexer.hh"
9 #include "sstack.hh"
10 #include "debug.hh"
11 #include "notename.hh"
12
13 static Keyword_ent the_key_tab[]={
14     "bar", BAR,
15     "bass", BASS,
16     "clef", CLEF,
17     "cm", CM,
18     "commands", COMMANDS,
19     "duration", DURATIONCOMMAND,
20     "geometric", GEOMETRIC,
21     "in", IN,
22     "key", KEY, 
23     "melodic", MELODIC,
24     "meter", METER,
25     "mm", MM,
26     "octave", OCTAVECOMMAND,
27     "output", OUTPUT,
28     "partial", PARTIAL,
29     "paper", PAPER,
30     "plet", PLET,
31     "pt", PT,
32     "rhythmic", RHYTHMIC,
33     "score", SCORE,
34     "script", SCRIPT,
35     "skip", SKIP,
36     "staff", STAFF,
37     "start", START_T,
38     "table", TABLE,
39     "symboltables", SYMBOLTABLES,
40     "notenames", NOTENAMES,
41     "texid", TEXID,
42     "textstyle", TEXTSTYLE,
43     "chord", CHORD,
44     "multi", MULTI,
45     "unitspace", UNITSPACE,
46     "violin", VIOLIN,
47     "voice", VOICE,
48     "voices", VOICES,
49     "width", WIDTH,
50     "music", MUSIC,
51     "grouping", GROUPING,
52     0,0
53 };
54
55 My_flex_lexer::My_flex_lexer()
56 {
57     keytable = new Keyword_table(the_key_tab);
58     the_id_tab = new Assoc<String, Identifier*>;
59     defaulttab = 0;
60 }
61
62 int
63 My_flex_lexer::lookup_keyword(String s)
64 {
65     return keytable->lookup(s);
66 }
67
68 Identifier*
69 My_flex_lexer::lookup_identifier(String s)
70 {
71     if (!the_id_tab->elt_query(s))
72         return 0;
73     
74     return (*the_id_tab)[s];
75 }
76
77 void
78 My_flex_lexer::add_identifier(Identifier*i)
79 {
80     delete lookup_identifier(i->name);
81     (*the_id_tab)[i->name] = i;
82 }
83
84 My_flex_lexer::~My_flex_lexer()
85 {
86     delete keytable;
87     delete defaulttab;
88     for (Assoc_iter<String,Identifier*> ai(*the_id_tab); ai.ok(); ai++) {
89         mtor << "deleting: " << ai.key()<<'\n';
90         delete ai.val();
91     }
92     delete the_id_tab;
93 }
94
95 void
96 My_flex_lexer::LexerError(const char *s)
97 {
98     if (lexer->include_stack.empty()) {
99         *mlog << "error at EOF" << s;
100     }else 
101         *mlog << lexer->include_stack.top()->name <<  ": " <<
102          lexer->lineno() <<  ": error:" << s << '\n';
103      exit(1);
104 }
105 // set the  new input to s, remember old file.
106 void
107 My_flex_lexer::new_input(String s)
108 {    
109    if (!include_stack.empty())
110         include_stack.top()->line = lineno();
111
112    Input_file *newin = new Input_file(s);
113    include_stack.push(newin);
114    switch_streams(newin->is);
115    yylineno = 1;
116 }
117
118 // pop the inputstack.
119 bool
120 My_flex_lexer::close_input()
121 {
122     Input_file *old = include_stack.pop();
123      bool ok =  true;
124     if (include_stack.empty()) {
125         ok = false;
126     } else {
127         Input_file *i = include_stack.top();
128         switch_streams(i->is);
129         yylineno = i->line;     
130     }
131     delete old;
132     return ok;
133 }