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

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