Skip to content

Commit

Permalink
[builtin/type] Don't lie about 'break' etc. being a builtin.
Browse files Browse the repository at this point in the history
Control flow are keywords in Oil.  Addresses issue oils-for-unix#637.
  • Loading branch information
Andy Chu authored and abathur committed Mar 10, 2020
1 parent 97e1843 commit dde9d2d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
11 changes: 6 additions & 5 deletions frontend/lexer_def.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ def R(pat, tok_type):

# These are treated like builtins in bash, but keywords in OSH. However, we
# maintain compatibility with bash for the 'type' builtin.
_MORE_KEYWORDS = [
_CONTROL_FLOW = [
C('break', Id.ControlFlow_Break),
C('continue', Id.ControlFlow_Continue),
C('return', Id.ControlFlow_Return),
Expand Down Expand Up @@ -246,12 +246,13 @@ def R(pat, tok_type):
# The 'compen' and 'type' builtins introspect on keywords and builtins.
OSH_KEYWORD_NAMES = [name for _, name, _ in _KEYWORDS]
OSH_KEYWORD_NAMES.append('{') # not in our lexer list
OTHER_OSH_BUILTINS = [name for _, name, _ in _MORE_KEYWORDS]


def IsOtherBuiltin(name):
_CF_NAMES = [name for _, name, _ in _CONTROL_FLOW]

def IsControlFlow(name):
# type: (str) -> bool
return name in OTHER_OSH_BUILTINS
return name in _CF_NAMES


def IsKeyword(name):
Expand Down Expand Up @@ -306,7 +307,7 @@ def IsKeyword(name):
C(r'&>', Id.Redir_AndGreat),
C(r'&>>', Id.Redir_AndDGreat),

] + _KEYWORDS + _MORE_KEYWORDS + _UNQUOTED + _EXTGLOB_BEGIN
] + _KEYWORDS + _CONTROL_FLOW + _UNQUOTED + _EXTGLOB_BEGIN

# Preprocessing before Outer
LEXER_DEF[lex_mode_e.Backtick] = [
Expand Down
4 changes: 2 additions & 2 deletions osh/builtin_pure.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,8 @@ def _ResolveNames(names, funcs, aliases, search_path):
kind = ('builtin', name)
elif consts.LookupAssignBuiltin(name) != 0:
kind = ('builtin', name)
elif lexer_def.IsOtherBuiltin(name): # continue, etc.
kind = ('builtin', name)
elif lexer_def.IsControlFlow(name): # continue, etc.
kind = ('keyword', name)

elif lexer_def.IsKeyword(name):
kind = ('keyword', name)
Expand Down
22 changes: 19 additions & 3 deletions spec/builtin-bash.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,14 @@ type -t foo
## stdout: alias

#### type -t -> builtin
type -t echo read : [ declare local break continue
type -t echo read : [ declare local
## STDOUT:
builtin
builtin
builtin
builtin
builtin
builtin
builtin
builtin
## END

#### type -t -> keyword
Expand All @@ -50,6 +48,24 @@ keyword
keyword
## END

#### type -t control flow

# this differs from bash, but don't lie!
type -t break continue return exit
## STDOUT:
keyword
keyword
keyword
keyword
## END
## OK bash STDOUT:
builtin
builtin
builtin
builtin
## END


#### type -t -> file
type -t find xargs
## STDOUT:
Expand Down

0 comments on commit dde9d2d

Please sign in to comment.