1 | """
|
2 | syntax_abbrev.py - Abbreviations for pretty-printing syntax.asdl.
|
3 |
|
4 | This module is not used directly, but is combined with generated code.
|
5 | """
|
6 |
|
7 | from _devbuild.gen.id_kind_asdl import Id
|
8 | from _devbuild.gen.hnode_asdl import hnode_t
|
9 | from asdl import runtime
|
10 |
|
11 |
|
12 | def _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 |
|
21 | def _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 |
|
32 | def _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 |
|
44 | def _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 |
|
57 | def _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 |
|
72 | def _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 |
|
92 | def _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 |
|
103 | def _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
|
107 | obj.is_last_cmd == True):
|
108 | return None # we have other fields to display; don't abbreviate
|
109 |
|
110 | p_node.abbrev = True
|
111 |
|
112 | for w in obj.words:
|
113 | p_node.unnamed_fields.append(w.AbbreviatedTree())
|
114 | return p_node
|
115 |
|
116 |
|
117 | def _expr__Var(obj):
|
118 | # type: (expr.Var) -> hnode_t
|
119 | p_node = runtime.NewRecord('Var')
|
120 | p_node.abbrev = True
|
121 |
|
122 | assert obj.left.id == Id.Expr_Name, obj.name
|
123 | n1 = runtime.NewLeaf(obj.name, color_e.StringConst)
|
124 | p_node.unnamed_fields.append(n1)
|
125 | return p_node
|
126 |
|
127 |
|
128 | def _expr__Const(obj):
|
129 | # type: (expr.Const) -> hnode_t
|
130 | p_node = runtime.NewRecord('Const')
|
131 | p_node.abbrev = True
|
132 |
|
133 | tok = obj.c
|
134 | out = p_node.unnamed_fields
|
135 |
|
136 | n1 = runtime.NewLeaf(Id_str(tok.id), color_e.OtherConst)
|
137 | out.append(n1)
|
138 |
|
139 | n2 = runtime.NewLeaf(tok.tval, color_e.StringConst)
|
140 | out.append(n2)
|
141 | return p_node
|