src/parser/Grammar.pm.skel - lemplate

  1. #============================================================= -*-Perl-*-
  2. #
  3. # Lemplate::Grammar
  4. #
  5. # DESCRIPTION
  6. #   Grammar file for the Template Toolkit language containing token
  7. #   definitions and parser state/rules tables generated by Parse::Yapp.
  8. #
  9. # AUTHOR
  10. #   Ingy döt Net   <ingy@cpan.org>
  11. #
  12. # ORIGINAL AUTHOR
  13. #   Andy Wardley   <abw@kfs.org>
  14. #
  15. # COPYRIGHT
  16. #   Copyright (C) 2006-2008 Ingy döt Net.
  17. #   Copyright (C) 1996-2000 Andy Wardley.
  18. #   Copyright (C) 1998-2000 Canon Research Centre Europe Ltd.
  19. #
  20. #   This module is free software; you can redistribute it and/or
  21. #   modify it under the same terms as Perl itself.
  22. #
  23. #------------------------------------------------------------------------
  24. #
  25. # NOTE: this module is constructed from the parser/Grammar.pm.skel
  26. # file by running the parser/yc script.  You only need to do this if
  27. # you have modified the grammar in the parser/Parser.yp file and need
  28. # to-recompile it.  See the README in the 'parser' directory for more
  29. # information (sub-directory of the Lemplate distribution).
  30. #
  31. #========================================================================

  32. package Lemplate::Grammar;

  33. require 5.004;

  34. use strict;
  35. use vars qw( $VERSION );

  36. $VERSION  = sprintf("%d.%02d", q$Revision: 2.10 $ =~ /(\d+)\.(\d+)/);

  37. my (@RESERVED, %CMPOP, $LEXTABLE, $RULES, $STATES);
  38. my ($factory, $rawstart);


  39. #========================================================================
  40. # Reserved words, comparison and binary operators
  41. #========================================================================

  42. @RESERVED = qw(
  43.    GET CALL SET DEFAULT INSERT INCLUDE PROCESS WRAPPER BLOCK END
  44.    USE RAW PLUGIN FILTER MACRO JAVASCRIPT TO STEP AND OR NOT DIV MOD
  45.    IF UNLESS ELSE ELSIF FOR NEXT WHILE SWITCH CASE META IN
  46.    TRY THROW CATCH FINAL LAST RETURN STOP CLEAR VIEW DEBUG
  47.     );

  48. # for historical reasons, != and == are converted to ne and eq to perform
  49. # stringwise comparison (mainly because it doesn't generate "non-numerical
  50. # comparison" warnings which != and == can) but the others (e.g. < > <= >=)
  51. # are not converted to their stringwise equivalents.  I added 'gt' et al,
  52. # briefly for v2.04d and then took them out again in 2.04e.

  53. %CMPOP = qw(
  54.     != ~=
  55.     == ==
  56.     <  <
  57.     >  >
  58.     >= >=
  59.     <= <=
  60. );


  61. #========================================================================
  62. # Lexer Token Table
  63. #========================================================================

  64. # lookup table used by lexer is initialised with special-cases
  65. $LEXTABLE = {
  66.     'FOREACH' => 'FOR',
  67.     'BREAK'   => 'LAST',
  68.     '&&'      => 'AND',
  69.     '||'      => 'OR',
  70.     '!'       => 'NOT',
  71.     '|'         => 'FILTER',
  72.     '.'       => 'DOT',
  73.     '_'       => 'CAT',
  74.     '..'      => 'TO',
  75. #    ':'       => 'MACRO',
  76.     '='       => 'ASSIGN',
  77.     '=>'      => 'ASSIGN',
  78. #    '->'      => 'ARROW',
  79.     ','       => 'COMMA',
  80.     '\\'      => 'REF',
  81.     'and'     => 'AND',      # explicitly specified so that qw( and or
  82.     'or'      => 'OR',      # not ) can always be used in lower case,
  83.     'not'     => 'NOT',      # regardless of ANYCASE flag
  84.     'mod'     => 'MOD',
  85.     'div'     => 'DIV',
  86. };

  87. # localise the temporary variables needed to complete lexer table
  88. {
  89. #    my @tokens = qw< ( ) [ ] { } ${ $ / ; : ? >;
  90.     my @tokens = qw< ( ) [ ] { } ${ $ + / ; : ? >;
  91.     my @cmpop  = keys %CMPOP;
  92. #    my @binop  = qw( + - * % );              # '/' above, in @tokens
  93.     my @binop  = qw( - * % );              # '+' and '/' above, in @tokens

  94.     # fill lexer table, slice by slice, with reserved words and operators
  95.     @$LEXTABLE{ @RESERVED, @cmpop, @binop, @tokens }
  96.    = ( @RESERVED, ('CMPOP') x @cmpop, ('BINOP') x @binop, @tokens );
  97. }


  98. #========================================================================
  99. # CLASS METHODS
  100. #========================================================================

  101. sub new {
  102.     my $class = shift;
  103.     bless {
  104.    LEXTABLE => $LEXTABLE,
  105.    STATES   => $STATES,
  106.    RULES    => $RULES,
  107.     }, $class;
  108. }

  109. # update method to set package-scoped $factory lexical
  110. sub install_factory {
  111.     my ($self, $new_factory) = @_;
  112.     $factory = $new_factory;
  113. }


  114. #========================================================================
  115. # States
  116. #========================================================================

  117. $STATES = <<$states>>;


  118. #========================================================================
  119. # Rules
  120. #========================================================================

  121. $RULES = <<$rules>>;

  122. 1;