CWB
Data Structures | Macros | Typedefs | Functions | Variables
macro.c File Reference
#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 _InputBufferInputBuffer
 
typedef struct _MacroSegmentMacroSegment
 the macro replacement string is stored as a sequence of segments: More...
 
typedef struct _MacroEntryMacroEntry
 an entry in the macro database More...
 
typedef struct _MacroHashTableMacroHashTable
 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...
 

Macro Definition Documentation

#define MACRO_HASH_BUCKETS   8000

Referenced by init_macros().

Typedef Documentation

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

Function Documentation

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)
int expand_macro ( char *  name)
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)
static int list_macros_sort ( const void *  p1,
const void *  p2 
)
static

internal function for sorting list of macros

Referenced by list_macros().

void load_macro_file ( char *  filename)
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 
)
MacroEntry macro_iterator_next_macro ( char *  prefix)
char* macro_iterator_next_prototype ( char *  prefix)
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 
)
void MacroHashDelete ( MacroEntry  macro)
MacroEntry MacroHashLookup ( char *  str,
int  args 
)
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 
)
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().

Variable Documentation

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)

See also
iterator_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().