| Cobra | Inline Programming Language | Next | ||
|---|---|---|---|---|
| NAMENext, Stop, and related token processing functionsSYNTAXNext Stop DESCRIPTIONNext is a predefined action that causes the processing of the current token to terminate, and execution to move to the next token in the input sequence, where the program will start execution from the start.Stop is a predefined action that causes the processing of all tokens to terminate. The current inline program ends at this point and processing moves to the next query command. It is useful for setting or unsetting global variables, and for defining functions. There is an implied call to Next whenever the end of the inline program text is reached (the closing %} of the program). The use of Stop is illustrated by the following program that first collects data over the entire token sequence, and then prints the result with a single call: 
	%{
		# check the identifier length for all tokens
		# and remember the longest in q
		if (@ident && .len > q.len)
		{	q = .;
		}
	%}
	%{
		print "longest identifier: " q.txt " = " q.len " chars\n";
		Stop;	# stops the second run after the line is printed
	%}
We could in principle achieve the same effect with a single inline program
as follows:
	%{
		# check the identifier length for all tokens
		# and remember the longest in q
		if (@ident && .len > q.len)
		{	q = .;
		}
		if (. == End)
		{	print "longest identifier: " q.txt " = " q.len " chars\n";
		}
	%}
but at the expense that we now ask the program to check if we're at the end
of the token sequence once for every token visited. This is avoided if we
use the separate small program, with the Stop command to prevent
that part from also executing once for every token in the sequence.EXAMPLES
	%{
		function square(x)
		{
			return x*x;
		}
		unset ArrayX;
		Stop;	# do not repeat this action for every token
	%}
	%{
		if (.typ != "ident")
		{	Next;
		}
		# ... only tokens with type ident
		# ... will be processed
		# a Next action is implied here
	%}
Marking identifiers:
	%{
		if (!@ident)
		{	Next;
		}
		.mark++;
	%}
	= "Number of identifiers"
 | ||||
| Return to Overview Manual Tutorial | (Last Updated: 17 August 2023) | |||