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