@@ -1802,8 +1802,11 @@ def __init__(self):
1802
1802
def set_names_and_parse_actions ():
1803
1803
for key , val in vars (p ).items ():
1804
1804
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 )
1807
1810
# Set actions
1808
1811
if hasattr (self , key ):
1809
1812
val .setParseAction (getattr (self , key ))
@@ -1840,63 +1843,39 @@ def csnames(group, names):
1840
1843
p .unknown_symbol = Regex (r"\\[A-Za-z]*" )("name" )
1841
1844
1842
1845
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" )) + "{"
1845
1847
p .end_group = Literal ("}" )
1846
1848
1847
1849
p .delim = oneOf (self ._delims )
1848
1850
1849
- set_names_and_parse_actions () # for root definitions.
1850
-
1851
1851
# Mutually recursive definitions. (Minimizing the number of Forward
1852
1852
# elements is important for speed.)
1853
- p .accent = Forward ()
1854
1853
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 ()
1865
1854
p .placeable = Forward ()
1866
1855
p .required_group = Forward ()
1867
- p .simple = Forward ()
1868
1856
p .optional_group = Forward ()
1869
- p .sqrt = Forward ()
1870
- p .subsuper = Forward ()
1871
1857
p .token = Forward ()
1872
- p .underset = Forward ()
1873
1858
1874
1859
set_names_and_parse_actions () # for mutually recursive definitions.
1875
1860
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" ) + "}"
1877
1863
1878
- p .accent <<= (
1864
+ p .customspace = cmd (r"\hspace" , "{" + p .float_literal ("space" ) + "}" )
1865
+
1866
+ p .accent = (
1879
1867
csnames ("accent" , [* self ._accent_map , * self ._wide_accents ])
1880
1868
- p .placeable ("sym" ))
1881
1869
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 )
1886
1871
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
1888
1873
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" ))
1898
1877
1899
- p .genfrac << = cmd (
1878
+ p .genfrac = cmd (
1900
1879
r"\genfrac" ,
1901
1880
"{" + Optional (p .delim )("ldelim" ) + "}"
1902
1881
+ "{" + Optional (p .delim )("rdelim" ) + "}"
@@ -1905,20 +1884,38 @@ def csnames(group, names):
1905
1884
+ p .required_group ("num" )
1906
1885
+ p .required_group ("den" ))
1907
1886
1908
- p .sqrt << = cmd (
1887
+ p .sqrt = cmd (
1909
1888
r"\sqrt{value}" ,
1910
1889
Optional ("[" + OneOrMore (NotAny ("]" ) + p .token )("root" ) + "]" )
1911
1890
+ p .required_group ("value" ))
1912
1891
1913
- p .overline << = cmd (r"\overline" , p .required_group ("body" ))
1892
+ p .overline = cmd (r"\overline" , p .required_group ("body" ))
1914
1893
1915
- p .overset << = cmd (
1894
+ p .overset = cmd (
1916
1895
r"\overset" ,
1917
1896
p .optional_group ("annotation" ) + p .optional_group ("body" ))
1918
- p .underset << = cmd (
1897
+ p .underset = cmd (
1919
1898
r"\underset" ,
1920
1899
p .optional_group ("annotation" ) + p .optional_group ("body" ))
1921
1900
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
+
1922
1919
p .placeable <<= (
1923
1920
p .accent # Must be before symbol as all accents are symbols
1924
1921
| p .symbol # Must be second to catch all named symbols and single
@@ -1936,27 +1933,6 @@ def csnames(group, names):
1936
1933
| p .overline
1937
1934
)
1938
1935
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
-
1960
1936
p .auto_delim <<= (
1961
1937
r"\left" - (p .delim ("left" ) | Error ("Expected a delimiter" ))
1962
1938
+ ZeroOrMore (p .simple | p .auto_delim )("mid" )
0 commit comments