@@ -1817,8 +1817,11 @@ def __init__(self):
1817
1817
def set_names_and_parse_actions ():
1818
1818
for key , val in vars (p ).items ():
1819
1819
if not key .startswith ('_' ):
1820
- # Set names on everything -- very useful for debugging
1821
- val .setName (key )
1820
+ # Set names on (almost) everything -- very useful for debugging
1821
+ # token, placeable, and auto_delim are forward references which
1822
+ # are left without names to ensure useful error messages
1823
+ if key not in ("token" , "placeable" , "auto_delim" ):
1824
+ val .setName (key )
1822
1825
# Set actions
1823
1826
if hasattr (self , key ):
1824
1827
val .setParseAction (getattr (self , key ))
@@ -1855,63 +1858,39 @@ def csnames(group, names):
1855
1858
p .unknown_symbol = Regex (r"\\[A-Za-z]*" )("name" )
1856
1859
1857
1860
p .font = csnames ("font" , self ._fontnames )
1858
- p .start_group = (
1859
- Optional (r"\math" + oneOf (self ._fontnames )("font" )) + "{" )
1861
+ p .start_group = Optional (r"\math" + oneOf (self ._fontnames )("font" )) + "{"
1860
1862
p .end_group = Literal ("}" )
1861
1863
1862
1864
p .delim = oneOf (self ._delims )
1863
1865
1864
- set_names_and_parse_actions () # for root definitions.
1865
-
1866
1866
# Mutually recursive definitions. (Minimizing the number of Forward
1867
1867
# elements is important for speed.)
1868
- p .accent = Forward ()
1869
1868
p .auto_delim = Forward ()
1870
- p .binom = Forward ()
1871
- p .customspace = Forward ()
1872
- p .frac = Forward ()
1873
- p .dfrac = Forward ()
1874
- p .function = Forward ()
1875
- p .genfrac = Forward ()
1876
- p .group = Forward ()
1877
- p .operatorname = Forward ()
1878
- p .overline = Forward ()
1879
- p .overset = Forward ()
1880
1869
p .placeable = Forward ()
1881
1870
p .required_group = Forward ()
1882
- p .simple = Forward ()
1883
1871
p .optional_group = Forward ()
1884
- p .sqrt = Forward ()
1885
- p .subsuper = Forward ()
1886
- p .text = Forward ()
1887
1872
p .token = Forward ()
1888
- p .underset = Forward ()
1889
1873
1890
1874
set_names_and_parse_actions () # for mutually recursive definitions.
1891
1875
1892
- p .customspace <<= cmd (r"\hspace" , "{" + p .float_literal ("space" ) + "}" )
1876
+ p .optional_group <<= "{" + ZeroOrMore (p .token )("group" ) + "}"
1877
+ p .required_group <<= "{" + OneOrMore (p .token )("group" ) + "}"
1893
1878
1894
- p .accent <<= (
1879
+ p .customspace = cmd (r"\hspace" , "{" + p .float_literal ("space" ) + "}" )
1880
+
1881
+ p .accent = (
1895
1882
csnames ("accent" , [* self ._accent_map , * self ._wide_accents ])
1896
1883
- p .placeable ("sym" ))
1897
1884
1898
- p .function <<= csnames ("name" , self ._function_names )
1899
- p .operatorname <<= cmd (
1900
- r"\operatorname" , "{" + ZeroOrMore (p .simple )("name" ) + "}" )
1885
+ p .function = csnames ("name" , self ._function_names )
1901
1886
1902
- p .group << = p .start_group + ZeroOrMore (p .token )("group" ) + p .end_group
1887
+ p .group = p .start_group + ZeroOrMore (p .token )("group" ) + p .end_group
1903
1888
1904
- p .optional_group <<= "{" + ZeroOrMore (p .token )("group" ) + "}"
1905
- p .required_group <<= "{" + OneOrMore (p .token )("group" ) + "}"
1906
-
1907
- p .frac <<= cmd (
1908
- r"\frac" , p .required_group ("num" ) + p .required_group ("den" ))
1909
- p .dfrac <<= cmd (
1910
- r"\dfrac" , p .required_group ("num" ) + p .required_group ("den" ))
1911
- p .binom <<= cmd (
1912
- r"\binom" , p .required_group ("num" ) + p .required_group ("den" ))
1889
+ p .frac = cmd (r"\frac" , p .required_group ("num" ) + p .required_group ("den" ))
1890
+ p .dfrac = cmd (r"\dfrac" , p .required_group ("num" ) + p .required_group ("den" ))
1891
+ p .binom = cmd (r"\binom" , p .required_group ("num" ) + p .required_group ("den" ))
1913
1892
1914
- p .genfrac << = cmd (
1893
+ p .genfrac = cmd (
1915
1894
r"\genfrac" ,
1916
1895
"{" + Optional (p .delim )("ldelim" ) + "}"
1917
1896
+ "{" + Optional (p .delim )("rdelim" ) + "}"
@@ -1920,21 +1899,39 @@ def csnames(group, names):
1920
1899
+ p .required_group ("num" )
1921
1900
+ p .required_group ("den" ))
1922
1901
1923
- p .sqrt << = cmd (
1902
+ p .sqrt = cmd (
1924
1903
r"\sqrt{value}" ,
1925
1904
Optional ("[" + OneOrMore (NotAny ("]" ) + p .token )("root" ) + "]" )
1926
1905
+ p .required_group ("value" ))
1927
1906
1928
- p .overline << = cmd (r"\overline" , p .required_group ("body" ))
1907
+ p .overline = cmd (r"\overline" , p .required_group ("body" ))
1929
1908
1930
- p .overset << = cmd (
1909
+ p .overset = cmd (
1931
1910
r"\overset" ,
1932
1911
p .optional_group ("annotation" ) + p .optional_group ("body" ))
1933
- p .underset << = cmd (
1912
+ p .underset = cmd (
1934
1913
r"\underset" ,
1935
1914
p .optional_group ("annotation" ) + p .optional_group ("body" ))
1936
1915
1937
- p .text <<= cmd (r"\text" , QuotedString ('{' , '\\ ' , endQuoteChar = "}" ))
1916
+ p .text = cmd (r"\text" , QuotedString ('{' , '\\ ' , endQuoteChar = "}" ))
1917
+
1918
+ p .subsuper = (
1919
+ (Optional (p .placeable )("nucleus" )
1920
+ + OneOrMore (oneOf (["_" , "^" ]) - p .placeable )("subsuper" )
1921
+ + Regex ("'*" )("apostrophes" ))
1922
+ | Regex ("'+" )("apostrophes" )
1923
+ | (p .placeable ("nucleus" ) + Regex ("'*" )("apostrophes" ))
1924
+ )
1925
+
1926
+ p .simple = p .space | p .customspace | p .font | p .subsuper
1927
+
1928
+ p .token <<= (
1929
+ p .simple
1930
+ | p .auto_delim
1931
+ | p .unknown_symbol # Must be last
1932
+ )
1933
+
1934
+ p .operatorname = cmd (r"\operatorname" , "{" + ZeroOrMore (p .simple )("name" ) + "}" )
1938
1935
1939
1936
p .placeable <<= (
1940
1937
p .accent # Must be before symbol as all accents are symbols
@@ -1954,27 +1951,6 @@ def csnames(group, names):
1954
1951
| p .text
1955
1952
)
1956
1953
1957
- p .simple <<= (
1958
- p .space
1959
- | p .customspace
1960
- | p .font
1961
- | p .subsuper
1962
- )
1963
-
1964
- p .subsuper <<= (
1965
- (Optional (p .placeable )("nucleus" )
1966
- + OneOrMore (oneOf (["_" , "^" ]) - p .placeable )("subsuper" )
1967
- + Regex ("'*" )("apostrophes" ))
1968
- | Regex ("'+" )("apostrophes" )
1969
- | (p .placeable ("nucleus" ) + Regex ("'*" )("apostrophes" ))
1970
- )
1971
-
1972
- p .token <<= (
1973
- p .simple
1974
- | p .auto_delim
1975
- | p .unknown_symbol # Must be last
1976
- )
1977
-
1978
1954
p .auto_delim <<= (
1979
1955
r"\left" - (p .delim ("left" ) | Error ("Expected a delimiter" ))
1980
1956
+ ZeroOrMore (p .simple | p .auto_delim )("mid" )
0 commit comments