diff options
Diffstat (limited to 'pyparsing')
-rw-r--r-- | pyparsing/__init__.py | 73 | ||||
-rw-r--r-- | pyparsing/common.py | 91 | ||||
-rw-r--r-- | pyparsing/core.py | 18 |
3 files changed, 121 insertions, 61 deletions
diff --git a/pyparsing/__init__.py b/pyparsing/__init__.py index 41a12db..0c27ea6 100644 --- a/pyparsing/__init__.py +++ b/pyparsing/__init__.py @@ -161,6 +161,7 @@ __all__ = [ "NotAny", "OneOrMore", "OnlyOnce", + "OpAssoc", "Optional", "Or", "ParseBaseException", @@ -189,10 +190,66 @@ __all__ = [ "alphanums", "alphas", "alphas8bit", + "any_close_tag", + "any_open_tag", + "c_style_comment", + "col", + "common_html_entity", + "counted_array", + "cpp_style_comment", + "dbl_quoted_string", + "dbl_slash_comment", + "delimited_list", + "dict_of", + "empty", + "hexnums", + "html_comment", + "java_style_comment", + "line", + "line_end", + "line_start", + "lineno", + "make_html_tags", + "make_xml_tags", + "match_only_at_col", + "match_previous_expr", + "match_previous_literal", + "nested_expr", + "null_debug_action", + "nums", + "one_of", + "printables", + "punc8bit", + "python_style_comment", + "quoted_string", + "remove_quotes", + "replace_with", + "replace_html_entity", + "rest_of_line", + "sgl_quoted_string", + "srange", + "string_end", + "string_start", + "trace_parse_action", + "unicode_string", + "with_attribute", + "indentedBlock", + "original_text_for", + "ungroup", + "infix_notation", + "locatedExpr", + "with_class", + "CloseMatch", + "token_map", + "pyparsing_common", + "pyparsing_unicode", + "unicode_set", + "condition_as_parse_action", + "pyparsing_test", + # pre-PEP8 compatibility names "anyCloseTag", "anyOpenTag", "cStyleComment", - "col", "commonHTMLEntity", "countedArray", "cppStyleComment", @@ -200,14 +257,10 @@ __all__ = [ "dblSlashComment", "delimitedList", "dictOf", - "empty", - "hexnums", "htmlComment", "javaStyleComment", - "line", "lineEnd", "lineStart", - "lineno", "makeHTMLTags", "makeXMLTags", "matchOnlyAtCol", @@ -215,11 +268,8 @@ __all__ = [ "matchPreviousLiteral", "nestedExpr", "nullDebugAction", - "nums", "oneOf", "opAssoc", - "printables", - "punc8bit", "pythonStyleComment", "quotedString", "removeQuotes", @@ -227,7 +277,6 @@ __all__ = [ "replaceWith", "restOfLine", "sglQuotedString", - "srange", "stringEnd", "stringStart", "traceParseAction", @@ -235,15 +284,9 @@ __all__ = [ "withAttribute", "indentedBlock", "originalTextFor", - "ungroup", "infixNotation", "locatedExpr", "withClass", - "CloseMatch", "tokenMap", - "pyparsing_common", - "pyparsing_unicode", - "unicode_set", "conditionAsParseAction", - "pyparsing_test", ] diff --git a/pyparsing/common.py b/pyparsing/common.py index abafcf3..b85e65a 100644 --- a/pyparsing/common.py +++ b/pyparsing/common.py @@ -1,6 +1,6 @@ # common.py from .core import * -from .helpers import delimitedList, anyOpenTag, anyCloseTag +from .helpers import delimited_list, any_open_tag, any_close_tag from datetime import datetime # some other useful expressions - using lower-case class name since we are really using this as a namespace @@ -149,52 +149,54 @@ class pyparsing_common: [UUID('12345678-1234-5678-1234-567812345678')] """ - convertToInteger = tokenMap(int) + convert_to_integer = token_map(int) """ Parse action for converting parsed integers to Python int """ - convertToFloat = tokenMap(float) + convert_to_float = token_map(float) """ Parse action for converting parsed numbers to Python float """ - integer = Word(nums).setName("integer").setParseAction(convertToInteger) + integer = Word(nums).set_name("integer").set_parse_action(convert_to_integer) """expression that parses an unsigned integer, returns an int""" - hex_integer = Word(hexnums).setName("hex integer").setParseAction(tokenMap(int, 16)) + hex_integer = ( + Word(hexnums).set_name("hex integer").set_parse_action(token_map(int, 16)) + ) """expression that parses a hexadecimal integer, returns an int""" signed_integer = ( - Regex(r"[+-]?\d+").setName("signed integer").setParseAction(convertToInteger) + Regex(r"[+-]?\d+").set_name("signed integer").set_parse_action(convert_to_integer) ) """expression that parses an integer with optional leading sign, returns an int""" fraction = ( - signed_integer().setParseAction(convertToFloat) + signed_integer().set_parse_action(convert_to_float) + "/" - + signed_integer().setParseAction(convertToFloat) - ).setName("fraction") + + signed_integer().set_parse_action(convert_to_float) + ).set_name("fraction") """fractional expression of an integer divided by an integer, returns a float""" - fraction.addParseAction(lambda t: t[0] / t[-1]) + fraction.add_parse_action(lambda t: t[0] / t[-1]) mixed_integer = ( fraction | signed_integer + Optional(Optional("-").suppress() + fraction) - ).setName("fraction or mixed integer-fraction") + ).set_name("fraction or mixed integer-fraction") """mixed integer of the form 'integer - fraction', with optional leading integer, returns float""" - mixed_integer.addParseAction(sum) + mixed_integer.add_parse_action(sum) real = ( Regex(r"[+-]?(?:\d+\.\d*|\.\d+)") - .setName("real number") - .setParseAction(convertToFloat) + .set_name("real number") + .set_parse_action(convert_to_float) ) """expression that parses a floating point number and returns a float""" sci_real = ( Regex(r"[+-]?(?:\d+(?:[eE][+-]?\d+)|(?:\d+\.\d*|\.\d+)(?:[eE][+-]?\d+)?)") - .setName("real number with scientific notation") - .setParseAction(convertToFloat) + .set_name("real number with scientific notation") + .set_parse_action(convert_to_float) ) """expression that parses a floating point number with optional scientific notation and returns a float""" @@ -205,46 +207,46 @@ class pyparsing_common: fnumber = ( Regex(r"[+-]?\d+\.?\d*([eE][+-]?\d+)?") - .setName("fnumber") - .setParseAction(convertToFloat) + .set_name("fnumber") + .setParseAction(convert_to_float) ) """any int or real number, returned as float""" - identifier = Word(alphas + "_", alphanums + "_").setName("identifier") + identifier = Word(alphas + "_", alphanums + "_").set_name("identifier") """typical code identifier (leading alpha or '_', followed by 0 or more alphas, nums, or '_')""" ipv4_address = Regex( r"(25[0-5]|2[0-4][0-9]|1?[0-9]{1,2})(\.(25[0-5]|2[0-4][0-9]|1?[0-9]{1,2})){3}" - ).setName("IPv4 address") + ).set_name("IPv4 address") "IPv4 address (``0.0.0.0 - 255.255.255.255``)" - _ipv6_part = Regex(r"[0-9a-fA-F]{1,4}").setName("hex_integer") - _full_ipv6_address = (_ipv6_part + (":" + _ipv6_part) * 7).setName( + _ipv6_part = Regex(r"[0-9a-fA-F]{1,4}").set_name("hex_integer") + _full_ipv6_address = (_ipv6_part + (":" + _ipv6_part) * 7).set_name( "full IPv6 address" ) _short_ipv6_address = ( Optional(_ipv6_part + (":" + _ipv6_part) * (0, 6)) + "::" + Optional(_ipv6_part + (":" + _ipv6_part) * (0, 6)) - ).setName("short IPv6 address") - _short_ipv6_address.addCondition( + ).set_name("short IPv6 address") + _short_ipv6_address.add_condition( lambda t: sum(1 for tt in t if pyparsing_common._ipv6_part.matches(tt)) < 8 ) - _mixed_ipv6_address = ("::ffff:" + ipv4_address).setName("mixed IPv6 address") + _mixed_ipv6_address = ("::ffff:" + ipv4_address).set_name("mixed IPv6 address") ipv6_address = Combine( - (_full_ipv6_address | _mixed_ipv6_address | _short_ipv6_address).setName( + (_full_ipv6_address | _mixed_ipv6_address | _short_ipv6_address).set_name( "IPv6 address" ) - ).setName("IPv6 address") + ).set_name("IPv6 address") "IPv6 address (long, short, or mixed form)" mac_address = Regex( r"[0-9a-fA-F]{2}([:.-])[0-9a-fA-F]{2}(?:\1[0-9a-fA-F]{2}){4}" - ).setName("MAC address") + ).set_name("MAC address") "MAC address xx:xx:xx:xx:xx (may also have '-' or '.' delimiters)" @staticmethod - def convertToDate(fmt="%Y-%m-%d"): + def convert_to_date(fmt="%Y-%m-%d"): """ Helper to create a parse action for converting parsed date string to Python datetime.date @@ -271,7 +273,7 @@ class pyparsing_common: return cvt_fn @staticmethod - def convertToDatetime(fmt="%Y-%m-%dT%H:%M:%S.%f"): + def convert_to_datetime(fmt="%Y-%m-%dT%H:%M:%S.%f"): """Helper to create a parse action for converting parsed datetime string to Python datetime.datetime @@ -299,21 +301,21 @@ class pyparsing_common: iso8601_date = Regex( r"(?P<year>\d{4})(?:-(?P<month>\d\d)(?:-(?P<day>\d\d))?)?" - ).setName("ISO8601 date") + ).set_name("ISO8601 date") "ISO8601 date (``yyyy-mm-dd``)" iso8601_datetime = Regex( r"(?P<year>\d{4})-(?P<month>\d\d)-(?P<day>\d\d)[T ](?P<hour>\d\d):(?P<minute>\d\d)(:(?P<second>\d\d(\.\d*)?)?)?(?P<tz>Z|[+-]\d\d:?\d\d)?" - ).setName("ISO8601 datetime") + ).set_name("ISO8601 datetime") "ISO8601 datetime (``yyyy-mm-ddThh:mm:ss.s(Z|+-00:00)``) - trailing seconds, milliseconds, and timezone optional; accepts separating ``'T'`` or ``' '``" - uuid = Regex(r"[0-9a-fA-F]{8}(-[0-9a-fA-F]{4}){3}-[0-9a-fA-F]{12}").setName("UUID") + uuid = Regex(r"[0-9a-fA-F]{8}(-[0-9a-fA-F]{4}){3}-[0-9a-fA-F]{12}").set_name("UUID") "UUID (``xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx``)" - _html_stripper = anyOpenTag.suppress() | anyCloseTag.suppress() + _html_stripper = any_open_tag.suppress() | any_close_tag.suppress() @staticmethod - def stripHTMLTags(s, l, tokens): + def strip_html_tags(s, l, tokens): """Parse action to remove HTML tags from web page HTML source Example:: @@ -340,19 +342,28 @@ class pyparsing_common: ) ) .streamline() - .setName("commaItem") + .set_name("commaItem") ) - comma_separated_list = delimitedList( + comma_separated_list = delimited_list( Optional(quotedString.copy() | _commasepitem, default="") - ).setName("comma separated list") + ).set_name("comma separated list") """Predefined expression of 1 or more printable words or quoted strin gs, separated by commas.""" - upcaseTokens = staticmethod(tokenMap(lambda t: t.upper())) + upcase_tokens = staticmethod(token_map(lambda t: t.upper())) """Parse action to convert tokens to upper case.""" - downcaseTokens = staticmethod(tokenMap(lambda t: t.lower())) + downcase_tokens = staticmethod(token_map(lambda t: t.lower())) """Parse action to convert tokens to lower case.""" + # pre-PEP8 compatibility names + convertToInteger = convert_to_integer + convertToFloat = convert_to_float + convertToDate = convert_to_date + convertToDatetime = convert_to_datetime + stripHTMLTags = strip_html_tags + upcaseTokens = upcase_tokens + downcaseTokens = downcase_tokens + _builtin_exprs = [ v for v in vars(pyparsing_common).values() if isinstance(v, ParserElement) diff --git a/pyparsing/core.py b/pyparsing/core.py index 519f8de..660251a 100644 --- a/pyparsing/core.py +++ b/pyparsing/core.py @@ -4882,7 +4882,7 @@ class Suppress(TokenConverter): return self -def traceParseAction(f): +def trace_parse_action(f): """Decorator for debugging parse actions. When the parse action is called, this decorator will print @@ -4931,10 +4931,10 @@ def traceParseAction(f): # convenience constants for positional expressions empty = Empty().set_name("empty") -lineStart = LineStart().set_name("lineStart") -lineEnd = LineEnd().set_name("lineEnd") -stringStart = StringStart().set_name("stringStart") -stringEnd = StringEnd().set_name("stringEnd") +line_start = LineStart().set_name("line_start") +line_end = LineEnd().set_name("line_end") +string_start = StringStart().set_name("string_start") +string_end = StringEnd().set_name("string_end") _escapedPunc = Word(_bslash, r"\[]-*.$+^?()~ ", exact=2).set_parse_action( lambda s, l, t: t[0][1] @@ -4994,7 +4994,7 @@ def srange(s): return "" -def tokenMap(func, *args): +def token_map(func, *args): """Helper to define a parse action by mapping a function to all elements of a :class:`ParseResults` list. If any additional args are passed, they are forwarded to the given function as additional arguments @@ -5064,9 +5064,15 @@ punc8bit = srange(r"[\0xa1-\0xbf\0xd7\0xf7]") _builtin_exprs = [v for v in vars().values() if isinstance(v, ParserElement)] # backward compatibility names +tokenMap = token_map conditionAsParseAction = condition_as_parse_action nullDebugAction = null_debug_action sglQuotedString = sgl_quoted_string dblQuotedString = dbl_quoted_string quotedString = quoted_string unicodeString = unicode_string +lineStart = line_start +lineEnd = line_end +stringStart = string_start +stringEnd = string_end +traceParseAction = trace_parse_action |