PreviousUpNext

15.4.207  src/lib/c-kit/src/parser/grammar/token-table-g.pkg

## token-table-g.pkg

# Compiled by:
#     src/lib/c-kit/src/parser/c-parser.sublib


stipulate
    package qht =  quickstring_hashtable;                                       # quickstring_hashtable         is from   src/lib/src/quickstring-hashtable.pkg
herein

    generic package   token_table_g   (
        #             =============
        #
        package tokens:  Ckit_Tokens;                                           # Ckit_Tokens                   is from   src/lib/c-kit/src/parser/grammar/c.grammar.api
    )
    : (weak)  Token_Table                                                       # Token_Table                   is from   src/lib/c-kit/src/parser/grammar/token-table.api
    {
        package tokens = tokens;
        package parse_control= config::parse_control;                           # config                        is from   src/lib/c-kit/src/variants/ansi-c/config.pkg

        Item = (Int, Int) -> tokens::Token( tokens::Semantic_Value, Int ); 

        exception KEYWORD;
        exception LEX_ERROR;

        my keywords:   qht::Hashtable( Item )
                   =   qht::make_hashtable  { size_hint => 64,  not_found_exception => KEYWORD };

        stipulate
          insert = qht::set keywords;
          fun ins (s, item) = insert (quickstring__premicrothread::from_string s, item);

          fun id_tok (s, pos, end_position)
              =
              if   (type_defs::check_tdef (s) == TRUE)
                   #
                   tokens::type_name  (s, pos, end_position);
              else tokens::identifier (s, pos, end_position);
              fi;

          #  to enter GCC-style 'underscore'-versions of certain keywords 
          fun insaug (s, item)
              =
              {
                  fun item' (p as (pos, end_position))
                      =
                      case parse_control::underscore_keywords   
                          NULL => id_tok (s, pos, end_position);
                         THE TRUE => item p;
                         THE FALSE =>
                          { parse_control::violation
                               (cat ["gcc-style keywords '__", s, "' or '__",
                                        s, "__' are not allowed"]);
                               raise exception LEX_ERROR;};
                      esac;

                  ins ("__" + s, item');
                  ins ("__" + s + "__", item');
              };

          normaltokens =
              [("auto", tokens::auto),
               ("extern", tokens::extern),
               ("register", tokens::register),
               ("static", tokens::static),
               ("unsigned", tokens::unsigned),
               ("break", tokens::break),
               ("case", tokens::case_t),
               ("continue", tokens::continue),
               ("default", tokens::default),
               ("do", tokens::do_t),
               ("else", tokens::else_t),
               ("for", tokens::for_t),
               ("goto", tokens::goto),
               ("if", tokens::if_t),
               ("enum", tokens::enum_t),
               ("float", tokens::float),
               ("double", tokens::double),
               ("char", tokens::char),
               ("int", tokens::int),
               ("long", tokens::long),
               ("short", tokens::short),
               ("struct", tokens::struct),
               ("union", tokens::union),
               ("void", tokens::void),
               ("sizeof", tokens::sizeof),
               ("typedef", tokens::typedef),
               ("return", tokens::return),
               ("switch", tokens::switch),
               ("while", tokens::while_t)];

          #  tokens for which gcc has __* and __*__ versions 
          #
          augmentabletokens
              =
              [ ("signed", tokens::signed),

                ("const", \\ p =  if   (parse_control::const_allowed)

                                       (tokens::const p);
                                  else
                                       parse_control::violation
                                       "the keyword 'const' not allowed";
                                       raise exception LEX_ERROR;
                                  fi
                ),

                ("volatile", \\ p =  if   (parse_control::volatile_allowed )

                                          (tokens::volatile p);
                                     else
                                          parse_control::violation
                                          "the keyword 'volatile' not allowed";
                                          raise exception LEX_ERROR;
                                     fi
                )
              ];

          #  tokens for D 
          #
          dtokens =
              [
               ];

          my _ =
              { apply ins normaltokens;
               apply ins augmentabletokens;
               apply insaug augmentabletokens;
               /* enter D keywords only when allowed...
                * (I think the ParseControl test is done at the wrong time here.
                *  - Blume) */
               if parse_control::dkeywords  apply ins dtokens; fi;};
        herein
            fun check_token (s, pos)
                =
                {
                    end_position = pos + size s;

                    name = quickstring__premicrothread::from_string s;

                    case (qht::find keywords name)
                        #
                        THE tok_g =>  tok_g (pos, end_position);
                        NULL      =>  id_tok   (s, pos, end_position);
                    esac;
                };
        end;                    # stipulate
    };
end;

## Copyright (c) 1998 by Lucent Technologies 
## Subsequent changes by Jeff Prothero Copyright (c) 2010-2015,
## released per terms of SMLNJ-COPYRIGHT.


Comments and suggestions to: bugs@mythryl.org

PreviousUpNext