CWB
|
#include "macro.h"
#include "options.h"
#include "output.h"
#include "cqp.h"
#include "hash.h"
#include "eval.h"
#include "ranges.h"
#include "targets.h"
#include "corpmanag.h"
#include "parser.tab.h"
#include "../cl/macros.h"
#include <assert.h>
#include <string.h>
Data Structures | |
struct | _InputBuffer |
struct | _MacroSegment |
the macro replacement string is stored as a sequence of segments: More... | |
struct | _MacroEntry |
an entry in the macro database More... | |
struct | _MacroHashTable |
This hash implementation can hold multiple macro entries in a single bucket in order to avoid cache overflow when very many macros are defined. More... | |
Macros | |
#define | MACRO_HASH_BUCKETS 8000 |
Typedefs | |
typedef struct _InputBuffer * | InputBuffer |
typedef struct _MacroSegment * | MacroSegment |
the macro replacement string is stored as a sequence of segments: More... | |
typedef struct _MacroEntry * | MacroEntry |
an entry in the macro database More... | |
typedef struct _MacroHashTable * | MacroHashTable |
This hash implementation can hold multiple macro entries in a single bucket in order to avoid cache overflow when very many macros are defined. More... | |
Functions | |
int | yylex (void) |
void | macro_debug_newline_indent (void) |
indent macro expansion debugging output (according to # of active input buffers) More... | |
InputBuffer | PushInputBuffer (int size) |
push new input buffer (which can hold string of lenght <size>) on top of buffer list More... | |
void | PopInputBuffer (void) |
delete input buffer from top of list More... | |
void | MakeMacroHash (int size) |
has to be called once to initialise the hash More... | |
MacroEntry | MacroHashLookup (char *str, int args) |
MacroEntry | MacroHashAdd (char *str, int args) |
void | MacroHashDelete (MacroEntry macro) |
MacroSegment | MacroAddSegment (MacroEntry macro) |
append segment to macro replacement string, returns pointer to the new, initialised segment More... | |
int | yy_input_char (void) |
Get a character for the lexer/parser. More... | |
int | yy_input_from_macro (void) |
checks if input is being read from macro expansion More... | |
void | init_macros (void) |
Initialises the macro hash and defines built-in macros. More... | |
int | expand_macro (char *name) |
expand macro <name>: More... | |
int | define_macro (char *name, int args, char *argstr, char *definition) |
define a new macro: name = macro name args = # of arguments (0 . More... | |
char * | preprocess_input_line (char *line) |
internal function for load_macro_file(): remove whitespace & comments from input line More... | |
char * | parse_macro_name (char *text, int *nr_of_args, char **prototype) |
void | load_macro_file (char *filename) |
int | delete_macro_buffers (int trace) |
delete active input buffers More... | |
void | macro_iterator_new (void) |
Resets the module-global iterator_bucket and iterator_entry variables. More... | |
MacroEntry | macro_iterator_next_macro (char *prefix) |
internal iterator function used by macro_iterator_next() and macro_iterator_next_prototype() More... | |
char * | macro_iterator_next (char *prefix, int *nargs) |
char * | macro_iterator_next_prototype (char *prefix) |
static int | list_macros_sort (const void *p1, const void *p2) |
internal function for sorting list of macros More... | |
void | list_macros (char *prefix) |
list all defined macros on stdout More... | |
void | print_macro_definition (char *name, int args) |
print definition of macro on stdout More... | |
void | macro_statistics (void) |
Prints macro hash statistics on stderr. More... | |
Variables | |
FILE * | yyin |
char * | yytext |
InputBuffer | InputBufferList = NULL |
list of active input buffers More... | |
MacroHashTable | MacroHash = NULL |
char * | macro_arg [10] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL} |
macro args are stored in a global array so we can always free() the memory More... | |
unsigned int | pseudo_arg_counter = 0 |
unique number for pseudo argument in each macro call More... | |
int | iterator_bucket = -1 |
current bucket in macro iterator More... | |
MacroEntry | iterator_entry = NULL |
current entry in bucket (if NULL, go to next bucket) More... | |
#define MACRO_HASH_BUCKETS 8000 |
Referenced by init_macros().
typedef struct _InputBuffer * InputBuffer |
typedef struct _MacroEntry * MacroEntry |
an entry in the macro database
typedef struct _MacroHashTable * MacroHashTable |
This hash implementation can hold multiple macro entries in a single bucket in order to avoid cache overflow when very many macros are defined.
Since we don't need to store any other information in the buckets, a bucket is simply a pointer to a list of macro entries, i.e. a MacroEntry.
typedef struct _MacroSegment * MacroSegment |
the macro replacement string is stored as a sequence of segments:
literal text ... string = "...", arg = -1 argument ... string = NULL, arg = 0 .. 9 pseudo arg ... string = NULL, arg = -1
int define_macro | ( | char * | name, |
int | args, | ||
char * | argstr, | ||
char * | definition | ||
) |
define a new macro: name = macro name args = # of arguments (0 .
. 9); alternatively, specify: argstr = macro argument string (e.g. ``$0=name $1=label'') definition = macro definition ... this string is substituted for /<name>(...) $0 .. $9 refer to the macro's arguments and CAN NOT be escaped returns 1 if macro definition was successful, 0 on syntax error
References _MacroSegment::arg, _MacroEntry::argnames, cl_malloc(), cqpmessage(), Error, MacroAddSegment(), MacroHashAdd(), MacroHashDelete(), MacroHashLookup(), silent, and _MacroSegment::string.
Referenced by init_macros(), and load_macro_file().
int delete_macro_buffers | ( | int | trace | ) |
delete active input buffers
References _MacroEntry::args, _InputBuffer::data, _InputBuffer::macro, _MacroEntry::name, PopInputBuffer(), and _InputBuffer::position.
int expand_macro | ( | char * | name | ) |
expand macro <name>:
References _MacroEntry::active, _MacroSegment::arg, buf, cl_free, cl_strdup(), cqpmessage(), _InputBuffer::data, Error, _InputBuffer::macro, macro_arg, macro_debug, macro_debug_newline_indent(), MacroHashLookup(), _MacroSegment::next, pseudo_arg_counter, PushInputBuffer(), _MacroEntry::replacement, _MacroSegment::string, token, yylex(), and yytext.
void init_macros | ( | void | ) |
Initialises the macro hash and defines built-in macros.
References cqpmessage(), define_macro(), Error, MACRO_HASH_BUCKETS, and MakeMacroHash().
Referenced by initialize_cqp().
void list_macros | ( | char * | prefix | ) |
list all defined macros on stdout
References _MacroEntry::args, cl_malloc(), enable_macros, end_indented_list(), _MacroHashTable::hash, list_macros_sort(), _MacroEntry::name, _MacroEntry::next, pretty_print, print_indented_list_br(), print_indented_list_item(), _MacroHashTable::size, and start_indented_list().
|
static |
internal function for sorting list of macros
Referenced by list_macros().
void load_macro_file | ( | char * | filename | ) |
References cl_free, cl_realloc(), cl_strdup(), cqpmessage(), define_macro(), Error, input_line, line, load_macro_file(), MACRO_FILE_MAX_LINE_LENGTH, parse_macro_name(), and preprocess_input_line().
Referenced by load_macro_file().
void macro_debug_newline_indent | ( | void | ) |
indent macro expansion debugging output (according to # of active input buffers)
References buf, InputBufferList, and _InputBuffer::next.
Referenced by expand_macro(), and yy_input_char().
void macro_iterator_new | ( | void | ) |
Resets the module-global iterator_bucket and iterator_entry variables.
References iterator_bucket.
char* macro_iterator_next | ( | char * | prefix, |
int * | nargs | ||
) |
References _MacroEntry::args, macro_iterator_next_macro(), and _MacroEntry::name.
MacroEntry macro_iterator_next_macro | ( | char * | prefix | ) |
internal iterator function used by macro_iterator_next() and macro_iterator_next_prototype()
References enable_macros, _MacroHashTable::hash, iterator_bucket, iterator_entry, _MacroEntry::name, _MacroEntry::next, and _MacroHashTable::size.
Referenced by macro_iterator_next(), and macro_iterator_next_prototype().
char* macro_iterator_next_prototype | ( | char * | prefix | ) |
References _MacroEntry::argnames, _MacroEntry::args, cl_malloc(), macro_iterator_next_macro(), and _MacroEntry::name.
void macro_statistics | ( | void | ) |
Prints macro hash statistics on stderr.
References _MacroHashTable::hash, _MacroEntry::next, and _MacroHashTable::size.
Referenced by main().
MacroSegment MacroAddSegment | ( | MacroEntry | macro | ) |
append segment to macro replacement string, returns pointer to the new, initialised segment
References _MacroSegment::arg, cl_malloc(), _MacroSegment::next, _MacroEntry::replacement, and _MacroSegment::string.
Referenced by define_macro().
MacroEntry MacroHashAdd | ( | char * | str, |
int | args | ||
) |
References _MacroEntry::active, _MacroEntry::argnames, _MacroEntry::args, cl_malloc(), cl_strdup(), cqpmessage(), Error, _MacroHashTable::hash, hash_macro(), _MacroEntry::name, _MacroEntry::next, _MacroEntry::replacement, and _MacroHashTable::size.
Referenced by define_macro().
void MacroHashDelete | ( | MacroEntry | macro | ) |
MacroEntry MacroHashLookup | ( | char * | str, |
int | args | ||
) |
References _MacroEntry::args, cqpmessage(), Error, _MacroHashTable::hash, hash_macro(), _MacroEntry::name, _MacroEntry::next, and _MacroHashTable::size.
Referenced by define_macro(), expand_macro(), and print_macro_definition().
void MakeMacroHash | ( | int | size | ) |
has to be called once to initialise the hash
References bytes, cl_malloc(), find_prime(), _MacroHashTable::hash, and _MacroHashTable::size.
Referenced by init_macros().
char* parse_macro_name | ( | char * | text, |
int * | nr_of_args, | ||
char ** | prototype | ||
) |
References cl_malloc().
Referenced by load_macro_file().
void PopInputBuffer | ( | void | ) |
delete input buffer from top of list
References _MacroEntry::active, _InputBuffer::data, InputBufferList, _InputBuffer::macro, and _InputBuffer::next.
Referenced by delete_macro_buffers(), and yy_input_char().
char* preprocess_input_line | ( | char * | line | ) |
internal function for load_macro_file(): remove whitespace & comments from input line
References line.
Referenced by load_macro_file().
void print_macro_definition | ( | char * | name, |
int | args | ||
) |
print definition of macro on stdout
References _MacroSegment::arg, _MacroEntry::argnames, cqpmessage(), enable_macros, Error, MacroHashLookup(), _MacroSegment::next, _MacroEntry::replacement, and _MacroSegment::string.
InputBuffer PushInputBuffer | ( | int | size | ) |
push new input buffer (which can hold string of lenght <size>) on top of buffer list
References cl_malloc(), _InputBuffer::data, InputBufferList, _InputBuffer::macro, _InputBuffer::next, and _InputBuffer::position.
Referenced by expand_macro().
int yy_input_char | ( | void | ) |
Get a character for the lexer/parser.
This function reads one character (byte) of input from InputBufferList[top], ... , InputBufferList[bottom], cqp_input_string, yyin in that order of precedence.
It adds the character to query buffer, and returns it.
Used by the lexer to get its characters; see parser.l where YY_INPUT() is redefined to a macro calling this function. (Not clear why it's in macro.c when it is not specific to just macros.)
References buf, cqp_input_string, cqp_input_string_position, cqpmessage(), _InputBuffer::data, InputBufferList, macro_debug, macro_debug_newline_indent(), PopInputBuffer(), _InputBuffer::position, QUERY_BUFFER_SIZE, QueryBuffer, QueryBufferOverflow, QueryBufferP, reading_cqprc, Warning, write_history_file, yy_input_char(), and yyin.
Referenced by yy_input_char().
int yy_input_from_macro | ( | void | ) |
checks if input is being read from macro expansion
int yylex | ( | void | ) |
Referenced by expand_macro().
InputBuffer InputBufferList = NULL |
list of active input buffers
Referenced by macro_debug_newline_indent(), PopInputBuffer(), PushInputBuffer(), and yy_input_char().
int iterator_bucket = -1 |
current bucket in macro iterator
Referenced by macro_iterator_new(), and macro_iterator_next_macro().
MacroEntry iterator_entry = NULL |
current entry in bucket (if NULL, go to next bucket)
Referenced by macro_iterator_next_macro().
char* macro_arg[10] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL} |
macro args are stored in a global array so we can always free() the memory
Referenced by expand_macro().
MacroHashTable MacroHash = NULL |
unsigned int pseudo_arg_counter = 0 |
unique number for pseudo argument in each macro call
Referenced by expand_macro().
FILE* yyin |
Referenced by yy_input_char().
char* yytext |
Referenced by expand_macro().