Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add lastError function #3654

Open
wants to merge 7 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion M2/Macaulay2/d/actors5.d
Original file line number Diff line number Diff line change
Expand Up @@ -1813,6 +1813,9 @@ export StandardE := Expr(StandardS);
export topLevelMode := Expr(StandardS);
topLevelModeS := dummySymbol;

threadLocal lastError := nullE;
lastErrorS := dummySymbol;

initialRandomSeed := zeroZZ;
initialRandomHeight := toInteger(10);

Expand Down Expand Up @@ -1847,7 +1850,8 @@ syms := SymbolSequence(
( handleInterruptsS = setupvar("handleInterrupts",toExpr(handleInterrupts)); handleInterruptsS ),
( printWidthS = setupvar("printWidth",toExpr(printWidth)); printWidthS ),
( notifyS = setupvar("notify",toExpr(notify)); notifyS ),
( topLevelModeS = setupvar("topLevelMode",topLevelMode); topLevelModeS )
( topLevelModeS = setupvar("topLevelMode",topLevelMode); topLevelModeS ),
( lastErrorS = setupvarThread("lastError", lastError); lastErrorS )
);

export setDebuggingMode(b:bool):void := (
Expand Down Expand Up @@ -1886,6 +1890,17 @@ export sethandleInterrupts(b:bool):void := (
handleInterruptsSetup(b);
setGlobalVariable(handleInterruptsS,toExpr(b));
);
setLastError(position:Position, message:string):void := (
if !(
message == returnMessage ||
message == continueMessage || message == continueMessageWithArg ||
message == stepMessage || message == stepMessageWithArg ||
message == breakMessage)
then (
lastError = seq(locate(position), toExpr(message));
setGlobalVariable(lastErrorS, lastError)));
setLastErrorpointer = setLastError;

threadLocal resetvars := (
-- These are the thread local variables that got re-initialized in tokens.d:
-- Actually, this is no good! If the user assigns to one of these variables, the "top level" version
Expand All @@ -1909,6 +1924,21 @@ store(e:Expr):Expr := ( -- called with (symbol,newvalue)
else when s.1
is Nothing do (
if sym === debuggerHookS then (debuggerHook = s.1; e)
else if sym === lastErrorS then (lastError = s.1; e)
else buildErrorPacket(msg))
is a:Sequence do (
if sym === lastErrorS then (
if length(a) == 2 then (
when a.0
is p:List
do (
if p.Class == filePositionClass then (
when a.1
is msg:stringCell do (lastError = s.1; e)
else WrongArgString(2))
else WrongArg(1, "a file position"))
else WrongArg(1, "a file position"))
else WrongNumArgs(2))
else buildErrorPacket(msg))
is b:Boolean do (
n := b.v;
Expand Down
16 changes: 5 additions & 11 deletions M2/Macaulay2/d/binding.d
Original file line number Diff line number Diff line change
Expand Up @@ -415,10 +415,12 @@ export makeSymbol(t:Token):Symbol := (
export makeErrorTree(e:ParseTree,message:string):void := (
HadError = true;
printErrorMessage(treePosition(e),message);
setLastErrorpointer(treePosition(e), message);
);
export makeErrorTree(e:Token,message:string):void := (
HadError = true;
printErrorMessage(e,message);
setLastErrorpointer(e.position, message);
);
makeSymbol(e:ParseTree,dictionary:Dictionary):void := (
when e
Expand Down Expand Up @@ -472,14 +474,8 @@ lookup(t:Token,forcedef:bool,thread:bool):void := (
is entry:Symbol do (
t.entry = entry;
if entry.position == tempPosition then entry.position = t.position;
if entry.flagLookup then (
printErrorMessage(t,"flagged symbol encountered");
HadError=true;
);
if thread && !entry.thread then (
printErrorMessage(t,"symbol already present, but not thread local");
HadError=true;
);
if entry.flagLookup then makeErrorTree(t,"flagged symbol encountered");
if thread && !entry.thread then makeErrorTree(t,"symbol already present, but not thread local");
)
else (
if forcedef
Expand All @@ -495,9 +491,7 @@ lookup(t:Token,forcedef:bool,thread:bool):void := (
t.dictionary = globalDictionary; -- undefined variables are defined as global
t.entry = makeSymbol(t.word,t.position,globalDictionary,thread,locallyCreated);
)
else (
printErrorMessage(t,"undefined symbol " + t.word.name);
HadError=true;))));
else makeErrorTree(t,"undefined symbol " + t.word.name))));
lookup(t:Token):void := lookup(t,true,false);
lookuponly(t:Token):void := lookup(t,false,false);
-----------------------------------------------------------------------------
Expand Down
17 changes: 12 additions & 5 deletions M2/Macaulay2/d/evaluate.d
Original file line number Diff line number Diff line change
Expand Up @@ -1343,8 +1343,9 @@ steppingFurther(c:Code):bool := steppingFlag && (

handleError(c:Code,e:Expr):Expr := (
when e is err:Error do (
if SuppressErrors then return e;
if err.message == returnMessage
p := codePosition(c);
if SuppressErrors
|| err.message == returnMessage
|| err.message == continueMessage || err.message == continueMessageWithArg
|| err.message == stepMessage || err.message == stepMessageWithArg
|| err.message == breakMessage
Expand All @@ -1353,10 +1354,9 @@ handleError(c:Code,e:Expr):Expr := (
then (
-- an error message that is really being used to transfer control must be passed up the line
-- the position is plugged in just in case it's unhandled
if err.position == dummyPosition then err.position = codePosition(c);
if err.position == dummyPosition then err.position = p;
return e;
);
p := codePosition(c);
clearAllFlags();
clearAlarm();
if p.loadDepth >= errorDepth && !err.position === p then (
Expand Down Expand Up @@ -1585,7 +1585,14 @@ export evalraw(c:Code):Expr := (
tmp)
else AngleBarList(r)
));
when e is Error do handleError(c,e) else e);
when e is Error
do (
f := handleError(c,e);
when f is err:Error
do setLastErrorpointer(err.position, err.message)
else nothing;
f)
else e);

export evalexcept(c:Code):Expr := (
-- printErrorMessage(codePosition(c),"--evaluating: "+present(tostring(c)));
Expand Down
74 changes: 41 additions & 33 deletions M2/Macaulay2/d/lex.d
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,20 @@ export install(name:string,word:Word):Word := (
foreach ch in name do node = install(node,int(ch));
node.word = word;
word);

-- setLastError defined in actors5.d
dummysetLastError(position:Position, message:string):void := nothing;
export setLastErrorpointer := dummysetLastError;

makeLexError(position:Position, message:string):void := (
printErrorMessage(position, message);
setLastErrorpointer(position, message);
empty(tokenbuf));

newPosition(file:PosFile, line:ushort, column:ushort):Position := Position(
-- [ beginning ] [ endpoint ] [ focus ]
file.filename, line, column, file.line, file.column, line, column, loadDepth);

recognize(file:PosFile):(null or Word) := (
i := 0;
state := baseLexNode;
Expand All @@ -98,7 +112,7 @@ recognize(file:PosFile):(null or Word) := (
is null do (
p := position(file);
getc(file);
printErrorMessage(p,"invalid character" );
makeLexError(p, "invalid character");
(null or Word)(NULL))
is word:Word do (
for length(word.name) do getc(file);
Expand All @@ -117,13 +131,14 @@ getstringslashes(o:PosFile):(null or Word) := ( -- /// ... ///
ch := getc(o);
if ch == ERROR then (
if !test(interruptedFlag)
then printErrorMessage(o.filename,line,column,"ERROR in string /// ... /// beginning here: " + o.file.errorMessage);
empty(tokenbuf);
then makeLexError(newPosition(o, line, column),
"ERROR in string /// ... /// beginning here: "
+ o.file.errorMessage);
return NULL;
);
if ch == EOF then (
printErrorMessage(o.filename,line,column,"EOF in string /// ... /// beginning here");
empty(tokenbuf);
makeLexError(newPosition(o, line, column),
"EOF in string /// ... /// beginning here");
return NULL;
);
-- this allows us to get 3,4,5,... slashes within the string by typing 4,6,8,... slashes
Expand Down Expand Up @@ -168,17 +183,16 @@ getstring(o:PosFile):(null or Word) := (
ch := getc(o);
if ch == ERROR then (
if !test(interruptedFlag)
then printErrorMessage(o.filename,line,column,
(if o.file.eof
then "reading beyond EOF in string beginning here: "
else "ERROR in string beginning here: ")
+ o.file.errorMessage);
empty(tokenbuf);
then makeLexError(newPosition(o, line, column),
(if o.file.eof
then "reading beyond EOF in string beginning here: "
else "ERROR in string beginning here: ")
+ o.file.errorMessage);
return NULL;
);
if ch == EOF then (
printErrorMessage(o.filename,line,column,"EOF in string beginning here");
empty(tokenbuf);
makeLexError(newPosition(o, line, column),
"EOF in string beginning here");
return NULL;
);
tokenbuf << char(ch);
Expand All @@ -187,9 +201,9 @@ getstring(o:PosFile):(null or Word) := (
hexcoming = hexcoming - 1;
)
else (
printErrorMessage(o.filename,line,column,"expected " +
tostring(hexcoming) + " more hex digit(s)");
empty(tokenbuf);
makeLexError(newPosition(o, line, column),
"expected " + tostring(hexcoming) +
" more hex digit(s)");
while true do (ch2 := getc(o); if ch2 == EOF || ch2 == ERROR || ch2 == int('\n') then return NULL;);
)
)
Expand All @@ -211,8 +225,8 @@ getstring(o:PosFile):(null or Word) := (
|| int('0') <= ch && ch < int('8')
then escaped = false
else (
empty(tokenbuf);
printErrorMessage(o.filename,line,column,"unknown escape sequence: \\" + char(ch));
makeLexError(newPosition(o, line, column),
"unknown escape sequence: \\" + char(ch));
while true do (ch2 := getc(o); if ch2 == EOF || ch2 == ERROR || ch2 == int('\n') then return NULL;);
);
)
Expand Down Expand Up @@ -285,23 +299,20 @@ export errorToken := Token(Word("-*error token*-",TCnone,hash_t(0),newParseinfo(
globalDictionary, -- should replace this by dummyDictionary, I think
dummySymbol,false);

newPosition(file:PosFile, line:ushort, column:ushort):Position := Position(
-- [ beginning ] [ endpoint ] [ focus ]
file.filename, line, column, file.line, file.column, line, column, loadDepth);

gettoken1(file:PosFile,sawNewline:bool):Token := (
-- warning : tokenbuf is static
while true do (
rc := skipwhite(file);
if rc == ERROR then return errorToken;
if rc == EOF then (
printErrorMessage(file.filename,swline,swcolumn,"EOF in block comment -* ... *- beginning here");
-- empty(tokenbuf);
makeLexError(newPosition(file, swline, swcolumn),
"EOF in block comment -* ... *- beginning here");
-- while true do (ch2 := getc(file); if ch2 == EOF || ch2 == ERROR || ch2 == int('\n') then break;);
return errorToken;
);
if rc == DEPRECATED then (
printErrorMessage(file.filename,swline,swcolumn,"encountered disabled block comment syntax {* ... *} beginning here");
makeLexError(newPosition(file, swline, swcolumn),
"encountered disabled block comment syntax {* ... *} beginning here");
return errorToken;
);
line := file.line;
Expand Down Expand Up @@ -371,8 +382,8 @@ gettoken1(file:PosFile,sawNewline:bool):Token := (
tokenbuf << char(getc(file));
while isdigit(peek(file)) do tokenbuf << char(getc(file)))
else (
printErrorMessage(position(file),"precision missing in floating point constant");
empty(tokenbuf);
makeLexError(position(file),
"precision missing in floating point constant");
return errorToken;
)
);
Expand All @@ -387,8 +398,8 @@ gettoken1(file:PosFile,sawNewline:bool):Token := (
while isdigit(peek(file)) do tokenbuf << char(getc(file));
)
else (
printErrorMessage(position(file),"exponent missing in floating point constant");
empty(tokenbuf);
makeLexError(position(file),
"exponent missing in floating point constant");
return errorToken;
)
);
Expand Down Expand Up @@ -419,10 +430,7 @@ gettoken1(file:PosFile,sawNewline:bool):Token := (
newPosition(file, line, column), globalDictionary, dummySymbol, sawNewline))
else (
when recognize(file)
is null do (
empty(tokenbuf);
return errorToken
)
is null do return errorToken
is word:Word do return Token(word,
newPosition(file, line, column), globalDictionary, dummySymbol, sawNewline))
)
Expand Down
Loading
Loading