Mercurial > hg > octave-lyh
comparison libinterp/parse-tree/lex.h @ 16293:57e87ddfee14
create base class for lexer
* lex.h, lex.ll, parse.h, oct-parse.in.yy: (octave_base_lexer):
New base class for lexer class. Move most of previous octave_lexer
class here. Change all uses.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 13 Mar 2013 02:46:56 -0400 |
parents | 6ce905b89cee |
children | 0925d1f6875e |
comparison
equal
deleted
inserted
replaced
16292:6ce905b89cee | 16293:57e87ddfee14 |
---|---|
389 lexical_feedback (const lexical_feedback&); | 389 lexical_feedback (const lexical_feedback&); |
390 | 390 |
391 lexical_feedback& operator = (const lexical_feedback&); | 391 lexical_feedback& operator = (const lexical_feedback&); |
392 }; | 392 }; |
393 | 393 |
394 // octave_lexer inherits from lexical_feedback because we will | 394 // octave_base_lexer inherits from lexical_feedback because we will |
395 // eventually have several different constructors and it is easier to | 395 // eventually have several different constructors and it is easier to |
396 // intialize if everything is grouped in a parent class rather than | 396 // intialize if everything is grouped in a parent class rather than |
397 // listing all the members in the octave_lexer class. | 397 // listing all the members in the octave_base_lexer class. |
398 | 398 |
399 class | 399 class |
400 octave_lexer : public lexical_feedback | 400 octave_base_lexer : public lexical_feedback |
401 { | 401 { |
402 public: | 402 public: |
403 | 403 |
404 // Handle buffering of input for lexer. | 404 // Handle buffering of input for lexer. |
405 | 405 |
426 const char *pos; | 426 const char *pos; |
427 size_t chars_left; | 427 size_t chars_left; |
428 bool eof; | 428 bool eof; |
429 }; | 429 }; |
430 | 430 |
431 octave_lexer (void) | 431 octave_base_lexer (void) |
432 : lexical_feedback (), scanner (0), input_buf (), input_reader () | 432 : lexical_feedback (), scanner (0), input_buf () |
433 { | 433 { |
434 init (); | 434 init (); |
435 } | 435 } |
436 | 436 |
437 octave_lexer (FILE *file) | 437 virtual ~octave_base_lexer (void); |
438 : lexical_feedback (), scanner (0), input_buf (), | |
439 input_reader (file) | |
440 { | |
441 init (); | |
442 } | |
443 | |
444 octave_lexer (const std::string& eval_string) | |
445 : lexical_feedback (), scanner (0), input_buf (), | |
446 input_reader (eval_string) | |
447 { | |
448 init (); | |
449 } | |
450 | |
451 ~octave_lexer (void); | |
452 | 438 |
453 void init (void); | 439 void init (void); |
454 | 440 |
455 void reset (void); | 441 virtual void reset (void); |
456 | 442 |
457 void prep_for_file (void); | 443 void prep_for_file (void); |
458 | 444 |
459 int read (char *buf, unsigned int max_size); | 445 virtual int fill_flex_buffer (char *buf, unsigned int max_size) = 0; |
460 | 446 |
461 int handle_end_of_input (void); | 447 int handle_end_of_input (void); |
462 | 448 |
463 char *flex_yytext (void); | 449 char *flex_yytext (void); |
464 | 450 |
527 void *scanner; | 513 void *scanner; |
528 | 514 |
529 // Object that reads and buffers input. | 515 // Object that reads and buffers input. |
530 input_buffer input_buf; | 516 input_buffer input_buf; |
531 | 517 |
532 octave_input_reader input_reader; | 518 virtual void increment_promptflag (void) = 0; |
519 | |
520 virtual void decrement_promptflag (void) = 0; | |
521 | |
522 virtual int promptflag (void) const = 0; | |
523 | |
524 virtual int promptflag (int) = 0; | |
525 | |
526 virtual std::string input_source (void) const { return "unknown"; } | |
527 | |
528 virtual bool input_from_terminal (void) const { return false; } | |
529 | |
530 virtual bool input_from_file (void) const { return false; } | |
531 | |
532 virtual bool input_from_eval_string (void) const { return false; } | |
533 | |
534 void push_start_state (int state); | |
535 | |
536 void pop_start_state (void); | |
537 | |
538 void clear_start_state (void); | |
539 | |
540 int start_state (void) const { return start_state_stack.top (); } | |
541 | |
542 void display_start_state (void) const; | |
543 | |
544 int handle_op (const char *pattern, int tok, bool bos = false); | |
545 | |
546 int handle_incompatible_op (const char *pattern, int tok, bool bos = false); | |
547 | |
548 bool maybe_unput_comma_before_unary_op (int tok); | |
549 | |
550 int handle_unary_op (int tok, bool bos = false); | |
551 | |
552 int handle_incompatible_unary_op (int tok, bool bos = false); | |
553 | |
554 int handle_assign_op (const char *pattern, int tok); | |
555 | |
556 int handle_incompatible_assign_op (const char *pattern, int tok); | |
557 | |
558 int handle_op_internal (int tok, bool bos, bool compat); | |
559 | |
560 int handle_token (const std::string& name, int tok); | |
561 | |
562 int handle_token (int tok, token *tok_val = 0); | |
563 | |
564 int count_token (int tok); | |
565 | |
566 int count_token_internal (int tok); | |
567 | |
568 int show_token (int tok); | |
569 | |
570 // For unwind protect. | |
571 static void cleanup (octave_base_lexer *lexer) { delete lexer; } | |
572 | |
573 protected: | |
574 | |
575 std::stack<int> start_state_stack; | |
576 | |
577 // No copying! | |
578 | |
579 octave_base_lexer (const octave_base_lexer&); | |
580 | |
581 octave_base_lexer& operator = (const octave_base_lexer&); | |
582 }; | |
583 | |
584 class | |
585 octave_lexer : public octave_base_lexer | |
586 { | |
587 public: | |
588 | |
589 octave_lexer (void) | |
590 : octave_base_lexer (), input_reader () | |
591 { } | |
592 | |
593 octave_lexer (FILE *file) | |
594 : octave_base_lexer (), input_reader (file) | |
595 { } | |
596 | |
597 octave_lexer (const std::string& eval_string) | |
598 : octave_base_lexer (), input_reader (eval_string) | |
599 { } | |
600 | |
601 void reset (void) | |
602 { | |
603 input_reader.reset (); | |
604 | |
605 octave_base_lexer::reset (); | |
606 } | |
533 | 607 |
534 void increment_promptflag (void) { input_reader.increment_promptflag (); } | 608 void increment_promptflag (void) { input_reader.increment_promptflag (); } |
535 | 609 |
536 void decrement_promptflag (void) { input_reader.decrement_promptflag (); } | 610 void decrement_promptflag (void) { input_reader.decrement_promptflag (); } |
537 | 611 |
557 bool input_from_eval_string (void) const | 631 bool input_from_eval_string (void) const |
558 { | 632 { |
559 return input_source () == "eval_string"; | 633 return input_source () == "eval_string"; |
560 } | 634 } |
561 | 635 |
562 void push_start_state (int state); | 636 int fill_flex_buffer (char *buf, unsigned int max_size); |
563 | 637 |
564 void pop_start_state (void); | 638 octave_input_reader input_reader; |
565 | 639 |
566 void clear_start_state (void); | 640 protected: |
567 | |
568 int start_state (void) const { return start_state_stack.top (); } | |
569 | |
570 void display_start_state (void) const; | |
571 | |
572 int handle_op (const char *pattern, int tok, bool bos = false); | |
573 | |
574 int handle_incompatible_op (const char *pattern, int tok, bool bos = false); | |
575 | |
576 bool maybe_unput_comma_before_unary_op (int tok); | |
577 | |
578 int handle_unary_op (int tok, bool bos = false); | |
579 | |
580 int handle_incompatible_unary_op (int tok, bool bos = false); | |
581 | |
582 int handle_assign_op (const char *pattern, int tok); | |
583 | |
584 int handle_incompatible_assign_op (const char *pattern, int tok); | |
585 | |
586 int handle_op_internal (int tok, bool bos, bool compat); | |
587 | |
588 int handle_token (const std::string& name, int tok); | |
589 | |
590 int handle_token (int tok, token *tok_val = 0); | |
591 | |
592 int count_token (int tok); | |
593 | |
594 int count_token_internal (int tok); | |
595 | |
596 int show_token (int tok); | |
597 | |
598 // For unwind protect. | |
599 static void cleanup (octave_lexer *lexer) { delete lexer; } | |
600 | |
601 private: | |
602 | |
603 std::stack<int> start_state_stack; | |
604 | 641 |
605 // No copying! | 642 // No copying! |
606 | 643 |
607 octave_lexer (const octave_lexer&); | 644 octave_lexer (const octave_lexer&); |
608 | 645 |