1;"Magical Token". Wrapper to make returning a token easier, without all the positions and input ports
2(define (lyimport::mtoken symbol value)
3	(make-lexical-token symbol (make-source-location (current-input-port) (lexer-get-line) (lexer-get-column) (lexer-get-offset) -1) value)
4)
5
6(define (lyimport::pop_state)
7					;(format #t "Now popping the state ~a becomes ~a~%~%" lyimport::state (cdr lyimport::state))
8  (set! lyimport::state (cdr lyimport::state)))
9;Accumulator for a string in double quotes (used by quote state of lexer)
10(define lyimport::quoted_string "")
11(define (lyimport::start_quote)
12  (set! lyimport::quoted_string "")
13;;(format #t "Now pushing the state ~a becomes ~a~%~%" lyimport::state  (cons 'quote lyimport::state))
14  (set! lyimport::state (cons 'quote lyimport::state)))
15(define (lyimport::quote-append str)
16      (set! lyimport::quoted_string (string-append lyimport::quoted_string str)))
17
18
19(define lyimport::block_string "")
20(define lyimport::brace_count 0)
21(define (lyimport::start_block)
22  (set! lyimport::block_string "")
23  (set! lyimport::brace_count 1)
24;;(format #t "Now pushing the state ~a becomes ~a~%~%" lyimport::state  (cons 'block lyimport::state))
25  (set! lyimport::state (cons 'block lyimport::state)))
26(define (lyimport::block-append str)
27      (set! lyimport::block_string (string-append lyimport::block_string str)))
28
29
30
31(define (lyimport::start_incl)
32  (set! lyimport::state (cons 'incl lyimport::state)))
33
34
35; List of Notenames
36(define lyimport::list_of_notenames
37	(list "c" "cis" "ces" "cisis" "ceses" "d" "dis" "des" "disis" "deses" "e" "eis" "es" "ees" "eisis" "eses" "eeses" "f" "fis" "fes" "fisis" "feses" "g" "gis" "ges" "gisis" "geses" "a" "ais" "as" "aes" "aisis" "aeses" "ases" "b" "bis" "bes" "bisis" "beses" "h" "his" "hes" "hisis" "heses")
38)
39
40; List of Special Identifiers
41 (define lyimport::list_of_special_scm_identifiers
42	(list "major" "minor" "dorian" "phrygian" "lydian" "mixolydian")
43 )
44
45
46;Lilyponds "try_special_identifiers" is a part of scan_escaped_words in Denemo and needs this function:
47(define (lyimport::try_special_identifiers_scm? yytext)
48 	;Helper function to test if the string matches any string of a list of strings, the scm identifiers.
49		(let loop ((counter 0))
50			(cond
51			  ((> (+ counter 1) (length lyimport::list_of_special_scm_identifiers)) #f)
52			  ((and (<= counter (length lyimport::list_of_special_scm_identifiers)) (string-ci=? yytext (list-ref lyimport::list_of_special_scm_identifiers counter))) #t)
53			  (else (loop (+ counter 1)))
54			)
55		)
56)
57
58
59;Three functions to handle assignments. The creation function is in the main file and its used in the parser. The lexer only looks for already existing assignments, triggered with \user-created-keyword-through-assignment
60(define (lyimport::as-type key)
61		(car (hashq-ref lyimport::AssignmentTable (string->symbol key)))
62
63)
64
65(define (lyimport::as-value key)
66    	(cdr (hashq-ref lyimport::AssignmentTable (string->symbol key)))
67
68)
69
70
71(define (lyimport::as-eval key)
72	    (lyimport::mtoken 'MUSIC_IDENTIFIER (lyimport::as-value key))
73)
74
75
76;Any time the lexer gets a \keyword is looks up if its a fixed expression, a identifier or an assignment (or wrong)
77(define (lyimport::scan_escaped_word yytext)
78	(cond
79		; Converted from Denemo
80		((string-ci=? "alias" yytext) (lyimport::mtoken 'ALIAS yytext))
81		((string-ci=? "apply" yytext) (lyimport::mtoken 'APPLY yytext))
82		((string-ci=? "arpeggio" yytext) (lyimport::mtoken 'ARPEGGIO yytext))
83		((string-ci=? "autochange" yytext) (lyimport::mtoken 'AUTOCHANGE yytext))
84		((string-ci=? "spanrequest" yytext) (lyimport::mtoken 'SPANREQUEST yytext))
85		((string-ci=? "commandspanrequest" yytext) (lyimport::mtoken 'COMMANDSPANREQUEST yytext))
86		((string-ci=? "simultaneous" yytext) (lyimport::mtoken 'SIMULTANEOUS yytext))
87		((string-ci=? "sequential" yytext) (lyimport::mtoken 'SEQUENTIAL yytext))
88		((string-ci=? "accepts" yytext) (lyimport::mtoken 'ACCEPTS}, yytext))
89		((string-ci=? "alternative" yytext) (lyimport::mtoken 'ALTERNATIVE yytext))
90		((string-ci=? "bar" yytext) (lyimport::mtoken 'BAR yytext))
91		((string-ci=? "breathe" yytext) (lyimport::mtoken 'BREATHE yytext))
92	;;;	((string-ci=? "break" yytext) (lyimport::mtoken 'BREAK yytext))
93		((string-ci=? "break" yytext) (lyimport::multilexer));;denemo special ignore break
94
95		((string-ci=? "char" yytext) (lyimport::mtoken 'CHAR_T yytext))
96		((string-ci=? "chordmodifiers" yytext) (lyimport::mtoken 'CHORDMODIFIERS yytext))
97		((string-ci=? "chords" yytext) (lyimport::mtoken 'CHORDS yytext))
98		((string-ci=? "clef" yytext) (lyimport::mtoken 'CLEF yytext))
99		((string-ci=? "cm" yytext) (lyimport::mtoken 'CM_T yytext))
100		((string-ci=? "consists" yytext) (lyimport::mtoken 'CONSISTS yytext))
101		((string-ci=? "consistsend" yytext) (lyimport::mtoken 'CONSISTSEND yytext))
102		((string-ci=? "context" yytext) (lyimport::mtoken 'CONTEXT yytext))
103		((string-ci=? "default" yytext) (lyimport::mtoken 'DEFAULT yytext))
104		((string-ci=? "denies" yytext) (lyimport::mtoken 'DENIES yytext))
105		((string-ci=? "duration" yytext) (lyimport::mtoken 'DURATION yytext))
106		((string-ci=? "dynamicscript" yytext) (lyimport::mtoken 'DYNAMICSCRIPT yytext))
107		((string-ci=? "grobdescriptions" yytext) (lyimport::mtoken 'GROBDESCRIPTIONS yytext))
108		((string-ci=? "fermata" yytext) (lyimport::mtoken 'FERMATA yytext))
109		((string-ci=? "figures" yytext) (lyimport::mtoken 'FIGURES yytext))
110		((string-ci=? "grace" yytext) (lyimport::mtoken 'GRACE yytext))
111		((string-ci=? "glissando" yytext) (lyimport::mtoken 'GLISSANDO yytext))
112		((string-ci=? "header" yytext) (lyimport::mtoken 'HEADER yytext))
113		((string-ci=? "in" yytext) (lyimport::mtoken 'IN_T yytext))
114		((string-ci=? "key" yytext) (lyimport::mtoken 'KEY yytext))
115		((string-ci=? "mark" yytext) (lyimport::mtoken 'MARK yytext))
116		((string-ci=? "new" yytext) (lyimport::mtoken 'NEWCONTEXT yytext))
117		((string-ci=? "pitch" yytext) (lyimport::mtoken 'PITCH yytext))
118		((string-ci=? "time" yytext) (lyimport::mtoken 'TIME_T yytext))
119		((string-ci=? "times" yytext) (lyimport::mtoken 'TIMES yytext))
120		((string-ci=? "layout" yytext) (lyimport::mtoken 'LAYOUT yytext))
121		((string-ci=? "lyricmode" yytext) (lyimport::mtoken 'LYRICMODE yytext))
122		((string-ci=? "lyrics" yytext) (lyimport::mtoken 'LYRICS yytext))
123		((string-ci=? "lyricsto" yytext) (lyimport::mtoken 'LYRICSTO yytext))
124		((string-ci=? "midi" yytext) (lyimport::mtoken 'MIDI yytext))
125		((string-ci=? "mm" yytext) (lyimport::mtoken 'MM_T yytext))
126		((string-ci=? "name" yytext) (lyimport::mtoken 'NAME yytext))
127		((string-ci=? "pitchnames" yytext) (lyimport::mtoken 'PITCHNAMES yytext))
128		((string-ci=? "notes" yytext) (lyimport::mtoken 'NOTES yytext))
129		((string-ci=? "outputproperty" yytext) (lyimport::mtoken 'OUTPUTPROPERTY yytext))
130		((string-ci=? "override" yytext) (lyimport::mtoken 'OVERRIDE yytext))
131		((string-ci=? "set" yytext) (lyimport::mtoken 'SET yytext))
132		((string-ci=? "rest" yytext) (lyimport::mtoken 'REST yytext))
133		((string-ci=? "revert" yytext) (lyimport::mtoken 'REVERT yytext))
134		((string-ci=? "partial" yytext) (lyimport::mtoken 'PARTIAL yytext))
135		((string-ci=? "paper" yytext) (lyimport::mtoken 'PAPER yytext))
136		((string-ci=? "penalty" yytext) (lyimport::mtoken 'PENALTY yytext))
137		((string-ci=? "property" yytext) (lyimport::mtoken 'PROPERTY yytext))
138		((string-ci=? "pt" yytext) (lyimport::mtoken 'PT_T yytext))
139		((string-ci=? "relative" yytext) (lyimport::mtoken 'RELATIVE yytext))
140		((string-ci=? "remove" yytext) (lyimport::mtoken 'REMOVE yytext))
141		((string-ci=? "repeat" yytext) (lyimport::mtoken 'REPEAT yytext))
142		((string-ci=? "addlyrics" yytext) (lyimport::mtoken 'ADDLYRICS yytext))
143		((string-ci=? "partcombine" yytext) (lyimport::mtoken 'PARTCOMBINE yytext))
144		((string-ci=? "score" yytext) (lyimport::mtoken 'SCORE yytext))
145		((string-ci=? "script" yytext) (lyimport::mtoken 'SCRIPT yytext))
146		((string-ci=? "stylesheet" yytext) (lyimport::mtoken 'STYLESHEET yytext))
147		((string-ci=? "skip" yytext) (lyimport::mtoken 'SKIP yytext))
148		((string-ci=? "tempo" yytext) (lyimport::mtoken 'TEMPO yytext))
149		((string-ci=? "translator" yytext) (lyimport::mtoken 'TRANSLATOR yytext))
150		((string-ci=? "transpose" yytext) (lyimport::mtoken 'TRANSPOSE yytext))
151		((string-ci=? "type" yytext) (lyimport::mtoken 'TYPE yytext))
152		((string-ci=? "unset" yytext) (lyimport::mtoken 'UNSET yytext))
153		((string-ci=? "version" yytext) (lyimport::mtoken 'LILYVERSION yytext))
154
155		;From parser.yy %token TOKEN "\\keyword"
156		((string-ci=? "change" yytext) (lyimport::mtoken 'CHANGE yytext))
157		((string-ci=? "with" yytext) (lyimport::mtoken 'WITH yytext))
158		((string-ci=? "book" yytext) (lyimport::mtoken 'BOOK yytext))
159		((string-ci=? "bookpart" yytext) (lyimport::mtoken 'BOOKPART yytext))
160		((string-ci=? "chordmode" yytext) (lyimport::mtoken 'CHORDMODE yytext))
161		((string-ci=? "defaultchild" yytext) (lyimport::mtoken 'DEFAULTCHILD yytext))
162		((string-ci=? "description" yytext) (lyimport::mtoken 'DESCRIPTION yytext))
163		((string-ci=? "drummode" yytext) (lyimport::mtoken 'DRUMMODE yytext))
164		((string-ci=? "drums" yytext) (lyimport::mtoken 'DRUMS yytext))
165		((string-ci=? "figuremode" yytext) (lyimport::mtoken 'FIGUREMODE yytext))
166		((string-ci=? "invalid" yytext) (lyimport::mtoken 'INVALID yytext))
167		((string-ci=? "markup" yytext) (lyimport::mtoken 'MARKUP yytext))
168		((string-ci=? "markuplines" yytext) (lyimport::mtoken 'MARKUPLINES yytext))
169		((string-ci=? "notemode" yytext) (lyimport::mtoken 'NOTEMODE yytext))
170		((string-ci=? "octave" yytext) (lyimport::mtoken 'OCTAVE yytext))
171		((string-ci=? "applyContext" yytext) (lyimport::mtoken 'APPLY_CONTEXT yytext))
172		; Denemo specific
173		;; we have to swallow # and an embedded scheme integer following barNumberCheck
174		((string-ci=? "barNumberCheck" yytext)
175
176		    (begin (let loop ((c #f)) (set! c (lexer-getc)) (if (char-whitespace? c) (loop c)))   (read (make-soft-port (vector #f #f #f  (lambda () (lexer-getc)) #f) "r"))(lyimport::multilexer)))
177
178
179        ;If its not a known keyword its probably a user assignment:
180        ((hashq-ref lyimport::AssignmentTable (string->symbol yytext)) (lyimport::as-eval yytext))
181
182        ;Next test for an try_special_identifiers
183		((lyimport::try_special_identifiers_scm? yytext) (lyimport::mtoken 'SCM_IDENTIFIER yytext))
184
185        ;If its not a keyword, assignment or special identifier then its wrong
186	;	(else (begin (display (string-append "error: Unknown word: " yytext " (Line: "(number->string (lexer-get-line)) " Column: " (number->string ;(lexer-get-column)) ")\n"))(lyimport::multilexer))
187	;	)
188	      (else (lyimport::multilexer)
189		;(else (lyimport::mtoken 'DENEMODIRECTIVE (string-append "\\" yytext))
190		)
191
192	)
193)
194
195(define (lyimport::scan_bare_word yytext)
196	;Helper function to test if the string matches any string of a list of strings, the notenames.
197	(define (notename_pitch? yytext)
198		(let loop ((counter 0))
199			(cond
200			  ((> (+ counter 1) (length lyimport::list_of_notenames)) #f)
201			  ((and (<= counter (length lyimport::list_of_notenames)) (string-ci=? yytext (list-ref lyimport::list_of_notenames counter))) #t)
202			  (else (loop (+ counter 1)))
203			)
204		)
205	)
206
207	(cond
208	    ((notename_pitch? yytext) (lyimport::mtoken 'NOTENAME_PITCH yytext))
209		(else (lyimport::mtoken 'STRING yytext))
210
211	)
212)
213
214(define (lyimport::scan_fraction yytext)
215	(lyimport::mtoken 'FRACTION yytext)
216
217
218	;{
219	;ssize i = frac.find ('/');
220	;string left = frac.substr (0, i);
221	;string right = frac.substr (i + 1, (frac.length () - i + 1));
222
223	;int n = String_convert::dec2int (left);
224	;int d = String_convert::dec2int (right);
225	;return scm_cons (scm_from_int (n), scm_from_int (d));
226    ;}
227
228)
229