       1 package Parsers::SimpleCalcYYLexer;
       2 #
       3 # File: SimpleCalcYYLexer.pm
       4 # Author: Manish Sud <msud@san.rr.com>
       5 #
       6 # Copyright (C) 2018 Manish Sud. All rights reserved.
       7 #
       8 # This file is part of MayaChemTools.
       9 #
      10 # MayaChemTools is free software; you can redistribute it and/or modify it under
      11 # the terms of the GNU Lesser General Public License as published by the Free
      12 # Software Foundation; either version 3 of the License, or (at your option) any
      13 # later version.
      14 #
      15 # MayaChemTools is distributed in the hope that it will be useful, but without
      16 # any warranty; without even the implied warranty of merchantability of fitness
      17 # for a particular purpose.  See the GNU Lesser General Public License for more
      18 # details.
      19 #
      20 # You should have received a copy of the GNU Lesser General Public License
      21 # along with MayaChemTools; if not, see <http://www.gnu.org/licenses/> or
      22 # write to the Free Software Foundation Inc., 59 Temple Place, Suite 330,
      23 # Boston, MA, 02111-1307, USA.
      24 #
      25
      26 use strict;
      27 use Carp;
      28 use Exporter;
      29 use Scalar::Util ();
      30 use Parsers::YYLexer;
      31
      32 use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
      33
      34 @ISA = qw(Parsers::YYLexer Exporter);
      35 @EXPORT = qw();
      36 @EXPORT_OK = qw();
      37
      38 %EXPORT_TAGS = (all  => [@EXPORT, @EXPORT_OK]);
      39
      40 # Setup class variables...
      41 my($ClassName, $YYTabFile, @YYLexerTokensSpec);
      42 _InitializeClass();
      43
      44 # Overload Perl functions...
      45 use overload '""' => 'StringifySimpleCalcYYLexer';
      46
      47 # Class constructor...
      48 sub new {
      49   my($Class, $Input) = @_;
      50   my(@TokensSpec);
      51
      52   # Initialize object...
      53   my $This = $Class->SUPER::new($Input,  @YYLexerTokensSpec);
      54   bless $This, ref($Class) || $Class;
      55   $This->_InitializeYYLexer();
      56
      57   return $This;
      58 }
      59
      60 # Initialize object data...
      61 #
      62 sub _InitializeYYLexer {
      63   my($This) = @_;
      64
      65   # Setup default YYTabFile containing mapping of token names to numbers...
      66   $This->SetupYYTabFile($YYTabFile);
      67
      68   return $This;
      69 }
      70
      71 # Initialize class ...
      72 sub _InitializeClass {
      73   #Class name...
      74   $ClassName = __PACKAGE__;
      75
      76   # Setup default token table file...
      77   $YYTabFile = "Parsers/SimpleCalcParser.tab.ph";
      78
      79   # Setup default lexer tokens specs...
      80   @YYLexerTokensSpec = (
      81                         [ 'LETTER', qr/[a-zA-Z]/ ],
      82                         [ 'NUMBER', qr/\d+/ ],
      83                         [ 'SPACE', qr/[ ]*/, sub { my($This, $TokenLabel, $MatchedText) = @_; return ''; } ],
      84                         [ 'NEWLINE', qr/(?:\r\n|\r|\n)/, sub { my($This, $TokenLabel, $MatchedText) = @_;  return "\n"; } ],
      85                         [ 'CHAR', qr/./ ]
      86                        );
      87 }
      88
      89 # Is it a lexer object?
      90 sub _IsSimpleCalcYYLexer {
      91   my($Object) = @_;
      92
      93   return (Scalar::Util::blessed($Object) && $Object->isa($ClassName)) ? 1 : 0;
      94 }
      95
      96 # Return a string containing information about lexer...
      97 sub StringifySimpleCalcYYLexer {
      98   my($This) = @_;
      99   my($SimleCalcYYLexerString);
     100
     101   $SimleCalcYYLexerString = "SimpleCalcYYLexer: PackageName: $ClassName; " . $This->_GetYYLexerInfoString();
     102
     103   return $SimleCalcYYLexerString;
     104 }
     105