parser.h File Reference

The fish parser. More...

#include <wchar.h>
#include "proc.h"
#include "util.h"
#include "parser.h"
#include "event.h"

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  event_block
 event_block_t represents a block on events of the specified type More...
struct  block
 block_t represents a block of commands. More...

Defines

#define PARSER_TEST_ERROR   1
#define PARSER_TEST_INCOMPLETE   2

Typedefs

typedef struct event_block event_block_t
 event_block_t represents a block on events of the specified type
typedef struct block block_t
 block_t represents a block of commands.

Enumerations

enum  block_type {
  WHILE, FOR, IF, FUNCTION_DEF,
  FUNCTION_CALL, FUNCTION_CALL_NO_SHADOW, SWITCH, FAKE,
  SUBST, TOP, BEGIN, SOURCE,
  EVENT, BREAKPOINT
}
 Types of blocks. More...
enum  loop_status { LOOP_NORMAL, LOOP_BREAK, LOOP_CONTINUE }
 Possible states for a loop. More...
enum  while_status { WHILE_TEST_FIRST, WHILE_TEST_AGAIN, WHILE_TESTED }
 Possible states for a while block. More...
enum  parser_error { NO_ERR = 0, SYNTAX_ERROR, EVAL_ERROR, CMDSUBST_ERROR }
 Errors that can be generated by the parser. More...

Functions

int eval (const wchar_t *cmd, io_data_t *io, int block_type)
 Evaluate the expressions contained in cmd.
int eval_args (const wchar_t *line, array_list_t *output)
 Evaluate line as a list of parameters, i.e.
void error (int ec, int p, const wchar_t *str,...)
 Sets the current evaluation error.
wchar_t * parser_current_line ()
 Returns a string describing the current parser pisition in the format 'FILENAME (line LINE_NUMBER): LINE'.
int parser_get_lineno ()
 Returns the current line number.
int parser_get_pos ()
 Returns the current position in the latest string of the tokenizer.
int parser_get_job_pos ()
 Returns the position where the current job started in the latest string of the tokenizer.
void parser_set_pos (int p)
 Set the current position in the latest string of the tokenizer.
const wchar_t * parser_get_buffer ()
 Get the string currently parsed.
void parser_push_block (int type)
 Create block of specified type.
void parser_pop_block ()
 Remove the outermost block namespace.
const wchar_t * parser_get_block_desc (int block)
 Return a description of the given blocktype.
int parser_test (const wchar_t *buff, int *block_level, string_buffer_t *out, const wchar_t *prefix)
 Test if the specified string can be parsed, or if more bytes need to be read first.
int parser_test_args (const wchar_t *buff, string_buffer_t *out, const wchar_t *prefix)
 Test if the specified string can be parsed as an argument list, e.g.
void parser_forbid_function (wchar_t *function)
 Tell the parser that the specified function may not be run if not inside of a conditional block.
void parser_allow_function ()
 Undo last call to parser_forbid_function().
void parser_init ()
 Initialize static parser data.
void parser_destroy ()
 Destroy static parser data.
int parser_is_help (wchar_t *s, int min_match)
 This function checks if the specified string is a help option.
const wchar_t * parser_current_filename ()
 Returns the file currently evaluated by the parser.
void parser_stack_trace (block_t *b, string_buffer_t *buff)
 Write a stack trace starting at the specified block to the specified string_buffer_t.
int parser_get_block_type (const wchar_t *cmd)
const wchar_t * parser_get_block_command (int type)

Variables

block_tcurrent_block
 The current innermost block.
event_block_tglobal_event_block
 Global event blocks.
io_data_tblock_io
 Current block level io redirections.


Detailed Description

The fish parser.


Enumeration Type Documentation

enum block_type

Types of blocks.

Enumerator:
WHILE  While loop block.
FOR  For loop block.
IF  If block.
FUNCTION_DEF  Function definition block.
FUNCTION_CALL  Function invocation block.
FUNCTION_CALL_NO_SHADOW  Function invocation block with no variable shadowing.
SWITCH  Switch block.
FAKE  Fake block.
SUBST  Command substitution scope.
TOP  Outermost block.
BEGIN  Unconditional block.
SOURCE  Block created by the .

(source) builtin

EVENT  Block created on event notifier invocation.
BREAKPOINT  Breakpoint block.

Possible states for a loop.

Enumerator:
LOOP_NORMAL  Current loop block executed as normal.
LOOP_BREAK  Current loop block should be removed.
LOOP_CONTINUE  Current loop block should be skipped.

Errors that can be generated by the parser.

Enumerator:
NO_ERR  No error.
SYNTAX_ERROR  An error in the syntax.
EVAL_ERROR  Error occured while evaluating commands.
CMDSUBST_ERROR  Error while evaluating cmdsubst.

Possible states for a while block.

Enumerator:
WHILE_TEST_FIRST  This is the first command of the first lap of a while loop.
WHILE_TEST_AGAIN  This is not the first lap of the while loop, but it is the first command of the loop.
WHILE_TESTED  This is not the first command in the loop.


Function Documentation

void error ( int  ec,
int  p,
const wchar_t *  str,
  ... 
)

Sets the current evaluation error.

This function should only be used by libraries that are called by

Parameters:
ec The new error code
p The character offset at which the error occured
str The printf-style error message filter

References CHECK, err_pos, error_code, global_context, sb_clear(), sb_halloc(), and sb_vprintf().

int eval ( const wchar_t *  cmd,
io_data_t io,
int  block_type 
)

int eval_args ( const wchar_t *  line,
array_list_t output 
)

Evaluate line as a list of parameters, i.e.

tokenize it and perform parameter expansion and cmdsubst execution on the tokens. The output is inserted into output, and should be freed by the caller.

Parameters:
line Line to evaluate
output List to insert output to

References CHECK, current_tokenizer_pos, DIE_MEM, err_pos, error, error_code, EXPAND_ERROR, expand_string(), print_errors_stderr(), proc_pop_interactive(), proc_push_interactive(), SYNTAX_ERROR, tmp, tok_destroy(), TOK_END, TOK_ERR_MSG, TOK_ERROR, tok_get_desc(), tok_get_pos(), tok_has_next(), tok_init(), tok_last(), tok_last_type(), tok_next(), TOK_STRING, and UNEXPECTED_TOKEN_ERR_MSG.

Referenced by complete_from_args().

const wchar_t* parser_current_filename (  ) 

Returns the file currently evaluated by the parser.

This can be different than reader_current_filename, e.g. if we are evaulating a function defined in a different file than the one curently read.

References FUNCTION_CALL, block::function_call_name, function_get_definition_file(), block::outer, block::param1, reader_current_filename(), and block::type.

Referenced by builtin_status(), parser_current_line(), and parser_push_block().

wchar_t* parser_current_line (  ) 

Returns a string describing the current parser pisition in the format 'FILENAME (line LINE_NUMBER): LINE'.

Example:

init.fish (line 127): ls|grep pancake

If we are not going to print a stack trace, at least print the line number and filename

References _, buffer::buff, current_tokenizer_pos, function_get_definition_offset(), is_function(), is_interactive, my_wcswidth(), parser_current_filename(), parser_stack_trace(), printed_width(), sb_clear(), sb_init(), sb_printf(), and tok_string().

Referenced by builtin_cd(), builtin_print_help(), eval(), parse_job(), parse_job_argument_list(), print_errors(), and print_errors_stderr().

void parser_forbid_function ( wchar_t *  function  ) 

Tell the parser that the specified function may not be run if not inside of a conditional block.

This is to remove some possibilities of infinite recursion.

References al_push(), and CHECK.

Referenced by exec().

const wchar_t* parser_get_block_command ( int  type  ) 

Returns:
the block command that createa the specified block type, or null on error.

References block_lookup_entry::desc, and block_lookup_entry::name.

Referenced by parser_test().

int parser_get_block_type ( const wchar_t *  cmd  ) 

Returns:
the block type created by the specified builtin, or -1 on error.

References block_lookup_entry::desc, block_lookup_entry::name, and block_lookup_entry::type.

Referenced by parser_test().

int parser_is_help ( wchar_t *  s,
int  min_match 
)

This function checks if the specified string is a help option.

Parameters:
s the string to test
min_match is the minimum number of characters that must match in a long style option, i.e. the longest common prefix between --help and any other option. If less than 3, 3 will be assumed.

References CHECK, and maxi().

Referenced by builtin_run(), parse_job_argument_list(), and parser_test().

int parser_test ( const wchar_t *  buff,
int *  block_level,
string_buffer_t out,
const wchar_t *  prefix 
)

Test if the specified string can be parsed, or if more bytes need to be read first.

The result will have the PARSER_TEST_ERROR bit set if there is a syntax error in the code, and the PARSER_TEST_INCOMPLETE bit set if the code contains unclosed blocks.

Parameters:
buff the text buffer to test
block_level if non-null, the block nesting level will be filled out into this array
out if non-null, any errors in the command will be filled out into this buffer
prefix the prefix string to prepend to each error message written to the out buffer

References BLOCK_END_ERR_MSG, BLOCK_ERR_MSG, BLOCK_MAX_COUNT, BUILTIN_FOR_ERR_COUNT, BUILTIN_FOR_ERR_IN, builtin_help_get(), CHECK, CMD_AND_ERR_MSG, CMD_ERR_MSG, CMD_OR_ERR_MSG, COND_ERR_MSG, contains, current_tokenizer_pos, err(), error, error_code, EXEC_ERR_MSG, expand_is_clean(), expand_one(), EXPAND_SKIP_CMDSUBST, EXPAND_SKIP_VARIABLES, FOR, FUNCTION_DEF, halloc(), halloc_free(), IF, ILLEGAL_CMD_ERR_MSG, INVALID_CASE_ERR_MSG, INVALID_ELSE_ERR_MSG, INVALID_END_ERR_MSG, INVALID_LOOP_ERR_MSG, INVALID_REDIRECTION_ERR_MSG, INVALID_RETURN_ERR_MSG, parser_get_block_command(), parser_get_block_type(), parser_is_help(), parser_is_pipe_forbidden(), parser_keywords_is_block(), parser_keywords_is_subcommand(), parser_keywords_skip_arguments(), parser_test_argument(), print_errors(), sb_printf(), SWITCH, SYNTAX_ERROR, TOK_BACKGROUND, tok_destroy(), TOK_END, TOK_ERR_MSG, TOK_ERROR, tok_get_desc(), tok_get_error(), tok_get_pos(), tok_has_next(), tok_init(), tok_last(), tok_last_type(), tok_next(), TOK_PIPE, TOK_REDIRECT_APPEND, TOK_REDIRECT_FD, TOK_REDIRECT_IN, TOK_REDIRECT_NOCLOB, TOK_REDIRECT_OUT, tok_set_pos(), TOK_STRING, wcsvarname(), and WHILE.

Referenced by builtin_complete(), parser_test_argument(), read_ni(), reader_repaint(), reader_shell_test(), and test_parser().

int parser_test_args ( const wchar_t *  buff,
string_buffer_t out,
const wchar_t *  prefix 
)

Test if the specified string can be parsed as an argument list, e.g.

sent to eval_args. The result has the first bit set if the string contains errors, and the second bit is set if the string contains an unclosed block.

References CHECK, current_tokenizer_pos, err(), error, error_code, parser_test_argument(), print_errors(), SYNTAX_ERROR, tok_destroy(), TOK_END, TOK_ERR_MSG, TOK_ERROR, tok_get_desc(), tok_get_pos(), tok_has_next(), tok_init(), tok_last(), tok_last_type(), tok_next(), TOK_STRING, and UNEXPECTED_TOKEN_ERR_MSG.

Referenced by builtin_complete().


Generated on Sun Mar 8 15:46:55 2009 for fish by  doxygen 1.5.6