Back to index

texmacs  1.0.7.15
packrat_parser.hpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : packrat_parser.hpp
00004 * DESCRIPTION: packrat parsers
00005 * COPYRIGHT  : (C) 2010  Joris van der Hoeven
00006 *******************************************************************************
00007 * This software falls under the GNU general public license version 3 or later.
00008 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
00009 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
00010 ******************************************************************************/
00011 
00012 #ifndef PACKRAT_PARSER_H
00013 #define PACKRAT_PARSER_H
00014 #include "packrat_grammar.hpp"
00015 
00016 #define PACKRAT_UNDEFINED ((C) (-2))
00017 #define PACKRAT_FAILED    ((C) (-1))
00018 
00019 class packrat_parser_rep: concrete_struct {
00020 public:
00021   hashmap<C,array<C> >      grammar;
00022   hashmap<C,tree>           productions;
00023   hashmap<D,string>         properties;
00024 
00025   tree                      current_tree;
00026   string                    current_string;
00027   hashmap<path,int>         current_start;
00028   hashmap<path,int>         current_end;
00029   hashmap<path,int>         current_path_pos;
00030   hashmap<int,path>         current_pos_path;
00031   C                         current_cursor;
00032   int                       current_hl_lan;
00033 
00034   array<C>                  current_input;
00035   hashmap<D,C>              current_cache;
00036   hashmap<D,tree>           current_production;
00037 
00038 protected:
00039   void serialize_atomic (tree t, path p);
00040   void serialize_compound (tree t, path p);
00041   void serialize (tree t, path p);
00042   void set_input (tree t);
00043   void set_cursor (path t_pos);
00044   path decode_path (tree t, path p, int pos);
00045   int  encode_path (tree t, path p, path pos);
00046 
00047 public:
00048   packrat_parser_rep (packrat_grammar gr);
00049 
00050   int  decode_string_position (C pos);
00051   C    encode_string_position (int i);
00052   path decode_tree_position (C pos);
00053   C    encode_tree_position (path p);
00054   C    parse (C sym, C pos);
00055 
00056   void inspect (C sym, C pos, array<C>& syms, array<C>& poss);
00057   bool is_left_recursive (C sym);
00058   bool is_associative (C sym);
00059   bool is_anti_associative (C sym);
00060   bool is_list_like (C sym);
00061   bool is_selectable (C sym);
00062   void context (C sym, C pos, C left, C right, int mode,
00063               array<C>& kind, array<C>& begin, array<C>& end);
00064   void compress (array<C>& kind, array<C>& begin, array<C>& end);
00065   void highlight (tree t, path tp, path p1, path p2, int col);
00066   void highlight (C sym, C pos);
00067 
00068   friend class packrat_parser;
00069 };
00070 
00071 class packrat_parser {
00072   CONCRETE_NULL (packrat_parser);
00073   inline packrat_parser (packrat_grammar gr, tree t, path t_pos= path ());
00074 };
00075 CONCRETE_NULL_CODE (packrat_parser);
00076 
00077 inline packrat_parser::packrat_parser
00078   (packrat_grammar gr, tree t, path t_pos):
00079     rep (tm_new<packrat_parser_rep> (gr)) {
00080       rep->set_input (t);
00081       rep->set_cursor (t_pos); }
00082 
00083 #endif // PACKRAT_PARSER_H