OILS / frontend / syntax_abbrev.py View on Github | oilshell.org

140 lines, 84 significant
1"""
2syntax_abbrev.py - Abbreviations for pretty-printing syntax.asdl.
3
4This module is not used directly, but is combined with generated code.
5"""
6
7from _devbuild.gen.id_kind_asdl import Id
8from _devbuild.gen.hnode_asdl import hnode_t
9from asdl import runtime
10
11
12def _AbbreviateToken(tok, out):
13 # type: (Token, List[hnode_t]) -> None
14 tok_str = tok.line.content[tok.col:tok.col + tok.length]
15 n1 = runtime.NewLeaf(Id_str(tok.id), color_e.OtherConst)
16 out.append(n1)
17 n2 = runtime.NewLeaf(tok_str, color_e.StringConst)
18 out.append(n2)
19
20
21def _Token(obj):
22 # type: (Token) -> hnode_t
23 p_node = runtime.NewRecord('') # don't show node type
24 p_node.abbrev = True
25
26 p_node.left = '<'
27 p_node.right = '>'
28 _AbbreviateToken(obj, p_node.unnamed_fields)
29 return p_node
30
31
32def _CompoundWord(obj):
33 # type: (CompoundWord) -> hnode_t
34 p_node = runtime.NewRecord('') # don't show node type
35 p_node.abbrev = True
36 p_node.left = '{'
37 p_node.right = '}'
38
39 for part in obj.parts:
40 p_node.unnamed_fields.append(part.AbbreviatedTree())
41 return p_node
42
43
44def _DoubleQuoted(obj):
45 # type: (DoubleQuoted) -> hnode_t
46 if obj.left.id != Id.Left_DoubleQuote:
47 return None # Fall back on obj._AbbreviatedTree()
48
49 p_node = runtime.NewRecord('DQ')
50 p_node.abbrev = True
51
52 for part in obj.parts:
53 p_node.unnamed_fields.append(part.AbbreviatedTree())
54 return p_node
55
56
57def _SingleQuoted(obj):
58 # type: (SingleQuoted) -> hnode_t
59
60 # Only abbreviate 'foo', not $'foo\n' or r'foo'
61 if obj.left.id != Id.Left_SingleQuote:
62 return None # Fall back on obj._AbbreviatedTree()
63
64 p_node = runtime.NewRecord('SQ')
65 p_node.abbrev = True
66
67 n2 = runtime.NewLeaf(obj.sval, color_e.StringConst)
68 p_node.unnamed_fields.append(n2)
69 return p_node
70
71
72def _SimpleVarSub(obj):
73 # type: (SimpleVarSub) -> hnode_t
74 p_node = runtime.NewRecord('$')
75 p_node.abbrev = True
76
77 if obj.tok.id in (Id.VSub_DollarName, Id.VSub_Number): # $myvar or $1
78 # We want to show the variable name
79 # _AbbreviateToken(obj.tok, p_node.unnamed_fields)
80 tok = obj.tok
81 # Omit $
82 var_name = tok.line.content[tok.col + 1:tok.col + tok.length]
83 n1 = runtime.NewLeaf(var_name, color_e.StringConst)
84 p_node.unnamed_fields.append(n1)
85 else: # $?
86 n1 = runtime.NewLeaf(Id_str(obj.tok.id), color_e.OtherConst)
87 p_node.unnamed_fields.append(n1)
88
89 return p_node
90
91
92def _BracedVarSub(obj):
93 # type: (BracedVarSub) -> hnode_t
94 p_node = runtime.NewRecord('${')
95 if obj.prefix_op or obj.bracket_op or obj.suffix_op:
96 return None # we have other fields to display; don't abbreviate
97
98 p_node.abbrev = True
99 _AbbreviateToken(obj.token, p_node.unnamed_fields)
100 return p_node
101
102
103def _command__Simple(obj):
104 # type: (command.Simple) -> hnode_t
105 p_node = runtime.NewRecord('C')
106 if (obj.more_env or obj.typed_args or obj.block or obj.do_fork == False):
107 return None # we have other fields to display; don't abbreviate
108
109 p_node.abbrev = True
110
111 for w in obj.words:
112 p_node.unnamed_fields.append(w.AbbreviatedTree())
113 return p_node
114
115
116def _expr__Var(obj):
117 # type: (expr.Var) -> hnode_t
118 p_node = runtime.NewRecord('Var')
119 p_node.abbrev = True
120
121 assert obj.left.id == Id.Expr_Name, obj.name
122 n1 = runtime.NewLeaf(obj.name, color_e.StringConst)
123 p_node.unnamed_fields.append(n1)
124 return p_node
125
126
127def _expr__Const(obj):
128 # type: (expr.Const) -> hnode_t
129 p_node = runtime.NewRecord('Const')
130 p_node.abbrev = True
131
132 tok = obj.c
133 out = p_node.unnamed_fields
134
135 n1 = runtime.NewLeaf(Id_str(tok.id), color_e.OtherConst)
136 out.append(n1)
137
138 n2 = runtime.NewLeaf(tok.tval, color_e.StringConst)
139 out.append(n2)
140 return p_node