File: tag_emitter.py

package info (click to toggle)
pyparsing 3.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 12,200 kB
  • sloc: python: 30,867; ansic: 422; sh: 112; makefile: 24
file content (28 lines) | stat: -rw-r--r-- 968 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#
# tag_emitter.py
#
# Example showing how to inject tags into the parsed results by adding
# an Empty() with a parse action to return the desired added data.
#
# Copyright 2023, Paul McGuire
#
import pyparsing as pp

# define expressions to parse different forms of integer constants
# add parse actions that will evaluate the integer correctly
binary_int = ("0b" + pp.Word("01")).add_parse_action(lambda t: int(t[1], base=2))
hex_int = ("0x" + pp.Word(pp.hexnums)).add_parse_action(lambda t: int(t[1], base=16))
dec_int = pp.Word(pp.nums).add_parse_action(lambda t: int(t[0]))


# define a parser that includes the tag for each integer format type
int_parser = (binary_int("value") + pp.Tag("original_format", "binary")
              | hex_int("value") + pp.Tag("original_format", "hex")
              | dec_int("value") + pp.Tag("original_format", "decimal")
              )

# parse some integers
int_parser.run_tests("""\
    0b11011000001
    0x6c1
    1729""")