Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/pyparsing/__init__.py: 90%

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

41 statements  

1# module pyparsing.py 

2# 

3# Copyright (c) 2003-2022 Paul T. McGuire 

4# 

5# Permission is hereby granted, free of charge, to any person obtaining 

6# a copy of this software and associated documentation files (the 

7# "Software"), to deal in the Software without restriction, including 

8# without limitation the rights to use, copy, modify, merge, publish, 

9# distribute, sublicense, and/or sell copies of the Software, and to 

10# permit persons to whom the Software is furnished to do so, subject to 

11# the following conditions: 

12# 

13# The above copyright notice and this permission notice shall be 

14# included in all copies or substantial portions of the Software. 

15# 

16# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 

17# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 

18# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 

19# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 

20# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 

21# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 

22# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 

23# 

24 

25__doc__ = """ 

26pyparsing - Classes and methods to define and execute parsing grammars 

27====================================================================== 

28 

29Pyparsing is an alternative approach to creating and executing simple 

30grammars, vs. the traditional lex/yacc approach, or the use of regular 

31expressions. With pyparsing, you don't need to learn a new syntax for 

32defining grammars or matching expressions - the parsing module provides 

33a library of classes that you use to construct the grammar directly in 

34Python. 

35 

36Here is a program to parse "Hello, World!" (or any greeting of the form 

37``"<salutation>, <addressee>!"``), built up using :class:`Word`, 

38:class:`Literal`, and :class:`And` elements 

39(the :meth:`'+'<ParserElement.__add__>` operators create :class:`And` expressions, 

40and the strings are auto-converted to :class:`Literal` expressions): 

41 

42.. testcode:: 

43 

44 from pyparsing import Word, alphas 

45 

46 # define grammar of a greeting 

47 greet = Word(alphas) + "," + Word(alphas) + "!" 

48 

49 hello = "Hello, World!" 

50 print(hello, "->", greet.parse_string(hello)) 

51 

52The program outputs the following: 

53 

54.. testoutput:: 

55 

56 Hello, World! -> ['Hello', ',', 'World', '!'] 

57 

58The Python representation of the grammar is quite readable, owing to the 

59self-explanatory class names, and the use of :class:`'+'<And>`, 

60:class:`'|'<MatchFirst>`, :class:`'^'<Or>` and :class:`'&'<Each>` operators. 

61 

62The :class:`ParseResults` object returned from 

63:class:`ParserElement.parse_string` can be 

64accessed as a nested list, a dictionary, or an object with named 

65attributes. 

66 

67The pyparsing module handles some of the problems that are typically 

68vexing when writing text parsers: 

69 

70 - extra or missing whitespace (the above program will also handle 

71 "Hello,World!", "Hello , World !", etc.) 

72 - quoted strings 

73 - embedded comments 

74 

75 

76Getting Started 

77--------------- 

78Visit the classes :class:`ParserElement` and :class:`ParseResults` to 

79see the base classes that most other pyparsing 

80classes inherit from. Use the docstrings for examples of how to: 

81 

82 - construct literal match expressions from :class:`Literal` and 

83 :class:`CaselessLiteral` classes 

84 - construct character word-group expressions using the :class:`Word` 

85 class 

86 - see how to create repetitive expressions using :class:`ZeroOrMore` 

87 and :class:`OneOrMore` classes 

88 - use :class:`'+'<And>`, :class:`'|'<MatchFirst>`, :class:`'^'<Or>`, 

89 and :class:`'&'<Each>` operators to combine simple expressions into 

90 more complex ones 

91 - associate names with your parsed results using 

92 :class:`ParserElement.set_results_name` 

93 - access the parsed data, which is returned as a :class:`ParseResults` 

94 object 

95 - find some helpful expression short-cuts like :class:`DelimitedList` 

96 and :class:`one_of` 

97 - find more useful common expressions in the :class:`pyparsing_common` 

98 namespace class 

99""" 

100from typing import NamedTuple 

101 

102 

103class version_info(NamedTuple): 

104 major: int 

105 minor: int 

106 micro: int 

107 releaselevel: str 

108 serial: int 

109 

110 @property 

111 def __version__(self): 

112 return ( 

113 f"{self.major}.{self.minor}.{self.micro}" 

114 + ( 

115 f"{'r' if self.releaselevel[0] == 'c' else ''}{self.releaselevel[0]}{self.serial}", 

116 "", 

117 )[self.releaselevel == "final"] 

118 ) 

119 

120 def __str__(self): 

121 return f"{__name__} {self.__version__} / {__version_time__}" 

122 

123 def __repr__(self): 

124 return f"{__name__}.{type(self).__name__}({', '.join('{}={!r}'.format(*nv) for nv in zip(self._fields, self))})" 

125 

126 

127__version_info__ = version_info(3, 2, 4, "final", 1) 

128__version_time__ = "07 Sep 2025 00:47 UTC" 

129__version__ = __version_info__.__version__ 

130__versionTime__ = __version_time__ 

131__author__ = "Paul McGuire <ptmcg.gm+pyparsing@gmail.com>" 

132 

133from .util import * 

134from .exceptions import * 

135from .actions import * 

136from .core import __diag__, __compat__ 

137from .results import * 

138from .core import * 

139from .core import _builtin_exprs as core_builtin_exprs 

140from .helpers import * 

141from .helpers import _builtin_exprs as helper_builtin_exprs 

142 

143from .unicode import unicode_set, UnicodeRangeList, pyparsing_unicode as unicode 

144from .testing import pyparsing_test as testing 

145from .common import ( 

146 pyparsing_common as common, 

147 _builtin_exprs as common_builtin_exprs, 

148) 

149 

150# Compatibility synonyms 

151if "pyparsing_unicode" not in globals(): 

152 pyparsing_unicode = unicode # type: ignore[misc] 

153if "pyparsing_common" not in globals(): 

154 pyparsing_common = common 

155if "pyparsing_test" not in globals(): 

156 pyparsing_test = testing 

157 

158core_builtin_exprs += common_builtin_exprs + helper_builtin_exprs 

159 

160 

161__all__ = [ 

162 "__version__", 

163 "__version_time__", 

164 "__author__", 

165 "__compat__", 

166 "__diag__", 

167 "And", 

168 "AtLineStart", 

169 "AtStringStart", 

170 "CaselessKeyword", 

171 "CaselessLiteral", 

172 "CharsNotIn", 

173 "CloseMatch", 

174 "Combine", 

175 "DelimitedList", 

176 "Dict", 

177 "Each", 

178 "Empty", 

179 "FollowedBy", 

180 "Forward", 

181 "GoToColumn", 

182 "Group", 

183 "IndentedBlock", 

184 "Keyword", 

185 "LineEnd", 

186 "LineStart", 

187 "Literal", 

188 "Located", 

189 "PrecededBy", 

190 "MatchFirst", 

191 "NoMatch", 

192 "NotAny", 

193 "OneOrMore", 

194 "OnlyOnce", 

195 "OpAssoc", 

196 "Opt", 

197 "Optional", 

198 "Or", 

199 "ParseBaseException", 

200 "ParseElementEnhance", 

201 "ParseException", 

202 "ParseExpression", 

203 "ParseFatalException", 

204 "ParseResults", 

205 "ParseSyntaxException", 

206 "ParserElement", 

207 "PositionToken", 

208 "QuotedString", 

209 "RecursiveGrammarException", 

210 "Regex", 

211 "SkipTo", 

212 "StringEnd", 

213 "StringStart", 

214 "Suppress", 

215 "Tag", 

216 "Token", 

217 "TokenConverter", 

218 "White", 

219 "Word", 

220 "WordEnd", 

221 "WordStart", 

222 "ZeroOrMore", 

223 "Char", 

224 "alphanums", 

225 "alphas", 

226 "alphas8bit", 

227 "any_close_tag", 

228 "any_open_tag", 

229 "autoname_elements", 

230 "c_style_comment", 

231 "col", 

232 "common_html_entity", 

233 "condition_as_parse_action", 

234 "counted_array", 

235 "cpp_style_comment", 

236 "dbl_quoted_string", 

237 "dbl_slash_comment", 

238 "delimited_list", 

239 "dict_of", 

240 "empty", 

241 "hexnums", 

242 "html_comment", 

243 "identchars", 

244 "identbodychars", 

245 "infix_notation", 

246 "java_style_comment", 

247 "line", 

248 "line_end", 

249 "line_start", 

250 "lineno", 

251 "make_html_tags", 

252 "make_xml_tags", 

253 "match_only_at_col", 

254 "match_previous_expr", 

255 "match_previous_literal", 

256 "nested_expr", 

257 "null_debug_action", 

258 "nums", 

259 "one_of", 

260 "original_text_for", 

261 "printables", 

262 "punc8bit", 

263 "pyparsing_common", 

264 "pyparsing_test", 

265 "pyparsing_unicode", 

266 "python_style_comment", 

267 "quoted_string", 

268 "remove_quotes", 

269 "replace_with", 

270 "replace_html_entity", 

271 "rest_of_line", 

272 "sgl_quoted_string", 

273 "srange", 

274 "string_end", 

275 "string_start", 

276 "token_map", 

277 "trace_parse_action", 

278 "ungroup", 

279 "unicode_set", 

280 "unicode_string", 

281 "with_attribute", 

282 "with_class", 

283 # pre-PEP8 compatibility names 

284 "__versionTime__", 

285 "anyCloseTag", 

286 "anyOpenTag", 

287 "cStyleComment", 

288 "commonHTMLEntity", 

289 "conditionAsParseAction", 

290 "countedArray", 

291 "cppStyleComment", 

292 "dblQuotedString", 

293 "dblSlashComment", 

294 "delimitedList", 

295 "dictOf", 

296 "htmlComment", 

297 "indentedBlock", 

298 "infixNotation", 

299 "javaStyleComment", 

300 "lineEnd", 

301 "lineStart", 

302 "locatedExpr", 

303 "makeHTMLTags", 

304 "makeXMLTags", 

305 "matchOnlyAtCol", 

306 "matchPreviousExpr", 

307 "matchPreviousLiteral", 

308 "nestedExpr", 

309 "nullDebugAction", 

310 "oneOf", 

311 "opAssoc", 

312 "originalTextFor", 

313 "pythonStyleComment", 

314 "quotedString", 

315 "removeQuotes", 

316 "replaceHTMLEntity", 

317 "replaceWith", 

318 "restOfLine", 

319 "sglQuotedString", 

320 "stringEnd", 

321 "stringStart", 

322 "tokenMap", 

323 "traceParseAction", 

324 "unicodeString", 

325 "withAttribute", 

326 "withClass", 

327 "common", 

328 "unicode", 

329 "testing", 

330]