Escaping substitution in strings
Posted: Tue Apr 08, 2008 7:04 am
I've been fiddling with cobra - its coming along great.
One thing that was bugging me is that while the [] syntax in strings for string expression substitution is very clean when you do want it,
when you dont, (but do want a '[' literal) your string starts to become unintelligible ....
the code for the displayed string gets obscured by either string catenation with ns'' or r'' syntax or various forms of
additional formatting or substitution
e.g.
I want a cleaner syntax like
I fiddled a bit with the Tokeniser to get some regexes and supporting code do what I wanted but I unfortunately couldnt get
something working for all valid cases (multiple mixed subst and non substitution in strings);
Either with modifying existing tokens or adding a whole lot of new tokens and munging the tokenising
behaviour to fit into the existing expression handling... redoing the string and substitution tokenising and expression parsing seemed to be the only way within
the existing structure and that is a bit excessive ( for me for the moment anyway)... so I resorted to a nasty little hack which seems to work fine;
If nothing else its a starting point - giving the desired effect in a perhaps non optimal way (:-)
Its not affected any of the existing tests and I have a new testfile (063-esc-string-subst) exercising the new escaped substitution behaviour.
i.e all the following compile and run
Will you take the patches for the changes ( for examination at least ) and the new test case?
If so how would you like them?
file attachment here? ( I thought there was a file upload option somewhere on here )
email to cobra mail addr?
ftp drop somewhere?
One thing that was bugging me is that while the [] syntax in strings for string expression substitution is very clean when you do want it,
when you dont, (but do want a '[' literal) your string starts to become unintelligible ....
the code for the displayed string gets obscured by either string catenation with ns'' or r'' syntax or various forms of
additional formatting or substitution
e.g.
- Code: Select all
a=99
print String.format('a={0}[a]]', c'[')
print 'a='+ ns'[' +'[a]]'
a1=String.format('{0}[a]]', ns'[')
print 'a=[a1]'
print 'a=[c'['][a]]'
# any of above to get 'a=[99]'
I want a cleaner syntax like
- Code: Select all
print 'a=\[[a]]'
I fiddled a bit with the Tokeniser to get some regexes and supporting code do what I wanted but I unfortunately couldnt get
something working for all valid cases (multiple mixed subst and non substitution in strings);
Either with modifying existing tokens or adding a whole lot of new tokens and munging the tokenising
behaviour to fit into the existing expression handling... redoing the string and substitution tokenising and expression parsing seemed to be the only way within
the existing structure and that is a bit excessive ( for me for the moment anyway)... so I resorted to a nasty little hack which seems to work fine;
If nothing else its a starting point - giving the desired effect in a perhaps non optimal way (:-)
Its not affected any of the existing tests and I have a new testfile (063-esc-string-subst) exercising the new escaped substitution behaviour.
i.e all the following compile and run
- Code: Select all
a=99
s0="a=[a]"
assert s0=="a=99"
s1="1:a=\[..[a]..]"
assert s1==r"1:a=[..99..]"
s2="2:a=\[[a]]"
assert s2==r"2:a=[99]"
s2a="2:a=\\[a]"
assert s2a==r"2:a=\99"
s3="3:a=\[xx: [a]]"
assert s3==r"3:a=[xx: 99]"
s4="4:a=\[33xx: [a]]"
assert s4==r"4:a=[33xx: 99]"
s4a='4:a=\[33xx: [a]]'
assert s4a==r'4:a=[33xx: 99]'
s5="4a:a=\[33xx] \[yy]: [a]]"
assert s5==r"4a:a=[33xx] [yy]: 99]"
s6="5:a=\[valueof(a)] \[b]"
assert s6==r"5:a=[valueof(a)] [b]"
s7="6:a=[a] a=\[..] [a] "
assert s7==r"6:a=99 a=[..] 99 "
s7a="7: \[a=][a]] bigjobs \[[a]] crivens"
assert s7a == r'7: [a=]99] bigjobs [99] crivens'
s8=ns"7:a=\[a][a]"
assert s8==r"7:a=[a][a]"
s9= r"8:a=\[[a]]"
assert s9==r"8:a=\[[a]]"
# xtn: \{ gets subst to '[' in \ expanded strings
s13='SUBST: a=\{[a]]'
assert s13==r'SUBST: a=[99]'
s13a=ns'a=\{[a]]'
assert s13a == r'a=[[a]]'
# except when it doesnt
s13b=r'a=\{[a]]'
assert s13b == r'a=\{[a]]'
b=ns"\\["
assert b==r'\['
assert b == r"\["
assert b == ns"\\\{"
assert b <> r'\\{'
b2="123 \[ 456"
assert b2==r"123 [ 456"
b2='123 \[ 456'
assert b2==r"123 [ 456"
b3=r'123 \[ 456'
assert b3==r"123 \[ 456"
Will you take the patches for the changes ( for examination at least ) and the new test case?
If so how would you like them?
file attachment here? ( I thought there was a file upload option somewhere on here )
email to cobra mail addr?
ftp drop somewhere?