mirror of
https://github.com/fish-shell/fish-shell
synced 2024-12-26 04:43:10 +00:00
Move keyword detection code to separate file
darcs-hash:20070422095026-ac50b-77a840e2830370f46b7a48fd8863095d2cd7a5f0.gz
This commit is contained in:
parent
e9790db64a
commit
45412f2b1f
12 changed files with 162 additions and 121 deletions
|
@ -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
|
||||
|
||||
|
||||
#
|
||||
|
|
|
@ -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"
|
||||
|
||||
|
@ -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,
|
||||
|
|
|
@ -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"
|
||||
|
@ -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)
|
||||
{
|
||||
|
|
|
@ -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"
|
||||
|
@ -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 );
|
||||
|
|
|
@ -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"
|
||||
|
@ -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;
|
||||
|
@ -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 )
|
||||
{
|
||||
/*
|
||||
|
|
|
@ -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
parser.c
76
parser.c
|
@ -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"
|
||||
|
@ -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
|
||||
*/
|
||||
|
@ -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++;
|
||||
}
|
||||
|
@ -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 )
|
||||
{
|
||||
|
@ -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 ) );
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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 ) );
|
||||
|
@ -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 )
|
||||
{
|
||||
|
@ -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;
|
||||
|
|
44
parser.h
44
parser.h
|
@ -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
|
||||
*/
|
||||
|
@ -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:
|
||||
|
|
75
parser_keywords.c
Normal file
75
parser_keywords.c
Normal file
|
@ -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" );
|
||||
}
|
||||
|
63
parser_keywords.h
Normal file
63
parser_keywords.h
Normal file
|
@ -0,0 +1,63 @@
|
|||
/** \file parser_keywords.h
|
||||
|
||||
Functions having to do with parser keywords, like testing if a function is a block command.
|
||||
*/
|
||||
|
||||
#ifndef FISH_PARSER_KEYWORD_H
|
||||
#define FISH_PARSER_KEYWORD_H
|
||||
|
||||
/**
|
||||
Return valuse for parser_keywords_is_switch()
|
||||
*/
|
||||
enum
|
||||
{
|
||||
ARG_NON_SWITCH,
|
||||
ARG_SWITCH,
|
||||
ARG_SKIP
|
||||
}
|
||||
;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
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_keywords_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_keywords_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_keywords_is_reserved( const wchar_t *word );
|
||||
|
||||
/**
|
||||
Test if the specified string is command that opens a new block
|
||||
*/
|
||||
|
||||
int parser_keywords_is_block( const wchar_t *word);
|
||||
|
||||
/**
|
||||
Check if the specified command is one of the builtins that cannot
|
||||
have arguments, any followin argument is interpreted as a new
|
||||
command
|
||||
*/
|
||||
int parser_keywords_skip_arguments( const wchar_t *cmd );
|
||||
|
||||
|
||||
#endif
|
1
path.c
1
path.c
|
@ -66,6 +66,7 @@ wchar_t *path_get_path( void *context, const wchar_t *cmd )
|
|||
Allocate string long enough to hold the whole command
|
||||
*/
|
||||
wchar_t *new_cmd = halloc( context, sizeof(wchar_t)*(wcslen(cmd)+wcslen(path)+2) );
|
||||
|
||||
/*
|
||||
We tokenize a copy of the path, since strtok modifies
|
||||
its arguments
|
||||
|
|
|
@ -1185,6 +1185,7 @@ int wildcard_expand( const wchar_t *wc,
|
|||
int c = al_get_count( out );
|
||||
int res = wildcard_expand_internal( wc, base_dir, flags, out );
|
||||
int i;
|
||||
|
||||
|
||||
if( flags & ACCEPT_INCOMPLETE )
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue