-
Notifications
You must be signed in to change notification settings - Fork 10
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
Milestone 5: Disallow Invalid Breakpoints #318
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c0e8bb2
Milestone 5: Disallow invalid breakpoints
NicholasRM 73f79b6
Regenerate documentation
NicholasRM 4ee8b97
Address previous code fragility
NicholasRM 116698d
Replace line breakpoint checking logic with incremental parsing
NicholasRM 63c4b80
Correctly passed functional mode parameter to MAD
NicholasRM File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,7 +48,7 @@ | |
########################################################################################### | ||
|
||
from os.path import exists, split, basename | ||
from asteroid.support import term2string, get_tail_term, term2verbose | ||
from asteroid.support import term2string, get_tail_term, term2verbose, find_function | ||
from asteroid.version import MAD_VERSION | ||
import copy | ||
|
||
|
@@ -283,10 +283,10 @@ def _handle_help(self,_): | |
print("next\t\t\t- step execution across a nested scope") | ||
print("print <name>[@<num>|<name>]+|* [-v]\t\t- print contents of <name>, * lists all vars in scope, recursively access (nested) objects with @, '-v' enables verbose printing of nested data") | ||
print("quit\t\t\t- quit debugger") | ||
print("set [<func>|<line#> [<file>]]\n\t\t\t- set a breakpoint") | ||
print("set [<func>|<line#> [<file>]]\n\t\t\t- set a breakpoint, breakpoints may only be set on valid statements on already loaded files") | ||
print("stack [<num>|* [-v]]\t\t\t- display runtime stack, list all items in specific frame with an index or all frames with '*', '-v' toggles verbose printing") | ||
print("step\t\t\t- step to next executable statement") | ||
print("trace [<num> [<num>]]\t\t\t- display runtime stack trace , display runtime stack trace, can specify either the first n frames or all of the frames between the start and end") | ||
print("trace [<num> [<num>]]\t\t\t- display runtime stack trace, display runtime stack trace, can specify either the first n frames or all of the frames between the start and end") | ||
print("up\t\t\t- move up one stack frame") | ||
print("where\t\t\t- print current program line") | ||
print() | ||
|
@@ -383,27 +383,80 @@ def _handle_quit(self,_): | |
raise SystemExit() | ||
|
||
def _handle_set(self,args): | ||
(file,lineno) = self.interp_state.lineinfo | ||
if len(args) == 0: | ||
# set a breakpoint at the current line | ||
(file,lineno) = self.interp_state.lineinfo | ||
self.line_breakpoints.append((lineno,file)) | ||
return START_DEBUGGER | ||
elif len(args) == 1: | ||
(file,_) = self.interp_state.lineinfo | ||
if args[0].isnumeric(): | ||
self.line_breakpoints.append((int(args[0]),file)) | ||
self._load_program_text(file) | ||
if args[0].isnumeric(): | ||
if self._validate_breakpoint_line(file, int(args[0])): | ||
self.line_breakpoints.append((int(args[0]),file)) | ||
else: | ||
self.function_breakpoints.append((args[0],file)) | ||
if self._validate_breakpoint_function(file, args[0]): | ||
self.function_breakpoints.append((args[0],file)) | ||
return START_DEBUGGER | ||
elif len(args) == 2: | ||
if args[0].isnumeric(): | ||
self.line_breakpoints.append((int(args[0]),args[1])) | ||
self._load_program_text(args[1]) | ||
if args[0].isnumeric(): | ||
if self._validate_breakpoint_line(args[1], int(args[0])): | ||
self.line_breakpoints.append((int(args[0]),args[1])) | ||
else: | ||
self.function_breakpoints.append((args[0],args[1])) | ||
if self._validate_breakpoint_function(args[1], args[0]): | ||
self.function_breakpoints.append((args[0],args[1])) | ||
return START_DEBUGGER | ||
else: | ||
print("error: too many arguments to set") | ||
return START_DEBUGGER | ||
|
||
def _validate_breakpoint_line(self, fname, lineno): | ||
# Load the correct file | ||
file_text = self.program_text[fname] | ||
# If the line is outside the current file, produce an error message and return | ||
if lineno > len(file_text): | ||
print("error: cannot place breakpoint on invalid lines") | ||
return False | ||
# Check if the line is empty | ||
line = file_text[lineno-1].strip() | ||
if len(line) == 0: | ||
print("error: cannot place breakpoints on blank lines") | ||
return False | ||
# If the line starts with any of these tokens, it is a valid statement and can accept a breakpoint | ||
for start in ['load', 'global', 'structure', 'let', 'loop', 'for', 'while', 'repeat', 'match', 'if', 'try', 'throw', 'break', 'return', 'function']: | ||
if len(line) >= len(start) and line[:len(start)] == start: | ||
return True | ||
# Get the lines above and below the current if they exist | ||
prev = file_text[lineno-2].strip() if lineno > 1 else None | ||
next = file_text[lineno].strip() if lineno < len(file_text) else None | ||
# If the line starts with any of these tokens, the line is in the middle of a statement or expression and cannot accept a breakpoint | ||
for start in ['system', 'as', 'with', 'end', 'do', 'in', 'until', 'elif', 'else', 'catch', '@', ',', '[', ']', '(', ')', '"', "'"]: | ||
if len(line) >= len(start) and line[:len(start)] == start: | ||
print("error: cannot place breakpoint inside a statement") | ||
return False | ||
# If the previous or next lines begins with any of these symbols, the current line is in the middle of a statement | ||
if prev: | ||
for start in ['@', ',', '[', '(', '"', "'"]: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see my comment above, having these collection arbitrarily inside the code makes the code fragile |
||
if len(prev) >= len(start) and prev[:len(start)] == start: | ||
print("error: cannot place breakpoint inside a statement") | ||
return False | ||
if next: | ||
for start in ['@', ',', ']', ')', '"', "'"]: | ||
if len(next) >= len(start) and next[:len(start)] == start: | ||
print("error: cannot place breakpoint inside a statement") | ||
return False | ||
# If it cannot be determined, assume the breakpoint is valid | ||
return True | ||
|
||
|
||
def _validate_breakpoint_function(self, fname, func_name): | ||
# Loop through every scope and check if the function was found | ||
loaded_syms = self.interp_state.symbol_table.scoped_symtab | ||
for scope in loaded_syms: | ||
for (sym, val) in scope.items(): | ||
if find_function(sym, val, fname, func_name): return True | ||
print("error: unable to find function '{}' in file '{}'".format(func_name, fname)) | ||
return False | ||
|
||
def _handle_frame(self,_): | ||
print("you are looking at frame #{}".format(self.frame_ix)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is very fragile, if the language evolves by introducing new tokens this will break and will prevent users from setting break points. Is there a way to do this in the lexer for example where it is natural for the user to extend collections when introducing new tokens. See the parser file where we define collection at the beginning.