Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find syntax error

When I evaluate a function in one cell, Mathematica says

ToExpression::notstrbox: {At Line = 6, the input was:,$Failed,InString[6]} 
is not a string or a box. ToExpression can only interpret strings or boxes 
as Mathematica input. >>

And the cell right edge turns red.

How do I find the error location?

like image 949
Robert H Avatar asked Jun 22 '11 00:06

Robert H


1 Answers

In reality, the kernel always sends the position of first syntax error in the input string to the FrontEnd (if this input string contains an error). It can be demonstrated with the following MathLink code:

In[32]:= link = LinkLaunch[First[$CommandLine] <> " -mathlink"];
LinkRead[link];
LinkWrite[link, EnterTextPacket["2+"]]
LinkRead[link]
LinkRead[link]
LinkRead[link]

Out[35]= MessagePacket[Syntax, "sntxi"]    
Out[36]= TextPacket["Syntax::sntxi: Incomplete expression; more input is needed.
 "]    
Out[37]= SyntaxPacket[5]

The integer in SyntaxPacket "indicates the position at which a syntax error was detected in the input line" according to the documentation. What is confusing at first is that this position in the case of the input line "2+" is obviously beyond the end of the input line. But it seems that in reality this position is counted for the InputForm of the input line which in this case is: "2+\n\n".

We can check how it works with $SyntaxHandler defined as follows:

In[41]:= link = LinkLaunch[First[$CommandLine] <> " -mathlink"];
LinkRead[link]
LinkWrite[link, 
 EnterTextPacket[
  "$SyntaxHandler=
      Function[{str,pos},
               Print["Input string: ",ToString[str,InputForm]];
               Print["Position of syntax error: ",pos];
               $Failed
      ];
  "]]
LinkRead[link]
LinkWrite[link, EnterTextPacket["2+"]]
While[Head[packet = LinkRead[link]] =!= InputNamePacket, 
 Print[packet]]; Print[packet]

Out[42]= InputNamePacket["In[1]:= "]    
Out[44]= InputNamePacket["In[2]:= "]    
During evaluation of In[41]:= MessagePacket[Syntax,sntxi]    
During evaluation of In[41]:= TextPacket[Syntax::sntxi: Incomplete expression; more input is needed.
]    
During evaluation of In[41]:= TextPacket[Input string: "2+\n\n"
]    
During evaluation of In[41]:= TextPacket[Position of syntax error: 6
]    
During evaluation of In[41]:= SyntaxPacket[5]    
During evaluation of In[41]:= InputNamePacket[In[2]:= ]

One can see an inconsistency between the positions of the same syntax error reported by SyntaxPacket and by $SyntaxHandler. But it seems possible to understand how they count the position: both use InputForm of the input line and the position before the input line has number 0 for $SyntaxHandler and number 1 in the case of SyntaxPacket. In this way we can define $SyntaxHandler for getting exact visual representation of the position of the syntax error inside of the input string (the input Cell must have "RawInputForm" style) as follows:

$SyntaxHandler = 
  Function[{str, pos}, 
   Print["Input string: ", ToString[str, InputForm], "\n", 
    "Position of syntax error: ", pos, "\n", 
    StringInsert[ToString[str, InputForm], 
     ToString[Style["\[DownArrowBar]", Red, Background -> Yellow], 
      StandardForm], pos + 2]]; $Failed];

I will stress again that input cell MUST have the "RawInputForm" style! Such cell may be created by creating ordinary empty input cell and then converting it to the "RawInputForm" cell by the appropriate command in the Cell -> Convert To menu.

Lets see how it works:

screenshot

The reason why we must use "RawInputForm" cell is probably that $SyntaxHandler is applied ony when the input is sent to the kernel in the form of a String, not in the form of Boxes as it happens with default StandardForm input cells.

like image 101
Alexey Popkov Avatar answered Oct 20 '22 17:10

Alexey Popkov