11#!/usr/bin/env python3
22# -*- coding: utf-8 -*-
33
4+ from typing import Any , FrozenSet , Tuple
5+
46from mathics .core .parser .parser import Parser
57from mathics .core .parser .convert import convert
68from mathics .core .parser .feed import SingleLineFeeder
79from mathics .core .expression import ensure_context
810
9-
1011parser = 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+
4772class 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
5785def 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