Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/blib2to3/pygram.py: 99%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

155 statements  

1# Copyright 2006 Google, Inc. All Rights Reserved. 

2# Licensed to PSF under a Contributor Agreement. 

3 

4"""Export the Python grammar and symbols.""" 

5 

6# Python imports 

7import os 

8from typing import Union 

9 

10# Local imports 

11from .pgen2 import driver 

12from .pgen2.grammar import Grammar 

13 

14# Moved into initialize because mypyc can't handle __file__ (XXX bug) 

15# # The grammar file 

16# _GRAMMAR_FILE = os.path.join(os.path.dirname(__file__), "Grammar.txt") 

17# _PATTERN_GRAMMAR_FILE = os.path.join(os.path.dirname(__file__), 

18# "PatternGrammar.txt") 

19 

20 

21class Symbols: 

22 def __init__(self, grammar: Grammar) -> None: 

23 """Initializer. 

24 

25 Creates an attribute for each grammar symbol (nonterminal), 

26 whose value is the symbol's type (an int >= 256). 

27 """ 

28 for name, symbol in grammar.symbol2number.items(): 

29 setattr(self, name, symbol) 

30 

31 

32class _python_symbols(Symbols): 

33 and_expr: int 

34 and_test: int 

35 annassign: int 

36 arglist: int 

37 argument: int 

38 arith_expr: int 

39 asexpr_test: int 

40 assert_stmt: int 

41 async_funcdef: int 

42 async_stmt: int 

43 atom: int 

44 augassign: int 

45 break_stmt: int 

46 case_block: int 

47 classdef: int 

48 comp_for: int 

49 comp_if: int 

50 comp_iter: int 

51 comp_op: int 

52 comparison: int 

53 compound_stmt: int 

54 continue_stmt: int 

55 decorated: int 

56 decorator: int 

57 decorators: int 

58 del_stmt: int 

59 dictsetmaker: int 

60 dotted_as_name: int 

61 dotted_as_names: int 

62 dotted_name: int 

63 encoding_decl: int 

64 eval_input: int 

65 except_clause: int 

66 expr: int 

67 expr_stmt: int 

68 exprlist: int 

69 factor: int 

70 file_input: int 

71 flow_stmt: int 

72 for_stmt: int 

73 fstring: int 

74 fstring_format_spec: int 

75 fstring_middle: int 

76 fstring_replacement_field: int 

77 funcdef: int 

78 global_stmt: int 

79 guard: int 

80 if_stmt: int 

81 import_as_name: int 

82 import_as_names: int 

83 import_from: int 

84 import_name: int 

85 import_stmt: int 

86 lambdef: int 

87 listmaker: int 

88 match_stmt: int 

89 namedexpr_test: int 

90 not_test: int 

91 old_comp_for: int 

92 old_comp_if: int 

93 old_comp_iter: int 

94 old_lambdef: int 

95 old_test: int 

96 or_test: int 

97 parameters: int 

98 paramspec: int 

99 pass_stmt: int 

100 pattern: int 

101 patterns: int 

102 power: int 

103 raise_stmt: int 

104 return_stmt: int 

105 shift_expr: int 

106 simple_stmt: int 

107 single_input: int 

108 sliceop: int 

109 small_stmt: int 

110 subject_expr: int 

111 star_expr: int 

112 stmt: int 

113 subscript: int 

114 subscriptlist: int 

115 suite: int 

116 term: int 

117 test: int 

118 testlist: int 

119 testlist1: int 

120 testlist_gexp: int 

121 testlist_safe: int 

122 testlist_star_expr: int 

123 tfpdef: int 

124 tfplist: int 

125 tname: int 

126 tname_star: int 

127 trailer: int 

128 try_stmt: int 

129 type_stmt: int 

130 typedargslist: int 

131 typeparam: int 

132 typeparams: int 

133 typevar: int 

134 typevartuple: int 

135 varargslist: int 

136 vfpdef: int 

137 vfplist: int 

138 vname: int 

139 while_stmt: int 

140 with_stmt: int 

141 xor_expr: int 

142 yield_arg: int 

143 yield_expr: int 

144 yield_stmt: int 

145 

146 

147class _pattern_symbols(Symbols): 

148 Alternative: int 

149 Alternatives: int 

150 Details: int 

151 Matcher: int 

152 NegatedUnit: int 

153 Repeater: int 

154 Unit: int 

155 

156 

157python_grammar: Grammar 

158python_grammar_async_keywords: Grammar 

159python_grammar_soft_keywords: Grammar 

160pattern_grammar: Grammar 

161python_symbols: _python_symbols 

162pattern_symbols: _pattern_symbols 

163 

164 

165def initialize(cache_dir: Union[str, "os.PathLike[str]", None] = None) -> None: 

166 global python_grammar 

167 global python_grammar_async_keywords 

168 global python_grammar_soft_keywords 

169 global python_symbols 

170 global pattern_grammar 

171 global pattern_symbols 

172 

173 # The grammar file 

174 _GRAMMAR_FILE = os.path.join(os.path.dirname(__file__), "Grammar.txt") 

175 _PATTERN_GRAMMAR_FILE = os.path.join( 

176 os.path.dirname(__file__), "PatternGrammar.txt" 

177 ) 

178 

179 python_grammar = driver.load_packaged_grammar("blib2to3", _GRAMMAR_FILE, cache_dir) 

180 assert "print" not in python_grammar.keywords 

181 assert "exec" not in python_grammar.keywords 

182 

183 soft_keywords = python_grammar.soft_keywords.copy() 

184 python_grammar.soft_keywords.clear() 

185 

186 python_symbols = _python_symbols(python_grammar) 

187 

188 # Python 3.0-3.6 

189 python_grammar.version = (3, 0) 

190 

191 # Python 3.7+ 

192 python_grammar_async_keywords = python_grammar.copy() 

193 python_grammar_async_keywords.async_keywords = True 

194 python_grammar_async_keywords.version = (3, 7) 

195 

196 # Python 3.10+ 

197 python_grammar_soft_keywords = python_grammar_async_keywords.copy() 

198 python_grammar_soft_keywords.soft_keywords = soft_keywords 

199 python_grammar_soft_keywords.version = (3, 10) 

200 

201 pattern_grammar = driver.load_packaged_grammar( 

202 "blib2to3", _PATTERN_GRAMMAR_FILE, cache_dir 

203 ) 

204 pattern_symbols = _pattern_symbols(pattern_grammar)