Skip to content

Commit

Permalink
Move keyword detection code to separate file
Browse files Browse the repository at this point in the history
darcs-hash:20070422095026-ac50b-77a840e2830370f46b7a48fd8863095d2cd7a5f0.gz
  • Loading branch information
liljencrantz committed Apr 22, 2007
1 parent e9790db commit 45412f2
Show file tree
Hide file tree
Showing 12 changed files with 162 additions and 121 deletions.
3 changes: 2 additions & 1 deletion Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ FISH_OBJS := function.o builtin.o complete.o env.o exec.o expand.o \
highlight.o history.o kill.o parser.o proc.o reader.o sanity.o \
tokenizer.o wildcard.o wgetopt.o wutil.o input.o output.o intern.o \
env_universal.o env_universal_common.o input_common.o event.o \
signal.o io.o parse_util.o common.o screen.o path.o
signal.o io.o parse_util.o common.o screen.o path.o \
parser_keywords.o


#
Expand Down
4 changes: 2 additions & 2 deletions builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@
#include "signal.h"
#include "exec.h"
#include "highlight.h"

#include "halloc.h"
#include "halloc_util.h"
#include "parse_util.h"
#include "parser_keywords.h"
#include "expand.h"
#include "path.h"

Expand Down Expand Up @@ -1335,7 +1335,7 @@ static int builtin_function( wchar_t **argv )

res=1;
}
else if( parser_is_reserved(argv[woptind] ) )
else if( parser_keywords_is_reserved(argv[woptind] ) )
{

sb_printf( sb_err,
Expand Down
4 changes: 2 additions & 2 deletions complete.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ These functions are used for storing and retrieving tab-completion data, as well
#include "reader.h"
#include "history.h"
#include "intern.h"

#include "parse_util.h"
#include "parser_keywords.h"
#include "halloc.h"
#include "halloc_util.h"
#include "wutil.h"
Expand Down Expand Up @@ -1890,7 +1890,7 @@ void complete( const wchar_t *cmd,
if( !had_cmd )
{

if( parser_is_subcommand( ncmd ) )
if( parser_keywords_is_subcommand( ncmd ) )
{
if( wcscmp( ncmd, L"builtin" )==0)
{
Expand Down
3 changes: 2 additions & 1 deletion function.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "event.h"
#include "reader.h"
#include "parse_util.h"
#include "parser_keywords.h"
#include "env.h"
#include "expand.h"
#include "halloc.h"
Expand Down Expand Up @@ -213,7 +214,7 @@ int function_exists( const wchar_t *cmd )

CHECK( cmd, 0 );

if( parser_is_reserved(cmd) )
if( parser_keywords_is_reserved(cmd) )
return 0;

load( cmd );
Expand Down
7 changes: 4 additions & 3 deletions highlight.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "proc.h"
#include "parser.h"
#include "parse_util.h"
#include "parser_keywords.h"
#include "builtin.h"
#include "function.h"
#include "env.h"
Expand Down Expand Up @@ -642,7 +643,7 @@ void highlight_shell( wchar_t * buff,
int mark = tok_get_pos( &tok );
color[ tok_get_pos( &tok ) ] = HIGHLIGHT_COMMAND;

if( parser_is_subcommand( cmd ) )
if( parser_keywords_is_subcommand( cmd ) )
{

int sw;
Expand All @@ -662,9 +663,9 @@ void highlight_shell( wchar_t * buff,

tok_next( &tok );

sw = parser_is_switch( tok_last( &tok ) );
sw = parser_keywords_is_switch( tok_last( &tok ) );

if( !parser_is_block( cmd ) &&
if( !parser_keywords_is_block( cmd ) &&
sw == ARG_SWITCH )
{
/*
Expand Down
2 changes: 2 additions & 0 deletions parse_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,6 @@ void parse_util_set_argv( wchar_t **argv, array_list_t *named_arguments );
*/
wchar_t *parse_util_unescape_wildcards( const wchar_t *in );



#endif
76 changes: 8 additions & 68 deletions parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ The fish parser. Contains functions for parsing and evaluating code.
#include "wutil.h"
#include "proc.h"
#include "parser.h"
#include "parser_keywords.h"
#include "tokenizer.h"
#include "exec.h"
#include "wildcard.h"
Expand Down Expand Up @@ -512,67 +513,6 @@ const wchar_t *parser_get_block_desc( int block )
return _(UNKNOWN_BLOCK);
}

/**
Check if the specified bcommand is one of the builtins that cannot
have arguments, any followin argument is interpreted as a new
command
*/
static int parser_skip_arguments( const wchar_t *cmd )
{
return CONTAINS( cmd,
L"else",
L"begin" );
}

int parser_is_switch( const wchar_t *cmd )
{
if( wcscmp( cmd, L"--" ) == 0 )
return ARG_SKIP;
else
return cmd[0] == L'-';
}


int parser_is_subcommand( const wchar_t *cmd )
{

return parser_skip_arguments( cmd ) ||
CONTAINS( cmd,
L"command",
L"builtin",
L"while",
L"exec",
L"if",
L"and",
L"or",
L"not" );

}

int parser_is_block( const wchar_t *word)
{
return CONTAINS( word,
L"for",
L"while",
L"if",
L"function",
L"switch",
L"begin" );
}

int parser_is_reserved( const wchar_t *word)
{
return parser_is_block(word) ||
parser_is_subcommand( word ) ||
CONTAINS( word,
L"end",
L"case",
L"else",
L"return",
L"continue",
L"break" );
}

/**
Returns 1 if the specified command is a builtin that may not be used in a pipeline
*/
Expand Down Expand Up @@ -614,7 +554,7 @@ static const wchar_t *parser_find_end( const wchar_t * buff )
{
count--;
}
else if( parser_is_block( tok_last(&tok) ) )
else if( parser_keywords_is_block( tok_last(&tok) ) )
{
count++;
}
Expand Down Expand Up @@ -1844,7 +1784,7 @@ static int parse_job( process_t *p,
}

tok_next( tok );
sw = parser_is_switch( tok_last( tok ) );
sw = parser_keywords_is_switch( tok_last( tok ) );

if( sw == ARG_SWITCH )
{
Expand Down Expand Up @@ -2007,7 +1947,7 @@ static int parse_job( process_t *p,
builtin_exists( (wchar_t *)al_get( args, 0 ) ) )
{
p->type = INTERNAL_BUILTIN;
is_new_block |= parser_is_block( (wchar_t *)al_get( args, 0 ) );
is_new_block |= parser_keywords_is_block( (wchar_t *)al_get( args, 0 ) );
}
}

Expand Down Expand Up @@ -2236,7 +2176,7 @@ static int parse_job( process_t *p,

if( !error_code )
{
if( p->type == INTERNAL_BUILTIN && parser_skip_arguments( (wchar_t *)al_get(args, 0) ) )
if( p->type == INTERNAL_BUILTIN && parser_keywords_skip_arguments( (wchar_t *)al_get(args, 0) ) )
{
if( !p->argv )
halloc_register( j, p->argv = list_to_char_arr( args ) );
Expand Down Expand Up @@ -3045,7 +2985,7 @@ int parser_test( const wchar_t * buff,
/*
Handle block commands
*/
if( parser_is_block( cmd ) )
if( parser_keywords_is_block( cmd ) )
{
if( count >= BLOCK_MAX_COUNT )
{
Expand All @@ -3066,12 +3006,12 @@ int parser_test( const wchar_t * buff,
}

/*
If parser_is_subcommand is true, the command
If parser_keywords_is_subcommand is true, the command
accepts a second command as it's first
argument. If parser_skip_arguments is true, the
second argument is optional.
*/
if( parser_is_subcommand( cmd ) && !parser_skip_arguments(cmd ) )
if( parser_keywords_is_subcommand( cmd ) && !parser_keywords_skip_arguments(cmd ) )
{
needs_cmd = 1;
had_cmd = 0;
Expand Down
44 changes: 0 additions & 44 deletions parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,6 @@
#define PARSER_TEST_ERROR 1
#define PARSER_TEST_INCOMPLETE 2

/**
REturn valuse for parser_is_switch()
*/
enum
{
ARG_NON_SWITCH,
ARG_SWITCH,
ARG_SKIP
}
;

/**
event_block_t represents a block on events of the specified type
*/
Expand Down Expand Up @@ -232,39 +221,6 @@ int eval_args( const wchar_t *line,
void error( int ec, int p, const wchar_t *str, ... );


/**
Check if the specified argument is a switch. Return ARG_SWITCH if yes,
ARG_NON_SWITCH if no and ARG_SKIP if the argument is '--'
*/
int parser_is_switch( const wchar_t *cmd );


/**
Tests if the specified commands parameters should be interpreted as another command, which will be true if the command is either 'command', 'exec', 'if', 'while' or 'builtin'.
\param cmd The command name to test
\return 1 of the command parameter is a command, 0 otherwise
*/

int parser_is_subcommand( const wchar_t *cmd );

/**
Tests if the specified command is a reserved word, i.e. if it is
the name of one of the builtin functions that change the block or
command scope, like 'for', 'end' or 'command' or 'exec'. These
functions may not be overloaded, so their names are reserved.
\param word The command name to test
\return 1 of the command parameter is a command, 0 otherwise
*/
int parser_is_reserved( const wchar_t *word );

/**
Test if the specified string is command that opens a new block
*/

int parser_is_block( const wchar_t *word);

/**
Returns a string describing the current parser pisition in the format 'FILENAME (line LINE_NUMBER): LINE'.
Example:
Expand Down
75 changes: 75 additions & 0 deletions parser_keywords.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/** \file parser_keywords.c
*/

#include "config.h"


#include <stdlib.h>
#include <stdio.h>

#include "fallback.h"
//#include "util.h"

//#include "wutil.h"
#include "common.h"
#include "parser_keywords.h"


int parser_keywords_is_switch( const wchar_t *cmd )
{
if( wcscmp( cmd, L"--" ) == 0 )
return ARG_SKIP;
else
return cmd[0] == L'-';
}

int parser_keywords_skip_arguments( const wchar_t *cmd )
{
return CONTAINS( cmd,
L"else",
L"begin" );
}


int parser_keywords_is_subcommand( const wchar_t *cmd )
{

return parser_keywords_skip_arguments( cmd ) ||
CONTAINS( cmd,
L"command",
L"builtin",
L"while",
L"exec",
L"if",
L"and",
L"or",
L"not" );

}

int parser_keywords_is_block( const wchar_t *word)
{
return CONTAINS( word,
L"for",
L"while",
L"if",
L"function",
L"switch",
L"begin" );
}

int parser_keywords_is_reserved( const wchar_t *word)
{
return parser_keywords_is_block(word) ||
parser_keywords_is_subcommand( word ) ||
CONTAINS( word,
L"end",
L"case",
L"else",
L"return",
L"continue",
L"break" );
}

Loading

0 comments on commit 45412f2

Please sign in to comment.