Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Notepad++ Python Script for De-Analyzing Nested Parentheses and Comma-Separated Arguments. Posted to https://sourceforge.net/p/notepad-plus/discussion/1290590/thread/c17ed459/
input_text = editor.getSelText()
EVENTS = 3
STATES = 2
state_table = [[[0,0] for x in range(EVENTS)] for x in range(STATES)]
event_list = [ "\"", "\n\r\t " ] # last event is anything that doesn't match one of the other actions
state_table[0] = [[1,0], [0,1], [0,0]] # normal state
state_table[1] = [[0,0], [1,0], [1,0]] # double-quote comment
output_text = ""
current_state = 0
for z in input_text:
for a in range(len(event_list)):
if z in event_list[a]:
takeaction = state_table[current_state][a][1]
current_state = state_table[current_state][a][0]
break
else:
takeaction = state_table[current_state][-1][1] # this sets takeaction to the value when the test character does not match any event
current_state = state_table[current_state][-1][0] # this sets current_state to the value when the test character does not match any event
if takeaction == 0:
output_text += z # just pass the characters along
elif takeaction == 1:
pass # strip whitespace from the input outside of quotes
else:
pass # should raise error, since the state table includes more actions than are defined
editor.replaceSel(output_text)
@DaneWeber
Copy link
Author

This pairs with my other script. You can just use a regex to do the same thing, but I went with this in case I discover new rules for Excel formula grammar that I need to handle, or if I want to handle a custom kind of commenting that this script should strip.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment