Skip to content

Commit

Permalink
[refactor] Shared the DoubleQuoted variant between expr and word_part.
Browse files Browse the repository at this point in the history
That was easier than I thought!

There are still some type errors which can be solved by multiple
inheritance.
  • Loading branch information
Andy Chu committed Sep 17, 2019
1 parent 973afe4 commit 43b976c
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 33 deletions.
8 changes: 8 additions & 0 deletions asdl/front_end_test.py
Expand Up @@ -41,6 +41,14 @@ def testSharedVariantCode(self):
self.assertEqual(1001, expr_e.DoubleQuoted)
self.assertEqual(expr_e.DoubleQuoted, word_part_e.DoubleQuoted)

d = double_quoted(5, ['foo', 'bar'])
d.PrettyPrint()
print()

b = expr.Binary(d, d)
b.PrettyPrint()



if __name__ == '__main__':
unittest.main()
5 changes: 2 additions & 3 deletions asdl/gen_python.py
Expand Up @@ -319,10 +319,9 @@ def VisitCompoundSum(self, sum, sum_name, depth):

def VisitProduct(self, product, name, depth):
self._shared_type_tags[name] = self._product_counter
self._product_counter += 1

self._GenClass(product, product.attributes, name, 'runtime.CompoundObj',
depth)
depth, tag_num=self._product_counter)
self._product_counter += 1

def EmitFooter(self):
pass
6 changes: 4 additions & 2 deletions frontend/syntax.asdl
Expand Up @@ -97,6 +97,8 @@ module syntax
| Slice(arith_expr? begin, arith_expr? length)
attributes (int* spids)

double_quoted = (token left, word_part* parts) attributes (int* spids)

word_part =
ArrayLiteral(word* words)
-- alternating key and value (saving some space)
Expand All @@ -105,7 +107,7 @@ module syntax
-- escaped case is separate so the evaluator doesn't have to check token ID
| EscapedLiteral(token token)
| SingleQuoted(token left, token* tokens)
| DoubleQuoted(token left, word_part* parts)
| DoubleQuoted %double_quoted
| SimpleVarSub(token token)

-- TODO: Add Oil expressions
Expand Down Expand Up @@ -337,7 +339,7 @@ module syntax
| SingleQuoted(token left, token* tokens)
-- NOTE: Duplicates word_part.DoubleQuoted
-- Not all word_part are valid here!
| DoubleQuoted(token left, word_part* parts)
| DoubleQuoted %double_quoted

| Unary(token op, expr child)
| Binary(token op, expr left, expr right)
Expand Down
28 changes: 14 additions & 14 deletions frontend/syntax_abbrev.py
Expand Up @@ -28,6 +28,20 @@ def _token(obj):
return p_node


def _double_quoted(obj):
# type: (double_quoted) -> PrettyNode
if obj.left.id != Id.Left_DoubleQuote:
return None # Fall back on obj._AbbreviatedTree()

p_node = runtime.PrettyNode()
p_node.abbrev = True
p_node.node_type = 'DQ'

for part in obj.parts:
p_node.unnamed_fields.append(part.AbbreviatedTree())
return p_node


def _word_part__Literal(obj):
# type: (word_part__Literal) -> PrettyNode
p_node = runtime.PrettyNode()
Expand Down Expand Up @@ -59,20 +73,6 @@ def _word_part__BracedVarSub(obj):
return p_node


def _word_part__DoubleQuoted(obj):
# type: (word_part__DoubleQuoted) -> PrettyNode
if obj.left.id != Id.Left_DoubleQuote:
return None # Fall back on obj._AbbreviatedTree()

p_node = runtime.PrettyNode()
p_node.abbrev = True
p_node.node_type = 'DQ'

for part in obj.parts:
p_node.unnamed_fields.append(part.AbbreviatedTree())
return p_node


def _word_part__SingleQuoted(obj):
# type: (word_part__SingleQuoted) -> PrettyNode

Expand Down
5 changes: 3 additions & 2 deletions oil_lang/expr_parse.py
Expand Up @@ -6,7 +6,8 @@
import sys

from _devbuild.gen.syntax_asdl import (
token, word__Token, word__Compound, word_part, word_part_t, expr
token, double_quoted, word__Token, word__Compound, word_part, word_part_t,
expr
)
from _devbuild.gen.id_kind_asdl import Id, Kind
from _devbuild.gen.types_asdl import lex_mode_e
Expand Down Expand Up @@ -312,7 +313,7 @@ def _PushOilTokens(parse_ctx, gr, p, lex):

parts = [] # type: List[word_part_t]
last_token = w_parser.ReadDoubleQuoted(left_token, parts)
expr_dq_part = expr.DoubleQuoted(left_token, parts)
expr_dq_part = double_quoted(left_token, parts)

typ = Id.Expr_CastedDummy.enum_id
opaque = cast(token, expr_dq_part) # HACK for expr_to_ast
Expand Down
8 changes: 4 additions & 4 deletions oil_lang/expr_to_ast.py
Expand Up @@ -5,9 +5,9 @@

from _devbuild.gen.id_kind_asdl import Id
from _devbuild.gen.syntax_asdl import (
token, command, command__VarDecl,
expr, expr_t,
expr__DoubleQuoted, expr__SingleQuoted, expr__Dict, expr__BracedVarSub,
token, double_quoted,
command, command__VarDecl,
expr, expr_t, expr__SingleQuoted, expr__Dict, expr__BracedVarSub,
expr_context_e, regex, regex_t,
word_t, word_part__CommandSub,
param, type_expr_t,
Expand Down Expand Up @@ -496,7 +496,7 @@ def Expr(self, pnode):
return part

elif typ == grammar_nt.dq_string:
dq_part = cast(expr__DoubleQuoted, children[1].tok)
dq_part = cast(double_quoted, children[1].tok)
return dq_part

elif typ == grammar_nt.sq_string:
Expand Down
9 changes: 5 additions & 4 deletions osh/word_.py
Expand Up @@ -5,10 +5,11 @@
from _devbuild.gen.id_kind_asdl import (Id, Kind, Id_t, Kind_t)
from _devbuild.gen.syntax_asdl import (
token,
double_quoted,
word_part, word_part_t, word_part_e,
word_part__ArrayLiteral, word_part__AssocArrayLiteral,
word_part__Literal, word_part__EscapedLiteral,
word_part__SingleQuoted, word_part__DoubleQuoted,
word_part__SingleQuoted,
word_part__SimpleVarSub, word_part__BracedVarSub, word_part__TildeSub,
word_part__CommandSub, word_part__ArithSub, word_part__BracedTuple,
word_part__ExtGlob, word_part__Splice, word_part__FuncCall,
Expand Down Expand Up @@ -84,7 +85,7 @@ def _EvalWordPart(part):
s = ''.join(t.val for t in part.tokens)
return True, s, True

elif isinstance(part, word_part__DoubleQuoted):
elif isinstance(part, double_quoted):
ret = ''
for p in part.parts:
ok, s, _ = _EvalWordPart(p)
Expand Down Expand Up @@ -145,7 +146,7 @@ def LeftMostSpanForPart(part):
elif isinstance(part, word_part__SingleQuoted):
return part.left.span_id # single quote location

elif isinstance(part, word_part__DoubleQuoted):
elif isinstance(part, double_quoted):
return part.left.span_id # double quote location

elif isinstance(part, word_part__SimpleVarSub):
Expand Down Expand Up @@ -201,7 +202,7 @@ def _RightMostSpanForPart(part):
elif isinstance(part, word_part__SingleQuoted):
return part.spids[1] # right '

elif isinstance(part, word_part__DoubleQuoted):
elif isinstance(part, double_quoted):
return part.spids[1] # right "

elif isinstance(part, word_part__SimpleVarSub):
Expand Down
8 changes: 4 additions & 4 deletions osh/word_parse.py
Expand Up @@ -51,14 +51,14 @@
from _devbuild.gen.id_kind_asdl import Id, Kind, Id_t
from _devbuild.gen.types_asdl import lex_mode_t, lex_mode_e
from _devbuild.gen.syntax_asdl import (
token, arith_expr_t,
token, double_quoted, arith_expr_t,
suffix_op, suffix_op_t, suffix_op__Slice, suffix_op__PatSub,
bracket_op, bracket_op_t,

word, word_t, word__Compound, word__Token,
word_part, word_part_t,
word_part__Literal, word_part__BracedVarSub, word_part__SingleQuoted,
word_part__ArithSub, word_part__DoubleQuoted, word_part__CommandSub,
word_part__ArithSub, word_part__CommandSub,
word_part__ExtGlob,

command, command_t, command__ForExpr,
Expand Down Expand Up @@ -687,7 +687,7 @@ def _ReadLikeDQ(self, left_dq_token, out_parts):
# Return nothing, since we appended to 'out_parts'

def _ReadDoubleQuoted(self):
# type: () -> word_part__DoubleQuoted
# type: () -> double_quoted
"""
Args:
eof_type: for stopping at }, Id.Lit_RBrace
Expand All @@ -699,7 +699,7 @@ def _ReadDoubleQuoted(self):
parts = [] # type: List[word_part_t]
self._ReadLikeDQ(left_dq_token, parts)

dq_part = word_part.DoubleQuoted(left_dq_token, parts)
dq_part = double_quoted(left_dq_token, parts)
dq_part.spids.append(left_dq_token.span_id) # Left ", sort of redundant
dq_part.spids.append(self.cur_token.span_id) # Right "
return dq_part
Expand Down

0 comments on commit 43b976c

Please sign in to comment.