Skip to content

Commit f422f9b

Browse files
committed
Better parse resolution in the face of PyMathics`
1 parent e706650 commit f422f9b

File tree

2 files changed

+40
-11
lines changed

2 files changed

+40
-11
lines changed

mathics/builtin/base.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ def check_options(options_to_check, evaluation):
150150
pattern = pattern % {"name": name}
151151
pattern = parse_builtin_rule(pattern, definition_class)
152152
replace = replace % {"name": name}
153+
# FIXME: Should system=True be system=not is_pymodule ?
153154
rules.append(Rule(pattern, parse_builtin_rule(replace), system=True))
154155

155156
box_rules = []

mathics/core/parser/util.py

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,33 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33

4+
from typing import Any, FrozenSet, Tuple
5+
46
from mathics.core.parser.parser import Parser
57
from mathics.core.parser.convert import convert
68
from mathics.core.parser.feed import SingleLineFeeder
79
from mathics.core.expression import ensure_context
810

9-
1011
parser = Parser()
1112

12-
def parse(definitions, feeder):
13-
'''
13+
14+
def parse(definitions, feeder) -> Any:
15+
"""
1416
Parse input (from the frontend, -e, input files, ToExpression etc).
1517
Look up symbols according to the Definitions instance supplied.
1618
1719
Feeder must implement the feed and empty methods, see core/parser/feed.py.
18-
'''
20+
"""
1921
return parse_returning_code(definitions, feeder)[0]
2022

2123

22-
def parse_returning_code(definitions, feeder):
23-
'''
24+
def parse_returning_code(definitions, feeder) -> Tuple[Any, str]:
25+
"""
2426
Parse input (from the frontend, -e, input files, ToExpression etc).
2527
Look up symbols according to the Definitions instance supplied.
2628
2729
Feeder must implement the feed and empty methods, see core/parser/feed.py.
28-
'''
30+
"""
2931
ast = parser.parse(feeder)
3032
source_code = parser.tokeniser.code if hasattr(parser.tokeniser, "code") else ""
3133
if ast is not None:
@@ -39,24 +41,50 @@ class SystemDefinitions(object):
3941
Dummy Definitions object that puts every unqualified symbol in
4042
System`.
4143
"""
44+
4245
def lookup_name(self, name):
4346
assert isinstance(name, str)
4447
return ensure_context(name)
4548

4649

50+
# FIXME: there has to be a better way, to get this
51+
# from the current System list.
52+
# For now we'll hack these in and figure this out
53+
# later
54+
SYSTEM_LIST: FrozenSet[str] = frozenset(
55+
[
56+
"Alternatives",
57+
"Complex",
58+
"Integer",
59+
"List",
60+
"MachineReal",
61+
"Number",
62+
"OptionsPattern",
63+
"PrecisionReal",
64+
"Real",
65+
"String",
66+
"StringExpression",
67+
"Symbol",
68+
]
69+
)
70+
71+
4772
class PyMathicsDefinitions(object):
4873
"""
4974
Dummy Definitions object that puts every unqualified symbol in
5075
PyMathics`.
5176
"""
77+
5278
def lookup_name(self, name):
5379
assert isinstance(name, str)
54-
return ensure_context(name, "PyMathics`")
80+
context = "System`" if name in SYSTEM_LIST else "PyMathics`"
81+
# print("XXX", name, context)
82+
return ensure_context(name, context)
5583

5684

5785
def parse_builtin_rule(string, definitions=SystemDefinitions()):
58-
'''
86+
"""
5987
Parse rules specified in builtin docstrings/attributes. Every symbol
6088
in the input is created in the System` context.
61-
'''
62-
return parse(definitions, SingleLineFeeder(string, '<builtin_rules>'))
89+
"""
90+
return parse(definitions, SingleLineFeeder(string, "<builtin_rules>"))

0 commit comments

Comments
 (0)