@@ -1802,8 +1802,11 @@ def __init__(self):
18021802 def set_names_and_parse_actions ():
18031803 for key , val in vars (p ).items ():
18041804 if not key .startswith ('_' ):
1805- # Set names on everything -- very useful for debugging
1806- val .setName (key )
1805+ # Set names on (almost) everything -- very useful for debugging
1806+ # token, placeable, and auto_delim are forward references which
1807+ # are left without names to ensure useful error messages
1808+ if key not in ("token" , "placeable" , "auto_delim" ):
1809+ val .setName (key )
18071810 # Set actions
18081811 if hasattr (self , key ):
18091812 val .setParseAction (getattr (self , key ))
@@ -1840,63 +1843,39 @@ def csnames(group, names):
18401843 p .unknown_symbol = Regex (r"\\[A-Za-z]*" )("name" )
18411844
18421845 p .font = csnames ("font" , self ._fontnames )
1843- p .start_group = (
1844- Optional (r"\math" + oneOf (self ._fontnames )("font" )) + "{" )
1846+ p .start_group = Optional (r"\math" + oneOf (self ._fontnames )("font" )) + "{"
18451847 p .end_group = Literal ("}" )
18461848
18471849 p .delim = oneOf (self ._delims )
18481850
1849- set_names_and_parse_actions () # for root definitions.
1850-
18511851 # Mutually recursive definitions. (Minimizing the number of Forward
18521852 # elements is important for speed.)
1853- p .accent = Forward ()
18541853 p .auto_delim = Forward ()
1855- p .binom = Forward ()
1856- p .customspace = Forward ()
1857- p .frac = Forward ()
1858- p .dfrac = Forward ()
1859- p .function = Forward ()
1860- p .genfrac = Forward ()
1861- p .group = Forward ()
1862- p .operatorname = Forward ()
1863- p .overline = Forward ()
1864- p .overset = Forward ()
18651854 p .placeable = Forward ()
18661855 p .required_group = Forward ()
1867- p .simple = Forward ()
18681856 p .optional_group = Forward ()
1869- p .sqrt = Forward ()
1870- p .subsuper = Forward ()
18711857 p .token = Forward ()
1872- p .underset = Forward ()
18731858
18741859 set_names_and_parse_actions () # for mutually recursive definitions.
18751860
1876- p .customspace <<= cmd (r"\hspace" , "{" + p .float_literal ("space" ) + "}" )
1861+ p .optional_group <<= "{" + ZeroOrMore (p .token )("group" ) + "}"
1862+ p .required_group <<= "{" + OneOrMore (p .token )("group" ) + "}"
18771863
1878- p .accent <<= (
1864+ p .customspace = cmd (r"\hspace" , "{" + p .float_literal ("space" ) + "}" )
1865+
1866+ p .accent = (
18791867 csnames ("accent" , [* self ._accent_map , * self ._wide_accents ])
18801868 - p .placeable ("sym" ))
18811869
1882- p .function <<= csnames ("name" , self ._function_names )
1883- p .operatorname <<= cmd (
1884- r"\operatorname" ,
1885- "{" + ZeroOrMore (p .simple | p .unknown_symbol )("name" ) + "}" )
1870+ p .function = csnames ("name" , self ._function_names )
18861871
1887- p .group << = p .start_group + ZeroOrMore (p .token )("group" ) + p .end_group
1872+ p .group = p .start_group + ZeroOrMore (p .token )("group" ) + p .end_group
18881873
1889- p .optional_group <<= "{" + ZeroOrMore (p .token )("group" ) + "}"
1890- p .required_group <<= "{" + OneOrMore (p .token )("group" ) + "}"
1891-
1892- p .frac <<= cmd (
1893- r"\frac" , p .required_group ("num" ) + p .required_group ("den" ))
1894- p .dfrac <<= cmd (
1895- r"\dfrac" , p .required_group ("num" ) + p .required_group ("den" ))
1896- p .binom <<= cmd (
1897- r"\binom" , p .required_group ("num" ) + p .required_group ("den" ))
1874+ p .frac = cmd (r"\frac" , p .required_group ("num" ) + p .required_group ("den" ))
1875+ p .dfrac = cmd (r"\dfrac" , p .required_group ("num" ) + p .required_group ("den" ))
1876+ p .binom = cmd (r"\binom" , p .required_group ("num" ) + p .required_group ("den" ))
18981877
1899- p .genfrac << = cmd (
1878+ p .genfrac = cmd (
19001879 r"\genfrac" ,
19011880 "{" + Optional (p .delim )("ldelim" ) + "}"
19021881 + "{" + Optional (p .delim )("rdelim" ) + "}"
@@ -1905,20 +1884,38 @@ def csnames(group, names):
19051884 + p .required_group ("num" )
19061885 + p .required_group ("den" ))
19071886
1908- p .sqrt << = cmd (
1887+ p .sqrt = cmd (
19091888 r"\sqrt{value}" ,
19101889 Optional ("[" + OneOrMore (NotAny ("]" ) + p .token )("root" ) + "]" )
19111890 + p .required_group ("value" ))
19121891
1913- p .overline << = cmd (r"\overline" , p .required_group ("body" ))
1892+ p .overline = cmd (r"\overline" , p .required_group ("body" ))
19141893
1915- p .overset << = cmd (
1894+ p .overset = cmd (
19161895 r"\overset" ,
19171896 p .optional_group ("annotation" ) + p .optional_group ("body" ))
1918- p .underset << = cmd (
1897+ p .underset = cmd (
19191898 r"\underset" ,
19201899 p .optional_group ("annotation" ) + p .optional_group ("body" ))
19211900
1901+ p .subsuper = (
1902+ (Optional (p .placeable )("nucleus" )
1903+ + OneOrMore (oneOf (["_" , "^" ]) - p .placeable )("subsuper" )
1904+ + Regex ("'*" )("apostrophes" ))
1905+ | Regex ("'+" )("apostrophes" )
1906+ | (p .placeable ("nucleus" ) + Regex ("'*" )("apostrophes" ))
1907+ )
1908+
1909+ p .simple = p .space | p .customspace | p .font | p .subsuper
1910+
1911+ p .token <<= (
1912+ p .simple
1913+ | p .auto_delim
1914+ | p .unknown_symbol # Must be last
1915+ )
1916+
1917+ p .operatorname = cmd (r"\operatorname" , "{" + ZeroOrMore (p .simple )("name" ) + "}" )
1918+
19221919 p .placeable <<= (
19231920 p .accent # Must be before symbol as all accents are symbols
19241921 | p .symbol # Must be second to catch all named symbols and single
@@ -1936,27 +1933,6 @@ def csnames(group, names):
19361933 | p .overline
19371934 )
19381935
1939- p .simple <<= (
1940- p .space
1941- | p .customspace
1942- | p .font
1943- | p .subsuper
1944- )
1945-
1946- p .subsuper <<= (
1947- (Optional (p .placeable )("nucleus" )
1948- + OneOrMore (oneOf (["_" , "^" ]) - p .placeable )("subsuper" )
1949- + Regex ("'*" )("apostrophes" ))
1950- | Regex ("'+" )("apostrophes" )
1951- | (p .placeable ("nucleus" ) + Regex ("'*" )("apostrophes" ))
1952- )
1953-
1954- p .token <<= (
1955- p .simple
1956- | p .auto_delim
1957- | p .unknown_symbol # Must be last
1958- )
1959-
19601936 p .auto_delim <<= (
19611937 r"\left" - (p .delim ("left" ) | Error ("Expected a delimiter" ))
19621938 + ZeroOrMore (p .simple | p .auto_delim )("mid" )
0 commit comments