| Cobra | Interactive Query Language | stretch | ||
|---|---|---|---|---|
| NAMEstretch — set a range, starting at marked tokens upto the pattern specifiedSYNTAXs[tretch] [top|up] pattern [pattern2] DESCRIPTIONThe command accepts a top or up qualifier, and one or two arguments. The command defines a range that starts at the current mark(s) and stretches to a token that matches the pattern(s) specified. For instance:: m goto # match gotos : n # move mark to the labelname : s $$ : # define a range from that name to the label definition # which is a match of the name only if followed by a colon # (this will succeed only for forward jumps)The $$ symbol is a reference to the text of the token that is marked at the start of the new range. If the literal text $$ must be matched at the start of a word, use: \$$. 
	: r		# reset
	: fcts		# match fct declarations
	: s {		# stretch to { and tag fct name to {
	: n {		# move to the {
	: c $$		# keep match if the range contains the function name
	: e \(		# and it is a true function call
As a side-effect of the stretch command, the symbol that is
associated with the current token (at the start of the new range) is
bound (tagged) to the token at the end of that range.
This allows us to move away from the current token to the
target of the stretch command and still match on the earlier token name.EXAMPLESA limited attempt to find cases where a controlling variable from a loop condition in a for statement is modified in the loop body:
	: m for		# find all for statements
	: n \(		# should be the next token
	: j		# move to matching )
	: e {		# keep match only the next token is {
	: b for		# move back to start of for (..)
	: n \;		# move the mark forward to the start of the for-condition
	: n		# move to the first symbol in that condition
	: m & @ident	# keep match only if this is an identifier
	: s {		# bind the current token to the {, which we know is there
	: n {		# move to the start of the loop body {
	: c $$ /[^!><=]*=
			# match for the bound symbol followed
			# by an assignment operator +=, -=, =, etc
	: >1
	: u; c $$ ++	# undo the last command, and try match followed by ++
	: <|1		# add the earlier matches
	: >1		# and update the set
	: u; c ++ $$	# undo, and try a prefix ++ followed by match
	: <|1		# and add back the earlier matches
	: d
To see what the difference is between qualifiers
top and up,
consider the following small experiment:
	$ echo "a { b { c } d } e" > /tmp/example
	$ cobra /tmp/example
	: m b; s top }; p * 10
	1 matches
	1 matches
	example:
	  1: >   1  { b { c } d } e 
	  1:          ^ ^ ^ ^       
	: r
	: m b; s up }; p * 10
	1 matches
	1 matches
	example:
	  1: >   1  { b { c } d } e 
	  1:          ^ ^ ^ ^ ^ ^   
	: r
	: m b; n; s top }; p * 10
	1 matches
	1 matches
	1 matches
	example:
	  1: >   1  { b { c } d } e 
	  1:            ^ ^ ^       
	: r
	: m b; n; s up }; p * 10
	1 matches
	1 matches
	1 matches
	example:
	  1: >   1  { b { c } d } e 
	  1:            ^ ^ ^ ^ ^   
	: q
SEE ALSOextend, mark, contains, qualifiers | ||||
| Return to index Manual Tutorial | (Last Updated: 7 May 2017) | |||