Using pyparsing
version 3.0.0a2
to generate some railroad diagrams I end up with sub-diagrams named Group or Unnamed 1, 2, etc. No need to says that several different expressions have the name Group
. How can we specify the wanted names for each expression?
Be sure that you are calling setName
on the significant expressions within your grammar.
This expression will show up as Unnamed:
# US social security numbers are of the form 000-00-0000
ssn = Word(nums, exact=3) + '-' + Word(nums, exact=2) + '-' + Word(nums, exact=4)
setName
attaches a name to the expression:
ssn = (Word(nums, exact=3)
+ '-' + Word(nums, exact=2)
+ '-' + Word(nums, exact=4)).setName("social_security_number")
Keep in mind the distinction between setName
and setResultsName
:
setName
attaches a name to the expression itself (a concept made more
clear now with the railroad diagram support)
setResultsName
designates a name to be attached to the parsed results
extracted by this expression
An expression will have just one name, such as Word(nums)
is an "integer". But that expression may be used in several places within a grammar, and so could have multiple results names. See how real
is used multiple times in this example:
real = Regex(r"\d+\.\d+").setName("real number")
lat_long = "(" + Char("NS") + real("latitude") + "," + Char("EW") + real("longitude") + ")"