mathics / Mathics

This repository is for archival. Please see https://github.com/Mathics3/mathics-core
https://mathics.org
Other
2.07k stars 205 forks source link

Multi-line string parsing inside `ToExpression[x, InputForm]` #1245

Closed vsht closed 3 years ago

vsht commented 3 years ago

It appears that Mma is somewhat more fault tolerant when it comes to missing semicolons, cf. e.g. the following admittedly sloppy code

foo::usage =
"foo is a foo."

bar::usage =
"bar is a bar."

Begin["`Package`"]

End[]

Begin["`Private`"]

Options[foo] = {
  "FooOpt" ->  True
}

foo[x_]:=x^2;

Despite of the semicolons missing at many places, Mma obviously figures out that a CR accounts for a semicolon here. So it happily accepts

myFileAsString = Import["testFile.m", "Text"];   
ToExpression[myFileAsString] 

Mathics doesn't

In[1]:= myFileAsString = Import["testFile.m", "Text"];   
In[2]:= ToExpression[myFileAsString] 
SetDelayed::write: Tag Times in {FooOpt -> True} foo[x_] is Protected.
Set::write: Tag Times in bar is a bar. Begin[`Package`] End[] Begin[`Private`] Options[foo] is Protected.
Set::write: Tag Times in foo is a foo. bar::usage is Protected.
In[3]:= 

For compatibility with other packages it would be good to emulate Mma's behavior. Cf. also Issue #1174

testFile.zip

rocky commented 3 years ago

Thanks for the detailed report! I will look at when I can.

rocky commented 3 years ago

When getting down to the details this has nothing to do with parsing. You might tell this by entering those lines inside one of the command-line programs.

Instead it has everything to do with ToExpression when it gets a multiline string to be interpreted as InputForm.

I don't have installed any version of Wolfram products, I only go by what docs there are on wolfram.com. So I can't check how this matches the Wolfram Language.

The behavior we have right now in #1247 is that the value returned is the last complete expression when there are several expressions.