Skip to content

Commit

Permalink
Starting to lex array assignment like 'words[0]=1'.
Browse files Browse the repository at this point in the history
Also moved a few tokens out of _UNQUOTED and into OUTER.

- cmd_parse.py: Clean up logic for looking ahead to a function def
- Audit of git-completion.bash language usage
  • Loading branch information
Andy Chu committed Sep 15, 2018
1 parent 04e2cf9 commit 61bc9db
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 14 deletions.
3 changes: 2 additions & 1 deletion core/id_kind.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ def AddKinds(spec):
spec.AddKind('WS', ['Space'])

spec.AddKind('Lit', [
'Chars', 'VarLike', 'Other', 'EscapedChar',
'Chars', 'VarLike', 'ArrayLhsOpen', 'ArrayLhsClose',
'Other', 'EscapedChar',
# Either brace expansion or keyword for { and }
'LBrace', 'RBrace', 'Comma',
'DRightBracket', # the ]] that matches [[, NOT a keyword
Expand Down
17 changes: 9 additions & 8 deletions osh/cmd_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -1333,11 +1333,13 @@ def ParseCommand(self, cur_aliases=None):
self._Peek()

if self.c_id in NOT_FIRST_WORDS:
p_die('Unexpected word when parsing cmomand', word=self.cur_word)
p_die('Unexpected word when parsing command', word=self.cur_word)

if self.c_id == Id.KW_Function:
return self.ParseKshFunctionDef()

# NOTE: We should have another Kind for "initial keywords". And then
# NOT_FIRST_WORDS are "secondary keywords".
if self.c_id in (
Id.KW_DLeftBracket, Id.Op_DLeftParen, Id.Op_LParen, Id.Lit_LBrace,
Id.KW_For, Id.KW_While, Id.KW_Until, Id.KW_If, Id.KW_Case, Id.KW_Time):
Expand All @@ -1359,14 +1361,13 @@ def ParseCommand(self, cur_aliases=None):
return self.ParseSimpleCommand(cur_aliases)

if self.c_kind == Kind.Word:
if self.w_parser.LookAhead() == Id.Op_LParen: # (
kov = word.LooksLikeAssignment(self.cur_word)
if kov:
return self.ParseSimpleCommand(cur_aliases) # f=(a b c) # array
else:
if (self.w_parser.LookAhead() == Id.Op_LParen and
not word.LooksLikeAssignment(self.cur_word)):
return self.ParseFunctionDef() # f() { echo; } # function

return self.ParseSimpleCommand(cur_aliases) # echo foo
# echo foo
# f=(a b c) # array
# array[1+2]+=1
return self.ParseSimpleCommand(cur_aliases)

if self.c_kind == Kind.Eof:
p_die("Unexpected EOF while parsing command", word=self.cur_word)
Expand Down
13 changes: 9 additions & 4 deletions osh/lex.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,6 @@
# NOTE: We could add anything 128 and above to this character class? So
# utf-8 characters don't get split?
R(r'[a-zA-Z0-9_/.-]+', Id.Lit_Chars),
# e.g. beginning of NAME=val, which will always be longer than the above
# Id.Lit_Chars.
R(r'[a-zA-Z_][a-zA-Z0-9_]*\+?=', Id.Lit_VarLike),

# For tilde expansion. The list of chars is Lit_Chars, but WITHOUT the /. We
# want the next token after the tilde TildeLike token start with a /.
Expand Down Expand Up @@ -304,7 +301,15 @@ def IsKeyword(name):
# Keywords have to be checked before _UNQUOTED so we get <KW_If "if"> instead
# of <Lit_Chars "if">.
LEXER_DEF[lex_mode_e.OUTER] = [
C('((', Id.Op_DLeftParen), # not allowed within [[
# These four are not allowed within [[, so they are in OUTER but not
# _UNQUOTED.

# e.g. beginning of NAME=val, which will always be longer than the above
# Id.Lit_Chars.
R(r'[a-zA-Z_][a-zA-Z0-9_]*\+?=', Id.Lit_VarLike),
R(r'[a-zA-Z_][a-zA-Z0-9_]*\[', Id.Lit_ArrayLhsOpen),
R(r'\]\+?=', Id.Lit_ArrayLhsClose),
C('((', Id.Op_DLeftParen),
] + _KEYWORDS + _MORE_KEYWORDS + _UNQUOTED + _EXTGLOB_BEGIN

# DBRACKET: can be like OUTER, except:
Expand Down
18 changes: 17 additions & 1 deletion scripts/complete.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,23 @@ set -o pipefail
set -o errexit

git-audit() {
grep -E -w 'complete|compgen|compopt' testdata/completion/git-completion.bash
local file=testdata/completion/git-completion.bash

echo
echo --
# Search for completion builtin usage
grep -E -w --color 'complete|compgen|compopt' $file

echo
echo --
# Search for special complation var usage
grep -E --color 'COMP_[A-Z]+' $file

echo
echo --
# Search for array usage
grep -E --color ']=' $file
grep -E --color ']+=' $file
}

"$@"

0 comments on commit 61bc9db

Please sign in to comment.