| Cobra | Inline Programming Language | Begin and End | ||
|---|---|---|---|---|
| NAMEBegin and End — first and last token in input sequenceSYNTAXBegin End first_t last_t DESCRIPTIONBegin is a token reference that points to the first lexical token in the part of the input sequence that was assigned to the current cpu. Similarly, End points to the last lexical token in that sequence.If executing single-core these are also the first and last tokens in the entire input stream. In multi-core mode, though, the part of the input sequence assigned to each cpu-core will be different. In that case the identifiers first_t and last_t can be used to refer to the first and last token in the entire input sequence, not just the part assigned to the current cpu-core. In single-core mode Begin and first_t point to the same token, as do End and last_t. EXAMPLES
	%{
		. = Begin;
		while (true)
		{	print .txt "\n";
			if (. == last_t)
			{	break;
			}
			. = .nxt;
		}
		Stop;
	%}
There are a few different ways to traverse the token sequence.
The default is to let Cobra do the scan and execute the inline
program once for each token in the sequence. For instance,
to count the number of identifiers:
	%{
		if (@ident)
		{	cnt++;
		}
	%}
	%{
		print "there are " cnt " identifiers\n";
		Stop;	# print result once, and stop
	%}
Equivalently, when taking over the scan itself:
	%{
		while (.seq <= End.seq)		# or while (. != End)
		{	if (@ident)
			{	cnt++;
			}
			. = .nxt;		# move to next token
		}
		print "there are " cnt " identifiers\n";
		Stop;				# done with all tokens
	%}
Similarly it is easy to traverse the sequence backwards, or in any
other order, using the token attributes .prv, .nxt, or .jmp (to jump
over token sequences enclosed in round, square, or curly braces).SEE ALSOcore, ncore, tokens. | ||||
| Return to Overview Manual Tutorial | (Last Updated: 26 July 2023) | |||