]> git.donarmstrong.com Git - lilypond.git/blob - src/mylexer.cc
release: 0.0.26
[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     "mark", MARK,
26     "meter", METER,
27     "mm", MM,
28     "octave", OCTAVECOMMAND,
29     "output", OUTPUT,
30     "partial", PARTIAL,
31     "paper", PAPER,
32     "plet", PLET,
33     "pt", PT,
34     "score", SCORE,
35     "script", SCRIPT,
36     "skip", SKIP,
37     "staff", STAFF,
38     "start", START_T,
39     "table", TABLE,
40     "symboltables", SYMBOLTABLES,
41     "notenames", NOTENAMES,
42     "texid", TEXID,
43     "textstyle", TEXTSTYLE,
44     "chord", CHORD,
45     "multi", MULTI,
46     "unitspace", UNITSPACE,
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 String
96 My_flex_lexer::spot()const
97 {
98     return include_stack.top()->name +  ": " + lineno();
99 }
100
101 void
102 My_flex_lexer::LexerError(const char *s)
103 {
104     if (lexer->include_stack.empty()) {
105         *mlog << "error at EOF" << s << '\n';
106     }else 
107         *mlog << spot() << ": error:" << s << '\n';
108      exit(1);
109 }
110 // set the  new input to s, remember old file.
111 void
112 My_flex_lexer::new_input(String s)
113 {    
114    if (!include_stack.empty())
115         include_stack.top()->line = lineno();
116
117    Input_file *newin = new Input_file(s);
118    include_stack.push(newin);
119    switch_streams(newin->is);
120    yylineno = 1;
121 }
122
123 // pop the inputstack.
124 bool
125 My_flex_lexer::close_input()
126 {
127     Input_file *old = include_stack.pop();
128      bool ok =  true;
129     if (include_stack.empty()) {
130         ok = false;
131     } else {
132         Input_file *i = include_stack.top();
133         switch_streams(i->is);
134         yylineno = i->line;     
135     }
136     delete old;
137     return ok;
138 }