-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmatlab_lex.ll
3842 lines (3048 loc) · 90.6 KB
/
matlab_lex.ll
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (C) 1993-2011 John W. Eaton
This file is part of Octave.
Octave is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 3 of the License, or (at your
option) any later version.
Octave is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with Octave; see the file COPYING. If not, see
<http://www.gnu.org/licenses/>.
*/
/*%option prefix = "matlab" */
%top {
}
%s COMMAND_START
%s MATRIX_START
%x SCRIPT_FILE_BEGIN
%x FUNCTION_FILE_BEGIN
%x TYPE_BEGIN
%{
#include <cctype>
#include <cstring>
#include <cassert>
#include <iostream>
#include <set>
#include <sstream>
#include <string>
#include <stack>
#include <sys/types.h>
#include <unistd.h>
#include "sage3basic.h"
#include "SymbolFinder.h"
class StatementList;
class MatlabFunctionBuilder;
#include "comment-list.h"
#include "error.h"
#include "input.h"
#include "lex.h"
#include "ourtoken.h"
#include "matlab_parse.h"
#include "octave.gperf.h"
// These would be alphabetical, but y.tab.h must be included before
// oct-gperf.h and y.tab.h must be included after token.h and the tree
// class declarations. We can't include y.tab.h in oct-gperf.h
// because it may not be protected to allow it to be included multiple
// times.
#if defined (GNULIB_NAMESPACE)
// Calls to the following functions appear in the generated output from
// flex without the namespace tag. Redefine them so we will use them
// via the gnulib namespace.
#define fprintf GNULIB_NAMESPACE::fprintf
#define fwrite GNULIB_NAMESPACE::fwrite
#define malloc GNULIB_NAMESPACE::malloc
#define realloc GNULIB_NAMESPACE::realloc
#endif
// The current input line number.
int input_line_number = 1;
// The column of the current token.
int current_input_column = 1;
// Buffer for help text snagged from function files.
std::stack<std::string> help_buf;
#if ! (defined (FLEX_SCANNER) \
&& defined (YY_FLEX_MAJOR_VERSION) && YY_FLEX_MAJOR_VERSION >= 2 \
&& defined (YY_FLEX_MINOR_VERSION) && YY_FLEX_MINOR_VERSION >= 5)
#error lex.l requires flex version 2.5.4 or later
#endif
/*#define yylval octave_lval*/
// Arrange to get input via readline.
/*
#ifdef YY_INPUT
#undef YY_INPUT
#endif
#define YY_INPUT(buf, result, max_size) \
if ((result = octave_read (buf, max_size)) < 0) \
YY_FATAL_ERROR ("octave_read () in flex scanner failed");*/
// Try to avoid crashing out completely on fatal scanner errors.
// The call to yy_fatal_error should never happen, but it avoids a
// `static function defined but not used' warning from gcc.
/*OCTAVE_QUIT;*/
#ifdef YY_FATAL_ERROR
#undef YY_FATAL_ERROR
#endif
#define YY_FATAL_ERROR(msg) \
do \
{ \
error (msg); \
yy_fatal_error (msg); \
} \
while (0)
#define DISPLAY_TOK_AND_RETURN(tok) \
do \
{ \
int tok_val = tok; \
if (Vdisplay_tokens) \
display_token (tok_val); \
if (lexer_debug_flag) \
{ \
std::cerr << "R: "; \
display_token (tok_val); \
std::cerr << std::endl; \
} \
return tok_val; \
} \
while (0)
#define COUNT_TOK_AND_RETURN(tok) \
do \
{ \
Vtoken_count++; \
DISPLAY_TOK_AND_RETURN (tok); \
} \
while (0)
#define TOK_RETURN(tok) \
do \
{ \
current_input_column += yyleng; \
lexer_flags.quote_is_transpose = false; \
lexer_flags.convert_spaces_to_comma = true; \
COUNT_TOK_AND_RETURN (tok); \
} \
while (0)
#define TOK_PUSH_AND_RETURN(name, tok) \
do \
{ \
yylval.tok_val = new token (name, input_line_number, \
current_input_column); \
token_stack.push (yylval.tok_val); \
TOK_RETURN (tok); \
} \
while (0)
#define BIN_OP_RETURN(tok, convert, bos) \
do \
{ \
yylval.tok_val = new token (input_line_number, current_input_column); \
token_stack.push (yylval.tok_val); \
current_input_column += yyleng; \
lexer_flags.quote_is_transpose = false; \
lexer_flags.convert_spaces_to_comma = convert; \
lexer_flags.looking_for_object_index = false; \
lexer_flags.at_beginning_of_statement = bos; \
COUNT_TOK_AND_RETURN (tok); \
} \
while (0)
#define XBIN_OP_RETURN(tok, convert, bos) \
do \
{ \
gripe_matlab_incompatible_operator (yytext); \
BIN_OP_RETURN (tok, convert, bos); \
} \
while (0)
#define LEXER_DEBUG(pattern) \
do \
{ \
if (lexer_debug_flag) \
lexer_debug (pattern, yytext); \
} \
while (0)
// TRUE means that we have encountered EOF on the input stream.
bool parser_end_of_input = false;
// Flags that need to be shared between the lexer and parser.
lexical_feedback lexer_flags;
// Stack to hold tokens so that we can delete them when the parser is
// reset and avoid growing forever just because we are stashing some
// information. This has to appear before lex.h is included, because
// one of the macros defined there uses token_stack.
//
// FIXME -- this should really be static, but that causes
// problems on some systems.
std::stack <token*> token_stack;
// Did eat_whitespace() eat a space or tab, or a newline, or both?
typedef int yum_yum;
const yum_yum ATE_NOTHING = 0;
const yum_yum ATE_SPACE_OR_TAB = 1;
const yum_yum ATE_NEWLINE = 2;
// Is the closest nesting level a square bracket, squiggly brace or a paren?
class bracket_brace_paren_nesting_level
{
public:
bracket_brace_paren_nesting_level (void) : context () { }
~bracket_brace_paren_nesting_level (void) { }
void bracket (void) { context.push (BRACKET); }
bool is_bracket (void)
{ return ! context.empty () && context.top () == BRACKET; }
void brace (void) { context.push (BRACE); }
bool is_brace (void)
{ return ! context.empty () && context.top () == BRACE; }
void paren (void) { context.push (PAREN); }
bool is_paren (void)
{ return ! context.empty () && context.top () == PAREN; }
bool is_bracket_or_brace (void)
{ return (! context.empty ()
&& (context.top () == BRACKET || context.top () == BRACE)); }
bool none (void) { return context.empty (); }
void remove (void) { if (! context.empty ()) context.pop (); }
void clear (void) { while (! context.empty ()) context.pop (); }
private:
std::stack<int> context;
static const int BRACKET;
static const int BRACE;
static const int PAREN;
bracket_brace_paren_nesting_level (const bracket_brace_paren_nesting_level&);
bracket_brace_paren_nesting_level&
operator = (const bracket_brace_paren_nesting_level&);
};
const int bracket_brace_paren_nesting_level::BRACKET = 1;
const int bracket_brace_paren_nesting_level::BRACE = 2;
const int bracket_brace_paren_nesting_level::PAREN = 3;
static bracket_brace_paren_nesting_level nesting_level;
static bool Vdisplay_tokens = false;
static unsigned int Vtoken_count = 0;
// The start state that was in effect when the beginning of a block
// comment was noticed.
static int block_comment_nesting_level = 0;
// Internal variable for lexer debugging state.
static bool lexer_debug_flag = false;
// Forward declarations for functions defined at the bottom of this
// file.
static int text_yyinput (void);
static void xunput (char c, char *buf);
static void fixup_column_count (char *s);
static void do_comma_insert_check (void);
static int is_keyword_token (const std::string& s);
static int process_comment (bool start_in_block, bool& eof);
static bool match_any (char c, const char *s);
static bool next_token_is_sep_op (void);
static bool next_token_is_bin_op (bool spc_prev);
static bool next_token_is_postfix_unary_op (bool spc_prev);
static std::string strip_trailing_whitespace (char *s);
static void handle_number (void);
static int handle_string (char delim);
static int handle_close_bracket (bool spc_gobbled, int bracket_type);
static int handle_superclass_identifier (void);
static int handle_meta_identifier (void);
static int handle_identifier (void);
static bool have_continuation (bool trailing_comments_ok = true);
static bool have_ellipsis_continuation (bool trailing_comments_ok = true);
static void scan_for_comments (const char *);
static yum_yum eat_whitespace (void);
static yum_yum eat_continuation (void);
static void maybe_warn_separator_insert (char sep);
static void gripe_single_quote_string (void);
static void gripe_matlab_incompatible (const std::string& msg);
static void maybe_gripe_matlab_incompatible_comment (char c);
static void gripe_matlab_incompatible_continuation (void);
static void gripe_matlab_incompatible_operator (const std::string& op);
static void display_token (int tok);
static void lexer_debug (const char *pattern, const char *text);
%}
D [0-9]
S [ \t]
NL ((\n)|(\r)|(\r\n))
SNL ({S}|{NL})
EL (\.\.\.)
BS (\\)
CONT ({EL}|{BS})
Im [iIjJ]
CCHAR [#%]
COMMENT ({CCHAR}.*{NL})
SNLCMT ({SNL}|{COMMENT})
NOT ((\~)|(\!))
POW ((\*\*)|(\^))
EPOW (\.{POW})
IDENT ([_$a-zA-Z][_$a-zA-Z0-9]*)
EXPON ([DdEe][+-]?{D}+)
NUMBER (({D}+\.?{D}*{EXPON}?)|(\.{D}+{EXPON}?)|(0[xX][0-9a-fA-F]+))
%%
%{
// Make script and function files start with a bogus token. This makes
// the parser go down a special path.
%}
<SCRIPT_FILE_BEGIN>. {
LEXER_DEBUG ("<SCRIPT_FILE_BEGIN>.");
BEGIN (INITIAL);
xunput (yytext[0], yytext);
COUNT_TOK_AND_RETURN (SCRIPT_FILE);
}
<FUNCTION_FILE_BEGIN>. {
LEXER_DEBUG ("<FUNCTION_FILE_BEGIN>.");
BEGIN (INITIAL);
xunput (yytext[0], yytext);
COUNT_TOK_AND_RETURN (FUNCTION_FILE);
}
%{
// Help and other command-style functions.
%}
<COMMAND_START>{NL} {
LEXER_DEBUG ("<COMMAND_START>{NL}");
BEGIN (INITIAL);
input_line_number++;
current_input_column = 1;
lexer_flags.quote_is_transpose = false;
lexer_flags.convert_spaces_to_comma = true;
lexer_flags.looking_for_object_index = false;
lexer_flags.at_beginning_of_statement = true;
COUNT_TOK_AND_RETURN ('\n');
}
<COMMAND_START>[\;\,] {
LEXER_DEBUG ("<COMMAND_START>[\\;\\,]");
lexer_flags.looking_for_object_index = false;
lexer_flags.at_beginning_of_statement = true;
BEGIN (INITIAL);
if (strcmp (yytext, ",") == 0)
TOK_RETURN (',');
else
TOK_RETURN (';');
}
<COMMAND_START>[\"\'] {
LEXER_DEBUG ("<COMMAND_START>[\\\"\\']");
lexer_flags.at_beginning_of_statement = false;
current_input_column++;
int tok = handle_string (yytext[0]);
COUNT_TOK_AND_RETURN (tok);
}
<COMMAND_START>[^#% \t\r\n\;\,\"\'][^ \t\r\n\;\,]*{S}* {
LEXER_DEBUG ("<COMMAND_START>[^#% \\t\\r\\n\\;\\,\\\"\\'][^ \\t\\r\\n\\;\\,]*{S}*");
std::string tok = strip_trailing_whitespace (yytext);
lexer_flags.looking_for_object_index = false;
lexer_flags.at_beginning_of_statement = false;
TOK_PUSH_AND_RETURN (tok, SQ_STRING);
}
%{
// For this and the next two rules, we're looking at ']', and we
// need to know if the next token is `=' or `=='.
//
// It would have been so much easier if the delimiters were simply
// different for the expression on the left hand side of the equals
// operator.
//
// It's also a pain in the ass to decide whether to insert a comma
// after seeing a ']' character...
// FIXME -- we need to handle block comments here.
%}
<MATRIX_START>{SNLCMT}*\]{S}* {
LEXER_DEBUG ("<MATRIX_START>{SNLCMT}*\\]{S}*");
scan_for_comments (yytext);
fixup_column_count (yytext);
lexer_flags.looking_at_object_index.pop_front ();
lexer_flags.looking_for_object_index = true;
lexer_flags.at_beginning_of_statement = false;
int c = yytext[yyleng-1];
int cont_is_spc = eat_continuation ();
bool spc_gobbled = (cont_is_spc || c == ' ' || c == '\t');
int tok_to_return = handle_close_bracket (spc_gobbled, ']');
if (spc_gobbled)
xunput (' ', yytext);
COUNT_TOK_AND_RETURN (tok_to_return);
}
%{
// FIXME -- we need to handle block comments here.
%}
<MATRIX_START>{SNLCMT}*\}{S}* {
LEXER_DEBUG ("<MATRIX_START>{SNLCMT}*\\}{S}*");
scan_for_comments (yytext);
fixup_column_count (yytext);
lexer_flags.looking_at_object_index.pop_front ();
lexer_flags.looking_for_object_index = true;
lexer_flags.at_beginning_of_statement = false;
int c = yytext[yyleng-1];
int cont_is_spc = eat_continuation ();
bool spc_gobbled = (cont_is_spc || c == ' ' || c == '\t');
int tok_to_return = handle_close_bracket (spc_gobbled, '}');
if (spc_gobbled)
xunput (' ', yytext);
COUNT_TOK_AND_RETURN (tok_to_return);
}
%{
// Commas are element separators in matrix constants. If we don't
// check for continuations here we can end up inserting too many
// commas.
%}
<MATRIX_START>{S}*\,{S}* {
LEXER_DEBUG ("<MATRIX_START>{S}*\\,{S}*");
current_input_column += yyleng;
int tmp = eat_continuation ();
lexer_flags.quote_is_transpose = false;
lexer_flags.convert_spaces_to_comma = true;
lexer_flags.looking_for_object_index = false;
lexer_flags.at_beginning_of_statement = false;
if (! lexer_flags.looking_at_object_index.front ())
{
if ((tmp & ATE_NEWLINE) == ATE_NEWLINE)
{
maybe_warn_separator_insert (';');
xunput (';', yytext);
}
}
COUNT_TOK_AND_RETURN (',');
}
%{
// In some cases, spaces in matrix constants can turn into commas.
// If commas are required, spaces are not important in matrix
// constants so we just eat them. If we don't check for continuations
// here we can end up inserting too many commas.
%}
<MATRIX_START>{S}+ {
LEXER_DEBUG ("<MATRIX_START>{S}+");
current_input_column += yyleng;
lexer_flags.at_beginning_of_statement = false;
int tmp = eat_continuation ();
if (! lexer_flags.looking_at_object_index.front ())
{
bool bin_op = next_token_is_bin_op (true);
bool postfix_un_op = next_token_is_postfix_unary_op (true);
bool sep_op = next_token_is_sep_op ();
if (! (postfix_un_op || bin_op || sep_op)
&& nesting_level.is_bracket_or_brace ()
&& lexer_flags.convert_spaces_to_comma)
{
if ((tmp & ATE_NEWLINE) == ATE_NEWLINE)
{
maybe_warn_separator_insert (';');
xunput (';', yytext);
}
lexer_flags.quote_is_transpose = false;
lexer_flags.convert_spaces_to_comma = true;
maybe_warn_separator_insert (',');
COUNT_TOK_AND_RETURN (',');
}
}
}
%{
// Semicolons are handled as row seprators in matrix constants. If we
// don't eat whitespace here we can end up inserting too many
// semicolons.
// FIXME -- we need to handle block comments here.
%}
<MATRIX_START>{SNLCMT}*;{SNLCMT}* {
LEXER_DEBUG ("<MATRIX_START>{SNLCMT}*;{SNLCMT}*");
scan_for_comments (yytext);
fixup_column_count (yytext);
eat_whitespace ();
lexer_flags.quote_is_transpose = false;
lexer_flags.convert_spaces_to_comma = true;
lexer_flags.looking_for_object_index = false;
lexer_flags.at_beginning_of_statement = false;
COUNT_TOK_AND_RETURN (';');
}
%{
// In some cases, new lines can also become row separators. If we
// don't eat whitespace here we can end up inserting too many
// semicolons.
// FIXME -- we need to handle block comments here.
%}
<MATRIX_START>{S}*{COMMENT}{SNLCMT}* |
<MATRIX_START>{S}*{NL}{SNLCMT}* {
LEXER_DEBUG ("<MATRIX_START>{S}*{COMMENT}{SNLCMT}*|<MATRIX_START>{S}*{NL}{SNLCMT}*");
scan_for_comments (yytext);
fixup_column_count (yytext);
eat_whitespace ();
lexer_flags.quote_is_transpose = false;
lexer_flags.convert_spaces_to_comma = true;
lexer_flags.at_beginning_of_statement = false;
if (nesting_level.none ())
return LEXICAL_ERROR;
if (! lexer_flags.looking_at_object_index.front ()
&& nesting_level.is_bracket_or_brace ())
{
maybe_warn_separator_insert (';');
COUNT_TOK_AND_RETURN (';');
}
}
\[{S}* {
LEXER_DEBUG ("\\[{S}*");
nesting_level.bracket ();
lexer_flags.looking_at_object_index.push_front (false);
current_input_column += yyleng;
lexer_flags.quote_is_transpose = false;
lexer_flags.convert_spaces_to_comma = true;
lexer_flags.looking_for_object_index = false;
lexer_flags.at_beginning_of_statement = false;
if (lexer_flags.defining_func
&& ! lexer_flags.parsed_function_name.top ())
lexer_flags.looking_at_return_list = true;
else
lexer_flags.looking_at_matrix_or_assign_lhs = true;
////promptflag--;
eat_whitespace ();
lexer_flags.bracketflag++;
BEGIN (MATRIX_START);
COUNT_TOK_AND_RETURN ('[');
}
\] {
LEXER_DEBUG ("\\]");
nesting_level.remove ();
lexer_flags.looking_at_object_index.pop_front ();
lexer_flags.looking_for_object_index = true;
lexer_flags.at_beginning_of_statement = false;
TOK_RETURN (']');
}
%{
// Imaginary numbers.
%}
{NUMBER}{Im} {
LEXER_DEBUG ("{NUMBER}{Im}");
handle_number ();
COUNT_TOK_AND_RETURN (IMAG_NUM);
}
%{
// Real numbers. Don't grab the `.' part of a dot operator as part of
// the constant.
%}
{D}+/\.[\*/\\^\'] |
{NUMBER} {
LEXER_DEBUG ("{D}+/\\.[\\*/\\^\\']|{NUMBER}");
handle_number ();
COUNT_TOK_AND_RETURN (NUM);
}
%{
// Eat whitespace. Whitespace inside matrix constants is handled by
// the <MATRIX_START> start state code above.
%}
{S}* {
current_input_column += yyleng;
}
%{
// Continuation lines. Allow comments after continuations.
%}
{CONT}{S}*{NL} |
{CONT}{S}*{COMMENT} {
LEXER_DEBUG ("{CONT}{S}*{NL}|{CONT}{S}*{COMMENT}");
if (yytext[0] == '\\')
gripe_matlab_incompatible_continuation ();
scan_for_comments (yytext);
////promptflag--;
input_line_number++;
current_input_column = 1;
}
%{
// End of file.
%}
<<EOF>> {
LEXER_DEBUG ("<<EOF>>");
if (block_comment_nesting_level != 0)
{
warning ("block comment open at end of input");
if ((reading_fcn_file || reading_script_file || reading_classdef_file)
&& ! curr_fcn_file_name.empty ())
warning ("near line %d of file `%s.m'",
input_line_number, curr_fcn_file_name.c_str ());
}
TOK_RETURN (END_OF_INPUT);
}
%{
//Types
%}
"%TYPE"{S}* {
BEGIN(TYPE_BEGIN);
}
%{
//Types
%}
<TYPE_BEGIN>[a-zA-Z\<\>0-9]+{S}*({IDENT}{S}*)*{IDENT}{S}*{NL} {
lexer_flags.addAnnotatedType(strdup(yytext));
BEGIN(INITIAL);
}
%{
// Identifiers. Truncate the token at the first space or tab but
// don't write directly on yytext.
%}
{IDENT}{S}* {
LEXER_DEBUG ("{IDENT}{S}*");
int id_tok = handle_identifier ();
if (id_tok >= 0)
COUNT_TOK_AND_RETURN (id_tok);
}
%{
// Superclass method identifiers.
%}
{IDENT}@{IDENT}{S}* |
{IDENT}@{IDENT}.{IDENT}{S}* {
LEXER_DEBUG ("{IDENT}@{IDENT}{S}*|{IDENT}@{IDENT}.{IDENT}{S}*");
int id_tok = handle_superclass_identifier ();
if (id_tok >= 0)
{
lexer_flags.looking_for_object_index = true;
COUNT_TOK_AND_RETURN (SUPERCLASSREF);
}
}
%{
// Metaclass query
%}
\?{IDENT}{S}* |
\?{IDENT}.{IDENT}{S}* {
LEXER_DEBUG ("\?{IDENT}{S}* | \?{IDENT}.{IDENT}{S}*");
int id_tok = handle_meta_identifier ();
if (id_tok >= 0)
{
lexer_flags.looking_for_object_index = true;
COUNT_TOK_AND_RETURN (METAQUERY);
}
}
%{
// Function handles and superclass references
%}
"@" {
LEXER_DEBUG ("@");
current_input_column++;
lexer_flags.quote_is_transpose = false;
lexer_flags.convert_spaces_to_comma = false;
lexer_flags.looking_at_function_handle++;
lexer_flags.looking_for_object_index = false;
lexer_flags.at_beginning_of_statement = false;
COUNT_TOK_AND_RETURN ('@');
}
%{
/*
%{
// Explicit type parameter
%}
"##" {
current_input_column++;
lexer_flags.quote_is_transpose = false;
lexer_flags.convert_spaces_to_comma = false;
lexer_flags.looking_for_object_index = false;
lexer_flags.at_beginning_of_statement = false;
COUNT_TOK_AND_RETURN('#');
}
*/
%}
%{
// A new line character. New line characters inside matrix constants
// are handled by the <MATRIX_START> start state code above. If closest
// nesting is inside parentheses, don't return a row separator.
%}
{NL} {
LEXER_DEBUG ("{NL}");
input_line_number++;
current_input_column = 1;
lexer_flags.quote_is_transpose = false;
lexer_flags.convert_spaces_to_comma = true;
if (nesting_level.none ())
{
lexer_flags.at_beginning_of_statement = true;
COUNT_TOK_AND_RETURN ('\n');
}
else if (nesting_level.is_paren ())
{
lexer_flags.at_beginning_of_statement = false;
gripe_matlab_incompatible ("bare newline inside parentheses");
}
else if (nesting_level.is_bracket_or_brace ())
return LEXICAL_ERROR;
}
%{
// Single quote can either be the beginning of a string or a transpose
// operator.
%}
"'" {
LEXER_DEBUG ("'");
current_input_column++;
lexer_flags.convert_spaces_to_comma = true;
if (lexer_flags.quote_is_transpose)
{
do_comma_insert_check ();
COUNT_TOK_AND_RETURN (QUOTE);
}
else
{
int tok = handle_string ('\'');
COUNT_TOK_AND_RETURN (tok);
}
}
%{
// Double quotes always begin strings.
%}
\" {
LEXER_DEBUG ("\"");
current_input_column++;
int tok = handle_string ('"');
COUNT_TOK_AND_RETURN (tok);
}
%{
// Gobble comments.
%}
{CCHAR} {
LEXER_DEBUG ("{CCHAR}");
lexer_flags.looking_for_object_index = false;
xunput (yytext[0], yytext);
bool eof = false;
int tok = process_comment (false, eof);
if (eof)
TOK_RETURN (END_OF_INPUT);
else if (tok > 0)
COUNT_TOK_AND_RETURN (tok);
}
%{
// Block comments.
%}
^{S}*{CCHAR}\{{S}*{NL} {
LEXER_DEBUG ("^{S}*{CCHAR}\\{{S}*{NL}");
lexer_flags.looking_for_object_index = false;
input_line_number++;
current_input_column = 1;
block_comment_nesting_level++;
////promptflag--;
bool eof = false;
process_comment (true, eof);
}
%{
// Other operators.
%}
":" { LEXER_DEBUG (":"); BIN_OP_RETURN (':', false, false); }
".+" { LEXER_DEBUG (".+"); XBIN_OP_RETURN (EPLUS, false, false); }
".-" { LEXER_DEBUG (".-"); XBIN_OP_RETURN (EMINUS, false, false); }
".*" { LEXER_DEBUG (".*"); BIN_OP_RETURN (EMUL, false, false); }
"./" { LEXER_DEBUG ("./"); BIN_OP_RETURN (EDIV, false, false); }
".\\" { LEXER_DEBUG (".\\"); BIN_OP_RETURN (ELEFTDIV, false, false); }
".^" { LEXER_DEBUG (".^"); BIN_OP_RETURN (EPOW, false, false); }
".**" { LEXER_DEBUG (".**"); XBIN_OP_RETURN (EPOW, false, false); }
".'" { LEXER_DEBUG (".'"); do_comma_insert_check (); BIN_OP_RETURN (TRANSPOSE, true, false); }
"++" { LEXER_DEBUG ("++"); do_comma_insert_check (); XBIN_OP_RETURN (PLUS_PLUS, true, false); }
"--" { LEXER_DEBUG ("--"); do_comma_insert_check (); XBIN_OP_RETURN (MINUS_MINUS, true, false); }
"<=" { LEXER_DEBUG ("<="); BIN_OP_RETURN (EXPR_LE, false, false); }
"==" { LEXER_DEBUG ("=="); BIN_OP_RETURN (EXPR_EQ, false, false); }
"~=" { LEXER_DEBUG ("~="); BIN_OP_RETURN (EXPR_NE, false, false); }
"!=" { LEXER_DEBUG ("!="); XBIN_OP_RETURN (EXPR_NE, false, false); }
">=" { LEXER_DEBUG (">="); BIN_OP_RETURN (EXPR_GE, false, false); }
"&" { LEXER_DEBUG ("&"); BIN_OP_RETURN (EXPR_AND, false, false); }
"|" { LEXER_DEBUG ("|"); BIN_OP_RETURN (EXPR_OR, false, false); }
"<" { LEXER_DEBUG ("<"); BIN_OP_RETURN (EXPR_LT, false, false); }
">" { LEXER_DEBUG (">"); BIN_OP_RETURN (EXPR_GT, false, false); }
"+" { LEXER_DEBUG ("+"); BIN_OP_RETURN ('+', false, false); }
"-" { LEXER_DEBUG ("-"); BIN_OP_RETURN ('-', false, false); }
"*" { LEXER_DEBUG ("*"); BIN_OP_RETURN ('*', false, false); }
"/" { LEXER_DEBUG ("/"); BIN_OP_RETURN ('/', false, false); }
"\\" { LEXER_DEBUG ("\\"); BIN_OP_RETURN (LEFTDIV, false, false); }
";" { LEXER_DEBUG (";"); BIN_OP_RETURN (';', true, true); }
"," { LEXER_DEBUG (","); BIN_OP_RETURN (',', true, ! lexer_flags.looking_at_object_index.front ()); }
"^" { LEXER_DEBUG ("^"); BIN_OP_RETURN (POW, false, false); }
"**" { LEXER_DEBUG ("**"); XBIN_OP_RETURN (POW, false, false); }
"=" { LEXER_DEBUG ("="); BIN_OP_RETURN ('=', true, false); }
"&&" { LEXER_DEBUG ("&&"); BIN_OP_RETURN (EXPR_AND_AND, false, false); }
"||" { LEXER_DEBUG ("||"); BIN_OP_RETURN (EXPR_OR_OR, false, false); }
"<<" { LEXER_DEBUG ("<<"); XBIN_OP_RETURN (LSHIFT, false, false); }
">>" { LEXER_DEBUG (">>"); XBIN_OP_RETURN (RSHIFT, false, false); }
{NOT} {
LEXER_DEBUG ("{NOT}");
if (yytext[0] == '~')
BIN_OP_RETURN (EXPR_NOT, false, false);
else
XBIN_OP_RETURN (EXPR_NOT, false, false);
}
"(" {
LEXER_DEBUG ("(");
// If we are looking for an object index, then push TRUE for
// looking_at_object_index. Otherwise, just push whatever state
// is current (so that we can pop it off the stack when we find
// the matching close paren).
lexer_flags.looking_at_object_index.push_front
(lexer_flags.looking_for_object_index);
lexer_flags.looking_at_indirect_ref = false;
lexer_flags.looking_for_object_index = false;
lexer_flags.at_beginning_of_statement = false;
nesting_level.paren ();
////promptflag--;
TOK_RETURN ('(');
}
")" {
LEXER_DEBUG (")");
nesting_level.remove ();
current_input_column++;
lexer_flags.looking_at_object_index.pop_front ();