Skip to content

Commit

Permalink
[grammar refactor] Consolidate eggex
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy Chu committed Dec 12, 2023
1 parent 6b6319e commit a82f94a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 deletions.
15 changes: 14 additions & 1 deletion test/ysh-parse-errors.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1278,7 +1278,7 @@ json write (x) {
_ysh-parse-error 'f(x)' # test short name
}

test-eggex() {
test-eggex-capture() {
_should-parse '= / d+ /'
#_should-parse '= / <d+ : date> /'
_should-parse '= / < capture d+ as date > /'
Expand All @@ -1289,6 +1289,19 @@ test-eggex() {
_parse-error 'var as = 42'
}


test-eggex-flags() {
_should-parse '= / d+ ; ignorecase /'

# ERE means is the default; it's POSIX ERE
# Other option is PCRE
_should-parse '= / d+ ; ignorecase ; ERE /'
_should-parse '= / d+ ; ; ERE /'

# typo should be parse error
_parse-error '= / d+ ; ignorecas /'
}

#
# Entry Points
#
Expand Down
23 changes: 11 additions & 12 deletions ysh/expr_to_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,13 +384,6 @@ def _Atom(self, parent):
i += 1
return self._Dict(parent, parent.GetChild(i))

if id_ == Id.Arith_Slash:
r = self._Regex(parent.GetChild(1))
flags = [] # type: List[Token]
# TODO: Parse translation preference.
trans_pref = None # type: Token
return Eggex(parent.GetChild(0).tok, r, flags, trans_pref)

if id_ == Id.Arith_Amp:
n = parent.NumChildren()
if n >= 3:
Expand Down Expand Up @@ -673,6 +666,9 @@ def Expr(self, pnode):
inner = self.Expr(pnode.GetChild(1))
return expr.Literal(inner)

elif typ == grammar_nt.eggex:
return self._Eggex(pnode)

elif typ == grammar_nt.ysh_expr_sub:
return self.Expr(pnode.GetChild(0))

Expand Down Expand Up @@ -824,6 +820,12 @@ def MakeMutation(self, p_node):
rhs = self.Expr(p_node.GetChild(2))
return command.Mutation(None, lhs_list, op_tok, rhs)

def _Eggex(self, p_node):
# type: (PNode) -> Eggex
left = p_node.GetChild(0).tok
regex = self._Regex(p_node.GetChild(1))
return Eggex(left, regex, [], None)

def YshCasePattern(self, pnode):
# type: (PNode) -> pat_t
assert pnode.typ == grammar_nt.ysh_case_pat, pnode
Expand All @@ -846,11 +848,8 @@ def YshCasePattern(self, pnode):
exprs.append(expr)
return pat.YshExprs(exprs)

elif typ == grammar_nt.pat_eggex:
# pat_eggex
left = pattern.GetChild(0).tok
re = self._Regex(pattern.GetChild(1))
return Eggex(left, re, [], None)
elif typ == grammar_nt.eggex:
return self._Eggex(pattern)

raise NotImplementedError()

Expand Down
7 changes: 4 additions & 3 deletions ysh/grammar.pgen2
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ atom: (
| '[' [testlist_comp] ']'
# Note: newlines are significant inside {}, unlike inside () and []
| '{' [Op_Newline] [dict] '}'
| '/' regex [re_flags] '/'
| '&' Expr_Name place_trailer*

# NOTE: These atoms are are allowed in typed array literals
Expand All @@ -105,6 +104,7 @@ atom: (
| dq_string | sq_string
# Expr_Symbol could be %mykey

| eggex
| literal_expr

# $foo is disallowed, but $? is allowed. Should be "$foo" to indicate a
Expand Down Expand Up @@ -472,14 +472,15 @@ regex: [re_alt] (('|'|'or') re_alt)*
# ^-- End of pattern/beginning of case arm body
# }

eggex: '/' regex [re_flags] '/'

ysh_case_pat: (
'(' (pat_else | pat_exprs)
| pat_eggex
| eggex
) [Op_Newline] '{'

pat_else: 'else' ')'
pat_exprs: expr ')' [Op_Newline] ('|' [Op_Newline] '(' expr ')' [Op_Newline])*
pat_eggex: '/' regex [re_flags] '/'

# e.g. /digit+ ; multiline !ignorecase/
#
Expand Down

0 comments on commit a82f94a

Please sign in to comment.