home *** CD-ROM | disk | FTP | other *** search
/ hobbes.nmsu.edu 2008 / 2008-06-02_hobbes.nmsu.edu.zip / new / scummc-0.2.0-os2.zip / ScummC / src / scc_lex.h < prev    next >
Encoding:
C/C++ Source or Header  |  2008-02-08  |  4.5 KB  |  166 lines

  1. /* ScummC
  2.  * Copyright (C) 2005-2006  Alban Bedel
  3.  *
  4.  * This program is free software; you can redistribute it and/or
  5.  * modify it under the terms of the GNU General Public License
  6.  * as published by the Free Software Foundation; either version 2
  7.  * of the License, or (at your option) any later version.
  8.  
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program; if not, write to the Free Software
  16.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  17.  *
  18.  */
  19.  
  20. /// @defgroup lex Lexical parsing
  21. /** 
  22.  *  @file scc_lex.h
  23.  *  @ingroup lex
  24.  *  @brief Base class to implement lexers.
  25.  */
  26.  
  27. // Get a default definition for YYSYPE and YYLTYPE.
  28. #ifndef DOXYGEN_SKIP
  29. #if ! defined (YYSTYPE) && ! defined (YYSTYPE_IS_DECLARED)
  30. #define YYSTYPE void
  31. #endif
  32.  
  33. #if ! defined (YYLTYPE) && ! defined (YYLTYPE_IS_DECLARED)
  34. #define YYLTYPE void
  35. #endif
  36. #endif
  37.  
  38. typedef struct scc_lexbuf scc_lexbuf_t;
  39. typedef struct scc_lexer scc_lexer_t;
  40. typedef struct scc_lex scc_lex_t;
  41. typedef struct scc_define scc_define_t;
  42. typedef struct scc_keyword scc_keyword_t;
  43.  
  44. /// Lexer function.
  45. typedef int (*scc_lexer_f)(YYSTYPE *lvalp, YYLTYPE *llocp,scc_lex_t* lex);
  46. /// Callback to set YYLTYPE
  47. typedef void (*scc_lexer_pos_f)(YYLTYPE *llocp,int line,int column);
  48. /// Callback to track which files are opened.
  49. typedef void (*scc_lexer_opened_f)(void* userdata,char* file);
  50.  
  51.  
  52. /// The lexer class.
  53. struct scc_lex {
  54.     /// buffer stack
  55.     scc_lexbuf_t* buffer;
  56.     /// pending error
  57.     char* error;
  58.     /// lexer stack
  59.     scc_lexer_t* lexer;
  60.     /// position tracking
  61.     scc_lexer_pos_f set_start_pos;
  62.     scc_lexer_pos_f set_end_pos;
  63.     /// include paths
  64.     char** include;
  65.     /// If set just continue when an include is missing
  66.     int ignore_missing_include;
  67.     /// callback to track deps
  68.     scc_lexer_opened_f opened;
  69.     void* userdata;
  70.     /// Defines list
  71.     scc_define_t* define;
  72.     unsigned num_define;
  73. };
  74.  
  75. /// Common struct to store compiler keywords.
  76. struct scc_keyword {
  77.     char* name;
  78.     int type, val;
  79. };
  80.  
  81.  
  82. /// @name Public Functions
  83. //@{
  84.  
  85. /// Create a lexer instance.
  86. scc_lex_t* scc_lex_new(scc_lexer_f lexer,scc_lexer_pos_f set_start_pos,
  87.                        scc_lexer_pos_f set_end_pos, char** include);
  88.  
  89. /// Return the next token setting lvalp and llocp
  90. int scc_lex_lex(YYSTYPE *lvalp, YYLTYPE *llocp,scc_lex_t* lex);
  91.  
  92. /// Set a buffer to be read, buffers are automatically poped when eof is reached.
  93. int scc_lex_push_buffer(scc_lex_t* lex,char* file);
  94.  
  95. /// Clear an error to resume parsing
  96. void scc_lex_clear_error(scc_lex_t* lex);
  97.  
  98. /// Get the current file.
  99. char* scc_lex_get_file(scc_lex_t* lex);
  100.  
  101. /// Get the current position.
  102. int scc_lex_get_line_column(scc_lex_t* lex,int* line,int* column);
  103.  
  104. //@}
  105.  
  106.  
  107. /// @name Lexer Functions
  108. /// These functions are for the lexer implementations.
  109. //@{
  110.  
  111. /// Set an error.
  112. void scc_lex_error(scc_lex_t* lex,char* fmt, ...)  PRINTF_ATTRIB(2,3);
  113.  
  114. /// Read a char but doesn't move the input stream.
  115. char scc_lex_at(scc_lex_t* lex,unsigned pos);
  116.  
  117. /// Pump a char from the input stream.
  118. char scc_lex_getc(scc_lex_t* lex);
  119.  
  120. /// Get a string of length len from the input.
  121. char* scc_lex_gets(scc_lex_t* lex,unsigned len);
  122.  
  123. /// Get a string of length len from the input and append it to str.
  124. char* scc_lex_strcat(scc_lex_t* lex,char* str,unsigned len);
  125.  
  126. /// Put a char back in the input stream.
  127. int scc_lex_ungetc(scc_lex_t* lex,char c);
  128.  
  129. /// Move the input stream forward of n chars.
  130. void scc_lex_drop(scc_lex_t* lex,unsigned n);
  131.  
  132. /// Search for the char c starting from start.
  133. int scc_lex_strchr(scc_lex_t* lex,unsigned start,char c);
  134.  
  135. /// Push a new lexer.
  136. int scc_lex_push_lexer(scc_lex_t* lex, scc_lexer_f lexf);
  137.  
  138. /// Pop the current lexer.
  139. int scc_lex_pop_lexer(scc_lex_t* lex);
  140.  
  141. /// Set a define
  142. void scc_lex_define(scc_lex_t* lex, char* name, char* val, int line, int col);
  143.  
  144. /// Check if a symbol is a define
  145. int scc_lex_is_define(scc_lex_t* lex, char* name);
  146.  
  147. /// Expand a define
  148. int scc_lex_expand_define(scc_lex_t* lex, char* name);
  149.  
  150. /// Find a keyword, the array MUST be sorted.
  151. scc_keyword_t* scc_is_keyword(char* s,scc_keyword_t* kw,unsigned num_kw);
  152.  
  153. //@}
  154.  
  155.  
  156. /// @name Private Functions
  157. //@{
  158.  
  159. /// Get back to the last file.
  160. int scc_lex_pop_buffer(scc_lex_t* lex);
  161.  
  162. /// Fill the buffer so that min_len bytes can be read
  163. int scc_lex_fill_buffer(scc_lex_t* lex,unsigned min_len);
  164.  
  165. //@}
  166.