Sto implementando un interprete per un linguaggio di scripting dell'editor di testo obsoleto e sto avendo qualche problema a far funzionare correttamente un lexer.modo più efficiente per analizzare questo linguaggio di scripting
Ecco un esempio della parte problematica del linguaggio:
T
L /LOCATE ME/
C /LOCATE ME/CHANGED ME/ * *
C ;CHANGED ME;CHANGED ME AGAIN; 1 *
I /
personaggi sembrano citare le stringhe e anche agire come delimitatore per il comando C
(CHANGE
) in una sintassi sed
tipo, anche se consente qualsiasi carattere come delimitatore.
Probabilmente ho implementato circa la metà dei comandi più comuni, usando solo parse_tokens(line.split())
fino ad ora. Era veloce e sporco, ma funzionava sorprendentemente bene.
Per evitare di scrivere il mio lexer, ho provato shlex
.
Funziona abbastanza bene, tranne per i casi CHANGE
:
import shlex
def shlex_test(cmd_str):
lex = shlex.shlex(cmd_str)
lex.quotes = '/'
return list(lex)
print(shlex_test('L /spaced string/'))
# OK! gives: ['L', '/spaced string/']
print(shlex_test('C /spaced string/another string/ * *'))
# gives : ['C', '/spaced string/', 'another', 'string/', '*', '*']
# desired : any format that doesn't split on a space between /'s
print(shlex_test('C ;a b;b a;'))
# gives : ['C', ';', 'b', 'a', ';', 'a', 'b', ';']
# desired : same format as CHANGE command above
Qualcuno sa un modo semplice per ottenere questo risultato (con shlex
o altro)?
EDIT:
Se aiuta, ecco la CHANGE
comando di sintassi indicata nel file di aiuto:
'''
C [/stg1/stg2/ [n|n m]]
The CHANGE command replaces the m-th occurrence of "stg1" with "stg2"
for the next n lines. The default value for m and n is 1.'''
La similmente difficile tokenize X
e Y
comandi:
'''
X [/command/[command/[...]]n]
Y [/command/[command/[...]]n]
The X and Y commands allow the execution of several commands contained
in one command. To define an X or Y "command string", enter X (or Y)
followed by a space, then individual commands, each separated by a
delimiter (e.g. a period "."). An unlimited number of commands may be
placed in the X or Y command string. Once the command string has been
defined, entering X (or Y) followed optionally by a count n will execute
the defined command string n times. If n is not specified, it will
default to 1.'''
Avete accesso alla definizione della lingua? Se è così, una citazione dalla parte rilevante potrebbe essere utile a tutti noi. – Marcin
@Marcin Ho aggiunto alcune informazioni rilevanti dal file di aiuto, è tutta la documentazione che ho. –
Non conosco 'shlex', ma penso che' regex' [(re)] (http://docs.python.org/library/re.html) possa anche essere utile. – machaku