lex.h

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 1993-2012 John W. Eaton
00004 
00005 This file is part of Octave.
00006 
00007 Octave is free software; you can redistribute it and/or modify it
00008 under the terms of the GNU General Public License as published by the
00009 Free Software Foundation; either version 3 of the License, or (at your
00010 option) any later version.
00011 
00012 Octave is distributed in the hope that it will be useful, but WITHOUT
00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00015 for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Octave; see the file COPYING.  If not, see
00019 <http://www.gnu.org/licenses/>.
00020 
00021 */
00022 
00023 #if !defined (octave_lex_h)
00024 #define octave_lex_h 1
00025 
00026 #include <list>
00027 #include <stack>
00028 
00029 // FIXME -- these input buffer things should be members of a
00030 // parser input stream class.
00031 
00032 typedef struct yy_buffer_state *YY_BUFFER_STATE;
00033 
00034 // Associate a buffer with a new file to read.
00035 extern OCTINTERP_API YY_BUFFER_STATE create_buffer (FILE *f);
00036 
00037 // Report the current buffer.
00038 extern OCTINTERP_API YY_BUFFER_STATE current_buffer (void);
00039 
00040 // Connect to new buffer buffer.
00041 extern OCTINTERP_API void switch_to_buffer (YY_BUFFER_STATE buf);
00042 
00043 // Delete a buffer.
00044 extern OCTINTERP_API void delete_buffer (YY_BUFFER_STATE buf);
00045 
00046 extern OCTINTERP_API void clear_all_buffers (void);
00047 
00048 extern OCTINTERP_API void cleanup_parser (void);
00049 
00050 // Is the given string a keyword?
00051 extern bool is_keyword (const std::string& s);
00052 
00053 extern void prep_lexer_for_script_file (void);
00054 extern void prep_lexer_for_function_file (void);
00055 
00056 // For communication between the lexer and parser.
00057 
00058 class
00059 lexical_feedback
00060 {
00061 public:
00062 
00063   lexical_feedback (void)
00064 
00065     : bracketflag (0), braceflag (0), looping (0),
00066       convert_spaces_to_comma (true), at_beginning_of_statement (true),
00067       defining_func (0), looking_at_function_handle (0),
00068       looking_at_anon_fcn_args (true),
00069       looking_at_return_list (false), looking_at_parameter_list (false),
00070       looking_at_decl_list (false), looking_at_initializer_expression (false),
00071       looking_at_matrix_or_assign_lhs (false), looking_at_object_index (),
00072       looking_for_object_index (false), do_comma_insert (false),
00073       looking_at_indirect_ref (false), parsed_function_name (),
00074       parsing_class_method (false), maybe_classdef_get_set_method (false),
00075       parsing_classdef (false), quote_is_transpose (false),
00076       pending_local_variables ()
00077 
00078     {
00079       init ();
00080     }
00081 
00082   ~lexical_feedback (void) { }
00083 
00084   void init (void);
00085 
00086   // Square bracket level count.
00087   int bracketflag;
00088 
00089   // Curly brace level count.
00090   int braceflag;
00091 
00092   // TRUE means we're in the middle of defining a loop.
00093   int looping;
00094 
00095   // TRUE means that we should convert spaces to a comma inside a
00096   // matrix definition.
00097   bool convert_spaces_to_comma;
00098 
00099   // TRUE means we are at the beginning of a statement, where a
00100   // command name is possible.
00101   bool at_beginning_of_statement;
00102 
00103   // Nonzero means we're in the middle of defining a function.
00104   int defining_func;
00105 
00106   // Nonzero means we are parsing a function handle.
00107   int looking_at_function_handle;
00108 
00109   // TRUE means we are parsing an anonymous function argument list.
00110   bool looking_at_anon_fcn_args;
00111 
00112   // TRUE means we're parsing the return list for a function.
00113   bool looking_at_return_list;
00114 
00115   // TRUE means we're parsing the parameter list for a function.
00116   bool looking_at_parameter_list;
00117 
00118   // TRUE means we're parsing a declaration list (global or
00119   // persistent).
00120   bool looking_at_decl_list;
00121 
00122   // TRUE means we are looking at the initializer expression for a
00123   // parameter list element.
00124   bool looking_at_initializer_expression;
00125 
00126   // TRUE means we're parsing a matrix or the left hand side of
00127   // multi-value assignment statement.
00128   bool looking_at_matrix_or_assign_lhs;
00129 
00130   // If the front of the list is TRUE, the closest paren, brace, or
00131   // bracket nesting is an index for an object.
00132   std::list<bool> looking_at_object_index;
00133 
00134   // Object index not possible until we've seen something.
00135   bool looking_for_object_index;
00136 
00137   // GAG.  Stupid kludge so that [[1,2][3,4]] will work.
00138   bool do_comma_insert;
00139 
00140   // TRUE means we're looking at an indirect reference to a
00141   // structure element.
00142   bool looking_at_indirect_ref;
00143 
00144   // If the top of the stack is TRUE, then we've already seen the name
00145   // of the current function.  Should only matter if
00146   // current_function_level > 0
00147   std::stack<bool> parsed_function_name;
00148 
00149   // TRUE means we are parsing a class method in function or classdef file.
00150   bool parsing_class_method;
00151 
00152   // TRUE means we are parsing a class method declaration line in a
00153   // classdef file and can accept a property get or set method name.
00154   // For example, "get.PropertyName" is recognized as a function name.
00155   bool maybe_classdef_get_set_method;
00156 
00157   // TRUE means we are parsing a classdef file
00158   bool parsing_classdef;
00159 
00160   // Return transpose or start a string?
00161   bool quote_is_transpose;
00162 
00163   // Set of identifiers that might be local variable names.
00164   std::set<std::string> pending_local_variables;
00165 
00166 private:
00167 
00168   lexical_feedback (const lexical_feedback&);
00169 
00170   lexical_feedback& operator = (const lexical_feedback&);
00171 };
00172 
00173 class
00174 stream_reader
00175 {
00176 public:
00177   virtual int getc (void) = 0;
00178   virtual int ungetc (int c) = 0;
00179 
00180 protected:
00181   stream_reader (void) { }
00182   ~stream_reader (void) { }
00183 
00184 private:
00185 
00186   // No copying!
00187   stream_reader (const stream_reader&);
00188   stream_reader& operator = (const stream_reader&);
00189 };
00190 
00191 extern std::string
00192 grab_comment_block (stream_reader& reader, bool at_bol, bool& eof);
00193 
00194 // TRUE means that we have encountered EOF on the input stream.
00195 extern bool parser_end_of_input;
00196 
00197 // Flags that need to be shared between the lexer and parser.
00198 extern lexical_feedback lexer_flags;
00199 
00200 #endif
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines