summaryrefslogtreecommitdiff
path: root/lexer.l
blob: 008f813bf5410f65759143025a62cf91d733542f (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
%{ // -*-Fundamental-*-

#include <fstream.h>
#include <stdio.h>
#include "glob.hh"
#include "string.hh"

#include "lexer.hh"
#include "keyword.hh"
#include "vray.hh"
#include "parser.hh"
#include "debug.hh"

sstack<istream *> include_stack;
static int last_print;
const int DOTPRINT=50; // every 50 lines dots
%}

%option c++
%option noyywrap
%option nodefault
%option yylineno
%option debug
%x notes
%x incl
%x quote

OPTSIGN		!?
NOTENAMEI       A|B|C|D|E|F|G|As|Bes|Ces|Des|Es|Fes|Ges|Ais|Bis|Cis|Dis|Eis|Fis|Gis
NOTENAMEII	a|b|c|d|e|f|g|as|bes|ces|des|es|fes|ges|ais|bis|cis|dis|eis|fis|gis
NOTENAMEIII      Ases|Beses|Ceses|Deses|Eses|Feses|Geses|Aisis|Bisis|Cisis|Disis|Eisis|Fisis|Gisis
NOTENAMEIIII      ases|beses|ceses|deses|eses|feses|geses|aisis|bisis|cisis|disis|eisis|fisis|gisis
RESTNAME        r|s
NOTENAME        {NOTENAMEI}|{NOTENAMEII}|{NOTENAMEIII}|{NOTENAMEIIII}
PITCH		['`]*{OPTSIGN}{NOTENAME}
DURNAME		1|2|4|8|16|32
DURATION	{DURNAME}\.*
FULLNOTE	{PITCH}{DURATION}?
WORD		[a-zA-Z]+
REAL		[0-9]+(\.[0-9]*)?

%%

\$		{
	BEGIN(notes); return '$';
}

<notes>{RESTNAME} 	{
	const char *s = YYText();
	yylval.string = new String (s);	
	mtor << "rest:"<< yylval.string;
	return RESTNAME;
}
<notes>{PITCH}	{
	const char *s = YYText();
	yylval.string = new String (s);
	mtor << "pitch:"<< *yylval.string;
	return PITCH;
}
<notes>{DURATION}	{
	yylval.string = new String (YYText());
	return DURATION;
}
<notes>[:space:]+		{
}
<notes>[ \t\n]+		{
}
<notes>%.*		{

}
<notes>\$	{
	BEGIN(INITIAL); return '$';
}
<notes>.	{
	error("lexer error: illegal character found: " + String(YYText()));
}

\"		{
	BEGIN(quote);
}
<quote>[^\"]*	{
	yylval.string = new String (YYText());
}
<quote>\"	{
	BEGIN(INITIAL);
	return STRING;
}

<<EOF>> {
	if(!close_input())
		yyterminate();
}
{WORD}		{
	int l = lookup_keyword(YYText());
	if (l != -1)
		return l;
	Identifier * id = lookup_identifier(YYText());
	if (id) {		
		yylval.id = id;
		return IDENTIFIER;
	}
	String *sp = new String( YYText());
	mtor << "new id: " << *sp;
	yylval.string=sp;
	return NEWIDENTIFIER;
}

{REAL}		{
	Real r;
	int cnv=sscanf (YYText(), "%lf", &r);
	assert(cnv == 1);
	mtor  << "token (REAL)" << r;
	yylval.real = r;
	return REAL;
}

[\{\}\[\]\(\)]	{

	mtor << "parens\n";
	return YYText()[0];
}
[:=]		{
	char c = YYText()[0];
	mtor << "misc char" <<c<<"\n";
	return c;
}
[ \t\n]+	{
	
}

%.*		{
	//ignore
}
.		{
	error("lexer error: illegal character '"+String(YYText()[0])+
	  "' encountered");
	return YYText()[0];
}

%%

yyFlexLexer *lexer=0;

// set the  new input to s, remember old file.
void
new_input(String s)
{    
    istream *newin ;
    
    if (s=="")
	newin = &cin;
    else
	newin = new ifstream( s ); //
    
   if ( ! *newin)
      error("cant open "  + s);
   cout << "["<<s<<flush;
   
   include_stack.push(newin);

   if (!lexer) {
       lexer = new yyFlexLexer;
       lexer->set_debug( !monitor.silence("Lexer"));
   }		
   
   lexer->switch_streams(newin);
}


// pop the inputstack.
bool
close_input()
{

  istream *closing= include_stack.pop();
  if (closing != &cin)
      delete closing;
  
  cout << "]" << flush;
  
  if (include_stack.empty())
      return false ;  
  else 
      lexer->switch_streams(include_stack.top());  
  return true;  
}

int
yylex() {
	return lexer->yylex();
}

void
yyerror(char *s)
{
  *mlog << "error in line " << lexer->lineno() <<  ": " << s << '\n';
  exit(1);
}


#if 0

<notes>{NOTENAME} 	{
	yylval.string = new String (YYText());
	return NOTENAME;
}

#endif